From e9efec29d385638b63544b8a05aedea6dc91eee3 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Wed, 19 Apr 2023 23:32:29 +0800 Subject: [PATCH] test: adapt to Group test --- test/CMakeLists.txt | 2 +- test/group/{build_cases.cc => advance.cc} | 77 +++++++++++------------ test/group/basic.cc | 12 +++- 3 files changed, 47 insertions(+), 44 deletions(-) rename test/group/{build_cases.cc => advance.cc} (71%) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index bc2211b..b7ced7e 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -62,7 +62,7 @@ add_test(NAME core COMMAND test_core) set(TEST_GROUP_SRC group/basic.cc -# group/build_cases.cc + group/advance.cc ) add_executable(test_group ${TEST_GROUP_SRC}) target_link_libraries(test_group ${TEST_DEPS} md5 tiny_pool absl::flat_hash_map) diff --git a/test/group/build_cases.cc b/test/group/advance.cc similarity index 71% rename from test/group/build_cases.cc rename to test/group/advance.cc index 8be861d..8c8161b 100644 --- a/test/group/build_cases.cc +++ b/test/group/advance.cc @@ -8,11 +8,11 @@ #include "gtest/gtest.h" using klotski::Group; -using klotski::TypeId; -using klotski::GroupId; -using klotski::AllCases; +using klotski::GroupCase; +using klotski::GroupType; using klotski::RawCode; +using klotski::AllCases; using klotski::ShortCode; using klotski::CommonCode; @@ -27,12 +27,12 @@ using klotski::ALL_CASES_SIZE_SUM; const char GROUP_INFO_MD5[] = "976bf22530085210e68a6a4e67053506"; TEST(Group, all_cases) { - std::array, TYPE_ID_LIMIT> all_cases; - auto build = [&all_cases](TypeId type_id) { + std::vector all_cases[TYPE_ID_LIMIT]; + auto build = [&all_cases](GroupType type_id) { auto cases = type_id.cases(); // build test data EXPECT_EQ(cases.size(), TYPE_ID_SIZE[type_id.unwrap()]); // verify cases number for (auto &&common_code : cases) { - EXPECT_EQ(TypeId(common_code), type_id); // verify type id + EXPECT_EQ(GroupType(common_code), type_id); // verify type id } EXPECT_EQ(std::is_sorted(cases.begin(), cases.end()), true); // verify data order all_cases[type_id.unwrap()] = cases; @@ -40,7 +40,7 @@ TEST(Group, all_cases) { auto pool = TinyPool(); for (uint32_t type_id = 0; type_id < TYPE_ID_LIMIT; ++type_id) { - pool.submit(build, TypeId(type_id)); + pool.submit(build, GroupType(type_id)); } pool.boot(); pool.join(); // wait data build complete @@ -59,16 +59,18 @@ TEST(Group, all_cases) { TEST(Group, group_cases) { auto build = [](CommonCode seed) -> std::vector { - auto group_raw = Group::cases(seed); - std::vector group(group_raw.begin(), group_raw.end()); // convert as CommonCodes - EXPECT_EQ(seed, std::min_element(group.begin(), group.end())->unwrap()); // confirm min seed - EXPECT_EQ(group.size(), GroupId::size(seed)); // verify group size - - uint32_t type_id = TypeId(seed).unwrap(); // current type id - for (auto &&elem : group) { - EXPECT_EQ(TypeId(elem).unwrap(), type_id); // verify type id of group cases + auto group = Group(seed); + auto tmp = group.cases(); + std::vector cases(tmp.begin(), tmp.end()); // convert as CommonCodes + EXPECT_EQ(seed, std::min_element(cases.begin(), cases.end())->unwrap()); // confirm min seed + EXPECT_EQ(cases.size(), group.size()); // verify group size + EXPECT_EQ(seed, group.seed()); // verify group seed + + uint32_t type_id = GroupType(seed).unwrap(); // current type id + for (auto &&elem : cases) { + EXPECT_EQ(GroupType(elem).unwrap(), type_id); // verify type id of group cases } - return group; + return cases; }; auto pool = TinyPool(); @@ -94,16 +96,16 @@ TEST(Group, group_seeds) { std::vector all_seeds; all_seeds.reserve(ALL_GROUP_NUM); for (uint32_t type_id = 0; type_id < TYPE_ID_LIMIT; ++type_id) { - auto seeds = TypeId(type_id).seeds(); + auto seeds = GroupType(type_id).seeds(); for (auto &&seed : seeds) { - EXPECT_EQ(TypeId(seed).unwrap(), type_id); // verify type id of seeds + EXPECT_EQ(GroupType(seed).unwrap(), type_id); // verify type id of seeds } all_seeds.insert(all_seeds.end(), seeds.begin(), seeds.end()); std::vector sub_seeds; sub_seeds.reserve(TYPE_ID_GROUP_NUM[type_id]); for (uint32_t group_id = 0; group_id < TYPE_ID_GROUP_NUM[type_id]; ++group_id) { - sub_seeds.emplace_back(GroupId(type_id, group_id).seed()); + sub_seeds.emplace_back(Group(type_id, group_id).seed()); } std::sort(seeds.begin(), seeds.end()); std::sort(sub_seeds.begin(), sub_seeds.end()); // don't verify seeds order for now @@ -111,36 +113,31 @@ TEST(Group, group_seeds) { } std::vector group_seeds(GROUP_SEEDS, GROUP_SEEDS + ALL_GROUP_NUM); EXPECT_EQ(all_seeds, group_seeds); // verify group seeds - - auto test = [](CommonCode seed) { - EXPECT_EQ(GroupId::seed(seed), seed); // verify group seed fetch - EXPECT_EQ(GroupId::seed(seed.to_raw_code()), seed); - }; - auto pool = TinyPool(); - for (auto &&seed : GROUP_SEEDS) { // traverse all seeds - pool.submit(test, CommonCode::unsafe_create(seed)); - } - pool.boot(); - pool.join(); } TEST(Group, build_groups) { - std::vector all_cases(SHORT_CODE_LIMIT); + std::vector all_cases(SHORT_CODE_LIMIT); - auto test = [&all_cases](TypeId type_id) { - auto groups = Group::build_groups(type_id); + auto test = [&all_cases](GroupType type_id) { + auto groups = type_id.groups(); EXPECT_EQ(groups.size(), TYPE_ID_GROUP_NUM[type_id.unwrap()]); // verify groups num + EXPECT_EQ(groups.size(), type_id.group_num()); // verify groups num std::vector group_sizes; std::map> group_seeds; // for (uint32_t group_id = 0; group_id < groups.size(); ++group_id) { - auto group = Group::build_group(GroupId(type_id, group_id)); + auto tmp = Group(type_id, group_id).cases(); + std::vector group(tmp.begin(), tmp.end()); std::sort(group.begin(), group.end()); std::sort(groups[group_id].begin(), groups[group_id].end()); - EXPECT_EQ(groups[group_id], group); // verify group data - EXPECT_EQ(group.size(), GroupId(type_id, group_id).size()); // verify group size - EXPECT_EQ(*group.begin(), GroupId(type_id, group_id).seed()); // verify group seed + + EXPECT_EQ(groups[group_id], group); // verify group cases + EXPECT_EQ(group.size(), Group(type_id, group_id).size()); // verify group size + EXPECT_EQ(*group.begin(), Group(type_id, group_id).seed()); // verify group seed + + EXPECT_EQ(Group(*group.begin()).unwrap(), group_id); // build from CommonCode + EXPECT_EQ(Group(group.begin()->to_raw_code()).unwrap(), group_id); // build from RawCode for (uint32_t index = 0; index < group.size(); ++index) { all_cases[group[index].to_short_code().unwrap()] = { // storage group info @@ -148,7 +145,7 @@ TEST(Group, build_groups) { .group_id = static_cast(group_id), .group_index = index, }; - EXPECT_EQ(TypeId(group[index]), type_id); // verify released type id + EXPECT_EQ(GroupType(group[index]), type_id); // verify released type id } group_seeds[group.size()].emplace_back(*group.begin()); // storage group seeds @@ -164,7 +161,7 @@ TEST(Group, build_groups) { auto pool = TinyPool(); ShortCode::speed_up(ShortCode::FAST); for (uint32_t type_id = 0; type_id < TYPE_ID_LIMIT; ++type_id) { - pool.submit(test, TypeId(type_id)); + pool.submit(test, GroupType(type_id)); } pool.boot(); pool.join(); @@ -179,4 +176,4 @@ TEST(Group, build_groups) { EXPECT_STREQ(group_info_md5.c_str(), GROUP_INFO_MD5); // verify all group info } -// TODO: verify GROUP_SEEDS_INDEX_REV +// TODO: verify GROUP_SEEDS_INDEX_REV (group_info) diff --git a/test/group/basic.cc b/test/group/basic.cc index 284635d..57cb3a8 100644 --- a/test/group/basic.cc +++ b/test/group/basic.cc @@ -52,6 +52,15 @@ TEST(Group, type_id) { threads[head] = std::thread(test, head); // multi-threads verify } for (auto &t : threads) { t.join(); } + + for (uint32_t type_id = 0; type_id < TYPE_ID_LIMIT; ++type_id) { + auto tmp = GroupType(type_id); + auto common_code = *tmp.cases().begin(); + EXPECT_EQ(tmp.group_num(), GroupType::group_num(common_code)); + EXPECT_EQ(tmp.group_num(), GroupType::group_num(common_code.to_raw_code())); + EXPECT_EQ(tmp.max_size(), GroupType::max_size(common_code)); + EXPECT_EQ(tmp.max_size(), GroupType::max_size(common_code.to_raw_code())); + } } TEST(Group, block_num) { @@ -126,9 +135,6 @@ TEST(Group, group_id) { pool.join(); } -// TODO: test Group(...).unwrap() == group_id -// TODO: test Group::size() / Group::seed() / Group::cases() - TEST(Group, operators) { EXPECT_EQ(GroupType(0), GroupType(0)); EXPECT_NE(GroupType(0), GroupType(1));