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.

data.test.c 11 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. #if defined IZ_MACOS
  2. #include <SDL.h>
  3. #endif
  4. #include <bdd-for-c.h>
  5. #include <stdinc/IZ_stdlib.mock.h>
  6. #include <common/IZ_common.h>
  7. #include <game/data/IZ_list.h>
  8. bool NodeExists(IZ_ListNode** node, u64 _index, IZ_List* list) {
  9. return *((u64*) (*node)->value) == 42069;
  10. }
  11. bool NodeExists2(IZ_ListNode** node, u64 _index, IZ_List* list) {
  12. return *((u64*) (*node)->value) == 69420;
  13. }
  14. bool NodeExists3(IZ_ListNode** node, u64 _index, IZ_List* list) {
  15. return *((u64*) (*node)->value) == 69069;
  16. }
  17. bool NodeDoesNotExist(IZ_ListNode** node, u64 _index, IZ_List* list) {
  18. return *((u64*) (*node)->value) == 55555;
  19. }
  20. bool NodeHasOddValue(IZ_ListNode** node, u64 _index, IZ_List* list) {
  21. return *((u64*) (*node)->value) % 2 == 1;
  22. }
  23. spec("data") {
  24. describe("list") {
  25. describe("Initialize") {
  26. static IZ_List list;
  27. after_each() {
  28. mock_mode(IZ_free, IZ_FREE_CALLS_UNTRACKED);
  29. IZ_ListTeardown(&list);
  30. }
  31. it("sets root to NULL") {
  32. IZ_ListInitialize(&list);
  33. check(
  34. list.root == NULL,
  35. "List not properly initialized. Root: %p", list.root
  36. );
  37. }
  38. }
  39. describe("Teardown") {
  40. static IZ_List list;
  41. before_each() {
  42. static u64 value1 = 69420u;
  43. static u64 value2 = 42069u;
  44. static u64 value3 = 69069u;
  45. IZ_ListInitialize(&list);
  46. IZ_ListAppendNode(&list, &value1, NULL);
  47. IZ_ListAppendNode(&list, &value2, NULL);
  48. IZ_ListAppendNode(&list, &value3, NULL);
  49. mock_mode(IZ_free, IZ_FREE_CALLS_TRACKED);
  50. }
  51. after_each() {
  52. mock_reset(IZ_free);
  53. }
  54. it("removes all nodes from the list") {
  55. mock_set_expected_calls(IZ_free, 3);
  56. IZ_ListTeardown(&list);
  57. unsigned int expected_calls = mock_get_expected_calls(IZ_free);
  58. unsigned int actual_calls = mock_get_actual_calls(IZ_free);
  59. check(
  60. expected_calls == actual_calls,
  61. "Deallocator function call count mismatch. Expected %u, got %u.", expected_calls, actual_calls
  62. );
  63. }
  64. }
  65. describe("AppendNode") {
  66. static IZ_List list;
  67. static IZ_List list2;
  68. before_each() {
  69. IZ_ListInitialize(&list);
  70. }
  71. before_each() {
  72. IZ_ListInitialize(&list2);
  73. static u64 existing_value = 69420u;
  74. static IZ_ListNode* existing_node;
  75. existing_node = IZ_malloc(sizeof(IZ_ListNode));
  76. existing_node->list = &list2;
  77. existing_node->previous = NULL;
  78. existing_node->value = &existing_value;
  79. existing_node->next = NULL;
  80. list2.root = existing_node;
  81. list2.length = 1;
  82. }
  83. after_each() {
  84. mock_reset(IZ_malloc);
  85. }
  86. after_each() {
  87. mock_mode(IZ_free, IZ_FREE_CALLS_UNTRACKED);
  88. IZ_ListTeardown(&list);
  89. }
  90. after_each() {
  91. mock_mode(IZ_free, IZ_FREE_CALLS_UNTRACKED);
  92. IZ_ListTeardown(&list2);
  93. }
  94. it("appends new node to empty list and sets it as root") {
  95. static u64 value = 69420u;
  96. static IZ_ListNode* check_inserted_node;
  97. IZ_ListAppendNode(&list, &value, &check_inserted_node);
  98. u64 added_value = *((u64*) list.root->value);
  99. check(added_value == 69420u, "Node not properly appended. Value: %u", added_value);
  100. check(*((u64*) check_inserted_node->value) == added_value, "Node value not properly set. Value: %u", added_value);
  101. check(
  102. mock_is_called(IZ_malloc),
  103. "Allocator function not called."
  104. );
  105. check(list.length == 1, "Length mismatch.");
  106. }
  107. it("appends new node to non-empty list") {
  108. static u64 value1 = 42069u;
  109. static IZ_ListNode* check_inserted_node;
  110. IZ_ListAppendNode(&list2, &value1, &check_inserted_node);
  111. check(*((u64*) check_inserted_node->value) == value1, "Node value not properly set. Value: %u", value1);
  112. check(*((u64*) list2.root->next->value) == 42069u, "Node not properly appended.");
  113. check(
  114. mock_is_called(IZ_malloc),
  115. "Allocator function not called."
  116. );
  117. check(list2.length == 2, "Length mismatch. Length: %u", list2.length);
  118. }
  119. }
  120. describe("FindFirstNode") {
  121. static IZ_List list;
  122. static u64 value1 = 69420u;
  123. static u64 value2 = 42069u;
  124. before_each() {
  125. IZ_ListInitialize(&list);
  126. IZ_ListAppendNode(&list, &value1, NULL);
  127. IZ_ListAppendNode(&list, &value2, NULL);
  128. }
  129. after_each() {
  130. mock_mode(IZ_free, IZ_FREE_CALLS_UNTRACKED);
  131. IZ_ListTeardown(&list);
  132. }
  133. it("retrieves first node satisfying the filter condition") {
  134. static IZ_ListNode* node;
  135. list.iterator = &list.root;
  136. IZ_ListFindFirstNode(&list, NodeExists, &node);
  137. check(*((u64*) node->value) == 42069u, "Existing node not found.");
  138. }
  139. it("returns NULL when all nodes do not satisfy the filter condition") {
  140. static IZ_ListNode* node;
  141. list.iterator = &list.root;
  142. IZ_ListFindFirstNode(&list, NodeDoesNotExist, &node);
  143. check(node == NULL, "Non-existing node found.");
  144. }
  145. }
  146. describe("DeleteNode") {
  147. static IZ_List list;
  148. before_each() {
  149. static u64 value1 = 69420u;
  150. static u64 value2 = 42069u;
  151. static u64 value3 = 69069u;
  152. IZ_ListInitialize(&list);
  153. IZ_ListAppendNode(&list, &value1, NULL);
  154. IZ_ListAppendNode(&list, &value2, NULL);
  155. IZ_ListAppendNode(&list, &value3, NULL);
  156. mock_mode(IZ_free, IZ_FREE_CALLS_TRACKED);
  157. }
  158. after_each() {
  159. mock_reset(IZ_free);
  160. }
  161. it("removes first node satisfying the filter condition") {
  162. list.iterator = &list.root;
  163. static IZ_ListNode* existing_node;
  164. static IZ_ListNode* check_node;
  165. IZ_ListFindFirstNode(&list, NodeExists, &existing_node);
  166. IZ_ListDeleteNode(existing_node);
  167. check(
  168. mock_is_called(IZ_free),
  169. "Deallocator function not called."
  170. );
  171. check_node = NULL;
  172. list.iterator = &list.root;
  173. IZ_ListFindFirstNode(&list, NodeExists2, &check_node);
  174. check(check_node, "Node supposed to be present in list is absent.");
  175. list.iterator = &list.root;
  176. check_node = NULL;
  177. IZ_ListFindFirstNode(&list, NodeExists, &check_node);
  178. check(!check_node, "Deleted node still present in list.");
  179. check(list.length == 2, "Length mismatch.");
  180. }
  181. }
  182. describe("FindAllNodes") {
  183. static IZ_List list;
  184. static IZ_List out;
  185. before_each() {
  186. static u64 value1 = 69420u;
  187. static u64 value2 = 42069u;
  188. static u64 value3 = 69069u;
  189. IZ_ListInitialize(&list);
  190. IZ_ListAppendNode(&list, &value1, NULL);
  191. IZ_ListAppendNode(&list, &value2, NULL);
  192. IZ_ListAppendNode(&list, &value3, NULL);
  193. mock_mode(IZ_free, IZ_FREE_CALLS_UNTRACKED);
  194. }
  195. before_each() {
  196. IZ_ListInitialize(&out);
  197. }
  198. after_each() {
  199. IZ_ListTeardown(&out);
  200. }
  201. after_each() {
  202. IZ_ListTeardown(&list);
  203. }
  204. it("finds all nodes satisfying the filter condition") {
  205. list.iterator = &list.root;
  206. IZ_ListFindAllNodes(&list, NodeHasOddValue, &out);
  207. check(out.length == 2, "Length mismatch. Length: %u", out.length);
  208. out.iterator = &out.root;
  209. static IZ_ListNode* check_node;
  210. check_node = NULL;
  211. IZ_ListFindFirstNode(&out, NodeExists, &check_node);
  212. check(check_node, "Node supposed to be present in list is absent.");
  213. out.iterator = &out.root;
  214. check_node = NULL;
  215. IZ_ListFindFirstNode(&out, NodeExists3, &check_node);
  216. check(check_node, "Node supposed to be present in list is absent.");
  217. out.iterator = &out.root;
  218. check_node = NULL;
  219. IZ_ListFindFirstNode(&out, NodeExists2, &check_node);
  220. check(!check_node, "Node not supposed to be present in list is present.");
  221. }
  222. }
  223. describe("InsertNodeAtIndex") {
  224. static IZ_List list;
  225. before_each() {
  226. static u64 value1 = 69420u;
  227. static u64 value2 = 42069u;
  228. static u64 value3 = 69069u;
  229. IZ_ListInitialize(&list);
  230. IZ_ListAppendNode(&list, &value1, NULL);
  231. IZ_ListAppendNode(&list, &value2, NULL);
  232. IZ_ListAppendNode(&list, &value3, NULL);
  233. mock_mode(IZ_free, IZ_FREE_CALLS_UNTRACKED);
  234. }
  235. after_each() {
  236. IZ_ListTeardown(&list);
  237. }
  238. it("inserts node at the beginning of the list") {
  239. static u64 value = 1337u;
  240. static IZ_ListNode* check_node;
  241. IZ_ListInsertNodeAtIndex(&list, &value, 0, &check_node);
  242. check(*((u64*) check_node->value) == value, "Incorrect value of inserted node.");
  243. static IZ_ListNode* check_other_node;
  244. list.iterator = &list.root;
  245. check_other_node = NULL;
  246. IZ_ListFindFirstNode(&list, NodeExists, &check_other_node);
  247. check(check_other_node, "Node supposed to be present in list is absent.");
  248. list.iterator = &list.root;
  249. check_other_node = NULL;
  250. IZ_ListFindFirstNode(&list, NodeExists2, &check_other_node);
  251. check(check_other_node, "Node supposed to be present in list is absent.");
  252. list.iterator = &list.root;
  253. check_other_node = NULL;
  254. IZ_ListFindFirstNode(&list, NodeExists3, &check_other_node);
  255. check(check_other_node, "Node supposed to be present in list is absent.");
  256. check(list.length == 4, "Length mismatch.");
  257. //IZ_ListPrintNodeValues(&list);
  258. }
  259. it("inserts node at the end of the list") {
  260. static u64 value = 1337u;
  261. static IZ_ListNode* check_node;
  262. IZ_ListInsertNodeAtIndex(&list, &value, 3, &check_node);
  263. check(*((u64*) check_node->value) == value, "Incorrect value of inserted node.");
  264. static IZ_ListNode* check_other_node;
  265. list.iterator = &list.root;
  266. check_other_node = NULL;
  267. IZ_ListFindFirstNode(&list, NodeExists, &check_other_node);
  268. check(check_other_node, "Node supposed to be present in list is absent.");
  269. list.iterator = &list.root;
  270. check_other_node = NULL;
  271. IZ_ListFindFirstNode(&list, NodeExists2, &check_other_node);
  272. check(check_other_node, "Node supposed to be present in list is absent.");
  273. list.iterator = &list.root;
  274. check_other_node = NULL;
  275. IZ_ListFindFirstNode(&list, NodeExists3, &check_other_node);
  276. check(check_other_node, "Node supposed to be present in list is absent.");
  277. check(list.length == 4, "Length mismatch.");
  278. //IZ_ListPrintNodeValues(&list);
  279. }
  280. it("inserts node in the middle of the list") {
  281. static u64 value = 1337u;
  282. static IZ_ListNode* check_node;
  283. IZ_ListInsertNodeAtIndex(&list, &value, 1, &check_node);
  284. check(*((u64*) check_node->value) == value, "Incorrect value of inserted node.");
  285. static IZ_ListNode* check_other_node;
  286. list.iterator = &list.root;
  287. check_other_node = NULL;
  288. IZ_ListFindFirstNode(&list, NodeExists, &check_other_node);
  289. check(check_other_node, "Node supposed to be present in list is absent.");
  290. list.iterator = &list.root;
  291. check_other_node = NULL;
  292. IZ_ListFindFirstNode(&list, NodeExists2, &check_other_node);
  293. check(check_other_node, "Node supposed to be present in list is absent.");
  294. list.iterator = &list.root;
  295. check_other_node = NULL;
  296. IZ_ListFindFirstNode(&list, NodeExists3, &check_other_node);
  297. check(check_other_node, "Node supposed to be present in list is absent.");
  298. check(list.length == 4, "Length mismatch.");
  299. //IZ_ListPrintNodeValues(&list);
  300. }
  301. }
  302. }
  303. }