]>
Commit | Line | Data |
---|---|---|
1c79356b A |
1 | /* |
2 | * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
d7e50217 | 6 | * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved. |
1c79356b | 7 | * |
d7e50217 A |
8 | * This file contains Original Code and/or Modifications of Original Code |
9 | * as defined in and that are subject to the Apple Public Source License | |
10 | * Version 2.0 (the 'License'). You may not use this file except in | |
11 | * compliance with the License. Please obtain a copy of the License at | |
12 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
13 | * file. | |
14 | * | |
15 | * The Original Code and all software distributed under the License are | |
16 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
1c79356b A |
17 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
18 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
d7e50217 A |
19 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
20 | * Please see the License for the specific language governing rights and | |
21 | * limitations under the License. | |
1c79356b A |
22 | * |
23 | * @APPLE_LICENSE_HEADER_END@ | |
24 | */ | |
25 | #if DEBUG | |
26 | #include "Tests.h" | |
27 | ||
28 | #include <libkern/c++/OSArray.h> | |
29 | #include <libkern/c++/OSSet.h> | |
30 | #include <libkern/c++/OSDictionary.h> | |
31 | #include <libkern/c++/OSString.h> | |
32 | #include <libkern/c++/OSSymbol.h> | |
33 | #include <libkern/c++/OSCollectionIterator.h> | |
34 | ||
35 | void testArray() | |
36 | { | |
37 | bool res = true; | |
38 | void *spaceCheck, *spaceCheck2 , *spaceCheck3; | |
39 | int i, j, count, count2; | |
40 | OSObject *cache[numStrCache], *str, *sym; | |
41 | OSArray *array1, *array2; | |
42 | ||
43 | // Do first test without memory leak tests to initialise the metaclass | |
44 | array1 = OSArray::withCapacity(1); | |
45 | TEST_ASSERT('A', "0a", array1); | |
46 | if (array1) | |
47 | array1->release(); | |
48 | ||
49 | // Grow the symbol pool to maximum | |
50 | for (i = 0; i < numStrCache; i++) | |
51 | cache[i] = (OSObject *) OSSymbol::withCStringNoCopy(strCache[i]); | |
52 | for (i = 0; i < numStrCache; i++) | |
53 | cache[i]->release(); | |
54 | ||
55 | // Create and destroy an array | |
56 | spaceCheck = checkPointSpace(); | |
57 | array1 = OSArray::withCapacity(1); | |
58 | TEST_ASSERT('A', "1a", array1); | |
59 | if (array1) { | |
60 | TEST_ASSERT('A', "1b", !array1->getCount()); | |
61 | TEST_ASSERT('A', "1c", 1 == array1->getCapacity()); | |
62 | TEST_ASSERT('A', "1d", 1 == array1->getCapacityIncrement()); | |
63 | TEST_ASSERT('A', "1e", 4 == array1->setCapacityIncrement(4)); | |
64 | TEST_ASSERT('A', "1f", 4 == array1->getCapacityIncrement()); | |
65 | TEST_ASSERT('A', "1g", 8 == array1->ensureCapacity(5)); | |
66 | ||
67 | spaceCheck2 = checkPointSpace(); | |
68 | cache[0] = IOString::withCStringNoCopy(strCache[0]); | |
69 | ||
70 | spaceCheck3 = checkPointSpace(); | |
71 | TEST_ASSERT('A', "1h", array1->setObject(cache[0])); | |
72 | TEST_ASSERT('A', "1i", cache[0] == array1->getObject(0)); | |
73 | cache[0]->release(); | |
74 | res = res && checkSpace("(A)1j", spaceCheck3, 0); | |
75 | ||
76 | TEST_ASSERT('A', "1k", 1 == array1->getCount()); | |
77 | array1->flushCollection(); | |
78 | TEST_ASSERT('A', "1l", !array1->getCount()); | |
79 | res = res && checkSpace("(A)1m", spaceCheck2, 0); | |
80 | ||
81 | array1->release(); | |
82 | } | |
83 | res = res && checkSpace("(A)1", spaceCheck, 0); | |
84 | ||
85 | // Check the creation of a sizable OSArray from an array of IOObjects | |
86 | // Also check indexing into the array. | |
87 | spaceCheck = checkPointSpace(); | |
88 | for (i = 0; i < numStrCache; i++) | |
89 | cache[i] = OSString::withCStringNoCopy(strCache[i]); | |
90 | array1 = OSArray::withObjects(cache, numStrCache, numStrCache); | |
91 | TEST_ASSERT('A', "2a", array1); | |
92 | for (i = 0; i < numStrCache; i++) | |
93 | cache[i]->release(); | |
94 | if (array1) { | |
95 | TEST_ASSERT('A', "2b", numStrCache == (int) array1->getCount()); | |
96 | TEST_ASSERT('A', "2c", numStrCache == (int) array1->getCapacity()); | |
97 | TEST_ASSERT('A', "2d", | |
98 | numStrCache == (int) array1->getCapacityIncrement()); | |
99 | ||
100 | for (i = 0; (str = array1->getObject(i)); i++) { | |
101 | if (str != cache[i]) { | |
102 | verPrintf(("testArray(A) test 2e%d failed\n", i)); | |
103 | res = false; | |
104 | } | |
105 | } | |
106 | TEST_ASSERT('A', "2f", numStrCache == i); | |
107 | array1->release(); | |
108 | } | |
109 | res = res && checkSpace("(A)2", spaceCheck, 0); | |
110 | ||
111 | // Test array creation from another array by both the setObject method | |
112 | // and the withArray factory. And test __takeObject code first | |
113 | // with tail removal then with head removal | |
114 | spaceCheck = checkPointSpace(); | |
115 | for (i = 0; i < numStrCache; i++) | |
116 | cache[i] = OSString::withCStringNoCopy(strCache[i]); | |
117 | array1 = OSArray::withObjects(cache, numStrCache, numStrCache); | |
118 | TEST_ASSERT('A', "3a", array1); | |
119 | for (i = 0; i < numStrCache; i++) | |
120 | cache[i]->release(); | |
121 | array2 = 0; | |
122 | if (array1) { | |
123 | array2 = OSArray::withCapacity(1); | |
124 | TEST_ASSERT('A', "3b", array2); | |
125 | TEST_ASSERT('A', "3c", !array2->getCount()); | |
126 | TEST_ASSERT('A', "3d", array2->setObject(array1)); | |
127 | TEST_ASSERT('A', "3e", array1->getCount() == array2->getCount()); | |
128 | } | |
129 | if (array2) { | |
130 | count = 0; | |
131 | TEST_ASSERT('A', "3f", numStrCache == (int) array2->getCount()); | |
132 | for (i = array2->getCount(); (str = array2->__takeObject(--i)); ) { | |
133 | if (str != cache[i]) { | |
134 | verPrintf(("testArray(A) test 3g%d failed\n", i)); | |
135 | res = false; | |
136 | } | |
137 | count += ((int) array2->getCount() == i); | |
138 | str->release(); | |
139 | } | |
140 | TEST_ASSERT('A', "3h", count == numStrCache); | |
141 | TEST_ASSERT('A', "3i", -1 == i); | |
142 | TEST_ASSERT('A', "3j", !array2->getCount()); | |
143 | ||
144 | spaceCheck2 = checkPointSpace(); | |
145 | array2->flushCollection(); | |
146 | res = res && checkSpace("(A)3k", spaceCheck2, 0); | |
147 | ||
148 | array2->release(); | |
149 | array2 = 0; | |
150 | } | |
151 | if (array1) { | |
152 | array2 = OSArray::withArray(array1, numStrCache - 1); | |
153 | TEST_ASSERT('A', "3l", !array2); | |
154 | array2 = OSArray::withArray(array1, array1->getCount()); | |
155 | TEST_ASSERT('A', "3m", array2); | |
156 | array1->release(); | |
157 | } | |
158 | if (array2) { | |
159 | count = 0; | |
160 | TEST_ASSERT('A', "3o", numStrCache == (int) array2->getCount()); | |
161 | for (i = 0; (str = array2->__takeObject(0)); i++) { | |
162 | count += (str == cache[i]); | |
163 | str->release(); | |
164 | } | |
165 | TEST_ASSERT('A', "3p", count == numStrCache); | |
166 | TEST_ASSERT('A', "3q", !array2->getCount()); | |
167 | array2->release(); | |
168 | array2 = 0; | |
169 | } | |
170 | res = res && checkSpace("(A)3", spaceCheck, 0); | |
171 | ||
172 | // Test object replacement from one array to another | |
173 | spaceCheck = checkPointSpace(); | |
174 | array1 = OSArray::withCapacity(numStrCache); | |
175 | TEST_ASSERT('A', "4a", array1); | |
176 | if (array1) { | |
177 | count = count2 = 0; | |
178 | for (i = 0; i < numStrCache; i++) { | |
179 | str = OSString::withCStringNoCopy(strCache[i]); | |
180 | count += array1->setObject(str); | |
181 | count2 += (str == array1->lastObject()); | |
182 | str->release(); | |
183 | } | |
184 | TEST_ASSERT('A', "4b", numStrCache == (int) array1->getCount()); | |
185 | TEST_ASSERT('A', "4c", count == numStrCache); | |
186 | TEST_ASSERT('A', "4d", count2 == numStrCache); | |
187 | } | |
188 | array2 = OSArray::withCapacity(1); | |
189 | TEST_ASSERT('A', "4e", array2); | |
190 | if (array2) { | |
191 | count = count2 = 0; | |
192 | str = (OSObject *) OSSymbol::withCStringNoCopy(strCache[0]); | |
193 | for (i = 0; i < numStrCache; i++) { | |
194 | sym = (OSObject *) OSSymbol::withCStringNoCopy(strCache[i]); | |
195 | count += array2->setObject(sym, 0); | |
196 | count2 += (str == array2->lastObject()); | |
197 | sym->release(); | |
198 | } | |
199 | str->release(); | |
200 | TEST_ASSERT('A', "4f", numStrCache == (int) array2->getCount()); | |
201 | TEST_ASSERT('A', "4g", count == numStrCache); | |
202 | TEST_ASSERT('A', "4h", count2 == numStrCache); | |
203 | } | |
204 | if (array1 && array2) { | |
205 | ||
206 | count = count2 = 0; | |
207 | for (i = array1->getCount() - 1; (sym = array2->__takeObject(0)); i--) { | |
208 | str = array1->replaceObject(sym, i); | |
209 | count += (str != 0); | |
210 | count2 += (sym != str); | |
211 | if (str) | |
212 | str->release(); | |
213 | if (sym) | |
214 | sym->release(); | |
215 | } | |
216 | TEST_ASSERT('A', "4k", numStrCache == (int) array1->getCount()); | |
217 | TEST_ASSERT('A', "4l", count == numStrCache); | |
218 | TEST_ASSERT('A', "4m", count2 == numStrCache); | |
219 | array1->release(); | |
220 | array2->release(); | |
221 | } | |
222 | else { | |
223 | if (array1) array1->release(); | |
224 | if (array2) array2->release(); | |
225 | } | |
226 | res = res && checkSpace("(A)4", spaceCheck, 0); | |
227 | ||
228 | // Test array duplicate removal | |
229 | spaceCheck = checkPointSpace(); | |
230 | array1 = OSArray::withCapacity(numStrCache); | |
231 | TEST_ASSERT('A', "5a", array1); | |
232 | if (array1) { | |
233 | for (i = 0; i < numStrCache; i++) { | |
234 | sym = (OSObject *) OSSymbol::withCStringNoCopy(strCache[i]); | |
235 | count += array1->setObject(sym); | |
236 | sym->release(); | |
237 | } | |
238 | TEST_ASSERT('A', "5b", numStrCache == (int) array1->getCount()); | |
239 | ||
240 | // remove duplicates | |
241 | for (i = 0; (sym = array1->getObject(i)); ) | |
242 | if (sym->getRetainCount() == 1) | |
243 | i++; | |
244 | else { | |
245 | //sym = array1->__takeObject(i); | |
246 | //sym->release(); | |
247 | array1->removeObject(i); | |
248 | } | |
249 | TEST_ASSERT('A', "5c", numStrCache != (int) array1->getCount()); | |
250 | ||
251 | // check to see that all symbols are really there | |
252 | for (count = 0, i = 0; i < numStrCache; i++) { | |
253 | sym = (OSObject *) OSSymbol::withCStringNoCopy(strCache[i]); | |
254 | for (count2 = false, j = 0; (str = array1->getObject(j)); j++) | |
255 | if (str == sym) { | |
256 | count2 = true; | |
257 | break; | |
258 | } | |
259 | count += count2; | |
260 | sym->release(); | |
261 | } | |
262 | TEST_ASSERT('A', "5c", count == numStrCache); | |
263 | array1->release(); | |
264 | } | |
265 | res = res && checkSpace("(S)5", spaceCheck, 0); | |
266 | ||
267 | if (res) | |
268 | verPrintf(("testArray: All OSArray Tests passed\n")); | |
269 | else | |
270 | logPrintf(("testArray: Some OSArray Tests failed\n")); | |
271 | } | |
272 | ||
273 | void testSet() | |
274 | { | |
275 | bool res = true; | |
276 | void *spaceCheck, *spaceCheck2 , *spaceCheck3; | |
277 | int i, count, count2; | |
278 | OSObject *cache[numStrCache], *str, *sym; | |
279 | OSSet *set1, *set2; | |
280 | OSArray *array; | |
281 | ||
282 | // Do first test without memory leak tests to initialise the metaclass | |
283 | set1 = OSSet::withCapacity(1); | |
284 | TEST_ASSERT('S', "0a", set1); | |
285 | if (set1) | |
286 | set1->release(); | |
287 | ||
288 | // Grow the symbol pool to maximum | |
289 | for (i = 0; i < numStrCache; i++) | |
290 | cache[i] = (OSObject *) OSSymbol::withCStringNoCopy(strCache[i]); | |
291 | for (i = 0; i < numStrCache; i++) | |
292 | cache[i]->release(); | |
293 | ||
294 | // Create and destroy an set | |
295 | spaceCheck = checkPointSpace(); | |
296 | set1 = OSSet::withCapacity(1); | |
297 | TEST_ASSERT('S', "1a", set1); | |
298 | if (set1) { | |
299 | TEST_ASSERT('S', "1b", !set1->getCount()); | |
300 | TEST_ASSERT('S', "1c", 1 == set1->getCapacity()); | |
301 | TEST_ASSERT('S', "1d", 1 == set1->getCapacityIncrement()); | |
302 | TEST_ASSERT('S', "1e", 4 == set1->setCapacityIncrement(4)); | |
303 | TEST_ASSERT('S', "1f", 4 == set1->getCapacityIncrement()); | |
304 | TEST_ASSERT('S', "1g", 8 == set1->ensureCapacity(5)); | |
305 | ||
306 | spaceCheck2 = checkPointSpace(); | |
307 | cache[0] = IOString::withCStringNoCopy(strCache[0]); | |
308 | ||
309 | spaceCheck3 = checkPointSpace(); | |
310 | TEST_ASSERT('S', "1h", set1->setObject(cache[0])); | |
311 | TEST_ASSERT('S', "1i", set1->containsObject(cache[0])); | |
312 | TEST_ASSERT('S', "1j", cache[0] == set1->getAnyObject()); | |
313 | cache[0]->release(); | |
314 | res = res && checkSpace("(S)1k", spaceCheck3, 0); | |
315 | ||
316 | TEST_ASSERT('S', "1l", 1 == set1->getCount()); | |
317 | set1->flushCollection(); | |
318 | TEST_ASSERT('S', "1m", !set1->getCount()); | |
319 | res = res && checkSpace("(S)1n", spaceCheck2, 0); | |
320 | ||
321 | set1->release(); | |
322 | } | |
323 | res = res && checkSpace("(S)1", spaceCheck, 0); | |
324 | ||
325 | // Check the creation of a sizable OSSet from an set of IOObjects | |
326 | // Also check member test of set. | |
327 | spaceCheck = checkPointSpace(); | |
328 | for (i = 0; i < numStrCache; i++) | |
329 | cache[i] = OSString::withCStringNoCopy(strCache[i]); | |
330 | set1 = OSSet::withObjects(cache, numStrCache, numStrCache); | |
331 | TEST_ASSERT('S', "2a", set1); | |
332 | for (i = 0; i < numStrCache; i++) | |
333 | cache[i]->release(); | |
334 | if (set1) { | |
335 | TEST_ASSERT('S', "2b", numStrCache == (int) set1->getCount()); | |
336 | TEST_ASSERT('S', "2c", numStrCache == (int) set1->getCapacity()); | |
337 | TEST_ASSERT('S', "2d", | |
338 | numStrCache == (int) set1->getCapacityIncrement()); | |
339 | ||
340 | count = 0; | |
341 | for (i = set1->getCount(); --i >= 0; ) | |
342 | count += set1->member(cache[i]); | |
343 | ||
344 | TEST_ASSERT('S', "2e", numStrCache == count); | |
345 | set1->release(); | |
346 | } | |
347 | res = res && checkSpace("(S)2", spaceCheck, 0); | |
348 | ||
349 | // Test set creation from another set by both the setObject method | |
350 | // and the withArray factory. And test __takeObject code first | |
351 | // with tail removal then with head removal | |
352 | spaceCheck = checkPointSpace(); | |
353 | for (i = 0; i < numStrCache; i++) | |
354 | cache[i] = OSString::withCStringNoCopy(strCache[i]); | |
355 | set1 = OSSet::withObjects(cache, numStrCache, numStrCache); | |
356 | TEST_ASSERT('S', "3a", set1); | |
357 | for (i = 0; i < numStrCache; i++) | |
358 | cache[i]->release(); | |
359 | set2 = 0; | |
360 | if (set1) { | |
361 | set2 = OSSet::withCapacity(set1->getCount()); | |
362 | TEST_ASSERT('S', "3b", set2); | |
363 | TEST_ASSERT('S', "3c", !set2->getCount()); | |
364 | TEST_ASSERT('S', "3d", set2->setObject(set1)); | |
365 | TEST_ASSERT('S', "3e", set1->getCount() == set2->getCount()); | |
366 | } | |
367 | if (set2) { | |
368 | TEST_ASSERT('S', "3f", numStrCache == (int) set2->getCount()); | |
369 | count = count2 = 0; | |
370 | while ( (str = set2->getAnyObject()) ) { | |
371 | count += set2->__takeObject(str); | |
372 | count2 += set1->member(str); | |
373 | str->release(); | |
374 | } | |
375 | TEST_ASSERT('S', "3g", !set2->getCount()); | |
376 | TEST_ASSERT('S', "3h", numStrCache == count); | |
377 | TEST_ASSERT('S', "3i", numStrCache == count2); | |
378 | ||
379 | spaceCheck2 = checkPointSpace(); | |
380 | set2->flushCollection(); | |
381 | res = res && checkSpace("(S)3j", spaceCheck2, 0); | |
382 | ||
383 | set2->release(); | |
384 | set2 = 0; | |
385 | } | |
386 | if (set1) { | |
387 | set2 = OSSet::withSet(set1, numStrCache - 1); | |
388 | TEST_ASSERT('S', "3k", !set2); | |
389 | set2 = OSSet::withSet(set1, set1->getCount()); | |
390 | TEST_ASSERT('S', "3l", set2); | |
391 | set1->release(); | |
392 | } | |
393 | if (set2) { | |
394 | TEST_ASSERT('S', "3m", numStrCache == (int) set2->getCount()); | |
395 | i = count = count2 = 0; | |
396 | while ( (str = set2->getAnyObject()) ) { | |
397 | count += set2->__takeObject(str); | |
398 | count2 += (cache[i++] == str); | |
399 | str->release(); | |
400 | } | |
401 | TEST_ASSERT('S', "3n", !set2->getCount()); | |
402 | TEST_ASSERT('S', "3o", numStrCache == count); | |
403 | TEST_ASSERT('S', "3p", numStrCache == count2); | |
404 | ||
405 | set2->release(); | |
406 | set2 = 0; | |
407 | } | |
408 | res = res && checkSpace("(S)3", spaceCheck, 0); | |
409 | ||
410 | // Test duplicate removal | |
411 | spaceCheck = checkPointSpace(); | |
412 | set2 = 0; | |
413 | set1 = OSSet::withCapacity(numStrCache); | |
414 | TEST_ASSERT('S', "4a", set1); | |
415 | if (set1) { | |
416 | count = 0; | |
417 | for (i = 0; i < numStrCache; i++) { | |
418 | sym = (OSObject *) OSSymbol::withCStringNoCopy(strCache[i]); | |
419 | count += set1->setObject(sym); | |
420 | sym->release(); | |
421 | } | |
422 | TEST_ASSERT('S', "4b", numStrCache != (int) set1->getCount()); | |
423 | TEST_ASSERT('S', "4c", count == (int) set1->getCount()); | |
424 | ||
425 | count = count2 = 0; | |
426 | for (i = 0; i < numStrCache; i++) { | |
427 | sym = (OSObject *) OSSymbol::withCStringNoCopy(strCache[i]); | |
428 | count += set1->member(sym); | |
429 | count2 += sym->getRetainCount(); | |
430 | sym->release(); | |
431 | } | |
432 | TEST_ASSERT('S', "4d", count == numStrCache); | |
433 | TEST_ASSERT('S', "4e", count2 == numStrCache * 2); | |
434 | ||
435 | set2 = OSSet::withSet(set1, 2 * set1->getCount()); | |
436 | } | |
437 | TEST_ASSERT('S', "4f", set2); | |
438 | if (set2) { | |
439 | set2->setObject(set1); | |
440 | TEST_ASSERT('S', "4g", set1->getCount() == set2->getCount()); | |
441 | set1->release(); | |
442 | set2->release(); | |
443 | } | |
444 | res = res && checkSpace("(S)4", spaceCheck, 0); | |
445 | ||
446 | // Test array duplicate removal | |
447 | spaceCheck = checkPointSpace(); | |
448 | array = OSArray::withCapacity(numStrCache); | |
449 | for (i = 0; i < numStrCache; i++) { | |
450 | sym = (OSObject *) OSSymbol::withCStringNoCopy(strCache[i]); | |
451 | count += array->setObject(sym); | |
452 | sym->release(); | |
453 | } | |
454 | set1 = OSSet::withArray(array, numStrCache); | |
455 | TEST_ASSERT('S', "5a", set1); | |
456 | if (set1) { | |
457 | TEST_ASSERT('S', "5b", array->getCount() != set1->getCount()); | |
458 | array->release(); | |
459 | ||
460 | count = count2 = set1->getCount(); | |
461 | while ( (sym = set1->getAnyObject()) ) { | |
462 | count -= set1->__takeObject(sym); | |
463 | count2 -= sym->getRetainCount(); | |
464 | sym->release(); | |
465 | } | |
466 | TEST_ASSERT('S', "5c", !count); | |
467 | TEST_ASSERT('S', "5d", !count2); | |
468 | set1->release(); | |
469 | } | |
470 | res = res && checkSpace("(S)5", spaceCheck, 0); | |
471 | ||
472 | if (res) | |
473 | verPrintf(("testSet: All OSSet Tests passed\n")); | |
474 | else | |
475 | logPrintf(("testSet: Some OSSet Tests failed\n")); | |
476 | } | |
477 | ||
478 | void testDictionary() | |
479 | { | |
480 | bool res = true; | |
481 | void *spaceCheck, *spaceCheck2, *spaceCheck3; | |
482 | OSObject *cache[numStrCache]; | |
483 | OSString *str; | |
484 | const OSSymbol *symCache[numStrCache], *sym; | |
485 | OSDictionary *dict1, *dict2; | |
486 | int i, numSymbols, count1, count2; | |
487 | ||
488 | // Do first test without memory leak tests to initialise the metaclass | |
489 | dict1 = OSDictionary::withCapacity(1); | |
490 | TEST_ASSERT('D', "0a", dict1); | |
491 | if (dict1) | |
492 | dict1->release(); | |
493 | ||
494 | // Grow the symbol pool to maximum | |
495 | for (i = 0; i < numStrCache; i++) | |
496 | symCache[i] = OSSymbol::withCStringNoCopy(strCache[i]); | |
497 | for (i = 0; i < numStrCache; i++) | |
498 | symCache[i]->release(); | |
499 | ||
500 | // Create and destroy a dictionary | |
501 | spaceCheck = checkPointSpace(); | |
502 | dict1 = OSDictionary::withCapacity(1); | |
503 | TEST_ASSERT('D', "1a", dict1); | |
504 | if (dict1) { | |
505 | TEST_ASSERT('D', "1b", !dict1->getCount()); | |
506 | TEST_ASSERT('D', "1c", 1 == dict1->getCapacity()); | |
507 | TEST_ASSERT('D', "1d", 1 == dict1->getCapacityIncrement()); | |
508 | TEST_ASSERT('D', "1e", 4 == dict1->setCapacityIncrement(4)); | |
509 | TEST_ASSERT('D', "1f", 4 == dict1->getCapacityIncrement()); | |
510 | TEST_ASSERT('D', "1g", 8 == dict1->ensureCapacity(5)); | |
511 | ||
512 | spaceCheck2 = checkPointSpace(); | |
513 | sym = OSSymbol::withCStringNoCopy(strCache[0]); | |
514 | ||
515 | spaceCheck3 = checkPointSpace(); | |
516 | TEST_ASSERT('D', "1h", dict1->setObject((OSObject *) sym, sym)); | |
517 | TEST_ASSERT('D', "1i", (OSObject *) sym == dict1->getObject(sym)); | |
518 | sym->release(); | |
519 | TEST_ASSERT('D', "1i", 2 == sym->getRetainCount()); | |
520 | res = res && checkSpace("(D)1j", spaceCheck3, 0); | |
521 | ||
522 | TEST_ASSERT('D', "1k", 1 == dict1->getCount()); | |
523 | dict1->flushCollection(); | |
524 | TEST_ASSERT('D', "1l", !dict1->getCount()); | |
525 | res = res && checkSpace("(D)1m", spaceCheck2, 0); | |
526 | ||
527 | dict1->release(); | |
528 | } | |
529 | res = res && checkSpace("(D)1", spaceCheck, 0); | |
530 | ||
531 | // Check the creation of a sizable OSDictionary from an array of IOObjects | |
532 | // Also check indexing into the array. | |
533 | spaceCheck = checkPointSpace(); | |
534 | for (i = 0, numSymbols = 0; i < numStrCache; i++) { | |
535 | sym = OSSymbol::withCStringNoCopy(strCache[i]); | |
536 | if (1 == sym->getRetainCount()) | |
537 | symCache[numSymbols++] = sym; | |
538 | else | |
539 | sym->release(); | |
540 | } | |
541 | dict1 = OSDictionary::withObjects( | |
542 | (OSObject **) symCache, symCache, numSymbols, numSymbols); | |
543 | TEST_ASSERT('D', "2a", dict1); | |
544 | count1 = count2 = 0; | |
545 | for (i = 0; i < numSymbols; i++) | |
546 | count1 += (symCache[i]->getRetainCount() == 3); | |
547 | TEST_ASSERT('D', "2b", count1 == numSymbols); | |
548 | if (dict1) { | |
549 | TEST_ASSERT('D', "2c", numSymbols == (int) dict1->getCount()); | |
550 | TEST_ASSERT('D', "2d", numSymbols == (int) dict1->getCapacity()); | |
551 | TEST_ASSERT('D', "2e", | |
552 | numSymbols == (int) dict1->getCapacityIncrement()); | |
553 | ||
554 | for (i = dict1->getCount(); --i >= 0; ) { | |
555 | str = (OSString *) dict1->getObject(symCache[i]); | |
556 | if (str != (OSString *) symCache[i]) { | |
557 | verPrintf(("testDictionary(D) test 2f%d failed\n", i)); | |
558 | res = false; | |
559 | } | |
560 | } | |
561 | dict1->release(); | |
562 | } | |
563 | count1 = count2 = 0; | |
564 | for (i = 0; i < numSymbols; i++) { | |
565 | count1 += (symCache[i]->getRetainCount() == 1); | |
566 | symCache[i]->release(); | |
567 | } | |
568 | TEST_ASSERT('D', "2g", count1 == numSymbols); | |
569 | res = res && checkSpace("(D)2", spaceCheck, 0); | |
570 | ||
571 | // Check the creation of a sizable Dictionary from an array of IOStrings | |
572 | // Also check searching dictionary use OSString for a key. | |
573 | spaceCheck = checkPointSpace(); | |
574 | for (i = 0, numSymbols = 0; i < numStrCache; i++) { | |
575 | sym = OSSymbol::withCStringNoCopy(strCache[i]); | |
576 | if (1 == sym->getRetainCount()) { | |
577 | cache[numSymbols] = OSString::withCStringNoCopy(strCache[i]); | |
578 | symCache[numSymbols] = sym; | |
579 | numSymbols++; | |
580 | } | |
581 | else | |
582 | sym->release(); | |
583 | } | |
584 | dict1 = OSDictionary::withObjects((OSObject **) symCache, | |
585 | (OSString **) cache, | |
586 | numSymbols, numSymbols); | |
587 | TEST_ASSERT('D', "3a", dict1); | |
588 | count1 = count2 = 0; | |
589 | for (i = 0; i < numSymbols; i++) { | |
590 | count1 += (symCache[i]->getRetainCount() == 3); | |
591 | count2 += (cache[i]->getRetainCount() == 1); | |
592 | } | |
593 | TEST_ASSERT('D', "3b", count1 == numSymbols); | |
594 | TEST_ASSERT('D', "3c", count2 == numSymbols); | |
595 | if (dict1) { | |
596 | count1 = count2 = 0; | |
597 | for (i = 0; i < numSymbols; i++) { | |
598 | str = (OSString *) cache[i]; | |
599 | count1 += (symCache[i] == (const OSSymbol *) dict1->getObject(str)); | |
600 | count2 += (symCache[i]->getRetainCount() == 3); | |
601 | } | |
602 | TEST_ASSERT('D', "3d", count1 == numSymbols); | |
603 | TEST_ASSERT('D', "3e", count2 == numSymbols); | |
604 | ||
605 | count1 = count2 = 0; | |
606 | for (i = 0; i < numSymbols; i++) { | |
607 | const char *cStr = ((OSString *) cache[i])->getCStringNoCopy(); | |
608 | ||
609 | count1 += (symCache[i] == (const OSSymbol *) dict1->getObject(cStr)); | |
610 | count2 += (symCache[i]->getRetainCount() == 3); | |
611 | } | |
612 | TEST_ASSERT('D', "3f", count1 == numSymbols); | |
613 | TEST_ASSERT('D', "3g", count2 == numSymbols); | |
614 | ||
615 | dict1->release(); | |
616 | } | |
617 | count1 = count2 = 0; | |
618 | for (i = 0; i < numSymbols; i++) { | |
619 | count1 += (symCache[i]->getRetainCount() == 1); | |
620 | count2 += (cache[i]->getRetainCount() == 1); | |
621 | symCache[i]->release(); | |
622 | cache[i]->release(); | |
623 | } | |
624 | TEST_ASSERT('D', "3h", count1 == numSymbols); | |
625 | res = res && checkSpace("(D)3", spaceCheck, 0); | |
626 | ||
627 | // Check the creation of a small dictionary then grow it one item at a time | |
628 | // Create a new dictionary from the old dictionary. | |
629 | // Finally remove each item permanently. | |
630 | spaceCheck = checkPointSpace(); | |
631 | for (i = 0, numSymbols = 0; i < numStrCache; i++) { | |
632 | sym = OSSymbol::withCStringNoCopy(strCache[i]); | |
633 | if (1 == sym->getRetainCount()) { | |
634 | cache[numSymbols] = OSString::withCStringNoCopy(strCache[i]); | |
635 | symCache[numSymbols] = sym; | |
636 | numSymbols++; | |
637 | } | |
638 | else | |
639 | sym->release(); | |
640 | } | |
641 | dict2 = 0; | |
642 | dict1 = OSDictionary::withCapacity(1); | |
643 | TEST_ASSERT('D', "4a", dict1); | |
644 | if (dict1) { | |
645 | count1 = count2 = 0; | |
646 | for (i = 0; i < numSymbols; i++) { | |
647 | sym = symCache[i]; | |
648 | count1 += ((OSObject *) sym == dict1->setObject((OSObject *) sym, | |
649 | sym->getCStringNoCopy())); | |
650 | count2 += (sym->getRetainCount() == 3); | |
651 | } | |
652 | TEST_ASSERT('D', "4b", numSymbols == (int) dict1->getCount()); | |
653 | TEST_ASSERT('D', "4c", numSymbols == count1); | |
654 | TEST_ASSERT('D', "4d", numSymbols == count2); | |
655 | ||
656 | dict2 = OSDictionary::withDictionary(dict1, numSymbols-1); | |
657 | TEST_ASSERT('D', "4b", !dict2); | |
658 | dict2 = OSDictionary::withDictionary(dict1, numSymbols); | |
659 | } | |
660 | TEST_ASSERT('D', "4e", dict2); | |
661 | if (dict2) { | |
662 | dict1->release(); dict1 = 0; | |
663 | ||
664 | TEST_ASSERT('D', "4f", numSymbols == (int) dict2->getCount()); | |
665 | ||
666 | count1 = count2 = 0; | |
667 | for (i = 0; i < numSymbols; i++) { | |
668 | OSObject *replacedObject; | |
669 | ||
670 | sym = symCache[i]; | |
671 | str = (OSString *) cache[i]; | |
672 | replacedObject = dict2->setObject(str, str); | |
673 | count1 += ((OSString *) sym == replacedObject); | |
674 | replacedObject->release(); | |
675 | count2 += (sym->getRetainCount() == 2); | |
676 | str->release(); | |
677 | } | |
678 | TEST_ASSERT('D', "4g", numSymbols == count1); | |
679 | TEST_ASSERT('D', "4h", numSymbols == count2); | |
680 | ||
681 | count1 = count2 = 0; | |
682 | for (i = 0; i < numSymbols; i++) { | |
683 | sym = symCache[i]; | |
684 | str = (OSString *) cache[i]; | |
685 | count1 += (str == dict2->__takeObject(sym)); | |
686 | str->release(); | |
687 | count2 += (sym->getRetainCount() == 1); | |
688 | sym->release(); | |
689 | } | |
690 | TEST_ASSERT('D', "4i", numSymbols == count1); | |
691 | TEST_ASSERT('D', "4j", numSymbols == count2); | |
692 | TEST_ASSERT('D', "4k", !dict2->getCount()); | |
693 | dict2->release(); dict2 = 0; | |
694 | } | |
695 | else if (dict1) | |
696 | dict1->release(); | |
697 | res = res && checkSpace("(D)4", spaceCheck, 0); | |
698 | ||
699 | if (res) | |
700 | verPrintf(("testDictionary: All OSDictionary Tests passed\n")); | |
701 | else | |
702 | logPrintf(("testDictionary: Some OSDictionary Tests failed\n")); | |
703 | } | |
704 | ||
705 | void testIterator() | |
706 | { | |
707 | bool res = true; | |
708 | void *spaceCheck; | |
709 | OSObject *cache[numStrCache]; | |
710 | OSString *str = 0; | |
711 | const OSSymbol *symCache[numStrCache], *sym; | |
712 | OSDictionary *dict; | |
713 | OSSet *set; | |
714 | OSArray *array, *bigReturn; | |
715 | OSCollectionIterator *iter1, *iter2; | |
716 | int i, numSymbols, count1, count2, count3; | |
717 | ||
718 | // Setup symbol and string pools | |
719 | for (i = 0, numSymbols = 0; i < numStrCache; i++) { | |
720 | sym = OSSymbol::withCStringNoCopy(strCache[i]); | |
721 | if (1 == sym->getRetainCount()) { | |
722 | cache[numSymbols] = OSString::withCStringNoCopy(strCache[i]); | |
723 | symCache[numSymbols] = sym; | |
724 | numSymbols++; | |
725 | } | |
726 | else | |
727 | sym->release(); | |
728 | } | |
729 | ||
730 | // Test the array iterator | |
731 | spaceCheck = checkPointSpace(); | |
732 | iter1 = iter2 = 0; | |
733 | array = OSArray::withCapacity(numSymbols); | |
734 | TEST_ASSERT('I', "1a", array); | |
735 | if (array) { | |
736 | count1 = count2 = 0; | |
737 | for (i = numSymbols; --i >= 0; ) | |
738 | count1 += array->setObject(cache[i], 0); | |
739 | TEST_ASSERT('I', "1b", count1 == numSymbols); | |
740 | ||
741 | iter1 = OSCollectionIterator::withCollection(array); | |
742 | iter2 = OSCollectionIterator::withCollection(array); | |
743 | } | |
744 | TEST_ASSERT('I', "1c", iter1); | |
745 | TEST_ASSERT('I', "1d", iter2); | |
746 | if (iter1 && iter2) { | |
747 | count1 = count2 = count3 = 0; | |
748 | for (i = 0; (str = (IOString *) iter1->getNextObject()); i++) { | |
749 | bigReturn = iter2->nextEntries(); | |
750 | count1 += (bigReturn->getCount() == 1); | |
751 | count2 += (cache[i] == bigReturn->getObject(0)); | |
752 | count3 += (cache[i] == str); | |
753 | } | |
754 | TEST_ASSERT('I', "1e", count1 == numSymbols); | |
755 | TEST_ASSERT('I', "1f", count2 == numSymbols); | |
756 | TEST_ASSERT('I', "1g", count3 == numSymbols); | |
757 | TEST_ASSERT('I', "1h", iter1->valid()); | |
758 | TEST_ASSERT('I', "1i", iter2->valid()); | |
759 | ||
760 | iter1->reset(); | |
761 | str = (OSString *) array->__takeObject(0); | |
762 | array->setObject(str, 0); | |
763 | str->release(); | |
764 | TEST_ASSERT('I', "1j", !iter1->getNextObject()); | |
765 | TEST_ASSERT('I', "1k", !iter1->valid()); | |
766 | ||
767 | iter1->reset(); | |
768 | count1 = count2 = count3 = 0; | |
769 | for (i = 0; ; i++) { | |
770 | if (i & 1) | |
771 | str = (OSString *) iter1->getNextObject(); | |
772 | else if ( (bigReturn = iter1->nextEntries()) ) | |
773 | str = (OSString *) bigReturn->getObject(0); | |
774 | else | |
775 | str = 0; | |
776 | ||
777 | if (!str) | |
778 | break; | |
779 | count1 += (cache[i] == str); | |
780 | } | |
781 | TEST_ASSERT('I', "1l", count1 == numSymbols); | |
782 | TEST_ASSERT('I', "1m", i == numSymbols); | |
783 | TEST_ASSERT('I', "1n", iter1->valid()); | |
784 | ||
785 | TEST_ASSERT('I', "1o", 3 == array->getRetainCount()); | |
786 | array->release(); | |
787 | } | |
788 | ||
789 | if (iter1) iter1->release(); | |
790 | if (iter2) iter2->release(); | |
791 | res = res && checkSpace("(I)1", spaceCheck, 0); | |
792 | ||
793 | // Test the set iterator | |
794 | spaceCheck = checkPointSpace(); | |
795 | iter1 = 0; | |
796 | set = OSSet::withCapacity(numSymbols); | |
797 | TEST_ASSERT('I', "2a", set); | |
798 | if (set) { | |
799 | count1 = count2 = 0; | |
800 | for (i = 0; i < numSymbols; i++) | |
801 | count1 += set->setObject(cache[i]); | |
802 | TEST_ASSERT('I', "2b", count1 == numSymbols); | |
803 | ||
804 | iter1 = OSCollectionIterator::withCollection(set); | |
805 | iter2 = OSCollectionIterator::withCollection(set); | |
806 | } | |
807 | TEST_ASSERT('I', "2c", iter1); | |
808 | TEST_ASSERT('I', "2d", iter2); | |
809 | if (iter1 && iter2) { | |
810 | count1 = count2 = count3 = 0; | |
811 | for (i = 0; (str = (IOString *) iter1->getNextObject()); i++) { | |
812 | bigReturn = iter2->nextEntries(); | |
813 | count1 += (bigReturn->getCount() == 1); | |
814 | count2 += (cache[i] == bigReturn->getObject(0)); | |
815 | count3 += (cache[i] == str); | |
816 | } | |
817 | TEST_ASSERT('I', "2e", count1 == numSymbols); | |
818 | TEST_ASSERT('I', "2f", count2 == numSymbols); | |
819 | TEST_ASSERT('I', "2g", count3 == numSymbols); | |
820 | TEST_ASSERT('I', "2h", iter1->valid()); | |
821 | TEST_ASSERT('I', "2i", iter2->valid()); | |
822 | ||
823 | iter1->reset(); | |
824 | count1 = count2 = count3 = 0; | |
825 | for (i = 0; ; i++) { | |
826 | if (i & 1) | |
827 | str = (OSString *) iter1->getNextObject(); | |
828 | else if ( (bigReturn = iter1->nextEntries()) ) | |
829 | str = (OSString *) bigReturn->getObject(0); | |
830 | else | |
831 | str = 0; | |
832 | ||
833 | if (!str) | |
834 | break; | |
835 | count1 += (cache[i] == str); | |
836 | } | |
837 | TEST_ASSERT('I', "2l", count1 == numSymbols); | |
838 | TEST_ASSERT('I', "2m", i == numSymbols); | |
839 | TEST_ASSERT('I', "2n", iter1->valid()); | |
840 | ||
841 | iter1->reset(); | |
842 | str = (OSString *) set->getAnyObject(); | |
843 | (void) set->__takeObject(str); | |
844 | set->setObject(str); | |
845 | str->release(); | |
846 | TEST_ASSERT('I', "2j", !iter1->getNextObject()); | |
847 | TEST_ASSERT('I', "2k", !iter1->valid()); | |
848 | ||
849 | TEST_ASSERT('I', "2o", 3 == set->getRetainCount()); | |
850 | set->release(); | |
851 | } | |
852 | ||
853 | if (iter1) iter1->release(); | |
854 | if (iter2) iter2->release(); | |
855 | res = res && checkSpace("(I)2", spaceCheck, 0); | |
856 | ||
857 | // Test the dictionary iterator | |
858 | spaceCheck = checkPointSpace(); | |
859 | iter1 = 0; | |
860 | dict = OSDictionary::withCapacity(numSymbols); | |
861 | TEST_ASSERT('I', "3a", dict); | |
862 | if (dict) { | |
863 | count1 = count2 = 0; | |
864 | for (i = 0; i < numSymbols; i++) | |
865 | count1 += (0 != dict->setObject(cache[i], symCache[i])); | |
866 | TEST_ASSERT('I', "3b", count1 == numSymbols); | |
867 | ||
868 | iter1 = OSCollectionIterator::withCollection(dict); | |
869 | iter2 = OSCollectionIterator::withCollection(dict); | |
870 | } | |
871 | TEST_ASSERT('I', "3c", iter1); | |
872 | TEST_ASSERT('I', "3d", iter2); | |
873 | if (iter1 && iter2) { | |
874 | count1 = count2 = count3 = 0; | |
875 | for (i = 0; (sym = (const IOSymbol *) iter1->getNextObject()); i++) { | |
876 | bigReturn = iter2->nextEntries(); | |
877 | count1 += (bigReturn->getCount() == 2); | |
878 | count2 += (cache[i] == bigReturn->getObject(1)); | |
879 | count3 += (symCache[i] == sym); | |
880 | } | |
881 | TEST_ASSERT('I', "3e", count1 == numSymbols); | |
882 | TEST_ASSERT('I', "3f", count2 == numSymbols); | |
883 | TEST_ASSERT('I', "3g", count3 == numSymbols); | |
884 | TEST_ASSERT('I', "3h", iter1->valid()); | |
885 | TEST_ASSERT('I', "3i", iter2->valid()); | |
886 | ||
887 | iter1->reset(); | |
888 | count1 = count2 = count3 = 0; | |
889 | i = 0; | |
890 | for (i = 0; ; i++) { | |
891 | if (i & 1) { | |
892 | sym = (const OSSymbol *) iter1->getNextObject(); | |
893 | str = 0; | |
894 | } | |
895 | else if ( (bigReturn = iter1->nextEntries()) ) { | |
896 | sym = (const OSSymbol *) bigReturn->getObject(0); | |
897 | str = (OSString *) bigReturn->getObject(1); | |
898 | } | |
899 | else | |
900 | sym = 0; | |
901 | ||
902 | if (!sym) | |
903 | break; | |
904 | ||
905 | count1 += (symCache[i] == sym); | |
906 | count2 += (!str || cache[i] == str); | |
907 | } | |
908 | TEST_ASSERT('I', "3l", count1 == numSymbols); | |
909 | TEST_ASSERT('I', "3m", count2 == numSymbols); | |
910 | TEST_ASSERT('I', "3n", i == numSymbols); | |
911 | TEST_ASSERT('I', "3o", iter1->valid()); | |
912 | ||
913 | iter1->reset(); | |
914 | str = (OSString *) dict->__takeObject(symCache[numSymbols-1]); | |
915 | dict->setObject(str, symCache[numSymbols-1]); | |
916 | str->release(); | |
917 | TEST_ASSERT('I', "3j", !iter1->getNextObject()); | |
918 | TEST_ASSERT('I', "3k", !iter1->valid()); | |
919 | ||
920 | TEST_ASSERT('I', "3p", 3 == dict->getRetainCount()); | |
921 | dict->release(); | |
922 | } | |
923 | ||
924 | if (iter1) iter1->release(); | |
925 | if (iter2) iter2->release(); | |
926 | res = res && checkSpace("(I)3", spaceCheck, 0); | |
927 | ||
928 | count1 = count2 = count3 = 0; | |
929 | for (i = 0; i < numSymbols; i++) { | |
930 | count1 += (1 == cache[i]->getRetainCount()); | |
931 | count2 += (1 == symCache[i]->getRetainCount()); | |
932 | cache[i]->release(); | |
933 | symCache[i]->release(); | |
934 | } | |
935 | TEST_ASSERT('I', "4a", count1 == numSymbols); | |
936 | TEST_ASSERT('I', "4b", count2 == numSymbols); | |
937 | ||
938 | if (res) | |
939 | verPrintf(("testIterator: All OSCollectionIterator Tests passed\n")); | |
940 | else | |
941 | logPrintf(("testIterator: Some OSCollectionIterator Tests failed\n")); | |
942 | } | |
943 | ||
944 | #endif /* DEBUG */ |