Research portable Memory game | Исследовать портируемую игру Память
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.

239 lines
5.6KB

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