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