From d418856dcc44323a266b9a4dd7a3db9e39d958b1 Mon Sep 17 00:00:00 2001 From: Tim Ebbeke Date: Mon, 21 Oct 2024 16:50:24 +0200 Subject: [PATCH] Fixed copy assign not working. --- include/interval-tree/interval_tree.hpp | 9 +- tests/interval_tree_tests.hpp | 111 ++++++++++++++++++++++++ tests/tests.cpp | 1 + 3 files changed, 119 insertions(+), 2 deletions(-) create mode 100644 tests/interval_tree_tests.hpp diff --git a/include/interval-tree/interval_tree.hpp b/include/interval-tree/interval_tree.hpp index a1fc0ab..2b283c5 100644 --- a/include/interval-tree/interval_tree.hpp +++ b/include/interval-tree/interval_tree.hpp @@ -393,6 +393,11 @@ namespace lib_interval_tree return &interval_; } + interval_type* interval() + { + return &interval_; + } + value_type max() const { return max_; @@ -1058,7 +1063,7 @@ namespace lib_interval_tree } }(); - node_type* x = [y](){ + node_type* x = [y]() { if (y->left_) return y->left_; else @@ -1468,7 +1473,7 @@ namespace lib_interval_tree { if (root) { - auto* cpy = new node_type(parent, root->interval()); + auto* cpy = new node_type(parent, *root->interval()); cpy->color_ = root->color_; cpy->max_ = root->max_; cpy->left_ = copy_tree_impl(root->left_, cpy); diff --git a/tests/interval_tree_tests.hpp b/tests/interval_tree_tests.hpp new file mode 100644 index 0000000..404cb4e --- /dev/null +++ b/tests/interval_tree_tests.hpp @@ -0,0 +1,111 @@ +#pragma once + +#include "test_utility.hpp" +#include "multi_join_interval.hpp" + +#include +#include +#include + +class IntervalTreeTests : public ::testing::Test +{ + public: + using types = IntervalTypes; +}; + +TEST_F(IntervalTreeTests, CanCopyConstruct) +{ + auto tree = types::tree_type{}; + tree.insert(types::interval_type{0, 5}); + auto tree2 = tree; + EXPECT_EQ(tree2.size(), 1); + EXPECT_EQ(tree.size(), 1); + EXPECT_EQ(tree2.begin()->low(), 0); + EXPECT_EQ(tree2.begin()->high(), 5); +} + +TEST_F(IntervalTreeTests, CanMoveConstruct) +{ + auto tree = types::tree_type{}; + tree.insert(types::interval_type{0, 5}); + auto tree2 = std::move(tree); + EXPECT_EQ(tree2.size(), 1); + EXPECT_EQ(tree.size(), 0); + EXPECT_EQ(tree2.begin()->low(), 0); + EXPECT_EQ(tree2.begin()->high(), 5); +} + +TEST_F(IntervalTreeTests, CanCopyAssign) +{ + auto tree = types::tree_type{}; + tree.insert(types::interval_type{0, 5}); + auto tree2 = types::tree_type{}; + tree2.insert(types::interval_type{5, 10}); + tree2.insert(types::interval_type{1, 2}); + tree2 = tree; + EXPECT_EQ(tree2.size(), 1); + EXPECT_EQ(tree.size(), 1); + EXPECT_EQ(tree2.begin()->low(), 0); + EXPECT_EQ(tree2.begin()->high(), 5); +} + +TEST_F(IntervalTreeTests, CanMoveAssign) +{ + auto tree = types::tree_type{}; + tree.insert(types::interval_type{0, 5}); + auto tree2 = types::tree_type{}; + tree2.insert(types::interval_type{5, 10}); + tree2.insert(types::interval_type{1, 2}); + tree2 = std::move(tree); + EXPECT_EQ(tree2.size(), 1); + EXPECT_EQ(tree.size(), 0); + EXPECT_EQ(tree2.begin()->low(), 0); + EXPECT_EQ(tree2.begin()->high(), 5); +} + +TEST_F(IntervalTreeTests, CanCopyBiggerTree) +{ + auto tree = types::tree_type{}; + tree.insert(types::interval_type{0, 5}); + tree.insert(types::interval_type{5, 10}); + tree.insert(types::interval_type{10, 15}); + tree.insert(types::interval_type{15, 20}); + tree.insert(types::interval_type{20, 25}); + tree.insert(types::interval_type{25, 30}); + tree.insert(types::interval_type{30, 35}); + tree.insert(types::interval_type{35, 40}); + tree.insert(types::interval_type{40, 45}); + tree.insert(types::interval_type{45, 50}); + auto tree2 = tree; + EXPECT_EQ(tree2.size(), 10); + EXPECT_EQ(tree.size(), 10); + + for (auto i = tree.begin(), j = tree2.begin(); i != tree.end(); ++i, ++j) + { + EXPECT_EQ(i->low(), j->low()); + EXPECT_EQ(i->high(), j->high()); + } +} + +TEST_F(IntervalTreeTests, CanMoveBiggerTree) +{ + auto tree = types::tree_type{}; + tree.insert(types::interval_type{0, 5}); + tree.insert(types::interval_type{5, 10}); + tree.insert(types::interval_type{10, 15}); + tree.insert(types::interval_type{15, 20}); + tree.insert(types::interval_type{20, 25}); + tree.insert(types::interval_type{25, 30}); + tree.insert(types::interval_type{30, 35}); + tree.insert(types::interval_type{35, 40}); + tree.insert(types::interval_type{40, 45}); + tree.insert(types::interval_type{45, 50}); + auto tree2 = std::move(tree); + EXPECT_EQ(tree2.size(), 10); + EXPECT_EQ(tree.size(), 0); + + for (auto i = tree2.begin(); i != tree2.end(); ++i) + { + EXPECT_EQ(i->low(), i->high() - 5); + } +} \ No newline at end of file diff --git a/tests/tests.cpp b/tests/tests.cpp index aa56f8c..def3a02 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -6,6 +6,7 @@ // following headers expect to be included after gtest headers and interval_tree #include "interval_tests.hpp" +#include "interval_tree_tests.hpp" #include "insert_tests.hpp" #include "erase_tests.hpp" #include "find_tests.hpp"