From b1f750e362950224e774508e51366e3c81c30e29 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 29 Jan 2023 18:12:07 +0800 Subject: [PATCH] test: support CommonCode test --- src/klotski_core/common_code/common_code.h | 2 +- test/CMakeLists.txt | 24 +++----- test/all_cases.cc | 2 +- test/common_code.cc | 65 ++++++++++++++++++++++ 4 files changed, 76 insertions(+), 17 deletions(-) diff --git a/src/klotski_core/common_code/common_code.h b/src/klotski_core/common_code/common_code.h index fe0a8c0..a6f626a 100644 --- a/src/klotski_core/common_code/common_code.h +++ b/src/klotski_core/common_code/common_code.h @@ -64,7 +64,7 @@ namespace klotski { class CommonCode { public: - inline bool valid() const; + bool valid() const; static bool check(uint64_t common_code); /// Operators of CommonCode diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 253ba7b..dbb6d78 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -14,26 +14,20 @@ add_library(md5 STATIC ../third_party/md5/md5.cpp) ################################################################ -#include_directories(../src/klotski_core/utils) -#add_executable(test_utils utils.cc) -#target_link_libraries(test_utils PUBLIC ${TEST_DEPS}) -#add_test(NAME utils COMMAND test_utils) - -################################################################ - -#include_directories(../src/klotski_core/all_cases) -#add_executable(test_all_cases all_cases.cc) -#target_link_libraries(test_all_cases PUBLIC ${TEST_DEPS} md5) -#add_test(NAME all_cases COMMAND test_all_cases) - -################################################################ - include_directories(../src/klotski_core/utils) include_directories(../src/klotski_core/all_cases) +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) 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) + ################################################################ diff --git a/test/all_cases.cc b/test/all_cases.cc index dcb2326..7680a9d 100644 --- a/test/all_cases.cc +++ b/test/all_cases.cc @@ -10,7 +10,7 @@ using klotski::ALL_CASES_SIZE; using klotski::BASIC_RANGES_SIZE; using klotski::ALL_CASES_SIZE_SUM; -const int TEST_THREAD_NUM = 8; +const static int TEST_THREAD_NUM = 8; /// basic ranges constants const char BASIC_RANGES_MD5[] = "6f385dc171e201089ff96bb010b47212"; diff --git a/test/common_code.cc b/test/common_code.cc index 8366fbc..bd8bb7d 100644 --- a/test/common_code.cc +++ b/test/common_code.cc @@ -1,3 +1,68 @@ +#include "all_cases.h" +#include "common_code.h" #include "gtest/gtest.h" +using klotski::RawCode; +using klotski::ShortCode; +using klotski::CommonCode; +using klotski::AllCases; + +const static uint64_t TEST_CODE = 0x1A9BF0C00; +const static std::string TEST_CODE_STR = "1A9BF0C00"; + +TEST(CommonCode, code_verify) { + for (uint64_t head = 0; head < 16; ++head) { + for (const auto &range : AllCases::fetch()[head]) { + uint64_t code = head << 32 | range; + EXPECT_EQ(CommonCode::check(code), true); // test static `check` interface + auto tmp = CommonCode::unsafe_create(code); // test dynamic `valid` interface + EXPECT_EQ(tmp.valid(), true); + } + } +} + +TEST(CommonCode, code_string) { + for (uint64_t head = 0; head < 16; ++head) { + for (const auto &range : AllCases::fetch()[head]) { + uint64_t code = head << 32 | range; + auto common_code = CommonCode::unsafe_create(code); + EXPECT_EQ(CommonCode::from_string(common_code.to_string()), common_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, operators) { + EXPECT_EQ(CommonCode(TEST_CODE), CommonCode(TEST_CODE)); // operator `==` + std::cout << 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) { + 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)); +} + +TEST(CommonCode, initializate) { + EXPECT_EQ(CommonCode::create(TEST_CODE).unwrap(), TEST_CODE); + EXPECT_EQ(CommonCode::from_string(TEST_CODE_STR).unwrap(), TEST_CODE); + EXPECT_EQ(CommonCode::unsafe_create(TEST_CODE).unwrap(), TEST_CODE); + + auto raw_code = RawCode::from_common_code(TEST_CODE); + EXPECT_EQ(CommonCode::from_raw_code(raw_code).unwrap(), TEST_CODE); + EXPECT_EQ(CommonCode::from_raw_code(raw_code.unwrap()).unwrap(), TEST_CODE); + + auto short_code = ShortCode::from_common_code(TEST_CODE); + EXPECT_EQ(CommonCode::from_short_code(short_code).unwrap(), TEST_CODE); + EXPECT_EQ(CommonCode::from_short_code(short_code.unwrap()).unwrap(), TEST_CODE); + EXPECT_EQ(CommonCode::from_short_code(short_code.to_string()).unwrap(), TEST_CODE); +}