test-director.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #include <array>
  2. #include <map>
  3. #include <string>
  4. #include <vector>
  5. #include "director.h"
  6. #include "galaxy.h"
  7. #include "gtest/gtest.h"
  8. #include "utils.h"
  9. /*
  10. How can I add logging, but not really add/use it?
  11. */
  12. #define GTEST_COUT std::cerr << "[ ] [ INFO ]"
  13. namespace {
  14. TEST(director, director_debugging) {
  15. Director dir;
  16. std::vector<std::string> client;
  17. std::vector<std::string> server;
  18. dir.to_client = [&client](const std::string line) -> void {
  19. client.push_back(line);
  20. };
  21. dir.to_server = [&server](const std::string &line,
  22. const std::string &source) -> void {
  23. server.push_back(line);
  24. };
  25. // dir.to_server = [](const std::string &line, const std::string &source) ->
  26. // void { server.push_back(line); }; activating the game gets this over to
  27. // dir.galaxy.username.
  28. dir.username = "test";
  29. // should I setup galaxy-N-test.yaml ? Probably yes.
  30. std::string lines[] = {
  31. "TradeWars Game Server Copyright (C) "
  32. "EIS",
  33. "Selection (? for menu): N",
  34. "Trade Wars 2002 Win32 module now loading.",
  35. " ==-- Trade Wars 2002 --==",
  36. "Enter your choice: ",
  37. "^Command [TL=00:00:00]:[2985] (?=Help)? :"};
  38. for (auto line : lines) {
  39. if (line[0] == '^') {
  40. dir.server_prompt(line.substr(1), line.substr(1));
  41. } else if (line[0] == '!') {
  42. // client input
  43. dir.client_input(line.substr(1));
  44. } else {
  45. dir.server_line(line, line);
  46. }
  47. }
  48. EXPECT_EQ(client[0],
  49. "\rTradeWars Proxy v2++ READY (~ or ESC to activate)\n\r");
  50. client.clear();
  51. dir.client_input("~");
  52. for (auto line : client) {
  53. GTEST_COUT << line << std::endl;
  54. }
  55. }
  56. /* // my save rountine handles sequences. This is a NOP.
  57. TEST(director, director_galaxy_save) {
  58. Director dir;
  59. dir.galaxy.username = "test";
  60. dir.galaxy.game = 'Z';
  61. // This causes a YAML::Node Sequence
  62. dir.galaxy.meta["trade"][10963][0] = 1;
  63. dir.galaxy.save();
  64. EXPECT_TRUE(1) << "No exception, this is good!";
  65. }
  66. */
  67. TEST(director, director_parsing_density) {
  68. Director dir;
  69. std::vector<std::string> client;
  70. std::vector<std::string> server;
  71. dir.to_client = [&client](const std::string line) { client.push_back(line); };
  72. dir.to_server = [&server](const std::string &line,
  73. const std::string &source) {
  74. server.push_back(line);
  75. };
  76. dir.username = "test";
  77. std::string lines[] = {
  78. "TradeWars Game Server Copyright (C) "
  79. "EIS",
  80. "Selection (? for menu): N",
  81. "^Command [TL=00:00:00]:[2985] (?=Help)? :",
  82. "!s",
  83. "Command [TL=00:00:00]:[926] (?=Help)? : S",
  84. "",
  85. "Long Range Scan",
  86. "^Select (H)olo Scan or (D)ensity Scan or (Q)uit? [D] ",
  87. "!d",
  88. "Select (H)olo Scan or (D)ensity Scan or (Q)uit? [D] D",
  89. "",
  90. " Relative Density Scan",
  91. "------------------------------------------------------------------------"
  92. "-----",
  93. "Sector 70 ==> 0 Warps : 6 NavHaz : 0% Anom "
  94. ": No",
  95. "Sector 441 ==> 100 Warps : 2 NavHaz : 0% Anom "
  96. ": No",
  97. "Sector ( 575) ==> 1000 Warps : 2 NavHaz : 15% Anom "
  98. ": Yes",
  99. "Sector 600 ==> 40 Warps : 6 NavHaz : 0% Anom "
  100. ": No",
  101. "Sector 629 ==> 1 Warps : 4 NavHaz : 0% Anom "
  102. ": No",
  103. "Sector 711 ==> 101 Warps : 6 NavHaz : 0% Anom "
  104. ": No",
  105. ""};
  106. for (auto line : lines) {
  107. if (line[0] == '^') {
  108. dir.server_prompt(line.substr(1), line.substr(1));
  109. } else if (line[0] == '!') {
  110. // client input
  111. dir.client_input(line.substr(1));
  112. } else {
  113. dir.server_line(line, line);
  114. }
  115. }
  116. std::array<density, 6> dense = {{{70, 0, 6, 0, false, true},
  117. {441, 100, 2, 0, false, true},
  118. {575, 1000, 2, 15, true, false},
  119. {600, 40, 6, 0, false, true},
  120. {629, 1, 4, 0, false, true},
  121. {711, 101, 6, 0, false, true}}};
  122. for (auto const &s : dense) {
  123. auto d = dir.galaxy.dscan.find(s.sector);
  124. EXPECT_EQ(d, s) << "Sector " << s.sector << " density";
  125. }
  126. // Check that each sector was correctly processed
  127. std::vector<int> sectors = {70, 441, 575, 600, 629, 711};
  128. json sector_data = {{"70",
  129. {{"density", 0},
  130. {"warps", 6},
  131. {"navhaz", 0},
  132. {"anom", false},
  133. {"known", true}}},
  134. {"441",
  135. {{"density", 100},
  136. {"warps", 2},
  137. {"navhaz", 0},
  138. {"anom", false},
  139. {"known", true}}},
  140. {"575",
  141. {{"density", 1000},
  142. {"warps", 2},
  143. {"navhaz", 15},
  144. {"anom", true},
  145. {"known", false}}},
  146. {"600",
  147. {{"density", 40},
  148. {"warps", 6},
  149. {"navhaz", 0},
  150. {"anom", false},
  151. {"known", true}}},
  152. {"629",
  153. {{"density", 1},
  154. {"warps", 4},
  155. {"navhaz", 0},
  156. {"anom", false},
  157. {"known", true}}},
  158. {"711",
  159. {{"density", 101},
  160. {"warps", 6},
  161. {"navhaz", 0},
  162. {"anom", false},
  163. {"known", true}}}};
  164. density_scan &ds = dir.galaxy.dscan;
  165. for (int x = 0; x < ds.pos; ++x) {
  166. int sector = sectors[x];
  167. // json prefers strings for keys.
  168. std::string sector_s = std::to_string(sector);
  169. // GTEST_COUT << "Testing Sector " << sector << std::endl;
  170. EXPECT_EQ(ds.d[x].density, json_int(sector_data[sector_s]["density"]))
  171. << "Sector " << sector << " density";
  172. EXPECT_EQ(ds.d[x].warps, json_int(sector_data[sector_s]["warps"]))
  173. << "Sector " << sector << " warps";
  174. EXPECT_EQ(ds.d[x].navhaz, json_int(sector_data[sector_s]["navhaz"]))
  175. << "Sector " << sector << " navhaz";
  176. EXPECT_EQ(ds.d[x].anomaly, json_bool(sector_data[sector_s]["anom"]))
  177. << "Sector " << sector << " anom";
  178. EXPECT_EQ(ds.d[x].known, json_bool(sector_data[sector_s]["known"]))
  179. << "Sector " << sector << " known";
  180. }
  181. }
  182. } // namespace