From b41f73da6e9337b75a145d2ae73cbb7b9fd3d0c9 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sat, 8 Apr 2023 17:58:14 +0800 Subject: [PATCH] update: AllCases release with CommonCode --- src/klotski_core/all_cases/all_cases.cc | 4 ++-- src/klotski_core/all_cases/all_cases.h | 10 ++++++---- src/klotski_core/short_code/short_code.h | 2 +- test/basic/all_cases.cc | 5 ++++- test/codec/short_code.cc | 7 +++++++ 5 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/klotski_core/all_cases/all_cases.cc b/src/klotski_core/all_cases/all_cases.cc index eb2ca86..637aaef 100644 --- a/src/klotski_core/all_cases/all_cases.cc +++ b/src/klotski_core/all_cases/all_cases.cc @@ -41,8 +41,8 @@ void AllCases::build() { // ensure that data is available } } -std::vector AllCases::release() { - std::vector data; +std::vector AllCases::release() { + std::vector data; data.reserve(ALL_CASES_SIZE_SUM); // memory pre-allocated for (uint64_t head = 0; head < 16; ++head) { for (const auto &range : fetch()[head]) { diff --git a/src/klotski_core/all_cases/all_cases.h b/src/klotski_core/all_cases/all_cases.h index 3092e6d..6635142 100644 --- a/src/klotski_core/all_cases/all_cases.h +++ b/src/klotski_core/all_cases/all_cases.h @@ -21,10 +21,13 @@ #include #include #include +#include "common_code.h" #include "basic_ranges.h" namespace klotski { +class CommonCode; + /// all cases count const uint32_t ALL_CASES_SIZE[16] = { 2942906, 2260392, 2942906, 0, @@ -32,9 +35,8 @@ const uint32_t ALL_CASES_SIZE[16] = { 2322050, 1876945, 2322050, 0, 2942906, 2260392, 2942906, 0, }; -const uint32_t ALL_CASES_SIZE_SUM = std::accumulate( // aka 29334498 - ALL_CASES_SIZE, ALL_CASES_SIZE + 16, (uint32_t)0 -); + +const uint32_t ALL_CASES_SIZE_SUM = 29334498; class AllCases : public BasicRanges { public: @@ -48,7 +50,7 @@ public: static const std::vector (&fetch())[16]; /// Export all possible common codes. - static std::vector release(); + static std::vector release(); private: static bool available_; diff --git a/src/klotski_core/short_code/short_code.h b/src/klotski_core/short_code/short_code.h index 8b8b978..9a69e1b 100644 --- a/src/klotski_core/short_code/short_code.h +++ b/src/klotski_core/short_code/short_code.h @@ -60,7 +60,7 @@ namespace klotski { class CommonCode; -const uint32_t SHORT_CODE_LIMIT = klotski::ALL_CASES_SIZE_SUM; +const uint32_t SHORT_CODE_LIMIT = 29334498; class ShortCodeException : public std::runtime_error { public: diff --git a/test/basic/all_cases.cc b/test/basic/all_cases.cc index 60e19d0..373f6d6 100644 --- a/test/basic/all_cases.cc +++ b/test/basic/all_cases.cc @@ -70,6 +70,9 @@ TEST(AllCases, all_cases_size) { for (uint32_t head = 0; head < 16; ++head) { EXPECT_EQ(all_cases[head].size(), ALL_CASES_SIZE[head]); } + EXPECT_EQ( + std::accumulate(ALL_CASES_SIZE, ALL_CASES_SIZE + 16, (uint32_t)0), ALL_CASES_SIZE_SUM + ); } /// all cases data verify @@ -92,7 +95,7 @@ TEST(AllCases, all_cases_release) { auto current = release.begin(); for (uint64_t head = 0; head < 16; ++head) { for (const auto &range : AllCases::fetch()[head]) { - EXPECT_EQ(*current, head << 32 | range); + EXPECT_EQ(current->unwrap(), head << 32 | range); ++current; } } diff --git a/test/codec/short_code.cc b/test/codec/short_code.cc index 878bc51..6f5df8b 100644 --- a/test/codec/short_code.cc +++ b/test/codec/short_code.cc @@ -10,6 +10,9 @@ using klotski::ShortCode; using klotski::CommonCode; using klotski::BasicRanges; +using klotski::SHORT_CODE_LIMIT; +using klotski::ALL_CASES_SIZE_SUM; + const static uint32_t TEST_CODE = 4091296; const static std::string TEST_CODE_STR = "4WVE1"; @@ -23,6 +26,10 @@ static inline void SHOULD_PANIC(const std::function &func) { EXPECT_EQ(panic_flag, true); } +TEST(ShortCode, limit) { + EXPECT_EQ(ALL_CASES_SIZE_SUM, SHORT_CODE_LIMIT); +} + TEST(ShortCode, hash) { auto tmp = std::unordered_set{ ShortCode(TEST_CODE) }; EXPECT_EQ(tmp.size(), 1);