From f2a410d3972d04ac76477ab4b30919e8986b3dde Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sat, 15 Apr 2023 11:31:18 +0800 Subject: [PATCH] test: `SHOULD_PANIC` as macro --- test/codec/common_code.cc | 19 +++++++------------ test/codec/short_code.cc | 21 ++++++++------------- 2 files changed, 15 insertions(+), 25 deletions(-) diff --git a/test/codec/common_code.cc b/test/codec/common_code.cc index 31b6476..e14eb6b 100644 --- a/test/codec/common_code.cc +++ b/test/codec/common_code.cc @@ -5,6 +5,11 @@ #include "common_code.h" #include "gtest/gtest.h" +#define SHOULD_PANIC(FUNC) \ + try { \ + FUNC; EXPECT_STREQ("should panic", "but no panic"); \ + } catch (...) {} + using klotski::RawCode; using klotski::AllCases; using klotski::ShortCode; @@ -13,16 +18,6 @@ using klotski::CommonCode; const static uint64_t TEST_CODE = 0x1'A9BF'0C00; const static std::string TEST_CODE_STR = "1A9BF0C00"; -static inline void SHOULD_PANIC(const std::function &func) { - bool panic_flag = false; - try { - func(); - } catch (klotski::CommonCodeExp &) { - panic_flag = true; - } - EXPECT_EQ(panic_flag, true); -} - TEST(CommonCode, hash) { auto tmp = std::unordered_set{ CommonCode(TEST_CODE) }; EXPECT_EQ(tmp.size(), 1); @@ -34,8 +29,8 @@ TEST(CommonCode, validity) { 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` + SHOULD_PANIC(CommonCode::from_string("0123456789")) // length > 9 + SHOULD_PANIC(CommonCode::from_string("123J432A9")) // with invalid `J` } TEST(CommonCode, code_verify) { // test all layout diff --git a/test/codec/short_code.cc b/test/codec/short_code.cc index a88635d..22b2f0c 100644 --- a/test/codec/short_code.cc +++ b/test/codec/short_code.cc @@ -5,6 +5,11 @@ #include "short_code.h" #include "gtest/gtest.h" +#define SHOULD_PANIC(FUNC) \ + try { \ + FUNC; EXPECT_STREQ("should panic", "but no panic"); \ + } catch (...) {} + using klotski::AllCases; using klotski::ShortCode; using klotski::CommonCode; @@ -16,16 +21,6 @@ using klotski::ALL_CASES_SIZE_SUM; const static uint32_t TEST_CODE = 4091296; const static std::string TEST_CODE_STR = "4WVE1"; -static inline void SHOULD_PANIC(const std::function &func) { - bool panic_flag = false; - try { - func(); - } catch (klotski::ShortCodeExp &) { - panic_flag = true; - } - EXPECT_EQ(panic_flag, true); -} - TEST(ShortCode, limit) { EXPECT_EQ(ALL_CASES_SIZE_SUM, SHORT_CODE_LIMIT); } @@ -38,9 +33,9 @@ TEST(ShortCode, hash) { 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` - SHOULD_PANIC([](){ ShortCode::from_string("123456"); }); // length != 5 - SHOULD_PANIC([](){ ShortCode::from_string("Z9EFV"); }); // 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 } TEST(ShortCode, speed_up) {