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.
 
 
 
 
 
 

191 lines
3.8 KiB

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