From 70a99e5f0697c3b1ed457da8e661ca2af60fb5f9 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Thu, 5 Oct 2023 14:07:24 +0800 Subject: [PATCH] update: multiple enhancements and fixes --- src/core/CMakeLists.txt | 2 +- src/core/all_cases/all_cases.cc | 2 - src/core/all_cases/all_cases.h | 40 +--------- src/core/all_cases/inline_impl.h | 51 ++++++++++++ src/core/common_code/sundry.cc | 41 ++++------ src/core/main.cc | 15 +++- src/core/raw_code/convert.cc | 8 +- src/core/raw_code/mirror.cc | 2 + src/core/raw_code/sundry.cc | 24 +++--- src/core/short_code/short_code.cc | 14 ---- src/core/short_code/short_code.h | 1 - src/core/short_code/sundry.cc | 48 +++++------- src/core_test/codec/common_code.cc | 121 +++++++++++++++++------------ src/core_test/codec/sample.h | 36 +++++++++ 14 files changed, 224 insertions(+), 181 deletions(-) create mode 100644 src/core/all_cases/inline_impl.h create mode 100644 src/core_test/codec/sample.h diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index a8c32b3..8f39fae 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.0) -set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_STANDARD 23) project(klotski-core VERSION 0.9.1 LANGUAGES CXX) diff --git a/src/core/all_cases/all_cases.cc b/src/core/all_cases/all_cases.cc index 27a4884..3a1af1b 100644 --- a/src/core/all_cases/all_cases.cc +++ b/src/core/all_cases/all_cases.cc @@ -88,8 +88,6 @@ void AllCases::build_parallel(Executor &&executor) noexcept { } std::vector> futures; for (auto head : case_heads()) { - // TODO: using std::move_only_function in C++23 - // -> avoid using std::shared_ptr> auto promise = std::make_shared>(); futures.emplace_back(promise->get_future()); executor([head, promise = std::move(promise)]() { diff --git a/src/core/all_cases/all_cases.h b/src/core/all_cases/all_cases.h index 09c8fe5..21f0412 100644 --- a/src/core/all_cases/all_cases.h +++ b/src/core/all_cases/all_cases.h @@ -89,25 +89,6 @@ private: static void spawn_ranges(Ranges &ranges, int, int, int, int) noexcept; }; -inline BasicRanges& BasicRanges::instance() noexcept { - static BasicRanges instance; - return instance; -} - -inline Ranges& BasicRanges::get_ranges() noexcept { - static Ranges ranges; - return ranges; -} - -inline const Ranges& BasicRanges::fetch() noexcept { - this->build(); - return get_ranges(); -} - -inline bool BasicRanges::is_available() const noexcept { - return available_; // no mutex required in one-way state -} - // ----------------------------------------------------------------------------------------- // class AllCases { @@ -131,26 +112,9 @@ private: static void build_cases(int head, Ranges &release) noexcept; }; -inline AllCases& AllCases::instance() noexcept { - static AllCases instance; - return instance; -} - -inline RangesUnion& AllCases::get_cases() noexcept { - static RangesUnion cases; - return cases; -} - -inline const RangesUnion& AllCases::fetch() noexcept { - this->build(); - return get_cases(); -} - -inline bool AllCases::is_available() const noexcept { - return available_; // no mutex required in one-way state -} - // ----------------------------------------------------------------------------------------- // } // namespace cases } // namespace klotski + +#include "inline_impl.h" diff --git a/src/core/all_cases/inline_impl.h b/src/core/all_cases/inline_impl.h new file mode 100644 index 0000000..fe44af0 --- /dev/null +++ b/src/core/all_cases/inline_impl.h @@ -0,0 +1,51 @@ +#pragma once + +namespace klotski { +namespace cases { + +// ----------------------------------------------------------------------------------------- // + +inline BasicRanges& BasicRanges::instance() noexcept { + static BasicRanges instance; + return instance; +} + +inline Ranges& BasicRanges::get_ranges() noexcept { + static Ranges ranges; + return ranges; +} + +inline const Ranges& BasicRanges::fetch() noexcept { + this->build(); + return get_ranges(); +} + +inline bool BasicRanges::is_available() const noexcept { + return available_; // no mutex required in one-way state +} + +// ----------------------------------------------------------------------------------------- // + +inline AllCases& AllCases::instance() noexcept { + static AllCases instance; + return instance; +} + +inline RangesUnion& AllCases::get_cases() noexcept { + static RangesUnion cases; + return cases; +} + +inline const RangesUnion& AllCases::fetch() noexcept { + this->build(); + return get_cases(); +} + +inline bool AllCases::is_available() const noexcept { + return available_; // no mutex required in one-way state +} + +// ----------------------------------------------------------------------------------------- // + +} // namespace codec +} // namespace klotski diff --git a/src/core/common_code/sundry.cc b/src/core/common_code/sundry.cc index d0eb320..660b258 100644 --- a/src/core/common_code/sundry.cc +++ b/src/core/common_code/sundry.cc @@ -39,11 +39,9 @@ std::optional CommonCode::from_string(std::string &&common_code) noe } std::optional CommonCode::from_string(const std::string &common_code) noexcept { - auto code = string_decode(common_code); - if (!code.has_value()) { - return std::nullopt; // invalid string - } - return CommonCode::unsafe_create(code.value()); + return string_decode(common_code).transform([](auto code) { + return CommonCode::unsafe_create(code); + }); } // ----------------------------------------------------------------------------------------- // @@ -52,13 +50,10 @@ CommonCode CommonCode::from_raw_code(RawCode raw_code) noexcept { return raw_code.to_common_code(); } -// TODO: using `std::optional::transform` in C++23 std::optional CommonCode::from_raw_code(uint64_t raw_code) noexcept { - auto code = RawCode::create(raw_code); - if (!code.has_value()) { - return std::nullopt; // invalid raw code - } - return code->to_common_code(); + return RawCode::create(raw_code).transform([](auto raw_code) { + return raw_code.to_common_code(); + }); } // ----------------------------------------------------------------------------------------- // @@ -68,27 +63,21 @@ CommonCode CommonCode::from_short_code(ShortCode short_code) noexcept { } std::optional CommonCode::from_short_code(uint32_t short_code) noexcept { - auto code = ShortCode::create(short_code); - if (!code.has_value()) { - return std::nullopt; // invalid short code - } - return code->to_common_code(); + return ShortCode::create(short_code).transform([](auto short_code) { + return short_code.to_common_code(); + }); } std::optional CommonCode::from_short_code(std::string &&short_code) noexcept { - auto code = ShortCode::from_string(std::move(short_code)); - if (!code.has_value()) { - return std::nullopt; // invalid short code - } - return code->to_common_code(); + return ShortCode::from_string(std::move(short_code)).transform([](auto short_code) { + return short_code.to_common_code(); + }); } std::optional CommonCode::from_short_code(const std::string &short_code) noexcept { - auto code = ShortCode::from_string(short_code); - if (!code.has_value()) { - return std::nullopt; // invalid short code - } - return code->to_common_code(); + return ShortCode::from_string(short_code).transform([](auto short_code) { + return short_code.to_common_code(); + }); } // ----------------------------------------------------------------------------------------- // diff --git a/src/core/main.cc b/src/core/main.cc index 4c59625..5694eed 100644 --- a/src/core/main.cc +++ b/src/core/main.cc @@ -1,4 +1,7 @@ +#include #include +#include + #include "raw_code.h" #include "all_cases.h" #include "short_code.h" @@ -11,10 +14,12 @@ using klotski::codec::RawCode; using klotski::codec::ShortCode; using klotski::codec::CommonCode; +using klotski::codec::SHORT_CODE_LIMIT; + int main() { -// AllCases::instance().build(); - BasicRanges::instance().build(); + AllCases::instance().build(); +// BasicRanges::instance().build(); // std::vector common_codes; // common_codes.reserve(klotski::cases::ALL_CASES_NUM_); @@ -31,8 +36,14 @@ int main() { // common_codes_str.emplace_back(CommonCode::string_encode(x, false)); // } + ShortCode::speed_up(true); + auto start = clock(); + for (uint32_t short_code = 0; short_code < SHORT_CODE_LIMIT; ++short_code) { + ShortCode::unsafe_create(short_code).to_common_code(true); + } + // for (auto common_code : common_codes) { // printf("%llX\n", common_code); // CommonCode::string_encode(common_code, true); diff --git a/src/core/raw_code/convert.cc b/src/core/raw_code/convert.cc index d5bdb23..e5c05e3 100644 --- a/src/core/raw_code/convert.cc +++ b/src/core/raw_code/convert.cc @@ -5,8 +5,8 @@ namespace klotski { namespace codec { -/// NOTE: ensure that input raw code is valid! -uint64_t RawCode::compact(uint64_t raw_code) noexcept { // raw code --> common code +/// Convert RawCode to CommonCode. +uint64_t RawCode::compact(uint64_t raw_code) noexcept { int unfilled = 16; uint64_t head = 0; // 2x2 block address uint32_t range = 0; @@ -34,8 +34,8 @@ uint64_t RawCode::compact(uint64_t raw_code) noexcept { // raw code --> common c return head | (range << (unfilled << 1)); // fill low bits as zero } -/// NOTE: ensure that input common code is valid! -uint64_t RawCode::extract(uint64_t common_code) noexcept { // common code --> raw code +/// Convert CommonCode to RawCode. +uint64_t RawCode::extract(uint64_t common_code) noexcept { auto code = K_MASK_2x2 << (common_code >> 32) * 3; // flag for 2x2 block auto range = range_reverse((uint32_t)common_code); // reversed range diff --git a/src/core/raw_code/mirror.cc b/src/core/raw_code/mirror.cc index aecd7c5..06a7aaf 100644 --- a/src/core/raw_code/mirror.cc +++ b/src/core/raw_code/mirror.cc @@ -116,5 +116,7 @@ bool RawCode::check_horizontal_mirror(uint64_t raw_code) noexcept { && !(MASK_MIRROR_H2 & ((raw_code >> 3) ^ raw_code)); } +// ----------------------------------------------------------------------------------------- // + } // namespace codec } // namespace klotski diff --git a/src/core/raw_code/sundry.cc b/src/core/raw_code/sundry.cc index 777eb97..224cedf 100644 --- a/src/core/raw_code/sundry.cc +++ b/src/core/raw_code/sundry.cc @@ -23,27 +23,21 @@ RawCode RawCode::from_common_code(CommonCode common_code) noexcept { } std::optional RawCode::from_common_code(uint64_t common_code) noexcept { - auto code = CommonCode::create(common_code); - if (!code.has_value()) { - return std::nullopt; // invalid common code - } - return code->to_raw_code(); + return CommonCode::create(common_code).transform([](auto common_code) { + return common_code.to_raw_code(); + }); } std::optional RawCode::from_common_code(std::string &&common_code) noexcept { - auto code = CommonCode::from_string(std::move(common_code)); - if (!code.has_value()) { - return std::nullopt; // invalid common code - } - return code->to_raw_code(); + return CommonCode::from_short_code(std::move(common_code)).transform([](auto common_code) { + return common_code.to_raw_code(); + }); } std::optional RawCode::from_common_code(const std::string &common_code) noexcept { - auto code = CommonCode::from_string(common_code); - if (!code.has_value()) { - return std::nullopt; // invalid common code - } - return code->to_raw_code(); + return CommonCode::from_string(common_code).transform([](auto common_code) { + return common_code.to_raw_code(); + }); } // ----------------------------------------------------------------------------------------- // diff --git a/src/core/short_code/short_code.cc b/src/core/short_code/short_code.cc index 9037bf3..b2a8d7b 100644 --- a/src/core/short_code/short_code.cc +++ b/src/core/short_code/short_code.cc @@ -18,21 +18,7 @@ void ShortCode::speed_up(bool fast_mode) noexcept { } else { BasicRanges::instance().build(); } - // FIXME: setting up `fast_available_` } -//void ShortCode::speed_up(ShortCode::Mode mode) { -// if (fast_mode_available_) { -// return; // fast mode already available -// } -// if (mode == ShortCode::FAST) { // build fast mode data -// AllCases::build(); // blocking function -// fast_mode_available_ = true; -// } else if (!normal_mode_available_) { // build normal mode data -// BasicRanges::build(); // blocking function -// normal_mode_available_ = true; -// } -//} - } // namespace codec } // namespace klotski diff --git a/src/core/short_code/short_code.h b/src/core/short_code/short_code.h index a4353a1..85d4fef 100644 --- a/src/core/short_code/short_code.h +++ b/src/core/short_code/short_code.h @@ -106,7 +106,6 @@ public: private: uint32_t code_; - static bool fast_available_; // TODO: try to remove it static uint64_t fast_decode(uint32_t short_code) noexcept; static uint32_t fast_encode(uint64_t common_code) noexcept; diff --git a/src/core/short_code/sundry.cc b/src/core/short_code/sundry.cc index e50daee..ccf7c5d 100644 --- a/src/core/short_code/sundry.cc +++ b/src/core/short_code/sundry.cc @@ -1,17 +1,19 @@ #include "short_code.h" #include "common_code.h" +using klotski::cases::AllCases; + namespace klotski { namespace codec { // ----------------------------------------------------------------------------------------- // ShortCode::ShortCode(CommonCode common_code) noexcept { -// if (fast_available_) { + if (AllCases::instance().is_available()) { code_ = fast_encode(common_code.unwrap()); -// } else { -// code_ = tiny_encode(common_code.unwrap()); -// } + } else { + code_ = tiny_encode(common_code.unwrap()); + } } // ----------------------------------------------------------------------------------------- // @@ -21,10 +23,10 @@ std::string ShortCode::to_string() const noexcept { } CommonCode ShortCode::to_common_code() const noexcept { -// if (fast_available_) { + if (AllCases::instance().is_available()) { return CommonCode::unsafe_create(fast_decode(code_)); -// } -// return CommonCode::unsafe_create(tiny_decode(code_)); + } + return CommonCode::unsafe_create(tiny_decode(code_)); } // ----------------------------------------------------------------------------------------- // @@ -34,11 +36,9 @@ std::optional ShortCode::from_string(std::string &&short_code) noexce } std::optional ShortCode::from_string(const std::string &short_code) noexcept { - auto code = ShortCode::string_decode(short_code); - if (!code.has_value()) { - return std::nullopt; // invalid string - } - return ShortCode::unsafe_create(code.value()); + return ShortCode::string_decode(short_code).transform([](auto code) { + return ShortCode::unsafe_create(code); + }); } // ----------------------------------------------------------------------------------------- // @@ -48,27 +48,21 @@ ShortCode ShortCode::from_common_code(CommonCode common_code) noexcept { } std::optional ShortCode::from_common_code(uint64_t common_code) noexcept { - auto code = CommonCode::create(common_code); - if (!code.has_value()) { - return std::nullopt; // invalid common code - } - return code->to_short_code(); + return CommonCode::create(common_code).transform([](auto common_code) { + return common_code.to_short_code(); + }); } std::optional ShortCode::from_common_code(std::string &&common_code) noexcept { - auto code = CommonCode::from_string(std::move(common_code)); - if (!code.has_value()) { - return std::nullopt; // invalid common code - } - return code->to_short_code(); + return CommonCode::from_string(std::move(common_code)).transform([](auto common_code) { + return common_code.to_short_code(); + }); } std::optional ShortCode::from_common_code(const std::string &common_code) noexcept { - auto code = CommonCode::from_string(common_code); - if (!code.has_value()) { - return std::nullopt; // invalid common code - } - return code->to_short_code(); + return CommonCode::from_string(common_code).transform([](auto common_code) { + return common_code.to_short_code(); + }); } // ----------------------------------------------------------------------------------------- // diff --git a/src/core_test/codec/common_code.cc b/src/core_test/codec/common_code.cc index 69e37e5..6b1a908 100644 --- a/src/core_test/codec/common_code.cc +++ b/src/core_test/codec/common_code.cc @@ -1,5 +1,6 @@ #include +#include "sample.h" #include "all_cases.h" #include "raw_code.h" #include "short_code.h" @@ -14,9 +15,6 @@ using klotski::codec::CommonCode; using klotski::cases::AllCases; using klotski::cases::ALL_CASES_NUM_; -const static uint64_t TEST_CODE = 0x1'A9BF'0C00; -const static std::string TEST_CODE_STR = "1A9BF0C00"; - TEST(CommonCode, validity) { EXPECT_NE(CommonCode::check(0x3'A9'BF'0C'00), true); // invalid 2x2 block EXPECT_NE(CommonCode::check(0x1'D9'BF'0C'00), true); // invalid block range @@ -29,23 +27,76 @@ TEST(CommonCode, validity) { } TEST(CommonCode, operators) { - auto common_code = CommonCode::create(TEST_CODE).value(); + auto common_code = CommonCode::create(TEST_C_CODE).value(); std::ostringstream tmp; tmp << common_code; // ostream capture - EXPECT_EQ(tmp.str(), TEST_CODE_STR); - EXPECT_EQ((uint64_t)common_code, TEST_CODE); // convert as uint64_t + EXPECT_EQ(tmp.str(), TEST_C_CODE_STR); + EXPECT_EQ((uint64_t)common_code, TEST_C_CODE); // convert as uint64_t - EXPECT_EQ(TEST_CODE, common_code); // uint64_t == CommonCode - EXPECT_EQ(common_code, TEST_CODE); // 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_CODE + 1, common_code); // uint64_t != CommonCode - EXPECT_NE(common_code, TEST_CODE + 1); // CommonCode != uint64_t - EXPECT_NE(common_code, CommonCode::unsafe_create(TEST_CODE + 1)); // 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_LT(common_code, CommonCode::unsafe_create(TEST_C_CODE + 1)); // CommonCode < CommonCode + EXPECT_GT(CommonCode::unsafe_create(TEST_C_CODE + 1), common_code); // CommonCode > CommonCode +} - EXPECT_LT(common_code, CommonCode::unsafe_create(TEST_CODE + 1)); // CommonCode < CommonCode - EXPECT_GT(CommonCode::unsafe_create(TEST_CODE + 1), common_code); // CommonCode > CommonCode +TEST(CommonCode, initializate) { + auto raw_code = RawCode::unsafe_create(TEST_R_CODE); + auto short_code = ShortCode::unsafe_create(TEST_S_CODE); + + // CommonCode(...) + EXPECT_EQ(CommonCode(raw_code), TEST_C_CODE); + EXPECT_EQ(CommonCode(short_code), TEST_C_CODE); + + // CommonCode::create(uint64_t) + EXPECT_TRUE(CommonCode::create(TEST_C_CODE).has_value()); + EXPECT_FALSE(CommonCode::create(TEST_C_CODE_ERR).has_value()); + EXPECT_EQ(CommonCode::create(TEST_C_CODE), TEST_C_CODE); + + // CommonCode::unsafe_create(uint64_t) + EXPECT_EQ(CommonCode::unsafe_create(TEST_C_CODE), TEST_C_CODE); + + // CommonCode::from_string(const std::string &) + 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); + + // CommonCode::from_raw_code(uint64_t) + EXPECT_TRUE(CommonCode::from_raw_code(TEST_R_CODE).has_value()); + EXPECT_FALSE(CommonCode::from_raw_code(TEST_R_CODE_ERR).has_value()); + EXPECT_EQ(CommonCode::from_raw_code(TEST_R_CODE), TEST_C_CODE); + + // CommonCode::from_short_code(ShortCode) + EXPECT_EQ(CommonCode::from_short_code(short_code), TEST_C_CODE); + + // CommonCode::from_short_code(uint32_t) + EXPECT_TRUE(CommonCode::from_short_code(TEST_S_CODE).has_value()); + 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 &) + 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) { @@ -54,7 +105,7 @@ TEST(CommonCode, code_verify) { pool.push_task([](uint64_t head) { for (auto range : AllCases::instance().fetch()[head]) { auto code = head << 32 | range; - EXPECT_TRUE(CommonCode::check(code)); // verify all valid code + EXPECT_TRUE(CommonCode::check(code)); // verify all cases } }, head); } @@ -97,11 +148,11 @@ TEST(CommonCode, code_string) { } TEST(CommonCode, code_convert) { - auto common_code = CommonCode::create(TEST_CODE).value(); - EXPECT_EQ(common_code.unwrap(), TEST_CODE); - EXPECT_EQ(common_code.to_string(), TEST_CODE_STR); - EXPECT_EQ(common_code.to_raw_code(), RawCode(common_code)); - EXPECT_EQ(common_code.to_short_code(), ShortCode(common_code)); + auto common_code = CommonCode::unsafe_create(TEST_C_CODE); + EXPECT_EQ(common_code.unwrap(), TEST_C_CODE); + EXPECT_EQ(common_code.to_string(), TEST_C_CODE_STR); + EXPECT_EQ(common_code.to_raw_code(), TEST_R_CODE); + 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 @@ -112,38 +163,6 @@ TEST(CommonCode, code_convert) { EXPECT_EQ(CommonCode::from_string(std::move(code_normal)), common_code); // r-value } -TEST(CommonCode, initializate) { - EXPECT_TRUE(CommonCode::create(TEST_CODE).has_value()); - EXPECT_EQ(CommonCode::create(TEST_CODE), TEST_CODE); - EXPECT_EQ(CommonCode::unsafe_create(TEST_CODE), TEST_CODE); - - auto common_code = CommonCode::create(TEST_CODE).value(); - EXPECT_EQ(CommonCode(RawCode(common_code)), TEST_CODE); - EXPECT_EQ(CommonCode(ShortCode(common_code)), TEST_CODE); - - EXPECT_TRUE(CommonCode::from_string(TEST_CODE_STR).has_value()); - EXPECT_EQ(CommonCode::from_string(TEST_CODE_STR), TEST_CODE); // l-value - EXPECT_EQ(CommonCode::from_string(std::string(TEST_CODE_STR)), TEST_CODE); // r-value - - auto raw_code = RawCode(common_code); - auto raw_code_val = raw_code.unwrap(); - EXPECT_EQ(CommonCode::from_raw_code(raw_code), TEST_CODE); - EXPECT_TRUE(CommonCode::from_raw_code(raw_code_val).has_value()); - EXPECT_EQ(CommonCode::from_raw_code(raw_code_val), TEST_CODE); - - auto short_code = ShortCode(common_code); - auto short_code_val = short_code.unwrap(); - auto short_code_str = short_code.to_string(); - EXPECT_TRUE(CommonCode::from_short_code(short_code_val).has_value()); - EXPECT_TRUE(CommonCode::from_short_code(short_code_str).has_value()); - EXPECT_TRUE(CommonCode::from_short_code(std::string(short_code_str)).has_value()); - - EXPECT_EQ(CommonCode::from_short_code(short_code_val), TEST_CODE); - EXPECT_EQ(CommonCode::from_short_code(short_code), TEST_CODE); - EXPECT_EQ(CommonCode::from_short_code(short_code_str), TEST_CODE); // l-value - EXPECT_EQ(CommonCode::from_short_code(std::string(short_code_str)), TEST_CODE); // r-value -} - TEST(CommonCode, DISABLED_global) { std::vector common_codes; common_codes.reserve(ALL_CASES_NUM_); diff --git a/src/core_test/codec/sample.h b/src/core_test/codec/sample.h new file mode 100644 index 0000000..0178501 --- /dev/null +++ b/src/core_test/codec/sample.h @@ -0,0 +1,36 @@ +#pragma once + +#include +#include + +// ----------------------------------------------------------------------------------------- // + +// 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 + +// ----------------------------------------------------------------------------------------- //