test-galaxy.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. std::map<int, const char *> port_classes = {{1, "BBS"}, {2, "BSB"}, {3, "SBB"},
  14. {4, "SSB"}, {5, "SBS"}, {6, "BSS"},
  15. {7, "SSS"}, {8, "BBB"}, {9, "BBB"}};
  16. /* From Galaxy.py
  17. PORT_CLASSES = {
  18. 1: "BBS",
  19. 2: "BSB",
  20. 3: "SBB",
  21. 4: "SSB",
  22. 5: "SBS",
  23. 6: "BSS",
  24. 7: "SSS",
  25. 8: "BBB",
  26. }*/
  27. TEST(ports, get_buysell) {
  28. for (int type = 1; type < 10; ++type) {
  29. buysell expected;
  30. const char *flags;
  31. flags = port_classes[type];
  32. for (int x = 0; x < 3; ++x) expected.foe[x] = flags[x] == 'B';
  33. buysell result = get_buysell(type);
  34. for (int x = 0; x < 3; ++x)
  35. EXPECT_EQ(result.foe[x], expected.foe[x])
  36. << "type: " << type << " pos: " << x;
  37. }
  38. }
  39. TEST(ports, invert_buysell) {
  40. for (int type = 1; type < 10; ++type) {
  41. buysell expected;
  42. const char *flags;
  43. flags = port_classes[type];
  44. for (int x = 0; x < 3; ++x) expected.foe[x] = flags[x] != 'B';
  45. buysell result = get_buysell(type);
  46. buysell invert = invert_buysell(result);
  47. for (int x = 0; x < 3; ++x)
  48. EXPECT_EQ(invert.foe[x], expected.foe[x])
  49. << "type: " << type << " pos: " << x;
  50. }
  51. }
  52. TEST(ports, text_from_type) {
  53. for (int type = 1; type < 10; ++type) {
  54. buysell_text expected;
  55. const char *flags;
  56. flags = port_classes[type];
  57. for (int x = 0; x < 3; ++x) expected.txt[x] = flags[x];
  58. buysell_text result = text_from_type(type);
  59. EXPECT_EQ(result, expected) << "type: " << type;
  60. }
  61. }
  62. TEST(ports, text_from_buysell) {
  63. for (int type = 1; type < 10; ++type) {
  64. buysell source = get_buysell(type);
  65. buysell_text expected;
  66. const char *flags;
  67. flags = port_classes[type];
  68. for (int x = 0; x < 3; ++x) expected.txt[x] = flags[x];
  69. buysell_text result = text_from_buysell(source);
  70. EXPECT_EQ(result, expected) << "type: " << type;
  71. }
  72. }
  73. TEST(ports, type_from_buysell) {
  74. for (int type = 1; type < 9; ++type) {
  75. buysell source = get_buysell(type);
  76. int result = type_from_buysell(source);
  77. EXPECT_EQ(result, type) << "type: " << type;
  78. }
  79. }
  80. struct pair {
  81. int t1;
  82. int t2;
  83. friend bool operator<(const pair &lhs, const pair &rhs) {
  84. if (lhs.t1 == rhs.t1) {
  85. return lhs.t2 < rhs.t2;
  86. }
  87. return lhs.t1 < rhs.t1;
  88. };
  89. friend bool operator==(const pair &lhs, const pair &rhs) {
  90. return (lhs.t1 == rhs.t1) && (lhs.t2 == rhs.t2);
  91. };
  92. };
  93. #ifdef NOT_ANYMORE
  94. TEST(ports, trade_types) {
  95. std::map<pair, int> expected;
  96. for (int x = 0; x < 9; ++x) {
  97. pair p{x, x}; // ; p.t1 = x; p.t2 = 2;
  98. expected[p] = 0;
  99. }
  100. expected[pair{1, 2}] = 1;
  101. expected[pair{1, 4}] = 1;
  102. expected[pair{2, 5}] = 1;
  103. expected[pair{4, 5}] = 1;
  104. // I'm checking 0's and 1's.
  105. expected[pair{1, 3}] = 2;
  106. expected[pair{2, 3}] = 2;
  107. expected[pair{4, 6}] = 2;
  108. expected[pair{5, 6}] = 2;
  109. /*
  110. GTEST_COUT << "size:" << expected.size() << std::endl;
  111. for (auto const & i : expected ) {
  112. GTEST_COUT << i.first.t1 << "," << i.first.t2 << " :" << i.second <<
  113. std::endl;
  114. }
  115. GTEST_COUT << "Done!" << std::endl;
  116. */
  117. for (int p1 = 1; p1 <= 8; ++p1) {
  118. for (int p2 = p1; p2 <= 8; ++p2) {
  119. // GTEST_COUT << "Type " << p1 << " and " << p2 << std::endl;
  120. int t = trade_type(p1, p2);
  121. int t2 = trade_type(p2, p1);
  122. EXPECT_EQ(t, t2) << "Comparing reversed types";
  123. pair p{p1, p2};
  124. auto r = expected.find(p);
  125. if (r != expected.end()) {
  126. EXPECT_EQ(t, r->second) << "Comparision";
  127. } else {
  128. GTEST_COUT << "TODO: Need type for " << p1 << " " << text_from_type(p1)
  129. << " , " << p2 << " " << text_from_type(p2) << " : " << t
  130. << std::endl;
  131. }
  132. }
  133. }
  134. }
  135. #endif
  136. TEST(ports, parse_portcim) {
  137. // This really needs to be checked against real lines from a log file
  138. // THis was copied directly from client (indentation may be off)
  139. std::map<std::string, port> data = {
  140. {" 2 - 2420 100% 612 35% - 2020 100% ", // BSB
  141. {2, 2, {2420, 612, 2020}, {100, 35, 100}}},
  142. {" 4 - 2880 100% - 275 23% 61 6% ", // BBS
  143. {4, 1, {2880, 275, 61}, {100, 23, 6}}},
  144. {" 6 - 820 100% 1952 89% - 2680 100% ", // BSB
  145. {6, 2, {820, 1952, 2680}, {100, 89, 100}}},
  146. {" 8 - 1000 100% - 855 85% - 1000 100% ", // BBB
  147. {8, 8, {1000, 855, 1000}, {100, 85, 100}}},
  148. {" 20 - 1708 97% - 710 56% 287 15% ", // BBS
  149. {20, 1, {1708, 710, 287}, {97, 56, 15}}},
  150. {" 23 - 2120 100% - 709 40% 1902 69% ", // BBS
  151. {23, 1, {2120, 709, 1902}, {100, 40, 69}}},
  152. {" 28 1511 98% - 478 29% 589 35% ", // SBS
  153. {28, 5, {1511, 478, 589}, {98, 29, 35}}},
  154. {" 29 913 56% - 970 100% - 1990 100% ", // SBB
  155. {29, 3, {913, 970, 1990}, {56, 100, 100}}}};
  156. /*
  157. // If you could make this a map or something better please improve
  158. std::vector<port> ports;
  159. ports.push_back( port{ 2, 2, {2420, 612, 2020}, {100,35,100} });
  160. struct port p;
  161. p.sector = 2;
  162. p.type = 2;
  163. p.amount[0] = 2420;
  164. p.amount[1] = 612;
  165. p.amount[2] = 2020;
  166. p.percent[0] = 100;
  167. p.percent[1] = 35;
  168. p.percent[2] = 100;
  169. ports.push_back(p);
  170. p.sector = 4;
  171. p.type = 1;
  172. p.amount[0] = 2880;
  173. p.amount[1] = 275;
  174. p.amount[2] = 61;
  175. p.percent[0] = 100;
  176. p.percent[1] = 23;
  177. p.percent[2] = 6;
  178. ports.push_back(p);
  179. */
  180. for (auto testdata : data) {
  181. port parse = parse_portcim(testdata.first);
  182. EXPECT_EQ((int)parse.sector, (int)testdata.second.sector)
  183. << "Text: [" << testdata.first << "]";
  184. if (parse.sector != 0) {
  185. EXPECT_EQ(parse.type, testdata.second.type);
  186. for (int x = 0; x < 3; ++x) {
  187. EXPECT_EQ(parse.amount[x], testdata.second.amount[x])
  188. << "Sector:" << parse.sector;
  189. EXPECT_EQ(parse.percent[x], testdata.second.percent[x])
  190. << "Sector:" << parse.sector;
  191. }
  192. }
  193. }
  194. /*
  195. port parse = parse_portcim(data[0]);
  196. EXPECT_EQ(parse.sector, 2 );
  197. EXPECT_EQ(parse.type, 2);
  198. EXPECT_EQ(parse.amount[0], 2420);
  199. EXPECT_EQ(parse.sector, ports[0].sector);
  200. EXPECT_EQ(parse.type, ports[0].type);
  201. */
  202. /*
  203. this can't work. trying to compare "pointers" .. that's why the numbers are
  204. huge: Expected equality of these values: parse.amount Which is: 0x7fffc5ef83f6
  205. ports[0].amount
  206. Which is: 0x5556513ddb94
  207. */
  208. // EXPECT_EQ(parse.amount, ports[0].amount);
  209. // EXPECT_EQ(parse.percent, ports[0].percent);
  210. }
  211. TEST(galaxy, trade_type_info) {
  212. // test this code to see that we get the correct trade type results.
  213. Galaxy g;
  214. g.add_port(port{68, 1, {1290, 557, 561}, {100, 47, 56}});
  215. g.add_port(port{706, 4, {2490, 2590, 743}, {100, 100, 94}});
  216. trade_type_result ttr = g.trade_type_info(68, 706);
  217. EXPECT_EQ(ttr.type, BEST) << "Expect Trade Type BEST";
  218. // burn out equipment
  219. g.add_port(port{68, 1, {1290, 557, 161}, {100, 47, 16}});
  220. ttr = g.trade_type_info(68, 706);
  221. EXPECT_EQ(ttr.type, FAIR_O) << "Expect Trade Type FAIR_O";
  222. std::vector<port_pair_type> vppt = g.find_trades(68);
  223. EXPECT_EQ(vppt.size(), 0) << "Should have no trades, because no warps";
  224. g.add_warp(sector_warps{706, {68}});
  225. g.add_warp(sector_warps{68, {51, 693, 706, 904}});
  226. vppt = g.find_trades(68);
  227. EXPECT_EQ(vppt.size(), 1) << "Found a trade";
  228. }
  229. } // namespace