blob: d3e1c175feb7d5fb476029503294352b2d86c25a [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 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 */
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00007
reed@google.com4c09d5c2011-02-22 13:16:38 +00008#include "Test.h"
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00009#include "TestClassDef.h"
reed@google.com4c09d5c2011-02-22 13:16:38 +000010#include "SkDeque.h"
11
12static void assert_count(skiatest::Reporter* reporter, const SkDeque& deq, int count) {
13 if (0 == count) {
14 REPORTER_ASSERT(reporter, deq.empty());
15 REPORTER_ASSERT(reporter, 0 == deq.count());
16 REPORTER_ASSERT(reporter, sizeof(int) == deq.elemSize());
17 REPORTER_ASSERT(reporter, NULL == deq.front());
18 REPORTER_ASSERT(reporter, NULL == deq.back());
19 } else {
20 REPORTER_ASSERT(reporter, !deq.empty());
21 REPORTER_ASSERT(reporter, count == deq.count());
22 REPORTER_ASSERT(reporter, sizeof(int) == deq.elemSize());
23 REPORTER_ASSERT(reporter, NULL != deq.front());
24 REPORTER_ASSERT(reporter, NULL != deq.back());
25 if (1 == count) {
26 REPORTER_ASSERT(reporter, deq.back() == deq.front());
27 } else {
28 REPORTER_ASSERT(reporter, deq.back() != deq.front());
29 }
30 }
31}
32
robertphillips@google.com0a78b0f2012-07-16 16:58:49 +000033static void assert_iter(skiatest::Reporter* reporter, const SkDeque& deq,
34 int max, int min) {
35 // test forward iteration
36 SkDeque::Iter iter(deq, SkDeque::Iter::kFront_IterStart);
reed@google.com4c09d5c2011-02-22 13:16:38 +000037 void* ptr;
38
39 int value = max;
robertphillips@google.com0a78b0f2012-07-16 16:58:49 +000040 while (NULL != (ptr = iter.next())) {
reed@google.com4c09d5c2011-02-22 13:16:38 +000041 REPORTER_ASSERT(reporter, value == *(int*)ptr);
42 value -= 1;
43 }
44 REPORTER_ASSERT(reporter, value+1 == min);
robertphillips@google.com0a78b0f2012-07-16 16:58:49 +000045
46 // test reverse iteration
47 iter.reset(deq, SkDeque::Iter::kBack_IterStart);
48
49 value = min;
50 while (NULL != (ptr = iter.prev())) {
51 REPORTER_ASSERT(reporter, value == *(int*)ptr);
52 value += 1;
53 }
54 REPORTER_ASSERT(reporter, value-1 == max);
55
56 // test mixed iteration
57 iter.reset(deq, SkDeque::Iter::kFront_IterStart);
58
59 value = max;
60 // forward iteration half-way
61 for (int i = 0; i < deq.count()/2 && NULL != (ptr = iter.next()); i++) {
62 REPORTER_ASSERT(reporter, value == *(int*)ptr);
63 value -= 1;
64 }
65 // then back down w/ reverse iteration
66 while (NULL != (ptr = iter.prev())) {
67 REPORTER_ASSERT(reporter, value == *(int*)ptr);
68 value += 1;
69 }
70 REPORTER_ASSERT(reporter, value-1 == max);
reed@google.com4c09d5c2011-02-22 13:16:38 +000071}
72
robertphillips@google.com0a78b0f2012-07-16 16:58:49 +000073// This helper is intended to only give the unit test access to SkDeque's
74// private numBlocksAllocated method
75class DequeUnitTestHelper {
76public:
77 int fNumBlocksAllocated;
78
79 DequeUnitTestHelper(const SkDeque& deq) {
80 fNumBlocksAllocated = deq.numBlocksAllocated();
81 }
82};
83
rmistry@google.comd6176b02012-08-23 18:14:13 +000084static void assert_blocks(skiatest::Reporter* reporter,
robertphillips@google.com0a78b0f2012-07-16 16:58:49 +000085 const SkDeque& deq,
86 int allocCount) {
rmistry@google.comd6176b02012-08-23 18:14:13 +000087 DequeUnitTestHelper helper(deq);
robertphillips@google.com0a78b0f2012-07-16 16:58:49 +000088
89 if (0 == deq.count()) {
90 REPORTER_ASSERT(reporter, 1 == helper.fNumBlocksAllocated);
91 } else {
92 int expected = (deq.count() + allocCount - 1) / allocCount;
93 // A block isn't freed in the deque when it first becomes empty so
94 // sometimes an extra block lingers around
rmistry@google.comd6176b02012-08-23 18:14:13 +000095 REPORTER_ASSERT(reporter,
96 expected == helper.fNumBlocksAllocated ||
robertphillips@google.com0a78b0f2012-07-16 16:58:49 +000097 expected+1 == helper.fNumBlocksAllocated);
98 }
99}
100
robertphillips@google.com2e41bec2012-07-16 17:19:21 +0000101static void TestSub(skiatest::Reporter* reporter, int allocCount) {
robertphillips@google.com0a78b0f2012-07-16 16:58:49 +0000102 SkDeque deq(sizeof(int), allocCount);
reed@google.com4c09d5c2011-02-22 13:16:38 +0000103 int i;
104
reed@google.comf9e71322011-02-22 19:56:18 +0000105 // test pushing on the front
106
reed@google.com4c09d5c2011-02-22 13:16:38 +0000107 assert_count(reporter, deq, 0);
108 for (i = 1; i <= 10; i++) {
109 *(int*)deq.push_front() = i;
110 }
111 assert_count(reporter, deq, 10);
robertphillips@google.com0a78b0f2012-07-16 16:58:49 +0000112 assert_iter(reporter, deq, 10, 1);
113 assert_blocks(reporter, deq, allocCount);
reed@google.com4c09d5c2011-02-22 13:16:38 +0000114
115 for (i = 0; i < 5; i++) {
116 deq.pop_front();
117 }
118 assert_count(reporter, deq, 5);
robertphillips@google.com0a78b0f2012-07-16 16:58:49 +0000119 assert_iter(reporter, deq, 5, 1);
120 assert_blocks(reporter, deq, allocCount);
reed@google.com4c09d5c2011-02-22 13:16:38 +0000121
122 for (i = 0; i < 5; i++) {
123 deq.pop_front();
124 }
125 assert_count(reporter, deq, 0);
robertphillips@google.com0a78b0f2012-07-16 16:58:49 +0000126 assert_blocks(reporter, deq, allocCount);
reed@google.comf9e71322011-02-22 19:56:18 +0000127
128 // now test pushing on the back
129
130 for (i = 10; i >= 1; --i) {
131 *(int*)deq.push_back() = i;
132 }
133 assert_count(reporter, deq, 10);
robertphillips@google.com0a78b0f2012-07-16 16:58:49 +0000134 assert_iter(reporter, deq, 10, 1);
135 assert_blocks(reporter, deq, allocCount);
reed@google.comf9e71322011-02-22 19:56:18 +0000136
137 for (i = 0; i < 5; i++) {
138 deq.pop_back();
139 }
140 assert_count(reporter, deq, 5);
robertphillips@google.com0a78b0f2012-07-16 16:58:49 +0000141 assert_iter(reporter, deq, 10, 6);
142 assert_blocks(reporter, deq, allocCount);
reed@google.comf9e71322011-02-22 19:56:18 +0000143
144 for (i = 0; i < 5; i++) {
145 deq.pop_back();
146 }
147 assert_count(reporter, deq, 0);
robertphillips@google.com0a78b0f2012-07-16 16:58:49 +0000148 assert_blocks(reporter, deq, allocCount);
reed@google.comf9e71322011-02-22 19:56:18 +0000149
robertphillips@google.com0a78b0f2012-07-16 16:58:49 +0000150 // now test pushing/popping on both ends
reed@google.comf9e71322011-02-22 19:56:18 +0000151
152 *(int*)deq.push_front() = 5;
153 *(int*)deq.push_back() = 4;
154 *(int*)deq.push_front() = 6;
155 *(int*)deq.push_back() = 3;
156 *(int*)deq.push_front() = 7;
157 *(int*)deq.push_back() = 2;
158 *(int*)deq.push_front() = 8;
159 *(int*)deq.push_back() = 1;
160 assert_count(reporter, deq, 8);
robertphillips@google.com0a78b0f2012-07-16 16:58:49 +0000161 assert_iter(reporter, deq, 8, 1);
162 assert_blocks(reporter, deq, allocCount);
reed@google.com4c09d5c2011-02-22 13:16:38 +0000163}
164
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000165DEF_TEST(Deque, reporter) {
robertphillips@google.com0a78b0f2012-07-16 16:58:49 +0000166 // test it once with the default allocation count
robertphillips@google.com2e41bec2012-07-16 17:19:21 +0000167 TestSub(reporter, 1);
robertphillips@google.com0a78b0f2012-07-16 16:58:49 +0000168 // test it again with a generous allocation count
robertphillips@google.com2e41bec2012-07-16 17:19:21 +0000169 TestSub(reporter, 10);
robertphillips@google.com0a78b0f2012-07-16 16:58:49 +0000170}