Browse Source

test: add mirror test helper

master
Dnomd343 2 weeks ago
parent
commit
606b8d88bf
  1. 1
      src/core_test/CMakeLists.txt
  2. 20
      src/core_test/codec/common_code.cc
  3. 20
      src/core_test/codec/raw_code.cc
  4. 88
      src/core_test/helper/internal/mirror.cc
  5. 17
      src/core_test/helper/mirror.h

1
src/core_test/CMakeLists.txt

@ -17,6 +17,7 @@ add_library(test_helper
helper/internal/parallel.cc helper/internal/parallel.cc
helper/internal/hash.cc helper/internal/hash.cc
helper/internal/group.cc helper/internal/group.cc
helper/internal/mirror.cc
) )
target_link_libraries(test_helper PRIVATE klotski_core bs::thread_pool md5sum::md5 xxHash::xxh3) target_link_libraries(test_helper PRIVATE klotski_core bs::thread_pool md5sum::md5 xxHash::xxh3)

20
src/core_test/codec/common_code.cc

@ -3,6 +3,7 @@
#include "test_samples.h" #include "test_samples.h"
#include "helper/expect.h" #include "helper/expect.h"
#include "helper/mirror.h"
#include "helper/parallel.h" #include "helper/parallel.h"
#include "raw_code/raw_code.h" #include "raw_code/raw_code.h"
@ -163,18 +164,23 @@ TEST(CommonCode, code_verify) {
TEST(CommonCode, code_mirror) { TEST(CommonCode, code_mirror) {
COMMON_CODE_PARALLEL({ COMMON_CODE_PARALLEL({
const auto mirror_v = code.to_vertical_mirror(); 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_EQ(mirror_v.to_vertical_mirror(), code);
EXPECT_FALSE(mirror_v.is_vertical_mirror()); // not exist if (mirror_v == code) {
EXPECT_NE(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(); 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); EXPECT_EQ(mirror_h.to_horizontal_mirror(), code);
if (mirror_h.is_horizontal_mirror()) { if (mirror_h == code) {
EXPECT_EQ(mirror_h, code); EXPECT_TRUE(code.is_horizontal_mirror());
} else { } else {
EXPECT_NE(mirror_h, code); EXPECT_FALSE(code.is_horizontal_mirror());
} }
}); });
} }

20
src/core_test/codec/raw_code.cc

@ -2,6 +2,7 @@
#include "test_samples.h" #include "test_samples.h"
#include "helper/expect.h" #include "helper/expect.h"
#include "helper/mirror.h"
#include "helper/parallel.h" #include "helper/parallel.h"
#include "utils/common.h" #include "utils/common.h"
@ -131,18 +132,23 @@ TEST(RawCode, code_verify) {
TEST(RawCode, code_mirror) { TEST(RawCode, code_mirror) {
RAW_CODE_PARALLEL({ RAW_CODE_PARALLEL({
const auto mirror_v = code.to_vertical_mirror(); 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_EQ(mirror_v.to_vertical_mirror(), code);
EXPECT_FALSE(mirror_v.is_vertical_mirror()); // not exist if (mirror_v == code) {
EXPECT_NE(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(); 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); EXPECT_EQ(mirror_h.to_horizontal_mirror(), code);
if (mirror_h.is_horizontal_mirror()) { if (mirror_h == code) {
EXPECT_EQ(mirror_h, code); EXPECT_TRUE(code.is_horizontal_mirror());
} else { } else {
EXPECT_NE(mirror_h, code); EXPECT_FALSE(code.is_horizontal_mirror());
} }
}); });
} }

88
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();
}

17
src/core_test/helper/mirror.h

@ -1,3 +1,18 @@
#pragma once #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

Loading…
Cancel
Save