item_state_regression_tests.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #include <Windows.h>
  2. #include <crtdbg.h>
  3. #include <cstddef>
  4. #include <cstdint>
  5. #include <cstdio>
  6. #include <span>
  7. #include <string_view>
  8. #include "core/settings/parser.h"
  9. #include "core/settings/settings.h"
  10. namespace sunrise::core::settings::parser {
  11. struct ParserTestAccess {
  12. static bool equipment_item(Parser& parser,
  13. state::account::inventory::Item& item) noexcept {
  14. return parser.equipment_item(item);
  15. }
  16. static bool at_end(Parser& parser) noexcept { return parser.at_end(); }
  17. };
  18. } // namespace sunrise::core::settings::parser
  19. namespace sunrise::core::log {
  20. Settings defaults() noexcept { return {}; }
  21. void early(std::string_view) noexcept {}
  22. void write(Channel, Level, std::string_view) noexcept {}
  23. } // namespace sunrise::core::log
  24. namespace sunrise::state::activity::defaults {
  25. ActivityDefaults authored() noexcept { return {}; }
  26. bool valid(const DefaultDestination&) noexcept { return true; }
  27. bool valid(const ActivityDefaults&) noexcept { return true; }
  28. } // namespace sunrise::state::activity::defaults
  29. namespace sunrise::state::entitlements {
  30. Table authored() noexcept { return {}; }
  31. bool valid(const Entitlement&) noexcept { return true; }
  32. bool valid(const Table&) noexcept { return true; }
  33. } // namespace sunrise::state::entitlements
  34. namespace sunrise::state::account {
  35. bool valid_authored(const AccountState&) noexcept { return true; }
  36. } // namespace sunrise::state::account
  37. namespace sunrise::state::account::settings {
  38. bool valid(const AccountSettings&) noexcept { return true; }
  39. } // namespace sunrise::state::account::settings
  40. int failures{};
  41. namespace {
  42. void expect(bool value, const char* label) noexcept {
  43. if (!value) {
  44. std::fprintf(stderr, "FAIL %s\n", label);
  45. ++failures;
  46. }
  47. }
  48. bool parse_item_flags(std::uint32_t flags,
  49. sunrise::state::account::inventory::Item& item) noexcept {
  50. char json[192]{};
  51. const int written = std::snprintf(
  52. json,
  53. sizeof json,
  54. R"({"instance_soid":"0x1","definition_hash":"0x12345678",)"
  55. R"("level":50,"quantity":1,"plugs":[],"flags":%u})",
  56. flags);
  57. if (written <= 0 || static_cast<std::size_t>(written) >= sizeof json) {
  58. return false;
  59. }
  60. sunrise::core::settings::parser::Parser parser(
  61. std::string_view{json, static_cast<std::size_t>(written)});
  62. return sunrise::core::settings::parser::ParserTestAccess::equipment_item(parser, item)
  63. && sunrise::core::settings::parser::ParserTestAccess::at_end(parser);
  64. }
  65. void test_catalyst_item_state_flags() noexcept {
  66. for (std::uint32_t flags = 0; flags <= 0x7U; ++flags) {
  67. sunrise::state::account::inventory::Item item{};
  68. expect(parse_item_flags(flags, item), "parser accepts known item-state bits");
  69. expect(item.flags == flags, "parser preserves known item-state bits");
  70. }
  71. sunrise::state::account::inventory::Item item{};
  72. expect(!parse_item_flags(0x8U, item), "parser rejects unknown item-state bits");
  73. }
  74. bool parse_catalyst_policy(std::string_view json, bool& output) noexcept {
  75. sunrise::core::settings::Settings settings{};
  76. if (!sunrise::core::settings::parse(json, settings)) {
  77. return false;
  78. }
  79. output = settings.completeExoticCatalysts;
  80. return true;
  81. }
  82. void test_catalyst_policy_setting() noexcept {
  83. bool enabled = true;
  84. expect(parse_catalyst_policy(R"({"version":8,"complete_exotic_catalysts":false})", enabled)
  85. && !enabled,
  86. "global catalyst policy parses false");
  87. expect(parse_catalyst_policy(R"({"version":8})", enabled) && enabled,
  88. "missing global catalyst policy keeps its default");
  89. expect(!parse_catalyst_policy(
  90. R"({"complete_exotic_catalysts":true,"complete_exotic_catalysts":false})",
  91. enabled),
  92. "duplicate global catalyst policy is rejected");
  93. expect(!parse_catalyst_policy(R"({"complete_exotic_catalysts":1})", enabled),
  94. "non-boolean global catalyst policy is rejected");
  95. }
  96. } // namespace
  97. void test_exotic_catalysts() noexcept;
  98. void test_resolved_catalyst_output() noexcept;
  99. void test_opcode406_item_state() noexcept;
  100. int main() {
  101. #if defined(_DEBUG)
  102. _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
  103. _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
  104. #endif
  105. test_catalyst_item_state_flags();
  106. test_catalyst_policy_setting();
  107. test_opcode406_item_state();
  108. test_exotic_catalysts();
  109. test_resolved_catalyst_output();
  110. if (failures != 0) {
  111. std::fprintf(stderr, "%d regression test(s) failed\n", failures);
  112. return 1;
  113. }
  114. std::puts("All regression tests passed");
  115. return 0;
  116. }