StackAllocatorTests.cpp 5.3 KB

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