-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
124 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,59 @@ | ||
#include <ecs/detail/range_tree.h> | ||
#include <exception> | ||
#include <numeric> | ||
#include <catch2/catch_test_macros.hpp> | ||
#include <ranges> | ||
|
||
// Override the default handler for contract violations. | ||
#include "override_contract_handler_to_throw.h" | ||
// Make a vector of n ranges, with some gaps in between | ||
std::vector<ecs::entity_range> make_ranges(int n) { | ||
std::vector<ecs::entity_range> ranges; | ||
ranges.reserve(n); | ||
for (int i = 0, offset=0; i < n; i++) { | ||
ranges.emplace_back(offset, offset + 3 + (i&1)); | ||
offset = ranges.back().last() + 1; | ||
} | ||
return ranges; | ||
} | ||
|
||
// A bunch of tests to ensure that the range_tree behaves as expected | ||
TEST_CASE("range_tree specification") { | ||
auto const random_ranges = make_ranges(40); | ||
|
||
SECTION("A new range_tree is empty") { | ||
ecs::detail::range_tree tree; | ||
REQUIRE(tree.size() == 0ull); | ||
REQUIRE(tree.size() == 0); | ||
} | ||
|
||
SECTION("inserting ranges works") { | ||
ecs::detail::range_tree tree; | ||
tree.insert({1, 3}); | ||
tree.insert({6, 7}); | ||
tree.insert({-10, -6}); | ||
REQUIRE(tree.size() == 3ull); | ||
|
||
SECTION("overlap testing works") { | ||
ecs::detail::range_tree tree; | ||
tree.insert({1, 3}); | ||
tree.insert({6, 7}); | ||
tree.insert({-10, -6}); | ||
REQUIRE(tree.size() == 3); | ||
|
||
REQUIRE(tree.overlaps({-14, -5}) == true); | ||
REQUIRE(tree.overlaps({4, 5}) == false); | ||
REQUIRE(tree.overlaps({7, 9}) == true); | ||
} | ||
|
||
SECTION("tree can be iterated") { | ||
const std::vector<ecs::entity_range> expected{{-10, -6} ,{1, 3}, {6, 7}}; | ||
auto const expected = make_ranges(25); | ||
ecs::detail::range_tree tree; | ||
for (auto const range : expected | std::views::reverse) | ||
tree.insert(range); | ||
|
||
std::vector<ecs::entity_range> ranges; | ||
for(auto range : tree) { | ||
for(auto const range : tree) | ||
ranges.push_back(range); | ||
} | ||
|
||
REQUIRE(expected == ranges); | ||
} | ||
} | ||
|
||
SECTION("An empty pool") { | ||
SECTION("does not throw on bad component access") { | ||
} | ||
SECTION("balancing") { | ||
ecs::detail::range_tree tree; | ||
for (auto r : random_ranges) | ||
tree.insert(r); | ||
|
||
CHECK(tree.height() < (int)random_ranges.size()); | ||
} | ||
} |