]>
git.saurik.com Git - apple/xnu.git/blob - iokit/Tests/TestCollections.cpp
2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
31 #include <libkern/c++/OSArray.h>
32 #include <libkern/c++/OSSet.h>
33 #include <libkern/c++/OSDictionary.h>
34 #include <libkern/c++/OSString.h>
35 #include <libkern/c++/OSSymbol.h>
36 #include <libkern/c++/OSCollectionIterator.h>
42 void *spaceCheck
, *spaceCheck2
, *spaceCheck3
;
43 int i
, j
, count
, count2
;
44 OSObject
*cache
[numStrCache
], *str
, *sym
;
45 OSArray
*array1
, *array2
;
47 // Do first test without memory leak tests to initialise the metaclass
48 array1
= OSArray::withCapacity(1);
49 TEST_ASSERT('A', "0a", array1
);
54 // Grow the symbol pool to maximum
55 for (i
= 0; i
< numStrCache
; i
++) {
56 cache
[i
] = (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[i
]);
58 for (i
= 0; i
< numStrCache
; i
++) {
62 // Create and destroy an array
63 spaceCheck
= checkPointSpace();
64 array1
= OSArray::withCapacity(1);
65 TEST_ASSERT('A', "1a", array1
);
67 TEST_ASSERT('A', "1b", !array1
->getCount());
68 TEST_ASSERT('A', "1c", 1 == array1
->getCapacity());
69 TEST_ASSERT('A', "1d", 1 == array1
->getCapacityIncrement());
70 TEST_ASSERT('A', "1e", 4 == array1
->setCapacityIncrement(4));
71 TEST_ASSERT('A', "1f", 4 == array1
->getCapacityIncrement());
72 TEST_ASSERT('A', "1g", 8 == array1
->ensureCapacity(5));
74 spaceCheck2
= checkPointSpace();
75 cache
[0] = IOString::withCStringNoCopy(strCache
[0]);
77 spaceCheck3
= checkPointSpace();
78 TEST_ASSERT('A', "1h", array1
->setObject(cache
[0]));
79 TEST_ASSERT('A', "1i", cache
[0] == array1
->getObject(0));
81 res
= res
&& checkSpace("(A)1j", spaceCheck3
, 0);
83 TEST_ASSERT('A', "1k", 1 == array1
->getCount());
84 array1
->flushCollection();
85 TEST_ASSERT('A', "1l", !array1
->getCount());
86 res
= res
&& checkSpace("(A)1m", spaceCheck2
, 0);
90 res
= res
&& checkSpace("(A)1", spaceCheck
, 0);
92 // Check the creation of a sizable OSArray from an array of IOObjects
93 // Also check indexing into the array.
94 spaceCheck
= checkPointSpace();
95 for (i
= 0; i
< numStrCache
; i
++) {
96 cache
[i
] = OSString::withCStringNoCopy(strCache
[i
]);
98 array1
= OSArray::withObjects(cache
, numStrCache
, numStrCache
);
99 TEST_ASSERT('A', "2a", array1
);
100 for (i
= 0; i
< numStrCache
; i
++) {
104 TEST_ASSERT('A', "2b", numStrCache
== (int) array1
->getCount());
105 TEST_ASSERT('A', "2c", numStrCache
== (int) array1
->getCapacity());
106 TEST_ASSERT('A', "2d",
107 numStrCache
== (int) array1
->getCapacityIncrement());
109 for (i
= 0; (str
= array1
->getObject(i
)); i
++) {
110 if (str
!= cache
[i
]) {
111 verPrintf(("testArray(A) test 2e%d failed\n", i
));
115 TEST_ASSERT('A', "2f", numStrCache
== i
);
118 res
= res
&& checkSpace("(A)2", spaceCheck
, 0);
120 // Test array creation from another array by both the setObject method
121 // and the withArray factory. And test __takeObject code first
122 // with tail removal then with head removal
123 spaceCheck
= checkPointSpace();
124 for (i
= 0; i
< numStrCache
; i
++) {
125 cache
[i
] = OSString::withCStringNoCopy(strCache
[i
]);
127 array1
= OSArray::withObjects(cache
, numStrCache
, numStrCache
);
128 TEST_ASSERT('A', "3a", array1
);
129 for (i
= 0; i
< numStrCache
; i
++) {
134 array2
= OSArray::withCapacity(1);
135 TEST_ASSERT('A', "3b", array2
);
136 TEST_ASSERT('A', "3c", !array2
->getCount());
137 TEST_ASSERT('A', "3d", array2
->setObject(array1
));
138 TEST_ASSERT('A', "3e", array1
->getCount() == array2
->getCount());
142 TEST_ASSERT('A', "3f", numStrCache
== (int) array2
->getCount());
143 for (i
= array2
->getCount(); (str
= array2
->__takeObject(--i
));) {
144 if (str
!= cache
[i
]) {
145 verPrintf(("testArray(A) test 3g%d failed\n", i
));
148 count
+= ((int) array2
->getCount() == i
);
151 TEST_ASSERT('A', "3h", count
== numStrCache
);
152 TEST_ASSERT('A', "3i", -1 == i
);
153 TEST_ASSERT('A', "3j", !array2
->getCount());
155 spaceCheck2
= checkPointSpace();
156 array2
->flushCollection();
157 res
= res
&& checkSpace("(A)3k", spaceCheck2
, 0);
163 array2
= OSArray::withArray(array1
, numStrCache
- 1);
164 TEST_ASSERT('A', "3l", !array2
);
165 array2
= OSArray::withArray(array1
, array1
->getCount());
166 TEST_ASSERT('A', "3m", array2
);
171 TEST_ASSERT('A', "3o", numStrCache
== (int) array2
->getCount());
172 for (i
= 0; (str
= array2
->__takeObject(0)); i
++) {
173 count
+= (str
== cache
[i
]);
176 TEST_ASSERT('A', "3p", count
== numStrCache
);
177 TEST_ASSERT('A', "3q", !array2
->getCount());
181 res
= res
&& checkSpace("(A)3", spaceCheck
, 0);
183 // Test object replacement from one array to another
184 spaceCheck
= checkPointSpace();
185 array1
= OSArray::withCapacity(numStrCache
);
186 TEST_ASSERT('A', "4a", array1
);
189 for (i
= 0; i
< numStrCache
; i
++) {
190 str
= OSString::withCStringNoCopy(strCache
[i
]);
191 count
+= array1
->setObject(str
);
192 count2
+= (str
== array1
->lastObject());
195 TEST_ASSERT('A', "4b", numStrCache
== (int) array1
->getCount());
196 TEST_ASSERT('A', "4c", count
== numStrCache
);
197 TEST_ASSERT('A', "4d", count2
== numStrCache
);
199 array2
= OSArray::withCapacity(1);
200 TEST_ASSERT('A', "4e", array2
);
203 str
= (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[0]);
204 for (i
= 0; i
< numStrCache
; i
++) {
205 sym
= (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[i
]);
206 count
+= array2
->setObject(sym
, 0);
207 count2
+= (str
== array2
->lastObject());
211 TEST_ASSERT('A', "4f", numStrCache
== (int) array2
->getCount());
212 TEST_ASSERT('A', "4g", count
== numStrCache
);
213 TEST_ASSERT('A', "4h", count2
== numStrCache
);
215 if (array1
&& array2
) {
217 for (i
= array1
->getCount() - 1; (sym
= array2
->__takeObject(0)); i
--) {
218 str
= array1
->replaceObject(sym
, i
);
220 count2
+= (sym
!= str
);
228 TEST_ASSERT('A', "4k", numStrCache
== (int) array1
->getCount());
229 TEST_ASSERT('A', "4l", count
== numStrCache
);
230 TEST_ASSERT('A', "4m", count2
== numStrCache
);
241 res
= res
&& checkSpace("(A)4", spaceCheck
, 0);
243 // Test array duplicate removal
244 spaceCheck
= checkPointSpace();
245 array1
= OSArray::withCapacity(numStrCache
);
246 TEST_ASSERT('A', "5a", array1
);
248 for (i
= 0; i
< numStrCache
; i
++) {
249 sym
= (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[i
]);
250 count
+= array1
->setObject(sym
);
253 TEST_ASSERT('A', "5b", numStrCache
== (int) array1
->getCount());
256 for (i
= 0; (sym
= array1
->getObject(i
));) {
257 if (sym
->getRetainCount() == 1) {
260 //sym = array1->__takeObject(i);
262 array1
->removeObject(i
);
265 TEST_ASSERT('A', "5c", numStrCache
!= (int) array1
->getCount());
267 // check to see that all symbols are really there
268 for (count
= 0, i
= 0; i
< numStrCache
; i
++) {
269 sym
= (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[i
]);
270 for (count2
= false, j
= 0; (str
= array1
->getObject(j
)); j
++) {
279 TEST_ASSERT('A', "5c", count
== numStrCache
);
282 res
= res
&& checkSpace("(S)5", spaceCheck
, 0);
285 verPrintf(("testArray: All OSArray Tests passed\n"));
287 logPrintf(("testArray: Some OSArray Tests failed\n"));
295 void *spaceCheck
, *spaceCheck2
, *spaceCheck3
;
296 int i
, count
, count2
;
297 OSObject
*cache
[numStrCache
], *str
, *sym
;
301 // Do first test without memory leak tests to initialise the metaclass
302 set1
= OSSet::withCapacity(1);
303 TEST_ASSERT('S', "0a", set1
);
308 // Grow the symbol pool to maximum
309 for (i
= 0; i
< numStrCache
; i
++) {
310 cache
[i
] = (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[i
]);
312 for (i
= 0; i
< numStrCache
; i
++) {
316 // Create and destroy an set
317 spaceCheck
= checkPointSpace();
318 set1
= OSSet::withCapacity(1);
319 TEST_ASSERT('S', "1a", set1
);
321 TEST_ASSERT('S', "1b", !set1
->getCount());
322 TEST_ASSERT('S', "1c", 1 == set1
->getCapacity());
323 TEST_ASSERT('S', "1d", 1 == set1
->getCapacityIncrement());
324 TEST_ASSERT('S', "1e", 4 == set1
->setCapacityIncrement(4));
325 TEST_ASSERT('S', "1f", 4 == set1
->getCapacityIncrement());
326 TEST_ASSERT('S', "1g", 8 == set1
->ensureCapacity(5));
328 spaceCheck2
= checkPointSpace();
329 cache
[0] = IOString::withCStringNoCopy(strCache
[0]);
331 spaceCheck3
= checkPointSpace();
332 TEST_ASSERT('S', "1h", set1
->setObject(cache
[0]));
333 TEST_ASSERT('S', "1i", set1
->containsObject(cache
[0]));
334 TEST_ASSERT('S', "1j", cache
[0] == set1
->getAnyObject());
336 res
= res
&& checkSpace("(S)1k", spaceCheck3
, 0);
338 TEST_ASSERT('S', "1l", 1 == set1
->getCount());
339 set1
->flushCollection();
340 TEST_ASSERT('S', "1m", !set1
->getCount());
341 res
= res
&& checkSpace("(S)1n", spaceCheck2
, 0);
345 res
= res
&& checkSpace("(S)1", spaceCheck
, 0);
347 // Check the creation of a sizable OSSet from an set of IOObjects
348 // Also check member test of set.
349 spaceCheck
= checkPointSpace();
350 for (i
= 0; i
< numStrCache
; i
++) {
351 cache
[i
] = OSString::withCStringNoCopy(strCache
[i
]);
353 set1
= OSSet::withObjects(cache
, numStrCache
, numStrCache
);
354 TEST_ASSERT('S', "2a", set1
);
355 for (i
= 0; i
< numStrCache
; i
++) {
359 TEST_ASSERT('S', "2b", numStrCache
== (int) set1
->getCount());
360 TEST_ASSERT('S', "2c", numStrCache
== (int) set1
->getCapacity());
361 TEST_ASSERT('S', "2d",
362 numStrCache
== (int) set1
->getCapacityIncrement());
365 for (i
= set1
->getCount(); --i
>= 0;) {
366 count
+= set1
->member(cache
[i
]);
369 TEST_ASSERT('S', "2e", numStrCache
== count
);
372 res
= res
&& checkSpace("(S)2", spaceCheck
, 0);
374 // Test set creation from another set by both the setObject method
375 // and the withArray factory. And test __takeObject code first
376 // with tail removal then with head removal
377 spaceCheck
= checkPointSpace();
378 for (i
= 0; i
< numStrCache
; i
++) {
379 cache
[i
] = OSString::withCStringNoCopy(strCache
[i
]);
381 set1
= OSSet::withObjects(cache
, numStrCache
, numStrCache
);
382 TEST_ASSERT('S', "3a", set1
);
383 for (i
= 0; i
< numStrCache
; i
++) {
388 set2
= OSSet::withCapacity(set1
->getCount());
389 TEST_ASSERT('S', "3b", set2
);
390 TEST_ASSERT('S', "3c", !set2
->getCount());
391 TEST_ASSERT('S', "3d", set2
->setObject(set1
));
392 TEST_ASSERT('S', "3e", set1
->getCount() == set2
->getCount());
395 TEST_ASSERT('S', "3f", numStrCache
== (int) set2
->getCount());
397 while ((str
= set2
->getAnyObject())) {
398 count
+= set2
->__takeObject(str
);
399 count2
+= set1
->member(str
);
402 TEST_ASSERT('S', "3g", !set2
->getCount());
403 TEST_ASSERT('S', "3h", numStrCache
== count
);
404 TEST_ASSERT('S', "3i", numStrCache
== count2
);
406 spaceCheck2
= checkPointSpace();
407 set2
->flushCollection();
408 res
= res
&& checkSpace("(S)3j", spaceCheck2
, 0);
414 set2
= OSSet::withSet(set1
, numStrCache
- 1);
415 TEST_ASSERT('S', "3k", !set2
);
416 set2
= OSSet::withSet(set1
, set1
->getCount());
417 TEST_ASSERT('S', "3l", set2
);
421 TEST_ASSERT('S', "3m", numStrCache
== (int) set2
->getCount());
422 i
= count
= count2
= 0;
423 while ((str
= set2
->getAnyObject())) {
424 count
+= set2
->__takeObject(str
);
425 count2
+= (cache
[i
++] == str
);
428 TEST_ASSERT('S', "3n", !set2
->getCount());
429 TEST_ASSERT('S', "3o", numStrCache
== count
);
430 TEST_ASSERT('S', "3p", numStrCache
== count2
);
435 res
= res
&& checkSpace("(S)3", spaceCheck
, 0);
437 // Test duplicate removal
438 spaceCheck
= checkPointSpace();
440 set1
= OSSet::withCapacity(numStrCache
);
441 TEST_ASSERT('S', "4a", set1
);
444 for (i
= 0; i
< numStrCache
; i
++) {
445 sym
= (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[i
]);
446 count
+= set1
->setObject(sym
);
449 TEST_ASSERT('S', "4b", numStrCache
!= (int) set1
->getCount());
450 TEST_ASSERT('S', "4c", count
== (int) set1
->getCount());
453 for (i
= 0; i
< numStrCache
; i
++) {
454 sym
= (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[i
]);
455 count
+= set1
->member(sym
);
456 count2
+= sym
->getRetainCount();
459 TEST_ASSERT('S', "4d", count
== numStrCache
);
460 TEST_ASSERT('S', "4e", count2
== numStrCache
* 2);
462 set2
= OSSet::withSet(set1
, 2 * set1
->getCount());
464 TEST_ASSERT('S', "4f", set2
);
466 set2
->setObject(set1
);
467 TEST_ASSERT('S', "4g", set1
->getCount() == set2
->getCount());
471 res
= res
&& checkSpace("(S)4", spaceCheck
, 0);
473 // Test array duplicate removal
474 spaceCheck
= checkPointSpace();
475 array
= OSArray::withCapacity(numStrCache
);
476 for (i
= 0; i
< numStrCache
; i
++) {
477 sym
= (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[i
]);
478 count
+= array
->setObject(sym
);
481 set1
= OSSet::withArray(array
, numStrCache
);
482 TEST_ASSERT('S', "5a", set1
);
484 TEST_ASSERT('S', "5b", array
->getCount() != set1
->getCount());
487 count
= count2
= set1
->getCount();
488 while ((sym
= set1
->getAnyObject())) {
489 count
-= set1
->__takeObject(sym
);
490 count2
-= sym
->getRetainCount();
493 TEST_ASSERT('S', "5c", !count
);
494 TEST_ASSERT('S', "5d", !count2
);
497 res
= res
&& checkSpace("(S)5", spaceCheck
, 0);
500 verPrintf(("testSet: All OSSet Tests passed\n"));
502 logPrintf(("testSet: Some OSSet Tests failed\n"));
510 void *spaceCheck
, *spaceCheck2
, *spaceCheck3
;
511 OSObject
*cache
[numStrCache
];
513 const OSSymbol
*symCache
[numStrCache
], *sym
;
514 OSDictionary
*dict1
, *dict2
;
515 int i
, numSymbols
, count1
, count2
;
517 // Do first test without memory leak tests to initialise the metaclass
518 dict1
= OSDictionary::withCapacity(1);
519 TEST_ASSERT('D', "0a", dict1
);
524 // Grow the symbol pool to maximum
525 for (i
= 0; i
< numStrCache
; i
++) {
526 symCache
[i
] = OSSymbol::withCStringNoCopy(strCache
[i
]);
528 for (i
= 0; i
< numStrCache
; i
++) {
529 symCache
[i
]->release();
532 // Create and destroy a dictionary
533 spaceCheck
= checkPointSpace();
534 dict1
= OSDictionary::withCapacity(1);
535 TEST_ASSERT('D', "1a", dict1
);
537 TEST_ASSERT('D', "1b", !dict1
->getCount());
538 TEST_ASSERT('D', "1c", 1 == dict1
->getCapacity());
539 TEST_ASSERT('D', "1d", 1 == dict1
->getCapacityIncrement());
540 TEST_ASSERT('D', "1e", 4 == dict1
->setCapacityIncrement(4));
541 TEST_ASSERT('D', "1f", 4 == dict1
->getCapacityIncrement());
542 TEST_ASSERT('D', "1g", 8 == dict1
->ensureCapacity(5));
544 spaceCheck2
= checkPointSpace();
545 sym
= OSSymbol::withCStringNoCopy(strCache
[0]);
547 spaceCheck3
= checkPointSpace();
548 TEST_ASSERT('D', "1h", dict1
->setObject((OSObject
*) sym
, sym
));
549 TEST_ASSERT('D', "1i", (OSObject
*) sym
== dict1
->getObject(sym
));
551 TEST_ASSERT('D', "1i", 2 == sym
->getRetainCount());
552 res
= res
&& checkSpace("(D)1j", spaceCheck3
, 0);
554 TEST_ASSERT('D', "1k", 1 == dict1
->getCount());
555 dict1
->flushCollection();
556 TEST_ASSERT('D', "1l", !dict1
->getCount());
557 res
= res
&& checkSpace("(D)1m", spaceCheck2
, 0);
561 res
= res
&& checkSpace("(D)1", spaceCheck
, 0);
563 // Check the creation of a sizable OSDictionary from an array of IOObjects
564 // Also check indexing into the array.
565 spaceCheck
= checkPointSpace();
566 for (i
= 0, numSymbols
= 0; i
< numStrCache
; i
++) {
567 sym
= OSSymbol::withCStringNoCopy(strCache
[i
]);
568 if (1 == sym
->getRetainCount()) {
569 symCache
[numSymbols
++] = sym
;
574 dict1
= OSDictionary::withObjects(
575 (OSObject
**) symCache
, symCache
, numSymbols
, numSymbols
);
576 TEST_ASSERT('D', "2a", dict1
);
578 for (i
= 0; i
< numSymbols
; i
++) {
579 count1
+= (symCache
[i
]->getRetainCount() == 3);
581 TEST_ASSERT('D', "2b", count1
== numSymbols
);
583 TEST_ASSERT('D', "2c", numSymbols
== (int) dict1
->getCount());
584 TEST_ASSERT('D', "2d", numSymbols
== (int) dict1
->getCapacity());
585 TEST_ASSERT('D', "2e",
586 numSymbols
== (int) dict1
->getCapacityIncrement());
588 for (i
= dict1
->getCount(); --i
>= 0;) {
589 str
= (OSString
*) dict1
->getObject(symCache
[i
]);
590 if (str
!= (OSString
*) symCache
[i
]) {
591 verPrintf(("testDictionary(D) test 2f%d failed\n", i
));
598 for (i
= 0; i
< numSymbols
; i
++) {
599 count1
+= (symCache
[i
]->getRetainCount() == 1);
600 symCache
[i
]->release();
602 TEST_ASSERT('D', "2g", count1
== numSymbols
);
603 res
= res
&& checkSpace("(D)2", spaceCheck
, 0);
605 // Check the creation of a sizable Dictionary from an array of IOStrings
606 // Also check searching dictionary use OSString for a key.
607 spaceCheck
= checkPointSpace();
608 for (i
= 0, numSymbols
= 0; i
< numStrCache
; i
++) {
609 sym
= OSSymbol::withCStringNoCopy(strCache
[i
]);
610 if (1 == sym
->getRetainCount()) {
611 cache
[numSymbols
] = OSString::withCStringNoCopy(strCache
[i
]);
612 symCache
[numSymbols
] = sym
;
618 dict1
= OSDictionary::withObjects((OSObject
**) symCache
,
620 numSymbols
, numSymbols
);
621 TEST_ASSERT('D', "3a", dict1
);
623 for (i
= 0; i
< numSymbols
; i
++) {
624 count1
+= (symCache
[i
]->getRetainCount() == 3);
625 count2
+= (cache
[i
]->getRetainCount() == 1);
627 TEST_ASSERT('D', "3b", count1
== numSymbols
);
628 TEST_ASSERT('D', "3c", count2
== numSymbols
);
631 for (i
= 0; i
< numSymbols
; i
++) {
632 str
= (OSString
*) cache
[i
];
633 count1
+= (symCache
[i
] == (const OSSymbol
*) dict1
->getObject(str
));
634 count2
+= (symCache
[i
]->getRetainCount() == 3);
636 TEST_ASSERT('D', "3d", count1
== numSymbols
);
637 TEST_ASSERT('D', "3e", count2
== numSymbols
);
640 for (i
= 0; i
< numSymbols
; i
++) {
641 const char *cStr
= ((OSString
*) cache
[i
])->getCStringNoCopy();
643 count1
+= (symCache
[i
] == (const OSSymbol
*) dict1
->getObject(cStr
));
644 count2
+= (symCache
[i
]->getRetainCount() == 3);
646 TEST_ASSERT('D', "3f", count1
== numSymbols
);
647 TEST_ASSERT('D', "3g", count2
== numSymbols
);
652 for (i
= 0; i
< numSymbols
; i
++) {
653 count1
+= (symCache
[i
]->getRetainCount() == 1);
654 count2
+= (cache
[i
]->getRetainCount() == 1);
655 symCache
[i
]->release();
658 TEST_ASSERT('D', "3h", count1
== numSymbols
);
659 res
= res
&& checkSpace("(D)3", spaceCheck
, 0);
661 // Check the creation of a small dictionary then grow it one item at a time
662 // Create a new dictionary from the old dictionary.
663 // Finally remove each item permanently.
664 spaceCheck
= checkPointSpace();
665 for (i
= 0, numSymbols
= 0; i
< numStrCache
; i
++) {
666 sym
= OSSymbol::withCStringNoCopy(strCache
[i
]);
667 if (1 == sym
->getRetainCount()) {
668 cache
[numSymbols
] = OSString::withCStringNoCopy(strCache
[i
]);
669 symCache
[numSymbols
] = sym
;
676 dict1
= OSDictionary::withCapacity(1);
677 TEST_ASSERT('D', "4a", dict1
);
680 for (i
= 0; i
< numSymbols
; i
++) {
682 count1
+= ((OSObject
*) sym
== dict1
->setObject((OSObject
*) sym
,
683 sym
->getCStringNoCopy()));
684 count2
+= (sym
->getRetainCount() == 3);
686 TEST_ASSERT('D', "4b", numSymbols
== (int) dict1
->getCount());
687 TEST_ASSERT('D', "4c", numSymbols
== count1
);
688 TEST_ASSERT('D', "4d", numSymbols
== count2
);
690 dict2
= OSDictionary::withDictionary(dict1
, numSymbols
- 1);
691 TEST_ASSERT('D', "4b", !dict2
);
692 dict2
= OSDictionary::withDictionary(dict1
, numSymbols
);
694 TEST_ASSERT('D', "4e", dict2
);
696 dict1
->release(); dict1
= 0;
698 TEST_ASSERT('D', "4f", numSymbols
== (int) dict2
->getCount());
701 for (i
= 0; i
< numSymbols
; i
++) {
702 OSObject
*replacedObject
;
705 str
= (OSString
*) cache
[i
];
706 replacedObject
= dict2
->setObject(str
, str
);
707 count1
+= ((OSString
*) sym
== replacedObject
);
708 replacedObject
->release();
709 count2
+= (sym
->getRetainCount() == 2);
712 TEST_ASSERT('D', "4g", numSymbols
== count1
);
713 TEST_ASSERT('D', "4h", numSymbols
== count2
);
716 for (i
= 0; i
< numSymbols
; i
++) {
718 str
= (OSString
*) cache
[i
];
719 count1
+= (str
== dict2
->__takeObject(sym
));
721 count2
+= (sym
->getRetainCount() == 1);
724 TEST_ASSERT('D', "4i", numSymbols
== count1
);
725 TEST_ASSERT('D', "4j", numSymbols
== count2
);
726 TEST_ASSERT('D', "4k", !dict2
->getCount());
727 dict2
->release(); dict2
= 0;
731 res
= res
&& checkSpace("(D)4", spaceCheck
, 0);
734 verPrintf(("testDictionary: All OSDictionary Tests passed\n"));
736 logPrintf(("testDictionary: Some OSDictionary Tests failed\n"));
745 OSObject
*cache
[numStrCache
];
747 const OSSymbol
*symCache
[numStrCache
], *sym
;
750 OSArray
*array
, *bigReturn
;
751 OSCollectionIterator
*iter1
, *iter2
;
752 int i
, numSymbols
, count1
, count2
, count3
;
754 // Setup symbol and string pools
755 for (i
= 0, numSymbols
= 0; i
< numStrCache
; i
++) {
756 sym
= OSSymbol::withCStringNoCopy(strCache
[i
]);
757 if (1 == sym
->getRetainCount()) {
758 cache
[numSymbols
] = OSString::withCStringNoCopy(strCache
[i
]);
759 symCache
[numSymbols
] = sym
;
766 // Test the array iterator
767 spaceCheck
= checkPointSpace();
769 array
= OSArray::withCapacity(numSymbols
);
770 TEST_ASSERT('I', "1a", array
);
773 for (i
= numSymbols
; --i
>= 0;) {
774 count1
+= array
->setObject(cache
[i
], 0);
776 TEST_ASSERT('I', "1b", count1
== numSymbols
);
778 iter1
= OSCollectionIterator::withCollection(array
);
779 iter2
= OSCollectionIterator::withCollection(array
);
781 TEST_ASSERT('I', "1c", iter1
);
782 TEST_ASSERT('I', "1d", iter2
);
783 if (iter1
&& iter2
) {
784 count1
= count2
= count3
= 0;
785 for (i
= 0; (str
= (IOString
*) iter1
->getNextObject()); i
++) {
786 bigReturn
= iter2
->nextEntries();
787 count1
+= (bigReturn
->getCount() == 1);
788 count2
+= (cache
[i
] == bigReturn
->getObject(0));
789 count3
+= (cache
[i
] == str
);
791 TEST_ASSERT('I', "1e", count1
== numSymbols
);
792 TEST_ASSERT('I', "1f", count2
== numSymbols
);
793 TEST_ASSERT('I', "1g", count3
== numSymbols
);
794 TEST_ASSERT('I', "1h", iter1
->valid());
795 TEST_ASSERT('I', "1i", iter2
->valid());
798 str
= (OSString
*) array
->__takeObject(0);
799 array
->setObject(str
, 0);
801 TEST_ASSERT('I', "1j", !iter1
->getNextObject());
802 TEST_ASSERT('I', "1k", !iter1
->valid());
805 count1
= count2
= count3
= 0;
808 str
= (OSString
*) iter1
->getNextObject();
809 } else if ((bigReturn
= iter1
->nextEntries())) {
810 str
= (OSString
*) bigReturn
->getObject(0);
818 count1
+= (cache
[i
] == str
);
820 TEST_ASSERT('I', "1l", count1
== numSymbols
);
821 TEST_ASSERT('I', "1m", i
== numSymbols
);
822 TEST_ASSERT('I', "1n", iter1
->valid());
824 TEST_ASSERT('I', "1o", 3 == array
->getRetainCount());
834 res
= res
&& checkSpace("(I)1", spaceCheck
, 0);
836 // Test the set iterator
837 spaceCheck
= checkPointSpace();
839 set
= OSSet::withCapacity(numSymbols
);
840 TEST_ASSERT('I', "2a", set
);
843 for (i
= 0; i
< numSymbols
; i
++) {
844 count1
+= set
->setObject(cache
[i
]);
846 TEST_ASSERT('I', "2b", count1
== numSymbols
);
848 iter1
= OSCollectionIterator::withCollection(set
);
849 iter2
= OSCollectionIterator::withCollection(set
);
851 TEST_ASSERT('I', "2c", iter1
);
852 TEST_ASSERT('I', "2d", iter2
);
853 if (iter1
&& iter2
) {
854 count1
= count2
= count3
= 0;
855 for (i
= 0; (str
= (IOString
*) iter1
->getNextObject()); i
++) {
856 bigReturn
= iter2
->nextEntries();
857 count1
+= (bigReturn
->getCount() == 1);
858 count2
+= (cache
[i
] == bigReturn
->getObject(0));
859 count3
+= (cache
[i
] == str
);
861 TEST_ASSERT('I', "2e", count1
== numSymbols
);
862 TEST_ASSERT('I', "2f", count2
== numSymbols
);
863 TEST_ASSERT('I', "2g", count3
== numSymbols
);
864 TEST_ASSERT('I', "2h", iter1
->valid());
865 TEST_ASSERT('I', "2i", iter2
->valid());
868 count1
= count2
= count3
= 0;
871 str
= (OSString
*) iter1
->getNextObject();
872 } else if ((bigReturn
= iter1
->nextEntries())) {
873 str
= (OSString
*) bigReturn
->getObject(0);
881 count1
+= (cache
[i
] == str
);
883 TEST_ASSERT('I', "2l", count1
== numSymbols
);
884 TEST_ASSERT('I', "2m", i
== numSymbols
);
885 TEST_ASSERT('I', "2n", iter1
->valid());
888 str
= (OSString
*) set
->getAnyObject();
889 (void) set
->__takeObject(str
);
892 TEST_ASSERT('I', "2j", !iter1
->getNextObject());
893 TEST_ASSERT('I', "2k", !iter1
->valid());
895 TEST_ASSERT('I', "2o", 3 == set
->getRetainCount());
905 res
= res
&& checkSpace("(I)2", spaceCheck
, 0);
907 // Test the dictionary iterator
908 spaceCheck
= checkPointSpace();
910 dict
= OSDictionary::withCapacity(numSymbols
);
911 TEST_ASSERT('I', "3a", dict
);
914 for (i
= 0; i
< numSymbols
; i
++) {
915 count1
+= (0 != dict
->setObject(cache
[i
], symCache
[i
]));
917 TEST_ASSERT('I', "3b", count1
== numSymbols
);
919 iter1
= OSCollectionIterator::withCollection(dict
);
920 iter2
= OSCollectionIterator::withCollection(dict
);
922 TEST_ASSERT('I', "3c", iter1
);
923 TEST_ASSERT('I', "3d", iter2
);
924 if (iter1
&& iter2
) {
925 count1
= count2
= count3
= 0;
926 for (i
= 0; (sym
= (const IOSymbol
*) iter1
->getNextObject()); i
++) {
927 bigReturn
= iter2
->nextEntries();
928 count1
+= (bigReturn
->getCount() == 2);
929 count2
+= (cache
[i
] == bigReturn
->getObject(1));
930 count3
+= (symCache
[i
] == sym
);
932 TEST_ASSERT('I', "3e", count1
== numSymbols
);
933 TEST_ASSERT('I', "3f", count2
== numSymbols
);
934 TEST_ASSERT('I', "3g", count3
== numSymbols
);
935 TEST_ASSERT('I', "3h", iter1
->valid());
936 TEST_ASSERT('I', "3i", iter2
->valid());
939 count1
= count2
= count3
= 0;
943 sym
= (const OSSymbol
*) iter1
->getNextObject();
945 } else if ((bigReturn
= iter1
->nextEntries())) {
946 sym
= (const OSSymbol
*) bigReturn
->getObject(0);
947 str
= (OSString
*) bigReturn
->getObject(1);
956 count1
+= (symCache
[i
] == sym
);
957 count2
+= (!str
|| cache
[i
] == str
);
959 TEST_ASSERT('I', "3l", count1
== numSymbols
);
960 TEST_ASSERT('I', "3m", count2
== numSymbols
);
961 TEST_ASSERT('I', "3n", i
== numSymbols
);
962 TEST_ASSERT('I', "3o", iter1
->valid());
965 str
= (OSString
*) dict
->__takeObject(symCache
[numSymbols
- 1]);
966 dict
->setObject(str
, symCache
[numSymbols
- 1]);
968 TEST_ASSERT('I', "3j", !iter1
->getNextObject());
969 TEST_ASSERT('I', "3k", !iter1
->valid());
971 TEST_ASSERT('I', "3p", 3 == dict
->getRetainCount());
981 res
= res
&& checkSpace("(I)3", spaceCheck
, 0);
983 count1
= count2
= count3
= 0;
984 for (i
= 0; i
< numSymbols
; i
++) {
985 count1
+= (1 == cache
[i
]->getRetainCount());
986 count2
+= (1 == symCache
[i
]->getRetainCount());
988 symCache
[i
]->release();
990 TEST_ASSERT('I', "4a", count1
== numSymbols
);
991 TEST_ASSERT('I', "4b", count2
== numSymbols
);
994 verPrintf(("testIterator: All OSCollectionIterator Tests passed\n"));
996 logPrintf(("testIterator: Some OSCollectionIterator Tests failed\n"));