Skip to content

Commit 03f32ab

Browse files
committed
added first gtests and cmake
1 parent c99ad84 commit 03f32ab

File tree

6 files changed

+168
-147
lines changed

6 files changed

+168
-147
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
*.swp
22
*.out
33
out/
4+
build/
5+
.vscode

CMakeLists.txt

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
cmake_minimum_required(VERSION 3.14)
2+
3+
project(RTree)
4+
5+
# GoogleTest requires at least C++14
6+
set(CMAKE_CXX_STANDARD 14)
7+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
8+
9+
include(FetchContent)
10+
FetchContent_Declare(
11+
googletest
12+
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
13+
DOWNLOAD_EXTRACT_TIMESTAMP false
14+
)
15+
# For Windows: Prevent overriding the parent project's compiler/linker settings
16+
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
17+
FetchContent_MakeAvailable(googletest)
18+
19+
enable_testing()
20+
21+
add_subdirectory(tests)

Test.cpp

-147
This file was deleted.

tests/.clang-format

Whitespace-only changes.

tests/CMakeLists.txt

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
add_executable(
2+
basics
3+
basics.cpp
4+
)
5+
6+
target_link_libraries(
7+
basics
8+
GTest::gtest_main
9+
GTest::gmock_main
10+
)
11+
12+
include(GoogleTest)
13+
gtest_discover_tests(basics)
14+

tests/basics.cpp

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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

Comments
 (0)