From 7a1f4a45908ae9c9b69bb76738a7adec8483e466 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Fri, 10 Feb 2023 13:06:59 +0800 Subject: [PATCH] update: framework of common code global test --- test/CMakeLists.txt | 14 ++++++-------- test/global/common_code.cc | 25 ++++++++++++++++++++++-- test/global/global_utils.cc | 29 ++++++++++++++++++++++++++++ test/global/global_utils.h | 27 ++++++++++++++++++++++++++ test/global/utils.cc | 37 ------------------------------------ third_party/tiny_thread_pool | 2 +- 6 files changed, 86 insertions(+), 48 deletions(-) create mode 100644 test/global/global_utils.cc create mode 100644 test/global/global_utils.h delete mode 100644 test/global/utils.cc diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 61ad45f..df91664 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -15,8 +15,8 @@ add_library(md5 STATIC ../third_party/md5/md5.cpp) ################################################################################ unset(CMAKE_ARCHIVE_OUTPUT_DIRECTORY) -include_directories(../third_party/tiny_thread_pool/src) -add_library(tiny_pool STATIC ../third_party/tiny_thread_pool/src/tiny_pool.c) +include_directories(../third_party/tiny_thread_pool) +add_library(tiny_pool STATIC ../third_party/tiny_thread_pool/tiny_pool.c) ################################################################################ @@ -55,15 +55,13 @@ add_test(NAME ffi COMMAND test_ffi) ################################################################################ include_directories(../src/klotski_core) +add_library(test_global_utils STATIC global/global_utils.cc) +set(TEST_GLOBAL_DEPS ${TEST_DEPS} test_global_utils tiny_pool) -add_library(test_global_utils STATIC global/utils.cc) +include_directories(../src/klotski_core/common_code) add_executable(test_global_common_code global/common_code.cc) - -target_link_libraries(test_global_common_code test_global_utils tiny_pool) - -target_link_libraries(test_global_common_code ${TEST_DEPS}) - +target_link_libraries(test_global_common_code ${TEST_GLOBAL_DEPS}) add_test(NAME global_common_code COMMAND test_global_common_code) ################################################################################ diff --git a/test/global/common_code.cc b/test/global/common_code.cc index d4d168d..e9cfbf1 100644 --- a/test/global/common_code.cc +++ b/test/global/common_code.cc @@ -1,8 +1,29 @@ -#include "klotski.h" #include "gtest/gtest.h" +#include "common_code.h" +#include "global_utils.h" + +using klotski::CommonCode; + +std::vector common_code_search(uint64_t start, uint64_t end) { + std::vector ret; + for (uint64_t common_code = start; common_code < end; ++common_code) { + if (CommonCode::check(common_code)) { + ret.emplace_back(common_code); // valid common code + } + } + return ret; +} + TEST(GLOBAL, common_code) { -// std::cout << "demo ok" << std::endl; +// 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); + } } diff --git a/test/global/global_utils.cc b/test/global/global_utils.cc new file mode 100644 index 0000000..7f01cad --- /dev/null +++ b/test/global/global_utils.cc @@ -0,0 +1,29 @@ +#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; + for (uint64_t i = start; i < rear; i += size) { + ranges.emplace_back(i, i + size); + } + if (rear != end) { + ranges.emplace_back(rear, end); + } + return ranges; +} diff --git a/test/global/global_utils.h b/test/global/global_utils.h new file mode 100644 index 0000000..2ac533c --- /dev/null +++ b/test/global/global_utils.h @@ -0,0 +1,27 @@ +#include +#include +#include +#include "tiny_pool.h" + +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 + auto func_ptr = static_cast*>(func); + (*func_ptr)(); + delete func_ptr; + } + +public: + void boot() { tiny_pool_boot(pool); } + void join() { tiny_pool_join(pool); } + void kill() { tiny_pool_kill(pool); } + void detach() { tiny_pool_detach(pool); } + explicit TinyPool(uint32_t size) { pool = tiny_pool_create(size); } + + template + auto submit(Func &&func, Args &&...args) -> std::future; +}; + +typedef std::vector> range_split_t; + +range_split_t range_split(uint64_t start, uint64_t end, uint64_t size); diff --git a/test/global/utils.cc b/test/global/utils.cc deleted file mode 100644 index d3a9edd..0000000 --- a/test/global/utils.cc +++ /dev/null @@ -1,37 +0,0 @@ -#include -#include -#include "tiny_pool.h" - -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 - (*static_cast*>(func))(); - free(func); - } - -public: - void boot() { tiny_pool_boot(pool); } - void join() { tiny_pool_join(pool); } - void kill() { tiny_pool_kill(pool); } - void detach() { tiny_pool_detach(pool); } - explicit TinyPool(uint32_t size) { pool = tiny_pool_create(size); } - - template - auto submit(Func &&func, Args &&...args) -> std::future; -}; - -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(); -} diff --git a/third_party/tiny_thread_pool b/third_party/tiny_thread_pool index 77351b1..216c554 160000 --- a/third_party/tiny_thread_pool +++ b/third_party/tiny_thread_pool @@ -1 +1 @@ -Subproject commit 77351b1aace75c17b5163ea1d2940f665d7ed400 +Subproject commit 216c55476c45f9c2b8b6567ea2bf1aa643d95c0a