test-director.cpp 6.3 KB

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