settings_regression_tests.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include <Windows.h>
  2. #include <crtdbg.h>
  3. #include <cstdint>
  4. #include <cstdio>
  5. #include <string_view>
  6. #include "core/settings/settings.h"
  7. #include "state/account/inventory/item_state.h"
  8. namespace sunrise::core::log {
  9. Settings defaults() noexcept {
  10. return {};
  11. }
  12. void early(std::string_view) noexcept {}
  13. void write(Channel, Level, std::string_view) noexcept {}
  14. } // namespace sunrise::core::log
  15. namespace sunrise::state::activity::defaults {
  16. ActivityDefaults authored() noexcept {
  17. return {};
  18. }
  19. bool valid(const DefaultDestination&) noexcept {
  20. return true;
  21. }
  22. bool valid(const ActivityDefaults&) noexcept {
  23. return true;
  24. }
  25. } // namespace sunrise::state::activity::defaults
  26. namespace sunrise::state::entitlements {
  27. Table authored() noexcept {
  28. return {};
  29. }
  30. bool valid(const Entitlement&) noexcept {
  31. return true;
  32. }
  33. bool valid(const Table&) noexcept {
  34. return true;
  35. }
  36. } // namespace sunrise::state::entitlements
  37. namespace sunrise::state::account {
  38. bool valid_authored(const AccountState&) noexcept {
  39. return true;
  40. }
  41. } // namespace sunrise::state::account
  42. namespace sunrise::state::account::settings {
  43. bool valid(const AccountSettings&) noexcept {
  44. return true;
  45. }
  46. } // namespace sunrise::state::account::settings
  47. int failures{};
  48. namespace {
  49. void expect(bool value, const char* label) noexcept {
  50. if (!value) {
  51. std::fprintf(stderr, "FAIL %s\n", label);
  52. ++failures;
  53. }
  54. }
  55. bool parse_catalyst_policy(std::string_view json, bool& output) noexcept {
  56. sunrise::core::settings::Settings settings{};
  57. if (!sunrise::core::settings::parse(json, settings)) {
  58. return false;
  59. }
  60. output = settings.completeExoticCatalysts;
  61. return true;
  62. }
  63. void test_item_state_contract() noexcept {
  64. for (std::uint32_t flags = 0; flags <= 0x7U; ++flags) {
  65. expect(sunrise::state::account::inventory::valid_item_state(flags),
  66. "known item-state bits are valid");
  67. }
  68. expect(!sunrise::state::account::inventory::valid_item_state(0x8U),
  69. "unknown item-state bits are invalid");
  70. }
  71. void test_optional_catalyst_policy() noexcept {
  72. bool enabled = false;
  73. expect(sunrise::core::settings::kSettingsVersion == 8,
  74. "optional catalyst key keeps settings version 8");
  75. expect(parse_catalyst_policy(R"({"version":8})", enabled) && enabled,
  76. "version 8 without catalyst key keeps the true default");
  77. expect(parse_catalyst_policy(R"({"version":8,"complete_exotic_catalysts":false})", enabled)
  78. && !enabled,
  79. "version 8 accepts an explicit false catalyst policy");
  80. expect(
  81. !parse_catalyst_policy(
  82. R"({"version":8,"complete_exotic_catalysts":true,"complete_exotic_catalysts":false})",
  83. enabled),
  84. "duplicate catalyst policy is rejected");
  85. expect(!parse_catalyst_policy(R"({"version":8,"complete_exotic_catalysts":1})", enabled),
  86. "non-boolean catalyst policy is rejected");
  87. }
  88. } // namespace
  89. void test_exotic_catalysts() noexcept;
  90. void test_resolved_catalyst_output() noexcept;
  91. void test_opcode406_item_state() noexcept;
  92. int main() {
  93. #if defined(_DEBUG)
  94. _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
  95. _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
  96. #endif
  97. test_item_state_contract();
  98. test_optional_catalyst_policy();
  99. test_opcode406_item_state();
  100. test_exotic_catalysts();
  101. test_resolved_catalyst_output();
  102. if (failures != 0) {
  103. std::fprintf(stderr, "%d regression test(s) failed\n", failures);
  104. return 1;
  105. }
  106. std::puts("All regression tests passed");
  107. return 0;
  108. }