Browse Source

test: optimize Ranges and RangesUnion test suites

legacy
Dnomd343 3 months ago
parent
commit
3605803856
  1. 3
      src/core_test/CMakeLists.txt
  2. 2
      src/core_test/cases/helper/cases.h
  3. 34
      src/core_test/cases/ranges.cc
  4. 36
      src/core_test/cases/ranges_union.cc

3
src/core_test/CMakeLists.txt

@ -10,9 +10,6 @@ set(KLSK_TEST_DEPS klotski_c klotski_core
include_directories(${CMAKE_CURRENT_SOURCE_DIR}) include_directories(${CMAKE_CURRENT_SOURCE_DIR})
# TODO: remove it
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/utility)
# ------------------------------------------------------------------------------------ # # ------------------------------------------------------------------------------------ #
set(KLSK_TEST_CASES_SRC set(KLSK_TEST_CASES_SRC

2
src/core_test/cases/helper/cases.h

@ -16,7 +16,7 @@ using klotski::cases::RangesUnion;
using klotski::cases::BLOCK_NUM; using klotski::cases::BLOCK_NUM;
using klotski::cases::ALL_CASES_NUM; using klotski::cases::ALL_CASES_NUM;
using klotski::cases::ALL_CASES_NUM; using klotski::cases::ALL_CASES_NUM_;
using klotski::cases::BASIC_RANGES_NUM; using klotski::cases::BASIC_RANGES_NUM;
using klotski::cases::BASIC_RANGES_NUM_; using klotski::cases::BASIC_RANGES_NUM_;

34
src/core_test/cases/ranges.cc

@ -25,7 +25,6 @@ TEST(Ranges, spawn) {
Ranges ranges; Ranges ranges;
ranges.spawn(n, n_2x1, n_1x1); ranges.spawn(n, n_2x1, n_1x1);
EXPECT_SORTED_AND_UNIQUE(ranges); // sorted and unique EXPECT_SORTED_AND_UNIQUE(ranges); // sorted and unique
for (const auto range : ranges) { for (const auto range : ranges) {
const auto [val_1x1, val_1x2, val_2x1] = cal_block_num(range); const auto [val_1x1, val_1x2, val_2x1] = cal_block_num(range);
EXPECT_EQ(val_1x1, n_1x1); EXPECT_EQ(val_1x1, n_1x1);
@ -55,16 +54,23 @@ TEST(Ranges, derive) {
} }
} }
TEST(Ranges, append) {
Ranges ranges;
for (auto [n, n_2x1, n_1x1] : BLOCK_NUM) {
Ranges r;
r.spawn(n, n_2x1, n_1x1);
auto &tmp = ranges += r;
EXPECT_EQ(tmp, ranges); // reference of ranges
}
std::stable_sort(ranges.begin(), ranges.end());
EXPECT_EQ(ranges, BasicRanges::instance().fetch());
}
TEST(Ranges, reverse) { TEST(Ranges, reverse) {
auto ranges = BasicRanges::instance().fetch(); auto ranges = BasicRanges::instance().fetch();
Ranges reverse {ranges}; auto reverse = ranges | std::views::transform(range_reverse) | std::ranges::to<std::vector>();
for (auto &x : reverse) {
x = range_reverse(x); // manual reverse
}
ranges.reverse(); ranges.reverse();
EXPECT_EQ(ranges, reverse); EXPECT_EQ(ranges, reverse);
ranges.reverse(); ranges.reverse();
EXPECT_EQ(ranges, BasicRanges::instance().fetch()); EXPECT_EQ(ranges, BasicRanges::instance().fetch());
} }
@ -72,7 +78,6 @@ TEST(Ranges, reverse) {
TEST(Ranges, combine) { TEST(Ranges, combine) {
Ranges all_ranges; Ranges all_ranges;
RangesUnion all_cases; RangesUnion all_cases;
all_ranges.reserve(BASIC_RANGES_NUM_); // pre reserve all_ranges.reserve(BASIC_RANGES_NUM_); // pre reserve
for (const auto head : Heads) { for (const auto head : Heads) {
all_cases[head].reserve(ALL_CASES_NUM[head]); // pre reserve all_cases[head].reserve(ALL_CASES_NUM[head]); // pre reserve
@ -92,8 +97,8 @@ TEST(Ranges, combine) {
for (const auto head : Heads) { for (const auto head : Heads) {
std::ranges::stable_sort(all_cases[head].begin(), all_cases[head].end()); std::ranges::stable_sort(all_cases[head].begin(), all_cases[head].end());
} }
EXPECT_EQ(all_ranges, BasicRanges::instance().fetch()); // verify content EXPECT_EQ(all_ranges, BasicRanges::instance().fetch()); // verify all ranges
EXPECT_EQ(all_cases, AllCases::instance().fetch()); // verify content EXPECT_EQ(all_cases, AllCases::instance().fetch()); // verify all cases
all_ranges.reverse(); // reverse ranges for derive all_ranges.reverse(); // reverse ranges for derive
for (const auto head : Heads) { for (const auto head : Heads) {
@ -102,12 +107,3 @@ TEST(Ranges, combine) {
} }
EXPECT_EQ(all_cases, AllCases::instance().fetch()); // verify content EXPECT_EQ(all_cases, AllCases::instance().fetch()); // verify content
} }
TEST(Ranges, operator) {
Ranges r, r1, r2;
r.spawn(5, 3, 4);
r.spawn(5, 4, 4);
r1.spawn(5, 3, 4);
r2.spawn(5, 4, 4);
EXPECT_EQ(r, r1 += r2);
}

36
src/core_test/cases/ranges_union.cc

@ -1,35 +1,39 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <format>
#include "hash.h"
#include "helper/cases.h" #include "helper/cases.h"
#include "utility/hash.h"
#include "ranges/ranges.h" #include "ranges/ranges.h"
static_assert(std::is_base_of_v<std::array<Ranges, 16>, RangesUnion>);
constexpr std::string_view ALL_CASES_MD5 = "3888e9fab8d3cbb50908b12b147cfb23"; constexpr std::string_view ALL_CASES_MD5 = "3888e9fab8d3cbb50908b12b147cfb23";
static_assert(std::is_base_of_v<std::array<Ranges, 16>, RangesUnion>);
TEST(RangesUnion, export) { TEST(RangesUnion, export) {
std::string buffer; std::string buffer;
buffer.reserve(ALL_CASES_NUM_ * 10); // [\dA-F]{9} + '\n'
for (auto code : AllCases::instance().fetch().codes()) { for (auto code : AllCases::instance().fetch().codes()) {
buffer += std::format("{:09X}\n", code.unwrap()); buffer += std::format("{:09X}\n", code.unwrap());
} }
EXPECT_EQ(hash::md5(buffer), ALL_CASES_MD5); EXPECT_EQ(hash::md5(buffer), ALL_CASES_MD5);
} }
TEST(RangesUnion, operator) { TEST(RangesUnion, append) {
Ranges r, r1, r2; RangesUnion cases;
r.spawn(5, 3, 4); for (auto [n, n_2x1, n_1x1] : BLOCK_NUM) {
r.spawn(5, 4, 4); Ranges r;
r1.spawn(5, 3, 4); r.spawn(n, n_2x1, n_1x1);
r2.spawn(5, 4, 4); r.reverse();
RangesUnion ru;
for (const auto head : Heads) {
r.derive(head, ru[head]);
}
auto &tmp = cases += ru;
EXPECT_EQ(tmp, cases); // reference of cases
}
RangesUnion r_, r1_, r2_;
for (const auto head : Heads) { for (const auto head : Heads) {
r.derive(head, r_[head]); std::stable_sort(cases[head].begin(), cases[head].end());
r1.derive(head, r1_[head]);
r2.derive(head, r2_[head]);
} }
EXPECT_EQ(r_, r1_ += r2_); EXPECT_EQ(cases, AllCases::instance().fetch());
} }

Loading…
Cancel
Save