From f9c30256973d23b32e64356cc80a2c6f1ec6ce36 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 30 Apr 2023 01:26:57 +0800 Subject: [PATCH] test: add global test in codec test suites --- test/codec/common_code.cc | 37 +++++++++++++++++++++ test/codec/range_split.h | 18 +++++++++++ test/codec/raw_code.cc | 67 +++++++++++++++++++++++++++++++++++++++ test/codec/short_code.cc | 36 +++++++++++++++++++++ 4 files changed, 158 insertions(+) create mode 100644 test/codec/range_split.h diff --git a/test/codec/common_code.cc b/test/codec/common_code.cc index b3730c2..6c80fac 100644 --- a/test/codec/common_code.cc +++ b/test/codec/common_code.cc @@ -1,9 +1,12 @@ +#include #include #include #include #include "all_cases.h" +#include "tiny_pool.h" #include "common_code.h" #include "gtest/gtest.h" +#include "range_split.h" #define SHOULD_PANIC(FUNC) \ try { \ @@ -15,6 +18,7 @@ using klotski::AllCases; using klotski::ShortCode; using klotski::CommonCode; using klotski::ALL_CASES_SIZE; +using klotski::ALL_CASES_SIZE_SUM; const static uint64_t TEST_CODE = 0x1'A9BF'0C00; const static std::string TEST_CODE_STR = "1A9BF0C00"; @@ -182,3 +186,36 @@ TEST(CommonCode, initializate) { EXPECT_EQ(CommonCode::from_short_code(short_code_string).unwrap(), TEST_CODE); // l-value EXPECT_EQ(CommonCode::from_short_code(short_code.to_string()).unwrap(), TEST_CODE); // r-value } + +TEST(CommonCode, DISABLED_global) { + auto search = [](uint64_t start, uint64_t end) -> std::vector { + std::vector archive; + for (uint64_t common_code = start; common_code < end; ++common_code) { + if (CommonCode::check(common_code)) { + archive.emplace_back(common_code); // valid common code + } + } + return archive; + }; + + auto pool = TinyPool(); + std::vector>> tasks; + for (const auto &range : range_split(0, 0x10'0000'0000, 0x10'0000)) { + tasks.emplace_back( + pool.submit(search, range.first, range.second) + ); + } + pool.boot(); // running tasks + + std::vector result; + for (auto &tmp : tasks) { + auto ret = tmp.get(); // release data + result.insert(result.end(), ret.begin(), ret.end()); + } + pool.join(); + + auto all_cases = AllCases::release(); + for (uint32_t i = 0; i < ALL_CASES_SIZE_SUM; ++i) { + EXPECT_EQ(all_cases[i], result[i]); + } +} diff --git a/test/codec/range_split.h b/test/codec/range_split.h new file mode 100644 index 0000000..084b875 --- /dev/null +++ b/test/codec/range_split.h @@ -0,0 +1,18 @@ +#pragma once + +#include +#include + +typedef std::vector> range_split_t; + +inline range_split_t range_split(uint64_t start, uint64_t end, uint64_t size) { + range_split_t ranges; + uint64_t rear = end - (end - start) % size; // (rear - start) % size == 0 + for (uint64_t i = start; i < rear; i += size) { + ranges.emplace_back(i, i + size); // ranges with same length + } + if (rear != end) { + ranges.emplace_back(rear, end); // last range + } + return ranges; +} diff --git a/test/codec/raw_code.cc b/test/codec/raw_code.cc index 8c654d2..53867de 100644 --- a/test/codec/raw_code.cc +++ b/test/codec/raw_code.cc @@ -1,8 +1,12 @@ +#include #include #include +#include "common.h" #include "raw_code.h" #include "all_cases.h" +#include "tiny_pool.h" #include "gtest/gtest.h" +#include "range_split.h" #define SHOULD_PANIC(FUNC) \ try { \ @@ -13,6 +17,8 @@ using klotski::RawCode; using klotski::AllCases; using klotski::CommonCode; using klotski::ALL_CASES_SIZE; +using klotski::ALL_CASES_SIZE_SUM; +using klotski::Common::range_reverse; const static uint64_t TEST_CODE = 0x0603'EDF5'CAFF'F5E2; @@ -201,3 +207,64 @@ TEST(RawCode, horizontal_mirror_global) { } for (auto &t : threads) { t.join(); } } + +/// NOTE: for RawCode global test +uint64_t raw_code_convert(uint64_t common_code) { // try to convert as raw code + auto code = C_2x2 << (common_code >> 32) * 3; + auto range = 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; +} + +TEST(RawCode, DISABLED_global) { + auto search = [](uint64_t start, uint64_t end) -> std::vector { + std::vector archive; + for (uint64_t common_code = start; common_code < end; ++common_code) { + if (RawCode::check(raw_code_convert(common_code))) { + archive.emplace_back(common_code); // valid layout + } + } + return archive; + }; + + auto pool = TinyPool(); + std::vector>> tasks; + for (const auto &range : range_split(0, 0x10'0000'0000, 0x10'0000)) { + tasks.emplace_back( + pool.submit(search, range.first, range.second) + ); + } + pool.boot(); // running tasks + + std::vector result; + for (auto &tmp : tasks) { + auto ret = tmp.get(); // release data + result.insert(result.end(), ret.begin(), ret.end()); + } + pool.join(); + + auto all_cases = AllCases::release(); + for (uint32_t i = 0; i < ALL_CASES_SIZE_SUM; ++i) { + EXPECT_EQ(all_cases[i], result[i]); + } +} diff --git a/test/codec/short_code.cc b/test/codec/short_code.cc index 0073e0d..6bb0313 100644 --- a/test/codec/short_code.cc +++ b/test/codec/short_code.cc @@ -1,9 +1,12 @@ +#include #include #include #include #include "all_cases.h" +#include "tiny_pool.h" #include "short_code.h" #include "gtest/gtest.h" +#include "range_split.h" #define SHOULD_PANIC(FUNC) \ try { \ @@ -69,6 +72,39 @@ TEST(ShortCode, speed_up) { EXPECT_EQ(AllCases::status(), AllCases::AVAILABLE); } +TEST(ShortCode, DISABLED_global) { + auto check = [](uint32_t start, uint32_t end) -> std::vector { + std::vector archive; + for (uint32_t short_code = start; short_code < end; ++short_code) { + auto common_code = ShortCode::unsafe_create(short_code).to_common_code(); + archive.emplace_back(common_code.unwrap()); + EXPECT_EQ(common_code.to_short_code().unwrap(), short_code); + } + return archive; + }; + + auto pool = TinyPool(); + std::vector>> tasks; + for (const auto &range : range_split(0, klotski::ALL_CASES_SIZE_SUM, 10000)) { + tasks.emplace_back( + pool.submit(check, range.first, range.second) + ); + } + pool.boot(); // running tasks + + std::vector result; + for (auto &tmp : tasks) { + auto ret = tmp.get(); // release data + result.insert(result.end(), ret.begin(), ret.end()); + } + pool.join(); + + auto all_cases = AllCases::release(); + for (uint32_t i = 0; i < ALL_CASES_SIZE_SUM; ++i) { + EXPECT_EQ(all_cases[i], result[i]); + } +} + TEST(ShortCode, code_verify) { // test all layout auto test = [](uint64_t head) { for (const auto &range : AllCases::fetch()[head]) {