OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2012 Google Inc. | 3 * Copyright 2012 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 #include "Test.h" | 9 #include "Test.h" |
10 #include "SkRandom.h" | 10 #include "SkRandom.h" |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 } | 61 } |
62 | 62 |
63 // Just cast to long since sorting by the value of the void*'s was being pro
blematic... | 63 // Just cast to long since sorting by the value of the void*'s was being pro
blematic... |
64 SkTQSort(reinterpret_cast<long*>(expected.begin()), | 64 SkTQSort(reinterpret_cast<long*>(expected.begin()), |
65 reinterpret_cast<long*>(expected.end() - 1)); | 65 reinterpret_cast<long*>(expected.end() - 1)); |
66 SkTQSort(reinterpret_cast<long*>(found.begin()), | 66 SkTQSort(reinterpret_cast<long*>(found.begin()), |
67 reinterpret_cast<long*>(found.end() - 1)); | 67 reinterpret_cast<long*>(found.end() - 1)); |
68 return found == expected; | 68 return found == expected; |
69 } | 69 } |
70 | 70 |
71 static void runQueries(skiatest::Reporter* reporter, SkMWCRandom& rand, DataRect
rects[], | 71 static void run_queries(skiatest::Reporter* reporter, SkMWCRandom& rand, DataRec
t rects[], |
72 SkRTree& tree) { | 72 SkRTree& tree) { |
73 for (size_t i = 0; i < NUM_QUERIES; ++i) { | 73 for (size_t i = 0; i < NUM_QUERIES; ++i) { |
74 SkTDArray<void*> hits; | 74 SkTDArray<void*> hits; |
75 SkIRect query = random_rect(rand); | 75 SkIRect query = random_rect(rand); |
76 tree.search(query, &hits); | 76 tree.search(query, &hits); |
77 REPORTER_ASSERT(reporter, verify_query(query, rects, hits)); | 77 REPORTER_ASSERT(reporter, verify_query(query, rects, hits)); |
78 } | 78 } |
79 } | 79 } |
80 | 80 |
81 static void TestRTree(skiatest::Reporter* reporter) { | 81 static void rtree_test_main(SkRTree* rtree, skiatest::Reporter* reporter) { |
82 DataRect rects[NUM_RECTS]; | 82 DataRect rects[NUM_RECTS]; |
83 SkMWCRandom rand; | 83 SkMWCRandom rand; |
84 SkRTree* rtree = SkRTree::Create(MIN_CHILDREN, MAX_CHILDREN); | |
85 SkAutoUnref au(rtree); | |
86 REPORTER_ASSERT(reporter, NULL != rtree); | 84 REPORTER_ASSERT(reporter, NULL != rtree); |
87 | 85 |
88 int expectedDepthMin = -1; | 86 int expectedDepthMin = -1; |
89 int expectedDepthMax = -1; | 87 int expectedDepthMax = -1; |
90 | 88 |
91 int tmp = NUM_RECTS; | 89 int tmp = NUM_RECTS; |
92 while (tmp > 0) { | 90 while (tmp > 0) { |
93 tmp -= static_cast<int>(pow(static_cast<double>(MAX_CHILDREN), | 91 tmp -= static_cast<int>(pow(static_cast<double>(MAX_CHILDREN), |
94 static_cast<double>(expectedDepthMin + 1))); | 92 static_cast<double>(expectedDepthMin + 1))); |
95 ++expectedDepthMin; | 93 ++expectedDepthMin; |
96 } | 94 } |
97 | 95 |
98 tmp = NUM_RECTS; | 96 tmp = NUM_RECTS; |
99 while (tmp > 0) { | 97 while (tmp > 0) { |
100 tmp -= static_cast<int>(pow(static_cast<double>(MIN_CHILDREN), | 98 tmp -= static_cast<int>(pow(static_cast<double>(MIN_CHILDREN), |
101 static_cast<double>(expectedDepthMax + 1))); | 99 static_cast<double>(expectedDepthMax + 1))); |
102 ++expectedDepthMax; | 100 ++expectedDepthMax; |
103 } | 101 } |
104 | 102 |
105 for (size_t i = 0; i < NUM_ITERATIONS; ++i) { | 103 for (size_t i = 0; i < NUM_ITERATIONS; ++i) { |
106 random_data_rects(rand, rects, NUM_RECTS); | 104 random_data_rects(rand, rects, NUM_RECTS); |
107 | 105 |
108 // First try bulk-loaded inserts | 106 // First try bulk-loaded inserts |
109 for (int i = 0; i < NUM_RECTS; ++i) { | 107 for (int i = 0; i < NUM_RECTS; ++i) { |
110 rtree->insert(rects[i].data, rects[i].rect, true); | 108 rtree->insert(rects[i].data, rects[i].rect, true); |
111 } | 109 } |
112 rtree->flushDeferredInserts(); | 110 rtree->flushDeferredInserts(); |
113 runQueries(reporter, rand, rects, *rtree); | 111 run_queries(reporter, rand, rects, *rtree); |
114 REPORTER_ASSERT(reporter, NUM_RECTS == rtree->getCount()); | 112 REPORTER_ASSERT(reporter, NUM_RECTS == rtree->getCount()); |
115 REPORTER_ASSERT(reporter, expectedDepthMin <= rtree->getDepth() && | 113 REPORTER_ASSERT(reporter, expectedDepthMin <= rtree->getDepth() && |
116 expectedDepthMax >= rtree->getDepth()); | 114 expectedDepthMax >= rtree->getDepth()); |
117 rtree->clear(); | 115 rtree->clear(); |
118 REPORTER_ASSERT(reporter, 0 == rtree->getCount()); | 116 REPORTER_ASSERT(reporter, 0 == rtree->getCount()); |
119 | 117 |
120 // Then try immediate inserts | 118 // Then try immediate inserts |
121 for (int i = 0; i < NUM_RECTS; ++i) { | 119 for (int i = 0; i < NUM_RECTS; ++i) { |
122 rtree->insert(rects[i].data, rects[i].rect); | 120 rtree->insert(rects[i].data, rects[i].rect); |
123 } | 121 } |
124 runQueries(reporter, rand, rects, *rtree); | 122 run_queries(reporter, rand, rects, *rtree); |
125 REPORTER_ASSERT(reporter, NUM_RECTS == rtree->getCount()); | 123 REPORTER_ASSERT(reporter, NUM_RECTS == rtree->getCount()); |
126 REPORTER_ASSERT(reporter, expectedDepthMin <= rtree->getDepth() && | 124 REPORTER_ASSERT(reporter, expectedDepthMin <= rtree->getDepth() && |
127 expectedDepthMax >= rtree->getDepth()); | 125 expectedDepthMax >= rtree->getDepth()); |
128 rtree->clear(); | 126 rtree->clear(); |
129 REPORTER_ASSERT(reporter, 0 == rtree->getCount()); | 127 REPORTER_ASSERT(reporter, 0 == rtree->getCount()); |
130 | 128 |
131 // And for good measure try immediate inserts, but in reversed order | 129 // And for good measure try immediate inserts, but in reversed order |
132 for (int i = NUM_RECTS - 1; i >= 0; --i) { | 130 for (int i = NUM_RECTS - 1; i >= 0; --i) { |
133 rtree->insert(rects[i].data, rects[i].rect); | 131 rtree->insert(rects[i].data, rects[i].rect); |
134 } | 132 } |
135 runQueries(reporter, rand, rects, *rtree); | 133 run_queries(reporter, rand, rects, *rtree); |
136 REPORTER_ASSERT(reporter, NUM_RECTS == rtree->getCount()); | 134 REPORTER_ASSERT(reporter, NUM_RECTS == rtree->getCount()); |
137 REPORTER_ASSERT(reporter, expectedDepthMin <= rtree->getDepth() && | 135 REPORTER_ASSERT(reporter, expectedDepthMin <= rtree->getDepth() && |
138 expectedDepthMax >= rtree->getDepth()); | 136 expectedDepthMax >= rtree->getDepth()); |
139 rtree->clear(); | 137 rtree->clear(); |
140 REPORTER_ASSERT(reporter, 0 == rtree->getCount()); | 138 REPORTER_ASSERT(reporter, 0 == rtree->getCount()); |
141 } | 139 } |
142 } | 140 } |
143 | 141 |
| 142 static void test_rtree(skiatest::Reporter* reporter) { |
| 143 SkRTree* rtree = SkRTree::Create(MIN_CHILDREN, MAX_CHILDREN); |
| 144 SkAutoUnref au(rtree); |
| 145 rtree_test_main(rtree, reporter); |
| 146 |
| 147 // Rtree that orders input rectangles on deferred insert. |
| 148 SkRTree* unsortedRtree = SkRTree::Create(MIN_CHILDREN, MAX_CHILDREN, 1, fals
e); |
| 149 SkAutoUnref auo(unsortedRtree); |
| 150 rtree_test_main(unsortedRtree, reporter); |
| 151 } |
| 152 |
| 153 |
144 #include "TestClassDef.h" | 154 #include "TestClassDef.h" |
145 DEFINE_TESTCLASS("RTree", RTreeTestClass, TestRTree) | 155 DEFINE_TESTCLASS("RTree", RTreeTestClass, test_rtree) |
OLD | NEW |