]>
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_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
33 #include <libkern/c++/OSArray.h>
34 #include <libkern/c++/OSSet.h>
35 #include <libkern/c++/OSDictionary.h>
36 #include <libkern/c++/OSString.h>
37 #include <libkern/c++/OSSymbol.h>
38 #include <libkern/c++/OSCollectionIterator.h>
43 void *spaceCheck
, *spaceCheck2
, *spaceCheck3
;
44 int i
, j
, count
, count2
;
45 OSObject
*cache
[numStrCache
], *str
, *sym
;
46 OSArray
*array1
, *array2
;
48 // Do first test without memory leak tests to initialise the metaclass
49 array1
= OSArray::withCapacity(1);
50 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
]);
57 for (i
= 0; i
< numStrCache
; i
++)
60 // Create and destroy an array
61 spaceCheck
= checkPointSpace();
62 array1
= OSArray::withCapacity(1);
63 TEST_ASSERT('A', "1a", array1
);
65 TEST_ASSERT('A', "1b", !array1
->getCount());
66 TEST_ASSERT('A', "1c", 1 == array1
->getCapacity());
67 TEST_ASSERT('A', "1d", 1 == array1
->getCapacityIncrement());
68 TEST_ASSERT('A', "1e", 4 == array1
->setCapacityIncrement(4));
69 TEST_ASSERT('A', "1f", 4 == array1
->getCapacityIncrement());
70 TEST_ASSERT('A', "1g", 8 == array1
->ensureCapacity(5));
72 spaceCheck2
= checkPointSpace();
73 cache
[0] = IOString::withCStringNoCopy(strCache
[0]);
75 spaceCheck3
= checkPointSpace();
76 TEST_ASSERT('A', "1h", array1
->setObject(cache
[0]));
77 TEST_ASSERT('A', "1i", cache
[0] == array1
->getObject(0));
79 res
= res
&& checkSpace("(A)1j", spaceCheck3
, 0);
81 TEST_ASSERT('A', "1k", 1 == array1
->getCount());
82 array1
->flushCollection();
83 TEST_ASSERT('A', "1l", !array1
->getCount());
84 res
= res
&& checkSpace("(A)1m", spaceCheck2
, 0);
88 res
= res
&& checkSpace("(A)1", spaceCheck
, 0);
90 // Check the creation of a sizable OSArray from an array of IOObjects
91 // Also check indexing into the array.
92 spaceCheck
= checkPointSpace();
93 for (i
= 0; i
< numStrCache
; i
++)
94 cache
[i
] = OSString::withCStringNoCopy(strCache
[i
]);
95 array1
= OSArray::withObjects(cache
, numStrCache
, numStrCache
);
96 TEST_ASSERT('A', "2a", array1
);
97 for (i
= 0; i
< numStrCache
; i
++)
100 TEST_ASSERT('A', "2b", numStrCache
== (int) array1
->getCount());
101 TEST_ASSERT('A', "2c", numStrCache
== (int) array1
->getCapacity());
102 TEST_ASSERT('A', "2d",
103 numStrCache
== (int) array1
->getCapacityIncrement());
105 for (i
= 0; (str
= array1
->getObject(i
)); i
++) {
106 if (str
!= cache
[i
]) {
107 verPrintf(("testArray(A) test 2e%d failed\n", i
));
111 TEST_ASSERT('A', "2f", numStrCache
== i
);
114 res
= res
&& checkSpace("(A)2", spaceCheck
, 0);
116 // Test array creation from another array by both the setObject method
117 // and the withArray factory. And test __takeObject code first
118 // with tail removal then with head removal
119 spaceCheck
= checkPointSpace();
120 for (i
= 0; i
< numStrCache
; i
++)
121 cache
[i
] = OSString::withCStringNoCopy(strCache
[i
]);
122 array1
= OSArray::withObjects(cache
, numStrCache
, numStrCache
);
123 TEST_ASSERT('A', "3a", array1
);
124 for (i
= 0; i
< numStrCache
; i
++)
128 array2
= OSArray::withCapacity(1);
129 TEST_ASSERT('A', "3b", array2
);
130 TEST_ASSERT('A', "3c", !array2
->getCount());
131 TEST_ASSERT('A', "3d", array2
->setObject(array1
));
132 TEST_ASSERT('A', "3e", array1
->getCount() == array2
->getCount());
136 TEST_ASSERT('A', "3f", numStrCache
== (int) array2
->getCount());
137 for (i
= array2
->getCount(); (str
= array2
->__takeObject(--i
)); ) {
138 if (str
!= cache
[i
]) {
139 verPrintf(("testArray(A) test 3g%d failed\n", i
));
142 count
+= ((int) array2
->getCount() == i
);
145 TEST_ASSERT('A', "3h", count
== numStrCache
);
146 TEST_ASSERT('A', "3i", -1 == i
);
147 TEST_ASSERT('A', "3j", !array2
->getCount());
149 spaceCheck2
= checkPointSpace();
150 array2
->flushCollection();
151 res
= res
&& checkSpace("(A)3k", spaceCheck2
, 0);
157 array2
= OSArray::withArray(array1
, numStrCache
- 1);
158 TEST_ASSERT('A', "3l", !array2
);
159 array2
= OSArray::withArray(array1
, array1
->getCount());
160 TEST_ASSERT('A', "3m", array2
);
165 TEST_ASSERT('A', "3o", numStrCache
== (int) array2
->getCount());
166 for (i
= 0; (str
= array2
->__takeObject(0)); i
++) {
167 count
+= (str
== cache
[i
]);
170 TEST_ASSERT('A', "3p", count
== numStrCache
);
171 TEST_ASSERT('A', "3q", !array2
->getCount());
175 res
= res
&& checkSpace("(A)3", spaceCheck
, 0);
177 // Test object replacement from one array to another
178 spaceCheck
= checkPointSpace();
179 array1
= OSArray::withCapacity(numStrCache
);
180 TEST_ASSERT('A', "4a", array1
);
183 for (i
= 0; i
< numStrCache
; i
++) {
184 str
= OSString::withCStringNoCopy(strCache
[i
]);
185 count
+= array1
->setObject(str
);
186 count2
+= (str
== array1
->lastObject());
189 TEST_ASSERT('A', "4b", numStrCache
== (int) array1
->getCount());
190 TEST_ASSERT('A', "4c", count
== numStrCache
);
191 TEST_ASSERT('A', "4d", count2
== numStrCache
);
193 array2
= OSArray::withCapacity(1);
194 TEST_ASSERT('A', "4e", array2
);
197 str
= (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[0]);
198 for (i
= 0; i
< numStrCache
; i
++) {
199 sym
= (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[i
]);
200 count
+= array2
->setObject(sym
, 0);
201 count2
+= (str
== array2
->lastObject());
205 TEST_ASSERT('A', "4f", numStrCache
== (int) array2
->getCount());
206 TEST_ASSERT('A', "4g", count
== numStrCache
);
207 TEST_ASSERT('A', "4h", count2
== numStrCache
);
209 if (array1
&& array2
) {
212 for (i
= array1
->getCount() - 1; (sym
= array2
->__takeObject(0)); i
--) {
213 str
= array1
->replaceObject(sym
, i
);
215 count2
+= (sym
!= str
);
221 TEST_ASSERT('A', "4k", numStrCache
== (int) array1
->getCount());
222 TEST_ASSERT('A', "4l", count
== numStrCache
);
223 TEST_ASSERT('A', "4m", count2
== numStrCache
);
228 if (array1
) array1
->release();
229 if (array2
) array2
->release();
231 res
= res
&& checkSpace("(A)4", spaceCheck
, 0);
233 // Test array duplicate removal
234 spaceCheck
= checkPointSpace();
235 array1
= OSArray::withCapacity(numStrCache
);
236 TEST_ASSERT('A', "5a", array1
);
238 for (i
= 0; i
< numStrCache
; i
++) {
239 sym
= (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[i
]);
240 count
+= array1
->setObject(sym
);
243 TEST_ASSERT('A', "5b", numStrCache
== (int) array1
->getCount());
246 for (i
= 0; (sym
= array1
->getObject(i
)); )
247 if (sym
->getRetainCount() == 1)
250 //sym = array1->__takeObject(i);
252 array1
->removeObject(i
);
254 TEST_ASSERT('A', "5c", numStrCache
!= (int) array1
->getCount());
256 // check to see that all symbols are really there
257 for (count
= 0, i
= 0; i
< numStrCache
; i
++) {
258 sym
= (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[i
]);
259 for (count2
= false, j
= 0; (str
= array1
->getObject(j
)); j
++)
267 TEST_ASSERT('A', "5c", count
== numStrCache
);
270 res
= res
&& checkSpace("(S)5", spaceCheck
, 0);
273 verPrintf(("testArray: All OSArray Tests passed\n"));
275 logPrintf(("testArray: Some OSArray Tests failed\n"));
281 void *spaceCheck
, *spaceCheck2
, *spaceCheck3
;
282 int i
, count
, count2
;
283 OSObject
*cache
[numStrCache
], *str
, *sym
;
287 // Do first test without memory leak tests to initialise the metaclass
288 set1
= OSSet::withCapacity(1);
289 TEST_ASSERT('S', "0a", set1
);
293 // Grow the symbol pool to maximum
294 for (i
= 0; i
< numStrCache
; i
++)
295 cache
[i
] = (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[i
]);
296 for (i
= 0; i
< numStrCache
; i
++)
299 // Create and destroy an set
300 spaceCheck
= checkPointSpace();
301 set1
= OSSet::withCapacity(1);
302 TEST_ASSERT('S', "1a", set1
);
304 TEST_ASSERT('S', "1b", !set1
->getCount());
305 TEST_ASSERT('S', "1c", 1 == set1
->getCapacity());
306 TEST_ASSERT('S', "1d", 1 == set1
->getCapacityIncrement());
307 TEST_ASSERT('S', "1e", 4 == set1
->setCapacityIncrement(4));
308 TEST_ASSERT('S', "1f", 4 == set1
->getCapacityIncrement());
309 TEST_ASSERT('S', "1g", 8 == set1
->ensureCapacity(5));
311 spaceCheck2
= checkPointSpace();
312 cache
[0] = IOString::withCStringNoCopy(strCache
[0]);
314 spaceCheck3
= checkPointSpace();
315 TEST_ASSERT('S', "1h", set1
->setObject(cache
[0]));
316 TEST_ASSERT('S', "1i", set1
->containsObject(cache
[0]));
317 TEST_ASSERT('S', "1j", cache
[0] == set1
->getAnyObject());
319 res
= res
&& checkSpace("(S)1k", spaceCheck3
, 0);
321 TEST_ASSERT('S', "1l", 1 == set1
->getCount());
322 set1
->flushCollection();
323 TEST_ASSERT('S', "1m", !set1
->getCount());
324 res
= res
&& checkSpace("(S)1n", spaceCheck2
, 0);
328 res
= res
&& checkSpace("(S)1", spaceCheck
, 0);
330 // Check the creation of a sizable OSSet from an set of IOObjects
331 // Also check member test of set.
332 spaceCheck
= checkPointSpace();
333 for (i
= 0; i
< numStrCache
; i
++)
334 cache
[i
] = OSString::withCStringNoCopy(strCache
[i
]);
335 set1
= OSSet::withObjects(cache
, numStrCache
, numStrCache
);
336 TEST_ASSERT('S', "2a", set1
);
337 for (i
= 0; i
< numStrCache
; i
++)
340 TEST_ASSERT('S', "2b", numStrCache
== (int) set1
->getCount());
341 TEST_ASSERT('S', "2c", numStrCache
== (int) set1
->getCapacity());
342 TEST_ASSERT('S', "2d",
343 numStrCache
== (int) set1
->getCapacityIncrement());
346 for (i
= set1
->getCount(); --i
>= 0; )
347 count
+= set1
->member(cache
[i
]);
349 TEST_ASSERT('S', "2e", numStrCache
== count
);
352 res
= res
&& checkSpace("(S)2", spaceCheck
, 0);
354 // Test set creation from another set by both the setObject method
355 // and the withArray factory. And test __takeObject code first
356 // with tail removal then with head removal
357 spaceCheck
= checkPointSpace();
358 for (i
= 0; i
< numStrCache
; i
++)
359 cache
[i
] = OSString::withCStringNoCopy(strCache
[i
]);
360 set1
= OSSet::withObjects(cache
, numStrCache
, numStrCache
);
361 TEST_ASSERT('S', "3a", set1
);
362 for (i
= 0; i
< numStrCache
; i
++)
366 set2
= OSSet::withCapacity(set1
->getCount());
367 TEST_ASSERT('S', "3b", set2
);
368 TEST_ASSERT('S', "3c", !set2
->getCount());
369 TEST_ASSERT('S', "3d", set2
->setObject(set1
));
370 TEST_ASSERT('S', "3e", set1
->getCount() == set2
->getCount());
373 TEST_ASSERT('S', "3f", numStrCache
== (int) set2
->getCount());
375 while ( (str
= set2
->getAnyObject()) ) {
376 count
+= set2
->__takeObject(str
);
377 count2
+= set1
->member(str
);
380 TEST_ASSERT('S', "3g", !set2
->getCount());
381 TEST_ASSERT('S', "3h", numStrCache
== count
);
382 TEST_ASSERT('S', "3i", numStrCache
== count2
);
384 spaceCheck2
= checkPointSpace();
385 set2
->flushCollection();
386 res
= res
&& checkSpace("(S)3j", spaceCheck2
, 0);
392 set2
= OSSet::withSet(set1
, numStrCache
- 1);
393 TEST_ASSERT('S', "3k", !set2
);
394 set2
= OSSet::withSet(set1
, set1
->getCount());
395 TEST_ASSERT('S', "3l", set2
);
399 TEST_ASSERT('S', "3m", numStrCache
== (int) set2
->getCount());
400 i
= count
= count2
= 0;
401 while ( (str
= set2
->getAnyObject()) ) {
402 count
+= set2
->__takeObject(str
);
403 count2
+= (cache
[i
++] == str
);
406 TEST_ASSERT('S', "3n", !set2
->getCount());
407 TEST_ASSERT('S', "3o", numStrCache
== count
);
408 TEST_ASSERT('S', "3p", numStrCache
== count2
);
413 res
= res
&& checkSpace("(S)3", spaceCheck
, 0);
415 // Test duplicate removal
416 spaceCheck
= checkPointSpace();
418 set1
= OSSet::withCapacity(numStrCache
);
419 TEST_ASSERT('S', "4a", set1
);
422 for (i
= 0; i
< numStrCache
; i
++) {
423 sym
= (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[i
]);
424 count
+= set1
->setObject(sym
);
427 TEST_ASSERT('S', "4b", numStrCache
!= (int) set1
->getCount());
428 TEST_ASSERT('S', "4c", count
== (int) set1
->getCount());
431 for (i
= 0; i
< numStrCache
; i
++) {
432 sym
= (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[i
]);
433 count
+= set1
->member(sym
);
434 count2
+= sym
->getRetainCount();
437 TEST_ASSERT('S', "4d", count
== numStrCache
);
438 TEST_ASSERT('S', "4e", count2
== numStrCache
* 2);
440 set2
= OSSet::withSet(set1
, 2 * set1
->getCount());
442 TEST_ASSERT('S', "4f", set2
);
444 set2
->setObject(set1
);
445 TEST_ASSERT('S', "4g", set1
->getCount() == set2
->getCount());
449 res
= res
&& checkSpace("(S)4", spaceCheck
, 0);
451 // Test array duplicate removal
452 spaceCheck
= checkPointSpace();
453 array
= OSArray::withCapacity(numStrCache
);
454 for (i
= 0; i
< numStrCache
; i
++) {
455 sym
= (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[i
]);
456 count
+= array
->setObject(sym
);
459 set1
= OSSet::withArray(array
, numStrCache
);
460 TEST_ASSERT('S', "5a", set1
);
462 TEST_ASSERT('S', "5b", array
->getCount() != set1
->getCount());
465 count
= count2
= set1
->getCount();
466 while ( (sym
= set1
->getAnyObject()) ) {
467 count
-= set1
->__takeObject(sym
);
468 count2
-= sym
->getRetainCount();
471 TEST_ASSERT('S', "5c", !count
);
472 TEST_ASSERT('S', "5d", !count2
);
475 res
= res
&& checkSpace("(S)5", spaceCheck
, 0);
478 verPrintf(("testSet: All OSSet Tests passed\n"));
480 logPrintf(("testSet: Some OSSet Tests failed\n"));
483 void testDictionary()
486 void *spaceCheck
, *spaceCheck2
, *spaceCheck3
;
487 OSObject
*cache
[numStrCache
];
489 const OSSymbol
*symCache
[numStrCache
], *sym
;
490 OSDictionary
*dict1
, *dict2
;
491 int i
, numSymbols
, count1
, count2
;
493 // Do first test without memory leak tests to initialise the metaclass
494 dict1
= OSDictionary::withCapacity(1);
495 TEST_ASSERT('D', "0a", dict1
);
499 // Grow the symbol pool to maximum
500 for (i
= 0; i
< numStrCache
; i
++)
501 symCache
[i
] = OSSymbol::withCStringNoCopy(strCache
[i
]);
502 for (i
= 0; i
< numStrCache
; i
++)
503 symCache
[i
]->release();
505 // Create and destroy a dictionary
506 spaceCheck
= checkPointSpace();
507 dict1
= OSDictionary::withCapacity(1);
508 TEST_ASSERT('D', "1a", dict1
);
510 TEST_ASSERT('D', "1b", !dict1
->getCount());
511 TEST_ASSERT('D', "1c", 1 == dict1
->getCapacity());
512 TEST_ASSERT('D', "1d", 1 == dict1
->getCapacityIncrement());
513 TEST_ASSERT('D', "1e", 4 == dict1
->setCapacityIncrement(4));
514 TEST_ASSERT('D', "1f", 4 == dict1
->getCapacityIncrement());
515 TEST_ASSERT('D', "1g", 8 == dict1
->ensureCapacity(5));
517 spaceCheck2
= checkPointSpace();
518 sym
= OSSymbol::withCStringNoCopy(strCache
[0]);
520 spaceCheck3
= checkPointSpace();
521 TEST_ASSERT('D', "1h", dict1
->setObject((OSObject
*) sym
, sym
));
522 TEST_ASSERT('D', "1i", (OSObject
*) sym
== dict1
->getObject(sym
));
524 TEST_ASSERT('D', "1i", 2 == sym
->getRetainCount());
525 res
= res
&& checkSpace("(D)1j", spaceCheck3
, 0);
527 TEST_ASSERT('D', "1k", 1 == dict1
->getCount());
528 dict1
->flushCollection();
529 TEST_ASSERT('D', "1l", !dict1
->getCount());
530 res
= res
&& checkSpace("(D)1m", spaceCheck2
, 0);
534 res
= res
&& checkSpace("(D)1", spaceCheck
, 0);
536 // Check the creation of a sizable OSDictionary from an array of IOObjects
537 // Also check indexing into the array.
538 spaceCheck
= checkPointSpace();
539 for (i
= 0, numSymbols
= 0; i
< numStrCache
; i
++) {
540 sym
= OSSymbol::withCStringNoCopy(strCache
[i
]);
541 if (1 == sym
->getRetainCount())
542 symCache
[numSymbols
++] = sym
;
546 dict1
= OSDictionary::withObjects(
547 (OSObject
**) symCache
, symCache
, numSymbols
, numSymbols
);
548 TEST_ASSERT('D', "2a", dict1
);
550 for (i
= 0; i
< numSymbols
; i
++)
551 count1
+= (symCache
[i
]->getRetainCount() == 3);
552 TEST_ASSERT('D', "2b", count1
== numSymbols
);
554 TEST_ASSERT('D', "2c", numSymbols
== (int) dict1
->getCount());
555 TEST_ASSERT('D', "2d", numSymbols
== (int) dict1
->getCapacity());
556 TEST_ASSERT('D', "2e",
557 numSymbols
== (int) dict1
->getCapacityIncrement());
559 for (i
= dict1
->getCount(); --i
>= 0; ) {
560 str
= (OSString
*) dict1
->getObject(symCache
[i
]);
561 if (str
!= (OSString
*) symCache
[i
]) {
562 verPrintf(("testDictionary(D) test 2f%d failed\n", i
));
569 for (i
= 0; i
< numSymbols
; i
++) {
570 count1
+= (symCache
[i
]->getRetainCount() == 1);
571 symCache
[i
]->release();
573 TEST_ASSERT('D', "2g", count1
== numSymbols
);
574 res
= res
&& checkSpace("(D)2", spaceCheck
, 0);
576 // Check the creation of a sizable Dictionary from an array of IOStrings
577 // Also check searching dictionary use OSString for a key.
578 spaceCheck
= checkPointSpace();
579 for (i
= 0, numSymbols
= 0; i
< numStrCache
; i
++) {
580 sym
= OSSymbol::withCStringNoCopy(strCache
[i
]);
581 if (1 == sym
->getRetainCount()) {
582 cache
[numSymbols
] = OSString::withCStringNoCopy(strCache
[i
]);
583 symCache
[numSymbols
] = sym
;
589 dict1
= OSDictionary::withObjects((OSObject
**) symCache
,
591 numSymbols
, numSymbols
);
592 TEST_ASSERT('D', "3a", dict1
);
594 for (i
= 0; i
< numSymbols
; i
++) {
595 count1
+= (symCache
[i
]->getRetainCount() == 3);
596 count2
+= (cache
[i
]->getRetainCount() == 1);
598 TEST_ASSERT('D', "3b", count1
== numSymbols
);
599 TEST_ASSERT('D', "3c", count2
== numSymbols
);
602 for (i
= 0; i
< numSymbols
; i
++) {
603 str
= (OSString
*) cache
[i
];
604 count1
+= (symCache
[i
] == (const OSSymbol
*) dict1
->getObject(str
));
605 count2
+= (symCache
[i
]->getRetainCount() == 3);
607 TEST_ASSERT('D', "3d", count1
== numSymbols
);
608 TEST_ASSERT('D', "3e", count2
== numSymbols
);
611 for (i
= 0; i
< numSymbols
; i
++) {
612 const char *cStr
= ((OSString
*) cache
[i
])->getCStringNoCopy();
614 count1
+= (symCache
[i
] == (const OSSymbol
*) dict1
->getObject(cStr
));
615 count2
+= (symCache
[i
]->getRetainCount() == 3);
617 TEST_ASSERT('D', "3f", count1
== numSymbols
);
618 TEST_ASSERT('D', "3g", count2
== numSymbols
);
623 for (i
= 0; i
< numSymbols
; i
++) {
624 count1
+= (symCache
[i
]->getRetainCount() == 1);
625 count2
+= (cache
[i
]->getRetainCount() == 1);
626 symCache
[i
]->release();
629 TEST_ASSERT('D', "3h", count1
== numSymbols
);
630 res
= res
&& checkSpace("(D)3", spaceCheck
, 0);
632 // Check the creation of a small dictionary then grow it one item at a time
633 // Create a new dictionary from the old dictionary.
634 // Finally remove each item permanently.
635 spaceCheck
= checkPointSpace();
636 for (i
= 0, numSymbols
= 0; i
< numStrCache
; i
++) {
637 sym
= OSSymbol::withCStringNoCopy(strCache
[i
]);
638 if (1 == sym
->getRetainCount()) {
639 cache
[numSymbols
] = OSString::withCStringNoCopy(strCache
[i
]);
640 symCache
[numSymbols
] = sym
;
647 dict1
= OSDictionary::withCapacity(1);
648 TEST_ASSERT('D', "4a", dict1
);
651 for (i
= 0; i
< numSymbols
; i
++) {
653 count1
+= ((OSObject
*) sym
== dict1
->setObject((OSObject
*) sym
,
654 sym
->getCStringNoCopy()));
655 count2
+= (sym
->getRetainCount() == 3);
657 TEST_ASSERT('D', "4b", numSymbols
== (int) dict1
->getCount());
658 TEST_ASSERT('D', "4c", numSymbols
== count1
);
659 TEST_ASSERT('D', "4d", numSymbols
== count2
);
661 dict2
= OSDictionary::withDictionary(dict1
, numSymbols
-1);
662 TEST_ASSERT('D', "4b", !dict2
);
663 dict2
= OSDictionary::withDictionary(dict1
, numSymbols
);
665 TEST_ASSERT('D', "4e", dict2
);
667 dict1
->release(); dict1
= 0;
669 TEST_ASSERT('D', "4f", numSymbols
== (int) dict2
->getCount());
672 for (i
= 0; i
< numSymbols
; i
++) {
673 OSObject
*replacedObject
;
676 str
= (OSString
*) cache
[i
];
677 replacedObject
= dict2
->setObject(str
, str
);
678 count1
+= ((OSString
*) sym
== replacedObject
);
679 replacedObject
->release();
680 count2
+= (sym
->getRetainCount() == 2);
683 TEST_ASSERT('D', "4g", numSymbols
== count1
);
684 TEST_ASSERT('D', "4h", numSymbols
== count2
);
687 for (i
= 0; i
< numSymbols
; i
++) {
689 str
= (OSString
*) cache
[i
];
690 count1
+= (str
== dict2
->__takeObject(sym
));
692 count2
+= (sym
->getRetainCount() == 1);
695 TEST_ASSERT('D', "4i", numSymbols
== count1
);
696 TEST_ASSERT('D', "4j", numSymbols
== count2
);
697 TEST_ASSERT('D', "4k", !dict2
->getCount());
698 dict2
->release(); dict2
= 0;
702 res
= res
&& checkSpace("(D)4", spaceCheck
, 0);
705 verPrintf(("testDictionary: All OSDictionary Tests passed\n"));
707 logPrintf(("testDictionary: Some OSDictionary Tests failed\n"));
714 OSObject
*cache
[numStrCache
];
716 const OSSymbol
*symCache
[numStrCache
], *sym
;
719 OSArray
*array
, *bigReturn
;
720 OSCollectionIterator
*iter1
, *iter2
;
721 int i
, numSymbols
, count1
, count2
, count3
;
723 // Setup symbol and string pools
724 for (i
= 0, numSymbols
= 0; i
< numStrCache
; i
++) {
725 sym
= OSSymbol::withCStringNoCopy(strCache
[i
]);
726 if (1 == sym
->getRetainCount()) {
727 cache
[numSymbols
] = OSString::withCStringNoCopy(strCache
[i
]);
728 symCache
[numSymbols
] = sym
;
735 // Test the array iterator
736 spaceCheck
= checkPointSpace();
738 array
= OSArray::withCapacity(numSymbols
);
739 TEST_ASSERT('I', "1a", array
);
742 for (i
= numSymbols
; --i
>= 0; )
743 count1
+= array
->setObject(cache
[i
], 0);
744 TEST_ASSERT('I', "1b", count1
== numSymbols
);
746 iter1
= OSCollectionIterator::withCollection(array
);
747 iter2
= OSCollectionIterator::withCollection(array
);
749 TEST_ASSERT('I', "1c", iter1
);
750 TEST_ASSERT('I', "1d", iter2
);
751 if (iter1
&& iter2
) {
752 count1
= count2
= count3
= 0;
753 for (i
= 0; (str
= (IOString
*) iter1
->getNextObject()); i
++) {
754 bigReturn
= iter2
->nextEntries();
755 count1
+= (bigReturn
->getCount() == 1);
756 count2
+= (cache
[i
] == bigReturn
->getObject(0));
757 count3
+= (cache
[i
] == str
);
759 TEST_ASSERT('I', "1e", count1
== numSymbols
);
760 TEST_ASSERT('I', "1f", count2
== numSymbols
);
761 TEST_ASSERT('I', "1g", count3
== numSymbols
);
762 TEST_ASSERT('I', "1h", iter1
->valid());
763 TEST_ASSERT('I', "1i", iter2
->valid());
766 str
= (OSString
*) array
->__takeObject(0);
767 array
->setObject(str
, 0);
769 TEST_ASSERT('I', "1j", !iter1
->getNextObject());
770 TEST_ASSERT('I', "1k", !iter1
->valid());
773 count1
= count2
= count3
= 0;
776 str
= (OSString
*) iter1
->getNextObject();
777 else if ( (bigReturn
= iter1
->nextEntries()) )
778 str
= (OSString
*) bigReturn
->getObject(0);
784 count1
+= (cache
[i
] == str
);
786 TEST_ASSERT('I', "1l", count1
== numSymbols
);
787 TEST_ASSERT('I', "1m", i
== numSymbols
);
788 TEST_ASSERT('I', "1n", iter1
->valid());
790 TEST_ASSERT('I', "1o", 3 == array
->getRetainCount());
794 if (iter1
) iter1
->release();
795 if (iter2
) iter2
->release();
796 res
= res
&& checkSpace("(I)1", spaceCheck
, 0);
798 // Test the set iterator
799 spaceCheck
= checkPointSpace();
801 set
= OSSet::withCapacity(numSymbols
);
802 TEST_ASSERT('I', "2a", set
);
805 for (i
= 0; i
< numSymbols
; i
++)
806 count1
+= set
->setObject(cache
[i
]);
807 TEST_ASSERT('I', "2b", count1
== numSymbols
);
809 iter1
= OSCollectionIterator::withCollection(set
);
810 iter2
= OSCollectionIterator::withCollection(set
);
812 TEST_ASSERT('I', "2c", iter1
);
813 TEST_ASSERT('I', "2d", iter2
);
814 if (iter1
&& iter2
) {
815 count1
= count2
= count3
= 0;
816 for (i
= 0; (str
= (IOString
*) iter1
->getNextObject()); i
++) {
817 bigReturn
= iter2
->nextEntries();
818 count1
+= (bigReturn
->getCount() == 1);
819 count2
+= (cache
[i
] == bigReturn
->getObject(0));
820 count3
+= (cache
[i
] == str
);
822 TEST_ASSERT('I', "2e", count1
== numSymbols
);
823 TEST_ASSERT('I', "2f", count2
== numSymbols
);
824 TEST_ASSERT('I', "2g", count3
== numSymbols
);
825 TEST_ASSERT('I', "2h", iter1
->valid());
826 TEST_ASSERT('I', "2i", iter2
->valid());
829 count1
= count2
= count3
= 0;
832 str
= (OSString
*) iter1
->getNextObject();
833 else if ( (bigReturn
= iter1
->nextEntries()) )
834 str
= (OSString
*) bigReturn
->getObject(0);
840 count1
+= (cache
[i
] == str
);
842 TEST_ASSERT('I', "2l", count1
== numSymbols
);
843 TEST_ASSERT('I', "2m", i
== numSymbols
);
844 TEST_ASSERT('I', "2n", iter1
->valid());
847 str
= (OSString
*) set
->getAnyObject();
848 (void) set
->__takeObject(str
);
851 TEST_ASSERT('I', "2j", !iter1
->getNextObject());
852 TEST_ASSERT('I', "2k", !iter1
->valid());
854 TEST_ASSERT('I', "2o", 3 == set
->getRetainCount());
858 if (iter1
) iter1
->release();
859 if (iter2
) iter2
->release();
860 res
= res
&& checkSpace("(I)2", spaceCheck
, 0);
862 // Test the dictionary iterator
863 spaceCheck
= checkPointSpace();
865 dict
= OSDictionary::withCapacity(numSymbols
);
866 TEST_ASSERT('I', "3a", dict
);
869 for (i
= 0; i
< numSymbols
; i
++)
870 count1
+= (0 != dict
->setObject(cache
[i
], symCache
[i
]));
871 TEST_ASSERT('I', "3b", count1
== numSymbols
);
873 iter1
= OSCollectionIterator::withCollection(dict
);
874 iter2
= OSCollectionIterator::withCollection(dict
);
876 TEST_ASSERT('I', "3c", iter1
);
877 TEST_ASSERT('I', "3d", iter2
);
878 if (iter1
&& iter2
) {
879 count1
= count2
= count3
= 0;
880 for (i
= 0; (sym
= (const IOSymbol
*) iter1
->getNextObject()); i
++) {
881 bigReturn
= iter2
->nextEntries();
882 count1
+= (bigReturn
->getCount() == 2);
883 count2
+= (cache
[i
] == bigReturn
->getObject(1));
884 count3
+= (symCache
[i
] == sym
);
886 TEST_ASSERT('I', "3e", count1
== numSymbols
);
887 TEST_ASSERT('I', "3f", count2
== numSymbols
);
888 TEST_ASSERT('I', "3g", count3
== numSymbols
);
889 TEST_ASSERT('I', "3h", iter1
->valid());
890 TEST_ASSERT('I', "3i", iter2
->valid());
893 count1
= count2
= count3
= 0;
897 sym
= (const OSSymbol
*) iter1
->getNextObject();
900 else if ( (bigReturn
= iter1
->nextEntries()) ) {
901 sym
= (const OSSymbol
*) bigReturn
->getObject(0);
902 str
= (OSString
*) bigReturn
->getObject(1);
910 count1
+= (symCache
[i
] == sym
);
911 count2
+= (!str
|| cache
[i
] == str
);
913 TEST_ASSERT('I', "3l", count1
== numSymbols
);
914 TEST_ASSERT('I', "3m", count2
== numSymbols
);
915 TEST_ASSERT('I', "3n", i
== numSymbols
);
916 TEST_ASSERT('I', "3o", iter1
->valid());
919 str
= (OSString
*) dict
->__takeObject(symCache
[numSymbols
-1]);
920 dict
->setObject(str
, symCache
[numSymbols
-1]);
922 TEST_ASSERT('I', "3j", !iter1
->getNextObject());
923 TEST_ASSERT('I', "3k", !iter1
->valid());
925 TEST_ASSERT('I', "3p", 3 == dict
->getRetainCount());
929 if (iter1
) iter1
->release();
930 if (iter2
) iter2
->release();
931 res
= res
&& checkSpace("(I)3", spaceCheck
, 0);
933 count1
= count2
= count3
= 0;
934 for (i
= 0; i
< numSymbols
; i
++) {
935 count1
+= (1 == cache
[i
]->getRetainCount());
936 count2
+= (1 == symCache
[i
]->getRetainCount());
938 symCache
[i
]->release();
940 TEST_ASSERT('I', "4a", count1
== numSymbols
);
941 TEST_ASSERT('I', "4b", count2
== numSymbols
);
944 verPrintf(("testIterator: All OSCollectionIterator Tests passed\n"));
946 logPrintf(("testIterator: Some OSCollectionIterator Tests failed\n"));