From 8adcf9ac511b1e734132f5bbc103cc54e5ccf9e3 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sat, 24 Aug 2024 10:44:03 +0800 Subject: [PATCH] test: adapt CommonCode test suites --- src/core_test/CMakeLists.txt | 1 + src/core_test/codec/common_code.cc | 61 ++++++++++++++++++----------- src/core_test/codec/helper/codec.cc | 27 +++++++++++++ src/core_test/codec/helper/codec.h | 14 +++++++ src/core_test/codec/helper/sample.h | 36 +++++++++++++++++ src/core_test/codec/raw_code.cc | 3 +- src/core_test/codec/sample.h | 54 ------------------------- src/core_test/codec/short_code.cc | 5 ++- 8 files changed, 122 insertions(+), 79 deletions(-) create mode 100644 src/core_test/codec/helper/codec.cc create mode 100644 src/core_test/codec/helper/codec.h create mode 100644 src/core_test/codec/helper/sample.h delete mode 100644 src/core_test/codec/sample.h diff --git a/src/core_test/CMakeLists.txt b/src/core_test/CMakeLists.txt index 169e364..30c7ffb 100644 --- a/src/core_test/CMakeLists.txt +++ b/src/core_test/CMakeLists.txt @@ -46,6 +46,7 @@ set(KLSK_TEST_CODEC_SRC codec/raw_code.cc codec/short_code.cc codec/common_code.cc + codec/helper/codec.cc ) add_executable(test_klotski_codec ${KLSK_TEST_CODEC_SRC}) diff --git a/src/core_test/codec/common_code.cc b/src/core_test/codec/common_code.cc index 0191e0a..af3fe0f 100644 --- a/src/core_test/codec/common_code.cc +++ b/src/core_test/codec/common_code.cc @@ -2,7 +2,8 @@ #include #include -#include "sample.h" +#include "helper/codec.h" +#include "helper/sample.h" #include "raw_code/raw_code.h" #include "all_cases/all_cases.h" #include "short_code/short_code.h" @@ -34,15 +35,36 @@ TEST(CommonCode, operators) { EXPECT_EQ(tmp.str(), TEST_C_CODE_STR); EXPECT_EQ((uint64_t)common_code, TEST_C_CODE); // convert as uint64_t + EXPECT_NE(0, common_code); // uint64_t != CommonCode + EXPECT_NE(common_code, 0); // CommonCode != uint64_t EXPECT_EQ(TEST_C_CODE, common_code); // uint64_t == CommonCode EXPECT_EQ(common_code, TEST_C_CODE); // CommonCode == uint64_t - EXPECT_EQ(common_code, common_code); // CommonCode == CommonCode - EXPECT_NE(TEST_C_CODE + 1, common_code); // uint64_t != CommonCode - EXPECT_NE(common_code, TEST_C_CODE + 1); // CommonCode != uint64_t - EXPECT_NE(common_code, CommonCode::unsafe_create(TEST_C_CODE + 1)); // CommonCode != CommonCode + EXPECT_LE(TEST_C_CODE, common_code); // uint64_t <= CommonCode + EXPECT_LE(TEST_C_CODE - 1, common_code); + EXPECT_LT(TEST_C_CODE - 1, common_code); // uint64_t < CommonCode + + EXPECT_LE(common_code, TEST_C_CODE); // CommonCode <= uint64_t + EXPECT_LE(common_code, TEST_C_CODE + 1); + EXPECT_LT(common_code, TEST_C_CODE + 1); // CommonCode < uint64_t + + EXPECT_GE(TEST_C_CODE, common_code); // uint64_t >= CommonCode + EXPECT_GE(TEST_C_CODE + 1, common_code); + EXPECT_GT(TEST_C_CODE + 1, common_code); // uint64_t > CommonCode + + EXPECT_GE(common_code, TEST_C_CODE); // CommonCode >= uint64_t + EXPECT_GE(common_code, TEST_C_CODE - 1); + EXPECT_GT(common_code, TEST_C_CODE - 1); // CommonCode > uint64_t + + EXPECT_EQ(common_code, common_code); // CommonCode == CommonCode + EXPECT_NE(common_code, CommonCode::unsafe_create(0)); // CommonCode != CommonCode + EXPECT_LE(common_code, common_code); // CommonCode <= CommonCode + EXPECT_LE(common_code, CommonCode::unsafe_create(TEST_C_CODE + 1)); EXPECT_LT(common_code, CommonCode::unsafe_create(TEST_C_CODE + 1)); // CommonCode < CommonCode + + EXPECT_GE(common_code, common_code); // CommonCode >= CommonCode + EXPECT_GE(CommonCode::unsafe_create(TEST_C_CODE + 1), common_code); EXPECT_GT(CommonCode::unsafe_create(TEST_C_CODE + 1), common_code); // CommonCode > CommonCode } @@ -54,12 +76,10 @@ TEST(CommonCode, exporter) { EXPECT_EQ(common_code.to_short_code(), TEST_S_CODE); auto code_shorten = common_code.to_string(true); - EXPECT_EQ(CommonCode::from_string(code_shorten), common_code); // l-value - EXPECT_EQ(CommonCode::from_string(std::move(code_shorten)), common_code); // r-value + EXPECT_EQ(CommonCode::from_string(code_shorten), common_code); auto code_normal = common_code.to_string(false); - EXPECT_EQ(CommonCode::from_string(code_normal), common_code); // l-value - EXPECT_EQ(CommonCode::from_string(std::move(code_normal)), common_code); // r-value + EXPECT_EQ(CommonCode::from_string(code_normal), common_code); } TEST(CommonCode, initializate) { @@ -67,11 +87,17 @@ TEST(CommonCode, initializate) { auto short_code = ShortCode::unsafe_create(TEST_S_CODE); auto common_code = CommonCode::unsafe_create(TEST_C_CODE); + // operator== + CommonCode c1 = common_code; + EXPECT_EQ(c1, TEST_C_CODE); // l-value + CommonCode c2 = CommonCode {common_code}; + EXPECT_EQ(c2, TEST_C_CODE); // r-value + // CommonCode(...) EXPECT_EQ(CommonCode(raw_code), TEST_C_CODE); EXPECT_EQ(CommonCode(short_code), TEST_C_CODE); EXPECT_EQ(CommonCode(common_code), TEST_C_CODE); // l-value - EXPECT_EQ(CommonCode(CommonCode(common_code)), TEST_C_CODE); // r-value + EXPECT_EQ(CommonCode(CommonCode {common_code}), TEST_C_CODE); // r-value // CommonCode::create(uint64_t) EXPECT_TRUE(CommonCode::create(TEST_C_CODE).has_value()); @@ -80,17 +106,13 @@ TEST(CommonCode, initializate) { // CommonCode::unsafe_create(uint64_t) EXPECT_EQ(CommonCode::unsafe_create(TEST_C_CODE), TEST_C_CODE); + EXPECT_EQ(CommonCode::unsafe_create(TEST_C_CODE_ERR), TEST_C_CODE_ERR); - // CommonCode::from_string(const std::string &) + // CommonCode::from_string(std::string_view) EXPECT_TRUE(CommonCode::from_string(TEST_C_CODE_STR).has_value()); EXPECT_FALSE(CommonCode::from_string(TEST_C_CODE_STR_ERR).has_value()); EXPECT_EQ(CommonCode::from_string(TEST_C_CODE_STR), TEST_C_CODE); - // CommonCode::from_string(std::string &&) - EXPECT_TRUE(CommonCode::from_string(TEST_C_CODE_STR_RV).has_value()); - EXPECT_FALSE(CommonCode::from_string(TEST_C_CODE_STR_ERR_RV).has_value()); - EXPECT_EQ(CommonCode::from_string(TEST_C_CODE_STR_RV), TEST_C_CODE); - // CommonCode::from_raw_code(RawCode) EXPECT_EQ(CommonCode::from_raw_code(raw_code), TEST_C_CODE); @@ -107,15 +129,10 @@ TEST(CommonCode, initializate) { EXPECT_FALSE(CommonCode::from_short_code(TEST_S_CODE_ERR).has_value()); EXPECT_EQ(CommonCode::from_short_code(TEST_S_CODE), TEST_C_CODE); - // CommonCode::from_short_code(const std::string &) + // CommonCode::from_short_code(std::string_view) EXPECT_TRUE(CommonCode::from_short_code(TEST_S_CODE_STR).has_value()); EXPECT_FALSE(CommonCode::from_short_code(TEST_S_CODE_STR_ERR).has_value()); EXPECT_EQ(CommonCode::from_short_code(TEST_S_CODE_STR), TEST_C_CODE); - - // CommonCode::from_short_code(std::string &&) - EXPECT_TRUE(CommonCode::from_short_code(TEST_S_CODE_STR_RV).has_value()); - EXPECT_FALSE(CommonCode::from_short_code(TEST_S_CODE_STR_ERR_RV).has_value()); - EXPECT_EQ(CommonCode::from_short_code(TEST_S_CODE_STR_RV), TEST_C_CODE); } TEST(CommonCode, code_verify) { diff --git a/src/core_test/codec/helper/codec.cc b/src/core_test/codec/helper/codec.cc new file mode 100644 index 0000000..4b59f9a --- /dev/null +++ b/src/core_test/codec/helper/codec.cc @@ -0,0 +1,27 @@ +#include + +#include "codec.h" + +void head_parallel(std::function &&func) { + BS::thread_pool pool; + // TODO: skip invalid head + pool.detach_sequence(0, 16, [func = std::move(func)](const uint64_t head) { + if (head == 3 || head == 7 || head == 11 || head == 15) { + return; + } + func(head); + }); + pool.wait(); +} + +std::vector all_common_codes() { + // TODO: using `std::ranges` + std::vector common_codes; + common_codes.reserve(ALL_CASES_NUM_); + for (uint64_t head = 0; head < 16; ++head) { + for (auto range : AllCases::instance().fetch()[head]) { + common_codes.emplace_back(head << 32 | range); + } + } + return common_codes; +} diff --git a/src/core_test/codec/helper/codec.h b/src/core_test/codec/helper/codec.h new file mode 100644 index 0000000..4d55c9c --- /dev/null +++ b/src/core_test/codec/helper/codec.h @@ -0,0 +1,14 @@ +#pragma once + +#include + +#include "all_cases/all_cases.h" + +using klotski::cases::AllCases; +using klotski::cases::ALL_CASES_NUM_; + +/// Build all valid CommonCodes. +std::vector all_common_codes(); + +/// Spawn all valid klotski headers in parallel. +void head_parallel(std::function &&func); diff --git a/src/core_test/codec/helper/sample.h b/src/core_test/codec/helper/sample.h new file mode 100644 index 0000000..ee03abf --- /dev/null +++ b/src/core_test/codec/helper/sample.h @@ -0,0 +1,36 @@ +#pragma once + +#include +#include + +// ----------------------------------------------------------------------------------------- // + +/// Valid klotski RawCode +constexpr uint64_t TEST_R_CODE = 0x0603'EDF5'CAFF'F5E2; + +/// Valid klotski ShortCode +constexpr uint32_t TEST_S_CODE = 4091296; +const std::string TEST_S_CODE_STR = "4WVE1"; // TODO: using `std::string_view` +#define TEST_S_CODE_STR_RV std::string(TEST_S_CODE_STR) // TODO: remove r-value + +/// Valid klotski CommonCode +constexpr uint64_t TEST_C_CODE = 0x1'A9BF'0C00; +const std::string TEST_C_CODE_STR = "1A9BF0C00"; +#define TEST_C_CODE_STR_RV std::string(TEST_C_CODE_STR) // TODO: remove r-value + +// ----------------------------------------------------------------------------------------- // + +/// Invalid klotski RawCode +constexpr uint64_t TEST_R_CODE_ERR = 0x1603'ED00'CAFF'F5E2; + +/// Invalid klotski ShortCode +constexpr uint32_t TEST_S_CODE_ERR = 1234564323; +const std::string TEST_S_CODE_STR_ERR = "ZZZZZZ"; +#define TEST_S_CODE_STR_ERR_RV std::string(TEST_S_CODE_STR_ERR) // TODO: remove r-value + +/// Invalid klotski CommonCode +constexpr uint64_t TEST_C_CODE_ERR = 0x3'A9BF'0C00; +const static std::string TEST_C_CODE_STR_ERR = "0123456789"; +#define TEST_C_CODE_STR_ERR_RV std::string(TEST_C_CODE_STR_ERR) // TODO: remove r-value + +// ----------------------------------------------------------------------------------------- // diff --git a/src/core_test/codec/raw_code.cc b/src/core_test/codec/raw_code.cc index f533f96..836ec9e 100644 --- a/src/core_test/codec/raw_code.cc +++ b/src/core_test/codec/raw_code.cc @@ -1,8 +1,9 @@ #include #include -#include "sample.h" #include "utils/common.h" +#include "helper/codec.h" +#include "helper/sample.h" #include "raw_code/raw_code.h" #include "all_cases/all_cases.h" #include "common_code/common_code.h" diff --git a/src/core_test/codec/sample.h b/src/core_test/codec/sample.h deleted file mode 100644 index 03c94e5..0000000 --- a/src/core_test/codec/sample.h +++ /dev/null @@ -1,54 +0,0 @@ -#pragma once - -#include -#include - -#include "all_cases/all_cases.h" - -// ----------------------------------------------------------------------------------------- // - -// Valid RawCode -const uint64_t TEST_R_CODE = 0x0603'EDF5'CAFF'F5E2; - -// Valid ShortCode -const uint32_t TEST_S_CODE = 4091296; -const std::string TEST_S_CODE_STR = "4WVE1"; -#define TEST_S_CODE_STR_RV std::string(TEST_S_CODE_STR) // r-value - -// Valid CommonCode -const uint64_t TEST_C_CODE = 0x1'A9BF'0C00; -const std::string TEST_C_CODE_STR = "1A9BF0C00"; -#define TEST_C_CODE_STR_RV std::string(TEST_C_CODE_STR) // r-value - -// ----------------------------------------------------------------------------------------- // - -// Invalid RawCode -const static uint64_t TEST_R_CODE_ERR = 0x1603'ED00'CAFF'F5E2; - -// Invalid ShortCode -const static uint32_t TEST_S_CODE_ERR = 1234564323; -const static std::string TEST_S_CODE_STR_ERR = "ZZZZZZ"; -#define TEST_S_CODE_STR_ERR_RV std::string(TEST_S_CODE_STR_ERR) // r-value - -// Invalid CommonCode -const static uint64_t TEST_C_CODE_ERR = 0x3'A9BF'0C00; -const static std::string TEST_C_CODE_STR_ERR = "0123456789"; -#define TEST_C_CODE_STR_ERR_RV std::string(TEST_C_CODE_STR_ERR) // r-value - -// ----------------------------------------------------------------------------------------- // - -using klotski::cases::AllCases; -using klotski::cases::ALL_CASES_NUM_; - -inline std::vector all_common_codes() { - std::vector common_codes; - common_codes.reserve(ALL_CASES_NUM_); - for (uint64_t head = 0; head < 16; ++head) { - for (auto range : AllCases::instance().fetch()[head]) { - common_codes.emplace_back(head << 32 | range); - } - } - return common_codes; -} - -// ----------------------------------------------------------------------------------------- // diff --git a/src/core_test/codec/short_code.cc b/src/core_test/codec/short_code.cc index 4c6cf4c..a993f83 100644 --- a/src/core_test/codec/short_code.cc +++ b/src/core_test/codec/short_code.cc @@ -2,8 +2,9 @@ #include #include -#include "sample.h" -#include "exposer.h" +#include "helper/codec.h" +#include "helper/sample.h" +#include "utility/exposer.h" #include "all_cases/all_cases.h" #include "short_code/short_code.h" #include "common_code/common_code.h"