Browse Source

test: complete common code global test

legacy
Dnomd343 2 years ago
parent
commit
558190fcfd
  1. 33
      test/global/common_code.cc
  2. 23
      test/global/global_utils.cc
  3. 26
      test/global/global_utils.h

33
test/global/common_code.cc

@ -1,7 +1,9 @@
#include "all_cases.h"
#include "gtest/gtest.h"
#include "common_code.h"
#include "global_utils.h"
using klotski::AllCases;
using klotski::CommonCode;
std::vector<uint64_t> common_code_search(uint64_t start, uint64_t end) {
@ -14,16 +16,31 @@ std::vector<uint64_t> common_code_search(uint64_t start, uint64_t end) {
return ret;
}
TEST(GLOBAL, common_code) {
/// create common code check tasks
auto pool = TinyPool(thread_num());
std::vector<std::future<std::vector<uint64_t>>> futures;
for (const auto &range : range_split(0, 0x10'0000'0000, 0x10'0000)) {
futures.emplace_back(
pool.submit(common_code_search, range.first, range.second)
);
}
// 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);
/// run common code search
pool.boot();
std::vector<uint64_t> result;
for (auto &f : futures) {
auto ret = f.get();
result.insert(result.end(), ret.begin(), ret.end());
}
pool.join();
/// verify check result
std::vector<uint64_t> all_cases;
for (uint64_t head = 0; head < 16; ++head) {
for (const auto &range : AllCases::fetch()[head]) {
all_cases.emplace_back(head << 32 | range);
}
}
EXPECT_EQ(result, all_cases);
}

23
test/global/global_utils.cc

@ -1,21 +1,5 @@
#include "global_utils.h"
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();
}
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;
@ -27,3 +11,10 @@ range_split_t range_split(uint64_t start, uint64_t end, uint64_t size) {
}
return ranges;
}
uint32_t thread_num() {
// TODO: use CPU core number
return 16;
}

26
test/global/global_utils.h

@ -1,8 +1,15 @@
#pragma once
#include <future>
#include <vector>
#include <functional>
#include "tiny_pool.h"
uint32_t thread_num();
typedef std::vector<std::pair<uint64_t, uint64_t>> range_split_t;
range_split_t range_split(uint64_t start, uint64_t end, uint64_t size);
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
@ -19,9 +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...))> {
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();
}
};
typedef std::vector<std::pair<uint64_t, uint64_t>> range_split_t;
range_split_t range_split(uint64_t start, uint64_t end, uint64_t size);

Loading…
Cancel
Save