1
+ #include " ../RTree.h"
2
+ #include < gmock/gmock.h>
3
+ #include < gtest/gtest-matchers.h>
4
+ #include < gtest/gtest.h>
5
+ #include < iostream>
6
+ #include < limits>
7
+ #include < vector>
8
+
9
+ using namespace ::testing;
10
+
11
+ typedef int ValueType;
12
+
13
+ struct Rect {
14
+ Rect () {}
15
+
16
+ Rect (int a_minX, int a_minY, int a_maxX, int a_maxY) {
17
+ min[0 ] = a_minX;
18
+ min[1 ] = a_minY;
19
+
20
+ max[0 ] = a_maxX;
21
+ max[1 ] = a_maxY;
22
+ }
23
+
24
+ bool operator ==(const Rect &o) const {
25
+ return min[0 ] == o.min [0 ] && min[1 ] == o.min [1 ] && max[0 ] == o.max [0 ] &&
26
+ max[1 ] == o.max [1 ];
27
+ }
28
+
29
+ int min[2 ];
30
+ int max[2 ];
31
+ };
32
+
33
+ const std::vector<Rect > rects = {
34
+ Rect (0 , 0 , 2 , 2 ), // xmin, ymin, xmax, ymax (for 2 dimensional RTree)
35
+ Rect (5 , 5 , 7 , 7 ),
36
+ Rect (8 , 5 , 9 , 6 ),
37
+ Rect (7 , 1 , 9 , 2 ),
38
+ };
39
+
40
+ const std::vector<ValueType> values = {0 , 2 , 3 , 5 };
41
+
42
+ int nrects = rects.size();
43
+
44
+ Rect search_rect (6 , 4 , 10 ,
45
+ 6 ); // search will find above rects that this one overlaps
46
+
47
+ TEST (BasicTests, BasicTests) {
48
+ typedef RTree<ValueType, int , 2 , float > MyTree;
49
+ MyTree tree;
50
+
51
+ int i, nhits;
52
+
53
+ ASSERT_EQ (nrects, 4 );
54
+
55
+ for (i = 0 ; i < nrects; i++) {
56
+ tree.Insert (
57
+ rects[i].min , rects[i].max ,
58
+ values[i]); // Note, all values including zero are fine in this version
59
+ }
60
+
61
+ MockFunction<bool (ValueType)> mock_function;
62
+ ON_CALL (mock_function, Call).WillByDefault (Return (true ));
63
+ EXPECT_CALL (mock_function, Call (_)).Times (2 );
64
+
65
+ nhits = tree.Search (search_rect.min , search_rect.max ,
66
+ mock_function.AsStdFunction ());
67
+
68
+ ASSERT_EQ (nhits, 2 );
69
+
70
+ std::vector<Rect > collectedRects = std::vector<Rect >();
71
+
72
+ // Iterator test
73
+ int itIndex = 0 ;
74
+ MyTree::Iterator it;
75
+ for (tree.GetFirst (it); !tree.IsNull (it); tree.GetNext (it)) {
76
+ int value = tree.GetAt (it);
77
+
78
+ int boundsMin[2 ] = {0 , 0 };
79
+ int boundsMax[2 ] = {0 , 0 };
80
+ it.GetBounds (boundsMin, boundsMax);
81
+ collectedRects.push_back (
82
+ Rect (boundsMin[0 ], boundsMin[1 ], boundsMax[0 ], boundsMax[1 ]));
83
+ }
84
+
85
+ EXPECT_THAT (rects, UnorderedElementsAreArray (collectedRects));
86
+
87
+ std::vector<ValueType> collectedValues = std::vector<ValueType>();
88
+
89
+ // Iterator test, alternate syntax
90
+ itIndex = 0 ;
91
+ tree.GetFirst (it);
92
+ while (!it.IsNull ()) {
93
+ int value = *it;
94
+ ++it;
95
+ collectedValues.push_back (value);
96
+ }
97
+
98
+ EXPECT_THAT (values, UnorderedElementsAreArray (collectedValues));
99
+
100
+ // test copy constructor
101
+ MyTree copy = tree;
102
+
103
+ collectedRects.clear ();
104
+
105
+ // Iterator test
106
+ itIndex = 0 ;
107
+ for (copy.GetFirst (it); !copy.IsNull (it); copy.GetNext (it)) {
108
+ int value = copy.GetAt (it);
109
+
110
+ int boundsMin[2 ] = {0 , 0 };
111
+ int boundsMax[2 ] = {0 , 0 };
112
+ it.GetBounds (boundsMin, boundsMax);
113
+ collectedRects.push_back (
114
+ Rect (boundsMin[0 ], boundsMin[1 ], boundsMax[0 ], boundsMax[1 ]));
115
+ }
116
+
117
+ EXPECT_THAT (rects, UnorderedElementsAreArray (collectedRects));
118
+
119
+ collectedValues.clear ();
120
+
121
+ // Iterator test, alternate syntax
122
+ itIndex = 0 ;
123
+ copy.GetFirst (it);
124
+ while (!it.IsNull ()) {
125
+ int value = *it;
126
+ ++it;
127
+ collectedValues.push_back (value);
128
+ }
129
+
130
+ EXPECT_THAT (values, UnorderedElementsAreArray (collectedValues));
131
+ }
0 commit comments