Project
Loading...
Searching...
No Matches
SegmentTree.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
14
15#define BOOST_TEST_MODULE Test MCHContour SegmentTree
16#define BOOST_TEST_MAIN
17#define BOOST_TEST_DYN_LINK
18
19#include <boost/test/unit_test.hpp>
20#include <boost/test/data/test_case.hpp>
21#include <iostream>
22#include "../include/MCHContour/SegmentTree.h"
23
24using namespace o2::mch::contour::impl;
25
26struct YPOS {
27
29 {
30
31 auto* left = new Node<int>{Interval<int>{0, 4}, 2};
32 auto* right = new Node<int>{Interval<int>{4, 8}, 6};
33
35
36 left->setCardinality(dummyCardinality);
37 right->setCardinality(dummyCardinality);
38 }
39
40 std::vector<int> yposInt{0, 1, 2, 3, 4, 5, 6, 7, 8};
41 std::vector<double> yposDouble{0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8};
45};
46
47BOOST_AUTO_TEST_SUITE(o2_mch_contour)
48
49BOOST_FIXTURE_TEST_SUITE(segmenttree, YPOS)
50
51BOOST_AUTO_TEST_CASE(NeedAtLeastTwoValuesToBuildASegmentTree)
52{
53 std::vector<int> onlyOneElement{0};
54 BOOST_CHECK_THROW(createSegmentTree(onlyOneElement), std::invalid_argument);
55}
56
57BOOST_AUTO_TEST_CASE(NodeInsertAndDeleteIntVersion)
58{
59 std::unique_ptr<Node<int>> t{createSegmentTree(yposInt)};
60
61 t->insertInterval(Interval<int>{1, 5});
62 t->insertInterval(Interval<int>{5, 8});
63 t->deleteInterval(Interval<int>{6, 7});
64
65 std::ostringstream os;
66
67 os << '\n'
68 << (*t);
69
70 std::string expectedOutput =
71 R"(
72[0,8] potent
73 [0,4] potent
74 [0,2] potent
75 [0,1]
76 [1,2] C=1
77 [2,4] C=1
78 [2,3]
79 [3,4]
80 [4,8] potent
81 [4,6] C=1
82 [4,5]
83 [5,6]
84 [6,8] potent
85 [6,7]
86 [7,8] C=1
87)";
88
89 BOOST_CHECK_EQUAL(os.str(), expectedOutput);
90}
91
92BOOST_AUTO_TEST_CASE(NodeInsertAndDeleteDoubleVersion)
93{
94 std::unique_ptr<Node<double>> t{createSegmentTree(yposDouble)};
96 t->insertInterval(Interval<double>{0.1, 0.5});
97 t->insertInterval(Interval<double>{0.5, 0.8});
98 t->deleteInterval(Interval<double>{0.6, 0.7});
100 std::ostringstream os;
101
102 os << '\n'
103 << (*t);
104
105 std::string expectedOutput =
106 R"(
107[0,0.8] potent
108 [0,0.4] potent
109 [0,0.2] potent
110 [0,0.1]
111 [0.1,0.2] C=1
112 [0.2,0.4] C=1
113 [0.2,0.3]
114 [0.3,0.4]
115 [0.4,0.8] potent
116 [0.4,0.6] C=1
117 [0.4,0.5]
118 [0.5,0.6]
119 [0.6,0.8] potent
120 [0.6,0.7]
121 [0.7,0.8] C=1
122)";
123
124 BOOST_CHECK_EQUAL(os.str(), expectedOutput);
125}
126
127BOOST_AUTO_TEST_CASE(JustCreatedNodeIsNotPotent) { BOOST_CHECK_EQUAL(node.isPotent(), false); }
128
129BOOST_AUTO_TEST_CASE(JustCreatedNodeHasCardinalityEqualsZero) { BOOST_CHECK_EQUAL(node.cardinality(), 0); }
130
131BOOST_AUTO_TEST_CASE(PromoteNode)
132{
133 testNode.promote();
134
135 BOOST_CHECK_EQUAL(testNode.cardinality(), 1);
136 BOOST_CHECK_EQUAL(testNode.left()->cardinality(), dummyCardinality - 1);
137 BOOST_CHECK_EQUAL(testNode.right()->cardinality(), dummyCardinality - 1);
138}
139
140BOOST_AUTO_TEST_CASE(DemoteNode)
141{
142 testNode.promote();
143 testNode.demote();
144 BOOST_CHECK_EQUAL(testNode.cardinality(), 0);
145 BOOST_CHECK_EQUAL(testNode.left()->cardinality(), dummyCardinality);
146 BOOST_CHECK_EQUAL(testNode.right()->cardinality(), dummyCardinality);
147 BOOST_CHECK_EQUAL(testNode.isPotent(), true);
148}
149
150BOOST_AUTO_TEST_CASE(MidPointOfANodeIsNotHalfPoint)
151{
152 std::vector<double> ypos{-2.0, -1.5, -1, 0};
153 std::unique_ptr<Node<double>> root{createSegmentTree(ypos)};
154 auto right = root->right();
155 BOOST_CHECK_EQUAL(right->interval(), Interval<double>(-1.5, 0));
156 BOOST_CHECK(right->midpoint() != 1.5 / 2);
157 BOOST_CHECK_EQUAL(right->midpoint(), -1);
158}
159
160BOOST_AUTO_TEST_SUITE_END()
161BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(NeedAtLeastTwoValuesToBuildASegmentTree)
Node & setLeft(Node *left)
Definition SegmentTree.h:83
Node & setRight(Node *right)
Definition SegmentTree.h:89
GLdouble GLdouble right
Definition glcorearb.h:4077
Node< T > * createSegmentTree(std::vector< T > values)
std::vector< int > yposInt
Node< int > node
std::vector< double > yposDouble
Node< int > testNode
int dummyCardinality
BOOST_CHECK(tree)
BOOST_CHECK_EQUAL(triggersD.size(), triggers.size())