Research portable Memory game | Исследовать портируемую игру Память
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

215 lines
4.9KB

  1. #include <map>
  2. #include <string>
  3. #include <vector>
  4. #include "cli.h"
  5. #include "cli_Context.h"
  6. #include "memory.h"
  7. #include "memory_Context.h"
  8. #include "shell.h"
  9. std::string memory_test_generateConstPlayfield(
  10. ) {
  11. auto c = memory_createContext();
  12. c.playfieldSize = 2;
  13. c = memory_generateConstPlayfield(c);
  14. if (
  15. c.recentField == "playfieldItems" &&
  16. c.playfieldItems.size() == 4 &&
  17. c.playfieldItems[0] == 0 &&
  18. c.playfieldItems[1] == 0 &&
  19. c.playfieldItems[2] == 1 &&
  20. c.playfieldItems[3] == 1
  21. ) {
  22. return "OK: memory_generateConstPlayfield";
  23. }
  24. return "ERR: memory_generateConstPlayfield";
  25. }
  26. std::string memory_test_selectItem_1x(
  27. ) {
  28. auto c = memory_createContext();
  29. c.playfieldSize = 2;
  30. c = memory_generateConstPlayfield(c);
  31. // Select the first item.
  32. c.selectedId = 0;
  33. c = memory_selectItem(c);
  34. // See if it's in selectedItems now.
  35. if (
  36. c.recentField == "selectedItems" &&
  37. c.selectedItems.size() == 1 &&
  38. c.selectedItems[0] == 0
  39. ) {
  40. return "OK: memory_selectItem_1x";
  41. }
  42. return "ERR: memory_selectItem_1x";
  43. }
  44. std::string memory_test_selectItem_2x(
  45. ) {
  46. auto c = memory_createContext();
  47. c.playfieldSize = 2;
  48. c = memory_generateConstPlayfield(c);
  49. // Select the first two items.
  50. c.selectedId = 0;
  51. c = memory_selectItem(c);
  52. c.selectedId = 1;
  53. c = memory_selectItem(c);
  54. // See if both items are selected now.
  55. if (
  56. c.recentField == "selectedItems" &&
  57. c.selectedItems.size() == 2 &&
  58. c.selectedItems[0] == 0 &&
  59. c.selectedItems[1] == 1
  60. ) {
  61. return "OK: memory_selectItem_2x";
  62. }
  63. return "ERR: memory_selectItem_2x";
  64. }
  65. std::string memory_test_selectItem_3x(
  66. ) {
  67. auto c = memory_createContext();
  68. c.playfieldSize = 2;
  69. c = memory_generateConstPlayfield(c);
  70. // Select three items.
  71. c.selectedId = 0;
  72. c = memory_selectItem(c);
  73. c.selectedId = 1;
  74. c = memory_selectItem(c);
  75. c.selectedId = 2;
  76. c = memory_selectItem(c);
  77. // See if only one (last) item is selected now.
  78. if (
  79. c.recentField == "selectedItems" &&
  80. c.selectedItems.size() == 1 &&
  81. c.selectedItems[0] == 2
  82. ) {
  83. return "OK: memory_selectItem_3x";
  84. }
  85. return "ERR: memory_selectItem_3x";
  86. }
  87. std::string memory_test_shouldDeselectMismatchedItems(
  88. ) {
  89. auto c = memory_createContext();
  90. c.playfieldSize = 2;
  91. c = memory_generateConstPlayfield(c);
  92. // Select two items of different groups.
  93. c.selectedId = 0;
  94. c = memory_selectItem(c);
  95. c.selectedId = 2;
  96. c = memory_selectItem(c);
  97. // Detect mismatching.
  98. c = memory_shouldDeselectMismatchedItems(c);
  99. // See if the two selected items do not match.
  100. if (
  101. c.recentField == "mismatchedItems" &&
  102. c.mismatchedItems.size() == 2 &&
  103. c.mismatchedItems[0] == 0 &&
  104. c.mismatchedItems[1] == 2
  105. ) {
  106. return "OK: memory_shouldDeselectMismatchedItems";
  107. }
  108. return "ERR: memory_shouldDeselectMismatchedItems";
  109. }
  110. std::string memory_test_shouldDeselectMismatchedItems_itemTwice(
  111. ) {
  112. auto c = memory_createContext();
  113. c.playfieldSize = 2;
  114. c = memory_generateConstPlayfield(c);
  115. // Select the same item twice.
  116. c.selectedId = 0;
  117. c = memory_selectItem(c);
  118. c.selectedId = 0;
  119. c = memory_selectItem(c);
  120. // Detect mismatching.
  121. c = memory_shouldDeselectMismatchedItems(c);
  122. // See if the two selected items do not match.
  123. if (
  124. c.recentField == "mismatchedItems" &&
  125. c.mismatchedItems.size() == 1 &&
  126. c.mismatchedItems[0] == 0
  127. ) {
  128. return "OK: memory_shouldDeselectMismatchedItems_itemTwice";
  129. }
  130. return "ERR: memory_shouldDeselectMismatchedItems_itemTwice";
  131. }
  132. std::string memory_test_shouldDetectVictory(
  133. ) {
  134. auto c = memory_createContext();
  135. c.playfieldSize = 2;
  136. c = memory_generateConstPlayfield(c);
  137. // Select the first two items of the same group.
  138. c.selectedId = 0;
  139. c = memory_selectItem(c);
  140. c.selectedId = 1;
  141. c = memory_selectItem(c);
  142. // Hide the first pair.
  143. c = memory_shouldHideMatchingItems(c);
  144. // Select the last two items of the same group.
  145. c.selectedId = 2;
  146. c = memory_selectItem(c);
  147. c.selectedId = 3;
  148. c = memory_selectItem(c);
  149. // Hide the second pair.
  150. c = memory_shouldHideMatchingItems(c);
  151. // Detect victory.
  152. c = memory_shouldDetectVictory(c);
  153. // See if victory has been detected.
  154. if (
  155. c.recentField == "victory" &&
  156. c.victory == true
  157. ) {
  158. return "OK: memory_shouldDetectVictory";
  159. }
  160. return "ERR: memory_shouldDetectVictory";
  161. }
  162. std::string memory_test_shouldHideMatchingItems(
  163. ) {
  164. auto c = memory_createContext();
  165. c.playfieldSize = 2;
  166. c = memory_generateConstPlayfield(c);
  167. // Select two items of the same group.
  168. c.selectedId = 0;
  169. c = memory_selectItem(c);
  170. c.selectedId = 1;
  171. c = memory_selectItem(c);
  172. // Hide matching items.
  173. c = memory_shouldHideMatchingItems(c);
  174. // See if the two selected items match.
  175. if (
  176. c.recentField == "hiddenItems" &&
  177. c.hiddenItems.size() == 2 &&
  178. c.hiddenItems[0] == 0 &&
  179. c.hiddenItems[1] == 1
  180. ) {
  181. return "OK: memory_shouldHideMatchingItems";
  182. }
  183. return "ERR: memory_shouldHideMatchingItems";
  184. }