diff --git a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp index c6c3e6b052..cfc9664f24 100644 --- a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp +++ b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp @@ -17,6 +17,7 @@ #include "Kernels/Kernels_Tests.h" #include "Pokemon/AutoStory/Pokemon_AutoStorySelect_Tests.h" #include "Pokemon/AutoStory/Pokemon_AutoStoryStats_Tests.h" +#include "Pokemon/AutoStory/Pokemon_CheckpointId_Tests.h" #include "CommonFramework/AudioPipeline/AudioInfo_Tests.h" #include "CommonFramework/AudioPipeline/Tools/ChannelMixer.h" #include "CommonFramework/AudioPipeline/SinkPipeline_Tests.h" @@ -78,6 +79,7 @@ UnitTestDatabase make_UNIT_TESTS_ALL(){ Kernels::add_tests(ret); Pokemon::add_tests_AutoStorySelect(ret); Pokemon::add_tests_AutoStoryStats(ret); + Pokemon::add_tests_CheckpointId(ret); NintendoSwitch::add_tests_CheckOnlineDetector(ret); NintendoSwitch::add_tests_FailedToConnectDetector(ret); NintendoSwitch::add_tests_UpdatePopupDetector(ret); diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.cpp new file mode 100644 index 0000000000..432f32333c --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.cpp @@ -0,0 +1,74 @@ +/* AutoStory Checkpoint Id + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "Pokemon_CheckpointId.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + +namespace{ + +// The value of two decimal digits, or nullopt if either is not a digit. +std::optional parse_two_digits(std::string_view text){ + if (text.size() != 2){ + return std::nullopt; + } + uint8_t value = 0; + for (char c : text){ + if (c < '0' || c > '9'){ + return std::nullopt; + } + value = (uint8_t)(value * 10 + (c - '0')); + } + return value; +} + +} // namespace + + + +std::optional CheckpointId::parse(std::string_view text){ + if ((text.size() != 5 && text.size() != 6) || text[2] != '.'){ + return std::nullopt; + } + std::optional segment = parse_two_digits(text.substr(0, 2)); + std::optional checkpoint = parse_two_digits(text.substr(3, 2)); + if (!segment || !checkpoint){ + return std::nullopt; + } + std::optional suffix; + if (text.size() == 6){ + const char letter = text[5]; + if (letter < 'a' || letter > 'z'){ + return std::nullopt; + } + suffix = letter; + } + return CheckpointId{*segment, *checkpoint, suffix}; +} + +std::string CheckpointId::to_string() const{ + std::string ret = std::format("{:02}.{:02}", m_segment, m_checkpoint); + if (m_suffix){ + ret += *m_suffix; + } + return ret; +} + +std::string CheckpointId::segment_string() const{ + return std::format("{:02}", m_segment); +} + + +bool starts_segment(std::optional previous, CheckpointId next){ + return !previous || previous->segment() != next.segment(); +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.h new file mode 100644 index 0000000000..7e5001266e --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.h @@ -0,0 +1,67 @@ +/* AutoStory Checkpoint Id + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Pokemon_CheckpointId_H +#define PokemonAutomation_Pokemon_CheckpointId_H + +#include +#include +#include +#include +#include + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// An AutoStory checkpoint, written "SS.CC" with an optional lowercase +// letter: segment 03, checkpoint 08 is "03.08". A letter splits a checkpoint +// without renumbering the ones after it, e.g. FRLG's "12.02b" for a boss +// battle cut out of 12.02. Ids order by segment, then checkpoint, then +// letter: 12.02 < 12.02b < 12.02c < 12.03. parse() is the only way to build +// one, so every id is valid. +class CheckpointId{ +public: + // Reads two digits, a dot, two digits and at most one lowercase letter. + // Anything else ("3.08", "03.08B", "03.08bc", "03.08b1", "03-08", + // "03.08.01") returns nullopt. + static std::optional parse(std::string_view text); + + // "03.08", or "03.08b" with a suffix. + std::string to_string() const; + // "03". + std::string segment_string() const; + + uint8_t segment() const{ return m_segment; } + uint8_t checkpoint() const{ return m_checkpoint; } + // 'a' to 'z'. + std::optional suffix() const{ return m_suffix; } + + auto operator<=>(const CheckpointId&) const = default; + +private: + CheckpointId(uint8_t segment, uint8_t checkpoint, std::optional suffix) + : m_segment(segment) + , m_checkpoint(checkpoint) + , m_suffix(suffix) + {} + + uint8_t m_segment = 0; + uint8_t m_checkpoint = 0; + std::optional m_suffix; +}; + + +// Whether reaching `next` starts a segment: true for the first checkpoint of +// a run (`previous` is nullopt) and when the segment differs from the last +// checkpoint's. The suffix does not matter. +bool starts_segment(std::optional previous, CheckpointId next); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.cpp new file mode 100644 index 0000000000..9a507d7234 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.cpp @@ -0,0 +1,326 @@ +/* AutoStory Checkpoint Id Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include "Pokemon_CheckpointId.h" +#include "Pokemon_CheckpointId_Tests.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + +namespace{ + + +// Parse: text in, the id printed back out (nullopt when parse rejects it). + +struct ParseInput{ + std::string text; +}; + +struct ParsedId{ + std::string id; + std::string segment; +}; + +struct ParseExpectation{ + std::optional parsed; +}; + +struct ParseScenario{ + std::string name; + ParseInput input; + ParseExpectation expectation; +}; + +class Test_CheckpointIdParse : public UnitTest{ +public: + Test_CheckpointIdParse(ParseScenario scenario) + : UnitTest(std::format("Pokemon::CheckpointId::parse - {}", scenario.name)) + , m_scenario(std::move(scenario)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const std::optional got = CheckpointId::parse(m_scenario.input.text); + const ParseExpectation& expected = m_scenario.expectation; + if (!expected.parsed){ + if (got){ + return std::format("Expected \"{}\" to be rejected, got {}", m_scenario.input.text, got->to_string()); + } + return true; + } + if (!got){ + return std::format("Expected \"{}\" to parse, it was rejected", m_scenario.input.text); + } + if (got->to_string() != expected.parsed->id){ + return std::format("Expected id {}, got {}", expected.parsed->id, got->to_string()); + } + if (got->segment_string() != expected.parsed->segment){ + return std::format("Expected segment {}, got {}", expected.parsed->segment, got->segment_string()); + } + return true; + } + +private: + const ParseScenario m_scenario; +}; + + +// Ordering: two ids, how the first compares to the second. + +struct OrderInput{ + std::string left; + std::string right; +}; + +struct OrderExpectation{ + std::strong_ordering order; +}; + +struct OrderScenario{ + std::string name; + OrderInput input; + OrderExpectation expectation; +}; + +std::string_view order_name(std::strong_ordering order){ + if (order < 0){ + return "less"; + } + if (order > 0){ + return "greater"; + } + return "equal"; +} + +class Test_CheckpointIdOrder : public UnitTest{ +public: + Test_CheckpointIdOrder(OrderScenario scenario) + : UnitTest(std::format("Pokemon::CheckpointId order - {}", scenario.name)) + , m_scenario(std::move(scenario)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const std::optional left = CheckpointId::parse(m_scenario.input.left); + const std::optional right = CheckpointId::parse(m_scenario.input.right); + if (!left || !right){ + return std::format( + "Bad checkpoint id \"{}\" or \"{}\" in the test input", + m_scenario.input.left, m_scenario.input.right + ); + } + const std::strong_ordering got = *left <=> *right; + if (got != m_scenario.expectation.order){ + return std::format( + "Expected {} to be {} than {}, got {}", + m_scenario.input.left, + order_name(m_scenario.expectation.order), + m_scenario.input.right, + order_name(got) + ); + } + return true; + } + +private: + const OrderScenario m_scenario; +}; + + +// Segment starts: the checkpoints a run passes, in order, and the ones that +// start a segment. + +struct SegmentStartInput{ + std::vector passed; +}; + +struct SegmentStartExpectation{ + std::vector starts; +}; + +struct SegmentStartScenario{ + std::string name; + SegmentStartInput input; + SegmentStartExpectation expectation; +}; + +std::string join(const std::vector& ids){ + std::string ret; + for (const std::string& id : ids){ + if (!ret.empty()){ + ret += ", "; + } + ret += id; + } + return ret; +} + +class Test_CheckpointIdSegmentStart : public UnitTest{ +public: + Test_CheckpointIdSegmentStart(SegmentStartScenario scenario) + : UnitTest(std::format("Pokemon::CheckpointId starts_segment - {}", scenario.name)) + , m_scenario(std::move(scenario)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + std::vector got; + std::optional previous; + for (const std::string& text : m_scenario.input.passed){ + const std::optional next = CheckpointId::parse(text); + if (!next){ + return std::format("Bad checkpoint id \"{}\" in the test input", text); + } + if (starts_segment(previous, *next)){ + got.emplace_back(next->to_string()); + } + previous = next; + } + if (got != m_scenario.expectation.starts){ + return std::format( + "Expected segment starts [{}], got [{}]", + join(m_scenario.expectation.starts), join(got) + ); + } + return true; + } + +private: + const SegmentStartScenario m_scenario; +}; + + +void add_parse_tests(UnitTestDatabase& database){ + const std::vector parse_cases{ + {.name = "segment and checkpoint", .input = {.text = "03.08"}, .expectation = {.parsed = ParsedId{.id = "03.08", .segment = "03"}}}, + {.name = "first checkpoint", .input = {.text = "00.00"}, .expectation = {.parsed = ParsedId{.id = "00.00", .segment = "00"}}}, + {.name = "largest id", .input = {.text = "99.99"}, .expectation = {.parsed = ParsedId{.id = "99.99", .segment = "99"}}}, + {.name = "letter suffix", .input = {.text = "12.02b"}, .expectation = {.parsed = ParsedId{.id = "12.02b", .segment = "12"}}}, + {.name = "first letter", .input = {.text = "00.12a"}, .expectation = {.parsed = ParsedId{.id = "00.12a", .segment = "00"}}}, + {.name = "last letter", .input = {.text = "03.08z"}, .expectation = {.parsed = ParsedId{.id = "03.08z", .segment = "03"}}}, + {.name = "uppercase letter", .input = {.text = "03.08B"}, .expectation = {}}, + {.name = "two letters", .input = {.text = "03.08bc"}, .expectation = {}}, + {.name = "digit after the letter", .input = {.text = "03.08b1"}, .expectation = {}}, + {.name = "third checkpoint digit", .input = {.text = "03.081"}, .expectation = {}}, + {.name = "punctuation suffix", .input = {.text = "03.08!"}, .expectation = {}}, + {.name = "letter in the segment", .input = {.text = "0b.08"}, .expectation = {}}, + {.name = "one-digit segment", .input = {.text = "3.08"}, .expectation = {}}, + {.name = "one-digit checkpoint", .input = {.text = "03.8"}, .expectation = {}}, + {.name = "three-digit segment", .input = {.text = "100.08"}, .expectation = {}}, + {.name = "three-digit checkpoint", .input = {.text = "03.100"}, .expectation = {}}, + {.name = "no dot", .input = {.text = "0308"}, .expectation = {}}, + {.name = "other separator", .input = {.text = "03-08"}, .expectation = {}}, + {.name = "extra part", .input = {.text = "03.08.01"}, .expectation = {}}, + {.name = "segment only", .input = {.text = "03"}, .expectation = {}}, + {.name = "sign", .input = {.text = "+3.08"}, .expectation = {}}, + {.name = "spaces", .input = {.text = " 3.08"}, .expectation = {}}, + {.name = "empty", .input = {.text = ""}, .expectation = {}}, + }; + for (const ParseScenario& scenario : parse_cases){ + database.add(scenario); + } +} + +void add_order_tests(UnitTestDatabase& database){ + const std::vector order_cases{ + { + .name = "same id", + .input = {.left = "03.08", .right = "03.08"}, + .expectation = {.order = std::strong_ordering::equal}, + }, + { + .name = "later checkpoint in the same segment", + .input = {.left = "03.09", .right = "03.08"}, + .expectation = {.order = std::strong_ordering::greater}, + }, + { + .name = "segment wins over checkpoint", + .input = {.left = "02.99", .right = "03.00"}, + .expectation = {.order = std::strong_ordering::less}, + }, + { + .name = "no suffix before a suffix", + .input = {.left = "12.02", .right = "12.02b"}, + .expectation = {.order = std::strong_ordering::less}, + }, + { + .name = "suffixes in letter order", + .input = {.left = "12.02b", .right = "12.02c"}, + .expectation = {.order = std::strong_ordering::less}, + }, + { + .name = "suffix before the next checkpoint", + .input = {.left = "12.02c", .right = "12.03"}, + .expectation = {.order = std::strong_ordering::less}, + }, + { + .name = "same suffix", + .input = {.left = "12.02b", .right = "12.02b"}, + .expectation = {.order = std::strong_ordering::equal}, + }, + }; + for (const OrderScenario& scenario : order_cases){ + database.add(scenario); + } +} + +void add_segment_start_tests(UnitTestDatabase& database){ + const std::vector segment_cases{ + { + .name = "first checkpoint of a run starts its segment", + .input = {.passed = {"03.08"}}, + .expectation = {.starts = {"03.08"}}, + }, + { + .name = "checkpoints in one segment start it once", + .input = {.passed = {"03.08", "03.09", "03.10"}}, + .expectation = {.starts = {"03.08"}}, + }, + { + .name = "each new segment starts once", + .input = {.passed = {"02.04", "02.05", "03.00", "03.01", "04.00"}}, + .expectation = {.starts = {"02.04", "03.00", "04.00"}}, + }, + { + .name = "suffix does not start a segment", + .input = {.passed = {"12.01", "12.02", "12.02b", "12.03"}}, + .expectation = {.starts = {"12.01"}}, + }, + { + .name = "suffixed checkpoint can start a segment", + .input = {.passed = {"00.12", "00.12b", "01.00"}}, + .expectation = {.starts = {"00.12", "01.00"}}, + }, + { + .name = "empty run starts nothing", + .input = {.passed = {}}, + .expectation = {.starts = {}}, + }, + }; + for (const SegmentStartScenario& scenario : segment_cases){ + database.add(scenario); + } +} + + +} // namespace + + + +void add_tests_CheckpointId(UnitTestDatabase& database){ + add_parse_tests(database); + add_order_tests(database); + add_segment_start_tests(database); +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.h new file mode 100644 index 0000000000..37212aa763 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.h @@ -0,0 +1,23 @@ +/* AutoStory Checkpoint Id Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Pokemon_CheckpointId_Tests_H +#define PokemonAutomation_Pokemon_CheckpointId_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Registers the tests for parsing, printing and ordering checkpoint ids. +void add_tests_CheckpointId(UnitTestDatabase& database); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index b2df0b557f..f534e8dd22 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1486,6 +1486,10 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/AutoStory/Pokemon_AutoStoryStats.h Source/Pokemon/AutoStory/Pokemon_AutoStoryStats_Tests.cpp Source/Pokemon/AutoStory/Pokemon_AutoStoryStats_Tests.h + Source/Pokemon/AutoStory/Pokemon_CheckpointId.cpp + Source/Pokemon/AutoStory/Pokemon_CheckpointId.h + Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.cpp + Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.h Source/Pokemon/Pokemon_AdvRng.cpp Source/Pokemon/Pokemon_AdvRng.h Source/Pokemon/Pokemon_BdspRng.cpp