您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

136 行
3.3 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. #include "./error.hpp"
  11. #include "./hooks.hpp"
  12. #include "./hashmap.hpp"
  13. #include "./error.hpp"
  14. #include "./print.hpp"
  15. u32 hm_hash(u32 u) {
  16. return ((u64)u * 2654435761) % 4294967296;
  17. }
  18. inline bool hm_objects_match(u32 a, u32 b) {
  19. return a == b;
  20. }
  21. struct Key {
  22. int x;
  23. int y;
  24. int z;
  25. };
  26. u32 hm_hash(Key u) {
  27. return ((u.y ^ (u.x << 1)) >> 1) ^ u.z;
  28. }
  29. inline bool hm_objects_match(Key a, Key b) {
  30. return a.x == b.x
  31. && a.y == b.y
  32. && a.z == b.z;
  33. }
  34. auto print_dots(FILE* f) -> u32 {
  35. return print_to_file(f, "...");
  36. }
  37. auto test_printer() -> void {
  38. u32 arr[] = {1,2,3,4,1,1,3};
  39. f32 f_arr[] = {1.1,2.1,3.2};
  40. register_printer("dots", print_dots, Printer_Function_Type::_void);
  41. u32 u1 = -1;
  42. u64 u2 = -1;
  43. char* str;
  44. print_to_string(&str, " - %{dots[5]} %{->} <> %{->,2}\n", &u1, &arr, nullptr);
  45. print("---> %{->char}", str);
  46. print(" - %{dots[3]}\n");
  47. print(" - %{u32} %{u64}\n", u1, u2);
  48. print(" - %{u32} %{u32} %{u32}\n", 2, 5, 7);
  49. print(" - %{f32} %{f32} %{f32}\n", 2.0, 5.0, 7.0);
  50. print(" - %{u32} %{bool} %{->char}\n", 2, true, "hello");
  51. print(" - %{f32[3]}\n", f_arr);
  52. print(" - %{f32,3}\n", 44.9, 55.1, 66.2);
  53. print(" - %{u32[5]}\n", arr);
  54. print(" - %{u32[*]}\n", arr, 4);
  55. print(" - %{u32,5}\n", 1,2,3,4,1,2);
  56. print(" - %{unknown%d}\n", 1);
  57. print(" - %{s32,3}\n", -1,200,-300);
  58. print(" - %{->} <> %{->,2}\n", &u1, &arr, nullptr);
  59. print("%{->char}%{->char}%{->char}",
  60. true ? "general " : "",
  61. false ? "validation " : "",
  62. false ? "performance " : "");
  63. // print("%{->char}%{->char}\n\n", "hallo","");
  64. }
  65. auto test_hm() -> void {
  66. Hash_Map<u32, u32> h1;
  67. h1.alloc();
  68. defer { h1.dealloc(); };
  69. h1.set_object(1, 2);
  70. h1.set_object(2, 4);
  71. h1.set_object(3, 6);
  72. h1.set_object(4, 8);
  73. assert(h1.key_exists(1), "key shoud exist");
  74. assert(h1.key_exists(2), "key shoud exist");
  75. assert(h1.key_exists(3), "key shoud exist");
  76. assert(h1.key_exists(4), "key shoud exist");
  77. assert(!h1.key_exists(5), "key shoud not exist");
  78. assert(h1.get_object(1) == 2, "value should be correct");
  79. assert(h1.get_object(2) == 4, "value should be correct");
  80. assert(h1.get_object(3) == 6, "value should be correct");
  81. assert(h1.get_object(4) == 8, "value should be correct");
  82. Hash_Map<Key, u32> h2;
  83. h2.alloc();
  84. defer { h2.dealloc(); };
  85. h2.set_object({.x = 1, .y = 2, .z = 3}, 1);
  86. h2.set_object({.x = 3, .y = 3, .z = 3}, 3);
  87. assert(h2.key_exists({.x = 1, .y = 2, .z = 3}), "key shoud exist");
  88. assert(h2.key_exists({.x = 3, .y = 3, .z = 3}), "key shoud exist");
  89. assert(h2.get_object({.x = 1, .y = 2, .z = 3}) == 1, "value should be correct");
  90. assert(h2.get_object({.x = 3, .y = 3, .z = 3}) == 3, "value should be correct");
  91. h2.for_each([] (Key k, u32 v, u32 i) {
  92. print("%{s32} %{u32} %{u32}\n", k.x, v, i);
  93. });
  94. }
  95. s32 main(s32 argc, char* argv[]) {
  96. init_printer();
  97. test_printer();
  98. print("done.");
  99. return 0;
  100. }