From 99f60e08549e0ce7ed9a1c8353fa90864ffa94f7 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 23:18:33 -0800 Subject: [PATCH 1/5] AutoStory: CheckpointId value type and segment-start check CheckpointId parses "SS.CC" ids like "03.08" (exactly two digits, a dot, two digits), prints them back, orders them, and starts_segment() tells a caller when a run enters a new segment so it can count Segments Started. --- .../ComputerPrograms_UnitTestRunner.cpp | 2 + .../AutoStory/Pokemon_CheckpointId.cpp | 62 +++++ .../Pokemon/AutoStory/Pokemon_CheckpointId.h | 48 ++++ .../AutoStory/Pokemon_CheckpointId_Tests.cpp | 260 ++++++++++++++++++ .../AutoStory/Pokemon_CheckpointId_Tests.h | 23 ++ SerialPrograms/cmake/SourceFiles.cmake | 4 + 6 files changed, 399 insertions(+) create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.cpp create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.h create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.cpp create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.h diff --git a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp index c6c3e6b052..9ead73d4c0 100644 --- a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp +++ b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp @@ -16,6 +16,7 @@ #include "Common/Qt/QtThreadPool_Tests.h" #include "Kernels/Kernels_Tests.h" #include "Pokemon/AutoStory/Pokemon_AutoStorySelect_Tests.h" +#include "Pokemon/AutoStory/Pokemon_CheckpointId_Tests.h" #include "Pokemon/AutoStory/Pokemon_AutoStoryStats_Tests.h" #include "CommonFramework/AudioPipeline/AudioInfo_Tests.h" #include "CommonFramework/AudioPipeline/Tools/ChannelMixer.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..6bb35dedae --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.cpp @@ -0,0 +1,62 @@ +/* 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[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; + } + return CheckpointId{*segment, *checkpoint}; +} + +std::string CheckpointId::to_string() const{ + return std::format("{:02}.{:02}", segment, checkpoint); +} + +std::string CheckpointId::segment_string() const{ + return std::format("{:02}", 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..67ffb07c12 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.h @@ -0,0 +1,48 @@ +/* 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": segment 03, checkpoint 08 is +// "03.08". Ids order by segment, then by checkpoint. +struct CheckpointId{ + uint8_t segment; + uint8_t checkpoint; + + // Reads exactly two digits, a dot and two digits. Anything else + // ("3.08", "03.08b", "03-08", "03.08.01") returns nullopt. + static std::optional parse(std::string_view text); + + // "03.08". + std::string to_string() const; + // "03". + std::string segment_string() const; + + auto operator<=>(const CheckpointId&) const = default; +}; + + +// 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. A caller adds one to AutoStoryStats::m_segments_started then. +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..dd2a3fe601 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.cpp @@ -0,0 +1,260 @@ +/* AutoStory Checkpoint Id Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#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 ParseExpectation{ + std::optional id; + std::optional segment; +}; + +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.id){ + 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.id){ + return std::format("Expected id {}, got {}", *expected.id, got->to_string()); + } + if (got->segment_string() != *expected.segment){ + return std::format("Expected segment {}, got {}", *expected.segment, got->segment_string()); + } + return true; + } + +private: + const ParseScenario m_scenario; +}; + + +// Ordering: two ids, how the first compares to the second. + +struct OrderInput{ + CheckpointId left; + CheckpointId right; +}; + +struct OrderExpectation{ + std::strong_ordering order; +}; + +struct OrderScenario{ + std::string name; + OrderInput input; + OrderExpectation expectation; +}; + +const char* 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::strong_ordering got = m_scenario.input.left <=> m_scenario.input.right; + if (got != m_scenario.expectation.order){ + return std::format( + "Expected {} to be {} than {}, got {}", + m_scenario.input.left.to_string(), + order_name(m_scenario.expectation.order), + m_scenario.input.right.to_string(), + 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 (CheckpointId next : m_scenario.input.passed){ + 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; +}; + + +} // namespace + + + +void add_tests_CheckpointId(UnitTestDatabase& database){ + const std::vector parse_cases{ + {.name = "segment and checkpoint", .input = {.text = "03.08"}, .expectation = {.id = "03.08", .segment = "03"}}, + {.name = "first checkpoint", .input = {.text = "00.00"}, .expectation = {.id = "00.00", .segment = "00"}}, + {.name = "largest id", .input = {.text = "99.99"}, .expectation = {.id = "99.99", .segment = "99"}}, + {.name = "letter suffix", .input = {.text = "03.08b"}}, + {.name = "one-digit segment", .input = {.text = "3.08"}}, + {.name = "one-digit checkpoint", .input = {.text = "03.8"}}, + {.name = "three-digit segment", .input = {.text = "100.08"}}, + {.name = "three-digit checkpoint", .input = {.text = "03.100"}}, + {.name = "no dot", .input = {.text = "0308"}}, + {.name = "other separator", .input = {.text = "03-08"}}, + {.name = "extra part", .input = {.text = "03.08.01"}}, + {.name = "segment only", .input = {.text = "03"}}, + {.name = "sign", .input = {.text = "+3.08"}}, + {.name = "spaces", .input = {.text = " 3.08"}}, + {.name = "empty", .input = {.text = ""}}, + }; + for (const ParseScenario& scenario : parse_cases){ + database.add(scenario); + } + + const std::vector order_cases{ + { + .name = "same id", + .input = {.left = {3, 8}, .right = {3, 8}}, + .expectation = {.order = std::strong_ordering::equal}, + }, + { + .name = "later checkpoint in the same segment", + .input = {.left = {3, 9}, .right = {3, 8}}, + .expectation = {.order = std::strong_ordering::greater}, + }, + { + .name = "segment wins over checkpoint", + .input = {.left = {2, 99}, .right = {3, 0}}, + .expectation = {.order = std::strong_ordering::less}, + }, + }; + for (const OrderScenario& scenario : order_cases){ + database.add(scenario); + } + + const std::vector segment_cases{ + { + .name = "first checkpoint of a run starts its segment", + .input = {.passed = {{3, 8}}}, + .expectation = {.starts = {"03.08"}}, + }, + { + .name = "checkpoints in one segment start it once", + .input = {.passed = {{3, 8}, {3, 9}, {3, 10}}}, + .expectation = {.starts = {"03.08"}}, + }, + { + .name = "each new segment starts once", + .input = {.passed = {{2, 4}, {2, 5}, {3, 0}, {3, 1}, {4, 0}}}, + .expectation = {.starts = {"02.04", "03.00", "04.00"}}, + }, + { + .name = "empty run starts nothing", + .input = {.passed = {}}, + .expectation = {.starts = {}}, + }, + }; + for (const SegmentStartScenario& scenario : segment_cases){ + database.add(scenario); + } +} + + + +} // 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..3c546cb9f6 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1483,6 +1483,10 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/AutoStory/Pokemon_AutoStorySelect.h Source/Pokemon/AutoStory/Pokemon_AutoStorySelect_Tests.cpp Source/Pokemon/AutoStory/Pokemon_AutoStorySelect_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/AutoStory/Pokemon_AutoStoryStats.h Source/Pokemon/AutoStory/Pokemon_AutoStoryStats_Tests.cpp Source/Pokemon/AutoStory/Pokemon_AutoStoryStats_Tests.h From 99cdd6d0bfc4d9cb04a50527d680a1b0c9e195bf Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 23:18:54 -0800 Subject: [PATCH 2/5] CheckpointId tests: split registration, full case rows --- .../AutoStory/Pokemon_CheckpointId_Tests.cpp | 54 +++++++++++-------- 1 file changed, 33 insertions(+), 21 deletions(-) diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.cpp index dd2a3fe601..a9a100f3cc 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include "Pokemon_CheckpointId.h" @@ -86,7 +87,7 @@ struct OrderScenario{ OrderExpectation expectation; }; -const char* order_name(std::strong_ordering order){ +std::string_view order_name(std::strong_ordering order){ if (order < 0){ return "less"; } @@ -180,32 +181,30 @@ class Test_CheckpointIdSegmentStart : public UnitTest{ }; -} // namespace - - - -void add_tests_CheckpointId(UnitTestDatabase& database){ +void add_parse_tests(UnitTestDatabase& database){ const std::vector parse_cases{ - {.name = "segment and checkpoint", .input = {.text = "03.08"}, .expectation = {.id = "03.08", .segment = "03"}}, - {.name = "first checkpoint", .input = {.text = "00.00"}, .expectation = {.id = "00.00", .segment = "00"}}, - {.name = "largest id", .input = {.text = "99.99"}, .expectation = {.id = "99.99", .segment = "99"}}, - {.name = "letter suffix", .input = {.text = "03.08b"}}, - {.name = "one-digit segment", .input = {.text = "3.08"}}, - {.name = "one-digit checkpoint", .input = {.text = "03.8"}}, - {.name = "three-digit segment", .input = {.text = "100.08"}}, - {.name = "three-digit checkpoint", .input = {.text = "03.100"}}, - {.name = "no dot", .input = {.text = "0308"}}, - {.name = "other separator", .input = {.text = "03-08"}}, - {.name = "extra part", .input = {.text = "03.08.01"}}, - {.name = "segment only", .input = {.text = "03"}}, - {.name = "sign", .input = {.text = "+3.08"}}, - {.name = "spaces", .input = {.text = " 3.08"}}, - {.name = "empty", .input = {.text = ""}}, + {.name = "segment and checkpoint", .input = {.text = "03.08"}, .expectation = {.id = "03.08", .segment = "03"}}, + {.name = "first checkpoint", .input = {.text = "00.00"}, .expectation = {.id = "00.00", .segment = "00"}}, + {.name = "largest id", .input = {.text = "99.99"}, .expectation = {.id = "99.99", .segment = "99"}}, + {.name = "letter suffix", .input = {.text = "03.08b"}, .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", @@ -226,7 +225,9 @@ void add_tests_CheckpointId(UnitTestDatabase& database){ 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", @@ -255,6 +256,17 @@ void add_tests_CheckpointId(UnitTestDatabase& database){ } +} // namespace + + + +void add_tests_CheckpointId(UnitTestDatabase& database){ + add_parse_tests(database); + add_order_tests(database); + add_segment_start_tests(database); +} + + } // namespace Pokemon } // namespace PokemonAutomation From 51b07e89fd1d8563a39a4e29b77c0b2128cdac00 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 23:19:11 -0800 Subject: [PATCH 3/5] CheckpointId tests: segment runs as id strings --- .../AutoStory/Pokemon_CheckpointId_Tests.cpp | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.cpp index a9a100f3cc..b1eade625f 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.cpp @@ -127,7 +127,7 @@ class Test_CheckpointIdOrder : public UnitTest{ // start a segment. struct SegmentStartInput{ - std::vector passed; + std::vector passed; }; struct SegmentStartExpectation{ @@ -161,9 +161,13 @@ class Test_CheckpointIdSegmentStart : public UnitTest{ virtual UnitTestResult run(Logger&, CancellableScope&) const override{ std::vector got; std::optional previous; - for (CheckpointId next : m_scenario.input.passed){ - if (starts_segment(previous, next)){ - got.emplace_back(next.to_string()); + 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; } @@ -231,17 +235,17 @@ void add_segment_start_tests(UnitTestDatabase& database){ const std::vector segment_cases{ { .name = "first checkpoint of a run starts its segment", - .input = {.passed = {{3, 8}}}, + .input = {.passed = {"03.08"}}, .expectation = {.starts = {"03.08"}}, }, { .name = "checkpoints in one segment start it once", - .input = {.passed = {{3, 8}, {3, 9}, {3, 10}}}, + .input = {.passed = {"03.08", "03.09", "03.10"}}, .expectation = {.starts = {"03.08"}}, }, { .name = "each new segment starts once", - .input = {.passed = {{2, 4}, {2, 5}, {3, 0}, {3, 1}, {4, 0}}}, + .input = {.passed = {"02.04", "02.05", "03.00", "03.01", "04.00"}}, .expectation = {.starts = {"02.04", "03.00", "04.00"}}, }, { From 6d5cfa409aaf652d0db857deb9183b3a53c9de39 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 23:22:07 -0800 Subject: [PATCH 4/5] CheckpointId: allow one optional lowercase letter suffix FRLG splits boss battles out of a checkpoint without renumbering the ones after it (00.12b, 12.02b). The suffix sorts after the bare id and before the next checkpoint; starts_segment ignores it. --- .../AutoStory/Pokemon_CheckpointId.cpp | 17 +++++-- .../Pokemon/AutoStory/Pokemon_CheckpointId.h | 18 +++++--- .../AutoStory/Pokemon_CheckpointId_Tests.cpp | 46 +++++++++++++++++-- 3 files changed, 68 insertions(+), 13 deletions(-) diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.cpp index 6bb35dedae..38a7f2491f 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.cpp @@ -32,7 +32,7 @@ std::optional parse_two_digits(std::string_view text){ std::optional CheckpointId::parse(std::string_view text){ - if (text.size() != 5 || text[2] != '.'){ + if ((text.size() != 5 && text.size() != 6) || text[2] != '.'){ return std::nullopt; } std::optional segment = parse_two_digits(text.substr(0, 2)); @@ -40,11 +40,22 @@ std::optional CheckpointId::parse(std::string_view text){ if (!segment || !checkpoint){ return std::nullopt; } - return CheckpointId{*segment, *checkpoint}; + char suffix = '\0'; + if (text.size() == 6){ + suffix = text[5]; + if (suffix < 'a' || suffix > 'z'){ + return std::nullopt; + } + } + return CheckpointId{*segment, *checkpoint, suffix}; } std::string CheckpointId::to_string() const{ - return std::format("{:02}.{:02}", segment, checkpoint); + std::string ret = std::format("{:02}.{:02}", segment, checkpoint); + if (suffix != '\0'){ + ret += suffix; + } + return ret; } std::string CheckpointId::segment_string() const{ diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.h index 67ffb07c12..16fca4c994 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.h @@ -17,17 +17,23 @@ namespace PokemonAutomation{ namespace Pokemon{ -// An AutoStory checkpoint, written "SS.CC": segment 03, checkpoint 08 is -// "03.08". Ids order by segment, then by checkpoint. +// 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. struct CheckpointId{ uint8_t segment; uint8_t checkpoint; + // 'a' to 'z', or '\0' for none. + char suffix = '\0'; - // Reads exactly two digits, a dot and two digits. Anything else - // ("3.08", "03.08b", "03-08", "03.08.01") returns nullopt. + // 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". + // "03.08", or "03.08b" with a suffix. std::string to_string() const; // "03". std::string segment_string() const; @@ -38,7 +44,7 @@ struct CheckpointId{ // 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. A caller adds one to AutoStoryStats::m_segments_started then. +// checkpoint's. The suffix does not matter. A caller adds one to AutoStoryStats::m_segments_started then. bool starts_segment(std::optional previous, CheckpointId next); diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.cpp index b1eade625f..ec2f9ed0b6 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.cpp @@ -190,7 +190,15 @@ void add_parse_tests(UnitTestDatabase& database){ {.name = "segment and checkpoint", .input = {.text = "03.08"}, .expectation = {.id = "03.08", .segment = "03"}}, {.name = "first checkpoint", .input = {.text = "00.00"}, .expectation = {.id = "00.00", .segment = "00"}}, {.name = "largest id", .input = {.text = "99.99"}, .expectation = {.id = "99.99", .segment = "99"}}, - {.name = "letter suffix", .input = {.text = "03.08b"}, .expectation = {}}, + {.name = "letter suffix", .input = {.text = "12.02b"}, .expectation = {.id = "12.02b", .segment = "12"}}, + {.name = "first letter", .input = {.text = "00.12a"}, .expectation = {.id = "00.12a", .segment = "00"}}, + {.name = "last letter", .input = {.text = "03.08z"}, .expectation = {.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 = {}}, @@ -212,19 +220,39 @@ void add_order_tests(UnitTestDatabase& database){ const std::vector order_cases{ { .name = "same id", - .input = {.left = {3, 8}, .right = {3, 8}}, + .input = {.left = {3, 8, '\0'}, .right = {3, 8, '\0'}}, .expectation = {.order = std::strong_ordering::equal}, }, { .name = "later checkpoint in the same segment", - .input = {.left = {3, 9}, .right = {3, 8}}, + .input = {.left = {3, 9, '\0'}, .right = {3, 8, '\0'}}, .expectation = {.order = std::strong_ordering::greater}, }, { .name = "segment wins over checkpoint", - .input = {.left = {2, 99}, .right = {3, 0}}, + .input = {.left = {2, 99, '\0'}, .right = {3, 0, '\0'}}, .expectation = {.order = std::strong_ordering::less}, }, + { + .name = "no suffix before a suffix", + .input = {.left = {12, 2, '\0'}, .right = {12, 2, 'b'}}, + .expectation = {.order = std::strong_ordering::less}, + }, + { + .name = "suffixes in letter order", + .input = {.left = {12, 2, 'b'}, .right = {12, 2, 'c'}}, + .expectation = {.order = std::strong_ordering::less}, + }, + { + .name = "suffix before the next checkpoint", + .input = {.left = {12, 2, 'c'}, .right = {12, 3, '\0'}}, + .expectation = {.order = std::strong_ordering::less}, + }, + { + .name = "same suffix", + .input = {.left = {12, 2, 'b'}, .right = {12, 2, 'b'}}, + .expectation = {.order = std::strong_ordering::equal}, + }, }; for (const OrderScenario& scenario : order_cases){ database.add(scenario); @@ -248,6 +276,16 @@ void add_segment_start_tests(UnitTestDatabase& database){ .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 = {}}, From 6a78529a22f35bb001da27b434db4129c049c849 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 23:28:25 -0800 Subject: [PATCH 5/5] CheckpointId: optional suffix, private fields built only by parse() suffix is std::optional instead of a '\0' sentinel, the fields have defaults and are private behind accessors, so parse() is the only way to build an id. Order tests take ids as text. The parse test expects one optional. Sorted the CMake list and the test-runner include; shortened the starts_segment comment. --- .../ComputerPrograms_UnitTestRunner.cpp | 2 +- .../AutoStory/Pokemon_CheckpointId.cpp | 17 ++--- .../Pokemon/AutoStory/Pokemon_CheckpointId.h | 29 ++++++--- .../AutoStory/Pokemon_CheckpointId_Tests.cpp | 62 +++++++++++-------- SerialPrograms/cmake/SourceFiles.cmake | 6 +- 5 files changed, 71 insertions(+), 45 deletions(-) diff --git a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp index 9ead73d4c0..cfc9664f24 100644 --- a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp +++ b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp @@ -16,8 +16,8 @@ #include "Common/Qt/QtThreadPool_Tests.h" #include "Kernels/Kernels_Tests.h" #include "Pokemon/AutoStory/Pokemon_AutoStorySelect_Tests.h" -#include "Pokemon/AutoStory/Pokemon_CheckpointId_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" diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.cpp index 38a7f2491f..432f32333c 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.cpp @@ -40,31 +40,32 @@ std::optional CheckpointId::parse(std::string_view text){ if (!segment || !checkpoint){ return std::nullopt; } - char suffix = '\0'; + std::optional suffix; if (text.size() == 6){ - suffix = text[5]; - if (suffix < 'a' || suffix > 'z'){ + 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}", segment, checkpoint); - if (suffix != '\0'){ - ret += suffix; + 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}", segment); + return std::format("{:02}", m_segment); } bool starts_segment(std::optional previous, CheckpointId next){ - return !previous || previous->segment != next.segment; + return !previous || previous->segment() != next.segment(); } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.h index 16fca4c994..7e5001266e 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.h @@ -21,13 +21,10 @@ namespace Pokemon{ // 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. -struct CheckpointId{ - uint8_t segment; - uint8_t checkpoint; - // 'a' to 'z', or '\0' for none. - char suffix = '\0'; - +// 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. @@ -38,13 +35,29 @@ struct CheckpointId{ // "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. A caller adds one to AutoStoryStats::m_segments_started then. +// checkpoint's. The suffix does not matter. bool starts_segment(std::optional previous, CheckpointId next); diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.cpp index ec2f9ed0b6..9a507d7234 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.cpp @@ -26,9 +26,13 @@ struct ParseInput{ std::string text; }; +struct ParsedId{ + std::string id; + std::string segment; +}; + struct ParseExpectation{ - std::optional id; - std::optional segment; + std::optional parsed; }; struct ParseScenario{ @@ -47,7 +51,7 @@ class Test_CheckpointIdParse : public UnitTest{ 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.id){ + if (!expected.parsed){ if (got){ return std::format("Expected \"{}\" to be rejected, got {}", m_scenario.input.text, got->to_string()); } @@ -56,11 +60,11 @@ class Test_CheckpointIdParse : public UnitTest{ if (!got){ return std::format("Expected \"{}\" to parse, it was rejected", m_scenario.input.text); } - if (got->to_string() != *expected.id){ - return std::format("Expected id {}, got {}", *expected.id, got->to_string()); + if (got->to_string() != expected.parsed->id){ + return std::format("Expected id {}, got {}", expected.parsed->id, got->to_string()); } - if (got->segment_string() != *expected.segment){ - return std::format("Expected segment {}, got {}", *expected.segment, got->segment_string()); + if (got->segment_string() != expected.parsed->segment){ + return std::format("Expected segment {}, got {}", expected.parsed->segment, got->segment_string()); } return true; } @@ -73,8 +77,8 @@ class Test_CheckpointIdParse : public UnitTest{ // Ordering: two ids, how the first compares to the second. struct OrderInput{ - CheckpointId left; - CheckpointId right; + std::string left; + std::string right; }; struct OrderExpectation{ @@ -105,13 +109,21 @@ class Test_CheckpointIdOrder : public UnitTest{ {} virtual UnitTestResult run(Logger&, CancellableScope&) const override{ - const std::strong_ordering got = m_scenario.input.left <=> m_scenario.input.right; + 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.to_string(), + m_scenario.input.left, order_name(m_scenario.expectation.order), - m_scenario.input.right.to_string(), + m_scenario.input.right, order_name(got) ); } @@ -187,12 +199,12 @@ class Test_CheckpointIdSegmentStart : public UnitTest{ void add_parse_tests(UnitTestDatabase& database){ const std::vector parse_cases{ - {.name = "segment and checkpoint", .input = {.text = "03.08"}, .expectation = {.id = "03.08", .segment = "03"}}, - {.name = "first checkpoint", .input = {.text = "00.00"}, .expectation = {.id = "00.00", .segment = "00"}}, - {.name = "largest id", .input = {.text = "99.99"}, .expectation = {.id = "99.99", .segment = "99"}}, - {.name = "letter suffix", .input = {.text = "12.02b"}, .expectation = {.id = "12.02b", .segment = "12"}}, - {.name = "first letter", .input = {.text = "00.12a"}, .expectation = {.id = "00.12a", .segment = "00"}}, - {.name = "last letter", .input = {.text = "03.08z"}, .expectation = {.id = "03.08z", .segment = "03"}}, + {.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 = {}}, @@ -220,37 +232,37 @@ void add_order_tests(UnitTestDatabase& database){ const std::vector order_cases{ { .name = "same id", - .input = {.left = {3, 8, '\0'}, .right = {3, 8, '\0'}}, + .input = {.left = "03.08", .right = "03.08"}, .expectation = {.order = std::strong_ordering::equal}, }, { .name = "later checkpoint in the same segment", - .input = {.left = {3, 9, '\0'}, .right = {3, 8, '\0'}}, + .input = {.left = "03.09", .right = "03.08"}, .expectation = {.order = std::strong_ordering::greater}, }, { .name = "segment wins over checkpoint", - .input = {.left = {2, 99, '\0'}, .right = {3, 0, '\0'}}, + .input = {.left = "02.99", .right = "03.00"}, .expectation = {.order = std::strong_ordering::less}, }, { .name = "no suffix before a suffix", - .input = {.left = {12, 2, '\0'}, .right = {12, 2, 'b'}}, + .input = {.left = "12.02", .right = "12.02b"}, .expectation = {.order = std::strong_ordering::less}, }, { .name = "suffixes in letter order", - .input = {.left = {12, 2, 'b'}, .right = {12, 2, 'c'}}, + .input = {.left = "12.02b", .right = "12.02c"}, .expectation = {.order = std::strong_ordering::less}, }, { .name = "suffix before the next checkpoint", - .input = {.left = {12, 2, 'c'}, .right = {12, 3, '\0'}}, + .input = {.left = "12.02c", .right = "12.03"}, .expectation = {.order = std::strong_ordering::less}, }, { .name = "same suffix", - .input = {.left = {12, 2, 'b'}, .right = {12, 2, 'b'}}, + .input = {.left = "12.02b", .right = "12.02b"}, .expectation = {.order = std::strong_ordering::equal}, }, }; diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 3c546cb9f6..f534e8dd22 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1483,13 +1483,13 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/AutoStory/Pokemon_AutoStorySelect.h Source/Pokemon/AutoStory/Pokemon_AutoStorySelect_Tests.cpp Source/Pokemon/AutoStory/Pokemon_AutoStorySelect_Tests.h + 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/AutoStory/Pokemon_AutoStoryStats.h - Source/Pokemon/AutoStory/Pokemon_AutoStoryStats_Tests.cpp - Source/Pokemon/AutoStory/Pokemon_AutoStoryStats_Tests.h Source/Pokemon/Pokemon_AdvRng.cpp Source/Pokemon/Pokemon_AdvRng.h Source/Pokemon/Pokemon_BdspRng.cpp