2D Run-and-gun shooter inspired by One Man's Doomsday, Counter-Strike, and Metal Slug.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

189 lines
3.7 KiB

  1. #include "../../test/IZ_test.h"
  2. #include "../../../__mocks__/SDL_stdinc.mock.h"
  3. #include "IZ_pool.h"
  4. struct DummyInnerStruct {
  5. i8 f;
  6. i8 g;
  7. };
  8. struct DummyStruct {
  9. i8 a;
  10. i16 b;
  11. i32 c;
  12. i64 d;
  13. struct DummyInnerStruct e;
  14. };
  15. spec("memory") {
  16. describe("pool") {
  17. describe("Initialize") {
  18. static IZ_Pool pool;
  19. after_each() {
  20. mock_reset(SDL_memset);
  21. }
  22. after_each() {
  23. mock_reset(SDL_malloc);
  24. }
  25. after_each() {
  26. IZ_PoolTeardown(&pool);
  27. }
  28. it("initializes the pool values") {
  29. mock_set_expected_calls(SDL_memset, 1);
  30. IZ_PoolInitialize(&pool, POOL_MAX_SIZE);
  31. check(
  32. mock_get_expected_calls(SDL_memset) == mock_get_actual_calls(SDL_memset),
  33. "Call count mismatch."
  34. );
  35. }
  36. it("initializes the pool memory") {
  37. IZ_PoolInitialize(&pool, POOL_MAX_SIZE);
  38. check(
  39. mock_is_called(SDL_malloc),
  40. "Memory not allocated."
  41. );
  42. }
  43. }
  44. describe("Allocate") {
  45. static IZ_Pool pool;
  46. after_each() {
  47. IZ_PoolTeardown(&pool);
  48. }
  49. it("assigns contiguous memory") {
  50. IZ_PoolInitialize(&pool, POOL_MAX_SIZE);
  51. void* p1 = IZ_PoolAllocate(&pool, (IZ_PoolAllocationArgs){
  52. .size = sizeof(struct DummyStruct),
  53. .priority = 0,
  54. .timestamp = 0,
  55. })->pointer;
  56. void* p2 = IZ_PoolAllocate(&pool, (IZ_PoolAllocationArgs){
  57. .size = sizeof(u8),
  58. .priority = 0,
  59. .timestamp = 0,
  60. })->pointer;
  61. check(
  62. p2 - p1 == sizeof(struct DummyStruct),
  63. "Allocated memory mismatch."
  64. );
  65. }
  66. it("ignores previously deallocated items") {
  67. IZ_PoolInitialize(&pool, POOL_MAX_SIZE);
  68. void* p1 = IZ_PoolAllocate(&pool, (IZ_PoolAllocationArgs){
  69. .size = sizeof(struct DummyStruct),
  70. .priority = 0,
  71. .timestamp = 0,
  72. })->pointer;
  73. void* p2 = IZ_PoolAllocate(&pool, (IZ_PoolAllocationArgs){
  74. .size = sizeof(u8),
  75. .priority = 0,
  76. .timestamp = 0,
  77. })->pointer;
  78. void* p3 = IZ_PoolAllocate(&pool, (IZ_PoolAllocationArgs){
  79. .size = sizeof(u8),
  80. .priority = 0,
  81. .timestamp = 0,
  82. })->pointer;
  83. IZ_PoolDeallocate(p2);
  84. check(
  85. p3 - p1 == sizeof(struct DummyStruct) + sizeof(u8),
  86. "Allocated memory mismatch."
  87. );
  88. }
  89. it("deallocates old items to make way for new ones when the memory is full") {
  90. IZ_PoolInitialize(&pool, sizeof(u32));
  91. void* p1 = IZ_PoolAllocate(&pool, (IZ_PoolAllocationArgs){
  92. .size = sizeof(u16),
  93. .priority = 0,
  94. .timestamp = 0,
  95. });
  96. IZ_PoolAllocate(&pool, (IZ_PoolAllocationArgs){
  97. .size = sizeof(u8),
  98. .priority = 0,
  99. .timestamp = 1,
  100. });
  101. IZ_PoolAllocate(&pool, (IZ_PoolAllocationArgs){
  102. .size = sizeof(u8),
  103. .priority = 0,
  104. .timestamp = 2,
  105. });
  106. void* p2 = IZ_PoolAllocate(&pool, (IZ_PoolAllocationArgs){
  107. .size = sizeof(u8),
  108. .priority = 0,
  109. .timestamp = 3,
  110. });
  111. check(
  112. p1 == p2,
  113. "Old memory not properly deallocated."
  114. );
  115. void* p3 = IZ_PoolAllocate(&pool, (IZ_PoolAllocationArgs){
  116. .size = sizeof(u8),
  117. .priority = 0,
  118. .timestamp = 4,
  119. });
  120. // FIXME here onwards
  121. check(
  122. p3 == p2 + sizeof(u8),
  123. "Free memory not properly utilized."
  124. );
  125. void* p4 = IZ_PoolAllocate(&pool, (IZ_PoolAllocationArgs){
  126. .size = sizeof(u8),
  127. .priority = 0,
  128. .timestamp = 5,
  129. });
  130. check(
  131. p4 == p3 + sizeof(u8),
  132. "Free memory not properly utilized."
  133. );
  134. }
  135. }
  136. describe("Deallocate") {}
  137. describe("Teardown") {
  138. static IZ_Pool pool;
  139. before_each() {
  140. IZ_PoolInitialize(&pool, POOL_MAX_SIZE);
  141. }
  142. after_each() {
  143. mock_reset(SDL_free);
  144. }
  145. it("frees memory") {
  146. IZ_PoolTeardown(&pool);
  147. check(
  148. mock_is_called(SDL_free),
  149. "Memory not freed."
  150. );
  151. }
  152. }
  153. }
  154. }