Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
RTreeTest.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
11#include "src/base/SkRandom.h"
12#include "src/core/SkRTree.h"
13#include "tests/Test.h"
14
15#include <cmath>
16#include <cstddef>
17#include <vector>
18
19using namespace skia_private;
20
21static const int NUM_RECTS = 200;
22static const size_t NUM_ITERATIONS = 100;
23static const size_t NUM_QUERIES = 50;
24
26 SkRect rect = {0,0,0,0};
27 while (rect.isEmpty()) {
28 rect.fLeft = rand.nextRangeF(0, 1000);
29 rect.fRight = rand.nextRangeF(0, 1000);
30 rect.fTop = rand.nextRangeF(0, 1000);
31 rect.fBottom = rand.nextRangeF(0, 1000);
32 rect.sort();
33 }
34 return rect;
35}
36
37static bool verify_query(SkRect query, SkRect rects[], const std::vector<int>& found) {
38 std::vector<int> expected;
39 // manually intersect with every rectangle
40 for (int i = 0; i < NUM_RECTS; ++i) {
41 if (SkRect::Intersects(query, rects[i])) {
42 expected.push_back(i);
43 }
44 }
45
46 if (expected.size() != found.size()) {
47 return false;
48 }
49 if (0 == expected.size()) {
50 return true;
51 }
52 return found == expected;
53}
54
56 const SkRTree& tree) {
57 for (size_t i = 0; i < NUM_QUERIES; ++i) {
58 std::vector<int> hits;
59 SkRect query = random_rect(rand);
60 tree.search(query, &hits);
61 REPORTER_ASSERT(reporter, verify_query(query, rects, hits));
62 }
63}
64
66 int expectedDepthMin = -1;
67 int tmp = NUM_RECTS;
68 while (tmp > 0) {
69 tmp -= static_cast<int>(pow(static_cast<double>(SkRTree::kMaxChildren),
70 static_cast<double>(expectedDepthMin + 1)));
71 ++expectedDepthMin;
72 }
73
74 int expectedDepthMax = -1;
75 tmp = NUM_RECTS;
76 while (tmp > 0) {
77 tmp -= static_cast<int>(pow(static_cast<double>(SkRTree::kMinChildren),
78 static_cast<double>(expectedDepthMax + 1)));
79 ++expectedDepthMax;
80 }
81
82 SkRandom rand;
84 for (size_t i = 0; i < NUM_ITERATIONS; ++i) {
85 SkRTree rtree;
86 REPORTER_ASSERT(reporter, 0 == rtree.getCount());
87
88 for (int j = 0; j < NUM_RECTS; j++) {
89 rects[j] = random_rect(rand);
90 }
91
92 rtree.insert(rects.data(), NUM_RECTS);
93
94 run_queries(reporter, rand, rects.data(), rtree);
96 REPORTER_ASSERT(reporter, expectedDepthMin <= rtree.getDepth() &&
97 expectedDepthMax >= rtree.getDepth());
98 }
99}
reporter
static const size_t NUM_QUERIES
Definition RTreeTest.cpp:23
static SkRect random_rect(SkRandom &rand)
Definition RTreeTest.cpp:25
static const int NUM_RECTS
Definition RTreeTest.cpp:21
static void run_queries(skiatest::Reporter *reporter, SkRandom &rand, SkRect rects[], const SkRTree &tree)
Definition RTreeTest.cpp:55
static const size_t NUM_ITERATIONS
Definition RTreeTest.cpp:22
static bool verify_query(SkRect query, SkRect rects[], const std::vector< int > &found)
Definition RTreeTest.cpp:37
#define DEF_TEST(name, reporter)
Definition Test.h:312
#define REPORTER_ASSERT(r, cond,...)
Definition Test.h:286
void insert(const SkRect[], int N) override
Definition SkRTree.cpp:15
static const int kMinChildren
Definition SkRTree.h:50
int getDepth() const
Definition SkRTree.h:45
void search(const SkRect &query, std::vector< int > *results) const override
Definition SkRTree.cpp:147
int getCount() const
Definition SkRTree.h:47
static const int kMaxChildren
Definition SkRTree.h:51
float nextRangeF(float min, float max)
Definition SkRandom.h:64
const T * data() const