test-director.cpp 6.1 KB

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