Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 

713 rindas
15 KiB

  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "../types.hpp"
  5. u32 hm_hash(u32 u);
  6. inline bool hm_objects_match(u32 a, u32 b);
  7. struct Key;
  8. u32 hm_hash(Key u);
  9. inline bool hm_objects_match(Key a, Key b);
  10. #define ZoneScoped
  11. #define ZoneScopedN(name)
  12. #include "../print.hpp"
  13. #include "../testing.hpp"
  14. #include "../bucket_allocator.hpp"
  15. #include "../error.hpp"
  16. #include "../hooks.hpp"
  17. #include "../hashmap.hpp"
  18. #include "../stacktrace.hpp"
  19. #include "../scheduler.cpp"
  20. #include "../error.hpp"
  21. u32 hm_hash(u32 u) {
  22. return ((u64)u * 2654435761) % 4294967296;
  23. }
  24. inline bool hm_objects_match(u32 a, u32 b) {
  25. return a == b;
  26. }
  27. struct Key {
  28. int x;
  29. int y;
  30. int z;
  31. };
  32. u32 hm_hash(Key u) {
  33. return ((u.y ^ (u.x << 1)) >> 1) ^ u.z;
  34. }
  35. inline bool hm_objects_match(Key a, Key b) {
  36. return a.x == b.x
  37. && a.y == b.y
  38. && a.z == b.z;
  39. }
  40. auto print_dots(FILE* f) -> u32 {
  41. return print_to_file(f, "...");
  42. }
  43. proc is_sorted = [](Array_List<s32> list) -> bool {
  44. for (u32 i = 0; i < list.count - 1; ++i) {
  45. if (list.data[i] > list.data[i+1])
  46. return false;
  47. }
  48. return true;
  49. };
  50. proc is_sorted_vp = [](Array_List<void*> list) -> bool {
  51. for (u32 i = 0; i < list.count - 1; ++i) {
  52. if (list.data[i] > list.data[i+1])
  53. return false;
  54. }
  55. return true;
  56. };
  57. auto test_printer() -> void {
  58. u32 arr[] = {1,2,3,4,1,1,3};
  59. f32 f_arr[] = {1.1,2.1,3.2};
  60. register_printer("dots", print_dots, Printer_Function_Type::_void);
  61. u32 u1 = -1;
  62. u64 u2 = -1;
  63. char* str;
  64. print_to_string(&str, " - %{dots[5]} %{->} <> %{->,2}\n", &u1, &arr, nullptr);
  65. print("---> %{->char}", str);
  66. print(" - %{dots[3]}\n");
  67. print(" - %{u32} %{u64}\n", u1, u2);
  68. print(" - %{u32} %{u32} %{u32}\n", 2, 5, 7);
  69. print(" - %{f32} %{f32} %{f32}\n", 2.0, 5.0, 7.0);
  70. print(" - %{u32} %{bool} %{->char}\n", 2, true, "hello");
  71. print(" - %{f32[3]}\n", f_arr);
  72. print(" - %{f32,3}\n", 44.9, 55.1, 66.2);
  73. print(" - %{u32[5]}\n", arr);
  74. print(" - %{u32[*]}\n", arr, 4);
  75. print(" - %{u32,5}\n", 1,2,3,4,1,2);
  76. print(" - %{unknown%d}\n", 1);
  77. print(" - %{s32,3}\n", -1,200,-300);
  78. print(" - %{->} <> %{->,2}\n", &u1, &arr, nullptr);
  79. print("%{->char}%{->char}%{->char}",
  80. true ? "general " : "",
  81. false ? "validation " : "",
  82. false ? "performance " : "");
  83. // print("%{->char}%{->char}\n\n", "hallo","");
  84. }
  85. auto test_hm() -> void {
  86. Hash_Map<u32, u32> h1;
  87. h1.alloc();
  88. defer { h1.dealloc(); };
  89. h1.set_object(1, 2);
  90. h1.set_object(2, 4);
  91. h1.set_object(3, 6);
  92. h1.set_object(4, 8);
  93. assert(h1.key_exists(1), "key shoud exist");
  94. assert(h1.key_exists(2), "key shoud exist");
  95. assert(h1.key_exists(3), "key shoud exist");
  96. assert(h1.key_exists(4), "key shoud exist");
  97. assert(!h1.key_exists(5), "key shoud not exist");
  98. assert(h1.get_object(1) == 2, "value should be correct");
  99. assert(h1.get_object(2) == 4, "value should be correct");
  100. assert(h1.get_object(3) == 6, "value should be correct");
  101. assert(h1.get_object(4) == 8, "value should be correct");
  102. Hash_Map<Key, u32> h2;
  103. h2.alloc();
  104. defer { h2.dealloc(); };
  105. h2.set_object({.x = 1, .y = 2, .z = 3}, 1);
  106. h2.set_object({.x = 3, .y = 3, .z = 3}, 3);
  107. assert(h2.key_exists({.x = 1, .y = 2, .z = 3}), "key shoud exist");
  108. assert(h2.key_exists({.x = 3, .y = 3, .z = 3}), "key shoud exist");
  109. assert(h2.get_object({.x = 1, .y = 2, .z = 3}) == 1, "value should be correct");
  110. assert(h2.get_object({.x = 3, .y = 3, .z = 3}) == 3, "value should be correct");
  111. h2.for_each([] (Key k, u32 v, u32 i) {
  112. print("%{s32} %{u32} %{u32}\n", k.x, v, i);
  113. });
  114. }
  115. proc test_stack_array_lists() -> testresult {
  116. Stack_Array_List<int> list(20);
  117. assert_equal_int(list.count, 0);
  118. assert_equal_int(list.length, 20);
  119. assert(list.data != NULL, "list should have some data allocated");
  120. // test sum of empty list
  121. int sum = 0;
  122. int iter = 0;
  123. for (auto e : list) {
  124. sum += e;
  125. iter++;
  126. }
  127. assert_equal_int(sum, 0);
  128. assert_equal_int(iter, 0);
  129. // append some elements
  130. list.append(1);
  131. list.append(2);
  132. list.append(3);
  133. list.append(4);
  134. assert_equal_int(list.count, 4);
  135. assert_equal_int(list.length, 20);
  136. // test sum again
  137. sum = 0;
  138. iter = 0;
  139. for (auto e : list) {
  140. sum += e;
  141. iter++;
  142. }
  143. assert_equal_int(sum, 10);
  144. assert_equal_int(iter, 4);
  145. // bracketed access
  146. list[0] = 11;
  147. list[1] = 3;
  148. list[2] = 2;
  149. list.append(5);
  150. // test sum again
  151. sum = 0;
  152. iter = 0;
  153. for (auto e : list) {
  154. sum += e;
  155. ++iter;
  156. }
  157. assert_equal_int(sum, 25);
  158. assert_equal_int(iter, 5);
  159. // assert memory correct
  160. assert_equal_int(list.data[0], 11);
  161. assert_equal_int(list.data[1], 3);
  162. assert_equal_int(list.data[2], 2);
  163. assert_equal_int(list.data[3], 4);
  164. assert_equal_int(list.data[4], 5);
  165. // removing some indices
  166. list.remove_index(4);
  167. // test sum again
  168. sum = 0;
  169. iter = 0;
  170. for (auto e : list) {
  171. sum += e;
  172. ++iter;
  173. }
  174. assert_equal_int(sum, 20);
  175. assert_equal_int(iter, 4);
  176. // removing some indices
  177. list.remove_index(1);
  178. list.remove_index(0);
  179. // test sum again
  180. sum = 0;
  181. iter = 0;
  182. for (auto e : list) {
  183. sum += e;
  184. ++iter;
  185. }
  186. assert_equal_int(sum, 6);
  187. assert_equal_int(iter, 2);
  188. return pass;
  189. }
  190. proc test_queue() -> testresult {
  191. Queue<int> q;
  192. q.alloc(4);
  193. defer {
  194. q.dealloc();
  195. };
  196. assert(q.is_empty(), "queue should start empty");
  197. assert_equal_int(q.get_count(), 0);
  198. q.push_back(1);
  199. q.push_back(2);
  200. q.push_back(3);
  201. assert_equal_int(q.get_count(), 3);
  202. assert_equal_int(q.get_next(), 1);
  203. assert_equal_int(q.get_count(), 2);
  204. assert(!q.is_empty(), "should not be empty");
  205. assert_equal_int(q.get_next(), 2);
  206. assert_equal_int(q.get_count(), 1);
  207. q.push_back(4);
  208. assert_equal_int(q.get_count(), 2);
  209. assert_equal_int(q.get_next(), 3);
  210. assert_equal_int(q.get_count(), 1);
  211. assert(!q.is_empty(), "should not be empty");
  212. assert_equal_int(q.get_next(), 4);
  213. assert(q.is_empty(), "should be empty");
  214. assert_equal_int(q.get_count(), 0);
  215. return pass;
  216. }
  217. proc test_array_lists_adding_and_removing() -> testresult {
  218. // test adding and removing
  219. Array_List<s32> list;
  220. list.alloc();
  221. defer {
  222. list.dealloc();
  223. };
  224. list.append(1);
  225. list.append(2);
  226. list.append(3);
  227. list.append(4);
  228. assert_equal_int(list.count, 4);
  229. list.remove_index(0);
  230. assert_equal_int(list.count, 3);
  231. assert_equal_int(list[0], 4);
  232. assert_equal_int(list[1], 2);
  233. assert_equal_int(list[2], 3);
  234. list.remove_index(2);
  235. assert_equal_int(list.count, 2);
  236. assert_equal_int(list[0], 4);
  237. assert_equal_int(list[1], 2);
  238. return pass;
  239. }
  240. proc test_array_lists_sorting() -> testresult {
  241. //
  242. //
  243. // Test simple numbers
  244. //
  245. //
  246. Array_List<s32> list;
  247. list.alloc();
  248. defer {
  249. list.dealloc();
  250. };
  251. list.append(1);
  252. list.append(2);
  253. list.append(3);
  254. list.append(4);
  255. list.sort();
  256. assert_equal_int(is_sorted(list), true);
  257. list.append(4);
  258. list.append(2);
  259. list.append(1);
  260. assert_equal_int(is_sorted(list), false);
  261. list.sort();
  262. assert_equal_int(is_sorted(list), true);
  263. list.clear();
  264. list.extend({
  265. 8023, 7529, 2392, 7110,
  266. 3259, 2484, 9695, 2199,
  267. 6729, 9009, 8429, 7208});
  268. assert_equal_int(is_sorted(list), false);
  269. list.sort();
  270. assert_equal_int(is_sorted(list), true);
  271. //
  272. //
  273. // Test adding and removing
  274. //
  275. //
  276. Array_List<s32> list1;
  277. list1.alloc();
  278. defer {
  279. list1.dealloc();
  280. };
  281. list1.append(1);
  282. list1.append(2);
  283. list1.append(3);
  284. list1.append(4);
  285. list1.sort();
  286. assert_equal_int(list1.count, 4);
  287. assert_equal_int(list1[0], 1);
  288. assert_equal_int(list1[1], 2);
  289. assert_equal_int(list1[2], 3);
  290. assert_equal_int(list1[3], 4);
  291. assert_equal_int(is_sorted(list1), true);
  292. list1.append(0);
  293. list1.append(5);
  294. assert_equal_int(list1.count, 6);
  295. list1.sort();
  296. assert_equal_int(list1[0], 0);
  297. assert_equal_int(list1[1], 1);
  298. assert_equal_int(list1[2], 2);
  299. assert_equal_int(list1[3], 3);
  300. assert_equal_int(list1[4], 4);
  301. assert_equal_int(list1[5], 5);
  302. assert_equal_int(is_sorted(list1), true);
  303. //
  304. //
  305. //
  306. //
  307. // pointer list
  308. Array_List<void*> al;
  309. al.alloc();
  310. defer {
  311. al.dealloc();
  312. };
  313. al.append((void*)0x1703102F100);
  314. al.append((void*)0x1703102F1D8);
  315. al.append((void*)0x1703102F148);
  316. al.append((void*)0x1703102F190);
  317. al.append((void*)0x1703102F190);
  318. al.append((void*)0x1703102F1D8);
  319. assert_equal_int(is_sorted_vp(al), false);
  320. al.sort();
  321. assert_equal_int(is_sorted_vp(al), true);
  322. assert_not_equal_int(al.sorted_find((void*)0x1703102F100), -1);
  323. assert_not_equal_int(al.sorted_find((void*)0x1703102F1D8), -1);
  324. assert_not_equal_int(al.sorted_find((void*)0x1703102F148), -1);
  325. assert_not_equal_int(al.sorted_find((void*)0x1703102F190), -1);
  326. assert_not_equal_int(al.sorted_find((void*)0x1703102F190), -1);
  327. assert_not_equal_int(al.sorted_find((void*)0x1703102F1D8), -1);
  328. return pass;
  329. }
  330. proc test_array_lists_searching() -> testresult {
  331. Array_List<s32> list1;
  332. list1.alloc();
  333. defer {
  334. list1.dealloc();
  335. };
  336. list1.append(1);
  337. list1.append(2);
  338. list1.append(3);
  339. list1.append(4);
  340. s32 index = list1.sorted_find(3);
  341. assert_equal_int(index, 2);
  342. index = list1.sorted_find(1);
  343. assert_equal_int(index, 0);
  344. index = list1.sorted_find(5);
  345. assert_equal_int(index, -1);
  346. return pass;
  347. }
  348. proc test_bucket_allocator() -> testresult {
  349. Bucket_Allocator<s32> ba;
  350. ba.alloc();
  351. defer {
  352. ba.dealloc();
  353. };
  354. s32* s1 = ba.allocate();
  355. s32* s2 = ba.allocate();
  356. s32* s3 = ba.allocate();
  357. s32* s4 = ba.allocate();
  358. s32* s5 = ba.allocate();
  359. *s1 = 1;
  360. *s2 = 2;
  361. *s3 = 3;
  362. *s4 = 4;
  363. *s5 = 5;
  364. s32 wrong_answers = 0;
  365. s32 counter = 1;
  366. ba.for_each([&](s32* s) -> void {
  367. if(counter != *s)
  368. ++wrong_answers;
  369. ++counter;
  370. });
  371. assert_equal_int(wrong_answers, 0);
  372. Bucket_Allocator<s32> ba2;
  373. ba2.alloc();
  374. defer {
  375. ba2.dealloc();
  376. };
  377. s1 = ba2.allocate();
  378. s2 = ba2.allocate();
  379. s3 = ba2.allocate();
  380. s32* s3_copy = ba2.allocate();
  381. s32* s3_copy2 = ba2.allocate();
  382. s32* s3_copy3 = ba2.allocate();
  383. *s3_copy = 3;
  384. ba2.free_object(s3_copy);
  385. s4 = ba2.allocate();
  386. ba2.free_object(s3_copy2);
  387. s5 = ba2.allocate();
  388. ba2.free_object(s3_copy3);
  389. *s1 = 1;
  390. *s2 = 2;
  391. *s3 = 3;
  392. *s4 = 4;
  393. *s5 = 5;
  394. wrong_answers = 0;
  395. counter = 1;
  396. ba2.for_each([&](s32* s) -> void {
  397. if(counter != *s)
  398. ++wrong_answers;
  399. ++counter;
  400. });
  401. assert_equal_int(wrong_answers, 0);
  402. return pass;
  403. }
  404. auto test_array_list_sort_many() -> testresult {
  405. Array_List<s32> list;
  406. list.alloc();
  407. defer {
  408. list.dealloc();
  409. };
  410. for (int i = 0; i < 10000; ++i) {
  411. list.append(rand());
  412. }
  413. assert_equal_int(is_sorted(list), false);
  414. list.sort();
  415. assert_equal_int(is_sorted(list), true);
  416. for (int i = 0; i < 10000; ++i) {
  417. assert_not_equal_int(list.sorted_find(list.data[i]), -1);
  418. }
  419. list.clear();
  420. for (int i = 0; i < 1111; ++i) {
  421. list.append(rand());
  422. }
  423. assert_equal_int(is_sorted(list), false);
  424. list.sort();
  425. assert_equal_int(is_sorted(list), true);
  426. for (int i = 0; i < 1111; ++i) {
  427. assert_not_equal_int(list.sorted_find(list.data[i]), -1);
  428. }
  429. list.clear();
  430. for (int i = 0; i < 3331; ++i) {
  431. list.append(rand());
  432. }
  433. assert_equal_int(is_sorted(list), false);
  434. list.sort();
  435. assert_equal_int(is_sorted(list), true);
  436. for (int i = 0; i < 3331; ++i) {
  437. assert_not_equal_int(list.sorted_find(list.data[i]), -1);
  438. }
  439. return pass;
  440. }
  441. auto test_hooks() -> testresult {
  442. s32 a = 0;
  443. s32 b = 0;
  444. s32 c = 0;
  445. Hook hook;
  446. hook << [&]() {
  447. a = 1;
  448. };
  449. hook << [&]() {
  450. // NOTE(Felix): assert correct execution order
  451. if (a == 1 && c == 0) {
  452. b = 2;
  453. }
  454. };
  455. hook << [&]() {
  456. c = 3;
  457. };
  458. assert_equal_int(a, 0);
  459. assert_equal_int(b, 0);
  460. assert_equal_int(c, 0);
  461. hook();
  462. assert_equal_int(a, 1);
  463. assert_equal_int(b, 2);
  464. assert_equal_int(c, 3);
  465. a = 0;
  466. b = 0;
  467. c = 0;
  468. // NOTE(Felix): hook should be empty now
  469. hook();
  470. assert_equal_int(a, 0);
  471. assert_equal_int(b, 0);
  472. assert_equal_int(c, 0);
  473. return pass;
  474. }
  475. auto test_scheduler_animations() -> testresult {
  476. using namespace Scheduler;
  477. Scheduler::init();
  478. defer { Scheduler::deinit(); };
  479. f32 val = 0;
  480. f32 from = 1;
  481. f32 to = 2;
  482. schedule_animation({
  483. .seconds_to_start = 1,
  484. .seconds_to_end = 2,
  485. .interpolant = &val,
  486. .interpolant_type = Interpolant_Type::F32,
  487. .from = &from,
  488. .to = &to,
  489. .interpolation_type = Interpolation_Type::Lerp
  490. });
  491. assert_equal_f64((f64)val, 0.0);
  492. update_all(1);
  493. assert_equal_f64((f64)val, 1.0);
  494. update_all(0.1);
  495. assert_equal_int(abs(val - 1.1) < 0.001, true);
  496. update_all(0.1);
  497. assert_equal_int(abs(val - 1.2) < 0.001, true);
  498. update_all(0.2);
  499. assert_equal_int(abs(val - 1.4) < 0.001, true);
  500. update_all(1);
  501. assert_equal_int(abs(val - 2) < 0.001, true);
  502. // testing custom type interpolation
  503. enum My_Interpolant_Type : u8 {
  504. S32
  505. };
  506. register_interpolator([](void* p_from, f32 t, void* p_to, void* p_interpolant) {
  507. s32 from = *(s32*)p_from;
  508. s32 to = *(s32*)p_to;
  509. s32* target = (s32*)p_interpolant;
  510. *target = from + (to - from) * t;
  511. }, (Interpolant_Type)My_Interpolant_Type::S32, sizeof(s32));
  512. s32 test = 0;
  513. s32 s_from = 1;
  514. s32 s_to = 11;
  515. schedule_animation({
  516. .seconds_to_start = 1,
  517. .seconds_to_end = 2,
  518. .interpolant = &test,
  519. .interpolant_type = (Interpolant_Type)My_Interpolant_Type::S32,
  520. .from = &s_from,
  521. .to = &s_to,
  522. .interpolation_type = Interpolation_Type::Lerp
  523. });
  524. assert_equal_int(test, 0);
  525. update_all(1);
  526. assert_equal_int(test, 1);
  527. update_all(0.1);
  528. assert_equal_int(test, 2);
  529. update_all(0.1);
  530. assert_equal_int(test, 3);
  531. update_all(0.2);
  532. assert_equal_int(test, 5);
  533. update_all(1);
  534. assert_equal_int(test, 11);
  535. return pass;
  536. }
  537. s32 main(s32, char**) {
  538. init_printer();
  539. defer { deinit_printer(); };
  540. testresult result;
  541. invoke_test(test_array_lists_adding_and_removing);
  542. invoke_test(test_array_lists_sorting);
  543. invoke_test(test_array_lists_searching);
  544. invoke_test(test_array_list_sort_many);
  545. invoke_test(test_stack_array_lists);
  546. invoke_test(test_bucket_allocator);
  547. invoke_test(test_queue);
  548. invoke_test(test_hooks);
  549. invoke_test(test_scheduler_animations);
  550. return 0;
  551. }