UtilityTests.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. module Tests;
  2. import Core.Test;
  3. import Core.ToString;
  4. import Core.Utility;
  5. import Core.Meta;
  6. import Core.Types;
  7. import Core.Std;
  8. static void testPopCount() {
  9. Core::test(4, Core::popCount(0xF));
  10. Core::test(0, Core::popCount(0x0));
  11. Core::test(2, Core::popCount(0x6));
  12. Core::test(7, Core::popCount(0x7F));
  13. Core::test(3, Core::popCount(0x2A));
  14. Core::test(32, Core::popCount(0xFFFF'FFFF));
  15. Core::test(64, Core::popCount(0xFFFF'FFFF'FFFF'FFFF));
  16. Core::test(44, Core::popCount(0xFFFF'0FFF'FFFF));
  17. }
  18. static void testIf() {
  19. Core::testTrue(Core::IsSame<Core::If<true, int, double>, int>);
  20. Core::testTrue(Core::IsSame<Core::If<false, int, double>, double>);
  21. }
  22. static void testZeroRellocate() {
  23. void* buffer = Core::reallocateRaw(nullptr, 16);
  24. Core::testNotNull(buffer);
  25. buffer = Core::reallocateRaw(buffer, 0);
  26. Core::testNull(buffer);
  27. }
  28. static void testMemoryInfoList() {
  29. void* a = Core::allocateRaw(8);
  30. void* b = Core::allocateRaw(8);
  31. void* c = Core::allocateRaw(8);
  32. void* d = Core::allocateRaw(8);
  33. Core::deallocateRaw(b); // remove middle element
  34. Core::deallocateRaw(a); // remove first
  35. Core::deallocateRaw(d); // remove last
  36. Core::deallocateRaw(c); // remove single
  37. Core::deallocateRaw(nullptr);
  38. }
  39. static void testZeroAllocate() {
  40. constexpr size_t n = 1024 * 1024;
  41. void* a = Core::allocateRaw(n);
  42. memset(a, 0, n);
  43. void* b = Core::zeroAllocateRaw(n);
  44. Core::testTrue(memcmp(a, b, n) == 0);
  45. Core::deallocateRaw(a);
  46. Core::deallocateRaw(b);
  47. }
  48. typedef struct {
  49. int i;
  50. i64 d;
  51. } SwapTest;
  52. static void testSwap() {
  53. SwapTest a = {3, 20};
  54. SwapTest b = {7, 30};
  55. Core::swap(a, b);
  56. Core::test(7, a.i);
  57. Core::test(3, b.i);
  58. Core::test(30l, a.d);
  59. Core::test(20l, b.d);
  60. }
  61. static void testSort() {
  62. size_t data[] = {9, 0, 3, 1, 8, 4, 6, 2, 5, 7};
  63. size_t n = 10;
  64. Core::bubbleSort(data, n);
  65. Core::bubbleSort(data, n);
  66. Core::bubbleSort(data, 0);
  67. for(size_t i = 0; i < n; i++) {
  68. Core::test(data[i], i);
  69. }
  70. }
  71. static void testToString() {
  72. char buffer[512];
  73. Core::StringBase b(buffer, sizeof(buffer));
  74. Core::test(10, b.format("a{}a#a", 1.0, 2, 3));
  75. Core::testString("a1.00a#a23", b);
  76. Core::test(5, b.format("aa#ab"));
  77. Core::testString("aa#ab", b);
  78. Core::test(7, b.format("{{a}{}{{b}", 1));
  79. Core::testString("{a}1{b}", b);
  80. b = Core::StringBase(buffer, 3);
  81. Core::test(6, b.format("aaaaaa"));
  82. Core::testString("aa", b);
  83. b = Core::StringBase(buffer, 4);
  84. Core::test(4, b.format("a{}", 456));
  85. Core::testString("a45", b);
  86. b = Core::StringBase(buffer, 4);
  87. Core::test(10, b.format("{} {} {}", 456, 567, 78));
  88. Core::testString("456", b);
  89. b = Core::StringBase(buffer, 1);
  90. Core::test(10, b.format("{} {} {}", 456, 567, 78));
  91. Core::testString("", b);
  92. b = Core::StringBase(nullptr, 0);
  93. Core::test(10, b.format("{} {} {}", 456ll, 567l, 78ull));
  94. char c = 'a';
  95. short s = 4;
  96. unsigned char cu = 'h';
  97. unsigned short su = 67;
  98. signed char cs = 'x';
  99. b = Core::StringBase(buffer, sizeof(buffer));
  100. Core::test(10, b.format("{} {} {} {} {}", c, s, cu, su, cs));
  101. Core::testString("a 4 h 67 x", b);
  102. unsigned char text[] = "fgsdf";
  103. b.clear();
  104. Core::test(5, b.add(text));
  105. Core::testString("fgsdf", buffer);
  106. }
  107. static void testStringFormat() {
  108. char buffer[256];
  109. Core::StringBase s(buffer, sizeof(buffer));
  110. Core::test(3, s.format("X{}X", 5));
  111. Core::testString("X5X", s);
  112. Core::test(7, s.format("X{5}X", 5));
  113. Core::testString("X 5X", s);
  114. Core::test(7, s.format("X{r5}X", 5));
  115. Core::testString("X 5X", s);
  116. Core::test(7, s.format("X{l5}X", 5));
  117. Core::testString("X5 X", s);
  118. s = Core::StringBase(buffer, 0);
  119. Core::test(3, s.format("X{}X", 5));
  120. Core::testString("", s);
  121. Core::test(7, s.format("X{5}X", 5));
  122. Core::testString("", s);
  123. Core::test(7, s.format("X{r5}X", 5));
  124. Core::testString("", s);
  125. Core::test(7, s.format("X{l5}X", 5));
  126. Core::testString("", s);
  127. }
  128. static void testDefaultString() {
  129. Core::StringBase s;
  130. Core::test(3, s.format("X{}X", 5));
  131. Core::test(0, s.getCapacity());
  132. }
  133. void testUtility() {
  134. testPopCount();
  135. testIf();
  136. testZeroRellocate();
  137. testMemoryInfoList();
  138. testZeroAllocate();
  139. testSwap();
  140. testSort();
  141. testToString();
  142. testStringFormat();
  143. testDefaultString();
  144. }
  145. static void outOfMemory(void*) {
  146. Core::setOutOfMemoryHandler(nullptr, nullptr);
  147. }
  148. [[noreturn]] void testInvalidAllocate() {
  149. Core::setOutOfMemoryHandler(outOfMemory, nullptr);
  150. Core::allocateRaw(0xFFF'FFFF'FFFF);
  151. Core::testTrue(false);
  152. Core::finalizeTests();
  153. Core::exitWithHandler(0);
  154. }
  155. [[noreturn]] void testInvalidReallocate() {
  156. Core::setOutOfMemoryHandler(outOfMemory, nullptr);
  157. void* p = Core::allocateRaw(0xFF);
  158. Core::printMemoryReport();
  159. Core::reallocateRaw(p, 0xFFF'FFFF'FFFF);
  160. Core::testTrue(false);
  161. Core::finalizeTests();
  162. Core::exitWithHandler(0);
  163. }
  164. [[noreturn]] void testInvalidNew() {
  165. Core::setOutOfMemoryHandler(outOfMemory, nullptr);
  166. volatile char* p2 = new char[0xFF];
  167. p2[2] = 3;
  168. Core::printMemoryReport();
  169. #pragma GCC diagnostic push
  170. #pragma GCC diagnostic ignored "-Wpragmas"
  171. #pragma GCC diagnostic ignored "-Wunknown-warning-option"
  172. #pragma GCC diagnostic error "-Wlarger-than=17592186044416"
  173. volatile char* p = Core::newWithSourceN<char>(0xFFF'FFFF'FFFF);
  174. p[3] = 3;
  175. #pragma GCC diagnostic pop
  176. Core::testTrue(false);
  177. Core::finalizeTests();
  178. Core::exitWithHandler(0);
  179. }
  180. [[noreturn]] void testPreCanary() {
  181. #ifdef CHECK_MEMORY
  182. char* p = static_cast<char*>(Core::allocateRaw(16));
  183. p[-1] = 0;
  184. Core::deallocateRaw(p);
  185. Core::testTrue(false);
  186. #endif
  187. Core::finalizeTests();
  188. Core::exitWithHandler(0);
  189. }
  190. [[noreturn]] void testPreCanaryNew() {
  191. #ifdef CHECK_MEMORY
  192. volatile char* p2 = new char[3];
  193. volatile char* p = Core::newWithSourceN<char>(16);
  194. delete[] p2;
  195. Core::deleteWithSourceN(p);
  196. p = Core::newWithSourceN<char>(16);
  197. p[-1] = 0;
  198. Core::deleteWithSourceN(p);
  199. Core::testTrue(false);
  200. #endif
  201. Core::finalizeTests();
  202. Core::exitWithHandler(0);
  203. }
  204. [[noreturn]] void testPreCanaryNewArray() {
  205. #ifdef CHECK_MEMORY
  206. volatile char* p = Core::newWithSource<char>();
  207. p[-1] = 0;
  208. Core::deleteWithSource(p);
  209. Core::testTrue(false);
  210. #endif
  211. Core::finalizeTests();
  212. Core::exitWithHandler(0);
  213. }
  214. [[noreturn]] void testPostCanary() {
  215. #ifdef CHECK_MEMORY
  216. char* p = static_cast<char*>(Core::allocateRaw(16));
  217. p[17] = 0;
  218. Core::deallocateRaw(p);
  219. Core::testTrue(false);
  220. #endif
  221. Core::finalizeTests();
  222. Core::exitWithHandler(0);
  223. }