Bug 596580: Fix mozJSSubScriptLoader's version finding. (r=brendan)
[mozilla-central.git] / xpcom / tests / TestObserverArray.cpp
blobb4867bc089daf50cd3fd6ed0ba22ed7aeda69847
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
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/
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.
16 * The Original Code is mozilla.org code.
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.
23 * Contributor(s):
24 * Boris Zbarsky <bzbarsky@mit.edu> (original author)
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.
38 * ***** END LICENSE BLOCK ***** */
40 #include "TestHarness.h"
41 #include "nsTObserverArray.h"
42 #include "nsMemory.h" // for NS_ARRAY_LENGTH
44 typedef nsTObserverArray<int> Array;
46 #define DO_TEST(_type, _exp, _code) \
47 do { \
48 ++testNum; \
49 count = 0; \
50 Array::_type iter(arr); \
51 while (iter.HasMore() && count != NS_ARRAY_LENGTH(_exp)) { \
52 _code \
53 int next = iter.GetNext(); \
54 int expected = _exp[count++]; \
55 if (next != expected) { \
56 fail("During test %d at position %d got %d expected %d\n", \
57 testNum, count-1, next, expected); \
58 rv = 1; \
59 } \
60 } \
61 if (iter.HasMore()) { \
62 fail("During test %d, iterator ran over", testNum); \
63 rv = 1; \
64 } \
65 if (count != NS_ARRAY_LENGTH(_exp)) { \
66 fail("During test %d, iterator finished too early", testNum); \
67 rv = 1; \
68 } \
69 } while (0)
71 int main(int argc, char **argv)
73 ScopedXPCOM xpcom("nsTObserverArrayTests");
74 if (xpcom.failed()) {
75 return 1;
78 int rv = 0;
80 Array arr;
81 arr.AppendElement(3);
82 arr.AppendElement(4);
84 int count;
85 int testNum = 0;
87 // Basic sanity
88 static int test1Expected[] = { 3, 4 };
89 DO_TEST(ForwardIterator, test1Expected, );
91 // Appends
92 static int test2Expected[] = { 3, 4, 2 };
93 DO_TEST(ForwardIterator, test2Expected,
94 if (count == 1) arr.AppendElement(2);
96 DO_TEST(ForwardIterator, test2Expected, );
98 DO_TEST(EndLimitedIterator, test2Expected,
99 if (count == 1) arr.AppendElement(5);
102 static int test5Expected[] = { 3, 4, 2, 5 };
103 DO_TEST(ForwardIterator, test5Expected, );
105 // Removals
106 DO_TEST(ForwardIterator, test5Expected,
107 if (count == 1) arr.RemoveElementAt(0);
110 static int test7Expected[] = { 4, 2, 5 };
111 DO_TEST(ForwardIterator, test7Expected, );
113 static int test8Expected[] = { 4, 5 };
114 DO_TEST(ForwardIterator, test8Expected,
115 if (count == 1) arr.RemoveElementAt(1);
117 DO_TEST(ForwardIterator, test8Expected, );
119 arr.AppendElement(2);
120 arr.AppendElementUnlessExists(6);
121 static int test10Expected[] = { 4, 5, 2, 6 };
122 DO_TEST(ForwardIterator, test10Expected, );
124 arr.AppendElementUnlessExists(5);
125 DO_TEST(ForwardIterator, test10Expected, );
127 static int test12Expected[] = { 4, 5, 6 };
128 DO_TEST(ForwardIterator, test12Expected,
129 if (count == 1) arr.RemoveElementAt(2);
131 DO_TEST(ForwardIterator, test12Expected, );
133 // Removals + Appends
134 static int test14Expected[] = { 4, 6, 7 };
135 DO_TEST(ForwardIterator, test14Expected,
136 if (count == 1) {
137 arr.RemoveElementAt(1);
138 arr.AppendElement(7);
141 DO_TEST(ForwardIterator, test14Expected, );
143 arr.AppendElement(2);
144 static int test16Expected[] = { 4, 6, 7, 2 };
145 DO_TEST(ForwardIterator, test16Expected, );
147 static int test17Expected[] = { 4, 7, 2 };
148 DO_TEST(EndLimitedIterator, test17Expected,
149 if (count == 1) {
150 arr.RemoveElementAt(1);
151 arr.AppendElement(8);
155 static int test18Expected[] = { 4, 7, 2, 8 };
156 DO_TEST(ForwardIterator, test18Expected, );
158 // Prepends
159 arr.PrependElementUnlessExists(3);
160 static int test19Expected[] = { 3, 4, 7, 2, 8 };
161 DO_TEST(ForwardIterator, test19Expected, );
163 arr.PrependElementUnlessExists(7);
164 DO_TEST(ForwardIterator, test19Expected, );
166 // Commented out because it fails; bug 474369 will fix
167 /* DO_TEST(ForwardIterator, test19Expected,
168 if (count == 1) {
169 arr.PrependElementUnlessExists(9);
173 static int test22Expected[] = { 9, 3, 4, 7, 2, 8 };
174 DO_TEST(ForwardIterator, test22Expected, );
177 return rv;