1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 1998
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either of the GNU General Public License Version 2 or later (the "GPL"),
26 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include "TestHarness.h"
39 : #include "nsDeque.h"
40 : #include "nsCRT.h"
41 : #include <stdio.h>
42 :
43 : /**************************************************************
44 : Now define the token deallocator class...
45 : **************************************************************/
46 : class _TestDeque {
47 : public:
48 : int Test();
49 : private:
50 : int OriginalTest();
51 : int OriginalFlaw();
52 : int AssignFlaw();
53 : int TestRemove();
54 : };
55 :
56 4 : class _Dealloc: public nsDequeFunctor {
57 0 : virtual void* operator()(void* aObject) {
58 0 : return 0;
59 : }
60 : };
61 :
62 : #define TEST(aCondition, aMsg) \
63 : if (!(aCondition)) { fail("TestDeque: "#aMsg); return 1; }
64 :
65 :
66 : /**
67 : * conduct automated self test for this class
68 : *
69 : * @param
70 : * @return
71 : */
72 1 : int _TestDeque::Test() {
73 : /* the old deque should have failed a bunch of these tests */
74 1 : int results=0;
75 1 : results+=OriginalTest();
76 1 : results+=OriginalFlaw();
77 1 : results+=AssignFlaw();
78 1 : results+=TestRemove();
79 1 : return results;
80 : }
81 :
82 1 : int _TestDeque::OriginalTest() {
83 1 : const int size = 200;
84 : int ints[size];
85 1 : int i=0;
86 : int temp;
87 2 : nsDeque theDeque(new _Dealloc); //construct a simple one...
88 :
89 : // ints = [0...199]
90 201 : for (i=0;i<size;i++) { //initialize'em
91 200 : ints[i]=i;
92 : }
93 : // queue = [0...69]
94 71 : for (i=0;i<70;i++) {
95 70 : theDeque.Push(&ints[i]);
96 70 : temp=*(int*)theDeque.Peek();
97 70 : TEST(temp == i, "Verify end after push #1");
98 70 : TEST(theDeque.GetSize() == i + 1, "Verify size after push #1");
99 : }
100 1 : TEST(theDeque.GetSize() == 70, "Verify overall size after pushes #1");
101 : // queue = [0...14]
102 56 : for (i=1;i<=55;i++) {
103 55 : temp=*(int*)theDeque.Pop();
104 55 : TEST(temp == 70-i, "Verify end after pop # 1");
105 55 : TEST(theDeque.GetSize() == 70 - i, "Verify size after pop # 1");
106 : }
107 1 : TEST(theDeque.GetSize() == 15, "Verify overall size after pops");
108 :
109 : // queue = [0...14,0...54]
110 56 : for (i=0;i<55;i++) {
111 55 : theDeque.Push(&ints[i]);
112 55 : temp=*(int*)theDeque.Peek();
113 55 : TEST(temp == i, "Verify end after push #2");
114 55 : TEST(theDeque.GetSize() == i + 15 + 1, "Verify size after push # 2");
115 : }
116 1 : TEST(theDeque.GetSize() == 70, "Verify size after end of all pushes #2");
117 :
118 : // queue = [0...14,0...19]
119 36 : for (i=1;i<=35;i++) {
120 35 : temp=*(int*)theDeque.Pop();
121 35 : TEST(temp == 55-i, "Verify end after pop # 2");
122 35 : TEST(theDeque.GetSize() == 70 - i, "Verify size after pop #2");
123 : }
124 1 : TEST(theDeque.GetSize() == 35, "Verify overall size after end of all pops #2");
125 :
126 : // queue = [0...14,0...19,0...34]
127 36 : for (i=0;i<35;i++) {
128 35 : theDeque.Push(&ints[i]);
129 35 : temp = *(int*)theDeque.Peek();
130 35 : TEST(temp == i, "Verify end after push # 3");
131 35 : TEST(theDeque.GetSize() == 35 + 1 + i, "Verify size after push #3");
132 : }
133 :
134 : // queue = [0...14,0...19]
135 36 : for (i=0;i<35;i++) {
136 35 : temp=*(int*)theDeque.Pop();
137 35 : TEST(temp == 34 - i, "Verify end after pop # 3");
138 : }
139 :
140 : // queue = [0...14]
141 21 : for (i=0;i<20;i++) {
142 20 : temp=*(int*)theDeque.Pop();
143 20 : TEST(temp == 19 - i, "Verify end after pop # 4");
144 : }
145 :
146 : // queue = []
147 16 : for (i=0;i<15;i++) {
148 15 : temp=*(int*)theDeque.Pop();
149 15 : TEST(temp == 14 - i, "Verify end after pop # 5");
150 : }
151 :
152 1 : TEST(theDeque.GetSize() == 0, "Deque should finish empty.");
153 :
154 1 : return 0;
155 : }
156 :
157 1 : int _TestDeque::OriginalFlaw() {
158 : int ints[200];
159 1 : int i=0;
160 : int temp;
161 2 : nsDeque d(new _Dealloc);
162 : /**
163 : * Test 1. Origin near end, semi full, call Peek().
164 : * you start, mCapacity is 8
165 : */
166 1 : printf("fill array\n");
167 31 : for (i=0; i<30; i++)
168 30 : ints[i]=i;
169 :
170 7 : for (i=0; i<6; i++) {
171 6 : d.Push(&ints[i]);
172 6 : temp = *(int*)d.Peek();
173 6 : TEST(temp == i, "OriginalFlaw push #1");
174 : }
175 1 : TEST(d.GetSize() == 6, "OriginalFlaw size check #1");
176 :
177 5 : for (i=0; i<4; i++) {
178 4 : temp=*(int*)d.PopFront();
179 4 : TEST(temp == i, "PopFront test");
180 : }
181 : // d = [4,5]
182 1 : TEST(d.GetSize() == 2, "OriginalFlaw size check #2");
183 :
184 5 : for (i=0; i<4; i++) {
185 4 : d.Push(&ints[6 + i]);
186 : }
187 : // d = [4...9]
188 :
189 7 : for (i=4; i<=9; i++) {
190 6 : temp=*(int*)d.PopFront();
191 6 : TEST(temp == i, "OriginalFlaw empty check");
192 : }
193 :
194 1 : return 0;
195 : }
196 :
197 1 : int _TestDeque::AssignFlaw() {
198 3 : nsDeque src(new _Dealloc),dest(new _Dealloc);
199 1 : return 0;
200 : }
201 :
202 5 : static bool VerifyContents(const nsDeque& aDeque, const int* aContents, int aLength) {
203 30 : for (int i=0; i<aLength; ++i) {
204 25 : if (*(int*)aDeque.ObjectAt(i) != aContents[i]) {
205 0 : return false;
206 : }
207 : }
208 5 : return true;
209 : }
210 :
211 1 : int _TestDeque::TestRemove() {
212 2 : nsDeque d;
213 1 : const int count = 10;
214 : int ints[count];
215 11 : for (int i=0; i<count; i++) {
216 10 : ints[i] = i;
217 : }
218 :
219 7 : for (int i=0; i<6; i++) {
220 6 : d.Push(&ints[i]);
221 : }
222 : // d = [0...5]
223 1 : d.PopFront();
224 1 : d.PopFront();
225 :
226 : // d = [2,5]
227 5 : for (int i=2; i<=5; i++) {
228 4 : int t = *(int*)d.ObjectAt(i-2);
229 4 : TEST(t == i, "Verify ObjectAt()");
230 : }
231 :
232 1 : d.RemoveObjectAt(1);
233 : // d == [2,4,5]
234 : static const int t1[] = {2,4,5};
235 1 : TEST(VerifyContents(d, t1, 3), "verify contents t1");
236 :
237 1 : d.PushFront(&ints[1]);
238 1 : d.PushFront(&ints[0]);
239 1 : d.PushFront(&ints[7]);
240 1 : d.PushFront(&ints[6]);
241 : // d == [6,7,0,1,2,4,5] // (0==mOrigin)
242 : static const int t2[] = {6,7,0,1,2,4,5};
243 1 : TEST(VerifyContents(d, t2, 7), "verify contents t2");
244 :
245 1 : d.RemoveObjectAt(1);
246 : // d == [6,0,1,2,4,5] // (1==mOrigin)
247 : static const int t3[] = {6,0,1,2,4,5};
248 1 : TEST(VerifyContents(d, t3, 6), "verify contents t3");
249 :
250 1 : d.RemoveObjectAt(5);
251 : // d == [6,0,1,2,4] // (1==mOrigin)
252 : static const int t4[] = {6,0,1,2,4};
253 1 : TEST(VerifyContents(d, t4, 5), "verify contents t4");
254 :
255 1 : d.RemoveObjectAt(0);
256 : // d == [0,1,2,4] // (2==mOrigin)
257 : static const int t5[] = {0,1,2,4};
258 1 : TEST(VerifyContents(d, t5, 4), "verify contents t5");
259 :
260 :
261 1 : return 0;
262 : }
263 :
264 1 : int main (void) {
265 2 : ScopedXPCOM xpcom("TestTimers");
266 1 : NS_ENSURE_FALSE(xpcom.failed(), 1);
267 :
268 : _TestDeque test;
269 1 : int result = test.Test();
270 1 : TEST(result == 0, "All tests pass");
271 1 : return 0;
272 : }
|