Browse Source

test: adapt CommonCode test suites

master
Dnomd343 3 months ago
parent
commit
8adcf9ac51
  1. 1
      src/core_test/CMakeLists.txt
  2. 61
      src/core_test/codec/common_code.cc
  3. 27
      src/core_test/codec/helper/codec.cc
  4. 14
      src/core_test/codec/helper/codec.h
  5. 36
      src/core_test/codec/helper/sample.h
  6. 3
      src/core_test/codec/raw_code.cc
  7. 54
      src/core_test/codec/sample.h
  8. 5
      src/core_test/codec/short_code.cc

1
src/core_test/CMakeLists.txt

@ -46,6 +46,7 @@ set(KLSK_TEST_CODEC_SRC
codec/raw_code.cc codec/raw_code.cc
codec/short_code.cc codec/short_code.cc
codec/common_code.cc codec/common_code.cc
codec/helper/codec.cc
) )
add_executable(test_klotski_codec ${KLSK_TEST_CODEC_SRC}) add_executable(test_klotski_codec ${KLSK_TEST_CODEC_SRC})

61
src/core_test/codec/common_code.cc

@ -2,7 +2,8 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <BS_thread_pool.hpp> #include <BS_thread_pool.hpp>
#include "sample.h" #include "helper/codec.h"
#include "helper/sample.h"
#include "raw_code/raw_code.h" #include "raw_code/raw_code.h"
#include "all_cases/all_cases.h" #include "all_cases/all_cases.h"
#include "short_code/short_code.h" #include "short_code/short_code.h"
@ -34,15 +35,36 @@ TEST(CommonCode, operators) {
EXPECT_EQ(tmp.str(), TEST_C_CODE_STR); EXPECT_EQ(tmp.str(), TEST_C_CODE_STR);
EXPECT_EQ((uint64_t)common_code, TEST_C_CODE); // convert as uint64_t EXPECT_EQ((uint64_t)common_code, TEST_C_CODE); // convert as uint64_t
EXPECT_NE(0, common_code); // uint64_t != CommonCode
EXPECT_NE(common_code, 0); // CommonCode != uint64_t
EXPECT_EQ(TEST_C_CODE, common_code); // uint64_t == CommonCode EXPECT_EQ(TEST_C_CODE, common_code); // uint64_t == CommonCode
EXPECT_EQ(common_code, TEST_C_CODE); // CommonCode == uint64_t EXPECT_EQ(common_code, TEST_C_CODE); // CommonCode == uint64_t
EXPECT_EQ(common_code, common_code); // CommonCode == CommonCode
EXPECT_NE(TEST_C_CODE + 1, common_code); // uint64_t != CommonCode EXPECT_LE(TEST_C_CODE, common_code); // uint64_t <= CommonCode
EXPECT_NE(common_code, TEST_C_CODE + 1); // CommonCode != uint64_t EXPECT_LE(TEST_C_CODE - 1, common_code);
EXPECT_NE(common_code, CommonCode::unsafe_create(TEST_C_CODE + 1)); // CommonCode != CommonCode EXPECT_LT(TEST_C_CODE - 1, common_code); // uint64_t < CommonCode
EXPECT_LE(common_code, TEST_C_CODE); // CommonCode <= uint64_t
EXPECT_LE(common_code, TEST_C_CODE + 1);
EXPECT_LT(common_code, TEST_C_CODE + 1); // CommonCode < uint64_t
EXPECT_GE(TEST_C_CODE, common_code); // uint64_t >= CommonCode
EXPECT_GE(TEST_C_CODE + 1, common_code);
EXPECT_GT(TEST_C_CODE + 1, common_code); // uint64_t > CommonCode
EXPECT_GE(common_code, TEST_C_CODE); // CommonCode >= uint64_t
EXPECT_GE(common_code, TEST_C_CODE - 1);
EXPECT_GT(common_code, TEST_C_CODE - 1); // CommonCode > uint64_t
EXPECT_EQ(common_code, common_code); // CommonCode == CommonCode
EXPECT_NE(common_code, CommonCode::unsafe_create(0)); // CommonCode != CommonCode
EXPECT_LE(common_code, common_code); // CommonCode <= CommonCode
EXPECT_LE(common_code, CommonCode::unsafe_create(TEST_C_CODE + 1));
EXPECT_LT(common_code, CommonCode::unsafe_create(TEST_C_CODE + 1)); // CommonCode < CommonCode EXPECT_LT(common_code, CommonCode::unsafe_create(TEST_C_CODE + 1)); // CommonCode < CommonCode
EXPECT_GE(common_code, common_code); // CommonCode >= CommonCode
EXPECT_GE(CommonCode::unsafe_create(TEST_C_CODE + 1), common_code);
EXPECT_GT(CommonCode::unsafe_create(TEST_C_CODE + 1), common_code); // CommonCode > CommonCode EXPECT_GT(CommonCode::unsafe_create(TEST_C_CODE + 1), common_code); // CommonCode > CommonCode
} }
@ -54,12 +76,10 @@ TEST(CommonCode, exporter) {
EXPECT_EQ(common_code.to_short_code(), TEST_S_CODE); EXPECT_EQ(common_code.to_short_code(), TEST_S_CODE);
auto code_shorten = common_code.to_string(true); auto code_shorten = common_code.to_string(true);
EXPECT_EQ(CommonCode::from_string(code_shorten), common_code); // l-value EXPECT_EQ(CommonCode::from_string(code_shorten), common_code);
EXPECT_EQ(CommonCode::from_string(std::move(code_shorten)), common_code); // r-value
auto code_normal = common_code.to_string(false); auto code_normal = common_code.to_string(false);
EXPECT_EQ(CommonCode::from_string(code_normal), common_code); // l-value EXPECT_EQ(CommonCode::from_string(code_normal), common_code);
EXPECT_EQ(CommonCode::from_string(std::move(code_normal)), common_code); // r-value
} }
TEST(CommonCode, initializate) { TEST(CommonCode, initializate) {
@ -67,11 +87,17 @@ TEST(CommonCode, initializate) {
auto short_code = ShortCode::unsafe_create(TEST_S_CODE); auto short_code = ShortCode::unsafe_create(TEST_S_CODE);
auto common_code = CommonCode::unsafe_create(TEST_C_CODE); auto common_code = CommonCode::unsafe_create(TEST_C_CODE);
// operator==
CommonCode c1 = common_code;
EXPECT_EQ(c1, TEST_C_CODE); // l-value
CommonCode c2 = CommonCode {common_code};
EXPECT_EQ(c2, TEST_C_CODE); // r-value
// CommonCode(...) // CommonCode(...)
EXPECT_EQ(CommonCode(raw_code), TEST_C_CODE); EXPECT_EQ(CommonCode(raw_code), TEST_C_CODE);
EXPECT_EQ(CommonCode(short_code), TEST_C_CODE); EXPECT_EQ(CommonCode(short_code), TEST_C_CODE);
EXPECT_EQ(CommonCode(common_code), TEST_C_CODE); // l-value EXPECT_EQ(CommonCode(common_code), TEST_C_CODE); // l-value
EXPECT_EQ(CommonCode(CommonCode(common_code)), TEST_C_CODE); // r-value EXPECT_EQ(CommonCode(CommonCode {common_code}), TEST_C_CODE); // r-value
// CommonCode::create(uint64_t) // CommonCode::create(uint64_t)
EXPECT_TRUE(CommonCode::create(TEST_C_CODE).has_value()); EXPECT_TRUE(CommonCode::create(TEST_C_CODE).has_value());
@ -80,17 +106,13 @@ TEST(CommonCode, initializate) {
// CommonCode::unsafe_create(uint64_t) // CommonCode::unsafe_create(uint64_t)
EXPECT_EQ(CommonCode::unsafe_create(TEST_C_CODE), TEST_C_CODE); EXPECT_EQ(CommonCode::unsafe_create(TEST_C_CODE), TEST_C_CODE);
EXPECT_EQ(CommonCode::unsafe_create(TEST_C_CODE_ERR), TEST_C_CODE_ERR);
// CommonCode::from_string(const std::string &) // CommonCode::from_string(std::string_view)
EXPECT_TRUE(CommonCode::from_string(TEST_C_CODE_STR).has_value()); EXPECT_TRUE(CommonCode::from_string(TEST_C_CODE_STR).has_value());
EXPECT_FALSE(CommonCode::from_string(TEST_C_CODE_STR_ERR).has_value()); EXPECT_FALSE(CommonCode::from_string(TEST_C_CODE_STR_ERR).has_value());
EXPECT_EQ(CommonCode::from_string(TEST_C_CODE_STR), TEST_C_CODE); EXPECT_EQ(CommonCode::from_string(TEST_C_CODE_STR), TEST_C_CODE);
// CommonCode::from_string(std::string &&)
EXPECT_TRUE(CommonCode::from_string(TEST_C_CODE_STR_RV).has_value());
EXPECT_FALSE(CommonCode::from_string(TEST_C_CODE_STR_ERR_RV).has_value());
EXPECT_EQ(CommonCode::from_string(TEST_C_CODE_STR_RV), TEST_C_CODE);
// CommonCode::from_raw_code(RawCode) // CommonCode::from_raw_code(RawCode)
EXPECT_EQ(CommonCode::from_raw_code(raw_code), TEST_C_CODE); EXPECT_EQ(CommonCode::from_raw_code(raw_code), TEST_C_CODE);
@ -107,15 +129,10 @@ TEST(CommonCode, initializate) {
EXPECT_FALSE(CommonCode::from_short_code(TEST_S_CODE_ERR).has_value()); EXPECT_FALSE(CommonCode::from_short_code(TEST_S_CODE_ERR).has_value());
EXPECT_EQ(CommonCode::from_short_code(TEST_S_CODE), TEST_C_CODE); EXPECT_EQ(CommonCode::from_short_code(TEST_S_CODE), TEST_C_CODE);
// CommonCode::from_short_code(const std::string &) // CommonCode::from_short_code(std::string_view)
EXPECT_TRUE(CommonCode::from_short_code(TEST_S_CODE_STR).has_value()); EXPECT_TRUE(CommonCode::from_short_code(TEST_S_CODE_STR).has_value());
EXPECT_FALSE(CommonCode::from_short_code(TEST_S_CODE_STR_ERR).has_value()); EXPECT_FALSE(CommonCode::from_short_code(TEST_S_CODE_STR_ERR).has_value());
EXPECT_EQ(CommonCode::from_short_code(TEST_S_CODE_STR), TEST_C_CODE); EXPECT_EQ(CommonCode::from_short_code(TEST_S_CODE_STR), TEST_C_CODE);
// CommonCode::from_short_code(std::string &&)
EXPECT_TRUE(CommonCode::from_short_code(TEST_S_CODE_STR_RV).has_value());
EXPECT_FALSE(CommonCode::from_short_code(TEST_S_CODE_STR_ERR_RV).has_value());
EXPECT_EQ(CommonCode::from_short_code(TEST_S_CODE_STR_RV), TEST_C_CODE);
} }
TEST(CommonCode, code_verify) { TEST(CommonCode, code_verify) {

27
src/core_test/codec/helper/codec.cc

@ -0,0 +1,27 @@
#include <BS_thread_pool.hpp>
#include "codec.h"
void head_parallel(std::function<void(uint64_t head)> &&func) {
BS::thread_pool pool;
// TODO: skip invalid head
pool.detach_sequence(0, 16, [func = std::move(func)](const uint64_t head) {
if (head == 3 || head == 7 || head == 11 || head == 15) {
return;
}
func(head);
});
pool.wait();
}
std::vector<uint64_t> all_common_codes() {
// TODO: using `std::ranges`
std::vector<uint64_t> common_codes;
common_codes.reserve(ALL_CASES_NUM_);
for (uint64_t head = 0; head < 16; ++head) {
for (auto range : AllCases::instance().fetch()[head]) {
common_codes.emplace_back(head << 32 | range);
}
}
return common_codes;
}

14
src/core_test/codec/helper/codec.h

@ -0,0 +1,14 @@
#pragma once
#include <functional>
#include "all_cases/all_cases.h"
using klotski::cases::AllCases;
using klotski::cases::ALL_CASES_NUM_;
/// Build all valid CommonCodes.
std::vector<uint64_t> all_common_codes();
/// Spawn all valid klotski headers in parallel.
void head_parallel(std::function<void(uint64_t head)> &&func);

36
src/core_test/codec/helper/sample.h

@ -0,0 +1,36 @@
#pragma once
#include <string>
#include <cstdint>
// ----------------------------------------------------------------------------------------- //
/// Valid klotski RawCode
constexpr uint64_t TEST_R_CODE = 0x0603'EDF5'CAFF'F5E2;
/// Valid klotski ShortCode
constexpr uint32_t TEST_S_CODE = 4091296;
const std::string TEST_S_CODE_STR = "4WVE1"; // TODO: using `std::string_view`
#define TEST_S_CODE_STR_RV std::string(TEST_S_CODE_STR) // TODO: remove r-value
/// Valid klotski CommonCode
constexpr uint64_t TEST_C_CODE = 0x1'A9BF'0C00;
const std::string TEST_C_CODE_STR = "1A9BF0C00";
#define TEST_C_CODE_STR_RV std::string(TEST_C_CODE_STR) // TODO: remove r-value
// ----------------------------------------------------------------------------------------- //
/// Invalid klotski RawCode
constexpr uint64_t TEST_R_CODE_ERR = 0x1603'ED00'CAFF'F5E2;
/// Invalid klotski ShortCode
constexpr uint32_t TEST_S_CODE_ERR = 1234564323;
const std::string TEST_S_CODE_STR_ERR = "ZZZZZZ";
#define TEST_S_CODE_STR_ERR_RV std::string(TEST_S_CODE_STR_ERR) // TODO: remove r-value
/// Invalid klotski CommonCode
constexpr uint64_t TEST_C_CODE_ERR = 0x3'A9BF'0C00;
const static std::string TEST_C_CODE_STR_ERR = "0123456789";
#define TEST_C_CODE_STR_ERR_RV std::string(TEST_C_CODE_STR_ERR) // TODO: remove r-value
// ----------------------------------------------------------------------------------------- //

3
src/core_test/codec/raw_code.cc

@ -1,8 +1,9 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <BS_thread_pool.hpp> #include <BS_thread_pool.hpp>
#include "sample.h"
#include "utils/common.h" #include "utils/common.h"
#include "helper/codec.h"
#include "helper/sample.h"
#include "raw_code/raw_code.h" #include "raw_code/raw_code.h"
#include "all_cases/all_cases.h" #include "all_cases/all_cases.h"
#include "common_code/common_code.h" #include "common_code/common_code.h"

54
src/core_test/codec/sample.h

@ -1,54 +0,0 @@
#pragma once
#include <string>
#include <cstdint>
#include "all_cases/all_cases.h"
// ----------------------------------------------------------------------------------------- //
// Valid RawCode
const uint64_t TEST_R_CODE = 0x0603'EDF5'CAFF'F5E2;
// Valid ShortCode
const uint32_t TEST_S_CODE = 4091296;
const std::string TEST_S_CODE_STR = "4WVE1";
#define TEST_S_CODE_STR_RV std::string(TEST_S_CODE_STR) // r-value
// Valid CommonCode
const uint64_t TEST_C_CODE = 0x1'A9BF'0C00;
const std::string TEST_C_CODE_STR = "1A9BF0C00";
#define TEST_C_CODE_STR_RV std::string(TEST_C_CODE_STR) // r-value
// ----------------------------------------------------------------------------------------- //
// Invalid RawCode
const static uint64_t TEST_R_CODE_ERR = 0x1603'ED00'CAFF'F5E2;
// Invalid ShortCode
const static uint32_t TEST_S_CODE_ERR = 1234564323;
const static std::string TEST_S_CODE_STR_ERR = "ZZZZZZ";
#define TEST_S_CODE_STR_ERR_RV std::string(TEST_S_CODE_STR_ERR) // r-value
// Invalid CommonCode
const static uint64_t TEST_C_CODE_ERR = 0x3'A9BF'0C00;
const static std::string TEST_C_CODE_STR_ERR = "0123456789";
#define TEST_C_CODE_STR_ERR_RV std::string(TEST_C_CODE_STR_ERR) // r-value
// ----------------------------------------------------------------------------------------- //
using klotski::cases::AllCases;
using klotski::cases::ALL_CASES_NUM_;
inline std::vector<uint64_t> all_common_codes() {
std::vector<uint64_t> common_codes;
common_codes.reserve(ALL_CASES_NUM_);
for (uint64_t head = 0; head < 16; ++head) {
for (auto range : AllCases::instance().fetch()[head]) {
common_codes.emplace_back(head << 32 | range);
}
}
return common_codes;
}
// ----------------------------------------------------------------------------------------- //

5
src/core_test/codec/short_code.cc

@ -2,8 +2,9 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <BS_thread_pool.hpp> #include <BS_thread_pool.hpp>
#include "sample.h" #include "helper/codec.h"
#include "exposer.h" #include "helper/sample.h"
#include "utility/exposer.h"
#include "all_cases/all_cases.h" #include "all_cases/all_cases.h"
#include "short_code/short_code.h" #include "short_code/short_code.h"
#include "common_code/common_code.h" #include "common_code/common_code.h"

Loading…
Cancel
Save