From ef30046b84dfc07dad36c37ee6d1679b3223eb7c Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Thu, 20 Apr 2023 14:17:16 +0800 Subject: [PATCH] test: add group info test --- src/klotski_core/ffi/tmain.cc | 22 +++++----- test/CMakeLists.txt | 1 + test/group/advance.cc | 2 - test/group/group_info.cc | 75 +++++++++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+), 13 deletions(-) create mode 100644 test/group/group_info.cc diff --git a/src/klotski_core/ffi/tmain.cc b/src/klotski_core/ffi/tmain.cc index f1d355f..221b982 100644 --- a/src/klotski_core/ffi/tmain.cc +++ b/src/klotski_core/ffi/tmain.cc @@ -44,17 +44,17 @@ void tmain() { // std::cout << "group id: " << ret.group_id << std::endl; // std::cout << "group index: " << ret.group_index << std::endl; - std::cout << GroupCase::parse({ - .type_id = 169, - .group_id = 1, - .group_index = 7472, - }) << std::endl; - - std::cout << GroupCase::parse({ - .type_id = 164, - .group_id = 0, - .group_index = 30833, - }) << std::endl; +// std::cout << GroupCase::parse({ +// .type_id = 169, +// .group_id = 1, +// .group_index = 7472, +// }) << std::endl; +// +// std::cout << GroupCase::parse({ +// .type_id = 164, +// .group_id = 0, +// .group_index = 30833, +// }) << std::endl; // std::cerr << (clock() - start) * 1000 / CLOCKS_PER_SEC << "ms" << std::endl; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index b7ced7e..93d29d7 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -63,6 +63,7 @@ add_test(NAME core COMMAND test_core) set(TEST_GROUP_SRC group/basic.cc group/advance.cc + group/group_info.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/advance.cc b/test/group/advance.cc index 8c8161b..edf09a6 100644 --- a/test/group/advance.cc +++ b/test/group/advance.cc @@ -175,5 +175,3 @@ TEST(Group, build_groups) { auto group_info_md5 = md5(group_info_str.c_str(), group_info_str.size()); EXPECT_STREQ(group_info_md5.c_str(), GROUP_INFO_MD5); // verify all group info } - -// TODO: verify GROUP_SEEDS_INDEX_REV (group_info) diff --git a/test/group/group_info.cc b/test/group/group_info.cc new file mode 100644 index 0000000..f052851 --- /dev/null +++ b/test/group/group_info.cc @@ -0,0 +1,75 @@ +#include +#include "group.h" +#include "tiny_pool.h" +#include "gtest/gtest.h" + +#define SHOULD_PANIC(FUNC) \ + try { \ + FUNC; EXPECT_STREQ("should panic", "but no panic"); \ + } catch (...) {} + +using klotski::Group; +using klotski::GroupCase; +using klotski::GroupType; +using klotski::CommonCode; + +using klotski::TYPE_ID_LIMIT; + +TEST(Group, group_info_invalid) { + SHOULD_PANIC(GroupCase::parse(GroupCase::info_t { + .type_id = TYPE_ID_LIMIT, + .group_id = 0, + .group_index = 0, + })) + + SHOULD_PANIC(GroupCase::parse(GroupCase::info_t { + .type_id = 0, + .group_id = static_cast(GroupType(0).group_num()), + .group_index = 0, + })) + + SHOULD_PANIC(GroupCase::parse(GroupCase::info_t { + .type_id = 0, + .group_id = 0, + .group_index = Group(0, 0).size(), + })) +} + +TEST(Group, group_info) { + auto test = [](GroupType group_type) { + for (uint32_t group_id = 0; group_id < group_type.group_num(); ++group_id) { + auto tmp = Group(group_type, group_id).cases(); + std::vector cases(tmp.begin(), tmp.end()); + + auto min = *std::min_element(cases.begin(), cases.end()); + auto max = *std::max_element(cases.begin(), cases.end()); + + auto min_info = GroupCase::info_t{ + .type_id = static_cast(group_type.unwrap()), + .group_id = static_cast(group_id), + .group_index = 0, + }; + auto max_info = GroupCase::info_t{ + .type_id = static_cast(group_type.unwrap()), + .group_id = static_cast(group_id), + .group_index = static_cast(cases.size() - 1), + }; + + EXPECT_EQ(min, GroupCase::parse(min_info)); + EXPECT_EQ(max, GroupCase::parse(max_info)); + EXPECT_EQ(min_info, GroupCase::encode(min)); + EXPECT_EQ(max_info, GroupCase::encode(max)); + EXPECT_EQ(min_info, GroupCase::encode(min.to_raw_code())); + EXPECT_EQ(max_info, GroupCase::encode(max.to_raw_code())); + } + }; + + auto pool = TinyPool(); + for (uint32_t type_id = 0; type_id < TYPE_ID_LIMIT; ++type_id) { + pool.submit(test, GroupType(type_id)); + } + pool.boot(); + pool.join(); +} + +// TODO: add group_info FAST mode