Browse Source

test: add invalid cases check

master
Dnomd343 2 years ago
parent
commit
702f42cd98
  1. 4
      src/klotski_core/common_code/serialize.cc
  2. 11
      src/klotski_core/short_code/serialize.cc
  3. 2
      test/basic/all_cases.cc
  4. 21
      test/codec/common_code.cc
  5. 6
      test/codec/raw_code.cc
  6. 12
      test/codec/short_code.cc

4
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

11
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

2
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();

21
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<void(void)> &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];

6
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];

12
test/codec/short_code.cc

@ -23,14 +23,11 @@ inline void SHOULD_PANIC(const std::function<void(void)> &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();

Loading…
Cancel
Save