test-galaxy.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #include "gtest/gtest.h"
  2. #include "galaxy.h"
  3. #include <array>
  4. #include <map>
  5. #include <string>
  6. #include <vector>
  7. /*
  8. How can I add logging, but not really add/use it?
  9. */
  10. #define GTEST_COUT std::cerr << "[ ] [ INFO ]"
  11. namespace {
  12. std::map<int, const char *> port_classes = {{1, "BBS"}, {2, "BSB"}, {3, "SBB"},
  13. {4, "SSB"}, {5, "SBS"}, {6, "BSS"},
  14. {7, "SSS"}, {8, "BBB"}, {9, "BBB"}};
  15. /* From Galaxy.py
  16. PORT_CLASSES = {
  17. 1: "BBS",
  18. 2: "BSB",
  19. 3: "SBB",
  20. 4: "SSB",
  21. 5: "SBS",
  22. 6: "BSS",
  23. 7: "SSS",
  24. 8: "BBB",
  25. }*/
  26. TEST(ports, get_buysell) {
  27. for (int type = 1; type < 10; ++type) {
  28. buysell expected;
  29. const char *flags;
  30. flags = port_classes[type];
  31. for (int x = 0; x < 3; ++x)
  32. 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)
  45. expected.foe[x] = flags[x] != 'B';
  46. buysell result = get_buysell(type);
  47. buysell invert = invert_buysell(result);
  48. for (int x = 0; x < 3; ++x)
  49. EXPECT_EQ(invert.foe[x], expected.foe[x])
  50. << "type: " << type << " pos: " << x;
  51. }
  52. }
  53. TEST(ports, text_from_type) {
  54. for (int type = 1; type < 10; ++type) {
  55. buysell_text expected;
  56. const char *flags;
  57. flags = port_classes[type];
  58. for (int x = 0; x < 3; ++x)
  59. expected.txt[x] = flags[x];
  60. buysell_text result = text_from_type(type);
  61. EXPECT_EQ(result, expected) << "type: " << type;
  62. }
  63. }
  64. TEST(ports, text_from_buysell) {
  65. for (int type = 1; type < 10; ++type) {
  66. buysell source = get_buysell(type);
  67. buysell_text expected;
  68. const char *flags;
  69. flags = port_classes[type];
  70. for (int x = 0; x < 3; ++x)
  71. expected.txt[x] = flags[x];
  72. buysell_text result = text_from_buysell(source);
  73. EXPECT_EQ(result, expected) << "type: " << type;
  74. }
  75. }
  76. TEST(ports, type_from_buysell) {
  77. for (int type = 1; type < 9; ++type) {
  78. buysell source = get_buysell(type);
  79. int result = type_from_buysell(source);
  80. EXPECT_EQ(result, type) << "type: " << type;
  81. }
  82. }
  83. TEST(ports, parse_portcim) {
  84. // This really needs to be checked against real lines from a log file
  85. // THis was copied directly from client (indentation may be off)
  86. std::map<std::string, port> data = {
  87. {" 2 - 2420 100% 612 35% - 2020 100% ", // BSB
  88. {2, 2, {2420, 612, 2020}, {100, 35, 100}}
  89. },
  90. {" 4 - 2880 100% - 275 23% 61 6% ", // BBS
  91. {4, 1, {2880, 275, 61}, {100, 23, 6}}},
  92. {" 6 - 820 100% 1952 89% - 2680 100% ", // BSB
  93. {6, 2, {820, 1952, 2680}, {100, 89, 100}}
  94. },
  95. {" 8 - 1000 100% - 855 85% - 1000 100% ", // BBB
  96. {8, 8, {1000, 855, 1000}, {100, 85, 100}}
  97. },
  98. {" 20 - 1708 97% - 710 56% 287 15% ", // BBS
  99. {20, 1, {1708, 710, 287}, {97, 56, 15}}
  100. },
  101. {" 23 - 2120 100% - 709 40% 1902 69% ", // BBS
  102. {23, 1, {2120, 709, 1902}, {100, 40, 69}}
  103. },
  104. {" 28 1511 98% - 478 29% 589 35% ", // SBS
  105. {28, 5, {1511, 478, 589}, {98, 29, 35}}
  106. },
  107. {" 29 913 56% - 970 100% - 1990 100% ", // SBB
  108. {29, 3, {913, 970, 1990}, {56, 100, 100}}
  109. }
  110. };
  111. /*
  112. // If you could make this a map or something better please improve
  113. std::vector<port> ports;
  114. ports.push_back( port{ 2, 2, {2420, 612, 2020}, {100,35,100} });
  115. struct port p;
  116. p.sector = 2;
  117. p.type = 2;
  118. p.amount[0] = 2420;
  119. p.amount[1] = 612;
  120. p.amount[2] = 2020;
  121. p.percent[0] = 100;
  122. p.percent[1] = 35;
  123. p.percent[2] = 100;
  124. ports.push_back(p);
  125. p.sector = 4;
  126. p.type = 1;
  127. p.amount[0] = 2880;
  128. p.amount[1] = 275;
  129. p.amount[2] = 61;
  130. p.percent[0] = 100;
  131. p.percent[1] = 23;
  132. p.percent[2] = 6;
  133. ports.push_back(p);
  134. */
  135. for (auto testdata : data) {
  136. port parse = parse_portcim(testdata.first);
  137. EXPECT_EQ((int)parse.sector, (int)testdata.second.sector)
  138. << "Text: [" << testdata.first << "]";
  139. if (parse.sector != 0) {
  140. EXPECT_EQ(parse.type, testdata.second.type);
  141. for (int x = 0; x < 3; ++x) {
  142. EXPECT_EQ(parse.amount[x], testdata.second.amount[x])
  143. << "Sector:" << parse.sector;
  144. EXPECT_EQ(parse.percent[x], testdata.second.percent[x])
  145. << "Sector:" << parse.sector;
  146. }
  147. }
  148. }
  149. /*
  150. port parse = parse_portcim(data[0]);
  151. EXPECT_EQ(parse.sector, 2 );
  152. EXPECT_EQ(parse.type, 2);
  153. EXPECT_EQ(parse.amount[0], 2420);
  154. EXPECT_EQ(parse.sector, ports[0].sector);
  155. EXPECT_EQ(parse.type, ports[0].type);
  156. */
  157. /*
  158. this can't work. trying to compare "pointers" .. that's why the numbers are
  159. huge: Expected equality of these values: parse.amount Which is: 0x7fffc5ef83f6
  160. ports[0].amount
  161. Which is: 0x5556513ddb94
  162. */
  163. // EXPECT_EQ(parse.amount, ports[0].amount);
  164. // EXPECT_EQ(parse.percent, ports[0].percent);
  165. }
  166. } // namespace