Browse Source

test: framework of global test

legacy
Dnomd343 2 years ago
parent
commit
efe94179ad
  1. 20
      test/CMakeLists.txt
  2. 8
      test/global/common_code.cc
  3. 37
      test/global/utils.cc

20
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)
################################################################################

8
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;
}

37
test/global/utils.cc

@ -0,0 +1,37 @@
#include <future>
#include <functional>
#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<std::function<void()>*>(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 <typename Func, typename ...Args>
auto submit(Func &&func, Args &&...args) -> std::future<decltype(func(args...))>;
};
template <typename Func, typename ...Args>
auto TinyPool::submit(Func &&func, Args &&...args) -> std::future<decltype(func(args...))> {
std::function<decltype(func(args...))()> wrap_func = std::bind(
std::forward<Func>(func), std::forward<Args>(args)...
);
auto func_ptr = std::make_shared<
std::packaged_task<decltype(func(args...))()>
>(wrap_func);
tiny_pool_submit(pool, TinyPool::wrap_c_func, (void*)(
new std::function<void()> (
[func_ptr]() { (*func_ptr)(); }
)
));
return func_ptr->get_future();
}
Loading…
Cancel
Save