]>
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>
41 void *spaceCheck
, *spaceCheck2
, *spaceCheck3
;
42 int i
, j
, count
, count2
;
43 OSObject
*cache
[numStrCache
], *str
, *sym
;
44 OSArray
*array1
, *array2
;
46 // Do first test without memory leak tests to initialise the metaclass
47 array1
= OSArray::withCapacity(1);
48 TEST_ASSERT('A', "0a", array1
);
52 // Grow the symbol pool to maximum
53 for (i
= 0; i
< numStrCache
; i
++)
54 cache
[i
] = (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[i
]);
55 for (i
= 0; i
< numStrCache
; i
++)
58 // Create and destroy an array
59 spaceCheck
= checkPointSpace();
60 array1
= OSArray::withCapacity(1);
61 TEST_ASSERT('A', "1a", array1
);
63 TEST_ASSERT('A', "1b", !array1
->getCount());
64 TEST_ASSERT('A', "1c", 1 == array1
->getCapacity());
65 TEST_ASSERT('A', "1d", 1 == array1
->getCapacityIncrement());
66 TEST_ASSERT('A', "1e", 4 == array1
->setCapacityIncrement(4));
67 TEST_ASSERT('A', "1f", 4 == array1
->getCapacityIncrement());
68 TEST_ASSERT('A', "1g", 8 == array1
->ensureCapacity(5));
70 spaceCheck2
= checkPointSpace();
71 cache
[0] = IOString::withCStringNoCopy(strCache
[0]);
73 spaceCheck3
= checkPointSpace();
74 TEST_ASSERT('A', "1h", array1
->setObject(cache
[0]));
75 TEST_ASSERT('A', "1i", cache
[0] == array1
->getObject(0));
77 res
= res
&& checkSpace("(A)1j", spaceCheck3
, 0);
79 TEST_ASSERT('A', "1k", 1 == array1
->getCount());
80 array1
->flushCollection();
81 TEST_ASSERT('A', "1l", !array1
->getCount());
82 res
= res
&& checkSpace("(A)1m", spaceCheck2
, 0);
86 res
= res
&& checkSpace("(A)1", spaceCheck
, 0);
88 // Check the creation of a sizable OSArray from an array of IOObjects
89 // Also check indexing into the array.
90 spaceCheck
= checkPointSpace();
91 for (i
= 0; i
< numStrCache
; i
++)
92 cache
[i
] = OSString::withCStringNoCopy(strCache
[i
]);
93 array1
= OSArray::withObjects(cache
, numStrCache
, numStrCache
);
94 TEST_ASSERT('A', "2a", array1
);
95 for (i
= 0; i
< numStrCache
; i
++)
98 TEST_ASSERT('A', "2b", numStrCache
== (int) array1
->getCount());
99 TEST_ASSERT('A', "2c", numStrCache
== (int) array1
->getCapacity());
100 TEST_ASSERT('A', "2d",
101 numStrCache
== (int) array1
->getCapacityIncrement());
103 for (i
= 0; (str
= array1
->getObject(i
)); i
++) {
104 if (str
!= cache
[i
]) {
105 verPrintf(("testArray(A) test 2e%d failed\n", i
));
109 TEST_ASSERT('A', "2f", numStrCache
== i
);
112 res
= res
&& checkSpace("(A)2", spaceCheck
, 0);
114 // Test array creation from another array by both the setObject method
115 // and the withArray factory. And test __takeObject code first
116 // with tail removal then with head removal
117 spaceCheck
= checkPointSpace();
118 for (i
= 0; i
< numStrCache
; i
++)
119 cache
[i
] = OSString::withCStringNoCopy(strCache
[i
]);
120 array1
= OSArray::withObjects(cache
, numStrCache
, numStrCache
);
121 TEST_ASSERT('A', "3a", array1
);
122 for (i
= 0; i
< numStrCache
; i
++)
126 array2
= OSArray::withCapacity(1);
127 TEST_ASSERT('A', "3b", array2
);
128 TEST_ASSERT('A', "3c", !array2
->getCount());
129 TEST_ASSERT('A', "3d", array2
->setObject(array1
));
130 TEST_ASSERT('A', "3e", array1
->getCount() == array2
->getCount());
134 TEST_ASSERT('A', "3f", numStrCache
== (int) array2
->getCount());
135 for (i
= array2
->getCount(); (str
= array2
->__takeObject(--i
)); ) {
136 if (str
!= cache
[i
]) {
137 verPrintf(("testArray(A) test 3g%d failed\n", i
));
140 count
+= ((int) array2
->getCount() == i
);
143 TEST_ASSERT('A', "3h", count
== numStrCache
);
144 TEST_ASSERT('A', "3i", -1 == i
);
145 TEST_ASSERT('A', "3j", !array2
->getCount());
147 spaceCheck2
= checkPointSpace();
148 array2
->flushCollection();
149 res
= res
&& checkSpace("(A)3k", spaceCheck2
, 0);
155 array2
= OSArray::withArray(array1
, numStrCache
- 1);
156 TEST_ASSERT('A', "3l", !array2
);
157 array2
= OSArray::withArray(array1
, array1
->getCount());
158 TEST_ASSERT('A', "3m", array2
);
163 TEST_ASSERT('A', "3o", numStrCache
== (int) array2
->getCount());
164 for (i
= 0; (str
= array2
->__takeObject(0)); i
++) {
165 count
+= (str
== cache
[i
]);
168 TEST_ASSERT('A', "3p", count
== numStrCache
);
169 TEST_ASSERT('A', "3q", !array2
->getCount());
173 res
= res
&& checkSpace("(A)3", spaceCheck
, 0);
175 // Test object replacement from one array to another
176 spaceCheck
= checkPointSpace();
177 array1
= OSArray::withCapacity(numStrCache
);
178 TEST_ASSERT('A', "4a", array1
);
181 for (i
= 0; i
< numStrCache
; i
++) {
182 str
= OSString::withCStringNoCopy(strCache
[i
]);
183 count
+= array1
->setObject(str
);
184 count2
+= (str
== array1
->lastObject());
187 TEST_ASSERT('A', "4b", numStrCache
== (int) array1
->getCount());
188 TEST_ASSERT('A', "4c", count
== numStrCache
);
189 TEST_ASSERT('A', "4d", count2
== numStrCache
);
191 array2
= OSArray::withCapacity(1);
192 TEST_ASSERT('A', "4e", array2
);
195 str
= (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[0]);
196 for (i
= 0; i
< numStrCache
; i
++) {
197 sym
= (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[i
]);
198 count
+= array2
->setObject(sym
, 0);
199 count2
+= (str
== array2
->lastObject());
203 TEST_ASSERT('A', "4f", numStrCache
== (int) array2
->getCount());
204 TEST_ASSERT('A', "4g", count
== numStrCache
);
205 TEST_ASSERT('A', "4h", count2
== numStrCache
);
207 if (array1
&& array2
) {
210 for (i
= array1
->getCount() - 1; (sym
= array2
->__takeObject(0)); i
--) {
211 str
= array1
->replaceObject(sym
, i
);
213 count2
+= (sym
!= str
);
219 TEST_ASSERT('A', "4k", numStrCache
== (int) array1
->getCount());
220 TEST_ASSERT('A', "4l", count
== numStrCache
);
221 TEST_ASSERT('A', "4m", count2
== numStrCache
);
226 if (array1
) array1
->release();
227 if (array2
) array2
->release();
229 res
= res
&& checkSpace("(A)4", spaceCheck
, 0);
231 // Test array duplicate removal
232 spaceCheck
= checkPointSpace();
233 array1
= OSArray::withCapacity(numStrCache
);
234 TEST_ASSERT('A', "5a", array1
);
236 for (i
= 0; i
< numStrCache
; i
++) {
237 sym
= (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[i
]);
238 count
+= array1
->setObject(sym
);
241 TEST_ASSERT('A', "5b", numStrCache
== (int) array1
->getCount());
244 for (i
= 0; (sym
= array1
->getObject(i
)); )
245 if (sym
->getRetainCount() == 1)
248 //sym = array1->__takeObject(i);
250 array1
->removeObject(i
);
252 TEST_ASSERT('A', "5c", numStrCache
!= (int) array1
->getCount());
254 // check to see that all symbols are really there
255 for (count
= 0, i
= 0; i
< numStrCache
; i
++) {
256 sym
= (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[i
]);
257 for (count2
= false, j
= 0; (str
= array1
->getObject(j
)); j
++)
265 TEST_ASSERT('A', "5c", count
== numStrCache
);
268 res
= res
&& checkSpace("(S)5", spaceCheck
, 0);
271 verPrintf(("testArray: All OSArray Tests passed\n"));
273 logPrintf(("testArray: Some OSArray Tests failed\n"));
279 void *spaceCheck
, *spaceCheck2
, *spaceCheck3
;
280 int i
, count
, count2
;
281 OSObject
*cache
[numStrCache
], *str
, *sym
;
285 // Do first test without memory leak tests to initialise the metaclass
286 set1
= OSSet::withCapacity(1);
287 TEST_ASSERT('S', "0a", set1
);
291 // Grow the symbol pool to maximum
292 for (i
= 0; i
< numStrCache
; i
++)
293 cache
[i
] = (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[i
]);
294 for (i
= 0; i
< numStrCache
; i
++)
297 // Create and destroy an set
298 spaceCheck
= checkPointSpace();
299 set1
= OSSet::withCapacity(1);
300 TEST_ASSERT('S', "1a", set1
);
302 TEST_ASSERT('S', "1b", !set1
->getCount());
303 TEST_ASSERT('S', "1c", 1 == set1
->getCapacity());
304 TEST_ASSERT('S', "1d", 1 == set1
->getCapacityIncrement());
305 TEST_ASSERT('S', "1e", 4 == set1
->setCapacityIncrement(4));
306 TEST_ASSERT('S', "1f", 4 == set1
->getCapacityIncrement());
307 TEST_ASSERT('S', "1g", 8 == set1
->ensureCapacity(5));
309 spaceCheck2
= checkPointSpace();
310 cache
[0] = IOString::withCStringNoCopy(strCache
[0]);
312 spaceCheck3
= checkPointSpace();
313 TEST_ASSERT('S', "1h", set1
->setObject(cache
[0]));
314 TEST_ASSERT('S', "1i", set1
->containsObject(cache
[0]));
315 TEST_ASSERT('S', "1j", cache
[0] == set1
->getAnyObject());
317 res
= res
&& checkSpace("(S)1k", spaceCheck3
, 0);
319 TEST_ASSERT('S', "1l", 1 == set1
->getCount());
320 set1
->flushCollection();
321 TEST_ASSERT('S', "1m", !set1
->getCount());
322 res
= res
&& checkSpace("(S)1n", spaceCheck2
, 0);
326 res
= res
&& checkSpace("(S)1", spaceCheck
, 0);
328 // Check the creation of a sizable OSSet from an set of IOObjects
329 // Also check member test of set.
330 spaceCheck
= checkPointSpace();
331 for (i
= 0; i
< numStrCache
; i
++)
332 cache
[i
] = OSString::withCStringNoCopy(strCache
[i
]);
333 set1
= OSSet::withObjects(cache
, numStrCache
, numStrCache
);
334 TEST_ASSERT('S', "2a", set1
);
335 for (i
= 0; i
< numStrCache
; i
++)
338 TEST_ASSERT('S', "2b", numStrCache
== (int) set1
->getCount());
339 TEST_ASSERT('S', "2c", numStrCache
== (int) set1
->getCapacity());
340 TEST_ASSERT('S', "2d",
341 numStrCache
== (int) set1
->getCapacityIncrement());
344 for (i
= set1
->getCount(); --i
>= 0; )
345 count
+= set1
->member(cache
[i
]);
347 TEST_ASSERT('S', "2e", numStrCache
== count
);
350 res
= res
&& checkSpace("(S)2", spaceCheck
, 0);
352 // Test set creation from another set by both the setObject method
353 // and the withArray factory. And test __takeObject code first
354 // with tail removal then with head removal
355 spaceCheck
= checkPointSpace();
356 for (i
= 0; i
< numStrCache
; i
++)
357 cache
[i
] = OSString::withCStringNoCopy(strCache
[i
]);
358 set1
= OSSet::withObjects(cache
, numStrCache
, numStrCache
);
359 TEST_ASSERT('S', "3a", set1
);
360 for (i
= 0; i
< numStrCache
; i
++)
364 set2
= OSSet::withCapacity(set1
->getCount());
365 TEST_ASSERT('S', "3b", set2
);
366 TEST_ASSERT('S', "3c", !set2
->getCount());
367 TEST_ASSERT('S', "3d", set2
->setObject(set1
));
368 TEST_ASSERT('S', "3e", set1
->getCount() == set2
->getCount());
371 TEST_ASSERT('S', "3f", numStrCache
== (int) set2
->getCount());
373 while ( (str
= set2
->getAnyObject()) ) {
374 count
+= set2
->__takeObject(str
);
375 count2
+= set1
->member(str
);
378 TEST_ASSERT('S', "3g", !set2
->getCount());
379 TEST_ASSERT('S', "3h", numStrCache
== count
);
380 TEST_ASSERT('S', "3i", numStrCache
== count2
);
382 spaceCheck2
= checkPointSpace();
383 set2
->flushCollection();
384 res
= res
&& checkSpace("(S)3j", spaceCheck2
, 0);
390 set2
= OSSet::withSet(set1
, numStrCache
- 1);
391 TEST_ASSERT('S', "3k", !set2
);
392 set2
= OSSet::withSet(set1
, set1
->getCount());
393 TEST_ASSERT('S', "3l", set2
);
397 TEST_ASSERT('S', "3m", numStrCache
== (int) set2
->getCount());
398 i
= count
= count2
= 0;
399 while ( (str
= set2
->getAnyObject()) ) {
400 count
+= set2
->__takeObject(str
);
401 count2
+= (cache
[i
++] == str
);
404 TEST_ASSERT('S', "3n", !set2
->getCount());
405 TEST_ASSERT('S', "3o", numStrCache
== count
);
406 TEST_ASSERT('S', "3p", numStrCache
== count2
);
411 res
= res
&& checkSpace("(S)3", spaceCheck
, 0);
413 // Test duplicate removal
414 spaceCheck
= checkPointSpace();
416 set1
= OSSet::withCapacity(numStrCache
);
417 TEST_ASSERT('S', "4a", set1
);
420 for (i
= 0; i
< numStrCache
; i
++) {
421 sym
= (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[i
]);
422 count
+= set1
->setObject(sym
);
425 TEST_ASSERT('S', "4b", numStrCache
!= (int) set1
->getCount());
426 TEST_ASSERT('S', "4c", count
== (int) set1
->getCount());
429 for (i
= 0; i
< numStrCache
; i
++) {
430 sym
= (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[i
]);
431 count
+= set1
->member(sym
);
432 count2
+= sym
->getRetainCount();
435 TEST_ASSERT('S', "4d", count
== numStrCache
);
436 TEST_ASSERT('S', "4e", count2
== numStrCache
* 2);
438 set2
= OSSet::withSet(set1
, 2 * set1
->getCount());
440 TEST_ASSERT('S', "4f", set2
);
442 set2
->setObject(set1
);
443 TEST_ASSERT('S', "4g", set1
->getCount() == set2
->getCount());
447 res
= res
&& checkSpace("(S)4", spaceCheck
, 0);
449 // Test array duplicate removal
450 spaceCheck
= checkPointSpace();
451 array
= OSArray::withCapacity(numStrCache
);
452 for (i
= 0; i
< numStrCache
; i
++) {
453 sym
= (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[i
]);
454 count
+= array
->setObject(sym
);
457 set1
= OSSet::withArray(array
, numStrCache
);
458 TEST_ASSERT('S', "5a", set1
);
460 TEST_ASSERT('S', "5b", array
->getCount() != set1
->getCount());
463 count
= count2
= set1
->getCount();
464 while ( (sym
= set1
->getAnyObject()) ) {
465 count
-= set1
->__takeObject(sym
);
466 count2
-= sym
->getRetainCount();
469 TEST_ASSERT('S', "5c", !count
);
470 TEST_ASSERT('S', "5d", !count2
);
473 res
= res
&& checkSpace("(S)5", spaceCheck
, 0);
476 verPrintf(("testSet: All OSSet Tests passed\n"));
478 logPrintf(("testSet: Some OSSet Tests failed\n"));
481 void testDictionary()
484 void *spaceCheck
, *spaceCheck2
, *spaceCheck3
;
485 OSObject
*cache
[numStrCache
];
487 const OSSymbol
*symCache
[numStrCache
], *sym
;
488 OSDictionary
*dict1
, *dict2
;
489 int i
, numSymbols
, count1
, count2
;
491 // Do first test without memory leak tests to initialise the metaclass
492 dict1
= OSDictionary::withCapacity(1);
493 TEST_ASSERT('D', "0a", dict1
);
497 // Grow the symbol pool to maximum
498 for (i
= 0; i
< numStrCache
; i
++)
499 symCache
[i
] = OSSymbol::withCStringNoCopy(strCache
[i
]);
500 for (i
= 0; i
< numStrCache
; i
++)
501 symCache
[i
]->release();
503 // Create and destroy a dictionary
504 spaceCheck
= checkPointSpace();
505 dict1
= OSDictionary::withCapacity(1);
506 TEST_ASSERT('D', "1a", dict1
);
508 TEST_ASSERT('D', "1b", !dict1
->getCount());
509 TEST_ASSERT('D', "1c", 1 == dict1
->getCapacity());
510 TEST_ASSERT('D', "1d", 1 == dict1
->getCapacityIncrement());
511 TEST_ASSERT('D', "1e", 4 == dict1
->setCapacityIncrement(4));
512 TEST_ASSERT('D', "1f", 4 == dict1
->getCapacityIncrement());
513 TEST_ASSERT('D', "1g", 8 == dict1
->ensureCapacity(5));
515 spaceCheck2
= checkPointSpace();
516 sym
= OSSymbol::withCStringNoCopy(strCache
[0]);
518 spaceCheck3
= checkPointSpace();
519 TEST_ASSERT('D', "1h", dict1
->setObject((OSObject
*) sym
, sym
));
520 TEST_ASSERT('D', "1i", (OSObject
*) sym
== dict1
->getObject(sym
));
522 TEST_ASSERT('D', "1i", 2 == sym
->getRetainCount());
523 res
= res
&& checkSpace("(D)1j", spaceCheck3
, 0);
525 TEST_ASSERT('D', "1k", 1 == dict1
->getCount());
526 dict1
->flushCollection();
527 TEST_ASSERT('D', "1l", !dict1
->getCount());
528 res
= res
&& checkSpace("(D)1m", spaceCheck2
, 0);
532 res
= res
&& checkSpace("(D)1", spaceCheck
, 0);
534 // Check the creation of a sizable OSDictionary from an array of IOObjects
535 // Also check indexing into the array.
536 spaceCheck
= checkPointSpace();
537 for (i
= 0, numSymbols
= 0; i
< numStrCache
; i
++) {
538 sym
= OSSymbol::withCStringNoCopy(strCache
[i
]);
539 if (1 == sym
->getRetainCount())
540 symCache
[numSymbols
++] = sym
;
544 dict1
= OSDictionary::withObjects(
545 (OSObject
**) symCache
, symCache
, numSymbols
, numSymbols
);
546 TEST_ASSERT('D', "2a", dict1
);
548 for (i
= 0; i
< numSymbols
; i
++)
549 count1
+= (symCache
[i
]->getRetainCount() == 3);
550 TEST_ASSERT('D', "2b", count1
== numSymbols
);
552 TEST_ASSERT('D', "2c", numSymbols
== (int) dict1
->getCount());
553 TEST_ASSERT('D', "2d", numSymbols
== (int) dict1
->getCapacity());
554 TEST_ASSERT('D', "2e",
555 numSymbols
== (int) dict1
->getCapacityIncrement());
557 for (i
= dict1
->getCount(); --i
>= 0; ) {
558 str
= (OSString
*) dict1
->getObject(symCache
[i
]);
559 if (str
!= (OSString
*) symCache
[i
]) {
560 verPrintf(("testDictionary(D) test 2f%d failed\n", i
));
567 for (i
= 0; i
< numSymbols
; i
++) {
568 count1
+= (symCache
[i
]->getRetainCount() == 1);
569 symCache
[i
]->release();
571 TEST_ASSERT('D', "2g", count1
== numSymbols
);
572 res
= res
&& checkSpace("(D)2", spaceCheck
, 0);
574 // Check the creation of a sizable Dictionary from an array of IOStrings
575 // Also check searching dictionary use OSString for a key.
576 spaceCheck
= checkPointSpace();
577 for (i
= 0, numSymbols
= 0; i
< numStrCache
; i
++) {
578 sym
= OSSymbol::withCStringNoCopy(strCache
[i
]);
579 if (1 == sym
->getRetainCount()) {
580 cache
[numSymbols
] = OSString::withCStringNoCopy(strCache
[i
]);
581 symCache
[numSymbols
] = sym
;
587 dict1
= OSDictionary::withObjects((OSObject
**) symCache
,
589 numSymbols
, numSymbols
);
590 TEST_ASSERT('D', "3a", dict1
);
592 for (i
= 0; i
< numSymbols
; i
++) {
593 count1
+= (symCache
[i
]->getRetainCount() == 3);
594 count2
+= (cache
[i
]->getRetainCount() == 1);
596 TEST_ASSERT('D', "3b", count1
== numSymbols
);
597 TEST_ASSERT('D', "3c", count2
== numSymbols
);
600 for (i
= 0; i
< numSymbols
; i
++) {
601 str
= (OSString
*) cache
[i
];
602 count1
+= (symCache
[i
] == (const OSSymbol
*) dict1
->getObject(str
));
603 count2
+= (symCache
[i
]->getRetainCount() == 3);
605 TEST_ASSERT('D', "3d", count1
== numSymbols
);
606 TEST_ASSERT('D', "3e", count2
== numSymbols
);
609 for (i
= 0; i
< numSymbols
; i
++) {
610 const char *cStr
= ((OSString
*) cache
[i
])->getCStringNoCopy();
612 count1
+= (symCache
[i
] == (const OSSymbol
*) dict1
->getObject(cStr
));
613 count2
+= (symCache
[i
]->getRetainCount() == 3);
615 TEST_ASSERT('D', "3f", count1
== numSymbols
);
616 TEST_ASSERT('D', "3g", count2
== numSymbols
);
621 for (i
= 0; i
< numSymbols
; i
++) {
622 count1
+= (symCache
[i
]->getRetainCount() == 1);
623 count2
+= (cache
[i
]->getRetainCount() == 1);
624 symCache
[i
]->release();
627 TEST_ASSERT('D', "3h", count1
== numSymbols
);
628 res
= res
&& checkSpace("(D)3", spaceCheck
, 0);
630 // Check the creation of a small dictionary then grow it one item at a time
631 // Create a new dictionary from the old dictionary.
632 // Finally remove each item permanently.
633 spaceCheck
= checkPointSpace();
634 for (i
= 0, numSymbols
= 0; i
< numStrCache
; i
++) {
635 sym
= OSSymbol::withCStringNoCopy(strCache
[i
]);
636 if (1 == sym
->getRetainCount()) {
637 cache
[numSymbols
] = OSString::withCStringNoCopy(strCache
[i
]);
638 symCache
[numSymbols
] = sym
;
645 dict1
= OSDictionary::withCapacity(1);
646 TEST_ASSERT('D', "4a", dict1
);
649 for (i
= 0; i
< numSymbols
; i
++) {
651 count1
+= ((OSObject
*) sym
== dict1
->setObject((OSObject
*) sym
,
652 sym
->getCStringNoCopy()));
653 count2
+= (sym
->getRetainCount() == 3);
655 TEST_ASSERT('D', "4b", numSymbols
== (int) dict1
->getCount());
656 TEST_ASSERT('D', "4c", numSymbols
== count1
);
657 TEST_ASSERT('D', "4d", numSymbols
== count2
);
659 dict2
= OSDictionary::withDictionary(dict1
, numSymbols
-1);
660 TEST_ASSERT('D', "4b", !dict2
);
661 dict2
= OSDictionary::withDictionary(dict1
, numSymbols
);
663 TEST_ASSERT('D', "4e", dict2
);
665 dict1
->release(); dict1
= 0;
667 TEST_ASSERT('D', "4f", numSymbols
== (int) dict2
->getCount());
670 for (i
= 0; i
< numSymbols
; i
++) {
671 OSObject
*replacedObject
;
674 str
= (OSString
*) cache
[i
];
675 replacedObject
= dict2
->setObject(str
, str
);
676 count1
+= ((OSString
*) sym
== replacedObject
);
677 replacedObject
->release();
678 count2
+= (sym
->getRetainCount() == 2);
681 TEST_ASSERT('D', "4g", numSymbols
== count1
);
682 TEST_ASSERT('D', "4h", numSymbols
== count2
);
685 for (i
= 0; i
< numSymbols
; i
++) {
687 str
= (OSString
*) cache
[i
];
688 count1
+= (str
== dict2
->__takeObject(sym
));
690 count2
+= (sym
->getRetainCount() == 1);
693 TEST_ASSERT('D', "4i", numSymbols
== count1
);
694 TEST_ASSERT('D', "4j", numSymbols
== count2
);
695 TEST_ASSERT('D', "4k", !dict2
->getCount());
696 dict2
->release(); dict2
= 0;
700 res
= res
&& checkSpace("(D)4", spaceCheck
, 0);
703 verPrintf(("testDictionary: All OSDictionary Tests passed\n"));
705 logPrintf(("testDictionary: Some OSDictionary Tests failed\n"));
712 OSObject
*cache
[numStrCache
];
714 const OSSymbol
*symCache
[numStrCache
], *sym
;
717 OSArray
*array
, *bigReturn
;
718 OSCollectionIterator
*iter1
, *iter2
;
719 int i
, numSymbols
, count1
, count2
, count3
;
721 // Setup symbol and string pools
722 for (i
= 0, numSymbols
= 0; i
< numStrCache
; i
++) {
723 sym
= OSSymbol::withCStringNoCopy(strCache
[i
]);
724 if (1 == sym
->getRetainCount()) {
725 cache
[numSymbols
] = OSString::withCStringNoCopy(strCache
[i
]);
726 symCache
[numSymbols
] = sym
;
733 // Test the array iterator
734 spaceCheck
= checkPointSpace();
736 array
= OSArray::withCapacity(numSymbols
);
737 TEST_ASSERT('I', "1a", array
);
740 for (i
= numSymbols
; --i
>= 0; )
741 count1
+= array
->setObject(cache
[i
], 0);
742 TEST_ASSERT('I', "1b", count1
== numSymbols
);
744 iter1
= OSCollectionIterator::withCollection(array
);
745 iter2
= OSCollectionIterator::withCollection(array
);
747 TEST_ASSERT('I', "1c", iter1
);
748 TEST_ASSERT('I', "1d", iter2
);
749 if (iter1
&& iter2
) {
750 count1
= count2
= count3
= 0;
751 for (i
= 0; (str
= (IOString
*) iter1
->getNextObject()); i
++) {
752 bigReturn
= iter2
->nextEntries();
753 count1
+= (bigReturn
->getCount() == 1);
754 count2
+= (cache
[i
] == bigReturn
->getObject(0));
755 count3
+= (cache
[i
] == str
);
757 TEST_ASSERT('I', "1e", count1
== numSymbols
);
758 TEST_ASSERT('I', "1f", count2
== numSymbols
);
759 TEST_ASSERT('I', "1g", count3
== numSymbols
);
760 TEST_ASSERT('I', "1h", iter1
->valid());
761 TEST_ASSERT('I', "1i", iter2
->valid());
764 str
= (OSString
*) array
->__takeObject(0);
765 array
->setObject(str
, 0);
767 TEST_ASSERT('I', "1j", !iter1
->getNextObject());
768 TEST_ASSERT('I', "1k", !iter1
->valid());
771 count1
= count2
= count3
= 0;
774 str
= (OSString
*) iter1
->getNextObject();
775 else if ( (bigReturn
= iter1
->nextEntries()) )
776 str
= (OSString
*) bigReturn
->getObject(0);
782 count1
+= (cache
[i
] == str
);
784 TEST_ASSERT('I', "1l", count1
== numSymbols
);
785 TEST_ASSERT('I', "1m", i
== numSymbols
);
786 TEST_ASSERT('I', "1n", iter1
->valid());
788 TEST_ASSERT('I', "1o", 3 == array
->getRetainCount());
792 if (iter1
) iter1
->release();
793 if (iter2
) iter2
->release();
794 res
= res
&& checkSpace("(I)1", spaceCheck
, 0);
796 // Test the set iterator
797 spaceCheck
= checkPointSpace();
799 set
= OSSet::withCapacity(numSymbols
);
800 TEST_ASSERT('I', "2a", set
);
803 for (i
= 0; i
< numSymbols
; i
++)
804 count1
+= set
->setObject(cache
[i
]);
805 TEST_ASSERT('I', "2b", count1
== numSymbols
);
807 iter1
= OSCollectionIterator::withCollection(set
);
808 iter2
= OSCollectionIterator::withCollection(set
);
810 TEST_ASSERT('I', "2c", iter1
);
811 TEST_ASSERT('I', "2d", iter2
);
812 if (iter1
&& iter2
) {
813 count1
= count2
= count3
= 0;
814 for (i
= 0; (str
= (IOString
*) iter1
->getNextObject()); i
++) {
815 bigReturn
= iter2
->nextEntries();
816 count1
+= (bigReturn
->getCount() == 1);
817 count2
+= (cache
[i
] == bigReturn
->getObject(0));
818 count3
+= (cache
[i
] == str
);
820 TEST_ASSERT('I', "2e", count1
== numSymbols
);
821 TEST_ASSERT('I', "2f", count2
== numSymbols
);
822 TEST_ASSERT('I', "2g", count3
== numSymbols
);
823 TEST_ASSERT('I', "2h", iter1
->valid());
824 TEST_ASSERT('I', "2i", iter2
->valid());
827 count1
= count2
= count3
= 0;
830 str
= (OSString
*) iter1
->getNextObject();
831 else if ( (bigReturn
= iter1
->nextEntries()) )
832 str
= (OSString
*) bigReturn
->getObject(0);
838 count1
+= (cache
[i
] == str
);
840 TEST_ASSERT('I', "2l", count1
== numSymbols
);
841 TEST_ASSERT('I', "2m", i
== numSymbols
);
842 TEST_ASSERT('I', "2n", iter1
->valid());
845 str
= (OSString
*) set
->getAnyObject();
846 (void) set
->__takeObject(str
);
849 TEST_ASSERT('I', "2j", !iter1
->getNextObject());
850 TEST_ASSERT('I', "2k", !iter1
->valid());
852 TEST_ASSERT('I', "2o", 3 == set
->getRetainCount());
856 if (iter1
) iter1
->release();
857 if (iter2
) iter2
->release();
858 res
= res
&& checkSpace("(I)2", spaceCheck
, 0);
860 // Test the dictionary iterator
861 spaceCheck
= checkPointSpace();
863 dict
= OSDictionary::withCapacity(numSymbols
);
864 TEST_ASSERT('I', "3a", dict
);
867 for (i
= 0; i
< numSymbols
; i
++)
868 count1
+= (0 != dict
->setObject(cache
[i
], symCache
[i
]));
869 TEST_ASSERT('I', "3b", count1
== numSymbols
);
871 iter1
= OSCollectionIterator::withCollection(dict
);
872 iter2
= OSCollectionIterator::withCollection(dict
);
874 TEST_ASSERT('I', "3c", iter1
);
875 TEST_ASSERT('I', "3d", iter2
);
876 if (iter1
&& iter2
) {
877 count1
= count2
= count3
= 0;
878 for (i
= 0; (sym
= (const IOSymbol
*) iter1
->getNextObject()); i
++) {
879 bigReturn
= iter2
->nextEntries();
880 count1
+= (bigReturn
->getCount() == 2);
881 count2
+= (cache
[i
] == bigReturn
->getObject(1));
882 count3
+= (symCache
[i
] == sym
);
884 TEST_ASSERT('I', "3e", count1
== numSymbols
);
885 TEST_ASSERT('I', "3f", count2
== numSymbols
);
886 TEST_ASSERT('I', "3g", count3
== numSymbols
);
887 TEST_ASSERT('I', "3h", iter1
->valid());
888 TEST_ASSERT('I', "3i", iter2
->valid());
891 count1
= count2
= count3
= 0;
895 sym
= (const OSSymbol
*) iter1
->getNextObject();
898 else if ( (bigReturn
= iter1
->nextEntries()) ) {
899 sym
= (const OSSymbol
*) bigReturn
->getObject(0);
900 str
= (OSString
*) bigReturn
->getObject(1);
908 count1
+= (symCache
[i
] == sym
);
909 count2
+= (!str
|| cache
[i
] == str
);
911 TEST_ASSERT('I', "3l", count1
== numSymbols
);
912 TEST_ASSERT('I', "3m", count2
== numSymbols
);
913 TEST_ASSERT('I', "3n", i
== numSymbols
);
914 TEST_ASSERT('I', "3o", iter1
->valid());
917 str
= (OSString
*) dict
->__takeObject(symCache
[numSymbols
-1]);
918 dict
->setObject(str
, symCache
[numSymbols
-1]);
920 TEST_ASSERT('I', "3j", !iter1
->getNextObject());
921 TEST_ASSERT('I', "3k", !iter1
->valid());
923 TEST_ASSERT('I', "3p", 3 == dict
->getRetainCount());
927 if (iter1
) iter1
->release();
928 if (iter2
) iter2
->release();
929 res
= res
&& checkSpace("(I)3", spaceCheck
, 0);
931 count1
= count2
= count3
= 0;
932 for (i
= 0; i
< numSymbols
; i
++) {
933 count1
+= (1 == cache
[i
]->getRetainCount());
934 count2
+= (1 == symCache
[i
]->getRetainCount());
936 symCache
[i
]->release();
938 TEST_ASSERT('I', "4a", count1
== numSymbols
);
939 TEST_ASSERT('I', "4b", count2
== numSymbols
);
942 verPrintf(("testIterator: All OSCollectionIterator Tests passed\n"));
944 logPrintf(("testIterator: Some OSCollectionIterator Tests failed\n"));