From c3ba05a61ef092b30ffad81e86a9fe5a1f5cecbc Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sat, 2 Nov 2024 11:30:12 +0800 Subject: [PATCH] test: remove dependence of group extend --- src/core_test/group/group_legacy.cc | 2 +- src/core_test/group/group_union.cc | 37 +++++++++++----------- src/core_test/group/helper/cases.h | 25 --------------- src/core_test/group/helper/group_impl.cc | 39 ++++++++++++++++++++++-- src/core_test/helper/group.h | 12 ++++++++ src/core_test/helper/internal/group.cc | 37 +++++++++++++++++++++- 6 files changed, 103 insertions(+), 49 deletions(-) delete mode 100644 src/core_test/group/helper/cases.h diff --git a/src/core_test/group/group_legacy.cc b/src/core_test/group/group_legacy.cc index aeafc8e..4219f17 100644 --- a/src/core_test/group/group_legacy.cc +++ b/src/core_test/group/group_legacy.cc @@ -1,7 +1,7 @@ #include #include "group/group.h" -#include "helper/cases.h" +// #include "helper/cases.h" //using klotski::cases::Group; using klotski::group::GroupUnion; diff --git a/src/core_test/group/group_union.cc b/src/core_test/group/group_union.cc index 7ef3876..37f9836 100644 --- a/src/core_test/group/group_union.cc +++ b/src/core_test/group/group_union.cc @@ -4,7 +4,6 @@ #include #include -#include "helper/cases.h" #include "helper/parallel.h" #include "helper/block_num.h" @@ -65,11 +64,11 @@ TEST(GroupUnion, basic) { TEST(GroupUnion, constant) { - EXPECT_EQ(TYPE_ID_LIMIT, group_union_num()); + EXPECT_EQ(TYPE_ID_LIMIT, helper::group_union_num()); uint32_t sum = 0; - for (uint32_t i = 0; i < group_union_num(); ++i) { - sum += group_num(i); + for (uint32_t i = 0; i < helper::group_union_num(); ++i) { + sum += helper::group_union_group_num(i); } EXPECT_EQ(ALL_GROUP_NUM, sum); @@ -77,22 +76,22 @@ TEST(GroupUnion, constant) { // test from member function directly? } -TEST(GroupUnion, values) { - GROUP_UNION_PARALLEL({ - auto type_id = group_union.unwrap(); - auto &cases = group_union_cases(type_id); +// TEST(GroupUnion, values) { +// GROUP_UNION_PARALLEL({ +// auto type_id = group_union.unwrap(); +// auto &cases = group_union_cases(type_id); +// +// EXPECT_EQ(group_union.size(), cases.size()); +// EXPECT_EQ(group_union.cases().codes(), cases); +// EXPECT_EQ(group_union.group_num(), helper::group_union_group_num(type_id)); +// +// auto get_group_size = [](auto g) { return g.size(); }; +// const auto sizes = group_union.groups() | std::views::transform(get_group_size); +// EXPECT_EQ(group_union.max_group_size(), *std::ranges::max_element(sizes)); +// }); +// } - EXPECT_EQ(group_union.size(), cases.size()); - EXPECT_EQ(group_union.cases().codes(), cases); - EXPECT_EQ(group_union.group_num(), group_num(type_id)); - - auto get_group_size = [](auto g) { return g.size(); }; - const auto sizes = group_union.groups() | std::views::transform(get_group_size); - EXPECT_EQ(group_union.max_group_size(), *std::ranges::max_element(sizes)); - }); -} - -TEST(GroupUnion, values_pro) { +TEST(GroupUnion, values) { GROUP_UNION_PARALLEL({ auto type_id = group_union.unwrap(); auto &cases = helper::group_union_cases(type_id); diff --git a/src/core_test/group/helper/cases.h b/src/core_test/group/helper/cases.h deleted file mode 100644 index 31306c2..0000000 --- a/src/core_test/group/helper/cases.h +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once - -#include "group/group.h" - -// TODO: move to `.cc` file -using klotski::cases::AllCases; -using klotski::codec::CommonCode; - -// ----------------------------------------------------------------------------------------- // - -// TODO: using global test helper - -/// Get the type_id upper limit. -uint32_t group_union_num(); - -/// Get the group_id upper limit. -uint32_t group_num(uint32_t type_id); - -/// Get cases contained in the specified type_id. -const std::vector& group_union_cases(uint32_t type_id); - -/// Get cases contained in the specified type_id and group_id. -const std::vector& group_cases(uint32_t type_id, uint32_t group_id); - -// ----------------------------------------------------------------------------------------- // diff --git a/src/core_test/group/helper/group_impl.cc b/src/core_test/group/helper/group_impl.cc index 475c4ba..c1e4cc3 100644 --- a/src/core_test/group/helper/group_impl.cc +++ b/src/core_test/group/helper/group_impl.cc @@ -1,15 +1,48 @@ -#include "cases.h" - #include +#include +#include "group/group.h" #include "helper/block_num.h" // TODO: multi-threads builder +using klotski::codec::RawCode; +using klotski::cases::AllCases; +using klotski::codec::CommonCode; + +using klotski::mover::MaskMover; + +uint32_t group_union_num(); + +const std::vector& group_union_cases(uint32_t type_id); + +static std::vector Group_extend(RawCode raw_code) { + std::vector codes; + std::unordered_map cases; // + + auto core = MaskMover([&codes, &cases](RawCode code, uint64_t mask) { + if (const auto match = cases.find(code.unwrap()); match != cases.end()) { + match->second |= mask; // update mask + return; + } + cases.emplace(code, mask); + codes.emplace_back(code); // new case + }); + + uint64_t offset = 0; + codes.emplace_back(raw_code); + cases.emplace(raw_code, 0); // without mask + while (offset != codes.size()) { + auto curr = codes[offset++].unwrap(); + core.next_cases(RawCode::unsafe_create(curr), cases.find(curr)->second); + } + return codes; +} + /// Extend ordered Group from the specified CommonCode seed. static std::vector extend_cases(CommonCode seed) { // TODO: using inner build process -> only allow calling klotski::mover - auto raw_codes = klotski::group::Group_extend(seed.to_raw_code()); + auto raw_codes = Group_extend(seed.to_raw_code()); std::vector common_codes {raw_codes.begin(), raw_codes.end()}; std::ranges::sort(common_codes.begin(), common_codes.end()); return common_codes; diff --git a/src/core_test/helper/group.h b/src/core_test/helper/group.h index 22247ce..eeb4c53 100644 --- a/src/core_test/helper/group.h +++ b/src/core_test/helper/group.h @@ -2,6 +2,18 @@ #include "common_code/common_code.h" +// /// Get the type_id upper limit. +// uint32_t group_union_num(); +// +// /// Get the group_id upper limit. +// uint32_t group_num(uint32_t type_id); +// +// /// Get cases contained in the specified type_id. +// const std::vector& group_union_cases(uint32_t type_id); +// +// /// Get cases contained in the specified type_id and group_id. +// const std::vector& group_cases(uint32_t type_id, uint32_t group_id); + namespace helper { using klotski::codec::CommonCode; diff --git a/src/core_test/helper/internal/group.cc b/src/core_test/helper/internal/group.cc index 9de368a..c5d73f9 100644 --- a/src/core_test/helper/internal/group.cc +++ b/src/core_test/helper/internal/group.cc @@ -8,17 +8,52 @@ #include #include +#include using klotski::cases::AllCases; +using klotski::codec::RawCode; using klotski::codec::CommonCode; +using klotski::mover::MaskMover; + #define STATIC_DATA(name, impl) \ static const auto& name() { \ static auto data = [] {impl}(); \ return data; \ } + + +static std::vector Group_extend(RawCode raw_code) { + std::vector codes; + std::unordered_map cases; // + + auto core = MaskMover([&codes, &cases](RawCode code, uint64_t mask) { + if (const auto match = cases.find(code.unwrap()); match != cases.end()) { + match->second |= mask; // update mask + return; + } + cases.emplace(code, mask); + codes.emplace_back(code); // new case + }); + + uint64_t offset = 0; + codes.emplace_back(raw_code); + cases.emplace(raw_code, 0); // without mask + while (offset != codes.size()) { + auto curr = codes[offset++].unwrap(); + core.next_cases(RawCode::unsafe_create(curr), cases.find(curr)->second); + } + return codes; +} + + + + + + + /// Filter cases with different type_id from AllCases. STATIC_DATA(group_union_data, { std::vector> codes; @@ -53,7 +88,7 @@ const std::vector& helper::group_union_cases(const uint32_t type_id) /// Extend ordered Group from the specified CommonCode seed. static std::vector extend_cases(CommonCode seed) { // TODO: using inner build process -> only allow calling klotski::mover - auto raw_codes = klotski::group::Group_extend(seed.to_raw_code()); + auto raw_codes = Group_extend(seed.to_raw_code()); std::vector common_codes {raw_codes.begin(), raw_codes.end()}; std::ranges::sort(common_codes.begin(), common_codes.end()); return common_codes;