StackAllocatorTests.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include "tests/StackAllocatorTests.h"
  2. #include "tests/Test.h"
  3. #include "utils/Array.h"
  4. #include "memory/StackAllocator.h"
  5. static void testBasicAllocation(Test& test) {
  6. int elements = 6;
  7. StackAllocator::Pointer p = StackAllocator::allocate(4, elements);
  8. test.checkEqual(0, p.pointer, "first int pointer is 0");
  9. test.checkEqual(6, elements, "enough storage for all elements");
  10. StackAllocator::free(p);
  11. }
  12. static void testRepeatedAllocation(Test& test) {
  13. int elements[] = {4, 7, 8, 10, 12};
  14. StackAllocator::Pointer p[5];
  15. for(int i = 0; i < 5; i++) {
  16. p[i] = StackAllocator::allocate(4, elements[i]);
  17. }
  18. test.checkEqual(0, p[0].pointer, "repeated allocation pointer check 1");
  19. test.checkEqual(16, p[1].pointer, "repeated allocation pointer check 2");
  20. test.checkEqual(48, p[2].pointer, "repeated allocation pointer check 3");
  21. test.checkEqual(80, p[3].pointer, "repeated allocation pointer check 4");
  22. test.checkEqual(128, p[4].pointer, "repeated allocation pointer check 5");
  23. test.checkEqual(4, elements[0], "repeated allocation element check 1");
  24. test.checkEqual(7, elements[1], "repeated allocation element check 2");
  25. test.checkEqual(8, elements[2], "repeated allocation element check 3");
  26. test.checkEqual(10, elements[3], "repeated allocation element check 4");
  27. test.checkEqual(12, elements[4], "repeated allocation element check 5");
  28. for(int i = 4; i >= 0; i--) {
  29. StackAllocator::free(p[i]);
  30. }
  31. }
  32. static void testFree(Test& test) {
  33. for(int i = 0; i < 100; i++) {
  34. int a = 3;
  35. StackAllocator::Pointer ap = StackAllocator::allocate(4, a);
  36. test.checkEqual(0, ap.pointer, "free int pointer 1");
  37. int b = 5;
  38. StackAllocator::Pointer bp = StackAllocator::allocate(4, b);
  39. test.checkEqual(16, bp.pointer, "free int pointer 2");
  40. int c = 7;
  41. StackAllocator::Pointer cp = StackAllocator::allocate(4, c);
  42. test.checkEqual(48, cp.pointer, "free int pointer 3");
  43. StackAllocator::free(cp);
  44. StackAllocator::free(bp);
  45. StackAllocator::free(ap);
  46. }
  47. }
  48. static void testOverflow(Test& test) {
  49. int elements = 99999999;
  50. StackAllocator::Pointer p = StackAllocator::allocate(4, elements);
  51. test.checkEqual(true, elements < 99999999, "survives overflow, capped");
  52. StackAllocator::free(p);
  53. }
  54. static void testGrow(Test& test) {
  55. for(int k = 0; k < 1000; k++) {
  56. int elements = 7;
  57. StackAllocator::Pointer p = StackAllocator::allocate(4, elements);
  58. for(int i = 0; i < 100; i++) {
  59. elements += StackAllocator::grow(p, 4, 4);
  60. }
  61. test.checkEqual(0, p.pointer, "grow 1");
  62. test.checkEqual(407, elements, "grow 2");
  63. StackAllocator::free(p);
  64. }
  65. }
  66. static void testNotGrow(Test& test) {
  67. int a = 7;
  68. StackAllocator::Pointer ap = StackAllocator::allocate(4, a);
  69. int b = 7;
  70. StackAllocator::Pointer bp = StackAllocator::allocate(4, b);
  71. int c = 7;
  72. StackAllocator::Pointer cp = StackAllocator::allocate(4, c);
  73. test.checkEqual(0, StackAllocator::grow(ap, 4, 4), "no growth when the pointer is not the last on the stack 1");
  74. test.checkEqual(0, StackAllocator::grow(bp, 4, 4), "no growth when the pointer is not the last on the stack 2");
  75. test.checkEqual(4, StackAllocator::grow(cp, 4, 4), "last element on stack can grow 1");
  76. StackAllocator::free(cp);
  77. test.checkEqual(0, StackAllocator::grow(ap, 4, 4), "no growth when the pointer is not the last on the stack 3");
  78. test.checkEqual(4, StackAllocator::grow(bp, 4, 4), "last element on stack can grow 2");
  79. StackAllocator::free(bp);
  80. test.checkEqual(4, StackAllocator::grow(ap, 4, 4), "last element on stack can grow 3");
  81. StackAllocator::free(ap);
  82. }
  83. static void testArray(Test& test) {
  84. for(int i = 0; i < 100000; i++) {
  85. StackAllocator::Array<int> a1(30);
  86. StackAllocator::Array<int> a2(50);
  87. StackAllocator::Array<int> a3(70);
  88. test.checkEqual(30, a1.getLength(), "free array 1");
  89. test.checkEqual(50, a2.getLength(), "free array 2");
  90. test.checkEqual(70, a3.getLength(), "free array 3");
  91. }
  92. }
  93. static void testArrayGrow(Test& test) {
  94. StackAllocator::Array<int> a(700);
  95. for(int i = 0; i < 100; i++) {
  96. a.grow(2000);
  97. }
  98. test.checkEqual(200700, a.getLength(), "array grow");
  99. }
  100. static void testArrayNotGrow(Test& test) {
  101. StackAllocator::Array<int> a(7);
  102. {
  103. StackAllocator::Array<int> b(7);
  104. {
  105. StackAllocator::Array<int> c(7);
  106. test.checkEqual(0, a.grow(4), "no growth when the pointer is not the last on the stack 1");
  107. test.checkEqual(0, b.grow(4), "no growth when the pointer is not the last on the stack 2");
  108. test.checkEqual(4, c.grow(4), "last element on stack can grow 1");
  109. }
  110. test.checkEqual(0, a.grow(4), "no growth when the pointer is not the last on the stack 3");
  111. test.checkEqual(4, b.grow(4), "last element on stack can grow 2");
  112. }
  113. test.checkEqual(4, a.grow(4), "last element on stack can grow 3");
  114. }
  115. static void testObject(Test& test) {
  116. StackAllocator::Object<Array<int, 1024 * 1024 * 4 >> a;
  117. test.checkEqual(false, a.hasError(), "object fits in stack");
  118. StackAllocator::Object<Array<int, 1024 * 1024 * 100 >> b;
  119. test.checkEqual(true, b.hasError(), "object does not fit in stack");
  120. }
  121. void StackAllocatorTests::test() {
  122. Test test("StackAllocator");
  123. testBasicAllocation(test);
  124. testRepeatedAllocation(test);
  125. testFree(test);
  126. testOverflow(test);
  127. testGrow(test);
  128. testNotGrow(test);
  129. testArray(test);
  130. testArrayGrow(test);
  131. testArrayNotGrow(test);
  132. testObject(test);
  133. test.finalize();
  134. }