Browse Source

test: core test using multi-threads

legacy
Dnomd343 2 years ago
parent
commit
491994595c
  1. 2
      test/CMakeLists.txt
  2. 19
      test/core/core.cc

2
test/CMakeLists.txt

@ -55,7 +55,7 @@ add_test(NAME codec COMMAND test_codec)
set(TEST_CORE_SRC core/core.cc) set(TEST_CORE_SRC core/core.cc)
add_executable(test_core ${TEST_CORE_SRC}) add_executable(test_core ${TEST_CORE_SRC})
target_link_libraries(test_core ${TEST_DEPS} absl::flat_hash_map) target_link_libraries(test_core ${TEST_DEPS} tiny_pool absl::flat_hash_map)
add_test(NAME core COMMAND test_core) add_test(NAME core COMMAND test_core)
####################################################################################### #######################################################################################

19
test/core/core.cc

@ -1,8 +1,9 @@
#include <unordered_set>
#include "core.h" #include "core.h"
#include "group.h" #include "group.h"
#include "all_cases.h" #include "all_cases.h"
#include "tiny_pool.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "absl/container/flat_hash_set.h"
using klotski::RawCode; using klotski::RawCode;
using klotski::AllCases; using klotski::AllCases;
@ -11,25 +12,27 @@ using klotski::GroupType;
using klotski::TYPE_ID_LIMIT; using klotski::TYPE_ID_LIMIT;
TEST(Core, next_cases) { TEST(Core, next_cases) {
auto test = [](uint32_t type_id) { auto test = [](GroupType type_id) {
auto raw_codes = std::unordered_set<uint64_t>(); auto raw_codes = absl::flat_hash_set<uint64_t>();
raw_codes.reserve(klotski::ALL_CASES_SIZE_SUM); raw_codes.reserve(klotski::ALL_CASES_SIZE_SUM);
for (auto &&common_code: GroupType(type_id).cases()) { // load all cases in a type for (auto &&common_code: type_id.cases()) { // load all cases in a type
raw_codes.emplace(common_code.to_raw_code().unwrap()); raw_codes.emplace(common_code.to_raw_code().unwrap());
} }
auto core = klotski::Core( auto core = klotski::Core(
[&raw_codes](auto &&code, auto &&mask) { [&raw_codes](auto &&code, auto &&mask) {
EXPECT_NE(raw_codes.find(code), raw_codes.end()); // verify next case EXPECT_NE(raw_codes.find(code), raw_codes.end()); // next case existed
EXPECT_EQ(mask >> __builtin_ctzll(mask), 0b111); // verify mask EXPECT_EQ(mask >> __builtin_ctzll(mask), 0b111); // mask format
} }
); );
for (auto &&raw_code: raw_codes) { for (auto &&raw_code: raw_codes) {
core.next_cases(raw_code, 0); // search next cases core.next_cases(raw_code, 0); // search next cases
} }
}; };
// TODO: using multi-threads
auto pool = TinyPool();
for (uint32_t type_id = 0; type_id < TYPE_ID_LIMIT; ++type_id) { for (uint32_t type_id = 0; type_id < TYPE_ID_LIMIT; ++type_id) {
test(type_id); pool.submit(test, GroupType(type_id)); // test all type_ids
} }
pool.boot().join();
} }

Loading…
Cancel
Save