]>
git.saurik.com Git - apple/xnu.git/blob - iokit/Tests/TestCollections.cpp
0a961637e213a61319cb990c5cf9ebcbd09dfb93
2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
26 #include <libkern/c++/OSArray.h>
27 #include <libkern/c++/OSSet.h>
28 #include <libkern/c++/OSDictionary.h>
29 #include <libkern/c++/OSString.h>
30 #include <libkern/c++/OSSymbol.h>
31 #include <libkern/c++/OSCollectionIterator.h>
36 void *spaceCheck
, *spaceCheck2
, *spaceCheck3
;
37 int i
, j
, count
, count2
;
38 OSObject
*cache
[numStrCache
], *str
, *sym
;
39 OSArray
*array1
, *array2
;
41 // Do first test without memory leak tests to initialise the metaclass
42 array1
= OSArray::withCapacity(1);
43 TEST_ASSERT('A', "0a", array1
);
47 // Grow the symbol pool to maximum
48 for (i
= 0; i
< numStrCache
; i
++)
49 cache
[i
] = (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[i
]);
50 for (i
= 0; i
< numStrCache
; i
++)
53 // Create and destroy an array
54 spaceCheck
= checkPointSpace();
55 array1
= OSArray::withCapacity(1);
56 TEST_ASSERT('A', "1a", array1
);
58 TEST_ASSERT('A', "1b", !array1
->getCount());
59 TEST_ASSERT('A', "1c", 1 == array1
->getCapacity());
60 TEST_ASSERT('A', "1d", 1 == array1
->getCapacityIncrement());
61 TEST_ASSERT('A', "1e", 4 == array1
->setCapacityIncrement(4));
62 TEST_ASSERT('A', "1f", 4 == array1
->getCapacityIncrement());
63 TEST_ASSERT('A', "1g", 8 == array1
->ensureCapacity(5));
65 spaceCheck2
= checkPointSpace();
66 cache
[0] = IOString::withCStringNoCopy(strCache
[0]);
68 spaceCheck3
= checkPointSpace();
69 TEST_ASSERT('A', "1h", array1
->setObject(cache
[0]));
70 TEST_ASSERT('A', "1i", cache
[0] == array1
->getObject(0));
72 res
= res
&& checkSpace("(A)1j", spaceCheck3
, 0);
74 TEST_ASSERT('A', "1k", 1 == array1
->getCount());
75 array1
->flushCollection();
76 TEST_ASSERT('A', "1l", !array1
->getCount());
77 res
= res
&& checkSpace("(A)1m", spaceCheck2
, 0);
81 res
= res
&& checkSpace("(A)1", spaceCheck
, 0);
83 // Check the creation of a sizable OSArray from an array of IOObjects
84 // Also check indexing into the array.
85 spaceCheck
= checkPointSpace();
86 for (i
= 0; i
< numStrCache
; i
++)
87 cache
[i
] = OSString::withCStringNoCopy(strCache
[i
]);
88 array1
= OSArray::withObjects(cache
, numStrCache
, numStrCache
);
89 TEST_ASSERT('A', "2a", array1
);
90 for (i
= 0; i
< numStrCache
; i
++)
93 TEST_ASSERT('A', "2b", numStrCache
== (int) array1
->getCount());
94 TEST_ASSERT('A', "2c", numStrCache
== (int) array1
->getCapacity());
95 TEST_ASSERT('A', "2d",
96 numStrCache
== (int) array1
->getCapacityIncrement());
98 for (i
= 0; (str
= array1
->getObject(i
)); i
++) {
99 if (str
!= cache
[i
]) {
100 verPrintf(("testArray(A) test 2e%d failed\n", i
));
104 TEST_ASSERT('A', "2f", numStrCache
== i
);
107 res
= res
&& checkSpace("(A)2", spaceCheck
, 0);
109 // Test array creation from another array by both the setObject method
110 // and the withArray factory. And test __takeObject code first
111 // with tail removal then with head removal
112 spaceCheck
= checkPointSpace();
113 for (i
= 0; i
< numStrCache
; i
++)
114 cache
[i
] = OSString::withCStringNoCopy(strCache
[i
]);
115 array1
= OSArray::withObjects(cache
, numStrCache
, numStrCache
);
116 TEST_ASSERT('A', "3a", array1
);
117 for (i
= 0; i
< numStrCache
; i
++)
121 array2
= OSArray::withCapacity(1);
122 TEST_ASSERT('A', "3b", array2
);
123 TEST_ASSERT('A', "3c", !array2
->getCount());
124 TEST_ASSERT('A', "3d", array2
->setObject(array1
));
125 TEST_ASSERT('A', "3e", array1
->getCount() == array2
->getCount());
129 TEST_ASSERT('A', "3f", numStrCache
== (int) array2
->getCount());
130 for (i
= array2
->getCount(); (str
= array2
->__takeObject(--i
)); ) {
131 if (str
!= cache
[i
]) {
132 verPrintf(("testArray(A) test 3g%d failed\n", i
));
135 count
+= ((int) array2
->getCount() == i
);
138 TEST_ASSERT('A', "3h", count
== numStrCache
);
139 TEST_ASSERT('A', "3i", -1 == i
);
140 TEST_ASSERT('A', "3j", !array2
->getCount());
142 spaceCheck2
= checkPointSpace();
143 array2
->flushCollection();
144 res
= res
&& checkSpace("(A)3k", spaceCheck2
, 0);
150 array2
= OSArray::withArray(array1
, numStrCache
- 1);
151 TEST_ASSERT('A', "3l", !array2
);
152 array2
= OSArray::withArray(array1
, array1
->getCount());
153 TEST_ASSERT('A', "3m", array2
);
158 TEST_ASSERT('A', "3o", numStrCache
== (int) array2
->getCount());
159 for (i
= 0; (str
= array2
->__takeObject(0)); i
++) {
160 count
+= (str
== cache
[i
]);
163 TEST_ASSERT('A', "3p", count
== numStrCache
);
164 TEST_ASSERT('A', "3q", !array2
->getCount());
168 res
= res
&& checkSpace("(A)3", spaceCheck
, 0);
170 // Test object replacement from one array to another
171 spaceCheck
= checkPointSpace();
172 array1
= OSArray::withCapacity(numStrCache
);
173 TEST_ASSERT('A', "4a", array1
);
176 for (i
= 0; i
< numStrCache
; i
++) {
177 str
= OSString::withCStringNoCopy(strCache
[i
]);
178 count
+= array1
->setObject(str
);
179 count2
+= (str
== array1
->lastObject());
182 TEST_ASSERT('A', "4b", numStrCache
== (int) array1
->getCount());
183 TEST_ASSERT('A', "4c", count
== numStrCache
);
184 TEST_ASSERT('A', "4d", count2
== numStrCache
);
186 array2
= OSArray::withCapacity(1);
187 TEST_ASSERT('A', "4e", array2
);
190 str
= (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[0]);
191 for (i
= 0; i
< numStrCache
; i
++) {
192 sym
= (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[i
]);
193 count
+= array2
->setObject(sym
, 0);
194 count2
+= (str
== array2
->lastObject());
198 TEST_ASSERT('A', "4f", numStrCache
== (int) array2
->getCount());
199 TEST_ASSERT('A', "4g", count
== numStrCache
);
200 TEST_ASSERT('A', "4h", count2
== numStrCache
);
202 if (array1
&& array2
) {
205 for (i
= array1
->getCount() - 1; (sym
= array2
->__takeObject(0)); i
--) {
206 str
= array1
->replaceObject(sym
, i
);
208 count2
+= (sym
!= str
);
214 TEST_ASSERT('A', "4k", numStrCache
== (int) array1
->getCount());
215 TEST_ASSERT('A', "4l", count
== numStrCache
);
216 TEST_ASSERT('A', "4m", count2
== numStrCache
);
221 if (array1
) array1
->release();
222 if (array2
) array2
->release();
224 res
= res
&& checkSpace("(A)4", spaceCheck
, 0);
226 // Test array duplicate removal
227 spaceCheck
= checkPointSpace();
228 array1
= OSArray::withCapacity(numStrCache
);
229 TEST_ASSERT('A', "5a", array1
);
231 for (i
= 0; i
< numStrCache
; i
++) {
232 sym
= (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[i
]);
233 count
+= array1
->setObject(sym
);
236 TEST_ASSERT('A', "5b", numStrCache
== (int) array1
->getCount());
239 for (i
= 0; (sym
= array1
->getObject(i
)); )
240 if (sym
->getRetainCount() == 1)
243 //sym = array1->__takeObject(i);
245 array1
->removeObject(i
);
247 TEST_ASSERT('A', "5c", numStrCache
!= (int) array1
->getCount());
249 // check to see that all symbols are really there
250 for (count
= 0, i
= 0; i
< numStrCache
; i
++) {
251 sym
= (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[i
]);
252 for (count2
= false, j
= 0; (str
= array1
->getObject(j
)); j
++)
260 TEST_ASSERT('A', "5c", count
== numStrCache
);
263 res
= res
&& checkSpace("(S)5", spaceCheck
, 0);
266 verPrintf(("testArray: All OSArray Tests passed\n"));
268 logPrintf(("testArray: Some OSArray Tests failed\n"));
274 void *spaceCheck
, *spaceCheck2
, *spaceCheck3
;
275 int i
, count
, count2
;
276 OSObject
*cache
[numStrCache
], *str
, *sym
;
280 // Do first test without memory leak tests to initialise the metaclass
281 set1
= OSSet::withCapacity(1);
282 TEST_ASSERT('S', "0a", set1
);
286 // Grow the symbol pool to maximum
287 for (i
= 0; i
< numStrCache
; i
++)
288 cache
[i
] = (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[i
]);
289 for (i
= 0; i
< numStrCache
; i
++)
292 // Create and destroy an set
293 spaceCheck
= checkPointSpace();
294 set1
= OSSet::withCapacity(1);
295 TEST_ASSERT('S', "1a", set1
);
297 TEST_ASSERT('S', "1b", !set1
->getCount());
298 TEST_ASSERT('S', "1c", 1 == set1
->getCapacity());
299 TEST_ASSERT('S', "1d", 1 == set1
->getCapacityIncrement());
300 TEST_ASSERT('S', "1e", 4 == set1
->setCapacityIncrement(4));
301 TEST_ASSERT('S', "1f", 4 == set1
->getCapacityIncrement());
302 TEST_ASSERT('S', "1g", 8 == set1
->ensureCapacity(5));
304 spaceCheck2
= checkPointSpace();
305 cache
[0] = IOString::withCStringNoCopy(strCache
[0]);
307 spaceCheck3
= checkPointSpace();
308 TEST_ASSERT('S', "1h", set1
->setObject(cache
[0]));
309 TEST_ASSERT('S', "1i", set1
->containsObject(cache
[0]));
310 TEST_ASSERT('S', "1j", cache
[0] == set1
->getAnyObject());
312 res
= res
&& checkSpace("(S)1k", spaceCheck3
, 0);
314 TEST_ASSERT('S', "1l", 1 == set1
->getCount());
315 set1
->flushCollection();
316 TEST_ASSERT('S', "1m", !set1
->getCount());
317 res
= res
&& checkSpace("(S)1n", spaceCheck2
, 0);
321 res
= res
&& checkSpace("(S)1", spaceCheck
, 0);
323 // Check the creation of a sizable OSSet from an set of IOObjects
324 // Also check member test of set.
325 spaceCheck
= checkPointSpace();
326 for (i
= 0; i
< numStrCache
; i
++)
327 cache
[i
] = OSString::withCStringNoCopy(strCache
[i
]);
328 set1
= OSSet::withObjects(cache
, numStrCache
, numStrCache
);
329 TEST_ASSERT('S', "2a", set1
);
330 for (i
= 0; i
< numStrCache
; i
++)
333 TEST_ASSERT('S', "2b", numStrCache
== (int) set1
->getCount());
334 TEST_ASSERT('S', "2c", numStrCache
== (int) set1
->getCapacity());
335 TEST_ASSERT('S', "2d",
336 numStrCache
== (int) set1
->getCapacityIncrement());
339 for (i
= set1
->getCount(); --i
>= 0; )
340 count
+= set1
->member(cache
[i
]);
342 TEST_ASSERT('S', "2e", numStrCache
== count
);
345 res
= res
&& checkSpace("(S)2", spaceCheck
, 0);
347 // Test set creation from another set by both the setObject method
348 // and the withArray factory. And test __takeObject code first
349 // with tail removal then with head removal
350 spaceCheck
= checkPointSpace();
351 for (i
= 0; i
< numStrCache
; i
++)
352 cache
[i
] = OSString::withCStringNoCopy(strCache
[i
]);
353 set1
= OSSet::withObjects(cache
, numStrCache
, numStrCache
);
354 TEST_ASSERT('S', "3a", set1
);
355 for (i
= 0; i
< numStrCache
; i
++)
359 set2
= OSSet::withCapacity(set1
->getCount());
360 TEST_ASSERT('S', "3b", set2
);
361 TEST_ASSERT('S', "3c", !set2
->getCount());
362 TEST_ASSERT('S', "3d", set2
->setObject(set1
));
363 TEST_ASSERT('S', "3e", set1
->getCount() == set2
->getCount());
366 TEST_ASSERT('S', "3f", numStrCache
== (int) set2
->getCount());
368 while ( (str
= set2
->getAnyObject()) ) {
369 count
+= set2
->__takeObject(str
);
370 count2
+= set1
->member(str
);
373 TEST_ASSERT('S', "3g", !set2
->getCount());
374 TEST_ASSERT('S', "3h", numStrCache
== count
);
375 TEST_ASSERT('S', "3i", numStrCache
== count2
);
377 spaceCheck2
= checkPointSpace();
378 set2
->flushCollection();
379 res
= res
&& checkSpace("(S)3j", spaceCheck2
, 0);
385 set2
= OSSet::withSet(set1
, numStrCache
- 1);
386 TEST_ASSERT('S', "3k", !set2
);
387 set2
= OSSet::withSet(set1
, set1
->getCount());
388 TEST_ASSERT('S', "3l", set2
);
392 TEST_ASSERT('S', "3m", numStrCache
== (int) set2
->getCount());
393 i
= count
= count2
= 0;
394 while ( (str
= set2
->getAnyObject()) ) {
395 count
+= set2
->__takeObject(str
);
396 count2
+= (cache
[i
++] == str
);
399 TEST_ASSERT('S', "3n", !set2
->getCount());
400 TEST_ASSERT('S', "3o", numStrCache
== count
);
401 TEST_ASSERT('S', "3p", numStrCache
== count2
);
406 res
= res
&& checkSpace("(S)3", spaceCheck
, 0);
408 // Test duplicate removal
409 spaceCheck
= checkPointSpace();
411 set1
= OSSet::withCapacity(numStrCache
);
412 TEST_ASSERT('S', "4a", set1
);
415 for (i
= 0; i
< numStrCache
; i
++) {
416 sym
= (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[i
]);
417 count
+= set1
->setObject(sym
);
420 TEST_ASSERT('S', "4b", numStrCache
!= (int) set1
->getCount());
421 TEST_ASSERT('S', "4c", count
== (int) set1
->getCount());
424 for (i
= 0; i
< numStrCache
; i
++) {
425 sym
= (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[i
]);
426 count
+= set1
->member(sym
);
427 count2
+= sym
->getRetainCount();
430 TEST_ASSERT('S', "4d", count
== numStrCache
);
431 TEST_ASSERT('S', "4e", count2
== numStrCache
* 2);
433 set2
= OSSet::withSet(set1
, 2 * set1
->getCount());
435 TEST_ASSERT('S', "4f", set2
);
437 set2
->setObject(set1
);
438 TEST_ASSERT('S', "4g", set1
->getCount() == set2
->getCount());
442 res
= res
&& checkSpace("(S)4", spaceCheck
, 0);
444 // Test array duplicate removal
445 spaceCheck
= checkPointSpace();
446 array
= OSArray::withCapacity(numStrCache
);
447 for (i
= 0; i
< numStrCache
; i
++) {
448 sym
= (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[i
]);
449 count
+= array
->setObject(sym
);
452 set1
= OSSet::withArray(array
, numStrCache
);
453 TEST_ASSERT('S', "5a", set1
);
455 TEST_ASSERT('S', "5b", array
->getCount() != set1
->getCount());
458 count
= count2
= set1
->getCount();
459 while ( (sym
= set1
->getAnyObject()) ) {
460 count
-= set1
->__takeObject(sym
);
461 count2
-= sym
->getRetainCount();
464 TEST_ASSERT('S', "5c", !count
);
465 TEST_ASSERT('S', "5d", !count2
);
468 res
= res
&& checkSpace("(S)5", spaceCheck
, 0);
471 verPrintf(("testSet: All OSSet Tests passed\n"));
473 logPrintf(("testSet: Some OSSet Tests failed\n"));
476 void testDictionary()
479 void *spaceCheck
, *spaceCheck2
, *spaceCheck3
;
480 OSObject
*cache
[numStrCache
];
482 const OSSymbol
*symCache
[numStrCache
], *sym
;
483 OSDictionary
*dict1
, *dict2
;
484 int i
, numSymbols
, count1
, count2
;
486 // Do first test without memory leak tests to initialise the metaclass
487 dict1
= OSDictionary::withCapacity(1);
488 TEST_ASSERT('D', "0a", dict1
);
492 // Grow the symbol pool to maximum
493 for (i
= 0; i
< numStrCache
; i
++)
494 symCache
[i
] = OSSymbol::withCStringNoCopy(strCache
[i
]);
495 for (i
= 0; i
< numStrCache
; i
++)
496 symCache
[i
]->release();
498 // Create and destroy a dictionary
499 spaceCheck
= checkPointSpace();
500 dict1
= OSDictionary::withCapacity(1);
501 TEST_ASSERT('D', "1a", dict1
);
503 TEST_ASSERT('D', "1b", !dict1
->getCount());
504 TEST_ASSERT('D', "1c", 1 == dict1
->getCapacity());
505 TEST_ASSERT('D', "1d", 1 == dict1
->getCapacityIncrement());
506 TEST_ASSERT('D', "1e", 4 == dict1
->setCapacityIncrement(4));
507 TEST_ASSERT('D', "1f", 4 == dict1
->getCapacityIncrement());
508 TEST_ASSERT('D', "1g", 8 == dict1
->ensureCapacity(5));
510 spaceCheck2
= checkPointSpace();
511 sym
= OSSymbol::withCStringNoCopy(strCache
[0]);
513 spaceCheck3
= checkPointSpace();
514 TEST_ASSERT('D', "1h", dict1
->setObject((OSObject
*) sym
, sym
));
515 TEST_ASSERT('D', "1i", (OSObject
*) sym
== dict1
->getObject(sym
));
517 TEST_ASSERT('D', "1i", 2 == sym
->getRetainCount());
518 res
= res
&& checkSpace("(D)1j", spaceCheck3
, 0);
520 TEST_ASSERT('D', "1k", 1 == dict1
->getCount());
521 dict1
->flushCollection();
522 TEST_ASSERT('D', "1l", !dict1
->getCount());
523 res
= res
&& checkSpace("(D)1m", spaceCheck2
, 0);
527 res
= res
&& checkSpace("(D)1", spaceCheck
, 0);
529 // Check the creation of a sizable OSDictionary from an array of IOObjects
530 // Also check indexing into the array.
531 spaceCheck
= checkPointSpace();
532 for (i
= 0, numSymbols
= 0; i
< numStrCache
; i
++) {
533 sym
= OSSymbol::withCStringNoCopy(strCache
[i
]);
534 if (1 == sym
->getRetainCount())
535 symCache
[numSymbols
++] = sym
;
539 dict1
= OSDictionary::withObjects(
540 (OSObject
**) symCache
, symCache
, numSymbols
, numSymbols
);
541 TEST_ASSERT('D', "2a", dict1
);
543 for (i
= 0; i
< numSymbols
; i
++)
544 count1
+= (symCache
[i
]->getRetainCount() == 3);
545 TEST_ASSERT('D', "2b", count1
== numSymbols
);
547 TEST_ASSERT('D', "2c", numSymbols
== (int) dict1
->getCount());
548 TEST_ASSERT('D', "2d", numSymbols
== (int) dict1
->getCapacity());
549 TEST_ASSERT('D', "2e",
550 numSymbols
== (int) dict1
->getCapacityIncrement());
552 for (i
= dict1
->getCount(); --i
>= 0; ) {
553 str
= (OSString
*) dict1
->getObject(symCache
[i
]);
554 if (str
!= (OSString
*) symCache
[i
]) {
555 verPrintf(("testDictionary(D) test 2f%d failed\n", i
));
562 for (i
= 0; i
< numSymbols
; i
++) {
563 count1
+= (symCache
[i
]->getRetainCount() == 1);
564 symCache
[i
]->release();
566 TEST_ASSERT('D', "2g", count1
== numSymbols
);
567 res
= res
&& checkSpace("(D)2", spaceCheck
, 0);
569 // Check the creation of a sizable Dictionary from an array of IOStrings
570 // Also check searching dictionary use OSString for a key.
571 spaceCheck
= checkPointSpace();
572 for (i
= 0, numSymbols
= 0; i
< numStrCache
; i
++) {
573 sym
= OSSymbol::withCStringNoCopy(strCache
[i
]);
574 if (1 == sym
->getRetainCount()) {
575 cache
[numSymbols
] = OSString::withCStringNoCopy(strCache
[i
]);
576 symCache
[numSymbols
] = sym
;
582 dict1
= OSDictionary::withObjects((OSObject
**) symCache
,
584 numSymbols
, numSymbols
);
585 TEST_ASSERT('D', "3a", dict1
);
587 for (i
= 0; i
< numSymbols
; i
++) {
588 count1
+= (symCache
[i
]->getRetainCount() == 3);
589 count2
+= (cache
[i
]->getRetainCount() == 1);
591 TEST_ASSERT('D', "3b", count1
== numSymbols
);
592 TEST_ASSERT('D', "3c", count2
== numSymbols
);
595 for (i
= 0; i
< numSymbols
; i
++) {
596 str
= (OSString
*) cache
[i
];
597 count1
+= (symCache
[i
] == (const OSSymbol
*) dict1
->getObject(str
));
598 count2
+= (symCache
[i
]->getRetainCount() == 3);
600 TEST_ASSERT('D', "3d", count1
== numSymbols
);
601 TEST_ASSERT('D', "3e", count2
== numSymbols
);
604 for (i
= 0; i
< numSymbols
; i
++) {
605 const char *cStr
= ((OSString
*) cache
[i
])->getCStringNoCopy();
607 count1
+= (symCache
[i
] == (const OSSymbol
*) dict1
->getObject(cStr
));
608 count2
+= (symCache
[i
]->getRetainCount() == 3);
610 TEST_ASSERT('D', "3f", count1
== numSymbols
);
611 TEST_ASSERT('D', "3g", count2
== numSymbols
);
616 for (i
= 0; i
< numSymbols
; i
++) {
617 count1
+= (symCache
[i
]->getRetainCount() == 1);
618 count2
+= (cache
[i
]->getRetainCount() == 1);
619 symCache
[i
]->release();
622 TEST_ASSERT('D', "3h", count1
== numSymbols
);
623 res
= res
&& checkSpace("(D)3", spaceCheck
, 0);
625 // Check the creation of a small dictionary then grow it one item at a time
626 // Create a new dictionary from the old dictionary.
627 // Finally remove each item permanently.
628 spaceCheck
= checkPointSpace();
629 for (i
= 0, numSymbols
= 0; i
< numStrCache
; i
++) {
630 sym
= OSSymbol::withCStringNoCopy(strCache
[i
]);
631 if (1 == sym
->getRetainCount()) {
632 cache
[numSymbols
] = OSString::withCStringNoCopy(strCache
[i
]);
633 symCache
[numSymbols
] = sym
;
640 dict1
= OSDictionary::withCapacity(1);
641 TEST_ASSERT('D', "4a", dict1
);
644 for (i
= 0; i
< numSymbols
; i
++) {
646 count1
+= ((OSObject
*) sym
== dict1
->setObject((OSObject
*) sym
,
647 sym
->getCStringNoCopy()));
648 count2
+= (sym
->getRetainCount() == 3);
650 TEST_ASSERT('D', "4b", numSymbols
== (int) dict1
->getCount());
651 TEST_ASSERT('D', "4c", numSymbols
== count1
);
652 TEST_ASSERT('D', "4d", numSymbols
== count2
);
654 dict2
= OSDictionary::withDictionary(dict1
, numSymbols
-1);
655 TEST_ASSERT('D', "4b", !dict2
);
656 dict2
= OSDictionary::withDictionary(dict1
, numSymbols
);
658 TEST_ASSERT('D', "4e", dict2
);
660 dict1
->release(); dict1
= 0;
662 TEST_ASSERT('D', "4f", numSymbols
== (int) dict2
->getCount());
665 for (i
= 0; i
< numSymbols
; i
++) {
666 OSObject
*replacedObject
;
669 str
= (OSString
*) cache
[i
];
670 replacedObject
= dict2
->setObject(str
, str
);
671 count1
+= ((OSString
*) sym
== replacedObject
);
672 replacedObject
->release();
673 count2
+= (sym
->getRetainCount() == 2);
676 TEST_ASSERT('D', "4g", numSymbols
== count1
);
677 TEST_ASSERT('D', "4h", numSymbols
== count2
);
680 for (i
= 0; i
< numSymbols
; i
++) {
682 str
= (OSString
*) cache
[i
];
683 count1
+= (str
== dict2
->__takeObject(sym
));
685 count2
+= (sym
->getRetainCount() == 1);
688 TEST_ASSERT('D', "4i", numSymbols
== count1
);
689 TEST_ASSERT('D', "4j", numSymbols
== count2
);
690 TEST_ASSERT('D', "4k", !dict2
->getCount());
691 dict2
->release(); dict2
= 0;
695 res
= res
&& checkSpace("(D)4", spaceCheck
, 0);
698 verPrintf(("testDictionary: All OSDictionary Tests passed\n"));
700 logPrintf(("testDictionary: Some OSDictionary Tests failed\n"));
707 OSObject
*cache
[numStrCache
];
709 const OSSymbol
*symCache
[numStrCache
], *sym
;
712 OSArray
*array
, *bigReturn
;
713 OSCollectionIterator
*iter1
, *iter2
;
714 int i
, numSymbols
, count1
, count2
, count3
;
716 // Setup symbol and string pools
717 for (i
= 0, numSymbols
= 0; i
< numStrCache
; i
++) {
718 sym
= OSSymbol::withCStringNoCopy(strCache
[i
]);
719 if (1 == sym
->getRetainCount()) {
720 cache
[numSymbols
] = OSString::withCStringNoCopy(strCache
[i
]);
721 symCache
[numSymbols
] = sym
;
728 // Test the array iterator
729 spaceCheck
= checkPointSpace();
731 array
= OSArray::withCapacity(numSymbols
);
732 TEST_ASSERT('I', "1a", array
);
735 for (i
= numSymbols
; --i
>= 0; )
736 count1
+= array
->setObject(cache
[i
], 0);
737 TEST_ASSERT('I', "1b", count1
== numSymbols
);
739 iter1
= OSCollectionIterator::withCollection(array
);
740 iter2
= OSCollectionIterator::withCollection(array
);
742 TEST_ASSERT('I', "1c", iter1
);
743 TEST_ASSERT('I', "1d", iter2
);
744 if (iter1
&& iter2
) {
745 count1
= count2
= count3
= 0;
746 for (i
= 0; (str
= (IOString
*) iter1
->getNextObject()); i
++) {
747 bigReturn
= iter2
->nextEntries();
748 count1
+= (bigReturn
->getCount() == 1);
749 count2
+= (cache
[i
] == bigReturn
->getObject(0));
750 count3
+= (cache
[i
] == str
);
752 TEST_ASSERT('I', "1e", count1
== numSymbols
);
753 TEST_ASSERT('I', "1f", count2
== numSymbols
);
754 TEST_ASSERT('I', "1g", count3
== numSymbols
);
755 TEST_ASSERT('I', "1h", iter1
->valid());
756 TEST_ASSERT('I', "1i", iter2
->valid());
759 str
= (OSString
*) array
->__takeObject(0);
760 array
->setObject(str
, 0);
762 TEST_ASSERT('I', "1j", !iter1
->getNextObject());
763 TEST_ASSERT('I', "1k", !iter1
->valid());
766 count1
= count2
= count3
= 0;
769 str
= (OSString
*) iter1
->getNextObject();
770 else if ( (bigReturn
= iter1
->nextEntries()) )
771 str
= (OSString
*) bigReturn
->getObject(0);
777 count1
+= (cache
[i
] == str
);
779 TEST_ASSERT('I', "1l", count1
== numSymbols
);
780 TEST_ASSERT('I', "1m", i
== numSymbols
);
781 TEST_ASSERT('I', "1n", iter1
->valid());
783 TEST_ASSERT('I', "1o", 3 == array
->getRetainCount());
787 if (iter1
) iter1
->release();
788 if (iter2
) iter2
->release();
789 res
= res
&& checkSpace("(I)1", spaceCheck
, 0);
791 // Test the set iterator
792 spaceCheck
= checkPointSpace();
794 set
= OSSet::withCapacity(numSymbols
);
795 TEST_ASSERT('I', "2a", set
);
798 for (i
= 0; i
< numSymbols
; i
++)
799 count1
+= set
->setObject(cache
[i
]);
800 TEST_ASSERT('I', "2b", count1
== numSymbols
);
802 iter1
= OSCollectionIterator::withCollection(set
);
803 iter2
= OSCollectionIterator::withCollection(set
);
805 TEST_ASSERT('I', "2c", iter1
);
806 TEST_ASSERT('I', "2d", iter2
);
807 if (iter1
&& iter2
) {
808 count1
= count2
= count3
= 0;
809 for (i
= 0; (str
= (IOString
*) iter1
->getNextObject()); i
++) {
810 bigReturn
= iter2
->nextEntries();
811 count1
+= (bigReturn
->getCount() == 1);
812 count2
+= (cache
[i
] == bigReturn
->getObject(0));
813 count3
+= (cache
[i
] == str
);
815 TEST_ASSERT('I', "2e", count1
== numSymbols
);
816 TEST_ASSERT('I', "2f", count2
== numSymbols
);
817 TEST_ASSERT('I', "2g", count3
== numSymbols
);
818 TEST_ASSERT('I', "2h", iter1
->valid());
819 TEST_ASSERT('I', "2i", iter2
->valid());
822 count1
= count2
= count3
= 0;
825 str
= (OSString
*) iter1
->getNextObject();
826 else if ( (bigReturn
= iter1
->nextEntries()) )
827 str
= (OSString
*) bigReturn
->getObject(0);
833 count1
+= (cache
[i
] == str
);
835 TEST_ASSERT('I', "2l", count1
== numSymbols
);
836 TEST_ASSERT('I', "2m", i
== numSymbols
);
837 TEST_ASSERT('I', "2n", iter1
->valid());
840 str
= (OSString
*) set
->getAnyObject();
841 (void) set
->__takeObject(str
);
844 TEST_ASSERT('I', "2j", !iter1
->getNextObject());
845 TEST_ASSERT('I', "2k", !iter1
->valid());
847 TEST_ASSERT('I', "2o", 3 == set
->getRetainCount());
851 if (iter1
) iter1
->release();
852 if (iter2
) iter2
->release();
853 res
= res
&& checkSpace("(I)2", spaceCheck
, 0);
855 // Test the dictionary iterator
856 spaceCheck
= checkPointSpace();
858 dict
= OSDictionary::withCapacity(numSymbols
);
859 TEST_ASSERT('I', "3a", dict
);
862 for (i
= 0; i
< numSymbols
; i
++)
863 count1
+= (0 != dict
->setObject(cache
[i
], symCache
[i
]));
864 TEST_ASSERT('I', "3b", count1
== numSymbols
);
866 iter1
= OSCollectionIterator::withCollection(dict
);
867 iter2
= OSCollectionIterator::withCollection(dict
);
869 TEST_ASSERT('I', "3c", iter1
);
870 TEST_ASSERT('I', "3d", iter2
);
871 if (iter1
&& iter2
) {
872 count1
= count2
= count3
= 0;
873 for (i
= 0; (sym
= (const IOSymbol
*) iter1
->getNextObject()); i
++) {
874 bigReturn
= iter2
->nextEntries();
875 count1
+= (bigReturn
->getCount() == 2);
876 count2
+= (cache
[i
] == bigReturn
->getObject(1));
877 count3
+= (symCache
[i
] == sym
);
879 TEST_ASSERT('I', "3e", count1
== numSymbols
);
880 TEST_ASSERT('I', "3f", count2
== numSymbols
);
881 TEST_ASSERT('I', "3g", count3
== numSymbols
);
882 TEST_ASSERT('I', "3h", iter1
->valid());
883 TEST_ASSERT('I', "3i", iter2
->valid());
886 count1
= count2
= count3
= 0;
890 sym
= (const OSSymbol
*) iter1
->getNextObject();
893 else if ( (bigReturn
= iter1
->nextEntries()) ) {
894 sym
= (const OSSymbol
*) bigReturn
->getObject(0);
895 str
= (OSString
*) bigReturn
->getObject(1);
903 count1
+= (symCache
[i
] == sym
);
904 count2
+= (!str
|| cache
[i
] == str
);
906 TEST_ASSERT('I', "3l", count1
== numSymbols
);
907 TEST_ASSERT('I', "3m", count2
== numSymbols
);
908 TEST_ASSERT('I', "3n", i
== numSymbols
);
909 TEST_ASSERT('I', "3o", iter1
->valid());
912 str
= (OSString
*) dict
->__takeObject(symCache
[numSymbols
-1]);
913 dict
->setObject(str
, symCache
[numSymbols
-1]);
915 TEST_ASSERT('I', "3j", !iter1
->getNextObject());
916 TEST_ASSERT('I', "3k", !iter1
->valid());
918 TEST_ASSERT('I', "3p", 3 == dict
->getRetainCount());
922 if (iter1
) iter1
->release();
923 if (iter2
) iter2
->release();
924 res
= res
&& checkSpace("(I)3", spaceCheck
, 0);
926 count1
= count2
= count3
= 0;
927 for (i
= 0; i
< numSymbols
; i
++) {
928 count1
+= (1 == cache
[i
]->getRetainCount());
929 count2
+= (1 == symCache
[i
]->getRetainCount());
931 symCache
[i
]->release();
933 TEST_ASSERT('I', "4a", count1
== numSymbols
);
934 TEST_ASSERT('I', "4b", count2
== numSymbols
);
937 verPrintf(("testIterator: All OSCollectionIterator Tests passed\n"));
939 logPrintf(("testIterator: Some OSCollectionIterator Tests failed\n"));