Browse Source

test: add horizontal mirror global test

legacy
Dnomd343 2 years ago
parent
commit
8a6b5e3707
  1. 4
      src/klotski_core/raw_code/mirror.cc
  2. 4
      src/klotski_core/raw_code/raw_code.cc
  3. 1
      src/klotski_core/raw_code/raw_code.h
  4. 48
      test/codec/raw_code.cc

4
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) { bool RawCode::horizontal_mirror_check(uint64_t raw_code) {
horizontal_fill(raw_code); horizontal_fill(raw_code);
return ((raw_code ^ (raw_code >> 9)) & MASK_MIRROR_A) return ((MASK_MIRROR_A & ((raw_code >> 9) ^ raw_code)) == 0)
&& ((raw_code ^ (raw_code >> 3)) & MASK_MIRROR_B); && ((MASK_MIRROR_B & ((raw_code >> 3) ^ raw_code)) == 0);
} }

4
src/klotski_core/raw_code/raw_code.cc

@ -28,6 +28,10 @@ namespace klotski {
return this->code == raw_code.code; 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) { std::ostream& operator<<(std::ostream &out, const RawCode &self) {
char code[16]; char code[16];
char dump_map[] = { char dump_map[] = {

1
src/klotski_core/raw_code/raw_code.h

@ -74,6 +74,7 @@ namespace klotski {
/// Operators of RawCode /// Operators of RawCode
bool operator==(uint64_t raw_code) const; bool operator==(uint64_t raw_code) const;
bool operator==(const RawCode &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; } constexpr explicit operator uint64_t() const { return code; }
friend std::ostream& operator<<(std::ostream &out, const RawCode &self); friend std::ostream& operator<<(std::ostream &out, const RawCode &self);

48
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_string).unwrap(), TEST_CODE); // l-value
EXPECT_EQ(RawCode::from_common_code(common_code.to_string()).unwrap(), TEST_CODE); // r-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<RawCode&&>(raw_code_1)), true); // r-value
EXPECT_EQ(raw_code_1.is_horizontal_mirror(static_cast<RawCode&&>(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<RawCode&&>(raw_code_1)), false); // r-value
EXPECT_EQ(raw_code_2.is_horizontal_mirror(static_cast<RawCode&&>(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();
}
}

Loading…
Cancel
Save