diff --git a/src/core_test/codec/common_code.cc b/src/core_test/codec/common_code.cc index af3fe0f..8c4e9a5 100644 --- a/src/core_test/codec/common_code.cc +++ b/src/core_test/codec/common_code.cc @@ -25,15 +25,27 @@ TEST(CommonCode, validity) { EXPECT_FALSE(CommonCode::create(0x123456789).has_value()); // invalid code EXPECT_FALSE(CommonCode::from_string("0123456789").has_value()); // length > 9 EXPECT_FALSE(CommonCode::from_string("123J432A9").has_value()); // with invalid `J` + + EXPECT_TRUE(CommonCode::unsafe_create(0x1'A9BF0C00).is_horizontal_mirror()); + EXPECT_FALSE(CommonCode::unsafe_create(0x4'FEA13400).is_horizontal_mirror()); + EXPECT_FALSE(CommonCode::unsafe_create(0x1'A9BF0C00).is_vertical_mirror()); + EXPECT_FALSE(CommonCode::unsafe_create(0x4'FEA13400).is_vertical_mirror()); + + EXPECT_EQ(CommonCode::unsafe_create(0x1'A9BF0C00).to_horizontal_mirror(), 0x1'A9BF0C00); + EXPECT_EQ(CommonCode::unsafe_create(0x4'FEA13400).to_horizontal_mirror(), 0x6'BFA47000); + EXPECT_EQ(CommonCode::unsafe_create(0x1'A9BF0C00).to_vertical_mirror(), 0xD'C3BE6800); + EXPECT_EQ(CommonCode::unsafe_create(0x4'FEA13400).to_vertical_mirror(), 0x8'346AFC00); + +#ifndef KLSK_NDEBUG + std::ostringstream out; + out << CommonCode::unsafe_create(TEST_C_CODE); // ostream capture + EXPECT_EQ(out.str(), TEST_C_CODE_STR); +#endif } TEST(CommonCode, operators) { auto common_code = CommonCode::unsafe_create(TEST_C_CODE); - - std::ostringstream tmp; - tmp << common_code; // ostream capture - EXPECT_EQ(tmp.str(), TEST_C_CODE_STR); - EXPECT_EQ((uint64_t)common_code, TEST_C_CODE); // convert as uint64_t + EXPECT_EQ(static_cast(common_code), TEST_C_CODE); // uint64_t cast EXPECT_NE(0, common_code); // uint64_t != CommonCode EXPECT_NE(common_code, 0); // CommonCode != uint64_t @@ -87,10 +99,10 @@ TEST(CommonCode, initializate) { auto short_code = ShortCode::unsafe_create(TEST_S_CODE); auto common_code = CommonCode::unsafe_create(TEST_C_CODE); - // operator== - CommonCode c1 = common_code; + // operator= + auto c1 = common_code; + auto c2 = CommonCode {common_code}; EXPECT_EQ(c1, TEST_C_CODE); // l-value - CommonCode c2 = CommonCode {common_code}; EXPECT_EQ(c2, TEST_C_CODE); // r-value // CommonCode(...) @@ -135,6 +147,11 @@ TEST(CommonCode, initializate) { EXPECT_EQ(CommonCode::from_short_code(TEST_S_CODE_STR), TEST_C_CODE); } +// TODO: global test function +// -> check +// -> string_decode / string_encode / string_encode_shorten +// -> check_mirror / get_vertical_mirror / get_horizontal_mirror + TEST(CommonCode, code_verify) { BS::thread_pool pool; pool.detach_sequence(0, 16, [](const uint64_t head) { diff --git a/src/core_test/codec/raw_code.cc b/src/core_test/codec/raw_code.cc index 836ec9e..82e5f02 100644 --- a/src/core_test/codec/raw_code.cc +++ b/src/core_test/codec/raw_code.cc @@ -21,23 +21,51 @@ TEST(RawCode, validity) { EXPECT_FALSE(RawCode::check(0x8603'EDF5'CAFF'F5E2)); // high 4-bits not zero EXPECT_FALSE(RawCode::create(0x0000'0000'0000'0000).has_value()); // invalid code + + // TODO: add mirror test + +#ifndef KLSK_NDEBUG + std::ostringstream out; + out << RawCode::unsafe_create(TEST_R_CODE); // ostream capture + EXPECT_TRUE(out.str().starts_with("603EDF5CAFFF5E2\n")); // TODO: using full string +#endif } TEST(RawCode, operators) { auto raw_code = RawCode::unsafe_create(TEST_R_CODE); + EXPECT_EQ((uint64_t)raw_code, TEST_R_CODE); // uint64_t cast - std::ostringstream tmp; - tmp << raw_code; // ostream capture - EXPECT_TRUE(tmp.str().starts_with("603EDF5CAFFF5E2\n")); - EXPECT_EQ((uint64_t)raw_code, TEST_R_CODE); // convert as uint64_t - + EXPECT_NE(0, raw_code); // uint64_t != RawCode + EXPECT_NE(raw_code, 0); // RawCode != uint64_t EXPECT_EQ(TEST_R_CODE, raw_code); // uint64_t == RawCode EXPECT_EQ(raw_code, TEST_R_CODE); // RawCode == uint64_t + + EXPECT_LE(TEST_R_CODE, raw_code); // uint64_t <= RawCode + EXPECT_LE(TEST_R_CODE - 1, raw_code); + EXPECT_LT(TEST_R_CODE - 1, raw_code); // uint64_t < RawCode + + EXPECT_LE(raw_code, TEST_R_CODE); // RawCode <= uint64_t + EXPECT_LE(raw_code, TEST_R_CODE + 1); + EXPECT_LT(raw_code, TEST_R_CODE + 1); // RawCode < uint64_t + + EXPECT_GE(TEST_R_CODE, raw_code); // uint64_t >= RawCode + EXPECT_GE(TEST_R_CODE + 1, raw_code); + EXPECT_GT(TEST_R_CODE + 1, raw_code); // uint64_t > RawCode + + EXPECT_GE(raw_code, TEST_R_CODE); // RawCode >= uint64_t + EXPECT_GE(raw_code, TEST_R_CODE - 1); + EXPECT_GT(raw_code, TEST_R_CODE - 1); // RawCode > uint64_t + EXPECT_EQ(raw_code, raw_code); // RawCode == RawCode + EXPECT_NE(raw_code, RawCode::unsafe_create(0)); // RawCode != RawCode + + EXPECT_LE(raw_code, raw_code); // RawCode <= RawCode + EXPECT_LE(raw_code, RawCode::unsafe_create(TEST_R_CODE + 1)); + EXPECT_LT(raw_code, RawCode::unsafe_create(TEST_R_CODE + 1)); // RawCode < RawCode - EXPECT_NE(TEST_R_CODE + 1, raw_code); // uint64_t != RawCode - EXPECT_NE(raw_code, TEST_R_CODE + 1); // RawCode != uint64_t - EXPECT_NE(raw_code, RawCode::unsafe_create(TEST_R_CODE + 1)); // RawCode != RawCode + EXPECT_GE(raw_code, raw_code); // RawCode >= RawCode + EXPECT_GE(RawCode::unsafe_create(TEST_R_CODE + 1), raw_code); + EXPECT_GT(RawCode::unsafe_create(TEST_R_CODE + 1), raw_code); // RawCode > RawCode } TEST(RawCode, exporter) { @@ -50,6 +78,12 @@ TEST(RawCode, initializate) { auto raw_code = RawCode::unsafe_create(TEST_R_CODE); auto common_code = CommonCode::unsafe_create(TEST_C_CODE); + // operator= + auto r1 = raw_code; + auto r2 = RawCode {raw_code}; + EXPECT_EQ(r1, TEST_R_CODE); // l-value + EXPECT_EQ(r2, TEST_R_CODE); // r-value + // RawCode(...) EXPECT_EQ(RawCode(common_code), TEST_R_CODE); EXPECT_EQ(RawCode(raw_code), TEST_R_CODE); // l-value @@ -62,6 +96,7 @@ TEST(RawCode, initializate) { // RawCode::unsafe_create(uint64_t) EXPECT_EQ(RawCode::unsafe_create(TEST_R_CODE), TEST_R_CODE); + EXPECT_EQ(RawCode::unsafe_create(TEST_R_CODE_ERR), TEST_R_CODE_ERR); // RawCode::from_common_code(CommonCode) EXPECT_EQ(RawCode::from_common_code(common_code), TEST_R_CODE); @@ -71,17 +106,17 @@ TEST(RawCode, initializate) { EXPECT_FALSE(RawCode::from_common_code(TEST_C_CODE_ERR).has_value()); EXPECT_EQ(RawCode::from_common_code(TEST_C_CODE), TEST_R_CODE); - // RawCode::from_common_code(const std::string &) + // RawCode::from_common_code(std::string_view) EXPECT_TRUE(RawCode::from_common_code(TEST_C_CODE_STR).has_value()); EXPECT_FALSE(RawCode::from_common_code(TEST_C_CODE_STR_ERR).has_value()); EXPECT_EQ(RawCode::from_common_code(TEST_C_CODE_STR), TEST_R_CODE); - - // RawCode::from_common_code(std::string &&) - EXPECT_TRUE(RawCode::from_common_code(TEST_C_CODE_STR_RV).has_value()); - EXPECT_FALSE(RawCode::from_common_code(TEST_C_CODE_STR_ERR_RV).has_value()); - EXPECT_EQ(RawCode::from_common_code(TEST_C_CODE_STR_RV), TEST_R_CODE); } +// TODO: global check function: +// -> check +// -> compact / extract +// -> check_mirror / get_vertical_mirror / get_horizontal_mirror + TEST(RawCode, code_verify) { BS::thread_pool pool; pool.detach_sequence(0, 16, [](const uint64_t head) { diff --git a/src/core_test/codec/short_code.cc b/src/core_test/codec/short_code.cc index a993f83..203b386 100644 --- a/src/core_test/codec/short_code.cc +++ b/src/core_test/codec/short_code.cc @@ -48,25 +48,48 @@ TEST(ShortCode, validity) { EXPECT_FALSE(ShortCode::from_string("R50EH").has_value()); // with invalid `0` EXPECT_FALSE(ShortCode::from_string("123456").has_value()); // length != 5 EXPECT_FALSE(ShortCode::from_string("Z9EFV").has_value()); // out of short code range + +#ifndef KLSK_NDEBUG + std::ostringstream out; + out << ShortCode::unsafe_create(TEST_S_CODE); // ostream capture + EXPECT_EQ(out.str(), TEST_S_CODE_STR); +#endif } TEST(ShortCode, operators) { auto short_code = ShortCode::unsafe_create(TEST_S_CODE); + EXPECT_EQ(static_cast(short_code), TEST_S_CODE); // uint32_t cast - std::ostringstream tmp; - tmp << short_code; // ostream capture - EXPECT_EQ(tmp.str(), TEST_S_CODE_STR); - EXPECT_EQ((uint32_t)short_code, TEST_S_CODE); // convert as uint32_t - + EXPECT_NE(0, short_code); // uint32_t != ShortCode + EXPECT_NE(short_code, 0); // ShortCode != uint32_t EXPECT_EQ(TEST_S_CODE, short_code); // uint32_t == ShortCode EXPECT_EQ(short_code, TEST_S_CODE); // ShortCode == uint32_t - EXPECT_EQ(short_code, short_code); // ShortCode == ShortCode - EXPECT_NE(TEST_S_CODE + 1, short_code); // uint32_t != ShortCode - EXPECT_NE(short_code, TEST_S_CODE + 1); // ShortCode != uint32_t - EXPECT_NE(short_code, ShortCode::unsafe_create(TEST_S_CODE + 1)); // ShortCode != ShortCode + EXPECT_LE(TEST_S_CODE, short_code); // uint32_t <= ShortCode + EXPECT_LE(TEST_S_CODE - 1, short_code); + EXPECT_LT(TEST_S_CODE - 1, short_code); // uint32_t < ShortCode + + EXPECT_LE(short_code, TEST_S_CODE); // ShortCode <= uint32_t + EXPECT_LE(short_code, TEST_S_CODE + 1); + EXPECT_LT(short_code, TEST_S_CODE + 1); // ShortCode < uint32_t + + EXPECT_GE(TEST_S_CODE, short_code); // uint32_t >= ShortCode + EXPECT_GE(TEST_S_CODE + 1, short_code); + EXPECT_GT(TEST_S_CODE + 1, short_code); // uint32_t > ShortCode + + EXPECT_GE(short_code, TEST_S_CODE); // ShortCode >= uint32_t + EXPECT_GE(short_code, TEST_S_CODE - 1); + EXPECT_GT(short_code, TEST_S_CODE - 1); // ShortCode > uint32_t + + EXPECT_EQ(short_code, short_code); // ShortCode == ShortCode + EXPECT_NE(short_code, ShortCode::unsafe_create(0)); // ShortCode != ShortCode + EXPECT_LE(short_code, short_code); // ShortCode <= ShortCode + EXPECT_LE(short_code, ShortCode::unsafe_create(TEST_S_CODE + 1)); EXPECT_LT(short_code, ShortCode::unsafe_create(TEST_S_CODE + 1)); // ShortCode < ShortCode + + EXPECT_GE(short_code, short_code); // ShortCode >= ShortCode + EXPECT_GE(ShortCode::unsafe_create(TEST_S_CODE + 1), short_code); EXPECT_GT(ShortCode::unsafe_create(TEST_S_CODE + 1), short_code); // ShortCode > ShortCode } @@ -75,12 +98,24 @@ TEST(ShortCode, exporter) { EXPECT_EQ(short_code.unwrap(), TEST_S_CODE); EXPECT_EQ(short_code.to_string(), TEST_S_CODE_STR); EXPECT_EQ(short_code.to_common_code(), TEST_C_CODE); + + // TODO: test fast mode of `to_common_code` } +// TODO: maybe add `speed_up` test suite + TEST(ShortCode, initializate) { auto short_code = ShortCode::unsafe_create(TEST_S_CODE); auto common_code = CommonCode::unsafe_create(TEST_C_CODE); + // operator= + auto s1 = short_code; + auto s2 = ShortCode {short_code}; + EXPECT_EQ(s1, TEST_S_CODE); // l-value + EXPECT_EQ(s2, TEST_S_CODE); // r-value + + // TODO: test fast mode of `ShortCode(CommonCode)` + // ShortCode(...) EXPECT_EQ(ShortCode(common_code), TEST_S_CODE); EXPECT_EQ(ShortCode(short_code), TEST_S_CODE); // l-value @@ -94,16 +129,11 @@ TEST(ShortCode, initializate) { // ShortCode::unsafe_create(uint32_t) EXPECT_EQ(ShortCode::unsafe_create(TEST_S_CODE), TEST_S_CODE); - // ShortCode::from_string(const std::string &) + // ShortCode::from_string(std::string_view) EXPECT_TRUE(ShortCode::from_string(TEST_S_CODE_STR).has_value()); EXPECT_FALSE(ShortCode::from_string(TEST_S_CODE_STR_ERR).has_value()); EXPECT_EQ(ShortCode::from_string(TEST_S_CODE_STR), TEST_S_CODE); - // ShortCode::from_string(std::string &&) - EXPECT_TRUE(ShortCode::from_string(TEST_S_CODE_STR_RV).has_value()); - EXPECT_FALSE(ShortCode::from_string(TEST_S_CODE_STR_ERR_RV).has_value()); - EXPECT_EQ(ShortCode::from_string(TEST_S_CODE_STR_RV), TEST_S_CODE); - // ShortCode::from_common_code(CommonCode) EXPECT_EQ(ShortCode::from_common_code(common_code), TEST_S_CODE); @@ -112,17 +142,18 @@ TEST(ShortCode, initializate) { EXPECT_FALSE(ShortCode::from_common_code(TEST_C_CODE_ERR).has_value()); EXPECT_EQ(ShortCode::from_common_code(TEST_C_CODE), TEST_S_CODE); - // ShortCode::from_common_code(const std::string &) + // ShortCode::from_common_code(std::string_view) EXPECT_TRUE(ShortCode::from_common_code(TEST_C_CODE_STR).has_value()); EXPECT_FALSE(ShortCode::from_common_code(TEST_C_CODE_STR_ERR).has_value()); EXPECT_EQ(ShortCode::from_common_code(TEST_C_CODE_STR), TEST_S_CODE); - - // ShortCode::from_common_code(std::string &&) - EXPECT_TRUE(ShortCode::from_common_code(TEST_C_CODE_STR_RV).has_value()); - EXPECT_FALSE(ShortCode::from_common_code(TEST_C_CODE_STR_ERR_RV).has_value()); - EXPECT_EQ(ShortCode::from_common_code(TEST_C_CODE_STR_RV), TEST_S_CODE); } +// TODO: global verify function +// -> check +// -> fast_decode / fast_encode +// -> tiny_decode / tiny_encode +// -> string_encode / string_decode + TEST(ShortCode, speed_up) { all_cases_reset(); basic_ranges_reset();