From 8a6b5e3707243362e3517d97fbb4f98f274e2044 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 26 Feb 2023 14:21:20 +0800 Subject: [PATCH] test: add horizontal mirror global test --- src/klotski_core/raw_code/mirror.cc | 4 +-- src/klotski_core/raw_code/raw_code.cc | 4 +++ src/klotski_core/raw_code/raw_code.h | 1 + test/codec/raw_code.cc | 48 +++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 2 deletions(-) diff --git a/src/klotski_core/raw_code/mirror.cc b/src/klotski_core/raw_code/mirror.cc index d187349..e3162ae 100644 --- a/src/klotski_core/raw_code/mirror.cc +++ b/src/klotski_core/raw_code/mirror.cc @@ -101,6 +101,6 @@ bool RawCode::vertical_mirror_check(uint64_t raw_code) { bool RawCode::horizontal_mirror_check(uint64_t raw_code) { horizontal_fill(raw_code); - return ((raw_code ^ (raw_code >> 9)) & MASK_MIRROR_A) - && ((raw_code ^ (raw_code >> 3)) & MASK_MIRROR_B); + return ((MASK_MIRROR_A & ((raw_code >> 9) ^ raw_code)) == 0) + && ((MASK_MIRROR_B & ((raw_code >> 3) ^ raw_code)) == 0); } diff --git a/src/klotski_core/raw_code/raw_code.cc b/src/klotski_core/raw_code/raw_code.cc index 845643b..a486aed 100644 --- a/src/klotski_core/raw_code/raw_code.cc +++ b/src/klotski_core/raw_code/raw_code.cc @@ -28,6 +28,10 @@ namespace klotski { return this->code == raw_code.code; } + bool RawCode::operator!=(const RawCode &raw_code) const { + return this->code != raw_code.code; + } + std::ostream& operator<<(std::ostream &out, const RawCode &self) { char code[16]; char dump_map[] = { diff --git a/src/klotski_core/raw_code/raw_code.h b/src/klotski_core/raw_code/raw_code.h index ad34e07..40852c4 100644 --- a/src/klotski_core/raw_code/raw_code.h +++ b/src/klotski_core/raw_code/raw_code.h @@ -74,6 +74,7 @@ namespace klotski { /// Operators of RawCode bool operator==(uint64_t raw_code) const; bool operator==(const RawCode &raw_code) const; + bool operator!=(const RawCode &raw_code) const; constexpr explicit operator uint64_t() const { return code; } friend std::ostream& operator<<(std::ostream &out, const RawCode &self); diff --git a/test/codec/raw_code.cc b/test/codec/raw_code.cc index 85bdba9..dd0245c 100644 --- a/test/codec/raw_code.cc +++ b/test/codec/raw_code.cc @@ -70,3 +70,51 @@ TEST(RawCode, initializate) { EXPECT_EQ(RawCode::from_common_code(common_code_string).unwrap(), TEST_CODE); // l-value EXPECT_EQ(RawCode::from_common_code(common_code.to_string()).unwrap(), TEST_CODE); // r-value } + +TEST(RawCode, horizontal_mirror) { + auto raw_code_1 = RawCode::from_common_code(0x1A9BF0C00); + auto raw_code_2 = RawCode::from_common_code(0x4FEA13400); + auto raw_code_2_ = RawCode::from_common_code(0x6BFA47000); + + EXPECT_EQ(raw_code_1.is_horizontal_mirror(), true); + EXPECT_EQ(raw_code_1.to_horizontal_mirror(), raw_code_1); + EXPECT_EQ(raw_code_1.is_horizontal_mirror(raw_code_1), true); // l-value + EXPECT_EQ(raw_code_1.is_horizontal_mirror(raw_code_2), false); // l-value + EXPECT_EQ(raw_code_1.is_horizontal_mirror(static_cast(raw_code_1)), true); // r-value + EXPECT_EQ(raw_code_1.is_horizontal_mirror(static_cast(raw_code_2)), false); // r-value + + EXPECT_EQ(raw_code_2.is_horizontal_mirror(), false); + EXPECT_EQ(raw_code_2.to_horizontal_mirror(), raw_code_2_); + EXPECT_EQ(raw_code_2.is_horizontal_mirror(raw_code_1), false); // l-value + EXPECT_EQ(raw_code_2.is_horizontal_mirror(raw_code_2_), true); // l-value + EXPECT_EQ(raw_code_2.is_horizontal_mirror(static_cast(raw_code_1)), false); // r-value + EXPECT_EQ(raw_code_2.is_horizontal_mirror(static_cast(raw_code_2_)), true); // r-value +} + +TEST(RawCode, horizontal_mirror_global) { + std::thread threads[16]; + auto test = [](uint64_t head) { + for (const auto &range : AllCases::fetch()[head]) { + /// generate code and mirror layout + auto common_code = CommonCode::unsafe_create(head << 32 | range); + auto raw_code = common_code.to_raw_code(); + auto raw_code_mirror = raw_code.to_horizontal_mirror(); + + /// verify horizontal mirror + EXPECT_EQ(raw_code_mirror.valid(), true); + EXPECT_EQ(raw_code_mirror.to_common_code().to_raw_code(), raw_code_mirror); + if (raw_code.is_horizontal_mirror()) { + EXPECT_EQ(raw_code, raw_code_mirror); + } else { + EXPECT_NE(raw_code, raw_code_mirror); + } + EXPECT_EQ(raw_code_mirror.to_horizontal_mirror(), raw_code); + } + }; + for (uint64_t head = 0; head < 16; ++head) { // split into 16 threads + threads[head] = std::thread(test, head); + } + for (auto &t : threads) { + t.join(); + } +}