Browse Source

test: `SHOULD_PANIC` as macro

master
Dnomd343 1 year ago
parent
commit
f2a410d397
  1. 19
      test/codec/common_code.cc
  2. 21
      test/codec/short_code.cc

19
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<void()> &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>{ 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

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

Loading…
Cancel
Save