Browse Source

test: short code test framework

legacy
Dnomd343 2 years ago
parent
commit
27f0bfac0c
  1. 6
      test/CMakeLists.txt
  2. 14
      test/global/global_utils.h
  3. 37
      test/global/short_code.cc

6
test/CMakeLists.txt

@ -59,9 +59,13 @@ add_library(test_global_utils STATIC global/global_utils.cc)
set(TEST_GLOBAL_DEPS ${TEST_DEPS} test_global_utils tiny_pool)
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_DEPS})
add_test(NAME global_common_code COMMAND test_global_common_code)
include_directories(../src/klotski_core/short_code)
add_executable(test_global_short_code global/short_code.cc)
target_link_libraries(test_global_short_code ${TEST_GLOBAL_DEPS})
add_test(NAME global_short_code COMMAND test_global_short_code)
################################################################################

14
test/global/global_utils.h

@ -15,7 +15,7 @@ class TinyPool { // OOP for tiny_thread_pool
static void wrap_c_func(void *func) { // wrap std::function as c-style function ptr
auto func_ptr = static_cast<std::function<void()>*>(func);
(*func_ptr)();
delete func_ptr;
delete func_ptr; // free lambda function
}
public:
@ -26,18 +26,18 @@ public:
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...))> {
auto submit(Func &&func, Args &&...args) -> std::future<decltype(func(args...))> { // submit new task
std::function<decltype(func(args...))()> wrap_func = std::bind(
std::forward<Func>(func), std::forward<Args>(args)...
std::forward<Func>(func), std::forward<Args>(args)... // wrap as a function without params
);
auto func_ptr = std::make_shared<
std::packaged_task<decltype(func(args...))()>
std::packaged_task<decltype(func(args...))()> // function task as shared ptr
>(wrap_func);
tiny_pool_submit(pool, TinyPool::wrap_c_func, (void*)(
tiny_pool_submit(pool, TinyPool::wrap_c_func, (void*)( // submit with thread pool interface
new std::function<void()> (
[func_ptr]() { (*func_ptr)(); }
[func_ptr]() { (*func_ptr)(); } // create lambda for running task
)
));
return func_ptr->get_future();
return func_ptr->get_future(); // return future object
}
};

37
test/global/short_code.cc

@ -0,0 +1,37 @@
#include "gtest/gtest.h"
#include "short_code.h"
using klotski::ShortCode;
using klotski::CommonCode;
std::vector<uint64_t> short_code_check(uint32_t start, uint32_t end) {
// TODO: give me short_code range -> convert to common_code and archive it
// common_code convert to short_code -> verify normal mode
std::vector<uint64_t> archive;
for (uint32_t short_code = start; short_code < end; ++short_code) {
auto common_code = ShortCode::unsafe_create(short_code).to_common_code();
archive.emplace_back(common_code.unwrap());
EXPECT_EQ(common_code.to_short_code().unwrap(), short_code);
}
return archive;
}
TEST(GLOBAL, short_code) {
ShortCode::speed_up(ShortCode::NORMAL);
auto r = short_code_check(0, 100);
std::cout << r.size() << std::endl;
}
Loading…
Cancel
Save