Browse Source

test: remove dependence of group extend

legacy
Dnomd343 2 months ago
parent
commit
c3ba05a61e
  1. 2
      src/core_test/group/group_legacy.cc
  2. 37
      src/core_test/group/group_union.cc
  3. 25
      src/core_test/group/helper/cases.h
  4. 39
      src/core_test/group/helper/group_impl.cc
  5. 12
      src/core_test/helper/group.h
  6. 37
      src/core_test/helper/internal/group.cc

2
src/core_test/group/group_legacy.cc

@ -1,7 +1,7 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "group/group.h" #include "group/group.h"
#include "helper/cases.h" // #include "helper/cases.h"
//using klotski::cases::Group; //using klotski::cases::Group;
using klotski::group::GroupUnion; using klotski::group::GroupUnion;

37
src/core_test/group/group_union.cc

@ -4,7 +4,6 @@
#include <cstdint> #include <cstdint>
#include <algorithm> #include <algorithm>
#include "helper/cases.h"
#include "helper/parallel.h" #include "helper/parallel.h"
#include "helper/block_num.h" #include "helper/block_num.h"
@ -65,11 +64,11 @@ TEST(GroupUnion, basic) {
TEST(GroupUnion, constant) { TEST(GroupUnion, constant) {
EXPECT_EQ(TYPE_ID_LIMIT, group_union_num()); EXPECT_EQ(TYPE_ID_LIMIT, helper::group_union_num());
uint32_t sum = 0; uint32_t sum = 0;
for (uint32_t i = 0; i < group_union_num(); ++i) { for (uint32_t i = 0; i < helper::group_union_num(); ++i) {
sum += group_num(i); sum += helper::group_union_group_num(i);
} }
EXPECT_EQ(ALL_GROUP_NUM, sum); EXPECT_EQ(ALL_GROUP_NUM, sum);
@ -77,22 +76,22 @@ TEST(GroupUnion, constant) {
// test from member function directly? // test from member function directly?
} }
TEST(GroupUnion, values) { // TEST(GroupUnion, values) {
GROUP_UNION_PARALLEL({ // GROUP_UNION_PARALLEL({
auto type_id = group_union.unwrap(); // auto type_id = group_union.unwrap();
auto &cases = group_union_cases(type_id); // auto &cases = group_union_cases(type_id);
//
EXPECT_EQ(group_union.size(), cases.size()); // EXPECT_EQ(group_union.size(), cases.size());
EXPECT_EQ(group_union.cases().codes(), cases); // EXPECT_EQ(group_union.cases().codes(), cases);
EXPECT_EQ(group_union.group_num(), group_num(type_id)); // EXPECT_EQ(group_union.group_num(), helper::group_union_group_num(type_id));
//
auto get_group_size = [](auto g) { return g.size(); }; // auto get_group_size = [](auto g) { return g.size(); };
const auto sizes = group_union.groups() | std::views::transform(get_group_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.max_group_size(), *std::ranges::max_element(sizes));
}); // });
} // }
TEST(GroupUnion, values_pro) { TEST(GroupUnion, values) {
GROUP_UNION_PARALLEL({ GROUP_UNION_PARALLEL({
auto type_id = group_union.unwrap(); auto type_id = group_union.unwrap();
auto &cases = helper::group_union_cases(type_id); auto &cases = helper::group_union_cases(type_id);

25
src/core_test/group/helper/cases.h

@ -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<CommonCode>& group_union_cases(uint32_t type_id);
/// Get cases contained in the specified type_id and group_id.
const std::vector<CommonCode>& group_cases(uint32_t type_id, uint32_t group_id);
// ----------------------------------------------------------------------------------------- //

39
src/core_test/group/helper/group_impl.cc

@ -1,15 +1,48 @@
#include "cases.h"
#include <algorithm> #include <algorithm>
#include <mover/mover.h>
#include "group/group.h"
#include "helper/block_num.h" #include "helper/block_num.h"
// TODO: multi-threads builder // 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<CommonCode>& group_union_cases(uint32_t type_id);
static std::vector<RawCode> Group_extend(RawCode raw_code) {
std::vector<RawCode> codes;
std::unordered_map<uint64_t, uint64_t> cases; // <code, mask>
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. /// Extend ordered Group from the specified CommonCode seed.
static std::vector<CommonCode> extend_cases(CommonCode seed) { static std::vector<CommonCode> extend_cases(CommonCode seed) {
// TODO: using inner build process -> only allow calling klotski::mover // 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<CommonCode> common_codes {raw_codes.begin(), raw_codes.end()}; std::vector<CommonCode> common_codes {raw_codes.begin(), raw_codes.end()};
std::ranges::sort(common_codes.begin(), common_codes.end()); std::ranges::sort(common_codes.begin(), common_codes.end());
return common_codes; return common_codes;

12
src/core_test/helper/group.h

@ -2,6 +2,18 @@
#include "common_code/common_code.h" #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<CommonCode>& group_union_cases(uint32_t type_id);
//
// /// Get cases contained in the specified type_id and group_id.
// const std::vector<CommonCode>& group_cases(uint32_t type_id, uint32_t group_id);
namespace helper { namespace helper {
using klotski::codec::CommonCode; using klotski::codec::CommonCode;

37
src/core_test/helper/internal/group.cc

@ -8,17 +8,52 @@
#include <algorithm> #include <algorithm>
#include <unordered_set> #include <unordered_set>
#include <mover/mover.h>
using klotski::cases::AllCases; using klotski::cases::AllCases;
using klotski::codec::RawCode;
using klotski::codec::CommonCode; using klotski::codec::CommonCode;
using klotski::mover::MaskMover;
#define STATIC_DATA(name, impl) \ #define STATIC_DATA(name, impl) \
static const auto& name() { \ static const auto& name() { \
static auto data = [] {impl}(); \ static auto data = [] {impl}(); \
return data; \ return data; \
} }
static std::vector<RawCode> Group_extend(RawCode raw_code) {
std::vector<RawCode> codes;
std::unordered_map<uint64_t, uint64_t> cases; // <code, mask>
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. /// Filter cases with different type_id from AllCases.
STATIC_DATA(group_union_data, { STATIC_DATA(group_union_data, {
std::vector<std::vector<CommonCode>> codes; std::vector<std::vector<CommonCode>> codes;
@ -53,7 +88,7 @@ const std::vector<CommonCode>& helper::group_union_cases(const uint32_t type_id)
/// Extend ordered Group from the specified CommonCode seed. /// Extend ordered Group from the specified CommonCode seed.
static std::vector<CommonCode> extend_cases(CommonCode seed) { static std::vector<CommonCode> extend_cases(CommonCode seed) {
// TODO: using inner build process -> only allow calling klotski::mover // 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<CommonCode> common_codes {raw_codes.begin(), raw_codes.end()}; std::vector<CommonCode> common_codes {raw_codes.begin(), raw_codes.end()};
std::ranges::sort(common_codes.begin(), common_codes.end()); std::ranges::sort(common_codes.begin(), common_codes.end());
return common_codes; return common_codes;

Loading…
Cancel
Save