From 558190fcfd2a755d6f5220582b466cda6727da59 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Fri, 10 Feb 2023 14:40:15 +0800 Subject: [PATCH] test: complete common code global test --- test/global/common_code.cc | 33 +++++++++++++++++++++++++-------- test/global/global_utils.cc | 23 +++++++---------------- test/global/global_utils.h | 26 +++++++++++++++++++++----- 3 files changed, 53 insertions(+), 29 deletions(-) diff --git a/test/global/common_code.cc b/test/global/common_code.cc index e9cfbf1..b74e828 100644 --- a/test/global/common_code.cc +++ b/test/global/common_code.cc @@ -1,7 +1,9 @@ +#include "all_cases.h" #include "gtest/gtest.h" #include "common_code.h" #include "global_utils.h" +using klotski::AllCases; using klotski::CommonCode; std::vector common_code_search(uint64_t start, uint64_t end) { @@ -14,16 +16,31 @@ std::vector common_code_search(uint64_t start, uint64_t end) { return ret; } - TEST(GLOBAL, common_code) { + /// create common code check tasks + auto pool = TinyPool(thread_num()); + std::vector>> futures; + for (const auto &range : range_split(0, 0x10'0000'0000, 0x10'0000)) { + futures.emplace_back( + pool.submit(common_code_search, range.first, range.second) + ); + } -// auto ranges = range_split(0, 100, 30); -// auto ranges = range_split(0, 91, 30); -// auto ranges = range_split(0, 89, 30); - auto ranges = range_split(0, 90, 30); - - for (const auto &r : ranges) { - printf("[%lu, %lu)\n", r.first, r.second); + /// run common code search + pool.boot(); + std::vector result; + for (auto &f : futures) { + auto ret = f.get(); + result.insert(result.end(), ret.begin(), ret.end()); } + pool.join(); + /// verify check result + std::vector all_cases; + for (uint64_t head = 0; head < 16; ++head) { + for (const auto &range : AllCases::fetch()[head]) { + all_cases.emplace_back(head << 32 | range); + } + } + EXPECT_EQ(result, all_cases); } diff --git a/test/global/global_utils.cc b/test/global/global_utils.cc index 7f01cad..786ffb2 100644 --- a/test/global/global_utils.cc +++ b/test/global/global_utils.cc @@ -1,21 +1,5 @@ #include "global_utils.h" -template -auto TinyPool::submit(Func &&func, Args &&...args) -> std::future { - std::function wrap_func = std::bind( - std::forward(func), std::forward(args)... - ); - auto func_ptr = std::make_shared< - std::packaged_task - >(wrap_func); - tiny_pool_submit(pool, TinyPool::wrap_c_func, (void*)( - new std::function ( - [func_ptr]() { (*func_ptr)(); } - ) - )); - return func_ptr->get_future(); -} - range_split_t range_split(uint64_t start, uint64_t end, uint64_t size) { uint64_t rear = end - (end - start) % size; // (rear - start) % size == 0 range_split_t ranges; @@ -27,3 +11,10 @@ range_split_t range_split(uint64_t start, uint64_t end, uint64_t size) { } return ranges; } + +uint32_t thread_num() { + + // TODO: use CPU core number + + return 16; +} diff --git a/test/global/global_utils.h b/test/global/global_utils.h index 2ac533c..beb00fd 100644 --- a/test/global/global_utils.h +++ b/test/global/global_utils.h @@ -1,8 +1,15 @@ +#pragma once + #include #include #include #include "tiny_pool.h" +uint32_t thread_num(); + +typedef std::vector> range_split_t; +range_split_t range_split(uint64_t start, uint64_t end, uint64_t size); + class TinyPool { // OOP for tiny_thread_pool pool_t *pool; static void wrap_c_func(void *func) { // wrap std::function as c-style function ptr @@ -19,9 +26,18 @@ public: explicit TinyPool(uint32_t size) { pool = tiny_pool_create(size); } template - auto submit(Func &&func, Args &&...args) -> std::future; + auto submit(Func &&func, Args &&...args) -> std::future { + std::function wrap_func = std::bind( + std::forward(func), std::forward(args)... + ); + auto func_ptr = std::make_shared< + std::packaged_task + >(wrap_func); + tiny_pool_submit(pool, TinyPool::wrap_c_func, (void*)( + new std::function ( + [func_ptr]() { (*func_ptr)(); } + ) + )); + return func_ptr->get_future(); + } }; - -typedef std::vector> range_split_t; - -range_split_t range_split(uint64_t start, uint64_t end, uint64_t size);