Skip to content

Commit

Permalink
Fixed copy assign not working.
Browse files Browse the repository at this point in the history
  • Loading branch information
5cript committed Oct 21, 2024
1 parent 4b91cf4 commit d418856
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 2 deletions.
9 changes: 7 additions & 2 deletions include/interval-tree/interval_tree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,11 @@ namespace lib_interval_tree
return &interval_;
}

interval_type* interval()
{
return &interval_;
}

value_type max() const
{
return max_;
Expand Down Expand Up @@ -1058,7 +1063,7 @@ namespace lib_interval_tree
}
}();

node_type* x = [y](){
node_type* x = [y]() {
if (y->left_)
return y->left_;
else
Expand Down Expand Up @@ -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);
Expand Down
111 changes: 111 additions & 0 deletions tests/interval_tree_tests.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#pragma once

#include "test_utility.hpp"
#include "multi_join_interval.hpp"

#include <ctime>
#include <random>
#include <cmath>

class IntervalTreeTests : public ::testing::Test
{
public:
using types = IntervalTypes<int>;
};

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);
}
}
1 change: 1 addition & 0 deletions tests/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit d418856

Please sign in to comment.