Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 

577 Zeilen
12 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 "../error.hpp"
  19. u32 hm_hash(u32 u) {
  20. return ((u64)u * 2654435761) % 4294967296;
  21. }
  22. inline bool hm_objects_match(u32 a, u32 b) {
  23. return a == b;
  24. }
  25. struct Key {
  26. int x;
  27. int y;
  28. int z;
  29. };
  30. u32 hm_hash(Key u) {
  31. return ((u.y ^ (u.x << 1)) >> 1) ^ u.z;
  32. }
  33. inline bool hm_objects_match(Key a, Key b) {
  34. return a.x == b.x
  35. && a.y == b.y
  36. && a.z == b.z;
  37. }
  38. auto print_dots(FILE* f) -> u32 {
  39. return print_to_file(f, "...");
  40. }
  41. proc is_sorted = [](Array_List<s32> list) -> bool {
  42. for (u32 i = 0; i < list.count - 1; ++i) {
  43. if (list.data[i] > list.data[i+1])
  44. return false;
  45. }
  46. return true;
  47. };
  48. proc is_sorted_vp = [](Array_List<void*> list) -> bool {
  49. for (u32 i = 0; i < list.count - 1; ++i) {
  50. if (list.data[i] > list.data[i+1])
  51. return false;
  52. }
  53. return true;
  54. };
  55. auto test_printer() -> void {
  56. u32 arr[] = {1,2,3,4,1,1,3};
  57. f32 f_arr[] = {1.1,2.1,3.2};
  58. register_printer("dots", print_dots, Printer_Function_Type::_void);
  59. u32 u1 = -1;
  60. u64 u2 = -1;
  61. char* str;
  62. print_to_string(&str, " - %{dots[5]} %{->} <> %{->,2}\n", &u1, &arr, nullptr);
  63. print("---> %{->char}", str);
  64. print(" - %{dots[3]}\n");
  65. print(" - %{u32} %{u64}\n", u1, u2);
  66. print(" - %{u32} %{u32} %{u32}\n", 2, 5, 7);
  67. print(" - %{f32} %{f32} %{f32}\n", 2.0, 5.0, 7.0);
  68. print(" - %{u32} %{bool} %{->char}\n", 2, true, "hello");
  69. print(" - %{f32[3]}\n", f_arr);
  70. print(" - %{f32,3}\n", 44.9, 55.1, 66.2);
  71. print(" - %{u32[5]}\n", arr);
  72. print(" - %{u32[*]}\n", arr, 4);
  73. print(" - %{u32,5}\n", 1,2,3,4,1,2);
  74. print(" - %{unknown%d}\n", 1);
  75. print(" - %{s32,3}\n", -1,200,-300);
  76. print(" - %{->} <> %{->,2}\n", &u1, &arr, nullptr);
  77. print("%{->char}%{->char}%{->char}",
  78. true ? "general " : "",
  79. false ? "validation " : "",
  80. false ? "performance " : "");
  81. // print("%{->char}%{->char}\n\n", "hallo","");
  82. }
  83. auto test_hm() -> void {
  84. Hash_Map<u32, u32> h1;
  85. h1.alloc();
  86. defer { h1.dealloc(); };
  87. h1.set_object(1, 2);
  88. h1.set_object(2, 4);
  89. h1.set_object(3, 6);
  90. h1.set_object(4, 8);
  91. assert(h1.key_exists(1), "key shoud exist");
  92. assert(h1.key_exists(2), "key shoud exist");
  93. assert(h1.key_exists(3), "key shoud exist");
  94. assert(h1.key_exists(4), "key shoud exist");
  95. assert(!h1.key_exists(5), "key shoud not exist");
  96. assert(h1.get_object(1) == 2, "value should be correct");
  97. assert(h1.get_object(2) == 4, "value should be correct");
  98. assert(h1.get_object(3) == 6, "value should be correct");
  99. assert(h1.get_object(4) == 8, "value should be correct");
  100. Hash_Map<Key, u32> h2;
  101. h2.alloc();
  102. defer { h2.dealloc(); };
  103. h2.set_object({.x = 1, .y = 2, .z = 3}, 1);
  104. h2.set_object({.x = 3, .y = 3, .z = 3}, 3);
  105. assert(h2.key_exists({.x = 1, .y = 2, .z = 3}), "key shoud exist");
  106. assert(h2.key_exists({.x = 3, .y = 3, .z = 3}), "key shoud exist");
  107. assert(h2.get_object({.x = 1, .y = 2, .z = 3}) == 1, "value should be correct");
  108. assert(h2.get_object({.x = 3, .y = 3, .z = 3}) == 3, "value should be correct");
  109. h2.for_each([] (Key k, u32 v, u32 i) {
  110. print("%{s32} %{u32} %{u32}\n", k.x, v, i);
  111. });
  112. }
  113. proc test_stack_array_lists() -> testresult {
  114. Stack_Array_List<int> list(20);
  115. assert_equal_int(list.count, 0);
  116. assert_equal_int(list.length, 20);
  117. assert(list.data != NULL, "list should have some data allocated");
  118. // test sum of empty list
  119. int sum = 0;
  120. int iter = 0;
  121. for (auto e : list) {
  122. sum += e;
  123. iter++;
  124. }
  125. assert_equal_int(sum, 0);
  126. assert_equal_int(iter, 0);
  127. // append some elements
  128. list.append(1);
  129. list.append(2);
  130. list.append(3);
  131. list.append(4);
  132. assert_equal_int(list.count, 4);
  133. assert_equal_int(list.length, 20);
  134. // test sum again
  135. sum = 0;
  136. iter = 0;
  137. for (auto e : list) {
  138. sum += e;
  139. iter++;
  140. }
  141. assert_equal_int(sum, 10);
  142. assert_equal_int(iter, 4);
  143. // bracketed access
  144. list[0] = 11;
  145. list[1] = 3;
  146. list[2] = 2;
  147. list.append(5);
  148. // test sum again
  149. sum = 0;
  150. iter = 0;
  151. for (auto e : list) {
  152. sum += e;
  153. ++iter;
  154. }
  155. assert_equal_int(sum, 25);
  156. assert_equal_int(iter, 5);
  157. // assert memory correct
  158. assert_equal_int(list.data[0], 11);
  159. assert_equal_int(list.data[1], 3);
  160. assert_equal_int(list.data[2], 2);
  161. assert_equal_int(list.data[3], 4);
  162. assert_equal_int(list.data[4], 5);
  163. // removing some indices
  164. list.remove_index(4);
  165. // test sum again
  166. sum = 0;
  167. iter = 0;
  168. for (auto e : list) {
  169. sum += e;
  170. ++iter;
  171. }
  172. assert_equal_int(sum, 20);
  173. assert_equal_int(iter, 4);
  174. // removing some indices
  175. list.remove_index(1);
  176. list.remove_index(0);
  177. // test sum again
  178. sum = 0;
  179. iter = 0;
  180. for (auto e : list) {
  181. sum += e;
  182. ++iter;
  183. }
  184. assert_equal_int(sum, 6);
  185. assert_equal_int(iter, 2);
  186. return pass;
  187. }
  188. proc test_queue() -> testresult {
  189. Queue<int> q;
  190. q.alloc(4);
  191. assert(q.is_empty(), "queue should start empty");
  192. assert_equal_int(q.get_count(), 0);
  193. q.push_back(1);
  194. q.push_back(2);
  195. q.push_back(3);
  196. assert_equal_int(q.get_count(), 3);
  197. assert_equal_int(q.get_next(), 1);
  198. assert_equal_int(q.get_count(), 2);
  199. assert(!q.is_empty(), "should not be empty");
  200. assert_equal_int(q.get_next(), 2);
  201. assert_equal_int(q.get_count(), 1);
  202. q.push_back(4);
  203. assert_equal_int(q.get_count(), 2);
  204. assert_equal_int(q.get_next(), 3);
  205. assert_equal_int(q.get_count(), 1);
  206. assert(!q.is_empty(), "should not be empty");
  207. assert_equal_int(q.get_next(), 4);
  208. assert(q.is_empty(), "should be empty");
  209. assert_equal_int(q.get_count(), 0);
  210. return pass;
  211. }
  212. proc test_array_lists_adding_and_removing() -> testresult {
  213. // test adding and removing
  214. Array_List<s32> list;
  215. list.alloc();
  216. defer {
  217. list.dealloc();
  218. };
  219. list.append(1);
  220. list.append(2);
  221. list.append(3);
  222. list.append(4);
  223. assert_equal_int(list.count, 4);
  224. list.remove_index(0);
  225. assert_equal_int(list.count, 3);
  226. assert_equal_int(list[0], 4);
  227. assert_equal_int(list[1], 2);
  228. assert_equal_int(list[2], 3);
  229. list.remove_index(2);
  230. assert_equal_int(list.count, 2);
  231. assert_equal_int(list[0], 4);
  232. assert_equal_int(list[1], 2);
  233. return pass;
  234. }
  235. proc test_array_lists_sorting() -> testresult {
  236. //
  237. //
  238. // Test simple numbers
  239. //
  240. //
  241. Array_List<s32> list;
  242. list.alloc();
  243. defer {
  244. list.dealloc();
  245. };
  246. list.append(1);
  247. list.append(2);
  248. list.append(3);
  249. list.append(4);
  250. list.sort();
  251. assert_equal_int(is_sorted(list), true);
  252. list.append(4);
  253. list.append(2);
  254. list.append(1);
  255. assert_equal_int(is_sorted(list), false);
  256. list.sort();
  257. assert_equal_int(is_sorted(list), true);
  258. list.clear();
  259. list.extend({
  260. 8023, 7529, 2392, 7110,
  261. 3259, 2484, 9695, 2199,
  262. 6729, 9009, 8429, 7208});
  263. assert_equal_int(is_sorted(list), false);
  264. list.sort();
  265. assert_equal_int(is_sorted(list), true);
  266. //
  267. //
  268. // Test adding and removing
  269. //
  270. //
  271. Array_List<s32> list1;
  272. list1.alloc();
  273. defer {
  274. list1.dealloc();
  275. };
  276. list1.append(1);
  277. list1.append(2);
  278. list1.append(3);
  279. list1.append(4);
  280. list1.sort();
  281. assert_equal_int(list1.count, 4);
  282. assert_equal_int(list1[0], 1);
  283. assert_equal_int(list1[1], 2);
  284. assert_equal_int(list1[2], 3);
  285. assert_equal_int(list1[3], 4);
  286. assert_equal_int(is_sorted(list1), true);
  287. list1.append(0);
  288. list1.append(5);
  289. assert_equal_int(list1.count, 6);
  290. list1.sort();
  291. assert_equal_int(list1[0], 0);
  292. assert_equal_int(list1[1], 1);
  293. assert_equal_int(list1[2], 2);
  294. assert_equal_int(list1[3], 3);
  295. assert_equal_int(list1[4], 4);
  296. assert_equal_int(list1[5], 5);
  297. assert_equal_int(is_sorted(list1), true);
  298. //
  299. //
  300. //
  301. //
  302. // pointer list
  303. Array_List<void*> al;
  304. al.alloc();
  305. defer {
  306. al.dealloc();
  307. };
  308. al.append((void*)0x1703102F100);
  309. al.append((void*)0x1703102F1D8);
  310. al.append((void*)0x1703102F148);
  311. al.append((void*)0x1703102F190);
  312. al.append((void*)0x1703102F190);
  313. al.append((void*)0x1703102F1D8);
  314. assert_equal_int(is_sorted_vp(al), false);
  315. al.sort();
  316. assert_equal_int(is_sorted_vp(al), true);
  317. assert_not_equal_int(al.sorted_find((void*)0x1703102F100), -1);
  318. assert_not_equal_int(al.sorted_find((void*)0x1703102F1D8), -1);
  319. assert_not_equal_int(al.sorted_find((void*)0x1703102F148), -1);
  320. assert_not_equal_int(al.sorted_find((void*)0x1703102F190), -1);
  321. assert_not_equal_int(al.sorted_find((void*)0x1703102F190), -1);
  322. assert_not_equal_int(al.sorted_find((void*)0x1703102F1D8), -1);
  323. return pass;
  324. }
  325. proc test_array_lists_searching() -> testresult {
  326. Array_List<s32> list1;
  327. list1.alloc();
  328. defer {
  329. list1.dealloc();
  330. };
  331. list1.append(1);
  332. list1.append(2);
  333. list1.append(3);
  334. list1.append(4);
  335. s32 index = list1.sorted_find(3);
  336. assert_equal_int(index, 2);
  337. index = list1.sorted_find(1);
  338. assert_equal_int(index, 0);
  339. index = list1.sorted_find(5);
  340. assert_equal_int(index, -1);
  341. return pass;
  342. }
  343. proc test_bucket_allocator() -> testresult {
  344. Bucket_Allocator<s32> ba;
  345. ba.alloc();
  346. defer {
  347. ba.dealloc();
  348. };
  349. s32* s1 = ba.allocate();
  350. s32* s2 = ba.allocate();
  351. s32* s3 = ba.allocate();
  352. s32* s4 = ba.allocate();
  353. s32* s5 = ba.allocate();
  354. *s1 = 1;
  355. *s2 = 2;
  356. *s3 = 3;
  357. *s4 = 4;
  358. *s5 = 5;
  359. s32 wrong_answers = 0;
  360. s32 counter = 1;
  361. ba.for_each([&](s32* s) -> void {
  362. if(counter != *s)
  363. ++wrong_answers;
  364. ++counter;
  365. });
  366. assert_equal_int(wrong_answers, 0);
  367. Bucket_Allocator<s32> ba2;
  368. ba2.alloc();
  369. defer {
  370. ba2.dealloc();
  371. };
  372. s1 = ba2.allocate();
  373. s2 = ba2.allocate();
  374. s3 = ba2.allocate();
  375. s32* s3_copy = ba2.allocate();
  376. s32* s3_copy2 = ba2.allocate();
  377. s32* s3_copy3 = ba2.allocate();
  378. *s3_copy = 3;
  379. ba2.free_object(s3_copy);
  380. s4 = ba2.allocate();
  381. ba2.free_object(s3_copy2);
  382. s5 = ba2.allocate();
  383. ba2.free_object(s3_copy3);
  384. *s1 = 1;
  385. *s2 = 2;
  386. *s3 = 3;
  387. *s4 = 4;
  388. *s5 = 5;
  389. wrong_answers = 0;
  390. counter = 1;
  391. ba2.for_each([&](s32* s) -> void {
  392. if(counter != *s)
  393. ++wrong_answers;
  394. ++counter;
  395. });
  396. assert_equal_int(wrong_answers, 0);
  397. return pass;
  398. }
  399. auto test_array_list_sort_many() -> testresult {
  400. Array_List<s32> list;
  401. list.alloc();
  402. defer {
  403. list.dealloc();
  404. };
  405. for (int i = 0; i < 10000; ++i) {
  406. list.append(rand());
  407. }
  408. assert_equal_int(is_sorted(list), false);
  409. list.sort();
  410. assert_equal_int(is_sorted(list), true);
  411. for (int i = 0; i < 10000; ++i) {
  412. assert_not_equal_int(list.sorted_find(list.data[i]), -1);
  413. }
  414. list.clear();
  415. for (int i = 0; i < 1111; ++i) {
  416. list.append(rand());
  417. }
  418. assert_equal_int(is_sorted(list), false);
  419. list.sort();
  420. assert_equal_int(is_sorted(list), true);
  421. for (int i = 0; i < 1111; ++i) {
  422. assert_not_equal_int(list.sorted_find(list.data[i]), -1);
  423. }
  424. list.clear();
  425. for (int i = 0; i < 3331; ++i) {
  426. list.append(rand());
  427. }
  428. assert_equal_int(is_sorted(list), false);
  429. list.sort();
  430. assert_equal_int(is_sorted(list), true);
  431. for (int i = 0; i < 3331; ++i) {
  432. assert_not_equal_int(list.sorted_find(list.data[i]), -1);
  433. }
  434. return pass;
  435. }
  436. s32 main(s32, char**) {
  437. init_printer();
  438. testresult result;
  439. invoke_test(test_array_lists_adding_and_removing);
  440. invoke_test(test_array_lists_sorting);
  441. invoke_test(test_array_lists_searching);
  442. invoke_test(test_array_list_sort_many);
  443. invoke_test(test_stack_array_lists);
  444. invoke_test(test_bucket_allocator);
  445. invoke_test(test_queue);
  446. return 0;
  447. }