Browse Source

test: add global test in codec test suites

master
Dnomd343 2 years ago
parent
commit
f9c3025697
  1. 37
      test/codec/common_code.cc
  2. 18
      test/codec/range_split.h
  3. 67
      test/codec/raw_code.cc
  4. 36
      test/codec/short_code.cc

37
test/codec/common_code.cc

@ -1,9 +1,12 @@
#include <future>
#include <thread>
#include <algorithm>
#include <unordered_set>
#include "all_cases.h"
#include "tiny_pool.h"
#include "common_code.h"
#include "gtest/gtest.h"
#include "range_split.h"
#define SHOULD_PANIC(FUNC) \
try { \
@ -15,6 +18,7 @@ using klotski::AllCases;
using klotski::ShortCode;
using klotski::CommonCode;
using klotski::ALL_CASES_SIZE;
using klotski::ALL_CASES_SIZE_SUM;
const static uint64_t TEST_CODE = 0x1'A9BF'0C00;
const static std::string TEST_CODE_STR = "1A9BF0C00";
@ -182,3 +186,36 @@ TEST(CommonCode, initializate) {
EXPECT_EQ(CommonCode::from_short_code(short_code_string).unwrap(), TEST_CODE); // l-value
EXPECT_EQ(CommonCode::from_short_code(short_code.to_string()).unwrap(), TEST_CODE); // r-value
}
TEST(CommonCode, DISABLED_global) {
auto search = [](uint64_t start, uint64_t end) -> std::vector<uint64_t> {
std::vector<uint64_t> archive;
for (uint64_t common_code = start; common_code < end; ++common_code) {
if (CommonCode::check(common_code)) {
archive.emplace_back(common_code); // valid common code
}
}
return archive;
};
auto pool = TinyPool();
std::vector<std::future<std::vector<uint64_t>>> tasks;
for (const auto &range : range_split(0, 0x10'0000'0000, 0x10'0000)) {
tasks.emplace_back(
pool.submit(search, range.first, range.second)
);
}
pool.boot(); // running tasks
std::vector<uint64_t> result;
for (auto &tmp : tasks) {
auto ret = tmp.get(); // release data
result.insert(result.end(), ret.begin(), ret.end());
}
pool.join();
auto all_cases = AllCases::release();
for (uint32_t i = 0; i < ALL_CASES_SIZE_SUM; ++i) {
EXPECT_EQ(all_cases[i], result[i]);
}
}

18
test/codec/range_split.h

@ -0,0 +1,18 @@
#pragma once
#include <vector>
#include <cstdint>
typedef std::vector<std::pair<uint64_t, uint64_t>> range_split_t;
inline range_split_t range_split(uint64_t start, uint64_t end, uint64_t size) {
range_split_t ranges;
uint64_t rear = end - (end - start) % size; // (rear - start) % size == 0
for (uint64_t i = start; i < rear; i += size) {
ranges.emplace_back(i, i + size); // ranges with same length
}
if (rear != end) {
ranges.emplace_back(rear, end); // last range
}
return ranges;
}

67
test/codec/raw_code.cc

@ -1,8 +1,12 @@
#include <future>
#include <thread>
#include <unordered_set>
#include "common.h"
#include "raw_code.h"
#include "all_cases.h"
#include "tiny_pool.h"
#include "gtest/gtest.h"
#include "range_split.h"
#define SHOULD_PANIC(FUNC) \
try { \
@ -13,6 +17,8 @@ using klotski::RawCode;
using klotski::AllCases;
using klotski::CommonCode;
using klotski::ALL_CASES_SIZE;
using klotski::ALL_CASES_SIZE_SUM;
using klotski::Common::range_reverse;
const static uint64_t TEST_CODE = 0x0603'EDF5'CAFF'F5E2;
@ -201,3 +207,64 @@ TEST(RawCode, horizontal_mirror_global) {
}
for (auto &t : threads) { t.join(); }
}
/// NOTE: for RawCode global test
uint64_t raw_code_convert(uint64_t common_code) { // try to convert as raw code
auto code = C_2x2 << (common_code >> 32) * 3;
auto range = range_reverse((uint32_t)common_code);
for (int addr = 0; range; range >>= 2) {
while ((code >> addr) & 0b111 && addr < 60) {
addr += 3;
}
if (addr >= 60) {
return 0;
}
switch (range & 0b11) { // match low 2-bits
case 0b01: // 1x2 block
code |= C_1x2 << addr;
break;
case 0b10: // 2x1 block
code |= C_2x1 << addr;
break;
case 0b11: // 1x1 block
code |= C_1x1 << addr;
break;
case 0b00: // space
addr += 3;
}
}
return code;
}
TEST(RawCode, DISABLED_global) {
auto search = [](uint64_t start, uint64_t end) -> std::vector<uint64_t> {
std::vector<uint64_t> archive;
for (uint64_t common_code = start; common_code < end; ++common_code) {
if (RawCode::check(raw_code_convert(common_code))) {
archive.emplace_back(common_code); // valid layout
}
}
return archive;
};
auto pool = TinyPool();
std::vector<std::future<std::vector<uint64_t>>> tasks;
for (const auto &range : range_split(0, 0x10'0000'0000, 0x10'0000)) {
tasks.emplace_back(
pool.submit(search, range.first, range.second)
);
}
pool.boot(); // running tasks
std::vector<uint64_t> result;
for (auto &tmp : tasks) {
auto ret = tmp.get(); // release data
result.insert(result.end(), ret.begin(), ret.end());
}
pool.join();
auto all_cases = AllCases::release();
for (uint32_t i = 0; i < ALL_CASES_SIZE_SUM; ++i) {
EXPECT_EQ(all_cases[i], result[i]);
}
}

36
test/codec/short_code.cc

@ -1,9 +1,12 @@
#include <future>
#include <thread>
#include <algorithm>
#include <unordered_set>
#include "all_cases.h"
#include "tiny_pool.h"
#include "short_code.h"
#include "gtest/gtest.h"
#include "range_split.h"
#define SHOULD_PANIC(FUNC) \
try { \
@ -69,6 +72,39 @@ TEST(ShortCode, speed_up) {
EXPECT_EQ(AllCases::status(), AllCases::AVAILABLE);
}
TEST(ShortCode, DISABLED_global) {
auto check = [](uint32_t start, uint32_t end) -> std::vector<uint64_t> {
std::vector<uint64_t> archive;
for (uint32_t short_code = start; short_code < end; ++short_code) {
auto common_code = ShortCode::unsafe_create(short_code).to_common_code();
archive.emplace_back(common_code.unwrap());
EXPECT_EQ(common_code.to_short_code().unwrap(), short_code);
}
return archive;
};
auto pool = TinyPool();
std::vector<std::future<std::vector<uint64_t>>> tasks;
for (const auto &range : range_split(0, klotski::ALL_CASES_SIZE_SUM, 10000)) {
tasks.emplace_back(
pool.submit(check, range.first, range.second)
);
}
pool.boot(); // running tasks
std::vector<uint64_t> result;
for (auto &tmp : tasks) {
auto ret = tmp.get(); // release data
result.insert(result.end(), ret.begin(), ret.end());
}
pool.join();
auto all_cases = AllCases::release();
for (uint32_t i = 0; i < ALL_CASES_SIZE_SUM; ++i) {
EXPECT_EQ(all_cases[i], result[i]);
}
}
TEST(ShortCode, code_verify) { // test all layout
auto test = [](uint64_t head) {
for (const auto &range : AllCases::fetch()[head]) {

Loading…
Cancel
Save