unit_tests.cpp 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609
  1. /*
  2. * Copyright (c) 2012-2023 Daniele Bartolini et al.
  3. * SPDX-License-Identifier: MIT
  4. */
  5. #include "config.h"
  6. #if CROWN_BUILD_UNIT_TESTS
  7. #include "core/command_line.h"
  8. #include "core/containers/array.inl"
  9. #include "core/containers/hash_map.inl"
  10. #include "core/containers/hash_set.inl"
  11. #include "core/containers/vector.inl"
  12. #include "core/filesystem/path.h"
  13. #include "core/guid.inl"
  14. #include "core/json/json.h"
  15. #include "core/json/json_object.inl"
  16. #include "core/json/sjson.h"
  17. #include "core/math/aabb.inl"
  18. #include "core/math/color4.inl"
  19. #include "core/math/constants.h"
  20. #include "core/math/math.h"
  21. #include "core/math/matrix3x3.inl"
  22. #include "core/math/matrix4x4.inl"
  23. #include "core/math/quaternion.inl"
  24. #include "core/math/sphere.inl"
  25. #include "core/math/vector2.inl"
  26. #include "core/math/vector3.inl"
  27. #include "core/math/vector4.inl"
  28. #include "core/memory/memory.inl"
  29. #include "core/memory/temp_allocator.inl"
  30. #include "core/murmur.h"
  31. #include "core/os.h"
  32. #include "core/process.h"
  33. #include "core/strings/dynamic_string.inl"
  34. #include "core/strings/string.inl"
  35. #include "core/strings/string_id.inl"
  36. #include "core/strings/string_view.inl"
  37. #include "core/thread/thread.h"
  38. #include "core/time.h"
  39. #include <stdlib.h> // EXIT_SUCCESS, EXIT_FAILURE
  40. #include <stdio.h> // printf
  41. #undef CE_ASSERT
  42. #undef CE_ENSURE
  43. #undef CE_FATAL
  44. #define ENSURE(condition) \
  45. do \
  46. { \
  47. if (!(condition)) \
  48. { \
  49. printf("Assertion failed: '%s' in %s:%d\n\n" \
  50. , #condition \
  51. , __FILE__ \
  52. , __LINE__ \
  53. ); \
  54. exit(EXIT_FAILURE); \
  55. } \
  56. } \
  57. while (0)
  58. namespace crown
  59. {
  60. static void test_memory()
  61. {
  62. memory_globals::init();
  63. Allocator &a = default_allocator();
  64. void *p = a.allocate(32);
  65. ENSURE(a.allocated_size(p) >= 32);
  66. a.deallocate(p);
  67. memory_globals::shutdown();
  68. }
  69. static void test_array()
  70. {
  71. memory_globals::init();
  72. Allocator &a = default_allocator();
  73. {
  74. Array<int> v(a);
  75. ENSURE(array::size(v) == 0);
  76. array::push_back(v, 1);
  77. ENSURE(array::size(v) == 1);
  78. ENSURE(v[0] == 1);
  79. }
  80. memory_globals::shutdown();
  81. }
  82. static void test_vector()
  83. {
  84. memory_globals::init();
  85. Allocator &a = default_allocator();
  86. {
  87. Vector<int> v(a);
  88. ENSURE(vector::size(v) == 0);
  89. vector::push_back(v, 1);
  90. ENSURE(vector::size(v) == 1);
  91. ENSURE(v[0] == 1);
  92. }
  93. memory_globals::shutdown();
  94. }
  95. static void test_hash_map()
  96. {
  97. memory_globals::init();
  98. Allocator &a = default_allocator();
  99. {
  100. HashMap<s32, s32> m(a);
  101. ENSURE(hash_map::size(m) == 0);
  102. ENSURE(hash_map::get(m, 0, 42) == 42);
  103. ENSURE(!hash_map::has(m, 10));
  104. for (s32 i = 0; i < 100; ++i)
  105. hash_map::set(m, i, i*i);
  106. for (s32 i = 0; i < 100; ++i)
  107. ENSURE(hash_map::get(m, i, 0) == i*i);
  108. hash_map::remove(m, 20);
  109. ENSURE(!hash_map::has(m, 20));
  110. hash_map::remove(m, 2000);
  111. ENSURE(!hash_map::has(m, 2000));
  112. hash_map::remove(m, 50);
  113. ENSURE(!hash_map::has(m, 50));
  114. hash_map::clear(m);
  115. for (s32 i = 0; i < 100; ++i)
  116. ENSURE(!hash_map::has(m, i));
  117. }
  118. {
  119. HashMap<s32, s32> m(a);
  120. hash_map_internal::grow(m);
  121. ENSURE(hash_map::capacity(m) == 16);
  122. hash_map::set(m, 0, 7);
  123. hash_map::set(m, 1, 1);
  124. for (s32 i = 2; i < 150; ++i) {
  125. hash_map::set(m, i, 2);
  126. ENSURE(hash_map::has(m, 0));
  127. ENSURE(hash_map::has(m, 1));
  128. ENSURE(hash_map::has(m, i));
  129. hash_map::remove(m, i);
  130. }
  131. }
  132. {
  133. HashMap<s32, s32> ma(a);
  134. HashMap<s32, s32> mb(a);
  135. hash_map::set(ma, 0, 0);
  136. ma = mb;
  137. }
  138. memory_globals::shutdown();
  139. }
  140. static void test_hash_set()
  141. {
  142. memory_globals::init();
  143. Allocator &a = default_allocator();
  144. {
  145. HashSet<s32> m(a);
  146. ENSURE(hash_set::size(m) == 0);
  147. ENSURE(!hash_set::has(m, 10));
  148. for (s32 i = 0; i < 100; ++i)
  149. hash_set::insert(m, i*i);
  150. for (s32 i = 0; i < 100; ++i)
  151. ENSURE(hash_set::has(m, i*i));
  152. hash_set::remove(m, 5*5);
  153. ENSURE(!hash_set::has(m, 5*5));
  154. hash_set::remove(m, 80*80);
  155. ENSURE(!hash_set::has(m, 80*80));
  156. hash_set::remove(m, 40*40);
  157. ENSURE(!hash_set::has(m, 40*40));
  158. hash_set::clear(m);
  159. for (s32 i = 0; i < 100; ++i)
  160. ENSURE(!hash_set::has(m, i*i));
  161. }
  162. {
  163. HashSet<s32> ma(a);
  164. HashSet<s32> mb(a);
  165. hash_set::insert(ma, 0);
  166. ma = mb;
  167. }
  168. memory_globals::shutdown();
  169. }
  170. static void test_vector2()
  171. {
  172. {
  173. const Vector2 a = vector2(1.2f, 4.2f);
  174. const Vector2 b = vector2(2.7f, -1.9f);
  175. const Vector2 c = a - b;
  176. ENSURE(fequal(c.x, -1.5f, 0.0001f));
  177. ENSURE(fequal(c.y, 6.1f, 0.0001f));
  178. }
  179. {
  180. const Vector2 a = vector2(1.2f, 4.2f);
  181. const Vector2 b = vector2(2.7f, -1.9f);
  182. const Vector2 c = a + b;
  183. ENSURE(fequal(c.x, 3.9f, 0.0001f));
  184. ENSURE(fequal(c.y, 2.3f, 0.0001f));
  185. }
  186. {
  187. const Vector2 a = vector2(1.2f, 4.2f);
  188. const Vector2 b = a * 2.0f;
  189. ENSURE(fequal(b.x, 2.4f, 0.0001f));
  190. ENSURE(fequal(b.y, 8.4f, 0.0001f));
  191. }
  192. {
  193. const Vector2 a = vector2(1.2f, 4.2f);
  194. const Vector2 b = vector2(2.7f, -1.9f);
  195. const f32 c = dot(a, b);
  196. ENSURE(fequal(c, -4.74f, 0.0001f));
  197. }
  198. {
  199. const Vector2 a = vector2(1.2f, 4.2f);
  200. const f32 c = length_squared(a);
  201. ENSURE(fequal(c, 19.08f, 0.0001f));
  202. }
  203. {
  204. const Vector2 a = vector2(1.2f, 4.2f);
  205. const f32 c = length(a);
  206. ENSURE(fequal(c, 4.36806f, 0.0001f));
  207. }
  208. {
  209. Vector2 a = vector2(1.2f, 4.2f);
  210. normalize(a);
  211. ENSURE(fequal(length(a), 1.0f, 0.00001f));
  212. }
  213. {
  214. const Vector2 a = vector2(1.2f, 4.2f);
  215. const Vector2 b = vector2(2.7f, -1.9f);
  216. const float c = distance_squared(a, b);
  217. ENSURE(fequal(c, 39.46f, 0.00001f));
  218. }
  219. {
  220. const Vector2 a = vector2(1.2f, 4.2f);
  221. const Vector2 b = vector2(2.7f, -1.9f);
  222. const float c = distance(a, b);
  223. ENSURE(fequal(c, 6.28171f, 0.00001f));
  224. }
  225. {
  226. const Vector2 a = vector2(1.2f, 4.2f);
  227. const Vector2 b = vector2(2.7f, -1.9f);
  228. const Vector2 c = max(a, b);
  229. ENSURE(fequal(c.x, 2.7f, 0.00001f));
  230. ENSURE(fequal(c.y, 4.2f, 0.00001f));
  231. }
  232. {
  233. const Vector2 a = vector2(1.2f, 4.2f);
  234. const Vector2 b = vector2(2.7f, -1.9f);
  235. const Vector2 c = min(a, b);
  236. ENSURE(fequal(c.x, 1.2f, 0.00001f));
  237. ENSURE(fequal(c.y, -1.9f, 0.00001f));
  238. }
  239. }
  240. static void test_vector3()
  241. {
  242. {
  243. const Vector3 a = vector3(1.2f, 4.2f, -2.3f);
  244. const Vector3 b = vector3(2.7f, -1.9f, -4.1f);
  245. const Vector3 c = a - b;
  246. ENSURE(fequal(c.x, -1.5f, 0.0001f));
  247. ENSURE(fequal(c.y, 6.1f, 0.0001f));
  248. ENSURE(fequal(c.z, 1.8f, 0.0001f));
  249. }
  250. {
  251. const Vector3 a = vector3(1.2f, 4.2f, -2.3f);
  252. const Vector3 b = vector3(2.7f, -1.9f, -4.1f);
  253. const Vector3 c = a + b;
  254. ENSURE(fequal(c.x, 3.9f, 0.0001f));
  255. ENSURE(fequal(c.y, 2.3f, 0.0001f));
  256. ENSURE(fequal(c.z, -6.4f, 0.0001f));
  257. }
  258. {
  259. const Vector3 a = vector3(1.2f, 4.2f, -2.3f);
  260. const Vector3 b = a * 2.0f;
  261. ENSURE(fequal(b.x, 2.4f, 0.0001f));
  262. ENSURE(fequal(b.y, 8.4f, 0.0001f));
  263. ENSURE(fequal(b.z, -4.6f, 0.0001f));
  264. }
  265. {
  266. const Vector3 a = vector3(1.2f, 4.2f, -2.3f);
  267. const Vector3 b = vector3(2.7f, -1.9f, -4.1f);
  268. const f32 c = dot(a, b);
  269. ENSURE(fequal(c, 4.69f, 0.0001f));
  270. }
  271. {
  272. const Vector3 a = vector3(1.2f, 4.2f, -2.3f);
  273. const Vector3 b = vector3(2.7f, -1.9f, -4.1f);
  274. const Vector3 c = cross(a, b);
  275. ENSURE(fequal(c.x, -21.59f, 0.0001f));
  276. ENSURE(fequal(c.y, -1.29f, 0.0001f));
  277. ENSURE(fequal(c.z, -13.62f, 0.0001f));
  278. }
  279. {
  280. const Vector3 a = vector3(1.2f, 4.2f, -2.3f);
  281. const f32 c = length_squared(a);
  282. ENSURE(fequal(c, 24.37f, 0.0001f));
  283. }
  284. {
  285. const Vector3 a = vector3(1.2f, 4.2f, -2.3f);
  286. const f32 c = length(a);
  287. ENSURE(fequal(c, 4.93659f, 0.0001f));
  288. }
  289. {
  290. Vector3 a = vector3(1.2f, 4.2f, -2.3f);
  291. normalize(a);
  292. ENSURE(fequal(length(a), 1.0f, 0.00001f));
  293. }
  294. {
  295. const Vector3 a = vector3(1.2f, 4.2f, -2.3f);
  296. const Vector3 b = vector3(2.7f, -1.9f, -4.1f);
  297. const float c = distance_squared(a, b);
  298. ENSURE(fequal(c, 42.70f, 0.00001f));
  299. }
  300. {
  301. const Vector3 a = vector3(1.2f, 4.2f, -2.3f);
  302. const Vector3 b = vector3(2.7f, -1.9f, -4.1f);
  303. const float c = distance(a, b);
  304. ENSURE(fequal(c, 6.53452f, 0.00001f));
  305. }
  306. {
  307. const Vector3 a = vector3(1.2f, 4.2f, -2.3f);
  308. const Vector3 b = vector3(2.7f, -1.9f, -4.1f);
  309. const Vector3 c = max(a, b);
  310. ENSURE(fequal(c.x, 2.7f, 0.00001f));
  311. ENSURE(fequal(c.y, 4.2f, 0.00001f));
  312. ENSURE(fequal(c.z, -2.3f, 0.00001f));
  313. }
  314. {
  315. const Vector3 a = vector3(1.2f, 4.2f, -2.3f);
  316. const Vector3 b = vector3(2.7f, -1.9f, -4.1f);
  317. const Vector3 c = min(a, b);
  318. ENSURE(fequal(c.x, 1.2f, 0.00001f));
  319. ENSURE(fequal(c.y, -1.9f, 0.00001f));
  320. ENSURE(fequal(c.z, -4.1f, 0.00001f));
  321. }
  322. }
  323. static void test_vector4()
  324. {
  325. {
  326. const Vector4 a = vector4(1.2f, 4.2f, -2.3f, 5.5f);
  327. const Vector4 b = vector4(2.7f, -1.9f, -4.1f, 1.0f);
  328. const Vector4 c = a - b;
  329. ENSURE(fequal(c.x, -1.5f, 0.0001f));
  330. ENSURE(fequal(c.y, 6.1f, 0.0001f));
  331. ENSURE(fequal(c.z, 1.8f, 0.0001f));
  332. ENSURE(fequal(c.w, 4.5f, 0.0001f));
  333. }
  334. {
  335. const Vector4 a = vector4(1.2f, 4.2f, -2.3f, 5.5f);
  336. const Vector4 b = vector4(2.7f, -1.9f, -4.1f, 1.0f);
  337. const Vector4 c = a + b;
  338. ENSURE(fequal(c.x, 3.9f, 0.0001f));
  339. ENSURE(fequal(c.y, 2.3f, 0.0001f));
  340. ENSURE(fequal(c.z, -6.4f, 0.0001f));
  341. ENSURE(fequal(c.w, 6.5f, 0.0001f));
  342. }
  343. {
  344. const Vector4 a = vector4(1.2f, 4.2f, -2.3f, 1.5f);
  345. const Vector4 b = a * 2.0f;
  346. ENSURE(fequal(b.x, 2.4f, 0.0001f));
  347. ENSURE(fequal(b.y, 8.4f, 0.0001f));
  348. ENSURE(fequal(b.z, -4.6f, 0.0001f));
  349. ENSURE(fequal(b.w, 3.0f, 0.0001f));
  350. }
  351. {
  352. const Vector4 a = vector4(1.2f, 4.2f, -2.3f, 5.5f);
  353. const Vector4 b = vector4(2.7f, -1.9f, -4.1f, 1.0f);
  354. const f32 c = dot(a, b);
  355. ENSURE(fequal(c, 10.19f, 0.0001f));
  356. }
  357. {
  358. const Vector4 a = vector4(1.2f, 4.2f, -2.3f, 5.5f);
  359. const f32 c = length_squared(a);
  360. ENSURE(fequal(c, 54.62f, 0.0001f));
  361. }
  362. {
  363. const Vector4 a = vector4(1.2f, 4.2f, -2.3f, 5.5f);
  364. const f32 c = length(a);
  365. ENSURE(fequal(c, 7.39053f, 0.0001f));
  366. }
  367. {
  368. Vector4 a = vector4(1.2f, 4.2f, -2.3f, 5.5f);
  369. normalize(a);
  370. ENSURE(fequal(length(a), 1.0f, 0.00001f));
  371. }
  372. {
  373. const Vector4 a = vector4(1.2f, 4.2f, -2.3f, 5.5f);
  374. const Vector4 b = vector4(2.7f, -1.9f, -4.1f, 1.0f);
  375. const float c = distance_squared(a, b);
  376. ENSURE(fequal(c, 62.95f, 0.00001f));
  377. }
  378. {
  379. const Vector4 a = vector4(1.2f, 4.2f, -2.3f, 5.5f);
  380. const Vector4 b = vector4(2.7f, -1.9f, -4.1f, 1.0f);
  381. const float c = distance(a, b);
  382. ENSURE(fequal(c, 7.93410f, 0.00001f));
  383. }
  384. {
  385. const Vector4 a = vector4(1.2f, 4.2f, -2.3f, 5.5f);
  386. const Vector4 b = vector4(2.7f, -1.9f, -4.1f, 1.0f);
  387. const Vector4 c = max(a, b);
  388. ENSURE(fequal(c.x, 2.7f, 0.00001f));
  389. ENSURE(fequal(c.y, 4.2f, 0.00001f));
  390. ENSURE(fequal(c.z, -2.3f, 0.00001f));
  391. ENSURE(fequal(c.w, 5.5f, 0.00001f));
  392. }
  393. {
  394. const Vector4 a = vector4(1.2f, 4.2f, -2.3f, 5.5f);
  395. const Vector4 b = vector4(2.7f, -1.9f, -4.1f, 1.0f);
  396. const Vector4 c = min(a, b);
  397. ENSURE(fequal(c.x, 1.2f, 0.00001f));
  398. ENSURE(fequal(c.y, -1.9f, 0.00001f));
  399. ENSURE(fequal(c.z, -4.1f, 0.00001f));
  400. ENSURE(fequal(c.w, 1.0f, 0.00001f));
  401. }
  402. }
  403. static void test_quaternion()
  404. {
  405. {
  406. const Quaternion a = from_elements(0.0f, 0.0f, 0.0f, 1.0f);
  407. ENSURE(fequal(a.x, 0.0f, 0.00001f));
  408. ENSURE(fequal(a.y, 0.0f, 0.00001f));
  409. ENSURE(fequal(a.z, 0.0f, 0.00001f));
  410. ENSURE(fequal(a.w, 1.0f, 0.00001f));
  411. }
  412. }
  413. static void test_color4()
  414. {
  415. {
  416. const Color4 a = color4(1.3f, 2.6f, 0.2f, 0.6f);
  417. ENSURE(fequal(a.x, 1.3f, 0.00001f));
  418. ENSURE(fequal(a.y, 2.6f, 0.00001f));
  419. ENSURE(fequal(a.z, 0.2f, 0.00001f));
  420. ENSURE(fequal(a.w, 0.6f, 0.00001f));
  421. }
  422. {
  423. const Color4 a = from_rgba(63, 231, 12, 98);
  424. ENSURE(fequal(a.x, 0.24705f, 0.00001f));
  425. ENSURE(fequal(a.y, 0.90588f, 0.00001f));
  426. ENSURE(fequal(a.z, 0.04705f, 0.00001f));
  427. ENSURE(fequal(a.w, 0.38431f, 0.00001f));
  428. }
  429. {
  430. const Color4 a = from_rgb(63, 231, 12);
  431. ENSURE(fequal(a.x, 0.24705f, 0.00001f));
  432. ENSURE(fequal(a.y, 0.90588f, 0.00001f));
  433. ENSURE(fequal(a.z, 0.04705f, 0.00001f));
  434. ENSURE(fequal(a.w, 1.0f, 0.00001f));
  435. }
  436. {
  437. const Color4 a = from_rgba(0x3fe70c62);
  438. ENSURE(fequal(a.x, 0.24705f, 0.00001f));
  439. ENSURE(fequal(a.y, 0.90588f, 0.00001f));
  440. ENSURE(fequal(a.z, 0.04705f, 0.00001f));
  441. ENSURE(fequal(a.w, 0.38431f, 0.00001f));
  442. }
  443. {
  444. const Color4 a = from_rgba(63, 231, 12, 98);
  445. const u32 rgba = to_rgba(a);
  446. ENSURE(rgba == 0x3fe70c62);
  447. const u32 rgb = to_rgb(a);
  448. ENSURE(rgb == 0x3fe70cff);
  449. const u32 bgr = to_bgr(a);
  450. ENSURE(bgr == 0xff0ce73f);
  451. const u32 abgr = to_abgr(a);
  452. ENSURE(abgr == 0x620ce73f);
  453. }
  454. }
  455. static void test_matrix3x3()
  456. {
  457. {
  458. const Matrix3x3 a = from_elements(1.2f, -2.3f, 5.1f
  459. , 2.2f, -5.1f, 1.1f
  460. , 3.2f, 3.3f, -3.8f
  461. );
  462. const Matrix3x3 b = from_elements(3.2f, 4.8f, 6.0f
  463. , -1.6f, -7.1f, -2.4f
  464. , -3.1f, -2.2f, 8.9f
  465. );
  466. const Matrix3x3 c = a + b;
  467. ENSURE(fequal(c.x.x, 4.4f, 0.00001f));
  468. ENSURE(fequal(c.x.y, 2.5f, 0.00001f));
  469. ENSURE(fequal(c.x.z, 11.1f, 0.00001f));
  470. ENSURE(fequal(c.y.x, 0.6f, 0.00001f));
  471. ENSURE(fequal(c.y.y, -12.2f, 0.00001f));
  472. ENSURE(fequal(c.y.z, -1.3f, 0.00001f));
  473. ENSURE(fequal(c.z.x, 0.1f, 0.00001f));
  474. ENSURE(fequal(c.z.y, 1.1f, 0.00001f));
  475. ENSURE(fequal(c.z.z, 5.1f, 0.00001f));
  476. }
  477. {
  478. const Matrix3x3 a = from_elements(1.2f, -2.3f, 5.1f
  479. , 2.2f, -5.1f, 1.1f
  480. , 3.2f, 3.3f, -3.8f
  481. );
  482. const Matrix3x3 b = from_elements(3.2f, 4.8f, 6.0f
  483. , -1.6f, -7.1f, -2.4f
  484. , -3.1f, -2.2f, 8.9f
  485. );
  486. const Matrix3x3 c = a - b;
  487. ENSURE(fequal(c.x.x, -2.0f, 0.00001f));
  488. ENSURE(fequal(c.x.y, -7.1f, 0.00001f));
  489. ENSURE(fequal(c.x.z, -0.9f, 0.00001f));
  490. ENSURE(fequal(c.y.x, 3.8f, 0.00001f));
  491. ENSURE(fequal(c.y.y, 2.0f, 0.00001f));
  492. ENSURE(fequal(c.y.z, 3.5f, 0.00001f));
  493. ENSURE(fequal(c.z.x, 6.3f, 0.00001f));
  494. ENSURE(fequal(c.z.y, 5.5f, 0.00001f));
  495. ENSURE(fequal(c.z.z, -12.7f, 0.00001f));
  496. }
  497. {
  498. const Matrix3x3 a = from_elements(1.2f, -2.3f, 5.1f
  499. , 2.2f, -5.1f, 1.1f
  500. , 3.2f, 3.3f, -3.8f
  501. );
  502. const Matrix3x3 b = from_elements(3.2f, 4.8f, 6.0f
  503. , -1.6f, -7.1f, -2.4f
  504. , -3.1f, -2.2f, 8.9f
  505. );
  506. const Matrix3x3 c = a * b;
  507. ENSURE(fequal(c.x.x, -8.29f, 0.00001f));
  508. ENSURE(fequal(c.x.y, 10.87f, 0.00001f));
  509. ENSURE(fequal(c.x.z, 58.11f, 0.00001f));
  510. ENSURE(fequal(c.y.x, 11.79f, 0.00001f));
  511. ENSURE(fequal(c.y.y, 44.35f, 0.00001f));
  512. ENSURE(fequal(c.y.z, 35.23f, 0.00001f));
  513. ENSURE(fequal(c.z.x, 16.74f, 0.00001f));
  514. ENSURE(fequal(c.z.y, 0.29f, 0.00001f));
  515. ENSURE(fequal(c.z.z, -22.54f, 0.00001f));
  516. }
  517. {
  518. const Matrix3x3 a = from_elements(1.2f, -2.3f, 5.1f
  519. , 2.2f, -5.1f, 1.1f
  520. , 3.2f, 3.3f, -3.8f
  521. );
  522. const Matrix3x3 b = get_inverted(a);
  523. ENSURE(fequal(b.x.x, 0.140833f, 0.00001f));
  524. ENSURE(fequal(b.x.y, 0.072339f, 0.00001f));
  525. ENSURE(fequal(b.x.z, 0.209954f, 0.00001f));
  526. ENSURE(fequal(b.y.x, 0.106228f, 0.00001f));
  527. ENSURE(fequal(b.y.y, -0.186705f, 0.00001f));
  528. ENSURE(fequal(b.y.z, 0.088524f, 0.00001f));
  529. ENSURE(fequal(b.z.x, 0.210848f, 0.00001f));
  530. ENSURE(fequal(b.z.y, -0.101221f, 0.00001f));
  531. ENSURE(fequal(b.z.z, -0.009478f, 0.00001f));
  532. }
  533. {
  534. const Matrix3x3 a = from_elements(1.2f, -2.3f, 5.1f
  535. , 2.2f, -5.1f, 1.1f
  536. , 3.2f, 3.3f, -3.8f
  537. );
  538. const Matrix3x3 b = get_transposed(a);
  539. ENSURE(fequal(b.x.x, 1.2f, 0.00001f));
  540. ENSURE(fequal(b.x.y, 2.2f, 0.00001f));
  541. ENSURE(fequal(b.x.z, 3.2f, 0.00001f));
  542. ENSURE(fequal(b.y.x, -2.3f, 0.00001f));
  543. ENSURE(fequal(b.y.y, -5.1f, 0.00001f));
  544. ENSURE(fequal(b.y.z, 3.3f, 0.00001f));
  545. ENSURE(fequal(b.z.x, 5.1f, 0.00001f));
  546. ENSURE(fequal(b.z.y, 1.1f, 0.00001f));
  547. ENSURE(fequal(b.z.z, -3.8f, 0.00001f));
  548. }
  549. }
  550. static void test_matrix4x4()
  551. {
  552. {
  553. const Matrix4x4 a = from_elements(1.2f, -2.3f, 5.1f, -1.2f
  554. , 2.2f, -5.1f, 1.1f, -7.4f
  555. , 3.2f, 3.3f, -3.8f, -9.2f
  556. , -6.8f, -2.9f, 1.0f, 4.9f
  557. );
  558. const Matrix4x4 b = from_elements(3.2f, 4.8f, 6.0f, 5.3f
  559. , -1.6f, -7.1f, -2.4f, -6.2f
  560. , -3.1f, -2.2f, 8.9f, 8.3f
  561. , 3.8f, 9.1f, -3.1f, -7.1f
  562. );
  563. const Matrix4x4 c = a + b;
  564. ENSURE(fequal(c.x.x, 4.4f, 0.00001f));
  565. ENSURE(fequal(c.x.y, 2.5f, 0.00001f));
  566. ENSURE(fequal(c.x.z, 11.1f, 0.00001f));
  567. ENSURE(fequal(c.x.w, 4.1f, 0.00001f));
  568. ENSURE(fequal(c.y.x, 0.6f, 0.00001f));
  569. ENSURE(fequal(c.y.y, -12.2f, 0.00001f));
  570. ENSURE(fequal(c.y.z, -1.3f, 0.00001f));
  571. ENSURE(fequal(c.y.w, -13.6f, 0.00001f));
  572. ENSURE(fequal(c.z.x, 0.1f, 0.00001f));
  573. ENSURE(fequal(c.z.y, 1.1f, 0.00001f));
  574. ENSURE(fequal(c.z.z, 5.1f, 0.00001f));
  575. ENSURE(fequal(c.z.w, -0.9f, 0.00001f));
  576. ENSURE(fequal(c.t.x, -3.0f, 0.00001f));
  577. ENSURE(fequal(c.t.y, 6.2f, 0.00001f));
  578. ENSURE(fequal(c.t.z, -2.1f, 0.00001f));
  579. ENSURE(fequal(c.t.w, -2.2f, 0.00001f));
  580. }
  581. {
  582. const Matrix4x4 a = from_elements(1.2f, -2.3f, 5.1f, -1.2f
  583. , 2.2f, -5.1f, 1.1f, -7.4f
  584. , 3.2f, 3.3f, -3.8f, -9.2f
  585. , -6.8f, -2.9f, 1.0f, 4.9f
  586. );
  587. const Matrix4x4 b = from_elements(3.2f, 4.8f, 6.0f, 5.3f
  588. , -1.6f, -7.1f, -2.4f, -6.2f
  589. , -3.1f, -2.2f, 8.9f, 8.3f
  590. , 3.8f, 9.1f, -3.1f, -7.1f
  591. );
  592. const Matrix4x4 c = a - b;
  593. ENSURE(fequal(c.x.x, -2.0f, 0.00001f));
  594. ENSURE(fequal(c.x.y, -7.1f, 0.00001f));
  595. ENSURE(fequal(c.x.z, -0.9f, 0.00001f));
  596. ENSURE(fequal(c.x.w, -6.5f, 0.00001f));
  597. ENSURE(fequal(c.y.x, 3.8f, 0.00001f));
  598. ENSURE(fequal(c.y.y, 2.0f, 0.00001f));
  599. ENSURE(fequal(c.y.z, 3.5f, 0.00001f));
  600. ENSURE(fequal(c.y.w, -1.2f, 0.00001f));
  601. ENSURE(fequal(c.z.x, 6.3f, 0.00001f));
  602. ENSURE(fequal(c.z.y, 5.5f, 0.00001f));
  603. ENSURE(fequal(c.z.z, -12.7f, 0.00001f));
  604. ENSURE(fequal(c.z.w, -17.5f, 0.00001f));
  605. ENSURE(fequal(c.t.x, -10.6f, 0.00001f));
  606. ENSURE(fequal(c.t.y, -12.0f, 0.00001f));
  607. ENSURE(fequal(c.t.z, 4.1f, 0.00001f));
  608. ENSURE(fequal(c.t.w, 12.0f, 0.00001f));
  609. }
  610. {
  611. const Matrix4x4 a = from_elements(1.2f, -2.3f, 5.1f, -1.2f
  612. , 2.2f, -5.1f, 1.1f, -7.4f
  613. , 3.2f, 3.3f, -3.8f, -9.2f
  614. , -6.8f, -2.9f, 1.0f, 4.9f
  615. );
  616. const Matrix4x4 b = from_elements(3.2f, 4.8f, 6.0f, 5.3f
  617. , -1.6f, -7.1f, -2.4f, -6.2f
  618. , -3.1f, -2.2f, 8.9f, 8.3f
  619. , 3.8f, 9.1f, -3.1f, -7.1f
  620. );
  621. const Matrix4x4 c = a * b;
  622. ENSURE(fequal(c.x.x, -12.85f, 0.00001f));
  623. ENSURE(fequal(c.x.y, -0.05f, 0.00001f));
  624. ENSURE(fequal(c.x.z, 61.83f, 0.00001f));
  625. ENSURE(fequal(c.x.w, 71.47f, 0.00001f));
  626. ENSURE(fequal(c.y.x, -16.33f, 0.00001f));
  627. ENSURE(fequal(c.y.y, -22.99f, 0.00001f));
  628. ENSURE(fequal(c.y.z, 58.17f, 0.00001f));
  629. ENSURE(fequal(c.y.w, 104.95f, 0.00001f));
  630. ENSURE(fequal(c.z.x, -18.22f, 0.00001f));
  631. ENSURE(fequal(c.z.y, -83.43f, 0.00001f));
  632. ENSURE(fequal(c.z.z, 5.98f, 0.00001f));
  633. ENSURE(fequal(c.z.w, 30.28f, 0.00001f));
  634. ENSURE(fequal(c.t.x, -1.60f, 0.00001f));
  635. ENSURE(fequal(c.t.y, 30.34f, 0.00001f));
  636. ENSURE(fequal(c.t.z, -40.13f, 0.00001f));
  637. ENSURE(fequal(c.t.w, -44.55f, 0.00001f));
  638. }
  639. {
  640. const Matrix4x4 a = from_elements(1.2f, -2.3f, 5.1f, -1.2f
  641. , 2.2f, -5.1f, 1.1f, -7.4f
  642. , 3.2f, 3.3f, -3.8f, -9.2f
  643. , -6.8f, -2.9f, 1.0f, 4.9f
  644. );
  645. const Matrix4x4 b = get_inverted(a);
  646. ENSURE(fequal(b.x.x, -0.08464f, 0.00001f));
  647. ENSURE(fequal(b.x.y, 0.06129f, 0.00001f));
  648. ENSURE(fequal(b.x.z, -0.15210f, 0.00001f));
  649. ENSURE(fequal(b.x.w, -0.21374f, 0.00001f));
  650. ENSURE(fequal(b.y.x, 0.14384f, 0.00001f));
  651. ENSURE(fequal(b.y.y, -0.18486f, 0.00001f));
  652. ENSURE(fequal(b.y.z, 0.14892f, 0.00001f));
  653. ENSURE(fequal(b.y.w, 0.03565f, 0.00001f));
  654. ENSURE(fequal(b.z.x, 0.26073f, 0.00001f));
  655. ENSURE(fequal(b.z.y, -0.09877f, 0.00001f));
  656. ENSURE(fequal(b.z.z, 0.07063f, 0.00001f));
  657. ENSURE(fequal(b.z.w, 0.04729f, 0.00001f));
  658. ENSURE(fequal(b.t.x, -0.08553f, 0.00001f));
  659. ENSURE(fequal(b.t.y, -0.00419f, 0.00001f));
  660. ENSURE(fequal(b.t.z, -0.13735f, 0.00001f));
  661. ENSURE(fequal(b.t.w, -0.08108f, 0.00001f));
  662. }
  663. {
  664. const Matrix4x4 a = from_elements(1.2f, -2.3f, 5.1f, -1.2f
  665. , 2.2f, -5.1f, 1.1f, -7.4f
  666. , 3.2f, 3.3f, -3.8f, -9.2f
  667. , -6.8f, -2.9f, 1.0f, 4.9f
  668. );
  669. const Matrix4x4 b = get_transposed(a);
  670. ENSURE(fequal(b.x.x, 1.2f, 0.00001f));
  671. ENSURE(fequal(b.x.y, 2.2f, 0.00001f));
  672. ENSURE(fequal(b.x.z, 3.2f, 0.00001f));
  673. ENSURE(fequal(b.x.w, -6.8f, 0.00001f));
  674. ENSURE(fequal(b.y.x, -2.3f, 0.00001f));
  675. ENSURE(fequal(b.y.y, -5.1f, 0.00001f));
  676. ENSURE(fequal(b.y.z, 3.3f, 0.00001f));
  677. ENSURE(fequal(b.y.w, -2.9f, 0.00001f));
  678. ENSURE(fequal(b.z.x, 5.1f, 0.00001f));
  679. ENSURE(fequal(b.z.y, 1.1f, 0.00001f));
  680. ENSURE(fequal(b.z.z, -3.8f, 0.00001f));
  681. ENSURE(fequal(b.z.w, 1.0f, 0.00001f));
  682. ENSURE(fequal(b.t.x, -1.2f, 0.00001f));
  683. ENSURE(fequal(b.t.y, -7.4f, 0.00001f));
  684. ENSURE(fequal(b.t.z, -9.2f, 0.00001f));
  685. ENSURE(fequal(b.t.w, 4.9f, 0.00001f));
  686. }
  687. {
  688. Matrix4x4 m;
  689. Quaternion q;
  690. m = MATRIX4X4_IDENTITY;
  691. m.x.x = 0.0f;
  692. q = rotation(m);
  693. ENSURE(memcmp(&q, &QUATERNION_IDENTITY, sizeof(q)) == 0);
  694. m = MATRIX4X4_IDENTITY;
  695. m.y.y = 0.0f;
  696. q = rotation(m);
  697. ENSURE(memcmp(&q, &QUATERNION_IDENTITY, sizeof(q)) == 0);
  698. m = MATRIX4X4_IDENTITY;
  699. m.z.z = 0.0f;
  700. q = rotation(m);
  701. ENSURE(memcmp(&q, &QUATERNION_IDENTITY, sizeof(q)) == 0);
  702. }
  703. }
  704. static void test_aabb()
  705. {
  706. {
  707. AABB a;
  708. aabb::reset(a);
  709. ENSURE(a.min == VECTOR3_ZERO);
  710. ENSURE(a.max == VECTOR3_ZERO);
  711. }
  712. {
  713. AABB a;
  714. a.min = vector3(-2.3f, 1.2f, -4.5f);
  715. a.max = vector3(3.7f, 5.3f, -2.9f);
  716. const Vector3 c = aabb::center(a);
  717. ENSURE(fequal(c.x, 0.70f, 0.00001f));
  718. ENSURE(fequal(c.y, 3.25f, 0.00001f));
  719. ENSURE(fequal(c.z, -3.70f, 0.00001f));
  720. }
  721. {
  722. AABB a;
  723. a.min = vector3(-2.3f, 1.2f, -4.5f);
  724. a.max = vector3(3.7f, 5.3f, -2.9f);
  725. const float c = aabb::volume(a);
  726. ENSURE(fequal(c, 39.36f, 0.00001f));
  727. }
  728. {
  729. const Vector3 points[] =
  730. {
  731. { -1.2f, 3.4f, 5.5f },
  732. { 8.2f, -2.4f, -1.5f },
  733. { -5.9f, 9.2f, 6.0f }
  734. };
  735. AABB a;
  736. aabb::from_points(a, countof(points), points);
  737. ENSURE(fequal(a.min.x, -5.9f, 0.00001f));
  738. ENSURE(fequal(a.min.y, -2.4f, 0.00001f));
  739. ENSURE(fequal(a.min.z, -1.5f, 0.00001f));
  740. ENSURE(fequal(a.max.x, 8.2f, 0.00001f));
  741. ENSURE(fequal(a.max.y, 9.2f, 0.00001f));
  742. ENSURE(fequal(a.max.z, 6.0f, 0.00001f));
  743. }
  744. {
  745. const Vector3 points[] =
  746. {
  747. { -1.2f, 3.4f, 5.5f },
  748. { 8.2f, -2.4f, -1.5f },
  749. { -5.9f, 9.2f, 6.0f },
  750. { -2.8f, -3.5f, 1.9f },
  751. { -8.3f, -3.1f, 1.9f },
  752. { 4.0f, -3.9f, -1.4f },
  753. { -0.4f, -1.8f, -2.2f },
  754. { -8.6f, -4.8f, 2.8f },
  755. { 4.1f, 4.7f, -0.4f }
  756. };
  757. AABB boxes[3];
  758. aabb::from_points(boxes[0], countof(points)/3, &points[0]);
  759. aabb::from_points(boxes[1], countof(points)/3, &points[3]);
  760. aabb::from_points(boxes[2], countof(points)/3, &points[6]);
  761. AABB d;
  762. aabb::from_boxes(d, countof(boxes), boxes);
  763. ENSURE(fequal(d.min.x, -8.6f, 0.00001f));
  764. ENSURE(fequal(d.min.y, -4.8f, 0.00001f));
  765. ENSURE(fequal(d.min.z, -2.2f, 0.00001f));
  766. ENSURE(fequal(d.max.x, 8.2f, 0.00001f));
  767. ENSURE(fequal(d.max.y, 9.2f, 0.00001f));
  768. ENSURE(fequal(d.max.z, 6.0f, 0.00001f));
  769. }
  770. {
  771. AABB a;
  772. a.min = vector3(-2.3f, 1.2f, -4.5f);
  773. a.max = vector3(3.7f, 5.3f, -2.9f);
  774. ENSURE(aabb::contains_point(a, vector3(1.2f, 3.0f, -4.4f)));
  775. ENSURE(!aabb::contains_point(a, vector3(3.8f, 3.0f, -4.4f)));
  776. ENSURE(!aabb::contains_point(a, vector3(1.2f, -1.0f, -4.4f)));
  777. ENSURE(!aabb::contains_point(a, vector3(1.2f, 3.0f, -4.6f)));
  778. }
  779. }
  780. static void test_sphere()
  781. {
  782. {
  783. Sphere a;
  784. sphere::reset(a);
  785. ENSURE(a.c == VECTOR3_ZERO);
  786. ENSURE(fequal(a.r, 0.0f, 0.00001f));
  787. }
  788. {
  789. Sphere a;
  790. a.c = VECTOR3_ZERO;
  791. a.r = 1.61f;
  792. const float b = sphere::volume(a);
  793. ENSURE(fequal(b, 17.48099f, 0.00001f));
  794. }
  795. {
  796. Sphere a;
  797. sphere::reset(a);
  798. const Vector3 points[] =
  799. {
  800. { -1.2f, 3.4f, 5.5f },
  801. { 8.2f, -2.4f, -1.5f },
  802. { -5.9f, 9.2f, 6.0f }
  803. };
  804. sphere::add_points(a, countof(points), points);
  805. ENSURE(fequal(a.c.x, 0.0f, 0.00001f));
  806. ENSURE(fequal(a.c.y, 0.0f, 0.00001f));
  807. ENSURE(fequal(a.c.z, 0.0f, 0.00001f));
  808. ENSURE(fequal(a.r, 12.46795f, 0.00001f));
  809. }
  810. {
  811. Sphere spheres[3];
  812. sphere::reset(spheres[0]);
  813. sphere::reset(spheres[1]);
  814. sphere::reset(spheres[2]);
  815. const Vector3 points[] =
  816. {
  817. { 6.6f, 3.5f, -5.7f },
  818. { -5.3f, -9.1f, -7.9f },
  819. { -1.5f, 4.4f, -5.8f },
  820. { 7.2f, -2.4f, -9.5f },
  821. { 4.0f, -8.1f, 6.6f },
  822. { -8.2f, 2.2f, 4.6f },
  823. { 2.9f, -4.8f, -6.8f },
  824. { -7.6f, -7.0f, 0.8f },
  825. { 8.2f, 2.8f, -4.8f }
  826. };
  827. sphere::add_points(spheres[0], countof(points)/3, &points[0]);
  828. sphere::add_points(spheres[1], countof(points)/3, &points[3]);
  829. sphere::add_points(spheres[2], countof(points)/3, &points[6]);
  830. Sphere d;
  831. sphere::reset(d);
  832. sphere::add_spheres(d, countof(spheres), spheres);
  833. ENSURE(fequal(d.r, 13.16472f, 0.00001f));
  834. }
  835. {
  836. Sphere a;
  837. a.c = vector3(-2.3f, 1.2f, -4.5f);
  838. a.r = 1.0f;
  839. ENSURE(sphere::contains_point(a, vector3(-2.9f, 1.6f, -4.0f)));
  840. ENSURE(!sphere::contains_point(a, vector3(-3.9f, 1.6f, -4.0f)));
  841. ENSURE(!sphere::contains_point(a, vector3(-2.9f, 2.6f, -4.0f)));
  842. ENSURE(!sphere::contains_point(a, vector3(-2.9f, 1.6f, -6.0f)));
  843. }
  844. }
  845. static void test_murmur()
  846. {
  847. const u32 m = murmur32("murmur32", 8, 0);
  848. ENSURE(m == 0x7c2365dbu);
  849. const u64 n = murmur64("murmur64", 8, 0);
  850. ENSURE(n == 0x90631502d1a3432bu);
  851. }
  852. static void test_string_id()
  853. {
  854. memory_globals::init();
  855. {
  856. StringId32 a("murmur32");
  857. ENSURE(a._id == 0x7c2365dbu);
  858. StringId32 b("murmur32", 8);
  859. ENSURE(b._id == 0x7c2365dbu);
  860. char str[9];
  861. a.to_string(str, sizeof(str));
  862. ENSURE(strcmp(str, "7c2365db") == 0);
  863. }
  864. {
  865. StringId64 a("murmur64");
  866. ENSURE(a._id == 0x90631502d1a3432bu);
  867. StringId64 b("murmur64", 8);
  868. ENSURE(b._id == 0x90631502d1a3432bu);
  869. char str[17];
  870. a.to_string(str, sizeof(str));
  871. ENSURE(strcmp(str, "90631502d1a3432b") == 0);
  872. }
  873. {
  874. StringId32 id(0x2dd65fa6u);
  875. char str[9];
  876. id.to_string(str, sizeof(str));
  877. StringId32 parsed;
  878. parsed.parse(str);
  879. ENSURE(id == parsed);
  880. }
  881. {
  882. StringId64 id(0xa73491922dd65fa6u);
  883. char str[17];
  884. id.to_string(str, sizeof(str));
  885. StringId64 parsed;
  886. parsed.parse(str);
  887. ENSURE(id == parsed);
  888. }
  889. memory_globals::shutdown();
  890. }
  891. static void test_dynamic_string()
  892. {
  893. memory_globals::init();
  894. {
  895. TempAllocator1024 ta;
  896. DynamicString str(ta);
  897. ENSURE(str.empty());
  898. str.set("murmur32", 8);
  899. ENSURE(str.length() == 8);
  900. const StringId32 id = str.to_string_id();
  901. ENSURE(id._id == 0x7c2365dbu);
  902. }
  903. {
  904. TempAllocator1024 ta;
  905. DynamicString str(ta);
  906. str += "Test ";
  907. str += "string.";
  908. ENSURE(strcmp(str.c_str(), "Test string.") == 0);
  909. }
  910. {
  911. TempAllocator1024 ta;
  912. DynamicString str(ta);
  913. str.set(" \tSushi\t ", 13);
  914. str.ltrim();
  915. ENSURE(strcmp(str.c_str(), "Sushi\t ") == 0);
  916. }
  917. {
  918. TempAllocator1024 ta;
  919. DynamicString str(ta);
  920. str.set(" \tSushi\t ", 13);
  921. str.rtrim();
  922. ENSURE(strcmp(str.c_str(), " \tSushi") == 0);
  923. }
  924. {
  925. TempAllocator1024 ta;
  926. DynamicString str(ta);
  927. str.set(" \tSushi\t ", 13);
  928. str.trim();
  929. ENSURE(strcmp(str.c_str(), "Sushi") == 0);
  930. }
  931. {
  932. TempAllocator1024 ta;
  933. DynamicString str(ta);
  934. str.set("Hello everyone!", 15);
  935. ENSURE(str.has_prefix("Hello"));
  936. ENSURE(!str.has_prefix("Helloo"));
  937. ENSURE(str.has_suffix("one!"));
  938. ENSURE(!str.has_suffix("one"));
  939. ENSURE(!str.has_prefix("Hello everyone!!!"));
  940. ENSURE(!str.has_suffix("Hello everyone!!!"));
  941. }
  942. {
  943. Guid guid = guid::parse("dd733419-bbd0-4248-bc84-e0e8363d7165");
  944. TempAllocator128 ta;
  945. DynamicString str(ta);
  946. str.from_guid(guid);
  947. ENSURE(str.length() == 36);
  948. ENSURE(strcmp(str.c_str(), "dd733419-bbd0-4248-bc84-e0e8363d7165") == 0);
  949. }
  950. {
  951. StringId32 id = StringId32("test");
  952. TempAllocator128 ta;
  953. DynamicString str(ta);
  954. str.from_string_id(id);
  955. ENSURE(str.length() == 8);
  956. ENSURE(strcmp(str.c_str(), "1812752e") == 0);
  957. }
  958. {
  959. StringId64 id = StringId64("test");
  960. TempAllocator128 ta;
  961. DynamicString str(ta);
  962. str.from_string_id(id);
  963. ENSURE(str.length() == 16);
  964. ENSURE(strcmp(str.c_str(), "2f4a8724618f4c63") == 0);
  965. }
  966. {
  967. TempAllocator128 ta;
  968. DynamicString ds1(ta);
  969. DynamicString ds2(ta);
  970. ds1.set("foo", 3);
  971. ds2.set("foo", 3);
  972. ENSURE(ds1 == ds2);
  973. }
  974. {
  975. TempAllocator128 ta;
  976. DynamicString ds1(ta);
  977. DynamicString ds2(ta);
  978. ds1.set("foo", 3);
  979. ds2.set("bar", 3);
  980. ENSURE(ds1 != ds2);
  981. }
  982. {
  983. TempAllocator128 ta;
  984. DynamicString ds1(ta);
  985. DynamicString ds2(ta);
  986. ds1.set("bar", 3);
  987. ds2.set("foo", 3);
  988. ENSURE(ds1 < ds2);
  989. }
  990. memory_globals::shutdown();
  991. }
  992. static void test_string_view()
  993. {
  994. memory_globals::init();
  995. {
  996. const char *str = "foo";
  997. StringView sv(str);
  998. ENSURE(sv._length == 3);
  999. ENSURE(sv._data == &str[0]);
  1000. }
  1001. {
  1002. StringView sv1("foo");
  1003. StringView sv2("foo");
  1004. ENSURE(sv1 == sv2);
  1005. }
  1006. {
  1007. StringView sv1("foo");
  1008. StringView sv2("bar");
  1009. ENSURE(sv1 != sv2);
  1010. }
  1011. {
  1012. StringView sv1("bar");
  1013. StringView sv2("foo");
  1014. ENSURE(sv1 < sv2);
  1015. }
  1016. {
  1017. StringView sv1("foo");
  1018. StringView sv2("fooo");
  1019. ENSURE(sv1 < sv2);
  1020. }
  1021. memory_globals::shutdown();
  1022. }
  1023. static void test_guid()
  1024. {
  1025. memory_globals::init();
  1026. guid_globals::init();
  1027. {
  1028. Guid guid = guid::new_guid();
  1029. char str[37];
  1030. guid::to_string(str, sizeof(str), guid);
  1031. Guid parsed = guid::parse(str);
  1032. ENSURE(guid == parsed);
  1033. }
  1034. {
  1035. Guid guid;
  1036. ENSURE(guid::try_parse(guid, "961f8005-6a7e-4371-9272-8454dd786884"));
  1037. ENSURE(!guid::try_parse(guid, "961f80056a7e-4371-9272-8454dd786884"));
  1038. }
  1039. {
  1040. Guid guid1 = guid::parse("8ec79062-c8fd-41b5-b044-cb545afc9976");
  1041. Guid guid2 = guid::parse("8f879a4e-e9dd-4981-8b9e-344bb917d7dc");
  1042. ENSURE(guid1 < guid2);
  1043. }
  1044. guid_globals::shutdown();
  1045. memory_globals::shutdown();
  1046. }
  1047. static void test_json()
  1048. {
  1049. memory_globals::init();
  1050. {
  1051. JsonValueType::Enum t = json::type("null");
  1052. ENSURE(t == JsonValueType::NIL);
  1053. }
  1054. {
  1055. JsonValueType::Enum t = json::type("true");
  1056. ENSURE(t == JsonValueType::BOOL);
  1057. }
  1058. {
  1059. JsonValueType::Enum t = json::type("false");
  1060. ENSURE(t == JsonValueType::BOOL);
  1061. }
  1062. {
  1063. JsonValueType::Enum t = json::type("3.14");
  1064. ENSURE(t == JsonValueType::NUMBER);
  1065. }
  1066. {
  1067. JsonValueType::Enum t = json::type("\"foo\"");
  1068. ENSURE(t == JsonValueType::STRING);
  1069. }
  1070. {
  1071. JsonValueType::Enum t = json::type("[]");
  1072. ENSURE(t == JsonValueType::ARRAY);
  1073. }
  1074. {
  1075. JsonValueType::Enum t = json::type("{}");
  1076. ENSURE(t == JsonValueType::OBJECT);
  1077. }
  1078. {
  1079. const s32 a = json::parse_int("3.14");
  1080. ENSURE(a == 3);
  1081. }
  1082. {
  1083. const f32 a = json::parse_float("3.14");
  1084. ENSURE(fequal(a, 3.14f));
  1085. }
  1086. {
  1087. const bool a = json::parse_bool("true");
  1088. ENSURE(a == true);
  1089. }
  1090. {
  1091. const bool a = json::parse_bool("false");
  1092. ENSURE(a == false);
  1093. }
  1094. {
  1095. TempAllocator1024 ta;
  1096. DynamicString str(ta);
  1097. json::parse_string(str, "\"This is JSON\"");
  1098. ENSURE(strcmp(str.c_str(), "This is JSON") == 0);
  1099. }
  1100. {
  1101. TempAllocator1024 ta;
  1102. JsonObject obj(ta);
  1103. json::parse_object(obj, "{\"foo\":{\"}\":false}}");
  1104. }
  1105. {
  1106. TempAllocator1024 ta;
  1107. JsonObject obj(ta);
  1108. json::parse_object(obj, "{\"foo\":[\"]\"]}");
  1109. }
  1110. memory_globals::shutdown();
  1111. }
  1112. static void test_sjson()
  1113. {
  1114. memory_globals::init();
  1115. {
  1116. JsonValueType::Enum t = sjson::type("null");
  1117. ENSURE(t == JsonValueType::NIL);
  1118. }
  1119. {
  1120. JsonValueType::Enum t = sjson::type("true");
  1121. ENSURE(t == JsonValueType::BOOL);
  1122. }
  1123. {
  1124. JsonValueType::Enum t = sjson::type("false");
  1125. ENSURE(t == JsonValueType::BOOL);
  1126. }
  1127. {
  1128. JsonValueType::Enum t = sjson::type("3.14");
  1129. ENSURE(t == JsonValueType::NUMBER);
  1130. }
  1131. {
  1132. JsonValueType::Enum t = sjson::type("\"foo\"");
  1133. ENSURE(t == JsonValueType::STRING);
  1134. }
  1135. {
  1136. JsonValueType::Enum t = sjson::type("[]");
  1137. ENSURE(t == JsonValueType::ARRAY);
  1138. }
  1139. {
  1140. JsonValueType::Enum t = sjson::type("{}");
  1141. ENSURE(t == JsonValueType::OBJECT);
  1142. }
  1143. {
  1144. const s32 a = sjson::parse_int("3.14");
  1145. ENSURE(a == 3);
  1146. }
  1147. {
  1148. const f32 a = sjson::parse_float("3.14");
  1149. ENSURE(fequal(a, 3.14f));
  1150. }
  1151. {
  1152. const bool a = sjson::parse_bool("true");
  1153. ENSURE(a == true);
  1154. }
  1155. {
  1156. const bool a = sjson::parse_bool("false");
  1157. ENSURE(a == false);
  1158. }
  1159. {
  1160. TempAllocator1024 ta;
  1161. DynamicString str(ta);
  1162. sjson::parse_string(str, "\"This is JSON\"");
  1163. ENSURE(strcmp(str.c_str(), "This is JSON") == 0);
  1164. }
  1165. {
  1166. const Vector2 a = sjson::parse_vector2("[ 1.2 -2.5 ]");
  1167. ENSURE(fequal(a.x, 1.2f));
  1168. ENSURE(fequal(a.y, -2.5f));
  1169. }
  1170. {
  1171. const Vector3 a = sjson::parse_vector3("[ 3.1 0.5 -5.7]");
  1172. ENSURE(fequal(a.x, 3.1f));
  1173. ENSURE(fequal(a.y, 0.5f));
  1174. ENSURE(fequal(a.z, -5.7f));
  1175. }
  1176. {
  1177. const Vector4 a = sjson::parse_vector4("[ 6.7 -1.3 2.9 -0.4 ]");
  1178. ENSURE(fequal(a.x, 6.7f));
  1179. ENSURE(fequal(a.y, -1.3f));
  1180. ENSURE(fequal(a.z, 2.9f));
  1181. ENSURE(fequal(a.w, -0.4f));
  1182. }
  1183. {
  1184. const Quaternion a = sjson::parse_quaternion("[ -1.5 -3.4 9.1 -3.5 ]");
  1185. ENSURE(fequal(a.x, -1.5f));
  1186. ENSURE(fequal(a.y, -3.4f));
  1187. ENSURE(fequal(a.z, 9.1f));
  1188. ENSURE(fequal(a.w, -3.5f));
  1189. }
  1190. {
  1191. const Matrix4x4 a = sjson::parse_matrix4x4(
  1192. "["
  1193. "-3.2 5.3 -0.7 4.1 "
  1194. " 5.6 7.0 -3.2 -1.2 "
  1195. "-6.3 9.0 3.9 1.1 "
  1196. " 0.4 -7.3 8.9 -0.1 "
  1197. "]"
  1198. );
  1199. ENSURE(fequal(a.x.x, -3.2f));
  1200. ENSURE(fequal(a.x.y, 5.3f));
  1201. ENSURE(fequal(a.x.z, -0.7f));
  1202. ENSURE(fequal(a.x.w, 4.1f));
  1203. ENSURE(fequal(a.y.x, 5.6f));
  1204. ENSURE(fequal(a.y.y, 7.0f));
  1205. ENSURE(fequal(a.y.z, -3.2f));
  1206. ENSURE(fequal(a.y.w, -1.2f));
  1207. ENSURE(fequal(a.z.x, -6.3f));
  1208. ENSURE(fequal(a.z.y, 9.0f));
  1209. ENSURE(fequal(a.z.z, 3.9f));
  1210. ENSURE(fequal(a.z.w, 1.1f));
  1211. ENSURE(fequal(a.t.x, 0.4f));
  1212. ENSURE(fequal(a.t.y, -7.3f));
  1213. ENSURE(fequal(a.t.z, 8.9f));
  1214. ENSURE(fequal(a.t.w, -0.1f));
  1215. }
  1216. {
  1217. const StringId32 a = sjson::parse_string_id("\"murmur32\"");
  1218. ENSURE(a._id == 0x7c2365dbu);
  1219. }
  1220. {
  1221. const StringId64 a = sjson::parse_resource_name("\"murmur64\"");
  1222. ENSURE(a._id == 0x90631502d1a3432bu);
  1223. }
  1224. {
  1225. const Guid guid = guid::parse("0f6c3b1c-9cba-4282-9096-2a77ca047b1b");
  1226. const Guid parsed = sjson::parse_guid("\"0f6c3b1c-9cba-4282-9096-2a77ca047b1b\"");
  1227. ENSURE(guid == parsed);
  1228. }
  1229. {
  1230. TempAllocator128 ta;
  1231. DynamicString str(ta);
  1232. sjson::parse_verbatim(str, "\"\"\"verbatim\"\"\"");
  1233. ENSURE(strcmp(str.c_str(), "verbatim") == 0);
  1234. }
  1235. {
  1236. TempAllocator1024 ta;
  1237. JsonObject obj(ta);
  1238. sjson::parse_object(obj, "{foo={\"}\"=false}}");
  1239. }
  1240. {
  1241. TempAllocator1024 ta;
  1242. JsonObject obj(ta);
  1243. sjson::parse_object(obj, "{foo={bar=\"\"\"}\"\"\"}}");
  1244. }
  1245. {
  1246. TempAllocator1024 ta;
  1247. JsonObject obj(ta);
  1248. sjson::parse_object(obj, "{foo=[\"]\"]}");
  1249. }
  1250. {
  1251. TempAllocator1024 ta;
  1252. JsonObject obj(ta);
  1253. sjson::parse_object(obj, "{foo=[/*]*/]}");
  1254. }
  1255. {
  1256. TempAllocator1024 ta;
  1257. JsonObject obj(ta);
  1258. sjson::parse_object(obj, "{foo=[//]\n]}");
  1259. }
  1260. memory_globals::shutdown();
  1261. }
  1262. static void test_path()
  1263. {
  1264. #if CROWN_PLATFORM_WINDOWS
  1265. {
  1266. const bool a = path::is_absolute("C:\\Users\\foo");
  1267. ENSURE(a == true);
  1268. const bool b = path::is_absolute("Users\\foo");
  1269. ENSURE(b == false);
  1270. }
  1271. {
  1272. const bool a = path::is_relative("D:\\Users\\foo");
  1273. ENSURE(a == false);
  1274. const bool b = path::is_relative("Users\\foo");
  1275. ENSURE(b == true);
  1276. }
  1277. {
  1278. const bool a = path::is_root("E:\\");
  1279. ENSURE(a == true);
  1280. const bool b = path::is_root("E:\\Users");
  1281. ENSURE(b == false);
  1282. }
  1283. {
  1284. ENSURE(path::has_trailing_separator("C:\\Users\\foo\\"));
  1285. ENSURE(!path::has_trailing_separator("C:\\Users\\foo"));
  1286. }
  1287. {
  1288. TempAllocator128 ta;
  1289. DynamicString clean(ta);
  1290. path::reduce(clean, "C:\\Users\\\\foo\\");
  1291. ENSURE(clean == "C:\\Users\\foo");
  1292. }
  1293. {
  1294. TempAllocator128 ta;
  1295. DynamicString clean(ta);
  1296. path::reduce(clean, "C:/Users//foo/");
  1297. ENSURE(clean == "C:\\Users\\foo");
  1298. }
  1299. #else
  1300. {
  1301. const bool a = path::is_absolute("/home/foo");
  1302. ENSURE(a == true);
  1303. const bool b = path::is_absolute("home/foo");
  1304. ENSURE(b == false);
  1305. }
  1306. {
  1307. const bool a = path::is_relative("/home/foo");
  1308. ENSURE(a == false);
  1309. const bool b = path::is_relative("home/foo");
  1310. ENSURE(b == true);
  1311. }
  1312. {
  1313. const bool a = path::is_root("/");
  1314. ENSURE(a == true);
  1315. const bool b = path::is_root("/home");
  1316. ENSURE(b == false);
  1317. }
  1318. {
  1319. ENSURE(path::has_trailing_separator("/home/foo/"));
  1320. ENSURE(!path::has_trailing_separator("/home/foo"));
  1321. }
  1322. {
  1323. TempAllocator128 ta;
  1324. DynamicString clean(ta);
  1325. path::reduce(clean, "/home//foo/");
  1326. ENSURE(clean == "/home/foo");
  1327. }
  1328. {
  1329. TempAllocator128 ta;
  1330. DynamicString clean(ta);
  1331. path::reduce(clean, "\\home\\\\foo\\");
  1332. ENSURE(clean == "/home/foo");
  1333. }
  1334. #endif // if CROWN_PLATFORM_WINDOWS
  1335. {
  1336. const char *p = path::basename("");
  1337. ENSURE(strcmp(p, "") == 0);
  1338. }
  1339. {
  1340. const char *q = path::basename("/");
  1341. ENSURE(strcmp(q, "") == 0);
  1342. }
  1343. {
  1344. const char *p = path::basename("boot.config");
  1345. ENSURE(strcmp(p, "boot.config") == 0);
  1346. }
  1347. {
  1348. const char *p = path::basename("foo/boot.config");
  1349. ENSURE(strcmp(p, "boot.config") == 0);
  1350. }
  1351. {
  1352. const char *p = path::basename("/foo/boot.config");
  1353. ENSURE(strcmp(p, "boot.config") == 0);
  1354. }
  1355. {
  1356. const char *p = path::extension("");
  1357. ENSURE(p == NULL);
  1358. }
  1359. {
  1360. const char *p = path::extension("boot");
  1361. ENSURE(p == NULL);
  1362. }
  1363. {
  1364. const char *p = path::extension("boot.bar.config");
  1365. ENSURE(strcmp(p, "config") == 0);
  1366. }
  1367. {
  1368. const char *p = path::extension(".bar");
  1369. ENSURE(p == NULL);
  1370. }
  1371. {
  1372. const char *p = path::extension("foo/.bar");
  1373. ENSURE(p == NULL);
  1374. }
  1375. {
  1376. TempAllocator128 ta;
  1377. DynamicString path(ta);
  1378. path::join(path, "", "");
  1379. ENSURE(path == "");
  1380. }
  1381. {
  1382. TempAllocator128 ta;
  1383. DynamicString path(ta);
  1384. path::join(path, "foo", "");
  1385. ENSURE(path == "foo");
  1386. }
  1387. {
  1388. TempAllocator128 ta;
  1389. DynamicString path(ta);
  1390. path::join(path, "", "bar");
  1391. ENSURE(path == "bar");
  1392. }
  1393. #if CROWN_PLATFORM_WINDOWS
  1394. {
  1395. TempAllocator128 ta;
  1396. DynamicString path(ta);
  1397. path::join(path, "C:\\foo", "bar");
  1398. ENSURE(path == "C:\\foo\\bar");
  1399. }
  1400. #else
  1401. {
  1402. TempAllocator128 ta;
  1403. DynamicString path(ta);
  1404. path::join(path, "foo", "bar");
  1405. ENSURE(path == "foo/bar");
  1406. }
  1407. #endif
  1408. }
  1409. static void test_command_line()
  1410. {
  1411. const char *argv[] =
  1412. {
  1413. "args",
  1414. "-s",
  1415. "--switch",
  1416. "--argument",
  1417. "orange"
  1418. };
  1419. CommandLine cl(countof(argv), argv);
  1420. ENSURE(cl.has_option("switch", 's'));
  1421. const char *orange = cl.get_parameter(0, "argument");
  1422. ENSURE(orange != NULL && strcmp(orange, "orange") == 0);
  1423. }
  1424. static void test_thread()
  1425. {
  1426. Thread thread;
  1427. ENSURE(!thread.is_running());
  1428. thread.start([](void *) { return 0xbadc0d3; }, NULL);
  1429. thread.stop();
  1430. ENSURE(thread.exit_code() == 0xbadc0d3);
  1431. }
  1432. static void test_process()
  1433. {
  1434. #if CROWN_PLATFORM_LINUX
  1435. {
  1436. #if CROWN_DEVELOPMENT
  1437. #define CROWN_SUFFIX "development"
  1438. #elif CROWN_DEBUG
  1439. #define CROWN_SUFFIX "debug"
  1440. #else
  1441. #define CROWN_SUFFIX "release"
  1442. #endif
  1443. const char *argv[] =
  1444. {
  1445. EXE_PATH("crown-" CROWN_SUFFIX)
  1446. , "--version"
  1447. , NULL
  1448. };
  1449. Process pr;
  1450. s32 err = pr.spawn(argv, CROWN_PROCESS_STDOUT_PIPE);
  1451. ENSURE(err == 0);
  1452. u32 nbr;
  1453. char buf[128] = {0};
  1454. pr.read(&nbr, buf, sizeof(buf));
  1455. const char *ver = "Crown " CROWN_VERSION;
  1456. ENSURE(strncmp(buf, ver, strlen32(ver)) == 0);
  1457. s32 ec = pr.wait();
  1458. ENSURE(ec == 0);
  1459. }
  1460. #endif // if CROWN_PLATFORM_LINUX
  1461. }
  1462. static void test_filesystem()
  1463. {
  1464. #if CROWN_PLATFORM_POSIX
  1465. guid_globals::init();
  1466. {
  1467. Guid id = guid::new_guid();
  1468. char dir[5 + GUID_BUF_LEN] = "/tmp/";
  1469. guid::to_string(dir + 5, sizeof(dir) - 5, id);
  1470. DeleteResult dr = os::delete_directory(dir);
  1471. ENSURE(dr.error == DeleteResult::NO_ENTRY);
  1472. }
  1473. {
  1474. Guid id = guid::new_guid();
  1475. char dir[5 + GUID_BUF_LEN] = "/tmp/";
  1476. guid::to_string(dir + 5, sizeof(dir) - 5, id);
  1477. os::delete_directory(dir);
  1478. CreateResult cr = os::create_directory(dir);
  1479. ENSURE(cr.error == CreateResult::SUCCESS);
  1480. cr = os::create_directory(dir);
  1481. ENSURE(cr.error == CreateResult::ALREADY_EXISTS);
  1482. DeleteResult dr = os::delete_directory(dir);
  1483. ENSURE(dr.error == DeleteResult::SUCCESS);
  1484. }
  1485. guid_globals::shutdown();
  1486. #endif // if CROWN_PLATFORM_POSIX
  1487. }
  1488. #define RUN_TEST(name) \
  1489. do { \
  1490. printf(#name "\n"); \
  1491. name(); \
  1492. } while (0)
  1493. int main_unit_tests()
  1494. {
  1495. RUN_TEST(test_memory);
  1496. RUN_TEST(test_array);
  1497. RUN_TEST(test_vector);
  1498. RUN_TEST(test_hash_map);
  1499. RUN_TEST(test_hash_set);
  1500. RUN_TEST(test_vector2);
  1501. RUN_TEST(test_vector3);
  1502. RUN_TEST(test_vector4);
  1503. RUN_TEST(test_quaternion);
  1504. RUN_TEST(test_color4);
  1505. RUN_TEST(test_matrix3x3);
  1506. RUN_TEST(test_matrix4x4);
  1507. RUN_TEST(test_aabb);
  1508. RUN_TEST(test_sphere);
  1509. RUN_TEST(test_murmur);
  1510. RUN_TEST(test_string_id);
  1511. RUN_TEST(test_dynamic_string);
  1512. RUN_TEST(test_string_view);
  1513. RUN_TEST(test_guid);
  1514. RUN_TEST(test_json);
  1515. RUN_TEST(test_sjson);
  1516. RUN_TEST(test_path);
  1517. RUN_TEST(test_command_line);
  1518. RUN_TEST(test_thread);
  1519. RUN_TEST(test_process);
  1520. RUN_TEST(test_filesystem);
  1521. return EXIT_SUCCESS;
  1522. }
  1523. } // namespace crown
  1524. #endif // if CROWN_BUILD_UNIT_TESTS