unit_tests.cpp 34 KB

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