From 27f0bfac0c7cb766fc438c154c20c7c7cb3ab49e Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 19 Feb 2023 14:55:57 +0800 Subject: [PATCH] test: short code test framework --- test/CMakeLists.txt | 6 +++++- test/global/global_utils.h | 14 +++++++------- test/global/short_code.cc | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 8 deletions(-) create mode 100644 test/global/short_code.cc diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index df91664..5f94a1e 100644 --- a/test/CMakeLists.txt +++ b/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) + ################################################################################ diff --git a/test/global/global_utils.h b/test/global/global_utils.h index beb00fd..05aee9f 100644 --- a/test/global/global_utils.h +++ b/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*>(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 - auto submit(Func &&func, Args &&...args) -> std::future { + auto submit(Func &&func, Args &&...args) -> std::future { // submit new task std::function wrap_func = std::bind( - std::forward(func), std::forward(args)... + std::forward(func), std::forward(args)... // wrap as a function without params ); auto func_ptr = std::make_shared< - std::packaged_task + std::packaged_task // 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 ( - [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 } }; diff --git a/test/global/short_code.cc b/test/global/short_code.cc new file mode 100644 index 0000000..d55ad39 --- /dev/null +++ b/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 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 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; + + +}