From fece208b2f72c8f55c066689d98eba7618e63947 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sat, 15 Jun 2024 14:41:49 +0800 Subject: [PATCH] test: add operator test suites for Ranges --- src/core_test/CMakeLists.txt | 1 + src/core_test/cases/ranges.cc | 35 ++++++++++++++++------------- src/core_test/cases/ranges_union.cc | 24 ++++++++++++++++++++ 3 files changed, 44 insertions(+), 16 deletions(-) diff --git a/src/core_test/CMakeLists.txt b/src/core_test/CMakeLists.txt index 3f97da2..8348ab4 100644 --- a/src/core_test/CMakeLists.txt +++ b/src/core_test/CMakeLists.txt @@ -21,6 +21,7 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../core/common_code) set(KLSK_TEST_CASES_SRC cases/ranges.cc + cases/ranges_union.cc cases/basic_ranges.cc cases/all_cases.cc cases/group_union.cc diff --git a/src/core_test/cases/ranges.cc b/src/core_test/cases/ranges.cc index d6cda64..c6829d4 100644 --- a/src/core_test/cases/ranges.cc +++ b/src/core_test/cases/ranges.cc @@ -5,16 +5,13 @@ #include "ranges/ranges.h" static_assert(std::is_base_of_v, Ranges>); -static_assert(std::is_base_of_v, RangesUnion>); TEST(Ranges, check) { RangesUnion all_cases; - const auto ranges = BasicRanges::instance().fetch(); - for (const auto head : Heads) { - for (auto range : ranges) { + for (auto range : BasicRanges::instance().fetch()) { if (Ranges::check(head, range_reverse(range)) == 0) { - all_cases[head].emplace_back(range); // valid cases + all_cases[head].emplace_back(range); // found valid cases } } } @@ -25,13 +22,13 @@ TEST(Ranges, spawn) { for (auto [n, n_2x1, n_1x1] : BLOCK_NUM) { Ranges ranges; ranges.spawn(n, n_2x1, n_1x1); - EXPECT_SORTED_AND_UNIQUE(ranges); + EXPECT_SORTED_AND_UNIQUE(ranges); // sorted and unique for (const auto range : ranges) { const auto [val_1x1, val_1x2, val_2x1] = cal_block_num(range); EXPECT_EQ(val_1x1, n_1x1); EXPECT_EQ(val_2x1, n_2x1); - EXPECT_EQ(val_1x2 + val_2x1, n); + EXPECT_EQ(val_1x2 + val_2x1, n); // verify block nums } } } @@ -40,18 +37,18 @@ TEST(Ranges, derive) { for (auto [n, n_2x1, n_1x1] : BLOCK_NUM) { Ranges ranges; ranges.spawn(n, n_2x1, n_1x1); - ranges.reverse(); + ranges.reverse(); // reverse ranges for derive RangesUnion cases; for (const auto head : Heads) { ranges.derive(head, cases[head]); - EXPECT_SORTED_AND_UNIQUE(cases[head]); - EXPECT_COMMON_CODES(head, cases[head]); + EXPECT_SORTED_AND_UNIQUE(cases[head]); // sorted and unique + EXPECT_COMMON_CODES(head, cases[head]); // verify common codes } ranges.reverse(); for (const auto head : Heads) { - EXPECT_SUBSET(ranges, cases[head]); // subset verify + EXPECT_SUBSET(ranges, cases[head]); // derive ranges is subset } } } @@ -82,9 +79,8 @@ TEST(Ranges, combine) { for (auto [n, n_2x1, n_1x1] : BLOCK_NUM) { Ranges ranges; ranges.spawn(n, n_2x1, n_1x1); - // TODO: add += interface - all_ranges.insert(all_ranges.end(), ranges.begin(), ranges.end()); - ranges.reverse(); + all_ranges += ranges; + ranges.reverse(); // reverse ranges for derive for (const auto head : Heads) { ranges.derive(head, all_cases[head]); // derive from sub ranges } @@ -97,7 +93,7 @@ TEST(Ranges, combine) { EXPECT_EQ(all_ranges, BasicRanges::instance().fetch()); // verify content EXPECT_EQ(all_cases, AllCases::instance().fetch()); // verify content - all_ranges.reverse(); + all_ranges.reverse(); // reverse ranges for derive for (const auto head : Heads) { all_cases[head].clear(); all_ranges.derive(head, all_cases[head]); // derive from all ranges @@ -105,4 +101,11 @@ TEST(Ranges, combine) { EXPECT_EQ(all_cases, AllCases::instance().fetch()); // verify content } -// TODO: export CommonCode +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); +} diff --git a/src/core_test/cases/ranges_union.cc b/src/core_test/cases/ranges_union.cc index e69de29..f85b217 100644 --- a/src/core_test/cases/ranges_union.cc +++ b/src/core_test/cases/ranges_union.cc @@ -0,0 +1,24 @@ +#include + +#include "helper/cases.h" +#include "ranges/ranges.h" + +static_assert(std::is_base_of_v, RangesUnion>); + +// TODO: test CommonCode export + +TEST(RangesUnion, 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); + + RangesUnion r_, r1_, r2_; + for (const auto head : Heads) { + r.derive(head, r_[head]); + r1.derive(head, r1_[head]); + r2.derive(head, r2_[head]); + } + EXPECT_EQ(r_, r1_ += r2_); +}