test-director.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. TEST(director, director_galaxy_save) {
  49. Director dir;
  50. dir.galaxy.username = "test";
  51. dir.galaxy.game = 'Z';
  52. // This causes a YAML::Node Sequence
  53. dir.galaxy.meta["trade"][10963][0] = 1;
  54. dir.galaxy.save();
  55. EXPECT_TRUE(1) << "No exception, this is good!";
  56. }
  57. TEST(director, director_parsing_density) {
  58. Director dir;
  59. std::vector<std::string> client;
  60. std::vector<std::string> server;
  61. dir.to_client = [&client](const std::string line) { client.push_back(line); };
  62. dir.to_server = [&server](const std::string line) { server.push_back(line); };
  63. dir.username = "test";
  64. std::string lines[] = {
  65. "TradeWars Game Server Copyright (C) "
  66. "EIS",
  67. "Selection (? for menu): N",
  68. "^Command [TL=00:00:00]:[2985] (?=Help)? :",
  69. "!s",
  70. "Command [TL=00:00:00]:[926] (?=Help)? : S",
  71. "",
  72. "Long Range Scan",
  73. "^Select (H)olo Scan or (D)ensity Scan or (Q)uit? [D] ",
  74. "!d",
  75. "Select (H)olo Scan or (D)ensity Scan or (Q)uit? [D] D",
  76. "",
  77. " Relative Density Scan",
  78. "------------------------------------------------------------------------"
  79. "-----",
  80. "Sector 70 ==> 0 Warps : 6 NavHaz : 0% Anom "
  81. ": No",
  82. "Sector 441 ==> 100 Warps : 2 NavHaz : 0% Anom "
  83. ": No",
  84. "Sector ( 575) ==> 1000 Warps : 2 NavHaz : 15% Anom "
  85. ": Yes",
  86. "Sector 600 ==> 40 Warps : 6 NavHaz : 0% Anom "
  87. ": No",
  88. "Sector 629 ==> 1 Warps : 4 NavHaz : 0% Anom "
  89. ": No",
  90. "Sector 711 ==> 101 Warps : 6 NavHaz : 0% Anom "
  91. ": No",
  92. ""};
  93. for (auto line : lines) {
  94. if (line[0] == '^') {
  95. dir.server_prompt(line.substr(1), line.substr(1));
  96. } else if (line[0] == '!') {
  97. // client input
  98. dir.client_input(line.substr(1));
  99. } else {
  100. dir.server_line(line, line);
  101. }
  102. }
  103. std::array<density, 6> dense = {{{70, 0, 6, 0, false, true},
  104. {441, 100, 2, 0, false, true},
  105. {575, 1000, 2, 15, true, false},
  106. {600, 40, 6, 0, false, true},
  107. {629, 1, 4, 0, false, true},
  108. {711, 101, 6, 0, false, true}}};
  109. for (auto const& s : dense) {
  110. auto d = dir.galaxy.dscan.find(s.sector);
  111. EXPECT_EQ(d, s) << "Sector " << s.sector << " density";
  112. }
  113. // Check that each sector was correctly processed
  114. std::vector<int> sectors = {70, 441, 575, 600, 629, 711};
  115. YAML::Node sector_data;
  116. sector_data[70]["density"] = 0;
  117. sector_data[70]["warps"] = 6;
  118. sector_data[70]["navhaz"] = 0;
  119. sector_data[70]["anom"] = false;
  120. sector_data[70]["known"] = true;
  121. sector_data[441]["density"] = 100;
  122. sector_data[441]["warps"] = 2;
  123. sector_data[441]["navhaz"] = 0;
  124. sector_data[441]["anom"] = false;
  125. sector_data[441]["known"] = true;
  126. sector_data[575]["density"] = 1000;
  127. sector_data[575]["warps"] = 2;
  128. sector_data[575]["navhaz"] = 15;
  129. sector_data[575]["anom"] = true;
  130. sector_data[575]["known"] = false;
  131. sector_data[600]["density"] = 40;
  132. sector_data[600]["warps"] = 6;
  133. sector_data[600]["navhaz"] = 0;
  134. sector_data[600]["anom"] = false;
  135. sector_data[600]["known"] = true;
  136. sector_data[629]["density"] = 1;
  137. sector_data[629]["warps"] = 4;
  138. sector_data[629]["navhaz"] = 0;
  139. sector_data[629]["anom"] = false;
  140. sector_data[629]["known"] = true;
  141. sector_data[711]["density"] = 101;
  142. sector_data[711]["warps"] = 6;
  143. sector_data[711]["navhaz"] = 0;
  144. sector_data[711]["anom"] = false;
  145. sector_data[711]["known"] = true;
  146. for (auto sector : sectors) {
  147. // GTEST_COUT << "Testing Sector " << sector << std::endl;
  148. EXPECT_EQ(dir.galaxy.meta["density"][sector]["density"].as<int>(),
  149. sector_data[sector]["density"].as<int>())
  150. << "Sector " << sector << " density";
  151. EXPECT_EQ(dir.galaxy.meta["density"][sector]["warps"].as<int>(),
  152. sector_data[sector]["warps"].as<int>())
  153. << "Sector " << sector << " warps";
  154. EXPECT_EQ(dir.galaxy.meta["density"][sector]["navhaz"].as<int>(),
  155. sector_data[sector]["navhaz"].as<int>())
  156. << "Sector " << sector << " navhaz";
  157. EXPECT_EQ(dir.galaxy.meta["density"][sector]["anom"].as<bool>(),
  158. sector_data[sector]["anom"].as<bool>())
  159. << "Sector " << sector << " anom";
  160. EXPECT_EQ(dir.galaxy.meta["density"][sector]["known"].as<bool>(),
  161. sector_data[sector]["known"].as<bool>())
  162. << "Sector " << sector << " known";
  163. }
  164. }
  165. } // namespace