Browse Source

test: add test suite for BasicRanges and AllCases

legacy
Dnomd343 1 year ago
parent
commit
fa14dde73d
  1. 10
      CMakeLists.txt
  2. 1
      src/CMakeLists.txt
  3. 4
      src/core/CMakeLists.txt
  4. 1
      src/core/all_cases/all_cases.h
  5. 20
      src/core_test/CMakeLists.txt
  6. 89
      src/core_test/cases/all_cases.cc

10
CMakeLists.txt

@ -8,6 +8,8 @@ endif()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra")
get_filename_component(KLOTSKI_ROOT_PATH "${CMAKE_CURRENT_SOURCE_DIR}" ABSOLUTE)
################################################################################
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin)
@ -34,11 +36,11 @@ add_subdirectory(src)
################################################################################
option(CORE_TEST "build test for klotski core" ON)
#option(CORE_TEST "build test for klotski core" ON)
#if (CORE_TEST)
include_directories(third_party/googletest/googletest/include)
add_subdirectory(third_party/googletest EXCLUDE_FROM_ALL)
add_subdirectory(test)
# include_directories(third_party/googletest/googletest/include)
# add_subdirectory(third_party/googletest EXCLUDE_FROM_ALL)
# add_subdirectory(test)
#endif()
################################################################################

1
src/CMakeLists.txt

@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.0)
project(klotski-cli LANGUAGES C)
add_subdirectory(core)
add_subdirectory(core_test)
#add_subdirectory(klotski_core)
#include_directories(klotski_core)

4
src/core/CMakeLists.txt

@ -8,4 +8,6 @@ add_compile_options(-fno-exceptions)
include_directories(utils)
add_executable(${PROJECT_NAME} main.cc all_cases/basic_ranges.cc all_cases/all_cases.cc)
add_library(${PROJECT_NAME} all_cases/basic_ranges.cc all_cases/all_cases.cc)
#add_executable(${PROJECT_NAME} main.cc all_cases/basic_ranges.cc all_cases/all_cases.cc)

1
src/core/all_cases/all_cases.h

@ -40,6 +40,7 @@
#include <vector>
#include <cstdint>
#include <numeric>
#include <functional>
#include "utility.h"
namespace klotski {

20
src/core_test/CMakeLists.txt

@ -0,0 +1,20 @@
cmake_minimum_required(VERSION 3.0)
set(CMAKE_CXX_STANDARD 20)
include_directories(${KLOTSKI_ROOT_PATH}/third_party/googletest/googletest/include)
add_subdirectory(${KLOTSKI_ROOT_PATH}/third_party/googletest googletest EXCLUDE_FROM_ALL)
#unset(CMAKE_ARCHIVE_OUTPUT_DIRECTORY)
include_directories(${KLOTSKI_ROOT_PATH}/third_party/md5)
add_library(md5 STATIC ${KLOTSKI_ROOT_PATH}/third_party/md5/md5.cpp)
set(KLOTSKI_TEST_DEPS klotski-core gtest gtest_main md5)
include_directories(${KLOTSKI_ROOT_PATH}/src/core/all_cases)
include_directories(${KLOTSKI_ROOT_PATH}/src/core/utils)
add_executable(test_klotski_cases cases/all_cases.cc)
target_link_libraries(test_klotski_cases PRIVATE ${KLOTSKI_TEST_DEPS})
add_test(NAME klotski_cases COMMAND test_klotski_cases)

89
src/core_test/cases/all_cases.cc

@ -0,0 +1,89 @@
#include <string>
#include <thread>
#include <vector>
#include "gtest/gtest.h"
#include "all_cases.h"
#include "md5.h"
using klotski::cases::AllCases;
using klotski::cases::BasicRanges;
using klotski::cases::ALL_CASES_NUM;
using klotski::cases::ALL_CASES_NUM_;
using klotski::cases::BASIC_RANGES_NUM;
static const auto TEST_THREAD_NUM = 256;
/// The efficiency of string hashing is not very high, but there is a memorable
/// story, and this scheme is still retained here.
static const std::string ALL_CASES_MD5 = "3888e9fab8d3cbb50908b12b147cfb23";
static const std::string BASIC_RANGES_MD5 = "6f385dc171e201089ff96bb010b47212";
TEST(Cases, basic_ranges_build) {
std::vector<std::thread> threads;
threads.reserve(TEST_THREAD_NUM);
for (int i = 0; i < TEST_THREAD_NUM; ++i) {
threads.emplace_back([]() {
BasicRanges::Instance().Build();
});
}
for (auto &t : threads) {
t.join();
}
EXPECT_TRUE(BasicRanges::Instance().IsAvailable());
}
TEST(Cases, basic_ranges_size) {
auto &basic_ranges = BasicRanges::Instance().Fetch();
EXPECT_EQ(basic_ranges.size(), BASIC_RANGES_NUM);
}
TEST(Cases, basic_ranges_data) {
std::string basic_ranges_str;
basic_ranges_str.reserve(BASIC_RANGES_NUM * 9); // 8-bit + '\n'`
for (auto range : BasicRanges::Instance().Fetch()) {
char *tmp = nullptr;
asprintf(&tmp, "%08X\n", range);
basic_ranges_str += tmp;
}
EXPECT_EQ(md5(basic_ranges_str), BASIC_RANGES_MD5);
}
TEST(Cases, all_cases_build) {
std::vector<std::thread> threads;
threads.reserve(TEST_THREAD_NUM);
for (int i = 0; i < TEST_THREAD_NUM; ++i) {
threads.emplace_back([]() {
AllCases::Instance().Build();
});
}
for (auto &t : threads) {
t.join();
}
EXPECT_TRUE(AllCases::Instance().IsAvailable());
}
TEST(Cases, all_cases_size) {
auto &all_cases = AllCases::Instance().Fetch();
for (int head = 0; head < 16; ++head) {
EXPECT_EQ(all_cases[head].size(), ALL_CASES_NUM[head]);
}
auto all_cases_num = 0;
for (auto num : ALL_CASES_NUM) {
all_cases_num += num;
}
EXPECT_EQ(all_cases_num, ALL_CASES_NUM_);
}
TEST(Cases, all_cases_data) {
std::string all_cases_str;
all_cases_str.reserve(ALL_CASES_NUM_ * 10); // 9-bit + '\n'
for (uint64_t head = 0; head < 16; ++head) {
for (auto range : AllCases::Instance().Fetch()[head]) {
char *tmp = nullptr;
asprintf(&tmp, "%09llX\n", head << 32 | range);
all_cases_str += tmp;
}
}
EXPECT_EQ(md5(all_cases_str), ALL_CASES_MD5);
}
Loading…
Cancel
Save