From 3c210617287dcc87573be0b58aa082b401c36e41 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 29 Jan 2023 17:13:35 +0800 Subject: [PATCH] test: add mutex check for BasicRanges and AllCases --- test/CMakeLists.txt | 24 +++++++++++++++++------- test/all_cases.cc | 31 +++++++++++++++++++++++++++++++ test/common_code.cc | 3 +++ 3 files changed, 51 insertions(+), 7 deletions(-) create mode 100644 test/common_code.cc diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 322945c..253ba7b 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -14,16 +14,26 @@ 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/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) -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) + +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) ################################################################ diff --git a/test/all_cases.cc b/test/all_cases.cc index 37aa211..dcb2326 100644 --- a/test/all_cases.cc +++ b/test/all_cases.cc @@ -1,3 +1,4 @@ +#include #include #include "md5.h" #include "all_cases.h" @@ -9,12 +10,28 @@ using klotski::ALL_CASES_SIZE; using klotski::BASIC_RANGES_SIZE; using klotski::ALL_CASES_SIZE_SUM; +const int TEST_THREAD_NUM = 8; + /// basic ranges constants const char BASIC_RANGES_MD5[] = "6f385dc171e201089ff96bb010b47212"; /// all cases constants const char ALL_CASES_MD5[] = "3888e9fab8d3cbb50908b12b147cfb23"; +/// basic ranges mutex check +TEST(AllCases, basic_ranges_mutex) { + std::thread threads[TEST_THREAD_NUM]; + EXPECT_EQ(BasicRanges::status(), BasicRanges::NO_INIT); + for (auto &t : threads) { + t = std::thread(BasicRanges::build); + } + EXPECT_EQ(BasicRanges::status(), BasicRanges::BUILDING); + for (auto &t : threads) { + t.join(); + } + EXPECT_EQ(BasicRanges::status(), BasicRanges::AVAILABLE); +} + /// basic ranges size check TEST(AllCases, basic_ranges_size) { auto &basic_ranges = BasicRanges::fetch(); @@ -33,6 +50,20 @@ TEST(AllCases, basic_ranges_data) { EXPECT_STREQ(basic_ranges_md5.c_str(), BASIC_RANGES_MD5); } +/// basic ranges mutex check +TEST(AllCases, all_cases_mutex) { + std::thread threads[TEST_THREAD_NUM]; + EXPECT_EQ(AllCases::status(), AllCases::NO_INIT); + for (auto &t : threads) { + t = std::thread(AllCases::build); + } + EXPECT_EQ(AllCases::status(), AllCases::BUILDING); + for (auto &t : threads) { + t.join(); + } + EXPECT_EQ(AllCases::status(), AllCases::AVAILABLE); +} + /// all cases size check TEST(AllCases, all_cases_size) { auto &all_cases = AllCases::fetch(); diff --git a/test/common_code.cc b/test/common_code.cc new file mode 100644 index 0000000..8366fbc --- /dev/null +++ b/test/common_code.cc @@ -0,0 +1,3 @@ +#include "gtest/gtest.h" + +