test-director.cpp 6.2 KB

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