UtilityTests.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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::test(
  74. 10, Core::formatBuffer(buffer, sizeof(buffer), "a{}a#a", 1.0, 2, 3));
  75. Core::testString("a1.00a#a23", buffer);
  76. Core::test(5, Core::formatBuffer(buffer, sizeof(buffer), "aa#ab"));
  77. Core::testString("aa#ab", buffer);
  78. Core::test(7, Core::formatBuffer(buffer, sizeof(buffer), "{{a}{}{{b}", 1));
  79. Core::testString("{a}1{b}", buffer);
  80. Core::test(6, Core::formatBuffer(buffer, 3, "aaaaaa"));
  81. Core::testString("aa", buffer);
  82. Core::test(4, Core::formatBuffer(buffer, 4, "a{}", 456));
  83. Core::testString("a45", buffer);
  84. Core::test(10, Core::formatBuffer(buffer, 4, "{} {} {}", 456, 567, 78));
  85. Core::testString("456", buffer);
  86. Core::test(10, Core::formatBuffer(buffer, 1, "{} {} {}", 456, 567, 78));
  87. Core::testString("", buffer);
  88. Core::test(
  89. 10, Core::formatBuffer(nullptr, 0, "{} {} {}", 456ll, 567l, 78ull));
  90. char c = 'a';
  91. short s = 4;
  92. unsigned char cu = 'h';
  93. unsigned short su = 67;
  94. signed char cs = 'x';
  95. Core::test(
  96. 10, Core::formatBuffer(
  97. buffer, sizeof(buffer), "{} {} {} {} {}", c, s, cu, su, cs));
  98. Core::testString("a 4 h 67 x", buffer);
  99. unsigned char text[] = "fgsdf";
  100. Core::test(5, Core::toString(text, buffer, sizeof(buffer)));
  101. Core::testString("fgsdf", buffer);
  102. }
  103. void testUtility() {
  104. testPopCount();
  105. testIf();
  106. testZeroRellocate();
  107. testMemoryInfoList();
  108. testZeroAllocate();
  109. testSwap();
  110. testSort();
  111. testToString();
  112. }
  113. static void outOfMemory(void*) {
  114. Core::setOutOfMemoryHandler(nullptr, nullptr);
  115. }
  116. [[noreturn]] void testInvalidAllocate() {
  117. Core::setOutOfMemoryHandler(outOfMemory, nullptr);
  118. Core::allocateRaw(0xFFF'FFFF'FFFF);
  119. Core::testTrue(false);
  120. Core::finalizeTests();
  121. Core::exitWithHandler(0);
  122. }
  123. [[noreturn]] void testInvalidReallocate() {
  124. Core::setOutOfMemoryHandler(outOfMemory, nullptr);
  125. void* p = Core::allocateRaw(0xFF);
  126. Core::printMemoryReport();
  127. Core::reallocateRaw(p, 0xFFF'FFFF'FFFF);
  128. Core::testTrue(false);
  129. Core::finalizeTests();
  130. Core::exitWithHandler(0);
  131. }
  132. [[noreturn]] void testInvalidNew() {
  133. Core::setOutOfMemoryHandler(outOfMemory, nullptr);
  134. volatile char* p2 = new char[0xFF];
  135. p2[2] = 3;
  136. Core::printMemoryReport();
  137. #pragma GCC diagnostic push
  138. #pragma GCC diagnostic ignored "-Wpragmas"
  139. #pragma GCC diagnostic ignored "-Wunknown-warning-option"
  140. #pragma GCC diagnostic error "-Wlarger-than=17592186044416"
  141. volatile char* p = Core::newWithSourceN<char>(0xFFF'FFFF'FFFF);
  142. p[3] = 3;
  143. #pragma GCC diagnostic pop
  144. Core::testTrue(false);
  145. Core::finalizeTests();
  146. Core::exitWithHandler(0);
  147. }
  148. [[noreturn]] void testPreCanary() {
  149. #ifdef CHECK_MEMORY
  150. char* p = static_cast<char*>(Core::allocateRaw(16));
  151. p[-1] = 0;
  152. Core::deallocateRaw(p);
  153. Core::testTrue(false);
  154. #endif
  155. Core::finalizeTests();
  156. Core::exitWithHandler(0);
  157. }
  158. [[noreturn]] void testPreCanaryNew() {
  159. #ifdef CHECK_MEMORY
  160. volatile char* p2 = new char[3];
  161. volatile char* p = Core::newWithSourceN<char>(16);
  162. delete[] p2;
  163. Core::deleteWithSourceN(p);
  164. p = Core::newWithSourceN<char>(16);
  165. p[-1] = 0;
  166. Core::deleteWithSourceN(p);
  167. Core::testTrue(false);
  168. #endif
  169. Core::finalizeTests();
  170. Core::exitWithHandler(0);
  171. }
  172. [[noreturn]] void testPreCanaryNewArray() {
  173. #ifdef CHECK_MEMORY
  174. volatile char* p = Core::newWithSource<char>();
  175. p[-1] = 0;
  176. Core::deleteWithSource(p);
  177. Core::testTrue(false);
  178. #endif
  179. Core::finalizeTests();
  180. Core::exitWithHandler(0);
  181. }
  182. [[noreturn]] void testPostCanary() {
  183. #ifdef CHECK_MEMORY
  184. char* p = static_cast<char*>(Core::allocateRaw(16));
  185. p[17] = 0;
  186. Core::deallocateRaw(p);
  187. Core::testTrue(false);
  188. #endif
  189. Core::finalizeTests();
  190. Core::exitWithHandler(0);
  191. }