Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use catch range matcher to check ranges equality #62

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 29 additions & 38 deletions tests/eptree_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

#include <catch2/catch_test_macros.hpp>
#include <catch2/matchers/catch_matchers_range_equals.hpp>

#include <algorithm>
#include <random>
Expand All @@ -18,6 +19,8 @@
#include "utils/test_free_blocks_allocator.h"
#include "utils/test_utils.h"

using Catch::Matchers::RangeEquals;

TEST_CASE("EPTreeTests") {
auto test_device = std::make_shared<TestBlocksDevice>();
auto allocator_block = TestBlock::LoadMetadataBlock(test_device, 0);
Expand All @@ -27,25 +30,19 @@ TEST_CASE("EPTreeTests") {
eptree.Init(/*block_number=*/0);

SECTION("insert items sorted") {
constexpr int kItemsCount = 600 * 300;
constexpr auto kItemsCount = 600 * 300ul;
for (uint32_t i = 0; i < kItemsCount; ++i) {
REQUIRE(eptree.insert({i, i + 1}));
}

REQUIRE(eptree.tree_header()->depth.value() == 3);

REQUIRE(std::ranges::equal(
std::views::transform(eptree,
[](const auto& extent) -> std::pair<uint32_t, uint32_t> {
return {extent.key, extent.value};
}),
std::views::transform(std::views::iota(0, kItemsCount), [](int i) -> std::pair<uint32_t, uint32_t> {
return {i, i + 1};
})));
CHECK(eptree.tree_header()->depth.value() == 3);
CHECK_THAT(eptree, RangeEquals(std::views::iota(0ul, kItemsCount), [](const auto& extent, uint32_t i) {
return extent.key == i && extent.value == i + 1;
}));
}

SECTION("insert items unsorted") {
constexpr int kItemsCount = 600 * 300;
constexpr auto kItemsCount = 600 * 300ul;
auto unsorted_keys = createShuffledKeysArray<kItemsCount>();
for (auto key : unsorted_keys) {
REQUIRE(eptree.insert({key, key + 1}));
Expand All @@ -55,25 +52,20 @@ TEST_CASE("EPTreeTests") {
auto keys = std::views::transform(eptree, [](const auto& extent) -> uint32_t { return extent.key; });
auto sorted_keys = unsorted_keys;
std::ranges::sort(sorted_keys);
REQUIRE(std::ranges::equal(sorted_keys, keys));
CHECK_THAT(keys, RangeEquals(sorted_keys));

// Check the values
REQUIRE(std::ranges::equal(
std::views::transform(eptree,
[](const auto& extent) -> std::pair<uint32_t, uint32_t> {
return {extent.key, extent.value};
}),
std::views::transform(std::views::iota(0, kItemsCount), [](int i) -> std::pair<uint32_t, uint32_t> {
return {i, i + 1};
})));
CHECK_THAT(eptree, RangeEquals(std::views::iota(0ul, kItemsCount), [](const auto& extent, uint32_t i) {
return extent.key == i && extent.value == i + 1;
}));

for (uint32_t i = 0; i < kItemsCount; ++i) {
REQUIRE(eptree.find(i, true)->key == i);
CHECK(eptree.find(i, true)->key == i);
}
}

SECTION("erase items randomly") {
constexpr int kItemsCount = 600 * 300;
constexpr auto kItemsCount = 600 * 300ul;
for (uint32_t i = 0; i < kItemsCount; ++i) {
REQUIRE(eptree.insert({i, 0}));
}
Expand All @@ -85,55 +77,54 @@ TEST_CASE("EPTreeTests") {
REQUIRE(eptree.erase(key, blocks_to_delete));
}

// Ensure that the right items were deleted
auto sorted_upper_half = std::ranges::subrange(middle, unsorted_keys.end()) | std::ranges::to<std::vector>();
std::ranges::sort(sorted_upper_half);
// Ensure that the right items were deleted
REQUIRE(std::ranges::equal(std::views::transform(eptree, [](const auto& extent) -> int { return extent.key; }),
sorted_upper_half));
CHECK_THAT(eptree, RangeEquals(sorted_upper_half, [](const auto& extent, auto key) { return extent.key == key; }));

// Remove the second half
for (auto key : std::ranges::subrange(middle, unsorted_keys.end())) {
REQUIRE(eptree.erase(key, blocks_to_delete));
}

// Should be empty
REQUIRE(eptree.begin() == eptree.end());
REQUIRE(eptree.tree_header()->depth.value() == 1);
CHECK(eptree.begin() == eptree.end());
CHECK(eptree.tree_header()->depth.value() == 1);
}

SECTION("check backward/forward iterator") {
constexpr int kItemsCount = 600 * 300;
constexpr auto kItemsCount = 600 * 300ul;
for (uint32_t i = 0; i < kItemsCount; ++i) {
REQUIRE(eptree.insert({i, i}));
}

auto it = eptree.begin();
uint32_t steps = 0;
while (it != eptree.end()) {
REQUIRE(it->key == steps);
CHECK(it->key == steps);
++it;
++steps;
}
REQUIRE(steps == kItemsCount);
REQUIRE(it.is_end());
CHECK(steps == kItemsCount);
CHECK(it.is_end());
while (it != eptree.begin()) {
--it;
--steps;
REQUIRE(it->key == steps);
CHECK(it->key == steps);
}
REQUIRE(steps == 0);
REQUIRE(it.is_begin());
CHECK(steps == 0);
CHECK(it.is_begin());

for (int i = 0; i < 40; ++i) {
++it;
++steps;
REQUIRE(it->key == steps);
CHECK(it->key == steps);
}
for (int i = 0; i < 20; ++i) {
--it;
--steps;
REQUIRE(it->key == steps);
CHECK(it->key == steps);
}
REQUIRE(it->key == 20);
CHECK(it->key == 20);
}
}
Loading
Loading