From 2d814e595c992e65053d9e363244349019bd369e Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 29 Jan 2023 19:19:07 +0800 Subject: [PATCH] test: support RawCode test --- src/klotski_core/raw_code/raw_code.h | 2 +- test/CMakeLists.txt | 8 ++--- test/common_code.cc | 19 +++++----- test/raw_code.cc | 54 ++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 15 deletions(-) create mode 100644 test/raw_code.cc diff --git a/src/klotski_core/raw_code/raw_code.h b/src/klotski_core/raw_code/raw_code.h index 5273a81..5b27c97 100644 --- a/src/klotski_core/raw_code/raw_code.h +++ b/src/klotski_core/raw_code/raw_code.h @@ -45,7 +45,7 @@ namespace klotski { class RawCode { public: - inline bool valid() const; + bool valid() const; static bool check(uint64_t raw_code); /// Operators of RawCode diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index dbb6d78..720b745 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -21,13 +21,13 @@ include_directories(../src/klotski_core/raw_code) include_directories(../src/klotski_core/short_code) include_directories(../src/klotski_core/common_code) -add_executable(test_basic utils.cc all_cases.cc common_code.cc) +add_executable(test_basic utils.cc all_cases.cc raw_code.cc common_code.cc) target_link_libraries(test_basic PUBLIC ${TEST_DEPS} md5) add_test(NAME basic COMMAND test_basic) # TODO: combine into basic after develop complete -add_executable(test_common_code common_code.cc) -target_link_libraries(test_common_code ${TEST_DEPS}) -add_test(NAME common_code COMMAND test_common_code) +add_executable(test_raw_code raw_code.cc) +target_link_libraries(test_raw_code ${TEST_DEPS}) +add_test(NAME raw_code COMMAND test_raw_code) ################################################################ diff --git a/test/common_code.cc b/test/common_code.cc index be2c357..57533d9 100644 --- a/test/common_code.cc +++ b/test/common_code.cc @@ -5,11 +5,10 @@ #include "gtest/gtest.h" using klotski::RawCode; +using klotski::AllCases; using klotski::ShortCode; using klotski::CommonCode; -using klotski::AllCases; - const static uint64_t TEST_CODE = 0x1A9BF0C00; const static std::string TEST_CODE_STR = "1A9BF0C00"; @@ -65,18 +64,10 @@ TEST(CommonCode, code_string) { } } -TEST(CommonCode, constructors) { - EXPECT_EQ(CommonCode(TEST_CODE).unwrap(), TEST_CODE); - EXPECT_EQ(CommonCode(TEST_CODE_STR).unwrap(), TEST_CODE); - EXPECT_EQ(CommonCode(RawCode::from_common_code(TEST_CODE)).unwrap(), TEST_CODE); - EXPECT_EQ(CommonCode(ShortCode::from_common_code(TEST_CODE)).unwrap(), TEST_CODE); -} - TEST(CommonCode, operators) { EXPECT_EQ(CommonCode(TEST_CODE), CommonCode(TEST_CODE)); // operator `==` std::cout << "TEST OUTPUT -> " << CommonCode(TEST_CODE) << std::endl; // ostream test EXPECT_EQ((uint64_t)CommonCode(TEST_CODE), TEST_CODE); // convert as uint64_t - EXPECT_EQ(CommonCode(TEST_CODE).unwrap(), TEST_CODE); } TEST(CommonCode, code_convert) { @@ -84,6 +75,14 @@ TEST(CommonCode, code_convert) { EXPECT_EQ(CommonCode(CommonCode(TEST_CODE).to_string()), CommonCode(TEST_CODE)); EXPECT_EQ(CommonCode(TEST_CODE).to_raw_code(), RawCode::from_common_code(TEST_CODE)); EXPECT_EQ(CommonCode(TEST_CODE).to_short_code(), ShortCode::from_common_code(TEST_CODE)); + EXPECT_EQ(CommonCode(TEST_CODE).unwrap(), TEST_CODE); +} + +TEST(CommonCode, constructors) { + EXPECT_EQ(CommonCode(TEST_CODE).unwrap(), TEST_CODE); + EXPECT_EQ(CommonCode(TEST_CODE_STR).unwrap(), TEST_CODE); + EXPECT_EQ(CommonCode(RawCode::from_common_code(TEST_CODE)).unwrap(), TEST_CODE); + EXPECT_EQ(CommonCode(ShortCode::from_common_code(TEST_CODE)).unwrap(), TEST_CODE); } TEST(CommonCode, initializate) { diff --git a/test/raw_code.cc b/test/raw_code.cc new file mode 100644 index 0000000..ebd2bce --- /dev/null +++ b/test/raw_code.cc @@ -0,0 +1,54 @@ +#include +#include "raw_code.h" +#include "all_cases.h" +#include "gtest/gtest.h" + +using klotski::RawCode; +using klotski::AllCases; +using klotski::CommonCode; + +const static uint64_t TEST_CODE = 0x0603'EDF5'CAFF'F5E2; + +TEST(RawCode, code_verify) { + std::thread threads[16]; + auto test = [](uint64_t head) { + for (const auto &range : AllCases::fetch()[head]) { + auto code = (uint64_t)RawCode::from_common_code(head << 32 | range); + EXPECT_EQ(RawCode::check(code), true); // test static `check` interface + auto tmp = RawCode::unsafe_create(code); // test dynamic `valid` interface + EXPECT_EQ(tmp.valid(), true); + } + }; + for (uint64_t head = 0; head < 16; ++head) { + threads[head] = std::thread(test, head); + } + for (auto &t : threads) { + t.join(); + } +} + +TEST(RawCode, operators) { + EXPECT_EQ(RawCode(TEST_CODE), RawCode(TEST_CODE)); // operator `==` + std::cout << "TEST OUTPUT" << std::endl << RawCode(TEST_CODE); // ostream test + EXPECT_EQ((uint64_t)RawCode(TEST_CODE), TEST_CODE); // convert as uint64_t +} + +TEST(RawCode, code_convert) { + EXPECT_EQ(RawCode(TEST_CODE).to_common_code(), CommonCode::from_raw_code(TEST_CODE)); + EXPECT_EQ(RawCode(TEST_CODE).unwrap(), TEST_CODE); +} + +TEST(RawCode, constructors) { + EXPECT_EQ(RawCode(TEST_CODE).unwrap(), TEST_CODE); + EXPECT_EQ(RawCode(CommonCode::from_raw_code(TEST_CODE)).unwrap(), TEST_CODE); +} + +TEST(RawCode, initializate) { + EXPECT_EQ(RawCode::create(TEST_CODE).unwrap(), TEST_CODE); + EXPECT_EQ(RawCode::unsafe_create(TEST_CODE).unwrap(), TEST_CODE); + + auto common_code = CommonCode::from_raw_code(TEST_CODE); + EXPECT_EQ(RawCode::from_common_code(common_code).unwrap(), TEST_CODE); + EXPECT_EQ(RawCode::from_common_code(common_code.unwrap()).unwrap(), TEST_CODE); + EXPECT_EQ(RawCode::from_common_code(common_code.to_string()).unwrap(), TEST_CODE); +}