From d11d6fe5510859091d929522f9554c16870b4af6 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Thu, 13 Apr 2023 18:54:34 +0800 Subject: [PATCH] test: group cases support mutli-threads test --- test/CMakeLists.txt | 11 +---------- test/group/build_cases.cc | 28 ++++++++++++++++++++-------- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 922a972..8f31179 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -65,7 +65,7 @@ set(TEST_GROUP_SRC group/build_cases.cc ) add_executable(test_group ${TEST_GROUP_SRC}) -target_link_libraries(test_group ${TEST_DEPS} md5 absl::flat_hash_map) +target_link_libraries(test_group ${TEST_DEPS} md5 tiny_pool absl::flat_hash_map) add_test(NAME group COMMAND test_group) ####################################################################################### @@ -91,13 +91,4 @@ add_executable(test_codec_global ${TEST_CODEC_GLOBAL_SRC}) target_link_libraries(test_codec_global ${TEST_DEPS} tiny_pool) add_test(NAME codec_global COMMAND test_codec_global) -# TODO: update cmake configure - -#add_library(test_global_utils STATIC global/global_utils.cc) -#set(TEST_GLOBAL_DEPS ${TEST_DEPS} test_global_utils tiny_pool) - -#add_executable(test_codec_global global/short_code.cc global/common_code.cc global/raw_code.cc) -#target_link_libraries(test_codec_global ${TEST_GLOBAL_DEPS}) -#add_test(NAME codec_global COMMAND test_codec_global) - ####################################################################################### diff --git a/test/group/build_cases.cc b/test/group/build_cases.cc index 21385b4..f65adbc 100644 --- a/test/group/build_cases.cc +++ b/test/group/build_cases.cc @@ -4,6 +4,8 @@ #include "common_code.h" #include "gtest/gtest.h" +#include "tiny_pool.h" + #include "group/type_id.h" #include "group/group_seeds.h" @@ -48,22 +50,32 @@ TEST(Group, all_cases) { } TEST(Group, group_cases) { - - // TODO: using multi-threads - - std::vector all_cases; - all_cases.reserve(ALL_CASES_SIZE_SUM); - - for (auto &&seed : GROUP_SEEDS) { + auto test = [](CommonCode seed) -> std::vector { auto group_raw = Group::group_cases(RawCode::from_common_code(seed)); std::vector group(group_raw.begin(), group_raw.end()); // convert as CommonCodes EXPECT_EQ(seed, std::min_element(group.begin(), group.end())->unwrap()); // confirm min seed - all_cases.insert(all_cases.end(), group.begin(), group.end()); // archive group cases uint32_t type_id = Group::type_id(CommonCode(seed)); // current type id for (auto &&elem : group) { EXPECT_EQ(Group::type_id(elem), type_id); // verify type id of group cases } + return group; + }; + + auto pool = TinyPool(); + std::vector>> futures; + for (auto &&seed : GROUP_SEEDS) { + futures.emplace_back( + pool.submit(test, CommonCode::unsafe_create(seed)) + ); + } + + pool.boot(); + std::vector all_cases; + all_cases.reserve(ALL_CASES_SIZE_SUM); + for (auto &&f : futures) { + auto ret = f.get(); + all_cases.insert(all_cases.end(), ret.begin(), ret.end()); } std::sort(all_cases.begin(), all_cases.end()); EXPECT_EQ(all_cases, AllCases::release()); // verify all released cases