From c250f901ccaf4f8c92ff9ec0746473615fe2c34f Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Fri, 24 Feb 2023 23:36:30 +0800 Subject: [PATCH] test: add RawCode global test --- test/CMakeLists.txt | 5 +++ test/global/common_code.cc | 2 +- test/global/raw_code.cc | 75 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 test/global/raw_code.cc diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 5f94a1e..cc2a697 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -68,4 +68,9 @@ add_executable(test_global_short_code global/short_code.cc) target_link_libraries(test_global_short_code ${TEST_GLOBAL_DEPS}) add_test(NAME global_short_code COMMAND test_global_short_code) +include_directories(../src/klotski_core/raw_code) +add_executable(test_global_raw_code global/raw_code.cc) +target_link_libraries(test_global_raw_code ${TEST_GLOBAL_DEPS}) +add_test(NAME global_raw_code COMMAND test_global_raw_code) + ################################################################################ diff --git a/test/global/common_code.cc b/test/global/common_code.cc index b74e828..c095144 100644 --- a/test/global/common_code.cc +++ b/test/global/common_code.cc @@ -1,6 +1,6 @@ #include "all_cases.h" -#include "gtest/gtest.h" #include "common_code.h" +#include "gtest/gtest.h" #include "global_utils.h" using klotski::AllCases; diff --git a/test/global/raw_code.cc b/test/global/raw_code.cc new file mode 100644 index 0000000..19fc7fe --- /dev/null +++ b/test/global/raw_code.cc @@ -0,0 +1,75 @@ +#include "common.h" +#include "raw_code.h" +#include "gtest/gtest.h" +#include "global_utils.h" + +using klotski::Common; +using klotski::RawCode; +using klotski::AllCases; +using klotski::CommonCode; + +uint64_t convert(uint64_t common_code) { // try to convert as raw code + auto code = C_2x2 << (common_code >> 32) * 3; + auto range = Common::range_reverse((uint32_t)common_code); + for (int addr = 0; range; range >>= 2) { + while ((code >> addr) & 0b111 && addr < 60) { + addr += 3; + } + if (addr >= 60) { + return 0; + } + switch (range & 0b11) { // match low 2-bits + case 0b01: // 1x2 block + code |= C_1x2 << addr; + break; + case 0b10: // 2x1 block + code |= C_2x1 << addr; + break; + case 0b11: // 1x1 block + code |= C_1x1 << addr; + break; + case 0b00: // space + addr += 3; + } + } + return code; +} + +std::vector raw_code_search(uint64_t start, uint64_t end) { + std::vector ret; + for (uint64_t common_code = start; common_code < end; ++common_code) { + if (RawCode::check(convert(common_code))) { + ret.emplace_back(common_code); // valid layout + } + } + return ret; +} + +TEST(GLOBAL, raw_code) { + /// create raw code check tasks + auto pool = TinyPool(thread_num()); + std::vector>> futures; + for (const auto &range : range_split(0, 0x10'0000'0000, 0x10'0000)) { + futures.emplace_back( + pool.submit(raw_code_search, range.first, range.second) + ); + } + + /// run raw code search + pool.boot(); + std::vector result; + for (auto &f : futures) { + auto ret = f.get(); + result.insert(result.end(), ret.begin(), ret.end()); + } + pool.join(); + + /// verify check result + std::vector all_cases; + for (uint64_t head = 0; head < 16; ++head) { + for (const auto &range : AllCases::fetch()[head]) { + all_cases.emplace_back(head << 32 | range); + } + } + EXPECT_EQ(result, all_cases); +}