|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #include "TestHarness.h" |
|
7 #include "nsDeque.h" |
|
8 #include "nsCRT.h" |
|
9 #include <stdio.h> |
|
10 |
|
11 /************************************************************** |
|
12 Now define the token deallocator class... |
|
13 **************************************************************/ |
|
14 class _TestDeque { |
|
15 public: |
|
16 int Test(); |
|
17 private: |
|
18 int OriginalTest(); |
|
19 int OriginalFlaw(); |
|
20 int AssignFlaw(); |
|
21 int TestRemove(); |
|
22 }; |
|
23 |
|
24 class _Dealloc: public nsDequeFunctor { |
|
25 virtual void* operator()(void* aObject) { |
|
26 return 0; |
|
27 } |
|
28 }; |
|
29 |
|
30 #define TEST(aCondition, aMsg) \ |
|
31 if (!(aCondition)) { fail("TestDeque: "#aMsg); return 1; } |
|
32 |
|
33 |
|
34 /** |
|
35 * conduct automated self test for this class |
|
36 * |
|
37 * @param |
|
38 * @return |
|
39 */ |
|
40 int _TestDeque::Test() { |
|
41 /* the old deque should have failed a bunch of these tests */ |
|
42 int results=0; |
|
43 results+=OriginalTest(); |
|
44 results+=OriginalFlaw(); |
|
45 results+=AssignFlaw(); |
|
46 results+=TestRemove(); |
|
47 return results; |
|
48 } |
|
49 |
|
50 int _TestDeque::OriginalTest() { |
|
51 const int size = 200; |
|
52 int ints[size]; |
|
53 int i=0; |
|
54 int temp; |
|
55 nsDeque theDeque(new _Dealloc); //construct a simple one... |
|
56 |
|
57 // ints = [0...199] |
|
58 for (i=0;i<size;i++) { //initialize'em |
|
59 ints[i]=i; |
|
60 } |
|
61 // queue = [0...69] |
|
62 for (i=0;i<70;i++) { |
|
63 theDeque.Push(&ints[i]); |
|
64 temp=*(int*)theDeque.Peek(); |
|
65 TEST(temp == i, "Verify end after push #1"); |
|
66 TEST(theDeque.GetSize() == i + 1, "Verify size after push #1"); |
|
67 } |
|
68 TEST(theDeque.GetSize() == 70, "Verify overall size after pushes #1"); |
|
69 // queue = [0...14] |
|
70 for (i=1;i<=55;i++) { |
|
71 temp=*(int*)theDeque.Pop(); |
|
72 TEST(temp == 70-i, "Verify end after pop # 1"); |
|
73 TEST(theDeque.GetSize() == 70 - i, "Verify size after pop # 1"); |
|
74 } |
|
75 TEST(theDeque.GetSize() == 15, "Verify overall size after pops"); |
|
76 |
|
77 // queue = [0...14,0...54] |
|
78 for (i=0;i<55;i++) { |
|
79 theDeque.Push(&ints[i]); |
|
80 temp=*(int*)theDeque.Peek(); |
|
81 TEST(temp == i, "Verify end after push #2"); |
|
82 TEST(theDeque.GetSize() == i + 15 + 1, "Verify size after push # 2"); |
|
83 } |
|
84 TEST(theDeque.GetSize() == 70, "Verify size after end of all pushes #2"); |
|
85 |
|
86 // queue = [0...14,0...19] |
|
87 for (i=1;i<=35;i++) { |
|
88 temp=*(int*)theDeque.Pop(); |
|
89 TEST(temp == 55-i, "Verify end after pop # 2"); |
|
90 TEST(theDeque.GetSize() == 70 - i, "Verify size after pop #2"); |
|
91 } |
|
92 TEST(theDeque.GetSize() == 35, "Verify overall size after end of all pops #2"); |
|
93 |
|
94 // queue = [0...14,0...19,0...34] |
|
95 for (i=0;i<35;i++) { |
|
96 theDeque.Push(&ints[i]); |
|
97 temp = *(int*)theDeque.Peek(); |
|
98 TEST(temp == i, "Verify end after push # 3"); |
|
99 TEST(theDeque.GetSize() == 35 + 1 + i, "Verify size after push #3"); |
|
100 } |
|
101 |
|
102 // queue = [0...14,0...19] |
|
103 for (i=0;i<35;i++) { |
|
104 temp=*(int*)theDeque.Pop(); |
|
105 TEST(temp == 34 - i, "Verify end after pop # 3"); |
|
106 } |
|
107 |
|
108 // queue = [0...14] |
|
109 for (i=0;i<20;i++) { |
|
110 temp=*(int*)theDeque.Pop(); |
|
111 TEST(temp == 19 - i, "Verify end after pop # 4"); |
|
112 } |
|
113 |
|
114 // queue = [] |
|
115 for (i=0;i<15;i++) { |
|
116 temp=*(int*)theDeque.Pop(); |
|
117 TEST(temp == 14 - i, "Verify end after pop # 5"); |
|
118 } |
|
119 |
|
120 TEST(theDeque.GetSize() == 0, "Deque should finish empty."); |
|
121 |
|
122 return 0; |
|
123 } |
|
124 |
|
125 int _TestDeque::OriginalFlaw() { |
|
126 int ints[200]; |
|
127 int i=0; |
|
128 int temp; |
|
129 nsDeque d(new _Dealloc); |
|
130 /** |
|
131 * Test 1. Origin near end, semi full, call Peek(). |
|
132 * you start, mCapacity is 8 |
|
133 */ |
|
134 printf("fill array\n"); |
|
135 for (i=0; i<30; i++) |
|
136 ints[i]=i; |
|
137 |
|
138 for (i=0; i<6; i++) { |
|
139 d.Push(&ints[i]); |
|
140 temp = *(int*)d.Peek(); |
|
141 TEST(temp == i, "OriginalFlaw push #1"); |
|
142 } |
|
143 TEST(d.GetSize() == 6, "OriginalFlaw size check #1"); |
|
144 |
|
145 for (i=0; i<4; i++) { |
|
146 temp=*(int*)d.PopFront(); |
|
147 TEST(temp == i, "PopFront test"); |
|
148 } |
|
149 // d = [4,5] |
|
150 TEST(d.GetSize() == 2, "OriginalFlaw size check #2"); |
|
151 |
|
152 for (i=0; i<4; i++) { |
|
153 d.Push(&ints[6 + i]); |
|
154 } |
|
155 // d = [4...9] |
|
156 |
|
157 for (i=4; i<=9; i++) { |
|
158 temp=*(int*)d.PopFront(); |
|
159 TEST(temp == i, "OriginalFlaw empty check"); |
|
160 } |
|
161 |
|
162 return 0; |
|
163 } |
|
164 |
|
165 int _TestDeque::AssignFlaw() { |
|
166 nsDeque src(new _Dealloc),dest(new _Dealloc); |
|
167 return 0; |
|
168 } |
|
169 |
|
170 static bool VerifyContents(const nsDeque& aDeque, const int* aContents, int aLength) { |
|
171 for (int i=0; i<aLength; ++i) { |
|
172 if (*(int*)aDeque.ObjectAt(i) != aContents[i]) { |
|
173 return false; |
|
174 } |
|
175 } |
|
176 return true; |
|
177 } |
|
178 |
|
179 int _TestDeque::TestRemove() { |
|
180 nsDeque d; |
|
181 const int count = 10; |
|
182 int ints[count]; |
|
183 for (int i=0; i<count; i++) { |
|
184 ints[i] = i; |
|
185 } |
|
186 |
|
187 for (int i=0; i<6; i++) { |
|
188 d.Push(&ints[i]); |
|
189 } |
|
190 // d = [0...5] |
|
191 d.PopFront(); |
|
192 d.PopFront(); |
|
193 |
|
194 // d = [2,5] |
|
195 for (int i=2; i<=5; i++) { |
|
196 int t = *(int*)d.ObjectAt(i-2); |
|
197 TEST(t == i, "Verify ObjectAt()"); |
|
198 } |
|
199 |
|
200 d.RemoveObjectAt(1); |
|
201 // d == [2,4,5] |
|
202 static const int t1[] = {2,4,5}; |
|
203 TEST(VerifyContents(d, t1, 3), "verify contents t1"); |
|
204 |
|
205 d.PushFront(&ints[1]); |
|
206 d.PushFront(&ints[0]); |
|
207 d.PushFront(&ints[7]); |
|
208 d.PushFront(&ints[6]); |
|
209 // d == [6,7,0,1,2,4,5] // (0==mOrigin) |
|
210 static const int t2[] = {6,7,0,1,2,4,5}; |
|
211 TEST(VerifyContents(d, t2, 7), "verify contents t2"); |
|
212 |
|
213 d.RemoveObjectAt(1); |
|
214 // d == [6,0,1,2,4,5] // (1==mOrigin) |
|
215 static const int t3[] = {6,0,1,2,4,5}; |
|
216 TEST(VerifyContents(d, t3, 6), "verify contents t3"); |
|
217 |
|
218 d.RemoveObjectAt(5); |
|
219 // d == [6,0,1,2,4] // (1==mOrigin) |
|
220 static const int t4[] = {6,0,1,2,4}; |
|
221 TEST(VerifyContents(d, t4, 5), "verify contents t4"); |
|
222 |
|
223 d.RemoveObjectAt(0); |
|
224 // d == [0,1,2,4] // (2==mOrigin) |
|
225 static const int t5[] = {0,1,2,4}; |
|
226 TEST(VerifyContents(d, t5, 4), "verify contents t5"); |
|
227 |
|
228 |
|
229 return 0; |
|
230 } |
|
231 |
|
232 int main (void) { |
|
233 ScopedXPCOM xpcom("TestTimers"); |
|
234 NS_ENSURE_FALSE(xpcom.failed(), 1); |
|
235 |
|
236 _TestDeque test; |
|
237 int result = test.Test(); |
|
238 TEST(result == 0, "All tests pass"); |
|
239 return 0; |
|
240 } |