/src/mozilla-central/xpcom/tests/gtest/TestObserverArray.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | // vim:cindent:ts=4:et:sw=4: |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #include "nsTObserverArray.h" |
8 | | #include "gtest/gtest.h" |
9 | | #include "mozilla/ArrayUtils.h" |
10 | | |
11 | | using namespace mozilla; |
12 | | |
13 | | typedef nsTObserverArray<int> IntArray; |
14 | | |
15 | | #define DO_TEST(_type, _exp, _code) \ |
16 | 0 | do { \ |
17 | 0 | ++testNum; \ |
18 | 0 | count = 0; \ |
19 | 0 | IntArray::_type iter(arr); \ |
20 | 0 | while (iter.HasMore() && count != ArrayLength(_exp)) { \ |
21 | 0 | _code \ |
22 | 0 | int next = iter.GetNext(); \ |
23 | 0 | int expected = _exp[count++]; \ |
24 | 0 | ASSERT_EQ(next, expected) << "During test " << testNum << " at position " << count - 1; \ |
25 | 0 | } \ |
26 | 0 | ASSERT_FALSE(iter.HasMore()) << "During test " << testNum << ", iterator ran over"; \ |
27 | 0 | ASSERT_EQ(count, ArrayLength(_exp)) << "During test " << testNum << ", iterator finished too early"; \ |
28 | 0 | } while (0) |
29 | | |
30 | | TEST(ObserverArray, Tests) |
31 | 0 | { |
32 | 0 | IntArray arr; |
33 | 0 | arr.AppendElement(3); |
34 | 0 | arr.AppendElement(4); |
35 | 0 |
|
36 | 0 | size_t count; |
37 | 0 | int testNum = 0; |
38 | 0 |
|
39 | 0 | // Basic sanity |
40 | 0 | static int test1Expected[] = { 3, 4 }; |
41 | 0 | DO_TEST(ForwardIterator, test1Expected, { /* nothing */ }); |
42 | 0 |
|
43 | 0 | // Appends |
44 | 0 | static int test2Expected[] = { 3, 4, 2 }; |
45 | 0 | DO_TEST(ForwardIterator, test2Expected, |
46 | 0 | if (count == 1) arr.AppendElement(2); |
47 | 0 | ); |
48 | 0 | DO_TEST(ForwardIterator, test2Expected, { /* nothing */ }); |
49 | 0 |
|
50 | 0 | DO_TEST(EndLimitedIterator, test2Expected, |
51 | 0 | if (count == 1) arr.AppendElement(5); |
52 | 0 | ); |
53 | 0 |
|
54 | 0 | static int test5Expected[] = { 3, 4, 2, 5 }; |
55 | 0 | DO_TEST(ForwardIterator, test5Expected, { /* nothing */ }); |
56 | 0 |
|
57 | 0 | // Removals |
58 | 0 | DO_TEST(ForwardIterator, test5Expected, |
59 | 0 | if (count == 1) arr.RemoveElementAt(0); |
60 | 0 | ); |
61 | 0 |
|
62 | 0 | static int test7Expected[] = { 4, 2, 5 }; |
63 | 0 | DO_TEST(ForwardIterator, test7Expected, { /* nothing */ }); |
64 | 0 |
|
65 | 0 | static int test8Expected[] = { 4, 5 }; |
66 | 0 | DO_TEST(ForwardIterator, test8Expected, |
67 | 0 | if (count == 1) arr.RemoveElementAt(1); |
68 | 0 | ); |
69 | 0 | DO_TEST(ForwardIterator, test8Expected, { /* nothing */ }); |
70 | 0 |
|
71 | 0 | arr.AppendElement(2); |
72 | 0 | arr.AppendElementUnlessExists(6); |
73 | 0 | static int test10Expected[] = { 4, 5, 2, 6 }; |
74 | 0 | DO_TEST(ForwardIterator, test10Expected, { /* nothing */ }); |
75 | 0 |
|
76 | 0 | arr.AppendElementUnlessExists(5); |
77 | 0 | DO_TEST(ForwardIterator, test10Expected, { /* nothing */ }); |
78 | 0 |
|
79 | 0 | static int test12Expected[] = { 4, 5, 6 }; |
80 | 0 | DO_TEST(ForwardIterator, test12Expected, |
81 | 0 | if (count == 1) arr.RemoveElementAt(2); |
82 | 0 | ); |
83 | 0 | DO_TEST(ForwardIterator, test12Expected, { /* nothing */ }); |
84 | 0 |
|
85 | 0 | // Removals + Appends |
86 | 0 | static int test14Expected[] = { 4, 6, 7 }; |
87 | 0 | DO_TEST(ForwardIterator, test14Expected, |
88 | 0 | if (count == 1) { |
89 | 0 | arr.RemoveElementAt(1); |
90 | 0 | arr.AppendElement(7); |
91 | 0 | } |
92 | 0 | ); |
93 | 0 | DO_TEST(ForwardIterator, test14Expected, { /* nothing */ }); |
94 | 0 |
|
95 | 0 | arr.AppendElement(2); |
96 | 0 | static int test16Expected[] = { 4, 6, 7, 2 }; |
97 | 0 | DO_TEST(ForwardIterator, test16Expected, { /* nothing */ }); |
98 | 0 |
|
99 | 0 | static int test17Expected[] = { 4, 7, 2 }; |
100 | 0 | DO_TEST(EndLimitedIterator, test17Expected, |
101 | 0 | if (count == 1) { |
102 | 0 | arr.RemoveElementAt(1); |
103 | 0 | arr.AppendElement(8); |
104 | 0 | } |
105 | 0 | ); |
106 | 0 |
|
107 | 0 | static int test18Expected[] = { 4, 7, 2, 8 }; |
108 | 0 | DO_TEST(ForwardIterator, test18Expected, { /* nothing */ }); |
109 | 0 |
|
110 | 0 | // Prepends |
111 | 0 | arr.PrependElementUnlessExists(3); |
112 | 0 | static int test19Expected[] = { 3, 4, 7, 2, 8 }; |
113 | 0 | DO_TEST(ForwardIterator, test19Expected, { /* nothing */ }); |
114 | 0 |
|
115 | 0 | arr.PrependElementUnlessExists(7); |
116 | 0 | DO_TEST(ForwardIterator, test19Expected, { /* nothing */ }); |
117 | 0 |
|
118 | 0 | DO_TEST(ForwardIterator, test19Expected, |
119 | 0 | if (count == 1) { |
120 | 0 | arr.PrependElementUnlessExists(9); |
121 | 0 | } |
122 | 0 | ); |
123 | 0 |
|
124 | 0 | static int test22Expected[] = { 9, 3, 4, 7, 2, 8 }; |
125 | 0 | DO_TEST(ForwardIterator, test22Expected, { }); |
126 | 0 |
|
127 | 0 | // BackwardIterator |
128 | 0 | static int test23Expected[] = { 8, 2, 7, 4, 3, 9 }; |
129 | 0 | DO_TEST(BackwardIterator, test23Expected, ); |
130 | 0 |
|
131 | 0 | // Removals |
132 | 0 | static int test24Expected[] = { 8, 2, 7, 4, 9 }; |
133 | 0 | DO_TEST(BackwardIterator, test24Expected, |
134 | 0 | if (count == 1) arr.RemoveElementAt(1); |
135 | 0 | ); |
136 | 0 |
|
137 | 0 | // Appends |
138 | 0 | DO_TEST(BackwardIterator, test24Expected, |
139 | 0 | if (count == 1) arr.AppendElement(1); |
140 | 0 | ); |
141 | 0 |
|
142 | 0 | static int test26Expected[] = { 1, 8, 2, 7, 4, 9 }; |
143 | 0 | DO_TEST(BackwardIterator, test26Expected, ); |
144 | 0 |
|
145 | 0 | // Prepends |
146 | 0 | static int test27Expected[] = { 1, 8, 2, 7, 4, 9, 3 }; |
147 | 0 | DO_TEST(BackwardIterator, test27Expected, |
148 | 0 | if (count == 1) arr.PrependElementUnlessExists(3); |
149 | 0 | ); |
150 | 0 |
|
151 | 0 | // Removal using Iterator |
152 | 0 | DO_TEST(BackwardIterator, test27Expected, |
153 | 0 | // when this code runs, |GetNext()| has only been called once, so |
154 | 0 | // this actually removes the very first element |
155 | 0 | if (count == 1) iter.Remove(); |
156 | 0 | ); |
157 | 0 |
|
158 | 0 | static int test28Expected[] = { 8, 2, 7, 4, 9, 3 }; |
159 | 0 | DO_TEST(BackwardIterator, test28Expected, ); |
160 | 0 |
|
161 | 0 | /** |
162 | 0 | * Note: _code is executed before the call to GetNext(), it can therefore not |
163 | 0 | * test the case of prepending when the BackwardIterator already returned the |
164 | 0 | * first element. |
165 | 0 | * In that case BackwardIterator does not traverse the newly prepended Element |
166 | 0 | */ |
167 | 0 |
|
168 | 0 | } |