Browse Source

test: right value test for codec

master
Dnomd343 2 years ago
parent
commit
827c0b4900
  1. 5
      src/klotski_core/short_code/convert.cc
  2. 51
      test/codec/common_code.cc
  3. 27
      test/codec/raw_code.cc
  4. 41
      test/codec/short_code.cc

5
src/klotski_core/short_code/convert.cc

@ -1,6 +1,5 @@
#include <algorithm>
#include "common.h"
#include "all_cases.h"
#include "short_code.h"
#include "all_cases_offset.h"
#include "basic_ranges_offset.h"
@ -45,7 +44,9 @@ ShortCode ShortCode::from_common_code(CommonCode &&common_code) {
}
ShortCode ShortCode::from_common_code(std::string &&common_code) {
return ShortCode(std::forward<std::string>(common_code));
return ShortCode(std::forward<CommonCode>(
CommonCode(std::forward<std::string>(common_code))
));
}
ShortCode ShortCode::from_common_code(const CommonCode &common_code) {

51
test/codec/common_code.cc

@ -22,7 +22,7 @@ inline void SHOULD_PANIC(const std::function<void(void)> &func) {
EXPECT_EQ(panic_flag, true);
}
TEST(CommonCode, invalid) {
TEST(CommonCode, validity) {
EXPECT_NE(CommonCode::check(0x3'A9'BF'0C'00), true); // invalid 2x2 block
EXPECT_NE(CommonCode::check(0x1'D9'BF'0C'00), true); // invalid block range
EXPECT_NE(CommonCode::check(0x1'A9'BF'FC'00), true); // less than 2 space
@ -32,7 +32,7 @@ TEST(CommonCode, invalid) {
SHOULD_PANIC([](){ CommonCode::from_string("123J432A9"); }); // with invalid `J`
}
TEST(CommonCode, code_verify) {
TEST(CommonCode, code_verify) { // test all layout
std::thread threads[16];
auto test = [](uint64_t head) {
for (const auto &range : AllCases::fetch()[head]) {
@ -42,7 +42,7 @@ TEST(CommonCode, code_verify) {
EXPECT_EQ(tmp.valid(), true);
}
};
for (uint64_t head = 0; head < 16; ++head) {
for (uint64_t head = 0; head < 16; ++head) { // split into 16 threads
threads[head] = std::thread(test, head);
}
for (auto &t : threads) {
@ -50,7 +50,7 @@ TEST(CommonCode, code_verify) {
}
}
TEST(CommonCode, code_string) {
TEST(CommonCode, code_string) { // test all string code
std::thread threads[16];
auto test = [](uint64_t head) {
for (const auto &range : AllCases::fetch()[head]) {
@ -76,7 +76,7 @@ TEST(CommonCode, code_string) {
EXPECT_EQ(CommonCode::from_string(code_str), common_code); // test lower cases
}
};
for (uint64_t head = 0; head < 16; ++head) {
for (uint64_t head = 0; head < 16; ++head) { // split into 16 threads
threads[head] = std::thread(test, head);
}
for (auto &t : threads) {
@ -88,13 +88,15 @@ TEST(CommonCode, operators) {
std::cout.setstate(std::ios::failbit); // hide std::cout content
std::cout << "TEST OUTPUT -> " << CommonCode(TEST_CODE) << std::endl; // ostream test
std::cout.clear();
EXPECT_EQ(CommonCode(TEST_CODE), TEST_CODE); // operator `==`
EXPECT_EQ(CommonCode(TEST_CODE), CommonCode(TEST_CODE)); // operator `==`
EXPECT_EQ((uint64_t)CommonCode(TEST_CODE), TEST_CODE); // convert as uint64_t
}
TEST(CommonCode, code_convert) {
EXPECT_STREQ(CommonCode(TEST_CODE).to_string().c_str(), TEST_CODE_STR.c_str());
EXPECT_EQ(CommonCode(CommonCode(TEST_CODE).to_string()), CommonCode(TEST_CODE));
EXPECT_EQ(CommonCode(CommonCode(TEST_CODE).to_string(true)), CommonCode(TEST_CODE));
EXPECT_EQ(CommonCode(CommonCode(TEST_CODE).to_string(false)), CommonCode(TEST_CODE));
EXPECT_EQ(CommonCode(TEST_CODE).to_raw_code(), RawCode::from_common_code(TEST_CODE));
EXPECT_EQ(CommonCode(TEST_CODE).to_short_code(), ShortCode::from_common_code(TEST_CODE));
EXPECT_EQ(CommonCode(TEST_CODE).unwrap(), TEST_CODE);
@ -102,22 +104,45 @@ TEST(CommonCode, code_convert) {
TEST(CommonCode, constructors) {
EXPECT_EQ(CommonCode(TEST_CODE).unwrap(), TEST_CODE);
EXPECT_EQ(CommonCode(TEST_CODE_STR).unwrap(), TEST_CODE);
EXPECT_EQ(CommonCode(RawCode::from_common_code(TEST_CODE)).unwrap(), TEST_CODE);
EXPECT_EQ(CommonCode(ShortCode::from_common_code(TEST_CODE)).unwrap(), TEST_CODE);
EXPECT_EQ(CommonCode(TEST_CODE_STR).unwrap(), TEST_CODE); // l-value
EXPECT_EQ(CommonCode(std::string(TEST_CODE_STR)).unwrap(), TEST_CODE); // r-value
auto raw_code = RawCode::from_common_code(TEST_CODE);
EXPECT_EQ(CommonCode(raw_code).unwrap(), TEST_CODE); // l-value
EXPECT_EQ(CommonCode(RawCode::from_common_code(TEST_CODE)).unwrap(), TEST_CODE); // r-value
auto short_code = ShortCode::from_common_code(TEST_CODE);
EXPECT_EQ(CommonCode(short_code).unwrap(), TEST_CODE); // l-value
EXPECT_EQ(CommonCode(ShortCode::from_common_code(TEST_CODE)).unwrap(), TEST_CODE); // r-value
}
TEST(CommonCode, initializate) {
EXPECT_EQ(CommonCode::create(TEST_CODE).unwrap(), TEST_CODE);
EXPECT_EQ(CommonCode::from_string(TEST_CODE_STR).unwrap(), TEST_CODE);
EXPECT_EQ(CommonCode::unsafe_create(TEST_CODE).unwrap(), TEST_CODE);
EXPECT_EQ(CommonCode::from_string(TEST_CODE_STR).unwrap(), TEST_CODE); // l-value
EXPECT_EQ(CommonCode::from_string(std::string(TEST_CODE_STR)).unwrap(), TEST_CODE); // r-value
auto raw_code = RawCode::from_common_code(TEST_CODE);
EXPECT_EQ(CommonCode::from_raw_code(raw_code).unwrap(), TEST_CODE);
EXPECT_EQ(CommonCode::from_raw_code(raw_code.unwrap()).unwrap(), TEST_CODE);
EXPECT_EQ(CommonCode::from_raw_code(raw_code).unwrap(), TEST_CODE); // l-value
EXPECT_EQ(CommonCode::from_raw_code(
RawCode::from_common_code(TEST_CODE) // r-value
).unwrap(), TEST_CODE);
auto short_code = ShortCode::from_common_code(TEST_CODE);
EXPECT_EQ(CommonCode::from_short_code(short_code).unwrap(), TEST_CODE);
auto short_code_string = short_code.to_string();
EXPECT_EQ(CommonCode::from_short_code(short_code.unwrap()).unwrap(), TEST_CODE);
EXPECT_EQ(CommonCode::from_short_code(short_code.to_string()).unwrap(), TEST_CODE);
EXPECT_EQ(CommonCode::from_short_code(short_code).unwrap(), TEST_CODE); // l-value
EXPECT_EQ(CommonCode::from_short_code(
ShortCode::from_common_code(TEST_CODE) // r-value
).unwrap(), TEST_CODE);
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
}

27
test/codec/raw_code.cc

@ -9,12 +9,12 @@ using klotski::CommonCode;
const static uint64_t TEST_CODE = 0x0603'EDF5'CAFF'F5E2;
TEST(RawCode, invalid) {
EXPECT_NE(RawCode::check(0x0A34'182B'3810'2D21), true); // invalid code
EXPECT_NE(RawCode::check(0x8603'EDF5'CAFF'F5E2), true); // high 4-bits not zero
TEST(RawCode, validity) {
EXPECT_EQ(RawCode::check(0x0A34'182B'3810'2D21), false); // invalid code
EXPECT_EQ(RawCode::check(0x8603'EDF5'CAFF'F5E2), false); // high 4-bits not zero
}
TEST(RawCode, code_verify) {
TEST(RawCode, code_verify) { // test all layouts
std::thread threads[16];
auto test = [](uint64_t head) {
for (const auto &range : AllCases::fetch()[head]) {
@ -24,7 +24,7 @@ TEST(RawCode, code_verify) {
EXPECT_EQ(tmp.valid(), true);
}
};
for (uint64_t head = 0; head < 16; ++head) {
for (uint64_t head = 0; head < 16; ++head) { // split into 16 threads
threads[head] = std::thread(test, head);
}
for (auto &t : threads) {
@ -36,6 +36,7 @@ TEST(RawCode, operators) {
std::cout.setstate(std::ios::failbit); // hide std::cout content
std::cout << "TEST OUTPUT" << std::endl << RawCode(TEST_CODE); // ostream test
std::cout.clear();
EXPECT_EQ(RawCode(TEST_CODE), TEST_CODE); // operator `==`
EXPECT_EQ(RawCode(TEST_CODE), RawCode(TEST_CODE)); // operator `==`
EXPECT_EQ((uint64_t)RawCode(TEST_CODE), TEST_CODE); // convert as uint64_t
}
@ -47,7 +48,9 @@ TEST(RawCode, code_convert) {
TEST(RawCode, constructors) {
EXPECT_EQ(RawCode(TEST_CODE).unwrap(), TEST_CODE);
EXPECT_EQ(RawCode(CommonCode::from_raw_code(TEST_CODE)).unwrap(), TEST_CODE);
auto common_code = CommonCode::from_raw_code(TEST_CODE);
EXPECT_EQ(RawCode(common_code).unwrap(), TEST_CODE); // l-value
EXPECT_EQ(RawCode(CommonCode::from_raw_code(TEST_CODE)).unwrap(), TEST_CODE); // r-value
}
TEST(RawCode, initializate) {
@ -55,7 +58,15 @@ TEST(RawCode, initializate) {
EXPECT_EQ(RawCode::unsafe_create(TEST_CODE).unwrap(), TEST_CODE);
auto common_code = CommonCode::from_raw_code(TEST_CODE);
EXPECT_EQ(RawCode::from_common_code(common_code).unwrap(), TEST_CODE);
auto common_code_string = common_code.to_string();
EXPECT_EQ(RawCode::from_common_code(common_code.unwrap()).unwrap(), TEST_CODE);
EXPECT_EQ(RawCode::from_common_code(common_code.to_string()).unwrap(), TEST_CODE);
EXPECT_EQ(RawCode::from_common_code(common_code).unwrap(), TEST_CODE); // l-value
EXPECT_EQ(RawCode::from_common_code(
CommonCode::from_raw_code(TEST_CODE) // r-value
).unwrap(), TEST_CODE);
EXPECT_EQ(RawCode::from_common_code(common_code_string).unwrap(), TEST_CODE); // l-value
EXPECT_EQ(RawCode::from_common_code(common_code.to_string()).unwrap(), TEST_CODE); // r-value
}

41
test/codec/short_code.cc

@ -22,7 +22,7 @@ inline void SHOULD_PANIC(const std::function<void(void)> &func) {
EXPECT_EQ(panic_flag, true);
}
TEST(ShortCode, invalid) {
TEST(ShortCode, validity) {
EXPECT_NE(ShortCode::check(-1), true); // out of short code range
EXPECT_NE(ShortCode::check(29670987), true); // out of short code range
SHOULD_PANIC([](){ ShortCode::from_string("R50EH"); }); // with invalid `0`
@ -58,7 +58,7 @@ TEST(ShortCode, speed_up) {
EXPECT_EQ(AllCases::status(), AllCases::AVAILABLE);
}
TEST(ShortCode, code_verify) {
TEST(ShortCode, code_verify) { // test all layout
std::thread threads[16];
auto test = [](uint64_t head) {
for (const auto &range : AllCases::fetch()[head]) {
@ -68,15 +68,16 @@ TEST(ShortCode, code_verify) {
EXPECT_EQ(tmp.valid(), true);
}
};
for (uint64_t head = 0; head < 16; ++head) {
threads[head] = std::thread(test, head); // ensure that short code fast mode enabled
for (uint64_t head = 0; head < 16; ++head) { // split into 16 threads
/// NOTE: ensure that short code fast mode enabled
threads[head] = std::thread(test, head);
}
for (auto &t : threads) {
t.join();
}
}
TEST(ShortCode, code_string) {
TEST(ShortCode, code_string) { // test all string code
std::thread threads[16];
auto test = [](uint64_t head) {
for (const auto &range : AllCases::fetch()[head]) {
@ -96,8 +97,8 @@ TEST(ShortCode, code_string) {
EXPECT_EQ(ShortCode::from_string(code_str), short_code); // test lower cases
}
};
for (uint64_t head = 0; head < 16; ++head) {
threads[head] = std::thread(test, head); // ensure that short code fast mode enabled
for (uint64_t head = 0; head < 16; ++head) { // split into 16 threads
threads[head] = std::thread(test, head);
}
for (auto &t : threads) {
t.join();
@ -108,6 +109,7 @@ TEST(ShortCode, operators) {
std::cout.setstate(std::ios::failbit); // hide std::cout content
std::cout << "TEST OUTPUT -> " << ShortCode(TEST_CODE) << std::endl; // ostream test
std::cout.clear();
EXPECT_EQ(ShortCode(TEST_CODE), TEST_CODE); // operator `==`
EXPECT_EQ(ShortCode(TEST_CODE), ShortCode(TEST_CODE)); // operator `==`
EXPECT_EQ((uint32_t)ShortCode(TEST_CODE), TEST_CODE); // convert as uint64_t
}
@ -121,17 +123,32 @@ TEST(ShortCode, code_convert) {
TEST(ShortCode, constructors) {
EXPECT_EQ(ShortCode(TEST_CODE).unwrap(), TEST_CODE);
EXPECT_EQ(ShortCode(TEST_CODE_STR).unwrap(), TEST_CODE);
EXPECT_EQ(ShortCode(CommonCode::from_short_code(TEST_CODE)).unwrap(), TEST_CODE);
EXPECT_EQ(ShortCode(TEST_CODE_STR).unwrap(), TEST_CODE); // l-value
EXPECT_EQ(ShortCode(std::string(TEST_CODE_STR)).unwrap(), TEST_CODE); // r-value
auto common_code = CommonCode::from_short_code(TEST_CODE);
EXPECT_EQ(ShortCode(common_code).unwrap(), TEST_CODE); // l-value
EXPECT_EQ(ShortCode(CommonCode::from_short_code(TEST_CODE)).unwrap(), TEST_CODE); // r-value
}
TEST(ShortCode, initializate) {
EXPECT_EQ(ShortCode::create(TEST_CODE).unwrap(), TEST_CODE);
EXPECT_EQ(ShortCode::from_string(TEST_CODE_STR).unwrap(), TEST_CODE);
EXPECT_EQ(ShortCode::unsafe_create(TEST_CODE).unwrap(), TEST_CODE);
EXPECT_EQ(ShortCode::from_string(TEST_CODE_STR).unwrap(), TEST_CODE); // l-value
EXPECT_EQ(ShortCode::from_string(std::string(TEST_CODE_STR)).unwrap(), TEST_CODE); // r-value
auto common_code = CommonCode::from_short_code(TEST_CODE);
EXPECT_EQ(ShortCode::from_common_code(common_code).unwrap(), TEST_CODE);
auto common_code_string = common_code.to_string(false);
EXPECT_EQ(ShortCode::from_common_code(common_code.unwrap()).unwrap(), TEST_CODE);
EXPECT_EQ(ShortCode::from_common_code(common_code.to_string()).unwrap(), TEST_CODE);
EXPECT_EQ(ShortCode::from_common_code(common_code).unwrap(), TEST_CODE); // l-value
EXPECT_EQ(ShortCode::from_common_code(
CommonCode::from_short_code(TEST_CODE) // r-value
).unwrap(), TEST_CODE);
EXPECT_EQ(ShortCode::from_common_code(common_code_string).unwrap(), TEST_CODE); // l-value
EXPECT_EQ(ShortCode::from_common_code(common_code.to_string()).unwrap(), TEST_CODE); // r-value
}

Loading…
Cancel
Save