2
0

dataChunkerTest.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2023-2024 tgemit contributors.
  3. // See AUTHORS file and git repository for contributor information.
  4. //
  5. // SPDX-License-Identifier: MIT
  6. //-----------------------------------------------------------------------------
  7. #ifdef TORQUE_TESTS_ENABLED
  8. #include "testing/unitTesting.h"
  9. #include "core/dataChunker.h"
  10. struct TestClassChunkerStruct
  11. {
  12. U32 value;
  13. U32 value2;
  14. TestClassChunkerStruct()
  15. {
  16. value = 0xC001B33F;
  17. value2 = 0x10101010;
  18. }
  19. ~TestClassChunkerStruct()
  20. {
  21. value = 0;
  22. value2 = 0;
  23. }
  24. };
  25. TEST(BaseDataChunkerTest, BaseDataChunker_Should_Function_Correctly)
  26. {
  27. BaseDataChunker<TestClassChunkerStruct> testChunks(1024);
  28. BaseDataChunker<U32> testChunk4(1024);
  29. BaseDataChunker<U64> testChunk8(1024);
  30. EXPECT_TRUE(testChunks.countUsedBlocks() == 0);
  31. EXPECT_TRUE(testChunk4.countUsedBlocks() == 0);
  32. EXPECT_TRUE(testChunk8.countUsedBlocks() == 0);
  33. testChunks.alloc(1);
  34. testChunk4.alloc(1);
  35. testChunk8.alloc(1);
  36. EXPECT_TRUE(testChunks.countUsedBlocks() == 1);
  37. EXPECT_TRUE(testChunk4.countUsedBlocks() == 1);
  38. EXPECT_TRUE(testChunk8.countUsedBlocks() == 1);
  39. testChunks.alloc(1);
  40. testChunk4.alloc(1);
  41. testChunk8.alloc(1);
  42. EXPECT_TRUE(testChunks.countUsedBlocks() == 1);
  43. EXPECT_TRUE(testChunk4.countUsedBlocks() == 1);
  44. EXPECT_TRUE(testChunk8.countUsedBlocks() == 1);
  45. EXPECT_TRUE(testChunks.countUsedBytes() == (sizeof(TestClassChunkerStruct) * 2));
  46. EXPECT_TRUE(testChunk4.countUsedBytes() == (sizeof(U32) * 2));
  47. EXPECT_TRUE(testChunk8.countUsedBytes() == (sizeof(U64) * 2));
  48. testChunks.freeBlocks(true);
  49. testChunk4.freeBlocks(true);
  50. testChunk8.freeBlocks(true);
  51. EXPECT_TRUE(testChunks.countUsedBlocks() == 1);
  52. EXPECT_TRUE(testChunk4.countUsedBlocks() == 1);
  53. EXPECT_TRUE(testChunk8.countUsedBlocks() == 1);
  54. testChunks.freeBlocks(false);
  55. testChunk4.freeBlocks(false);
  56. testChunk8.freeBlocks(false);
  57. EXPECT_TRUE(testChunks.countUsedBlocks() == 0);
  58. EXPECT_TRUE(testChunk4.countUsedBlocks() == 0);
  59. EXPECT_TRUE(testChunk8.countUsedBlocks() == 0);
  60. testChunks.setChunkSize(sizeof(TestClassChunkerStruct));
  61. testChunks.alloc(1);
  62. EXPECT_TRUE(testChunks.countUsedBlocks() == 1);
  63. testChunks.alloc(1);
  64. EXPECT_TRUE(testChunks.countUsedBlocks() == 2);
  65. }
  66. TEST(DataChunkerTest, DataChunker_Should_Function_Correctly)
  67. {
  68. DataChunker testChunk(1024);
  69. testChunk.alloc(1024);
  70. EXPECT_TRUE(testChunk.countUsedBlocks() == 1);
  71. testChunk.alloc(1024);
  72. EXPECT_TRUE(testChunk.countUsedBlocks() == 2);
  73. testChunk.alloc(4096);
  74. EXPECT_TRUE(testChunk.countUsedBytes() == (1024+1024+4096));
  75. EXPECT_TRUE(testChunk.countUsedBlocks() == 3);
  76. testChunk.alloc(12);
  77. EXPECT_TRUE(testChunk.countUsedBlocks() == 4);
  78. testChunk.alloc(12);
  79. EXPECT_TRUE(testChunk.countUsedBlocks() == 4);
  80. U32 reqEls = AlignedBufferAllocator<uintptr_t>::calcRequiredElementSize(12) * sizeof(uintptr_t);
  81. EXPECT_TRUE(testChunk.countUsedBytes() == (1024+1024+4096+reqEls+reqEls));
  82. testChunk.freeBlocks(true);
  83. EXPECT_TRUE(testChunk.countUsedBlocks() == 1);
  84. testChunk.freeBlocks(false);
  85. EXPECT_TRUE(testChunk.countUsedBlocks() == 0);
  86. // Large block cases
  87. testChunk.alloc(8192);
  88. EXPECT_TRUE(testChunk.countUsedBlocks() == 1);
  89. testChunk.freeBlocks(true);
  90. EXPECT_TRUE(testChunk.countUsedBlocks() == 1);
  91. testChunk.alloc(8192);
  92. testChunk.alloc(1024);
  93. EXPECT_TRUE(testChunk.countUsedBlocks() == 2);
  94. testChunk.freeBlocks(true);
  95. EXPECT_TRUE(testChunk.countUsedBlocks() == 1);
  96. testChunk.freeBlocks(false);
  97. EXPECT_TRUE(testChunk.countUsedBlocks() == 0);
  98. // Instead using the chunk size
  99. for (U32 i=0; i<8; i++)
  100. {
  101. testChunk.alloc(1024);
  102. }
  103. EXPECT_TRUE(testChunk.countUsedBlocks() == 8);
  104. testChunk.freeBlocks(false);
  105. EXPECT_TRUE(testChunk.countUsedBlocks() == 0);
  106. }
  107. TEST(ChunkerTest,Chunker_Should_Function_Correctly)
  108. {
  109. Chunker<TestClassChunkerStruct> foo;
  110. TestClassChunkerStruct* value = foo.alloc();
  111. EXPECT_TRUE(value->value != 0xC001B33F);
  112. EXPECT_TRUE(value->value2 != 0x10101010);
  113. // Should otherwise just act like DataChunker
  114. }
  115. TEST(MultiTypedChunkerTest,MultiTypedChunker_Should_Function_Correctly)
  116. {
  117. struct TVS1
  118. {
  119. int a;
  120. int b;
  121. };
  122. struct TVS2
  123. {
  124. int a;
  125. int b;
  126. int c;
  127. };
  128. MultiTypedChunker chunker;
  129. TVS1* v1 = chunker.alloc<TVS1>();
  130. TVS2* v2 = chunker.alloc<TVS2>();
  131. TVS2* v3 = chunker.alloc<TVS2>();
  132. EXPECT_TRUE(((U8*)v2) - ((U8*)v1) == sizeof(TVS1));
  133. EXPECT_TRUE(((U8*)v3) - ((U8*)v2) == AlignedBufferAllocator<MultiTypedChunker::AlignmentType>::calcRequiredPaddedByteSize(sizeof(TVS2)));
  134. }
  135. TEST(ChunkerFreeClassListTest,ChunkerFreeClassList_Should_Function_Correctly)
  136. {
  137. TestClassChunkerStruct list[5];
  138. ChunkerFreeClassList<TestClassChunkerStruct> freeListTest;
  139. // Push & pop works as expected
  140. EXPECT_TRUE(freeListTest.isEmpty() == true);
  141. freeListTest.push((ChunkerFreeClassList<TestClassChunkerStruct>*)&list[0]);
  142. EXPECT_TRUE(freeListTest.isEmpty() == false);
  143. freeListTest.push((ChunkerFreeClassList<TestClassChunkerStruct>*)&list[4]);
  144. EXPECT_TRUE(freeListTest.pop() == &list[4]);
  145. EXPECT_TRUE(freeListTest.pop() == &list[0]);
  146. EXPECT_TRUE(freeListTest.pop() == NULL);
  147. // Reset clears list head
  148. freeListTest.push((ChunkerFreeClassList<TestClassChunkerStruct>*)&list[4]);
  149. freeListTest.reset();
  150. EXPECT_TRUE(freeListTest.pop() == NULL);
  151. }
  152. TEST(FreeListChunkerTest, FreeListChunkerTest_Should_Function_Correctly)
  153. {
  154. FreeListChunker<TestClassChunkerStruct> testFreeList;
  155. TestClassChunkerStruct* s1 = testFreeList.alloc();
  156. TestClassChunkerStruct* s2 = testFreeList.alloc();
  157. // Allocation is sequential
  158. EXPECT_TRUE(s2 > s1);
  159. EXPECT_TRUE(((s2 - s1) == 1));
  160. testFreeList.free(s1);
  161. // But previous reallocations are reused
  162. TestClassChunkerStruct* s3 = testFreeList.alloc();
  163. TestClassChunkerStruct* s4 = testFreeList.alloc();
  164. EXPECT_TRUE(s1 == s3);
  165. EXPECT_TRUE(((s4 - s2) == 1)); // continues from previous free alloc
  166. // Check sharing
  167. FreeListChunker<TestClassChunkerStruct> sharedChunker(testFreeList.getChunker());
  168. s2 = testFreeList.alloc();
  169. EXPECT_TRUE(((s2 - s4) == 1));
  170. }
  171. TEST(ClassChunkerTest, ClassChunker_Should_Function_Correctly)
  172. {
  173. ClassChunker<TestClassChunkerStruct> testClassList;
  174. TestClassChunkerStruct* s1 = testClassList.alloc();
  175. TestClassChunkerStruct* s2 = testClassList.alloc();
  176. // Allocation is sequential
  177. EXPECT_TRUE(s2 > s1);
  178. EXPECT_TRUE(((s2 - s1) == 1));
  179. testClassList.free(s1);
  180. EXPECT_TRUE(s1->value == 0);
  181. EXPECT_TRUE(s1->value2 == 0);
  182. // But previous reallocations are reused
  183. TestClassChunkerStruct* s3 = testClassList.alloc();
  184. TestClassChunkerStruct* s4 = testClassList.alloc();
  185. EXPECT_TRUE(s1 == s3);
  186. EXPECT_TRUE(((s4 - s2) == 1)); // continues from previous free alloc
  187. // Values should be initialized correctly for all allocs at this point
  188. EXPECT_TRUE(s1->value == 0xC001B33F);
  189. EXPECT_TRUE(s1->value2 == 0x10101010);
  190. EXPECT_TRUE(s2->value == 0xC001B33F);
  191. EXPECT_TRUE(s2->value2 == 0x10101010);
  192. EXPECT_TRUE(s3->value == 0xC001B33F);
  193. EXPECT_TRUE(s3->value2 == 0x10101010);
  194. EXPECT_TRUE(s4->value == 0xC001B33F);
  195. EXPECT_TRUE(s4->value2 == 0x10101010);
  196. // Should still be valid if using freeBlocks
  197. testClassList.freeBlocks(true);
  198. EXPECT_TRUE(s1->value == 0xC001B33F);
  199. EXPECT_TRUE(s1->value2 == 0x10101010);
  200. EXPECT_TRUE(s2->value == 0xC001B33F);
  201. EXPECT_TRUE(s2->value2 == 0x10101010);
  202. EXPECT_TRUE(s3->value == 0xC001B33F);
  203. EXPECT_TRUE(s3->value2 == 0x10101010);
  204. EXPECT_TRUE(s4->value == 0xC001B33F);
  205. EXPECT_TRUE(s4->value2 == 0x10101010);
  206. }
  207. TEST(ThreeTieredChunkerTest,ThreeTieredChunker_Should_Function_Correctly)
  208. {
  209. struct TThreeSA
  210. {
  211. uintptr_t a;
  212. };
  213. struct TThreeSB
  214. {
  215. uintptr_t a;
  216. uintptr_t b;
  217. };
  218. struct TThreeSC
  219. {
  220. uintptr_t a;
  221. uintptr_t b;
  222. uintptr_t c;
  223. };
  224. struct TThreeSD
  225. {
  226. uintptr_t a;
  227. uintptr_t b;
  228. uintptr_t c;
  229. uintptr_t d;
  230. };
  231. ThreeTieredChunker<TThreeSA, TThreeSB, TThreeSC> threeChunker;
  232. // Alloc should alloc in the correct lists
  233. auto h1 = threeChunker.alloc(sizeof(TThreeSA));
  234. auto h2 = threeChunker.alloc(sizeof(TThreeSB));
  235. auto h3 = threeChunker.alloc(sizeof(TThreeSC));
  236. auto h4 = threeChunker.alloc(sizeof(TThreeSD));
  237. EXPECT_TRUE(threeChunker.getT1Chunker().isManagedByChunker(h3.ptr) == false);
  238. EXPECT_TRUE(threeChunker.getT2Chunker().isManagedByChunker(h3.ptr) == false);
  239. EXPECT_TRUE(threeChunker.getT3Chunker().isManagedByChunker(h3.ptr) == true);
  240. EXPECT_TRUE(h3.tier == 3);
  241. EXPECT_TRUE(threeChunker.getT1Chunker().isManagedByChunker(h2.ptr) == false);
  242. EXPECT_TRUE(threeChunker.getT2Chunker().isManagedByChunker(h2.ptr) == true);
  243. EXPECT_TRUE(threeChunker.getT3Chunker().isManagedByChunker(h2.ptr) == false);
  244. EXPECT_TRUE(h2.tier == 2);
  245. EXPECT_TRUE(threeChunker.getT1Chunker().isManagedByChunker(h1.ptr) == true);
  246. EXPECT_TRUE(threeChunker.getT2Chunker().isManagedByChunker(h1.ptr) == false);
  247. EXPECT_TRUE(threeChunker.getT3Chunker().isManagedByChunker(h1.ptr) == false);
  248. EXPECT_TRUE(h1.tier == 1);
  249. EXPECT_TRUE(threeChunker.getT1Chunker().isManagedByChunker(h4.ptr) == false);
  250. EXPECT_TRUE(threeChunker.getT2Chunker().isManagedByChunker(h4.ptr) == false);
  251. EXPECT_TRUE(threeChunker.getT3Chunker().isManagedByChunker(h4.ptr) == false);
  252. EXPECT_TRUE(h4.tier == 0);
  253. threeChunker.free(h1);
  254. threeChunker.free(h2);
  255. threeChunker.free(h3);
  256. threeChunker.free(h4);
  257. EXPECT_TRUE(h1.ptr == NULL);
  258. EXPECT_TRUE(h2.ptr == NULL);
  259. EXPECT_TRUE(h3.ptr == NULL);
  260. EXPECT_TRUE(h4.ptr == NULL);
  261. // freeBlocks should also clear ALL the list heads
  262. EXPECT_FALSE(threeChunker.getT1Chunker().getFreeListHead().isEmpty());
  263. EXPECT_FALSE(threeChunker.getT2Chunker().getFreeListHead().isEmpty());
  264. EXPECT_FALSE(threeChunker.getT3Chunker().getFreeListHead().isEmpty());
  265. threeChunker.freeBlocks(false);
  266. EXPECT_TRUE(threeChunker.getT1Chunker().getFreeListHead().isEmpty());
  267. EXPECT_TRUE(threeChunker.getT2Chunker().getFreeListHead().isEmpty());
  268. EXPECT_TRUE(threeChunker.getT3Chunker().getFreeListHead().isEmpty());
  269. }
  270. #endif