1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : // vim:cindent:ts=4:et:sw=4:
3 : /* ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
10 : *
11 : * Software distributed under the License is distributed on an "AS IS" basis,
12 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 : * for the specific language governing rights and limitations under the
14 : * License.
15 : *
16 : * The Original Code is mozilla.org code.
17 : *
18 : * The Initial Developer of the Original Code is
19 : * the Mozilla Corporation.
20 : * Portions created by the Initial Developer are Copyright (C) 2009
21 : * the Initial Developer. All Rights Reserved.
22 : *
23 : * Contributor(s):
24 : * Boris Zbarsky <bzbarsky@mit.edu> (original author)
25 : *
26 : * Alternatively, the contents of this file may be used under the terms of
27 : * either of the GNU General Public License Version 2 or later (the "GPL"),
28 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 : * in which case the provisions of the GPL or the LGPL are applicable instead
30 : * of those above. If you wish to allow use of your version of this file only
31 : * under the terms of either the GPL or the LGPL, and not to allow others to
32 : * use your version of this file under the terms of the MPL, indicate your
33 : * decision by deleting the provisions above and replace them with the notice
34 : * and other provisions required by the GPL or the LGPL. If you do not delete
35 : * the provisions above, a recipient may use your version of this file under
36 : * the terms of any one of the MPL, the GPL or the LGPL.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : #include "TestHarness.h"
41 : #include "nsTObserverArray.h"
42 :
43 : using namespace mozilla;
44 :
45 : typedef nsTObserverArray<int> Array;
46 :
47 : #define DO_TEST(_type, _exp, _code) \
48 : do { \
49 : ++testNum; \
50 : count = 0; \
51 : Array::_type iter(arr); \
52 : while (iter.HasMore() && count != ArrayLength(_exp)) { \
53 : _code \
54 : int next = iter.GetNext(); \
55 : int expected = _exp[count++]; \
56 : if (next != expected) { \
57 : fail("During test %d at position %d got %d expected %d\n", \
58 : testNum, count-1, next, expected); \
59 : rv = 1; \
60 : } \
61 : } \
62 : if (iter.HasMore()) { \
63 : fail("During test %d, iterator ran over", testNum); \
64 : rv = 1; \
65 : } \
66 : if (count != ArrayLength(_exp)) { \
67 : fail("During test %d, iterator finished too early", testNum); \
68 : rv = 1; \
69 : } \
70 : } while (0)
71 :
72 1 : int main(int argc, char **argv)
73 : {
74 2 : ScopedXPCOM xpcom("nsTObserverArrayTests");
75 1 : if (xpcom.failed()) {
76 0 : return 1;
77 : }
78 :
79 1 : int rv = 0;
80 :
81 2 : Array arr;
82 1 : arr.AppendElement(3);
83 1 : arr.AppendElement(4);
84 :
85 : size_t count;
86 1 : int testNum = 0;
87 :
88 : // Basic sanity
89 : static int test1Expected[] = { 3, 4 };
90 1 : DO_TEST(ForwardIterator, test1Expected, );
91 :
92 : // Appends
93 : static int test2Expected[] = { 3, 4, 2 };
94 1 : DO_TEST(ForwardIterator, test2Expected,
95 : if (count == 1) arr.AppendElement(2);
96 : );
97 1 : DO_TEST(ForwardIterator, test2Expected, );
98 :
99 1 : DO_TEST(EndLimitedIterator, test2Expected,
100 : if (count == 1) arr.AppendElement(5);
101 : );
102 :
103 : static int test5Expected[] = { 3, 4, 2, 5 };
104 1 : DO_TEST(ForwardIterator, test5Expected, );
105 :
106 : // Removals
107 1 : DO_TEST(ForwardIterator, test5Expected,
108 : if (count == 1) arr.RemoveElementAt(0);
109 : );
110 :
111 : static int test7Expected[] = { 4, 2, 5 };
112 1 : DO_TEST(ForwardIterator, test7Expected, );
113 :
114 : static int test8Expected[] = { 4, 5 };
115 1 : DO_TEST(ForwardIterator, test8Expected,
116 : if (count == 1) arr.RemoveElementAt(1);
117 : );
118 1 : DO_TEST(ForwardIterator, test8Expected, );
119 :
120 1 : arr.AppendElement(2);
121 1 : arr.AppendElementUnlessExists(6);
122 : static int test10Expected[] = { 4, 5, 2, 6 };
123 1 : DO_TEST(ForwardIterator, test10Expected, );
124 :
125 1 : arr.AppendElementUnlessExists(5);
126 1 : DO_TEST(ForwardIterator, test10Expected, );
127 :
128 : static int test12Expected[] = { 4, 5, 6 };
129 1 : DO_TEST(ForwardIterator, test12Expected,
130 : if (count == 1) arr.RemoveElementAt(2);
131 : );
132 1 : DO_TEST(ForwardIterator, test12Expected, );
133 :
134 : // Removals + Appends
135 : static int test14Expected[] = { 4, 6, 7 };
136 1 : DO_TEST(ForwardIterator, test14Expected,
137 : if (count == 1) {
138 : arr.RemoveElementAt(1);
139 : arr.AppendElement(7);
140 : }
141 : );
142 1 : DO_TEST(ForwardIterator, test14Expected, );
143 :
144 1 : arr.AppendElement(2);
145 : static int test16Expected[] = { 4, 6, 7, 2 };
146 1 : DO_TEST(ForwardIterator, test16Expected, );
147 :
148 : static int test17Expected[] = { 4, 7, 2 };
149 1 : DO_TEST(EndLimitedIterator, test17Expected,
150 : if (count == 1) {
151 : arr.RemoveElementAt(1);
152 : arr.AppendElement(8);
153 : }
154 : );
155 :
156 : static int test18Expected[] = { 4, 7, 2, 8 };
157 1 : DO_TEST(ForwardIterator, test18Expected, );
158 :
159 : // Prepends
160 1 : arr.PrependElementUnlessExists(3);
161 : static int test19Expected[] = { 3, 4, 7, 2, 8 };
162 1 : DO_TEST(ForwardIterator, test19Expected, );
163 :
164 1 : arr.PrependElementUnlessExists(7);
165 1 : DO_TEST(ForwardIterator, test19Expected, );
166 :
167 : // Commented out because it fails; bug 474369 will fix
168 : /* DO_TEST(ForwardIterator, test19Expected,
169 : if (count == 1) {
170 : arr.PrependElementUnlessExists(9);
171 : }
172 : );
173 :
174 : static int test22Expected[] = { 9, 3, 4, 7, 2, 8 };
175 : DO_TEST(ForwardIterator, test22Expected, );
176 : */
177 :
178 1 : return rv;
179 : }
|