]>
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_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
25 #include <libkern/c++/OSArray.h>
26 #include <libkern/c++/OSSet.h>
27 #include <libkern/c++/OSDictionary.h>
28 #include <libkern/c++/OSString.h>
29 #include <libkern/c++/OSSymbol.h>
30 #include <libkern/c++/OSCollectionIterator.h>
35 void *spaceCheck
, *spaceCheck2
, *spaceCheck3
;
36 int i
, j
, count
, count2
;
37 OSObject
*cache
[numStrCache
], *str
, *sym
;
38 OSArray
*array1
, *array2
;
40 // Do first test without memory leak tests to initialise the metaclass
41 array1
= OSArray::withCapacity(1);
42 TEST_ASSERT('A', "0a", array1
);
46 // Grow the symbol pool to maximum
47 for (i
= 0; i
< numStrCache
; i
++)
48 cache
[i
] = (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[i
]);
49 for (i
= 0; i
< numStrCache
; i
++)
52 // Create and destroy an array
53 spaceCheck
= checkPointSpace();
54 array1
= OSArray::withCapacity(1);
55 TEST_ASSERT('A', "1a", array1
);
57 TEST_ASSERT('A', "1b", !array1
->getCount());
58 TEST_ASSERT('A', "1c", 1 == array1
->getCapacity());
59 TEST_ASSERT('A', "1d", 1 == array1
->getCapacityIncrement());
60 TEST_ASSERT('A', "1e", 4 == array1
->setCapacityIncrement(4));
61 TEST_ASSERT('A', "1f", 4 == array1
->getCapacityIncrement());
62 TEST_ASSERT('A', "1g", 8 == array1
->ensureCapacity(5));
64 spaceCheck2
= checkPointSpace();
65 cache
[0] = IOString::withCStringNoCopy(strCache
[0]);
67 spaceCheck3
= checkPointSpace();
68 TEST_ASSERT('A', "1h", array1
->setObject(cache
[0]));
69 TEST_ASSERT('A', "1i", cache
[0] == array1
->getObject(0));
71 res
= res
&& checkSpace("(A)1j", spaceCheck3
, 0);
73 TEST_ASSERT('A', "1k", 1 == array1
->getCount());
74 array1
->flushCollection();
75 TEST_ASSERT('A', "1l", !array1
->getCount());
76 res
= res
&& checkSpace("(A)1m", spaceCheck2
, 0);
80 res
= res
&& checkSpace("(A)1", spaceCheck
, 0);
82 // Check the creation of a sizable OSArray from an array of IOObjects
83 // Also check indexing into the array.
84 spaceCheck
= checkPointSpace();
85 for (i
= 0; i
< numStrCache
; i
++)
86 cache
[i
] = OSString::withCStringNoCopy(strCache
[i
]);
87 array1
= OSArray::withObjects(cache
, numStrCache
, numStrCache
);
88 TEST_ASSERT('A', "2a", array1
);
89 for (i
= 0; i
< numStrCache
; i
++)
92 TEST_ASSERT('A', "2b", numStrCache
== (int) array1
->getCount());
93 TEST_ASSERT('A', "2c", numStrCache
== (int) array1
->getCapacity());
94 TEST_ASSERT('A', "2d",
95 numStrCache
== (int) array1
->getCapacityIncrement());
97 for (i
= 0; (str
= array1
->getObject(i
)); i
++) {
98 if (str
!= cache
[i
]) {
99 verPrintf(("testArray(A) test 2e%d failed\n", i
));
103 TEST_ASSERT('A', "2f", numStrCache
== i
);
106 res
= res
&& checkSpace("(A)2", spaceCheck
, 0);
108 // Test array creation from another array by both the setObject method
109 // and the withArray factory. And test __takeObject code first
110 // with tail removal then with head removal
111 spaceCheck
= checkPointSpace();
112 for (i
= 0; i
< numStrCache
; i
++)
113 cache
[i
] = OSString::withCStringNoCopy(strCache
[i
]);
114 array1
= OSArray::withObjects(cache
, numStrCache
, numStrCache
);
115 TEST_ASSERT('A', "3a", array1
);
116 for (i
= 0; i
< numStrCache
; i
++)
120 array2
= OSArray::withCapacity(1);
121 TEST_ASSERT('A', "3b", array2
);
122 TEST_ASSERT('A', "3c", !array2
->getCount());
123 TEST_ASSERT('A', "3d", array2
->setObject(array1
));
124 TEST_ASSERT('A', "3e", array1
->getCount() == array2
->getCount());
128 TEST_ASSERT('A', "3f", numStrCache
== (int) array2
->getCount());
129 for (i
= array2
->getCount(); (str
= array2
->__takeObject(--i
)); ) {
130 if (str
!= cache
[i
]) {
131 verPrintf(("testArray(A) test 3g%d failed\n", i
));
134 count
+= ((int) array2
->getCount() == i
);
137 TEST_ASSERT('A', "3h", count
== numStrCache
);
138 TEST_ASSERT('A', "3i", -1 == i
);
139 TEST_ASSERT('A', "3j", !array2
->getCount());
141 spaceCheck2
= checkPointSpace();
142 array2
->flushCollection();
143 res
= res
&& checkSpace("(A)3k", spaceCheck2
, 0);
149 array2
= OSArray::withArray(array1
, numStrCache
- 1);
150 TEST_ASSERT('A', "3l", !array2
);
151 array2
= OSArray::withArray(array1
, array1
->getCount());
152 TEST_ASSERT('A', "3m", array2
);
157 TEST_ASSERT('A', "3o", numStrCache
== (int) array2
->getCount());
158 for (i
= 0; (str
= array2
->__takeObject(0)); i
++) {
159 count
+= (str
== cache
[i
]);
162 TEST_ASSERT('A', "3p", count
== numStrCache
);
163 TEST_ASSERT('A', "3q", !array2
->getCount());
167 res
= res
&& checkSpace("(A)3", spaceCheck
, 0);
169 // Test object replacement from one array to another
170 spaceCheck
= checkPointSpace();
171 array1
= OSArray::withCapacity(numStrCache
);
172 TEST_ASSERT('A', "4a", array1
);
175 for (i
= 0; i
< numStrCache
; i
++) {
176 str
= OSString::withCStringNoCopy(strCache
[i
]);
177 count
+= array1
->setObject(str
);
178 count2
+= (str
== array1
->lastObject());
181 TEST_ASSERT('A', "4b", numStrCache
== (int) array1
->getCount());
182 TEST_ASSERT('A', "4c", count
== numStrCache
);
183 TEST_ASSERT('A', "4d", count2
== numStrCache
);
185 array2
= OSArray::withCapacity(1);
186 TEST_ASSERT('A', "4e", array2
);
189 str
= (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[0]);
190 for (i
= 0; i
< numStrCache
; i
++) {
191 sym
= (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[i
]);
192 count
+= array2
->setObject(sym
, 0);
193 count2
+= (str
== array2
->lastObject());
197 TEST_ASSERT('A', "4f", numStrCache
== (int) array2
->getCount());
198 TEST_ASSERT('A', "4g", count
== numStrCache
);
199 TEST_ASSERT('A', "4h", count2
== numStrCache
);
201 if (array1
&& array2
) {
204 for (i
= array1
->getCount() - 1; (sym
= array2
->__takeObject(0)); i
--) {
205 str
= array1
->replaceObject(sym
, i
);
207 count2
+= (sym
!= str
);
213 TEST_ASSERT('A', "4k", numStrCache
== (int) array1
->getCount());
214 TEST_ASSERT('A', "4l", count
== numStrCache
);
215 TEST_ASSERT('A', "4m", count2
== numStrCache
);
220 if (array1
) array1
->release();
221 if (array2
) array2
->release();
223 res
= res
&& checkSpace("(A)4", spaceCheck
, 0);
225 // Test array duplicate removal
226 spaceCheck
= checkPointSpace();
227 array1
= OSArray::withCapacity(numStrCache
);
228 TEST_ASSERT('A', "5a", array1
);
230 for (i
= 0; i
< numStrCache
; i
++) {
231 sym
= (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[i
]);
232 count
+= array1
->setObject(sym
);
235 TEST_ASSERT('A', "5b", numStrCache
== (int) array1
->getCount());
238 for (i
= 0; (sym
= array1
->getObject(i
)); )
239 if (sym
->getRetainCount() == 1)
242 //sym = array1->__takeObject(i);
244 array1
->removeObject(i
);
246 TEST_ASSERT('A', "5c", numStrCache
!= (int) array1
->getCount());
248 // check to see that all symbols are really there
249 for (count
= 0, i
= 0; i
< numStrCache
; i
++) {
250 sym
= (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[i
]);
251 for (count2
= false, j
= 0; (str
= array1
->getObject(j
)); j
++)
259 TEST_ASSERT('A', "5c", count
== numStrCache
);
262 res
= res
&& checkSpace("(S)5", spaceCheck
, 0);
265 verPrintf(("testArray: All OSArray Tests passed\n"));
267 logPrintf(("testArray: Some OSArray Tests failed\n"));
273 void *spaceCheck
, *spaceCheck2
, *spaceCheck3
;
274 int i
, count
, count2
;
275 OSObject
*cache
[numStrCache
], *str
, *sym
;
279 // Do first test without memory leak tests to initialise the metaclass
280 set1
= OSSet::withCapacity(1);
281 TEST_ASSERT('S', "0a", set1
);
285 // Grow the symbol pool to maximum
286 for (i
= 0; i
< numStrCache
; i
++)
287 cache
[i
] = (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[i
]);
288 for (i
= 0; i
< numStrCache
; i
++)
291 // Create and destroy an set
292 spaceCheck
= checkPointSpace();
293 set1
= OSSet::withCapacity(1);
294 TEST_ASSERT('S', "1a", set1
);
296 TEST_ASSERT('S', "1b", !set1
->getCount());
297 TEST_ASSERT('S', "1c", 1 == set1
->getCapacity());
298 TEST_ASSERT('S', "1d", 1 == set1
->getCapacityIncrement());
299 TEST_ASSERT('S', "1e", 4 == set1
->setCapacityIncrement(4));
300 TEST_ASSERT('S', "1f", 4 == set1
->getCapacityIncrement());
301 TEST_ASSERT('S', "1g", 8 == set1
->ensureCapacity(5));
303 spaceCheck2
= checkPointSpace();
304 cache
[0] = IOString::withCStringNoCopy(strCache
[0]);
306 spaceCheck3
= checkPointSpace();
307 TEST_ASSERT('S', "1h", set1
->setObject(cache
[0]));
308 TEST_ASSERT('S', "1i", set1
->containsObject(cache
[0]));
309 TEST_ASSERT('S', "1j", cache
[0] == set1
->getAnyObject());
311 res
= res
&& checkSpace("(S)1k", spaceCheck3
, 0);
313 TEST_ASSERT('S', "1l", 1 == set1
->getCount());
314 set1
->flushCollection();
315 TEST_ASSERT('S', "1m", !set1
->getCount());
316 res
= res
&& checkSpace("(S)1n", spaceCheck2
, 0);
320 res
= res
&& checkSpace("(S)1", spaceCheck
, 0);
322 // Check the creation of a sizable OSSet from an set of IOObjects
323 // Also check member test of set.
324 spaceCheck
= checkPointSpace();
325 for (i
= 0; i
< numStrCache
; i
++)
326 cache
[i
] = OSString::withCStringNoCopy(strCache
[i
]);
327 set1
= OSSet::withObjects(cache
, numStrCache
, numStrCache
);
328 TEST_ASSERT('S', "2a", set1
);
329 for (i
= 0; i
< numStrCache
; i
++)
332 TEST_ASSERT('S', "2b", numStrCache
== (int) set1
->getCount());
333 TEST_ASSERT('S', "2c", numStrCache
== (int) set1
->getCapacity());
334 TEST_ASSERT('S', "2d",
335 numStrCache
== (int) set1
->getCapacityIncrement());
338 for (i
= set1
->getCount(); --i
>= 0; )
339 count
+= set1
->member(cache
[i
]);
341 TEST_ASSERT('S', "2e", numStrCache
== count
);
344 res
= res
&& checkSpace("(S)2", spaceCheck
, 0);
346 // Test set creation from another set by both the setObject method
347 // and the withArray factory. And test __takeObject code first
348 // with tail removal then with head removal
349 spaceCheck
= checkPointSpace();
350 for (i
= 0; i
< numStrCache
; i
++)
351 cache
[i
] = OSString::withCStringNoCopy(strCache
[i
]);
352 set1
= OSSet::withObjects(cache
, numStrCache
, numStrCache
);
353 TEST_ASSERT('S', "3a", set1
);
354 for (i
= 0; i
< numStrCache
; i
++)
358 set2
= OSSet::withCapacity(set1
->getCount());
359 TEST_ASSERT('S', "3b", set2
);
360 TEST_ASSERT('S', "3c", !set2
->getCount());
361 TEST_ASSERT('S', "3d", set2
->setObject(set1
));
362 TEST_ASSERT('S', "3e", set1
->getCount() == set2
->getCount());
365 TEST_ASSERT('S', "3f", numStrCache
== (int) set2
->getCount());
367 while ( (str
= set2
->getAnyObject()) ) {
368 count
+= set2
->__takeObject(str
);
369 count2
+= set1
->member(str
);
372 TEST_ASSERT('S', "3g", !set2
->getCount());
373 TEST_ASSERT('S', "3h", numStrCache
== count
);
374 TEST_ASSERT('S', "3i", numStrCache
== count2
);
376 spaceCheck2
= checkPointSpace();
377 set2
->flushCollection();
378 res
= res
&& checkSpace("(S)3j", spaceCheck2
, 0);
384 set2
= OSSet::withSet(set1
, numStrCache
- 1);
385 TEST_ASSERT('S', "3k", !set2
);
386 set2
= OSSet::withSet(set1
, set1
->getCount());
387 TEST_ASSERT('S', "3l", set2
);
391 TEST_ASSERT('S', "3m", numStrCache
== (int) set2
->getCount());
392 i
= count
= count2
= 0;
393 while ( (str
= set2
->getAnyObject()) ) {
394 count
+= set2
->__takeObject(str
);
395 count2
+= (cache
[i
++] == str
);
398 TEST_ASSERT('S', "3n", !set2
->getCount());
399 TEST_ASSERT('S', "3o", numStrCache
== count
);
400 TEST_ASSERT('S', "3p", numStrCache
== count2
);
405 res
= res
&& checkSpace("(S)3", spaceCheck
, 0);
407 // Test duplicate removal
408 spaceCheck
= checkPointSpace();
410 set1
= OSSet::withCapacity(numStrCache
);
411 TEST_ASSERT('S', "4a", set1
);
414 for (i
= 0; i
< numStrCache
; i
++) {
415 sym
= (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[i
]);
416 count
+= set1
->setObject(sym
);
419 TEST_ASSERT('S', "4b", numStrCache
!= (int) set1
->getCount());
420 TEST_ASSERT('S', "4c", count
== (int) set1
->getCount());
423 for (i
= 0; i
< numStrCache
; i
++) {
424 sym
= (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[i
]);
425 count
+= set1
->member(sym
);
426 count2
+= sym
->getRetainCount();
429 TEST_ASSERT('S', "4d", count
== numStrCache
);
430 TEST_ASSERT('S', "4e", count2
== numStrCache
* 2);
432 set2
= OSSet::withSet(set1
, 2 * set1
->getCount());
434 TEST_ASSERT('S', "4f", set2
);
436 set2
->setObject(set1
);
437 TEST_ASSERT('S', "4g", set1
->getCount() == set2
->getCount());
441 res
= res
&& checkSpace("(S)4", spaceCheck
, 0);
443 // Test array duplicate removal
444 spaceCheck
= checkPointSpace();
445 array
= OSArray::withCapacity(numStrCache
);
446 for (i
= 0; i
< numStrCache
; i
++) {
447 sym
= (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[i
]);
448 count
+= array
->setObject(sym
);
451 set1
= OSSet::withArray(array
, numStrCache
);
452 TEST_ASSERT('S', "5a", set1
);
454 TEST_ASSERT('S', "5b", array
->getCount() != set1
->getCount());
457 count
= count2
= set1
->getCount();
458 while ( (sym
= set1
->getAnyObject()) ) {
459 count
-= set1
->__takeObject(sym
);
460 count2
-= sym
->getRetainCount();
463 TEST_ASSERT('S', "5c", !count
);
464 TEST_ASSERT('S', "5d", !count2
);
467 res
= res
&& checkSpace("(S)5", spaceCheck
, 0);
470 verPrintf(("testSet: All OSSet Tests passed\n"));
472 logPrintf(("testSet: Some OSSet Tests failed\n"));
475 void testDictionary()
478 void *spaceCheck
, *spaceCheck2
, *spaceCheck3
;
479 OSObject
*cache
[numStrCache
];
481 const OSSymbol
*symCache
[numStrCache
], *sym
;
482 OSDictionary
*dict1
, *dict2
;
483 int i
, numSymbols
, count1
, count2
;
485 // Do first test without memory leak tests to initialise the metaclass
486 dict1
= OSDictionary::withCapacity(1);
487 TEST_ASSERT('D', "0a", dict1
);
491 // Grow the symbol pool to maximum
492 for (i
= 0; i
< numStrCache
; i
++)
493 symCache
[i
] = OSSymbol::withCStringNoCopy(strCache
[i
]);
494 for (i
= 0; i
< numStrCache
; i
++)
495 symCache
[i
]->release();
497 // Create and destroy a dictionary
498 spaceCheck
= checkPointSpace();
499 dict1
= OSDictionary::withCapacity(1);
500 TEST_ASSERT('D', "1a", dict1
);
502 TEST_ASSERT('D', "1b", !dict1
->getCount());
503 TEST_ASSERT('D', "1c", 1 == dict1
->getCapacity());
504 TEST_ASSERT('D', "1d", 1 == dict1
->getCapacityIncrement());
505 TEST_ASSERT('D', "1e", 4 == dict1
->setCapacityIncrement(4));
506 TEST_ASSERT('D', "1f", 4 == dict1
->getCapacityIncrement());
507 TEST_ASSERT('D', "1g", 8 == dict1
->ensureCapacity(5));
509 spaceCheck2
= checkPointSpace();
510 sym
= OSSymbol::withCStringNoCopy(strCache
[0]);
512 spaceCheck3
= checkPointSpace();
513 TEST_ASSERT('D', "1h", dict1
->setObject((OSObject
*) sym
, sym
));
514 TEST_ASSERT('D', "1i", (OSObject
*) sym
== dict1
->getObject(sym
));
516 TEST_ASSERT('D', "1i", 2 == sym
->getRetainCount());
517 res
= res
&& checkSpace("(D)1j", spaceCheck3
, 0);
519 TEST_ASSERT('D', "1k", 1 == dict1
->getCount());
520 dict1
->flushCollection();
521 TEST_ASSERT('D', "1l", !dict1
->getCount());
522 res
= res
&& checkSpace("(D)1m", spaceCheck2
, 0);
526 res
= res
&& checkSpace("(D)1", spaceCheck
, 0);
528 // Check the creation of a sizable OSDictionary from an array of IOObjects
529 // Also check indexing into the array.
530 spaceCheck
= checkPointSpace();
531 for (i
= 0, numSymbols
= 0; i
< numStrCache
; i
++) {
532 sym
= OSSymbol::withCStringNoCopy(strCache
[i
]);
533 if (1 == sym
->getRetainCount())
534 symCache
[numSymbols
++] = sym
;
538 dict1
= OSDictionary::withObjects(
539 (OSObject
**) symCache
, symCache
, numSymbols
, numSymbols
);
540 TEST_ASSERT('D', "2a", dict1
);
542 for (i
= 0; i
< numSymbols
; i
++)
543 count1
+= (symCache
[i
]->getRetainCount() == 3);
544 TEST_ASSERT('D', "2b", count1
== numSymbols
);
546 TEST_ASSERT('D', "2c", numSymbols
== (int) dict1
->getCount());
547 TEST_ASSERT('D', "2d", numSymbols
== (int) dict1
->getCapacity());
548 TEST_ASSERT('D', "2e",
549 numSymbols
== (int) dict1
->getCapacityIncrement());
551 for (i
= dict1
->getCount(); --i
>= 0; ) {
552 str
= (OSString
*) dict1
->getObject(symCache
[i
]);
553 if (str
!= (OSString
*) symCache
[i
]) {
554 verPrintf(("testDictionary(D) test 2f%d failed\n", i
));
561 for (i
= 0; i
< numSymbols
; i
++) {
562 count1
+= (symCache
[i
]->getRetainCount() == 1);
563 symCache
[i
]->release();
565 TEST_ASSERT('D', "2g", count1
== numSymbols
);
566 res
= res
&& checkSpace("(D)2", spaceCheck
, 0);
568 // Check the creation of a sizable Dictionary from an array of IOStrings
569 // Also check searching dictionary use OSString for a key.
570 spaceCheck
= checkPointSpace();
571 for (i
= 0, numSymbols
= 0; i
< numStrCache
; i
++) {
572 sym
= OSSymbol::withCStringNoCopy(strCache
[i
]);
573 if (1 == sym
->getRetainCount()) {
574 cache
[numSymbols
] = OSString::withCStringNoCopy(strCache
[i
]);
575 symCache
[numSymbols
] = sym
;
581 dict1
= OSDictionary::withObjects((OSObject
**) symCache
,
583 numSymbols
, numSymbols
);
584 TEST_ASSERT('D', "3a", dict1
);
586 for (i
= 0; i
< numSymbols
; i
++) {
587 count1
+= (symCache
[i
]->getRetainCount() == 3);
588 count2
+= (cache
[i
]->getRetainCount() == 1);
590 TEST_ASSERT('D', "3b", count1
== numSymbols
);
591 TEST_ASSERT('D', "3c", count2
== numSymbols
);
594 for (i
= 0; i
< numSymbols
; i
++) {
595 str
= (OSString
*) cache
[i
];
596 count1
+= (symCache
[i
] == (const OSSymbol
*) dict1
->getObject(str
));
597 count2
+= (symCache
[i
]->getRetainCount() == 3);
599 TEST_ASSERT('D', "3d", count1
== numSymbols
);
600 TEST_ASSERT('D', "3e", count2
== numSymbols
);
603 for (i
= 0; i
< numSymbols
; i
++) {
604 const char *cStr
= ((OSString
*) cache
[i
])->getCStringNoCopy();
606 count1
+= (symCache
[i
] == (const OSSymbol
*) dict1
->getObject(cStr
));
607 count2
+= (symCache
[i
]->getRetainCount() == 3);
609 TEST_ASSERT('D', "3f", count1
== numSymbols
);
610 TEST_ASSERT('D', "3g", count2
== numSymbols
);
615 for (i
= 0; i
< numSymbols
; i
++) {
616 count1
+= (symCache
[i
]->getRetainCount() == 1);
617 count2
+= (cache
[i
]->getRetainCount() == 1);
618 symCache
[i
]->release();
621 TEST_ASSERT('D', "3h", count1
== numSymbols
);
622 res
= res
&& checkSpace("(D)3", spaceCheck
, 0);
624 // Check the creation of a small dictionary then grow it one item at a time
625 // Create a new dictionary from the old dictionary.
626 // Finally remove each item permanently.
627 spaceCheck
= checkPointSpace();
628 for (i
= 0, numSymbols
= 0; i
< numStrCache
; i
++) {
629 sym
= OSSymbol::withCStringNoCopy(strCache
[i
]);
630 if (1 == sym
->getRetainCount()) {
631 cache
[numSymbols
] = OSString::withCStringNoCopy(strCache
[i
]);
632 symCache
[numSymbols
] = sym
;
639 dict1
= OSDictionary::withCapacity(1);
640 TEST_ASSERT('D', "4a", dict1
);
643 for (i
= 0; i
< numSymbols
; i
++) {
645 count1
+= ((OSObject
*) sym
== dict1
->setObject((OSObject
*) sym
,
646 sym
->getCStringNoCopy()));
647 count2
+= (sym
->getRetainCount() == 3);
649 TEST_ASSERT('D', "4b", numSymbols
== (int) dict1
->getCount());
650 TEST_ASSERT('D', "4c", numSymbols
== count1
);
651 TEST_ASSERT('D', "4d", numSymbols
== count2
);
653 dict2
= OSDictionary::withDictionary(dict1
, numSymbols
-1);
654 TEST_ASSERT('D', "4b", !dict2
);
655 dict2
= OSDictionary::withDictionary(dict1
, numSymbols
);
657 TEST_ASSERT('D', "4e", dict2
);
659 dict1
->release(); dict1
= 0;
661 TEST_ASSERT('D', "4f", numSymbols
== (int) dict2
->getCount());
664 for (i
= 0; i
< numSymbols
; i
++) {
665 OSObject
*replacedObject
;
668 str
= (OSString
*) cache
[i
];
669 replacedObject
= dict2
->setObject(str
, str
);
670 count1
+= ((OSString
*) sym
== replacedObject
);
671 replacedObject
->release();
672 count2
+= (sym
->getRetainCount() == 2);
675 TEST_ASSERT('D', "4g", numSymbols
== count1
);
676 TEST_ASSERT('D', "4h", numSymbols
== count2
);
679 for (i
= 0; i
< numSymbols
; i
++) {
681 str
= (OSString
*) cache
[i
];
682 count1
+= (str
== dict2
->__takeObject(sym
));
684 count2
+= (sym
->getRetainCount() == 1);
687 TEST_ASSERT('D', "4i", numSymbols
== count1
);
688 TEST_ASSERT('D', "4j", numSymbols
== count2
);
689 TEST_ASSERT('D', "4k", !dict2
->getCount());
690 dict2
->release(); dict2
= 0;
694 res
= res
&& checkSpace("(D)4", spaceCheck
, 0);
697 verPrintf(("testDictionary: All OSDictionary Tests passed\n"));
699 logPrintf(("testDictionary: Some OSDictionary Tests failed\n"));
706 OSObject
*cache
[numStrCache
];
708 const OSSymbol
*symCache
[numStrCache
], *sym
;
711 OSArray
*array
, *bigReturn
;
712 OSCollectionIterator
*iter1
, *iter2
;
713 int i
, numSymbols
, count1
, count2
, count3
;
715 // Setup symbol and string pools
716 for (i
= 0, numSymbols
= 0; i
< numStrCache
; i
++) {
717 sym
= OSSymbol::withCStringNoCopy(strCache
[i
]);
718 if (1 == sym
->getRetainCount()) {
719 cache
[numSymbols
] = OSString::withCStringNoCopy(strCache
[i
]);
720 symCache
[numSymbols
] = sym
;
727 // Test the array iterator
728 spaceCheck
= checkPointSpace();
730 array
= OSArray::withCapacity(numSymbols
);
731 TEST_ASSERT('I', "1a", array
);
734 for (i
= numSymbols
; --i
>= 0; )
735 count1
+= array
->setObject(cache
[i
], 0);
736 TEST_ASSERT('I', "1b", count1
== numSymbols
);
738 iter1
= OSCollectionIterator::withCollection(array
);
739 iter2
= OSCollectionIterator::withCollection(array
);
741 TEST_ASSERT('I', "1c", iter1
);
742 TEST_ASSERT('I', "1d", iter2
);
743 if (iter1
&& iter2
) {
744 count1
= count2
= count3
= 0;
745 for (i
= 0; (str
= (IOString
*) iter1
->getNextObject()); i
++) {
746 bigReturn
= iter2
->nextEntries();
747 count1
+= (bigReturn
->getCount() == 1);
748 count2
+= (cache
[i
] == bigReturn
->getObject(0));
749 count3
+= (cache
[i
] == str
);
751 TEST_ASSERT('I', "1e", count1
== numSymbols
);
752 TEST_ASSERT('I', "1f", count2
== numSymbols
);
753 TEST_ASSERT('I', "1g", count3
== numSymbols
);
754 TEST_ASSERT('I', "1h", iter1
->valid());
755 TEST_ASSERT('I', "1i", iter2
->valid());
758 str
= (OSString
*) array
->__takeObject(0);
759 array
->setObject(str
, 0);
761 TEST_ASSERT('I', "1j", !iter1
->getNextObject());
762 TEST_ASSERT('I', "1k", !iter1
->valid());
765 count1
= count2
= count3
= 0;
768 str
= (OSString
*) iter1
->getNextObject();
769 else if ( (bigReturn
= iter1
->nextEntries()) )
770 str
= (OSString
*) bigReturn
->getObject(0);
776 count1
+= (cache
[i
] == str
);
778 TEST_ASSERT('I', "1l", count1
== numSymbols
);
779 TEST_ASSERT('I', "1m", i
== numSymbols
);
780 TEST_ASSERT('I', "1n", iter1
->valid());
782 TEST_ASSERT('I', "1o", 3 == array
->getRetainCount());
786 if (iter1
) iter1
->release();
787 if (iter2
) iter2
->release();
788 res
= res
&& checkSpace("(I)1", spaceCheck
, 0);
790 // Test the set iterator
791 spaceCheck
= checkPointSpace();
793 set
= OSSet::withCapacity(numSymbols
);
794 TEST_ASSERT('I', "2a", set
);
797 for (i
= 0; i
< numSymbols
; i
++)
798 count1
+= set
->setObject(cache
[i
]);
799 TEST_ASSERT('I', "2b", count1
== numSymbols
);
801 iter1
= OSCollectionIterator::withCollection(set
);
802 iter2
= OSCollectionIterator::withCollection(set
);
804 TEST_ASSERT('I', "2c", iter1
);
805 TEST_ASSERT('I', "2d", iter2
);
806 if (iter1
&& iter2
) {
807 count1
= count2
= count3
= 0;
808 for (i
= 0; (str
= (IOString
*) iter1
->getNextObject()); i
++) {
809 bigReturn
= iter2
->nextEntries();
810 count1
+= (bigReturn
->getCount() == 1);
811 count2
+= (cache
[i
] == bigReturn
->getObject(0));
812 count3
+= (cache
[i
] == str
);
814 TEST_ASSERT('I', "2e", count1
== numSymbols
);
815 TEST_ASSERT('I', "2f", count2
== numSymbols
);
816 TEST_ASSERT('I', "2g", count3
== numSymbols
);
817 TEST_ASSERT('I', "2h", iter1
->valid());
818 TEST_ASSERT('I', "2i", iter2
->valid());
821 count1
= count2
= count3
= 0;
824 str
= (OSString
*) iter1
->getNextObject();
825 else if ( (bigReturn
= iter1
->nextEntries()) )
826 str
= (OSString
*) bigReturn
->getObject(0);
832 count1
+= (cache
[i
] == str
);
834 TEST_ASSERT('I', "2l", count1
== numSymbols
);
835 TEST_ASSERT('I', "2m", i
== numSymbols
);
836 TEST_ASSERT('I', "2n", iter1
->valid());
839 str
= (OSString
*) set
->getAnyObject();
840 (void) set
->__takeObject(str
);
843 TEST_ASSERT('I', "2j", !iter1
->getNextObject());
844 TEST_ASSERT('I', "2k", !iter1
->valid());
846 TEST_ASSERT('I', "2o", 3 == set
->getRetainCount());
850 if (iter1
) iter1
->release();
851 if (iter2
) iter2
->release();
852 res
= res
&& checkSpace("(I)2", spaceCheck
, 0);
854 // Test the dictionary iterator
855 spaceCheck
= checkPointSpace();
857 dict
= OSDictionary::withCapacity(numSymbols
);
858 TEST_ASSERT('I', "3a", dict
);
861 for (i
= 0; i
< numSymbols
; i
++)
862 count1
+= (0 != dict
->setObject(cache
[i
], symCache
[i
]));
863 TEST_ASSERT('I', "3b", count1
== numSymbols
);
865 iter1
= OSCollectionIterator::withCollection(dict
);
866 iter2
= OSCollectionIterator::withCollection(dict
);
868 TEST_ASSERT('I', "3c", iter1
);
869 TEST_ASSERT('I', "3d", iter2
);
870 if (iter1
&& iter2
) {
871 count1
= count2
= count3
= 0;
872 for (i
= 0; (sym
= (const IOSymbol
*) iter1
->getNextObject()); i
++) {
873 bigReturn
= iter2
->nextEntries();
874 count1
+= (bigReturn
->getCount() == 2);
875 count2
+= (cache
[i
] == bigReturn
->getObject(1));
876 count3
+= (symCache
[i
] == sym
);
878 TEST_ASSERT('I', "3e", count1
== numSymbols
);
879 TEST_ASSERT('I', "3f", count2
== numSymbols
);
880 TEST_ASSERT('I', "3g", count3
== numSymbols
);
881 TEST_ASSERT('I', "3h", iter1
->valid());
882 TEST_ASSERT('I', "3i", iter2
->valid());
885 count1
= count2
= count3
= 0;
889 sym
= (const OSSymbol
*) iter1
->getNextObject();
892 else if ( (bigReturn
= iter1
->nextEntries()) ) {
893 sym
= (const OSSymbol
*) bigReturn
->getObject(0);
894 str
= (OSString
*) bigReturn
->getObject(1);
902 count1
+= (symCache
[i
] == sym
);
903 count2
+= (!str
|| cache
[i
] == str
);
905 TEST_ASSERT('I', "3l", count1
== numSymbols
);
906 TEST_ASSERT('I', "3m", count2
== numSymbols
);
907 TEST_ASSERT('I', "3n", i
== numSymbols
);
908 TEST_ASSERT('I', "3o", iter1
->valid());
911 str
= (OSString
*) dict
->__takeObject(symCache
[numSymbols
-1]);
912 dict
->setObject(str
, symCache
[numSymbols
-1]);
914 TEST_ASSERT('I', "3j", !iter1
->getNextObject());
915 TEST_ASSERT('I', "3k", !iter1
->valid());
917 TEST_ASSERT('I', "3p", 3 == dict
->getRetainCount());
921 if (iter1
) iter1
->release();
922 if (iter2
) iter2
->release();
923 res
= res
&& checkSpace("(I)3", spaceCheck
, 0);
925 count1
= count2
= count3
= 0;
926 for (i
= 0; i
< numSymbols
; i
++) {
927 count1
+= (1 == cache
[i
]->getRetainCount());
928 count2
+= (1 == symCache
[i
]->getRetainCount());
930 symCache
[i
]->release();
932 TEST_ASSERT('I', "4a", count1
== numSymbols
);
933 TEST_ASSERT('I', "4b", count2
== numSymbols
);
936 verPrintf(("testIterator: All OSCollectionIterator Tests passed\n"));
938 logPrintf(("testIterator: Some OSCollectionIterator Tests failed\n"));