forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrtree_unittest.cc
97 lines (76 loc) · 2.2 KB
/
rtree_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "cc/base/rtree.h"
#include <stddef.h>
#include "testing/gtest/include/gtest/gtest.h"
namespace cc {
TEST(RTreeTest, NoOverlap) {
std::vector<gfx::Rect> rects;
for (int y = 0; y < 50; ++y) {
for (int x = 0; x < 50; ++x) {
rects.push_back(gfx::Rect(x, y, 1, 1));
}
}
RTree rtree;
rtree.Build(rects);
std::vector<size_t> results;
rtree.Search(gfx::Rect(0, 0, 50, 50), &results);
ASSERT_EQ(2500u, results.size());
for (size_t i = 0; i < 2500; ++i) {
ASSERT_EQ(results[i], i);
}
results.clear();
rtree.Search(gfx::Rect(0, 0, 50, 49), &results);
ASSERT_EQ(2450u, results.size());
for (size_t i = 0; i < 2450; ++i) {
ASSERT_EQ(results[i], i);
}
results.clear();
rtree.Search(gfx::Rect(5, 6, 1, 1), &results);
ASSERT_EQ(1u, results.size());
EXPECT_EQ(6u * 50 + 5u, results[0]);
}
TEST(RTreeTest, Overlap) {
std::vector<gfx::Rect> rects;
for (int h = 1; h <= 50; ++h) {
for (int w = 1; w <= 50; ++w) {
rects.push_back(gfx::Rect(0, 0, w, h));
}
}
RTree rtree;
rtree.Build(rects);
std::vector<size_t> results;
rtree.Search(gfx::Rect(0, 0, 1, 1), &results);
ASSERT_EQ(2500u, results.size());
for (size_t i = 0; i < 2500; ++i) {
ASSERT_EQ(results[i], i);
}
results.clear();
rtree.Search(gfx::Rect(0, 49, 1, 1), &results);
ASSERT_EQ(50u, results.size());
for (size_t i = 0; i < 50; ++i) {
EXPECT_EQ(results[i], 2450u + i);
}
}
TEST(RTreeTest, GetBoundsEmpty) {
RTree rtree;
ASSERT_EQ(gfx::Rect(), rtree.GetBounds());
}
TEST(RTreeTest, GetBoundsNonOverlapping) {
std::vector<gfx::Rect> rects;
rects.push_back(gfx::Rect(5, 6, 7, 8));
rects.push_back(gfx::Rect(11, 12, 13, 14));
RTree rtree;
rtree.Build(rects);
ASSERT_EQ(gfx::Rect(5, 6, 19, 20), rtree.GetBounds());
}
TEST(RTreeTest, GetBoundsOverlapping) {
std::vector<gfx::Rect> rects;
rects.push_back(gfx::Rect(0, 0, 10, 10));
rects.push_back(gfx::Rect(5, 5, 5, 5));
RTree rtree;
rtree.Build(rects);
ASSERT_EQ(gfx::Rect(0, 0, 10, 10), rtree.GetBounds());
}
} // namespace cc