From efe94179ad491faceb94261cb034fc7666f6fb36 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Thu, 9 Feb 2023 01:09:29 +0800 Subject: [PATCH] test: framework of global test --- test/CMakeLists.txt | 20 ++++++++++++++++++++ test/global/common_code.cc | 8 ++++++++ test/global/utils.cc | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 test/global/common_code.cc create mode 100644 test/global/utils.cc diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 1560c0a..61ad45f 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -14,6 +14,12 @@ 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(../src/klotski_core/utils) include_directories(../src/klotski_core/all_cases) @@ -47,3 +53,17 @@ target_link_libraries(test_ffi ${TEST_DEPS}) add_test(NAME ffi COMMAND test_ffi) ################################################################################ + +include_directories(../src/klotski_core) + +add_library(test_global_utils STATIC global/utils.cc) + +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}) + +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 new file mode 100644 index 0000000..d4d168d --- /dev/null +++ b/test/global/common_code.cc @@ -0,0 +1,8 @@ +#include "klotski.h" +#include "gtest/gtest.h" + +TEST(GLOBAL, common_code) { + +// std::cout << "demo ok" << std::endl; + +} diff --git a/test/global/utils.cc b/test/global/utils.cc new file mode 100644 index 0000000..d3a9edd --- /dev/null +++ b/test/global/utils.cc @@ -0,0 +1,37 @@ +#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(); +}