From 12beb94eeb5f563659ed6ae93be3a4c4bbc80da5 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sat, 14 Dec 2024 15:36:19 +0800 Subject: [PATCH] test: add mirror test helper --- src/core_test/CMakeLists.txt | 1 + src/core_test/codec/common_code.cc | 20 ++++-- src/core_test/codec/raw_code.cc | 20 ++++-- src/core_test/helper/internal/mirror.cc | 88 +++++++++++++++++++++++++ src/core_test/helper/mirror.h | 17 ++++- 5 files changed, 131 insertions(+), 15 deletions(-) create mode 100644 src/core_test/helper/internal/mirror.cc diff --git a/src/core_test/CMakeLists.txt b/src/core_test/CMakeLists.txt index 082ec53..69474d6 100644 --- a/src/core_test/CMakeLists.txt +++ b/src/core_test/CMakeLists.txt @@ -17,6 +17,7 @@ add_library(test_helper helper/internal/parallel.cc helper/internal/hash.cc helper/internal/group.cc + helper/internal/mirror.cc ) target_link_libraries(test_helper PRIVATE klotski_core bs::thread_pool md5sum::md5 xxHash::xxh3) diff --git a/src/core_test/codec/common_code.cc b/src/core_test/codec/common_code.cc index 2a86771..6f5e300 100644 --- a/src/core_test/codec/common_code.cc +++ b/src/core_test/codec/common_code.cc @@ -3,6 +3,7 @@ #include "test_samples.h" #include "helper/expect.h" +#include "helper/mirror.h" #include "helper/parallel.h" #include "raw_code/raw_code.h" @@ -163,18 +164,23 @@ TEST(CommonCode, code_verify) { TEST(CommonCode, code_mirror) { COMMON_CODE_PARALLEL({ const auto mirror_v = code.to_vertical_mirror(); - EXPECT_TRUE(CommonCode::check(mirror_v.unwrap())); + EXPECT_EQ(mirror_v, helper::get_vertical_mirror(code)); EXPECT_EQ(mirror_v.to_vertical_mirror(), code); - EXPECT_FALSE(mirror_v.is_vertical_mirror()); // not exist - EXPECT_NE(mirror_v, code); + if (mirror_v == code) { + EXPECT_TRUE(code.is_vertical_mirror()); + } else { + EXPECT_FALSE(code.is_vertical_mirror()); + } + }); + COMMON_CODE_PARALLEL({ const auto mirror_h = code.to_horizontal_mirror(); - EXPECT_TRUE(CommonCode::check(mirror_h.unwrap())); + EXPECT_EQ(mirror_h, helper::get_horizontal_mirror(code)); EXPECT_EQ(mirror_h.to_horizontal_mirror(), code); - if (mirror_h.is_horizontal_mirror()) { - EXPECT_EQ(mirror_h, code); + if (mirror_h == code) { + EXPECT_TRUE(code.is_horizontal_mirror()); } else { - EXPECT_NE(mirror_h, code); + EXPECT_FALSE(code.is_horizontal_mirror()); } }); } diff --git a/src/core_test/codec/raw_code.cc b/src/core_test/codec/raw_code.cc index de38adc..f01a598 100644 --- a/src/core_test/codec/raw_code.cc +++ b/src/core_test/codec/raw_code.cc @@ -2,6 +2,7 @@ #include "test_samples.h" #include "helper/expect.h" +#include "helper/mirror.h" #include "helper/parallel.h" #include "utils/common.h" @@ -131,18 +132,23 @@ TEST(RawCode, code_verify) { TEST(RawCode, code_mirror) { RAW_CODE_PARALLEL({ const auto mirror_v = code.to_vertical_mirror(); - EXPECT_TRUE(RawCode::check(mirror_v.unwrap())); + EXPECT_EQ(mirror_v, helper::get_vertical_mirror(code)); EXPECT_EQ(mirror_v.to_vertical_mirror(), code); - EXPECT_FALSE(mirror_v.is_vertical_mirror()); // not exist - EXPECT_NE(mirror_v, code); + if (mirror_v == code) { + EXPECT_TRUE(code.is_vertical_mirror()); + } else { + EXPECT_FALSE(code.is_vertical_mirror()); + } + }); + RAW_CODE_PARALLEL({ const auto mirror_h = code.to_horizontal_mirror(); - EXPECT_TRUE(RawCode::check(mirror_h.unwrap())); + EXPECT_EQ(mirror_h, helper::get_horizontal_mirror(code)); EXPECT_EQ(mirror_h.to_horizontal_mirror(), code); - if (mirror_h.is_horizontal_mirror()) { - EXPECT_EQ(mirror_h, code); + if (mirror_h == code) { + EXPECT_TRUE(code.is_horizontal_mirror()); } else { - EXPECT_NE(mirror_h, code); + EXPECT_FALSE(code.is_horizontal_mirror()); } }); } diff --git a/src/core_test/helper/internal/mirror.cc b/src/core_test/helper/internal/mirror.cc new file mode 100644 index 0000000..7854cc3 --- /dev/null +++ b/src/core_test/helper/internal/mirror.cc @@ -0,0 +1,88 @@ +#include "helper/mirror.h" + +#include "utils/common.h" + +using klotski::codec::RawCode; +using klotski::codec::CommonCode; + +using namespace klotski; + +RawCode helper::get_horizontal_mirror(RawCode code) { + uint64_t new_code = 0; + + for (int addr = 0; addr < 20; ++addr) { + + auto block = (code.unwrap() >> (addr * 3)) & 0b111; + + if (block == BLOCK_CE_1x1) { + auto target_addr = addr - (addr % 4); + target_addr += 3 - (addr % 4); + new_code |= K_MASK_1x1 << (target_addr * 3); + } else if (block == BLOCK_CE_2x1) { + auto target_addr = addr - (addr % 4); + target_addr += 3 - (addr % 4); + new_code |= K_MASK_2x1 << (target_addr * 3); + } else if (block == BLOCK_CE_1x2) { + auto target_addr = addr - (addr % 4); + if (addr % 4 == 0) { + target_addr += 2; + } else if (addr % 4 == 1) { + target_addr += 1; + } + new_code |= K_MASK_1x2 << (target_addr * 3); + } else if (block == BLOCK_CE_2x2) { + auto target_addr = addr - (addr % 4); + if (addr % 4 == 0) { + target_addr += 2; + } else if (addr % 4 == 1) { + target_addr += 1; + } + new_code |= K_MASK_2x2 << (target_addr * 3); + } + + } + + return RawCode::unsafe_create(new_code); +} + +RawCode helper::get_vertical_mirror(RawCode code) { + uint64_t new_code = 0; + + for (int addr = 0; addr < 20; ++addr) { + + auto block = (code.unwrap() >> (addr * 3)) & 0b111; + + if (block == BLOCK_CE_1x1) { + auto target_addr = addr - (addr % 4); + target_addr = 16 - target_addr; + target_addr += (addr % 4); + new_code |= K_MASK_1x1 << (target_addr * 3); + } else if (block == BLOCK_CE_2x1) { + auto target_addr = addr - (addr % 4); + target_addr = 12 - target_addr; + target_addr += (addr % 4); + new_code |= K_MASK_2x1 << (target_addr * 3); + } else if (block == BLOCK_CE_1x2) { + auto target_addr = addr - (addr % 4); + target_addr = 16 - target_addr; + target_addr += (addr % 4); + new_code |= K_MASK_1x2 << (target_addr * 3); + } else if (block == BLOCK_CE_2x2) { + auto target_addr = addr - (addr % 4); + target_addr = 12 - target_addr; + target_addr += (addr % 4); + new_code |= K_MASK_2x2 << (target_addr * 3); + } + + } + + return RawCode::unsafe_create(new_code); +} + +CommonCode helper::get_vertical_mirror(CommonCode code) { + return get_vertical_mirror(code.to_raw_code()).to_common_code(); +} + +CommonCode helper::get_horizontal_mirror(CommonCode code) { + return get_horizontal_mirror(code.to_raw_code()).to_common_code(); +} diff --git a/src/core_test/helper/mirror.h b/src/core_test/helper/mirror.h index 691943b..7bc94f6 100644 --- a/src/core_test/helper/mirror.h +++ b/src/core_test/helper/mirror.h @@ -1,3 +1,18 @@ #pragma once -// TODO: mirror convert of RawCode +#include "raw_code/raw_code.h" + +namespace helper { + +using klotski::codec::RawCode; +using klotski::codec::CommonCode; + +RawCode get_vertical_mirror(RawCode code); + +RawCode get_horizontal_mirror(RawCode code); + +CommonCode get_vertical_mirror(CommonCode code); + +CommonCode get_horizontal_mirror(CommonCode code); + +} // namespace helper