blob: e74fd2802f86069ade8b6486aeacfa9990962881 [file] [log] [blame]
reed@google.com4c09d5c2011-02-22 13:16:38 +00001#include "Test.h"
2#include "SkDeque.h"
3
4static void assert_count(skiatest::Reporter* reporter, const SkDeque& deq, int count) {
5 if (0 == count) {
6 REPORTER_ASSERT(reporter, deq.empty());
7 REPORTER_ASSERT(reporter, 0 == deq.count());
8 REPORTER_ASSERT(reporter, sizeof(int) == deq.elemSize());
9 REPORTER_ASSERT(reporter, NULL == deq.front());
10 REPORTER_ASSERT(reporter, NULL == deq.back());
11 } else {
12 REPORTER_ASSERT(reporter, !deq.empty());
13 REPORTER_ASSERT(reporter, count == deq.count());
14 REPORTER_ASSERT(reporter, sizeof(int) == deq.elemSize());
15 REPORTER_ASSERT(reporter, NULL != deq.front());
16 REPORTER_ASSERT(reporter, NULL != deq.back());
17 if (1 == count) {
18 REPORTER_ASSERT(reporter, deq.back() == deq.front());
19 } else {
20 REPORTER_ASSERT(reporter, deq.back() != deq.front());
21 }
22 }
23}
24
25static void assert_f2biter(skiatest::Reporter* reporter, const SkDeque& deq,
26 int max, int min) {
27 SkDeque::F2BIter iter(deq);
28 void* ptr;
29
30 int value = max;
31 while ((ptr = iter.next()) != NULL) {
32 REPORTER_ASSERT(reporter, value == *(int*)ptr);
33 value -= 1;
34 }
35 REPORTER_ASSERT(reporter, value+1 == min);
36}
37
38static void TestDeque(skiatest::Reporter* reporter) {
39 SkDeque deq(sizeof(int));
40 int i;
41
reed@google.comf9e71322011-02-22 19:56:18 +000042 // test pushing on the front
43
reed@google.com4c09d5c2011-02-22 13:16:38 +000044 assert_count(reporter, deq, 0);
45 for (i = 1; i <= 10; i++) {
46 *(int*)deq.push_front() = i;
47 }
48 assert_count(reporter, deq, 10);
49 assert_f2biter(reporter, deq, 10, 1);
50
51 for (i = 0; i < 5; i++) {
52 deq.pop_front();
53 }
54 assert_count(reporter, deq, 5);
55 assert_f2biter(reporter, deq, 5, 1);
56
57 for (i = 0; i < 5; i++) {
58 deq.pop_front();
59 }
60 assert_count(reporter, deq, 0);
reed@google.comf9e71322011-02-22 19:56:18 +000061
62 // now test pushing on the back
63
64 for (i = 10; i >= 1; --i) {
65 *(int*)deq.push_back() = i;
66 }
67 assert_count(reporter, deq, 10);
68 assert_f2biter(reporter, deq, 10, 1);
69
70 for (i = 0; i < 5; i++) {
71 deq.pop_back();
72 }
73 assert_count(reporter, deq, 5);
74 assert_f2biter(reporter, deq, 10, 6);
75
76 for (i = 0; i < 5; i++) {
77 deq.pop_back();
78 }
79 assert_count(reporter, deq, 0);
80
81 // now tests pushing/poping on both ends
82
83 *(int*)deq.push_front() = 5;
84 *(int*)deq.push_back() = 4;
85 *(int*)deq.push_front() = 6;
86 *(int*)deq.push_back() = 3;
87 *(int*)deq.push_front() = 7;
88 *(int*)deq.push_back() = 2;
89 *(int*)deq.push_front() = 8;
90 *(int*)deq.push_back() = 1;
91 assert_count(reporter, deq, 8);
92 assert_f2biter(reporter, deq, 8, 1);
reed@google.com4c09d5c2011-02-22 13:16:38 +000093}
94
95#include "TestClassDef.h"
96DEFINE_TESTCLASS("Deque", TestDequeClass, TestDeque)