2
0

frameAllocatorTest.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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/frameAllocator.h"
  10. struct TestAlignmentStruct
  11. {
  12. U64 values[4];
  13. };
  14. TEST(AlignedBufferAllocatorTest, AlignedBufferAllocator_Should_Function_Correctly)
  15. {
  16. AlignedBufferAllocator<U32> ba4;
  17. AlignedBufferAllocator<U64> ba8;
  18. AlignedBufferAllocator<TestAlignmentStruct> bas;
  19. const U32 bufSize32 = (sizeof(TestAlignmentStruct) / 4) * 20;
  20. U32 testAlignmentBuffer[bufSize32];
  21. for (U32 i=0; i<bufSize32; i++)
  22. {
  23. testAlignmentBuffer[i] = i;
  24. }
  25. EXPECT_TRUE(ba4.calcRequiredElementSize(20) == 5);
  26. EXPECT_TRUE(ba8.calcRequiredElementSize(20) == 3);
  27. EXPECT_TRUE(bas.calcRequiredElementSize(20) == 1);
  28. EXPECT_TRUE(bas.calcRequiredElementSize(32) == 1);
  29. EXPECT_TRUE(bas.calcRequiredElementSize(33) == 2);
  30. EXPECT_TRUE(bas.calcRequiredElementSize(64) == 2);
  31. EXPECT_TRUE(ba4.calcMaxElementSize(20) == 5);
  32. EXPECT_TRUE(ba8.calcMaxElementSize(20) == 2);
  33. EXPECT_TRUE(bas.calcMaxElementSize(20) == 0);
  34. EXPECT_TRUE(bas.calcMaxElementSize(32) == 1);
  35. EXPECT_TRUE(bas.calcMaxElementSize(33) == 1);
  36. EXPECT_TRUE(bas.calcMaxElementSize(64) == 2);
  37. ba4.initWithBytes((U32*)testAlignmentBuffer, sizeof(testAlignmentBuffer));
  38. ba8.initWithBytes((U64*)testAlignmentBuffer, sizeof(testAlignmentBuffer));
  39. bas.initWithBytes((TestAlignmentStruct*)testAlignmentBuffer, sizeof(testAlignmentBuffer));
  40. EXPECT_TRUE(ba4.getElementsLeft() == 160);
  41. EXPECT_TRUE(ba8.getElementsLeft() == 80);
  42. EXPECT_TRUE(bas.getElementsLeft() == 20);
  43. EXPECT_TRUE(ba4.getSizeBytes() == 640);
  44. EXPECT_TRUE(ba8.getSizeBytes() == 640);
  45. EXPECT_TRUE(bas.getSizeBytes() == 640);
  46. EXPECT_TRUE(ba4.allocElements(1) == &testAlignmentBuffer[0]);
  47. EXPECT_TRUE(ba4.getPosition() == 1);
  48. EXPECT_TRUE(ba4.getPositionBytes() == 4);
  49. EXPECT_TRUE(ba4.getElementsLeft() == 159);
  50. EXPECT_TRUE(ba4.allocElements(7) == &testAlignmentBuffer[1]);
  51. EXPECT_TRUE(ba4.getPosition() == 8);
  52. EXPECT_TRUE(ba4.getPositionBytes() == 32);
  53. EXPECT_TRUE(ba4.getElementsLeft() == 152);
  54. ba4.setPosition(100);
  55. EXPECT_TRUE(ba4.allocElements(1) == &testAlignmentBuffer[100]);
  56. EXPECT_TRUE(ba4.getPosition() == 101);
  57. EXPECT_TRUE(ba4.getPositionBytes() == 404);
  58. EXPECT_TRUE(ba4.getElementsLeft() == 59);
  59. ba4.setPosition(160);
  60. EXPECT_TRUE(ba4.allocElements(1) == NULL);
  61. EXPECT_TRUE(ba4.getPosition() == 160);
  62. EXPECT_TRUE(ba4.getPositionBytes() == (160*4));
  63. EXPECT_TRUE(ba4.getElementsLeft() == 0);
  64. }
  65. TEST(FrameAllocatorTest, FrameAllocator_Should_Function_Correctly)
  66. {
  67. // NOTE: assuming alloc and destroy already work
  68. EXPECT_TRUE(FrameAllocator::getWaterMark() == 0);
  69. FrameAllocator::setWaterMark(100);
  70. EXPECT_TRUE(FrameAllocator::getWaterMark() == 100);
  71. FrameAllocator::setWaterMark(104);
  72. EXPECT_TRUE(FrameAllocator::getWaterMark() == 104);
  73. FrameAllocator::alloc(1);
  74. EXPECT_TRUE(FrameAllocator::getWaterMark() == 105);
  75. FrameAllocator::alloc(5);
  76. EXPECT_TRUE(FrameAllocator::getWaterMark() == 107); // 5 bytes == 2 ints
  77. FrameAllocator::setWaterMark(0);
  78. FrameAllocator::alloc(1);
  79. EXPECT_TRUE(FrameAllocator::getWaterMarkBytes() == 4);
  80. FrameAllocator::setWaterMark(0);
  81. }
  82. TEST(FrameAllocatorMarker, FrameAllocatorMarker_Should_Function_Correctly)
  83. {
  84. U32 markerValue = 0;
  85. FrameAllocator::setWaterMark(8);
  86. // Marker should act as a bookmark for the FrameAllocator
  87. {
  88. FrameAllocatorMarker marker;
  89. FrameAllocator::alloc(100);
  90. markerValue = FrameAllocator::getWaterMark();
  91. EXPECT_TRUE(markerValue != 8);
  92. marker.alloc(4);
  93. EXPECT_TRUE(markerValue != FrameAllocator::getWaterMark());
  94. }
  95. // Going out of scope sets watermark
  96. EXPECT_TRUE(FrameAllocator::getWaterMark() == 8);
  97. }
  98. static U32 gFTDestructTest = 0;
  99. TEST(FrameTempTest, FrameTempShould_Function_Correctly)
  100. {
  101. FrameAllocator::setWaterMark(0);
  102. {
  103. FrameTemp<TestAlignmentStruct> fooTemp(20);
  104. EXPECT_TRUE(FrameAllocator::getWaterMarkBytes() == sizeof(TestAlignmentStruct)*20);
  105. EXPECT_TRUE(&fooTemp[0] == fooTemp.address());
  106. EXPECT_TRUE((&fooTemp[1] - &fooTemp[0]) == 1);
  107. EXPECT_TRUE(fooTemp.getObjectCount() == 20);
  108. EXPECT_TRUE(fooTemp.size() == 20);
  109. const FrameTemp<TestAlignmentStruct>& fooC = fooTemp;
  110. EXPECT_TRUE(&fooC[0] == fooC.address());
  111. EXPECT_TRUE((&fooC[1] - &fooC[0]) == 1);
  112. EXPECT_TRUE(fooC.getObjectCount() == 20);
  113. EXPECT_TRUE(fooC.size() == 20);
  114. // Accessors should work
  115. // Call the overloaded operators by name
  116. TestAlignmentStruct& value = fooTemp.operator*();
  117. TestAlignmentStruct** ptr = fooTemp.operator&();
  118. const TestAlignmentStruct* constPtr = fooTemp.operator const TestAlignmentStruct * ();
  119. TestAlignmentStruct& ref = fooTemp.operator TestAlignmentStruct & ();
  120. const TestAlignmentStruct& constRef = fooTemp.operator const TestAlignmentStruct & ();
  121. TestAlignmentStruct copy = fooTemp.operator TestAlignmentStruct();
  122. EXPECT_TRUE(*ptr == fooTemp.address());
  123. EXPECT_TRUE(&value == fooTemp.address());
  124. EXPECT_TRUE(constPtr == fooTemp.address());
  125. EXPECT_TRUE(&ref == fooTemp.address());
  126. EXPECT_TRUE(&constRef == fooTemp.address());
  127. EXPECT_TRUE(&copy != fooTemp.address());
  128. // Same for fooC
  129. const TestAlignmentStruct& Cvalue = fooC.operator*();
  130. TestAlignmentStruct* const* Cptr = fooC.operator&();
  131. const TestAlignmentStruct* CconstPtr = fooC.operator const TestAlignmentStruct * ();
  132. const TestAlignmentStruct& CconstRef = fooC.operator const TestAlignmentStruct & ();
  133. EXPECT_TRUE(*Cptr == fooC.address());
  134. EXPECT_TRUE(&Cvalue == fooC.address());
  135. EXPECT_TRUE(CconstPtr == fooC.address());
  136. EXPECT_TRUE(&CconstRef == fooC.address());
  137. EXPECT_TRUE(&ref == fooC.address());
  138. EXPECT_TRUE(&constRef == fooC.address());
  139. }
  140. // Exiting scope sets watermark
  141. EXPECT_TRUE(FrameAllocator::getWaterMarkBytes() == 0);
  142. // Test the destructor actually gets called
  143. struct FTDestructTest
  144. {
  145. ~FTDestructTest()
  146. {
  147. gFTDestructTest++;
  148. }
  149. };
  150. {
  151. gFTDestructTest = 0;
  152. FrameTemp<FTDestructTest> foo2Temp(10);
  153. }
  154. EXPECT_TRUE(gFTDestructTest == 10);
  155. }
  156. #endif