Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

453 linhas
9.7 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_array_lists_adding_and_removing() -> testresult {
  114. // test adding and removing
  115. Array_List<s32> list;
  116. list.alloc();
  117. defer {
  118. list.dealloc();
  119. };
  120. list.append(1);
  121. list.append(2);
  122. list.append(3);
  123. list.append(4);
  124. assert_equal_int(list.count, 4);
  125. list.remove_index(0);
  126. assert_equal_int(list.count, 3);
  127. assert_equal_int(list[0], 4);
  128. assert_equal_int(list[1], 2);
  129. assert_equal_int(list[2], 3);
  130. list.remove_index(2);
  131. assert_equal_int(list.count, 2);
  132. assert_equal_int(list[0], 4);
  133. assert_equal_int(list[1], 2);
  134. return pass;
  135. }
  136. proc test_array_lists_sorting() -> testresult {
  137. //
  138. //
  139. // Test simple numbers
  140. //
  141. //
  142. Array_List<s32> list;
  143. list.alloc();
  144. defer {
  145. list.dealloc();
  146. };
  147. list.append(1);
  148. list.append(2);
  149. list.append(3);
  150. list.append(4);
  151. list.sort();
  152. assert_equal_int(is_sorted(list), true);
  153. list.append(4);
  154. list.append(2);
  155. list.append(1);
  156. assert_equal_int(is_sorted(list), false);
  157. list.sort();
  158. assert_equal_int(is_sorted(list), true);
  159. list.clear();
  160. list.extend({
  161. 8023, 7529, 2392, 7110,
  162. 3259, 2484, 9695, 2199,
  163. 6729, 9009, 8429, 7208});
  164. assert_equal_int(is_sorted(list), false);
  165. list.sort();
  166. assert_equal_int(is_sorted(list), true);
  167. //
  168. //
  169. // Test adding and removing
  170. //
  171. //
  172. Array_List<s32> list1;
  173. list1.alloc();
  174. defer {
  175. list1.dealloc();
  176. };
  177. list1.append(1);
  178. list1.append(2);
  179. list1.append(3);
  180. list1.append(4);
  181. list1.sort();
  182. assert_equal_int(list1.count, 4);
  183. assert_equal_int(list1[0], 1);
  184. assert_equal_int(list1[1], 2);
  185. assert_equal_int(list1[2], 3);
  186. assert_equal_int(list1[3], 4);
  187. assert_equal_int(is_sorted(list1), true);
  188. list1.append(0);
  189. list1.append(5);
  190. assert_equal_int(list1.count, 6);
  191. list1.sort();
  192. assert_equal_int(list1[0], 0);
  193. assert_equal_int(list1[1], 1);
  194. assert_equal_int(list1[2], 2);
  195. assert_equal_int(list1[3], 3);
  196. assert_equal_int(list1[4], 4);
  197. assert_equal_int(list1[5], 5);
  198. assert_equal_int(is_sorted(list1), true);
  199. //
  200. //
  201. //
  202. //
  203. // pointer list
  204. Array_List<void*> al;
  205. al.alloc();
  206. defer {
  207. al.dealloc();
  208. };
  209. al.append((void*)0x1703102F100);
  210. al.append((void*)0x1703102F1D8);
  211. al.append((void*)0x1703102F148);
  212. al.append((void*)0x1703102F190);
  213. al.append((void*)0x1703102F190);
  214. al.append((void*)0x1703102F1D8);
  215. assert_equal_int(is_sorted_vp(al), false);
  216. al.sort();
  217. assert_equal_int(is_sorted_vp(al), true);
  218. assert_not_equal_int(al.sorted_find((void*)0x1703102F100), -1);
  219. assert_not_equal_int(al.sorted_find((void*)0x1703102F1D8), -1);
  220. assert_not_equal_int(al.sorted_find((void*)0x1703102F148), -1);
  221. assert_not_equal_int(al.sorted_find((void*)0x1703102F190), -1);
  222. assert_not_equal_int(al.sorted_find((void*)0x1703102F190), -1);
  223. assert_not_equal_int(al.sorted_find((void*)0x1703102F1D8), -1);
  224. return pass;
  225. }
  226. proc test_array_lists_searching() -> testresult {
  227. Array_List<s32> list1;
  228. list1.alloc();
  229. defer {
  230. list1.dealloc();
  231. };
  232. list1.append(1);
  233. list1.append(2);
  234. list1.append(3);
  235. list1.append(4);
  236. s32 index = list1.sorted_find(3);
  237. assert_equal_int(index, 2);
  238. index = list1.sorted_find(1);
  239. assert_equal_int(index, 0);
  240. index = list1.sorted_find(5);
  241. assert_equal_int(index, -1);
  242. return pass;
  243. }
  244. proc test_bucket_allocator() -> testresult {
  245. Bucket_Allocator<s32> ba;
  246. ba.alloc();
  247. defer {
  248. ba.dealloc();
  249. };
  250. s32* s1 = ba.allocate();
  251. s32* s2 = ba.allocate();
  252. s32* s3 = ba.allocate();
  253. s32* s4 = ba.allocate();
  254. s32* s5 = ba.allocate();
  255. *s1 = 1;
  256. *s2 = 2;
  257. *s3 = 3;
  258. *s4 = 4;
  259. *s5 = 5;
  260. s32 wrong_answers = 0;
  261. s32 counter = 1;
  262. ba.for_each([&](s32* s) -> void {
  263. if(counter != *s)
  264. ++wrong_answers;
  265. ++counter;
  266. });
  267. assert_equal_int(wrong_answers, 0);
  268. Bucket_Allocator<s32> ba2;
  269. ba2.alloc();
  270. defer {
  271. ba2.dealloc();
  272. };
  273. s1 = ba2.allocate();
  274. s2 = ba2.allocate();
  275. s3 = ba2.allocate();
  276. s32* s3_copy = ba2.allocate();
  277. s32* s3_copy2 = ba2.allocate();
  278. s32* s3_copy3 = ba2.allocate();
  279. *s3_copy = 3;
  280. ba2.free_object(s3_copy);
  281. s4 = ba2.allocate();
  282. ba2.free_object(s3_copy2);
  283. s5 = ba2.allocate();
  284. ba2.free_object(s3_copy3);
  285. *s1 = 1;
  286. *s2 = 2;
  287. *s3 = 3;
  288. *s4 = 4;
  289. *s5 = 5;
  290. wrong_answers = 0;
  291. counter = 1;
  292. ba2.for_each([&](s32* s) -> void {
  293. if(counter != *s)
  294. ++wrong_answers;
  295. ++counter;
  296. });
  297. assert_equal_int(wrong_answers, 0);
  298. return pass;
  299. }
  300. auto test_array_list_sort_many() -> testresult {
  301. Array_List<s32> list;
  302. list.alloc();
  303. defer {
  304. list.dealloc();
  305. };
  306. for (int i = 0; i < 10000; ++i) {
  307. list.append(rand());
  308. }
  309. assert_equal_int(is_sorted(list), false);
  310. list.sort();
  311. assert_equal_int(is_sorted(list), true);
  312. for (int i = 0; i < 10000; ++i) {
  313. assert_not_equal_int(list.sorted_find(list.data[i]), -1);
  314. }
  315. list.clear();
  316. for (int i = 0; i < 1111; ++i) {
  317. list.append(rand());
  318. }
  319. assert_equal_int(is_sorted(list), false);
  320. list.sort();
  321. assert_equal_int(is_sorted(list), true);
  322. for (int i = 0; i < 1111; ++i) {
  323. assert_not_equal_int(list.sorted_find(list.data[i]), -1);
  324. }
  325. list.clear();
  326. for (int i = 0; i < 3331; ++i) {
  327. list.append(rand());
  328. }
  329. assert_equal_int(is_sorted(list), false);
  330. list.sort();
  331. assert_equal_int(is_sorted(list), true);
  332. for (int i = 0; i < 3331; ++i) {
  333. assert_not_equal_int(list.sorted_find(list.data[i]), -1);
  334. }
  335. return pass;
  336. }
  337. s32 main(s32, char**) {
  338. init_printer();
  339. testresult result;
  340. invoke_test(test_array_lists_adding_and_removing);
  341. invoke_test(test_array_lists_sorting);
  342. invoke_test(test_array_lists_searching);
  343. invoke_test(test_array_list_sort_many);
  344. invoke_test(test_bucket_allocator);
  345. return 0;
  346. }