mirror of https://github.com/dnomd343/klotski.git
Dnomd343
2 years ago
5 changed files with 3 additions and 207 deletions
@ -1,46 +0,0 @@ |
|||
#include "all_cases.h" |
|||
#include "common_code.h" |
|||
#include "gtest/gtest.h" |
|||
#include "global_utils.h" |
|||
|
|||
using klotski::AllCases; |
|||
using klotski::CommonCode; |
|||
|
|||
std::vector<uint64_t> common_code_search(uint64_t start, uint64_t end) { |
|||
std::vector<uint64_t> ret; |
|||
for (uint64_t common_code = start; common_code < end; ++common_code) { |
|||
if (CommonCode::check(common_code)) { |
|||
ret.emplace_back(common_code); // valid common code
|
|||
} |
|||
} |
|||
return ret; |
|||
} |
|||
|
|||
TEST(GLOBAL, common_code) { |
|||
/// create common code check tasks
|
|||
auto pool = TinyPool(); |
|||
std::vector<std::future<std::vector<uint64_t>>> futures; |
|||
for (const auto &range : range_split(0, 0x10'0000'0000, 0x10'0000)) { |
|||
futures.emplace_back( |
|||
pool.submit(common_code_search, range.first, range.second) |
|||
); |
|||
} |
|||
|
|||
/// run common code search
|
|||
pool.boot(); |
|||
std::vector<uint64_t> result; |
|||
for (auto &f : futures) { |
|||
auto ret = f.get(); |
|||
result.insert(result.end(), ret.begin(), ret.end()); |
|||
} |
|||
pool.join(); |
|||
|
|||
/// verify check result
|
|||
std::vector<uint64_t> all_cases; |
|||
for (uint64_t head = 0; head < 16; ++head) { |
|||
for (const auto &range : AllCases::fetch()[head]) { |
|||
all_cases.emplace_back(head << 32 | range); |
|||
} |
|||
} |
|||
EXPECT_EQ(result, all_cases); |
|||
} |
@ -1,18 +0,0 @@ |
|||
#pragma once |
|||
|
|||
#include <vector> |
|||
#include "tiny_pool.h" |
|||
|
|||
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) { |
|||
uint64_t rear = end - (end - start) % size; // (rear - start) % size == 0
|
|||
range_split_t ranges; |
|||
for (uint64_t i = start; i < rear; i += size) { |
|||
ranges.emplace_back(i, i + size); |
|||
} |
|||
if (rear != end) { |
|||
ranges.emplace_back(rear, end); |
|||
} |
|||
return ranges; |
|||
} |
@ -1,75 +0,0 @@ |
|||
#include "common.h" |
|||
#include "raw_code.h" |
|||
#include "gtest/gtest.h" |
|||
#include "global_utils.h" |
|||
|
|||
using klotski::RawCode; |
|||
using klotski::AllCases; |
|||
using klotski::CommonCode; |
|||
using klotski::Common::range_reverse; |
|||
|
|||
uint64_t 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; |
|||
} |
|||
|
|||
std::vector<uint64_t> raw_code_search(uint64_t start, uint64_t end) { |
|||
std::vector<uint64_t> ret; |
|||
for (uint64_t common_code = start; common_code < end; ++common_code) { |
|||
if (RawCode::check(convert(common_code))) { |
|||
ret.emplace_back(common_code); // valid layout
|
|||
} |
|||
} |
|||
return ret; |
|||
} |
|||
|
|||
TEST(GLOBAL, raw_code) { |
|||
/// create raw code check tasks
|
|||
auto pool = TinyPool(); |
|||
std::vector<std::future<std::vector<uint64_t>>> futures; |
|||
for (const auto &range : range_split(0, 0x10'0000'0000, 0x10'0000)) { |
|||
futures.emplace_back( |
|||
pool.submit(raw_code_search, range.first, range.second) |
|||
); |
|||
} |
|||
|
|||
/// run raw code search
|
|||
pool.boot(); |
|||
std::vector<uint64_t> result; |
|||
for (auto &f : futures) { |
|||
auto ret = f.get(); |
|||
result.insert(result.end(), ret.begin(), ret.end()); |
|||
} |
|||
pool.join(); |
|||
|
|||
/// verify check result
|
|||
std::vector<uint64_t> all_cases; |
|||
for (uint64_t head = 0; head < 16; ++head) { |
|||
for (const auto &range : AllCases::fetch()[head]) { |
|||
all_cases.emplace_back(head << 32 | range); |
|||
} |
|||
} |
|||
EXPECT_EQ(result, all_cases); |
|||
} |
@ -1,55 +0,0 @@ |
|||
#include "all_cases.h" |
|||
#include "short_code.h" |
|||
#include "gtest/gtest.h" |
|||
#include "global_utils.h" |
|||
|
|||
using klotski::AllCases; |
|||
using klotski::ShortCode; |
|||
using klotski::CommonCode; |
|||
|
|||
std::vector<uint64_t> short_code_check(uint32_t start, uint32_t end) { |
|||
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; |
|||
} |
|||
|
|||
void short_code_verify() { |
|||
/// create short code check tasks
|
|||
auto pool = TinyPool(); |
|||
std::vector<std::future<std::vector<uint64_t>>> futures; |
|||
for (const auto &range : range_split(0, klotski::ALL_CASES_SIZE_SUM, 10000)) { |
|||
futures.emplace_back( |
|||
pool.submit(short_code_check, range.first, range.second) |
|||
); |
|||
} |
|||
|
|||
/// run short code search
|
|||
pool.boot(); |
|||
std::vector<uint64_t> result; |
|||
for (auto &f : futures) { |
|||
auto ret = f.get(); |
|||
result.insert(result.end(), ret.begin(), ret.end()); |
|||
} |
|||
pool.join(); |
|||
|
|||
/// verify check result
|
|||
std::vector<uint64_t> all_cases; |
|||
for (uint64_t head = 0; head < 16; ++head) { |
|||
for (const auto &range : AllCases::fetch()[head]) { |
|||
all_cases.emplace_back(head << 32 | range); |
|||
} |
|||
} |
|||
EXPECT_EQ(result, all_cases); |
|||
} |
|||
|
|||
TEST(GLOBAL, short_code) { |
|||
ShortCode::speed_up(ShortCode::NORMAL); // normal mode
|
|||
short_code_verify(); |
|||
|
|||
ShortCode::speed_up(ShortCode::FAST); // fast mode
|
|||
short_code_verify(); |
|||
} |
Loading…
Reference in new issue