Browse Source

test: add type_id and block_num test

legacy
Dnomd343 2 years ago
parent
commit
989e4fd581
  1. 6
      src/klotski_core/ffi/tmain.cc
  2. 33
      src/klotski_core/group/group.cc
  3. 6
      src/klotski_core/group/group.h
  4. 14
      test/CMakeLists.txt
  5. 73
      test/group/group.cc

6
src/klotski_core/ffi/tmain.cc

@ -23,11 +23,11 @@ using klotski::Benchmark;
void tmain() {
auto r = Group::block_num(123);
// auto r = Group::block_num(123);
std::cout << (int)r.n_1x1 << " " << (int)r.n_1x2 << " " << (int)r.n_2x1 << std::endl;
// std::cout << (int)r.n_1x1 << " " << (int)r.n_1x2 << " " << (int)r.n_2x1 << std::endl;
std::cout << Group::type_id(r) << std::endl;
// std::cout << Group::type_id(r) << std::endl;
// Group::block_num(CommonCode(0x1A9BF0C00));
// Group::block_num(RawCode::from_common_code(0x1A9BF0C00));

33
src/klotski_core/group/group.cc

@ -11,18 +11,24 @@ namespace klotski {
using Common::range_reverse;
uint32_t Group::type_id(const RawCode &raw_code) {
return type_id(block_num(raw_code));
}
uint32_t Group::type_id(const CommonCode &common_code) {
return type_id(block_num(common_code));
}
uint32_t Group::type_id(const block_num_t &block_num) {
auto n_x2x = block_num.n_1x2 + block_num.n_2x1;
auto flag = (n_x2x << 8) | (block_num.n_2x1 << 4) | block_num.n_1x1;
return std::lower_bound(TYPE_ID_INDEX, TYPE_ID_INDEX + 204, flag) - TYPE_ID_INDEX;
}
Group::block_num_t Group::block_num(uint32_t type_id) {
auto flag = TYPE_ID_INDEX[type_id];
uint8_t n_x2x = flag >> 8;
uint8_t n_2x1 = (flag >> 4) & 0b1111;
// std::cout << (int)n_x2x << std::endl;
// std::cout << (int)n_2x1 << std::endl;
// std::cout << flag << std::endl;
// 1046 -> 0100 0001 0110
// 1024+0016+0006
// 4 1 6
return block_num_t {
.n_1x1 = static_cast<uint8_t>(flag & 0b1111),
.n_1x2 = static_cast<uint8_t>(n_x2x - n_2x1),
@ -99,18 +105,7 @@ std::vector<RawCode> Group::group_cases(const RawCode &seed) {
return result;
}
uint32_t Group::type_id(const block_num_t &block_num) {
auto n_x2x = block_num.n_1x2 + block_num.n_2x1;
auto flag = (n_x2x << 8) | (block_num.n_2x1 << 4) | block_num.n_1x1;
std::cout << flag << std::endl;
// std::cout << << std::endl;
// search in TYPE_ID_INDEX
return std::lower_bound(TYPE_ID_INDEX, TYPE_ID_INDEX + 204, flag) - TYPE_ID_INDEX;
}
} // namespace klotski

6
src/klotski_core/group/group.h

@ -6,6 +6,8 @@
namespace klotski {
const uint32_t TYPE_ID_LIMIT = 204;
class Group {
public:
@ -15,9 +17,9 @@ public:
uint8_t n_2x1 = 0;
};
// static uint32_t type_id(const RawCode &raw_code);
static uint32_t type_id(const RawCode &raw_code);
static uint32_t type_id(const block_num_t &block_num);
// static uint32_t type_id(const CommonCode &common_code);
static uint32_t type_id(const CommonCode &common_code);
static block_num_t block_num(uint32_t type_id);
static block_num_t block_num(const RawCode &raw_code);

14
test/CMakeLists.txt

@ -56,6 +56,20 @@ add_test(NAME core COMMAND test_core)
################################################################################
#include_directories(../src/klotski_core/core)
#include_directories(../src/klotski_core/raw_code)
#include_directories(../src/klotski_core/all_cases)
#include_directories(../src/klotski_core/common_code)
include_directories(../src/klotski_core/group)
set(TEST_GROUP_SRC group.cc)
list(TRANSFORM TEST_GROUP_SRC PREPEND "group/")
add_executable(test_group ${TEST_GROUP_SRC})
target_link_libraries(test_group ${TEST_DEPS} md5 absl::flat_hash_map)
add_test(NAME group COMMAND test_group)
################################################################################
include_directories(../src/klotski_core)
include_directories(../src/klotski_core/utils)

73
test/group/group.cc

@ -0,0 +1,73 @@
#include <thread>
#include "md5.h"
#include "group.h"
#include "gtest/gtest.h"
using klotski::Group;
using klotski::AllCases;
using klotski::CommonCode;
using klotski::TYPE_ID_LIMIT;
using klotski::ALL_CASES_SIZE_SUM;
const char BLOCK_NUM_MD5[] = "46a7b3af6d039cbe2f7eaebdd196c6a2";
TEST(Group, block_num) {
std::thread threads[16];
std::string block_num_datas[16];
auto test = [&block_num_datas](uint64_t head) {
char buffer[13];
for (auto &&range: AllCases::fetch()[head]) {
auto common_code = CommonCode::unsafe_create(head << 32 | range);
auto tmp = Group::block_num(common_code);
EXPECT_EQ(tmp, Group::block_num(common_code.to_raw_code()));
sprintf(buffer, "%d,%d,%d\n", tmp.n_1x2 + tmp.n_2x1, tmp.n_1x1, tmp.n_2x1);
block_num_datas[head] += buffer;
}
};
for (uint64_t head = 0; head < 16; ++head) { // split into 16 threads
threads[head] = std::thread(test, head);
}
for (auto &t : threads) {
t.join();
}
std::string block_num_data;
for (auto &&tmp : block_num_datas) {
block_num_data += tmp;
}
auto block_num_md5 = md5(block_num_data.c_str(), block_num_data.size());
EXPECT_STREQ(block_num_md5.c_str(), BLOCK_NUM_MD5);
// auto *block_num_data = new char[ALL_CASES_SIZE_SUM * 7 + 1]; // 'x,?x,x' + '\n'
// auto *current = block_num_data;
// for (auto &&common_code : AllCases::release()) {
// auto tmp = Group::block_num(common_code);
// EXPECT_EQ(tmp, Group::block_num(common_code.to_raw_code()));
// sprintf(current, "%d,%d,%d\n", tmp.n_1x2 + tmp.n_2x1, tmp.n_1x1, tmp.n_2x1);
// current += strlen(current);
// }
// auto block_num_md5 = md5(block_num_data, current - block_num_data);
// EXPECT_STREQ(block_num_md5.c_str(), BLOCK_NUM_MD5);
}
TEST(Group, type_id) {
std::thread threads[16];
auto test = [](uint64_t head) {
for (const auto &range : AllCases::fetch()[head]) {
auto common_code = CommonCode::unsafe_create(head << 32 | range);
auto type_id = Group::type_id(common_code); // found type id
EXPECT_LT(type_id, TYPE_ID_LIMIT);
EXPECT_EQ(type_id, Group::type_id(common_code.to_raw_code()));
EXPECT_EQ(type_id, Group::type_id(Group::block_num(common_code)));
EXPECT_EQ(Group::block_num(type_id), Group::block_num(common_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