From f5c91b80848fa02e2735437efeef5e3b3b839e94 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sat, 8 Jun 2024 11:35:44 +0800 Subject: [PATCH] test: add Ranges test suites --- src/core_test/CMakeLists.txt | 1 + src/core_test/cases/ranges.cc | 120 ++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 src/core_test/cases/ranges.cc diff --git a/src/core_test/CMakeLists.txt b/src/core_test/CMakeLists.txt index 84b0d54..f9b5d5e 100644 --- a/src/core_test/CMakeLists.txt +++ b/src/core_test/CMakeLists.txt @@ -23,6 +23,7 @@ set(KLSK_TEST_CASES_SRC cases/all_cases.cc cases/basic_ranges.cc cases/group_union.cc + cases/ranges.cc ) add_executable(test_klotski_cases ${KLSK_TEST_CASES_SRC}) diff --git a/src/core_test/cases/ranges.cc b/src/core_test/cases/ranges.cc new file mode 100644 index 0000000..9284613 --- /dev/null +++ b/src/core_test/cases/ranges.cc @@ -0,0 +1,120 @@ +#include + +#include "ranges/ranges.h" + +#include +#include + +using klotski::cases::Ranges; + +static_assert(std::is_base_of_v, Ranges>); +static_assert(std::is_base_of_v, klotski::cases::RangesUnion>); + +TEST(Ranges, demo) { + + for (int type_id = 0; type_id < klotski::cases::TYPE_ID_LIMIT; ++type_id) { + auto [n, n_2x1, n_1x1] = klotski::cases::BLOCK_NUM[type_id]; + + Ranges ranges; + ranges.spawn(n, n_2x1, n_1x1); + + EXPECT_TRUE(std::ranges::is_sorted(ranges.begin(), ranges.end())); + + const auto match = std::ranges::adjacent_find(ranges.begin(), ranges.end()); + + EXPECT_EQ(match, ranges.end()); + + } + +} + +TEST(Ranges, combine) { + + Ranges ranges; + + for (int type_id = 0; type_id < klotski::cases::TYPE_ID_LIMIT; ++type_id) { + auto [n, n_2x1, n_1x1] = klotski::cases::BLOCK_NUM[type_id]; + + ranges.spawn(n, n_2x1, n_1x1); + + } + + std::ranges::stable_sort(ranges.begin(), ranges.end()); + + EXPECT_EQ(ranges, klotski::cases::BasicRanges::instance().fetch()); + +} + +TEST(Ranges, reverse) { + + for (int type_id = 0; type_id < klotski::cases::TYPE_ID_LIMIT; ++type_id) { + auto [n, n_2x1, n_1x1] = klotski::cases::BLOCK_NUM[type_id]; + + Ranges ranges; + ranges.spawn(n, n_2x1, n_1x1); + + Ranges reverse {ranges}; + for (auto &x : reverse) { + x = klotski::range_reverse(x); + } + ranges.reverse(); + + EXPECT_EQ(ranges, reverse); + + } + + +} + +TEST(Ranges, derive) { + + for (int type_id = 0; type_id < klotski::cases::TYPE_ID_LIMIT; ++type_id) { + auto [n, n_2x1, n_1x1] = klotski::cases::BLOCK_NUM[type_id]; + + Ranges ranges; + ranges.spawn(n, n_2x1, n_1x1); + + ranges.reverse(); + + auto kk = klotski::cases::GroupUnion::unsafe_create(type_id); + + klotski::cases::RangesUnion result; + + for (int head = 0; head < 16; ++head) { + + if (head % 4 == 3) { + continue; + } + + ranges.derive(head, result[head]); + } + + EXPECT_EQ(result, kk.cases()); + + } + + +} + +TEST(Ranges, check) { + auto ranges = klotski::cases::BasicRanges::instance().fetch(); + + klotski::cases::RangesUnion all_cases; + + for (int head = 0; head < 16; ++head) { + if (head % 4 == 3) { + continue; + } + + for (auto range : ranges) { + if (Ranges::check(head, klotski::range_reverse(range)) == 0) { + all_cases[head].emplace_back(range); + } + } + } + + EXPECT_EQ(klotski::cases::AllCases::instance().fetch(), all_cases); + +} + +// TODO: export CommonCode