From 702f42cd98c5a0c3bd53f30f74b27abc6e1cd7e5 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Mon, 30 Jan 2023 20:39:42 +0800 Subject: [PATCH] test: add invalid cases check --- src/klotski_core/common_code/serialize.cc | 4 ++-- src/klotski_core/short_code/serialize.cc | 11 ++++++----- test/basic/all_cases.cc | 2 ++ test/codec/common_code.cc | 21 +++++++++++++++++++-- test/codec/raw_code.cc | 6 ++++-- test/codec/short_code.cc | 12 +++++------- 6 files changed, 38 insertions(+), 18 deletions(-) diff --git a/src/klotski_core/common_code/serialize.cc b/src/klotski_core/common_code/serialize.cc index e6a4097..81bf169 100644 --- a/src/klotski_core/common_code/serialize.cc +++ b/src/klotski_core/common_code/serialize.cc @@ -46,9 +46,9 @@ CommonCode::CommonCode(const std::string &common_code) { // convert from 1 ~ 9 b result <<= 4; if (bit >= '0' && bit <= '9') { // 0 ~ 9 result |= (bit - 48); - } else if (bit >= 'A' && bit <= 'Z') { // A ~ Z + } else if (bit >= 'A' && bit <= 'F') { // A ~ F result |= (bit - 55); - } else if (bit >= 'a' && bit <= 'z') { // a ~ z + } else if (bit >= 'a' && bit <= 'f') { // a ~ f result |= (bit - 87); } else { throw CommonCodeException("common code with invalid character"); // unknown character diff --git a/src/klotski_core/short_code/serialize.cc b/src/klotski_core/short_code/serialize.cc index fa9d44c..26e107d 100644 --- a/src/klotski_core/short_code/serialize.cc +++ b/src/klotski_core/short_code/serialize.cc @@ -29,11 +29,12 @@ ShortCode::ShortCode(const std::string &short_code) { // 5-bits string decode if (bit >= 'a' && bit <= 'z') { bit -= 32; // convert to uppercase } - if (bit >= '1' && bit <= 'Z') { // valid characters - result += (bit = SHORT_CODE_TABLE_REV[bit - 49]); // table convert - if (bit == -1) { - throw ShortCodeException("short code with invalid character"); // unknown character - } + if (bit < '1' || bit > 'Z') { // invalid characters + throw ShortCodeException("short code with invalid character"); + } + result += (bit = SHORT_CODE_TABLE_REV[bit - 49]); // table convert + if (bit == -1) { // invalid character + throw ShortCodeException("short code with invalid character"); } } if (!ShortCode::check(result)) { // check converted short code diff --git a/test/basic/all_cases.cc b/test/basic/all_cases.cc index b936526..952d55d 100644 --- a/test/basic/all_cases.cc +++ b/test/basic/all_cases.cc @@ -23,6 +23,7 @@ TEST(AllCases, basic_ranges_mutex) { for (auto &t : threads) { t = std::thread(BasicRanges::build); } + usleep(1000); // wait 1ms -> avoid mutex unlocked EXPECT_EQ(BasicRanges::status(), BasicRanges::BUILDING); for (auto &t : threads) { t.join(); @@ -55,6 +56,7 @@ TEST(AllCases, all_cases_mutex) { for (auto &t : threads) { t = std::thread(AllCases::build); } + usleep(1000); // wait 1ms -> avoid mutex unlocked EXPECT_EQ(AllCases::status(), AllCases::BUILDING); for (auto &t : threads) { t.join(); diff --git a/test/codec/common_code.cc b/test/codec/common_code.cc index 9f781c6..506bdbb 100644 --- a/test/codec/common_code.cc +++ b/test/codec/common_code.cc @@ -11,9 +11,26 @@ using klotski::CommonCode; const static uint64_t TEST_CODE = 0x1'A9BF'0C00; const static std::string TEST_CODE_STR = "1A9BF0C00"; -const static uint64_t TEST_ERR_CODE = 0x1'2190'2300; -// TODO: test some invalid cases +inline void SHOULD_PANIC(const std::function &func) { + bool panic_flag = false; + try { + func(); + } catch (klotski::CommonCodeException&) { + panic_flag = true; + } + EXPECT_EQ(panic_flag, true); +} + +TEST(CommonCode, invalid) { + 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 + EXPECT_NE(CommonCode::check(0x1'A0'BF'0C'01), true); // low bits not fill zero + + SHOULD_PANIC([](){ CommonCode::from_string("0123456789"); }); // length > 9 + SHOULD_PANIC([](){ CommonCode::from_string("123J432A9"); }); // with invalid `J` +} TEST(CommonCode, code_verify) { std::thread threads[16]; diff --git a/test/codec/raw_code.cc b/test/codec/raw_code.cc index d8b212d..228ba50 100644 --- a/test/codec/raw_code.cc +++ b/test/codec/raw_code.cc @@ -8,9 +8,11 @@ using klotski::AllCases; using klotski::CommonCode; const static uint64_t TEST_CODE = 0x0603'EDF5'CAFF'F5E2; -const static uint64_t TEST_ERR_CODE = 0x0A34'182B'3810'2D21; -// TODO: test some invalid cases +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, code_verify) { std::thread threads[16]; diff --git a/test/codec/short_code.cc b/test/codec/short_code.cc index 3f9bc46..59d7f2b 100644 --- a/test/codec/short_code.cc +++ b/test/codec/short_code.cc @@ -23,14 +23,11 @@ inline void SHOULD_PANIC(const std::function &func) { } TEST(ShortCode, invalid) { - EXPECT_NE(ShortCode::check(29670987), true); - - std::cout << ShortCode::from_string("R50EH") << std::endl; - + 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` -// SHOULD_PANIC([](){ ShortCode::from_string("123456"); }); // length != 5 -// SHOULD_PANIC([](){ ShortCode::from_string("Z9EFV"); }); // out of short code range - + SHOULD_PANIC([](){ ShortCode::from_string("123456"); }); // length != 5 + SHOULD_PANIC([](){ ShortCode::from_string("Z9EFV"); }); // out of short code range } TEST(ShortCode, speed_up) { @@ -41,6 +38,7 @@ TEST(ShortCode, speed_up) { for (auto &t : threads) { t = std::thread(ShortCode::speed_up, ShortCode::NORMAL); } + usleep(1000); // wait 1ms -> avoid mutex unlocked EXPECT_EQ(BasicRanges::status(), BasicRanges::BUILDING); for (auto &t : threads) { t.join();