Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(126)

Side by Side Diff: tests/RTreeTest.cpp

Issue 617393004: BBHs: void* data -> unsigned data (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: bench Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright 2012 Google Inc. 2 * Copyright 2012 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkRTree.h" 8 #include "SkRTree.h"
9 #include "SkRandom.h" 9 #include "SkRandom.h"
10 #include "SkTSort.h" 10 #include "SkTSort.h"
11 #include "Test.h" 11 #include "Test.h"
12 12
13 static const size_t MIN_CHILDREN = 6; 13 static const size_t MIN_CHILDREN = 6;
14 static const size_t MAX_CHILDREN = 11; 14 static const size_t MAX_CHILDREN = 11;
15 15
16 static const int NUM_RECTS = 200; 16 static const int NUM_RECTS = 200;
17 static const size_t NUM_ITERATIONS = 100; 17 static const size_t NUM_ITERATIONS = 100;
18 static const size_t NUM_QUERIES = 50; 18 static const size_t NUM_QUERIES = 50;
19 19
20 struct DataRect { 20 struct DataRect {
21 SkRect rect; 21 SkRect rect;
22 void* data; 22 unsigned data;
23 }; 23 };
24 24
25 static SkRect random_rect(SkRandom& rand) { 25 static SkRect random_rect(SkRandom& rand) {
26 SkRect rect = {0,0,0,0}; 26 SkRect rect = {0,0,0,0};
27 while (rect.isEmpty()) { 27 while (rect.isEmpty()) {
28 rect.fLeft = rand.nextRangeF(0, 1000); 28 rect.fLeft = rand.nextRangeF(0, 1000);
29 rect.fRight = rand.nextRangeF(0, 1000); 29 rect.fRight = rand.nextRangeF(0, 1000);
30 rect.fTop = rand.nextRangeF(0, 1000); 30 rect.fTop = rand.nextRangeF(0, 1000);
31 rect.fBottom = rand.nextRangeF(0, 1000); 31 rect.fBottom = rand.nextRangeF(0, 1000);
32 rect.sort(); 32 rect.sort();
33 } 33 }
34 return rect; 34 return rect;
35 } 35 }
36 36
37 static void random_data_rects(SkRandom& rand, DataRect out[], int n) { 37 static void random_data_rects(SkRandom& rand, DataRect out[], int n) {
38 for (int i = 0; i < n; ++i) { 38 for (int i = 0; i < n; ++i) {
39 out[i].rect = random_rect(rand); 39 out[i].rect = random_rect(rand);
40 out[i].data = reinterpret_cast<void*>(i); 40 out[i].data = (unsigned)i;
41 } 41 }
42 } 42 }
43 43
44 static bool verify_query(SkRect query, DataRect rects[], 44 static bool verify_query(SkRect query, DataRect rects[],
45 SkTDArray<void*>& found) { 45 SkTDArray<unsigned>& found) {
46 // TODO(mtklein): no need to do this after everything's SkRects 46 // TODO(mtklein): no need to do this after everything's SkRects
47 query.roundOut(); 47 query.roundOut();
48 48
49 SkTDArray<void*> expected; 49 SkTDArray<unsigned> expected;
50 50
51 // manually intersect with every rectangle 51 // manually intersect with every rectangle
52 for (int i = 0; i < NUM_RECTS; ++i) { 52 for (int i = 0; i < NUM_RECTS; ++i) {
53 if (SkRect::Intersects(query, rects[i].rect)) { 53 if (SkRect::Intersects(query, rects[i].rect)) {
54 expected.push(rects[i].data); 54 expected.push(rects[i].data);
55 } 55 }
56 } 56 }
57 57
58 if (expected.count() != found.count()) { 58 if (expected.count() != found.count()) {
59 return false; 59 return false;
60 } 60 }
61 61
62 if (0 == expected.count()) { 62 if (0 == expected.count()) {
63 return true; 63 return true;
64 } 64 }
65 65
robertphillips 2014/10/02 12:27:35 same here
mtklein 2014/10/02 14:32:09 Turns out this sort is needed! I reopened skia:28
66 // Just cast to long since sorting by the value of the void*'s was being pro blematic... 66 SkTQSort(expected.begin(), expected.end() -1);
67 SkTQSort(reinterpret_cast<long*>(expected.begin()), 67 SkTQSort(found.begin(), found.end() -1);
68 reinterpret_cast<long*>(expected.end() - 1));
69 SkTQSort(reinterpret_cast<long*>(found.begin()),
70 reinterpret_cast<long*>(found.end() - 1));
71 return found == expected; 68 return found == expected;
72 } 69 }
73 70
74 static void run_queries(skiatest::Reporter* reporter, SkRandom& rand, DataRect r ects[], 71 static void run_queries(skiatest::Reporter* reporter, SkRandom& rand, DataRect r ects[],
75 SkRTree& tree) { 72 SkRTree& tree) {
76 for (size_t i = 0; i < NUM_QUERIES; ++i) { 73 for (size_t i = 0; i < NUM_QUERIES; ++i) {
77 SkTDArray<void*> hits; 74 SkTDArray<unsigned> hits;
78 SkRect query = random_rect(rand); 75 SkRect query = random_rect(rand);
79 tree.search(query, &hits); 76 tree.search(query, &hits);
80 REPORTER_ASSERT(reporter, verify_query(query, rects, hits)); 77 REPORTER_ASSERT(reporter, verify_query(query, rects, hits));
81 } 78 }
82 } 79 }
83 80
84 static void rtree_test_main(SkRTree* rtree, skiatest::Reporter* reporter) { 81 static void rtree_test_main(SkRTree* rtree, skiatest::Reporter* reporter) {
85 DataRect rects[NUM_RECTS]; 82 DataRect rects[NUM_RECTS];
86 SkRandom rand; 83 SkRandom rand;
87 REPORTER_ASSERT(reporter, rtree); 84 REPORTER_ASSERT(reporter, rtree);
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 DEF_TEST(RTree, reporter) { 142 DEF_TEST(RTree, reporter) {
146 SkRTree* rtree = SkRTree::Create(MIN_CHILDREN, MAX_CHILDREN); 143 SkRTree* rtree = SkRTree::Create(MIN_CHILDREN, MAX_CHILDREN);
147 SkAutoUnref au(rtree); 144 SkAutoUnref au(rtree);
148 rtree_test_main(rtree, reporter); 145 rtree_test_main(rtree, reporter);
149 146
150 // Rtree that orders input rectangles on deferred insert. 147 // Rtree that orders input rectangles on deferred insert.
151 SkRTree* unsortedRtree = SkRTree::Create(MIN_CHILDREN, MAX_CHILDREN, 1, fals e); 148 SkRTree* unsortedRtree = SkRTree::Create(MIN_CHILDREN, MAX_CHILDREN, 1, fals e);
152 SkAutoUnref auo(unsortedRtree); 149 SkAutoUnref auo(unsortedRtree);
153 rtree_test_main(unsortedRtree, reporter); 150 rtree_test_main(unsortedRtree, reporter);
154 } 151 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698