]>
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 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
28 #include <libkern/c++/OSArray.h>
29 #include <libkern/c++/OSSet.h>
30 #include <libkern/c++/OSDictionary.h>
31 #include <libkern/c++/OSString.h>
32 #include <libkern/c++/OSSymbol.h>
33 #include <libkern/c++/OSCollectionIterator.h>
38 void *spaceCheck
, *spaceCheck2
, *spaceCheck3
;
39 int i
, j
, count
, count2
;
40 OSObject
*cache
[numStrCache
], *str
, *sym
;
41 OSArray
*array1
, *array2
;
43 // Do first test without memory leak tests to initialise the metaclass
44 array1
= OSArray::withCapacity(1);
45 TEST_ASSERT('A', "0a", array1
);
49 // Grow the symbol pool to maximum
50 for (i
= 0; i
< numStrCache
; i
++)
51 cache
[i
] = (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[i
]);
52 for (i
= 0; i
< numStrCache
; i
++)
55 // Create and destroy an array
56 spaceCheck
= checkPointSpace();
57 array1
= OSArray::withCapacity(1);
58 TEST_ASSERT('A', "1a", array1
);
60 TEST_ASSERT('A', "1b", !array1
->getCount());
61 TEST_ASSERT('A', "1c", 1 == array1
->getCapacity());
62 TEST_ASSERT('A', "1d", 1 == array1
->getCapacityIncrement());
63 TEST_ASSERT('A', "1e", 4 == array1
->setCapacityIncrement(4));
64 TEST_ASSERT('A', "1f", 4 == array1
->getCapacityIncrement());
65 TEST_ASSERT('A', "1g", 8 == array1
->ensureCapacity(5));
67 spaceCheck2
= checkPointSpace();
68 cache
[0] = IOString::withCStringNoCopy(strCache
[0]);
70 spaceCheck3
= checkPointSpace();
71 TEST_ASSERT('A', "1h", array1
->setObject(cache
[0]));
72 TEST_ASSERT('A', "1i", cache
[0] == array1
->getObject(0));
74 res
= res
&& checkSpace("(A)1j", spaceCheck3
, 0);
76 TEST_ASSERT('A', "1k", 1 == array1
->getCount());
77 array1
->flushCollection();
78 TEST_ASSERT('A', "1l", !array1
->getCount());
79 res
= res
&& checkSpace("(A)1m", spaceCheck2
, 0);
83 res
= res
&& checkSpace("(A)1", spaceCheck
, 0);
85 // Check the creation of a sizable OSArray from an array of IOObjects
86 // Also check indexing into the array.
87 spaceCheck
= checkPointSpace();
88 for (i
= 0; i
< numStrCache
; i
++)
89 cache
[i
] = OSString::withCStringNoCopy(strCache
[i
]);
90 array1
= OSArray::withObjects(cache
, numStrCache
, numStrCache
);
91 TEST_ASSERT('A', "2a", array1
);
92 for (i
= 0; i
< numStrCache
; i
++)
95 TEST_ASSERT('A', "2b", numStrCache
== (int) array1
->getCount());
96 TEST_ASSERT('A', "2c", numStrCache
== (int) array1
->getCapacity());
97 TEST_ASSERT('A', "2d",
98 numStrCache
== (int) array1
->getCapacityIncrement());
100 for (i
= 0; (str
= array1
->getObject(i
)); i
++) {
101 if (str
!= cache
[i
]) {
102 verPrintf(("testArray(A) test 2e%d failed\n", i
));
106 TEST_ASSERT('A', "2f", numStrCache
== i
);
109 res
= res
&& checkSpace("(A)2", spaceCheck
, 0);
111 // Test array creation from another array by both the setObject method
112 // and the withArray factory. And test __takeObject code first
113 // with tail removal then with head removal
114 spaceCheck
= checkPointSpace();
115 for (i
= 0; i
< numStrCache
; i
++)
116 cache
[i
] = OSString::withCStringNoCopy(strCache
[i
]);
117 array1
= OSArray::withObjects(cache
, numStrCache
, numStrCache
);
118 TEST_ASSERT('A', "3a", array1
);
119 for (i
= 0; i
< numStrCache
; i
++)
123 array2
= OSArray::withCapacity(1);
124 TEST_ASSERT('A', "3b", array2
);
125 TEST_ASSERT('A', "3c", !array2
->getCount());
126 TEST_ASSERT('A', "3d", array2
->setObject(array1
));
127 TEST_ASSERT('A', "3e", array1
->getCount() == array2
->getCount());
131 TEST_ASSERT('A', "3f", numStrCache
== (int) array2
->getCount());
132 for (i
= array2
->getCount(); (str
= array2
->__takeObject(--i
)); ) {
133 if (str
!= cache
[i
]) {
134 verPrintf(("testArray(A) test 3g%d failed\n", i
));
137 count
+= ((int) array2
->getCount() == i
);
140 TEST_ASSERT('A', "3h", count
== numStrCache
);
141 TEST_ASSERT('A', "3i", -1 == i
);
142 TEST_ASSERT('A', "3j", !array2
->getCount());
144 spaceCheck2
= checkPointSpace();
145 array2
->flushCollection();
146 res
= res
&& checkSpace("(A)3k", spaceCheck2
, 0);
152 array2
= OSArray::withArray(array1
, numStrCache
- 1);
153 TEST_ASSERT('A', "3l", !array2
);
154 array2
= OSArray::withArray(array1
, array1
->getCount());
155 TEST_ASSERT('A', "3m", array2
);
160 TEST_ASSERT('A', "3o", numStrCache
== (int) array2
->getCount());
161 for (i
= 0; (str
= array2
->__takeObject(0)); i
++) {
162 count
+= (str
== cache
[i
]);
165 TEST_ASSERT('A', "3p", count
== numStrCache
);
166 TEST_ASSERT('A', "3q", !array2
->getCount());
170 res
= res
&& checkSpace("(A)3", spaceCheck
, 0);
172 // Test object replacement from one array to another
173 spaceCheck
= checkPointSpace();
174 array1
= OSArray::withCapacity(numStrCache
);
175 TEST_ASSERT('A', "4a", array1
);
178 for (i
= 0; i
< numStrCache
; i
++) {
179 str
= OSString::withCStringNoCopy(strCache
[i
]);
180 count
+= array1
->setObject(str
);
181 count2
+= (str
== array1
->lastObject());
184 TEST_ASSERT('A', "4b", numStrCache
== (int) array1
->getCount());
185 TEST_ASSERT('A', "4c", count
== numStrCache
);
186 TEST_ASSERT('A', "4d", count2
== numStrCache
);
188 array2
= OSArray::withCapacity(1);
189 TEST_ASSERT('A', "4e", array2
);
192 str
= (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[0]);
193 for (i
= 0; i
< numStrCache
; i
++) {
194 sym
= (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[i
]);
195 count
+= array2
->setObject(sym
, 0);
196 count2
+= (str
== array2
->lastObject());
200 TEST_ASSERT('A', "4f", numStrCache
== (int) array2
->getCount());
201 TEST_ASSERT('A', "4g", count
== numStrCache
);
202 TEST_ASSERT('A', "4h", count2
== numStrCache
);
204 if (array1
&& array2
) {
207 for (i
= array1
->getCount() - 1; (sym
= array2
->__takeObject(0)); i
--) {
208 str
= array1
->replaceObject(sym
, i
);
210 count2
+= (sym
!= str
);
216 TEST_ASSERT('A', "4k", numStrCache
== (int) array1
->getCount());
217 TEST_ASSERT('A', "4l", count
== numStrCache
);
218 TEST_ASSERT('A', "4m", count2
== numStrCache
);
223 if (array1
) array1
->release();
224 if (array2
) array2
->release();
226 res
= res
&& checkSpace("(A)4", spaceCheck
, 0);
228 // Test array duplicate removal
229 spaceCheck
= checkPointSpace();
230 array1
= OSArray::withCapacity(numStrCache
);
231 TEST_ASSERT('A', "5a", array1
);
233 for (i
= 0; i
< numStrCache
; i
++) {
234 sym
= (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[i
]);
235 count
+= array1
->setObject(sym
);
238 TEST_ASSERT('A', "5b", numStrCache
== (int) array1
->getCount());
241 for (i
= 0; (sym
= array1
->getObject(i
)); )
242 if (sym
->getRetainCount() == 1)
245 //sym = array1->__takeObject(i);
247 array1
->removeObject(i
);
249 TEST_ASSERT('A', "5c", numStrCache
!= (int) array1
->getCount());
251 // check to see that all symbols are really there
252 for (count
= 0, i
= 0; i
< numStrCache
; i
++) {
253 sym
= (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[i
]);
254 for (count2
= false, j
= 0; (str
= array1
->getObject(j
)); j
++)
262 TEST_ASSERT('A', "5c", count
== numStrCache
);
265 res
= res
&& checkSpace("(S)5", spaceCheck
, 0);
268 verPrintf(("testArray: All OSArray Tests passed\n"));
270 logPrintf(("testArray: Some OSArray Tests failed\n"));
276 void *spaceCheck
, *spaceCheck2
, *spaceCheck3
;
277 int i
, count
, count2
;
278 OSObject
*cache
[numStrCache
], *str
, *sym
;
282 // Do first test without memory leak tests to initialise the metaclass
283 set1
= OSSet::withCapacity(1);
284 TEST_ASSERT('S', "0a", set1
);
288 // Grow the symbol pool to maximum
289 for (i
= 0; i
< numStrCache
; i
++)
290 cache
[i
] = (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[i
]);
291 for (i
= 0; i
< numStrCache
; i
++)
294 // Create and destroy an set
295 spaceCheck
= checkPointSpace();
296 set1
= OSSet::withCapacity(1);
297 TEST_ASSERT('S', "1a", set1
);
299 TEST_ASSERT('S', "1b", !set1
->getCount());
300 TEST_ASSERT('S', "1c", 1 == set1
->getCapacity());
301 TEST_ASSERT('S', "1d", 1 == set1
->getCapacityIncrement());
302 TEST_ASSERT('S', "1e", 4 == set1
->setCapacityIncrement(4));
303 TEST_ASSERT('S', "1f", 4 == set1
->getCapacityIncrement());
304 TEST_ASSERT('S', "1g", 8 == set1
->ensureCapacity(5));
306 spaceCheck2
= checkPointSpace();
307 cache
[0] = IOString::withCStringNoCopy(strCache
[0]);
309 spaceCheck3
= checkPointSpace();
310 TEST_ASSERT('S', "1h", set1
->setObject(cache
[0]));
311 TEST_ASSERT('S', "1i", set1
->containsObject(cache
[0]));
312 TEST_ASSERT('S', "1j", cache
[0] == set1
->getAnyObject());
314 res
= res
&& checkSpace("(S)1k", spaceCheck3
, 0);
316 TEST_ASSERT('S', "1l", 1 == set1
->getCount());
317 set1
->flushCollection();
318 TEST_ASSERT('S', "1m", !set1
->getCount());
319 res
= res
&& checkSpace("(S)1n", spaceCheck2
, 0);
323 res
= res
&& checkSpace("(S)1", spaceCheck
, 0);
325 // Check the creation of a sizable OSSet from an set of IOObjects
326 // Also check member test of set.
327 spaceCheck
= checkPointSpace();
328 for (i
= 0; i
< numStrCache
; i
++)
329 cache
[i
] = OSString::withCStringNoCopy(strCache
[i
]);
330 set1
= OSSet::withObjects(cache
, numStrCache
, numStrCache
);
331 TEST_ASSERT('S', "2a", set1
);
332 for (i
= 0; i
< numStrCache
; i
++)
335 TEST_ASSERT('S', "2b", numStrCache
== (int) set1
->getCount());
336 TEST_ASSERT('S', "2c", numStrCache
== (int) set1
->getCapacity());
337 TEST_ASSERT('S', "2d",
338 numStrCache
== (int) set1
->getCapacityIncrement());
341 for (i
= set1
->getCount(); --i
>= 0; )
342 count
+= set1
->member(cache
[i
]);
344 TEST_ASSERT('S', "2e", numStrCache
== count
);
347 res
= res
&& checkSpace("(S)2", spaceCheck
, 0);
349 // Test set creation from another set by both the setObject method
350 // and the withArray factory. And test __takeObject code first
351 // with tail removal then with head removal
352 spaceCheck
= checkPointSpace();
353 for (i
= 0; i
< numStrCache
; i
++)
354 cache
[i
] = OSString::withCStringNoCopy(strCache
[i
]);
355 set1
= OSSet::withObjects(cache
, numStrCache
, numStrCache
);
356 TEST_ASSERT('S', "3a", set1
);
357 for (i
= 0; i
< numStrCache
; i
++)
361 set2
= OSSet::withCapacity(set1
->getCount());
362 TEST_ASSERT('S', "3b", set2
);
363 TEST_ASSERT('S', "3c", !set2
->getCount());
364 TEST_ASSERT('S', "3d", set2
->setObject(set1
));
365 TEST_ASSERT('S', "3e", set1
->getCount() == set2
->getCount());
368 TEST_ASSERT('S', "3f", numStrCache
== (int) set2
->getCount());
370 while ( (str
= set2
->getAnyObject()) ) {
371 count
+= set2
->__takeObject(str
);
372 count2
+= set1
->member(str
);
375 TEST_ASSERT('S', "3g", !set2
->getCount());
376 TEST_ASSERT('S', "3h", numStrCache
== count
);
377 TEST_ASSERT('S', "3i", numStrCache
== count2
);
379 spaceCheck2
= checkPointSpace();
380 set2
->flushCollection();
381 res
= res
&& checkSpace("(S)3j", spaceCheck2
, 0);
387 set2
= OSSet::withSet(set1
, numStrCache
- 1);
388 TEST_ASSERT('S', "3k", !set2
);
389 set2
= OSSet::withSet(set1
, set1
->getCount());
390 TEST_ASSERT('S', "3l", set2
);
394 TEST_ASSERT('S', "3m", numStrCache
== (int) set2
->getCount());
395 i
= count
= count2
= 0;
396 while ( (str
= set2
->getAnyObject()) ) {
397 count
+= set2
->__takeObject(str
);
398 count2
+= (cache
[i
++] == str
);
401 TEST_ASSERT('S', "3n", !set2
->getCount());
402 TEST_ASSERT('S', "3o", numStrCache
== count
);
403 TEST_ASSERT('S', "3p", numStrCache
== count2
);
408 res
= res
&& checkSpace("(S)3", spaceCheck
, 0);
410 // Test duplicate removal
411 spaceCheck
= checkPointSpace();
413 set1
= OSSet::withCapacity(numStrCache
);
414 TEST_ASSERT('S', "4a", set1
);
417 for (i
= 0; i
< numStrCache
; i
++) {
418 sym
= (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[i
]);
419 count
+= set1
->setObject(sym
);
422 TEST_ASSERT('S', "4b", numStrCache
!= (int) set1
->getCount());
423 TEST_ASSERT('S', "4c", count
== (int) set1
->getCount());
426 for (i
= 0; i
< numStrCache
; i
++) {
427 sym
= (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[i
]);
428 count
+= set1
->member(sym
);
429 count2
+= sym
->getRetainCount();
432 TEST_ASSERT('S', "4d", count
== numStrCache
);
433 TEST_ASSERT('S', "4e", count2
== numStrCache
* 2);
435 set2
= OSSet::withSet(set1
, 2 * set1
->getCount());
437 TEST_ASSERT('S', "4f", set2
);
439 set2
->setObject(set1
);
440 TEST_ASSERT('S', "4g", set1
->getCount() == set2
->getCount());
444 res
= res
&& checkSpace("(S)4", spaceCheck
, 0);
446 // Test array duplicate removal
447 spaceCheck
= checkPointSpace();
448 array
= OSArray::withCapacity(numStrCache
);
449 for (i
= 0; i
< numStrCache
; i
++) {
450 sym
= (OSObject
*) OSSymbol::withCStringNoCopy(strCache
[i
]);
451 count
+= array
->setObject(sym
);
454 set1
= OSSet::withArray(array
, numStrCache
);
455 TEST_ASSERT('S', "5a", set1
);
457 TEST_ASSERT('S', "5b", array
->getCount() != set1
->getCount());
460 count
= count2
= set1
->getCount();
461 while ( (sym
= set1
->getAnyObject()) ) {
462 count
-= set1
->__takeObject(sym
);
463 count2
-= sym
->getRetainCount();
466 TEST_ASSERT('S', "5c", !count
);
467 TEST_ASSERT('S', "5d", !count2
);
470 res
= res
&& checkSpace("(S)5", spaceCheck
, 0);
473 verPrintf(("testSet: All OSSet Tests passed\n"));
475 logPrintf(("testSet: Some OSSet Tests failed\n"));
478 void testDictionary()
481 void *spaceCheck
, *spaceCheck2
, *spaceCheck3
;
482 OSObject
*cache
[numStrCache
];
484 const OSSymbol
*symCache
[numStrCache
], *sym
;
485 OSDictionary
*dict1
, *dict2
;
486 int i
, numSymbols
, count1
, count2
;
488 // Do first test without memory leak tests to initialise the metaclass
489 dict1
= OSDictionary::withCapacity(1);
490 TEST_ASSERT('D', "0a", dict1
);
494 // Grow the symbol pool to maximum
495 for (i
= 0; i
< numStrCache
; i
++)
496 symCache
[i
] = OSSymbol::withCStringNoCopy(strCache
[i
]);
497 for (i
= 0; i
< numStrCache
; i
++)
498 symCache
[i
]->release();
500 // Create and destroy a dictionary
501 spaceCheck
= checkPointSpace();
502 dict1
= OSDictionary::withCapacity(1);
503 TEST_ASSERT('D', "1a", dict1
);
505 TEST_ASSERT('D', "1b", !dict1
->getCount());
506 TEST_ASSERT('D', "1c", 1 == dict1
->getCapacity());
507 TEST_ASSERT('D', "1d", 1 == dict1
->getCapacityIncrement());
508 TEST_ASSERT('D', "1e", 4 == dict1
->setCapacityIncrement(4));
509 TEST_ASSERT('D', "1f", 4 == dict1
->getCapacityIncrement());
510 TEST_ASSERT('D', "1g", 8 == dict1
->ensureCapacity(5));
512 spaceCheck2
= checkPointSpace();
513 sym
= OSSymbol::withCStringNoCopy(strCache
[0]);
515 spaceCheck3
= checkPointSpace();
516 TEST_ASSERT('D', "1h", dict1
->setObject((OSObject
*) sym
, sym
));
517 TEST_ASSERT('D', "1i", (OSObject
*) sym
== dict1
->getObject(sym
));
519 TEST_ASSERT('D', "1i", 2 == sym
->getRetainCount());
520 res
= res
&& checkSpace("(D)1j", spaceCheck3
, 0);
522 TEST_ASSERT('D', "1k", 1 == dict1
->getCount());
523 dict1
->flushCollection();
524 TEST_ASSERT('D', "1l", !dict1
->getCount());
525 res
= res
&& checkSpace("(D)1m", spaceCheck2
, 0);
529 res
= res
&& checkSpace("(D)1", spaceCheck
, 0);
531 // Check the creation of a sizable OSDictionary from an array of IOObjects
532 // Also check indexing into the array.
533 spaceCheck
= checkPointSpace();
534 for (i
= 0, numSymbols
= 0; i
< numStrCache
; i
++) {
535 sym
= OSSymbol::withCStringNoCopy(strCache
[i
]);
536 if (1 == sym
->getRetainCount())
537 symCache
[numSymbols
++] = sym
;
541 dict1
= OSDictionary::withObjects(
542 (OSObject
**) symCache
, symCache
, numSymbols
, numSymbols
);
543 TEST_ASSERT('D', "2a", dict1
);
545 for (i
= 0; i
< numSymbols
; i
++)
546 count1
+= (symCache
[i
]->getRetainCount() == 3);
547 TEST_ASSERT('D', "2b", count1
== numSymbols
);
549 TEST_ASSERT('D', "2c", numSymbols
== (int) dict1
->getCount());
550 TEST_ASSERT('D', "2d", numSymbols
== (int) dict1
->getCapacity());
551 TEST_ASSERT('D', "2e",
552 numSymbols
== (int) dict1
->getCapacityIncrement());
554 for (i
= dict1
->getCount(); --i
>= 0; ) {
555 str
= (OSString
*) dict1
->getObject(symCache
[i
]);
556 if (str
!= (OSString
*) symCache
[i
]) {
557 verPrintf(("testDictionary(D) test 2f%d failed\n", i
));
564 for (i
= 0; i
< numSymbols
; i
++) {
565 count1
+= (symCache
[i
]->getRetainCount() == 1);
566 symCache
[i
]->release();
568 TEST_ASSERT('D', "2g", count1
== numSymbols
);
569 res
= res
&& checkSpace("(D)2", spaceCheck
, 0);
571 // Check the creation of a sizable Dictionary from an array of IOStrings
572 // Also check searching dictionary use OSString for a key.
573 spaceCheck
= checkPointSpace();
574 for (i
= 0, numSymbols
= 0; i
< numStrCache
; i
++) {
575 sym
= OSSymbol::withCStringNoCopy(strCache
[i
]);
576 if (1 == sym
->getRetainCount()) {
577 cache
[numSymbols
] = OSString::withCStringNoCopy(strCache
[i
]);
578 symCache
[numSymbols
] = sym
;
584 dict1
= OSDictionary::withObjects((OSObject
**) symCache
,
586 numSymbols
, numSymbols
);
587 TEST_ASSERT('D', "3a", dict1
);
589 for (i
= 0; i
< numSymbols
; i
++) {
590 count1
+= (symCache
[i
]->getRetainCount() == 3);
591 count2
+= (cache
[i
]->getRetainCount() == 1);
593 TEST_ASSERT('D', "3b", count1
== numSymbols
);
594 TEST_ASSERT('D', "3c", count2
== numSymbols
);
597 for (i
= 0; i
< numSymbols
; i
++) {
598 str
= (OSString
*) cache
[i
];
599 count1
+= (symCache
[i
] == (const OSSymbol
*) dict1
->getObject(str
));
600 count2
+= (symCache
[i
]->getRetainCount() == 3);
602 TEST_ASSERT('D', "3d", count1
== numSymbols
);
603 TEST_ASSERT('D', "3e", count2
== numSymbols
);
606 for (i
= 0; i
< numSymbols
; i
++) {
607 const char *cStr
= ((OSString
*) cache
[i
])->getCStringNoCopy();
609 count1
+= (symCache
[i
] == (const OSSymbol
*) dict1
->getObject(cStr
));
610 count2
+= (symCache
[i
]->getRetainCount() == 3);
612 TEST_ASSERT('D', "3f", count1
== numSymbols
);
613 TEST_ASSERT('D', "3g", count2
== numSymbols
);
618 for (i
= 0; i
< numSymbols
; i
++) {
619 count1
+= (symCache
[i
]->getRetainCount() == 1);
620 count2
+= (cache
[i
]->getRetainCount() == 1);
621 symCache
[i
]->release();
624 TEST_ASSERT('D', "3h", count1
== numSymbols
);
625 res
= res
&& checkSpace("(D)3", spaceCheck
, 0);
627 // Check the creation of a small dictionary then grow it one item at a time
628 // Create a new dictionary from the old dictionary.
629 // Finally remove each item permanently.
630 spaceCheck
= checkPointSpace();
631 for (i
= 0, numSymbols
= 0; i
< numStrCache
; i
++) {
632 sym
= OSSymbol::withCStringNoCopy(strCache
[i
]);
633 if (1 == sym
->getRetainCount()) {
634 cache
[numSymbols
] = OSString::withCStringNoCopy(strCache
[i
]);
635 symCache
[numSymbols
] = sym
;
642 dict1
= OSDictionary::withCapacity(1);
643 TEST_ASSERT('D', "4a", dict1
);
646 for (i
= 0; i
< numSymbols
; i
++) {
648 count1
+= ((OSObject
*) sym
== dict1
->setObject((OSObject
*) sym
,
649 sym
->getCStringNoCopy()));
650 count2
+= (sym
->getRetainCount() == 3);
652 TEST_ASSERT('D', "4b", numSymbols
== (int) dict1
->getCount());
653 TEST_ASSERT('D', "4c", numSymbols
== count1
);
654 TEST_ASSERT('D', "4d", numSymbols
== count2
);
656 dict2
= OSDictionary::withDictionary(dict1
, numSymbols
-1);
657 TEST_ASSERT('D', "4b", !dict2
);
658 dict2
= OSDictionary::withDictionary(dict1
, numSymbols
);
660 TEST_ASSERT('D', "4e", dict2
);
662 dict1
->release(); dict1
= 0;
664 TEST_ASSERT('D', "4f", numSymbols
== (int) dict2
->getCount());
667 for (i
= 0; i
< numSymbols
; i
++) {
668 OSObject
*replacedObject
;
671 str
= (OSString
*) cache
[i
];
672 replacedObject
= dict2
->setObject(str
, str
);
673 count1
+= ((OSString
*) sym
== replacedObject
);
674 replacedObject
->release();
675 count2
+= (sym
->getRetainCount() == 2);
678 TEST_ASSERT('D', "4g", numSymbols
== count1
);
679 TEST_ASSERT('D', "4h", numSymbols
== count2
);
682 for (i
= 0; i
< numSymbols
; i
++) {
684 str
= (OSString
*) cache
[i
];
685 count1
+= (str
== dict2
->__takeObject(sym
));
687 count2
+= (sym
->getRetainCount() == 1);
690 TEST_ASSERT('D', "4i", numSymbols
== count1
);
691 TEST_ASSERT('D', "4j", numSymbols
== count2
);
692 TEST_ASSERT('D', "4k", !dict2
->getCount());
693 dict2
->release(); dict2
= 0;
697 res
= res
&& checkSpace("(D)4", spaceCheck
, 0);
700 verPrintf(("testDictionary: All OSDictionary Tests passed\n"));
702 logPrintf(("testDictionary: Some OSDictionary Tests failed\n"));
709 OSObject
*cache
[numStrCache
];
711 const OSSymbol
*symCache
[numStrCache
], *sym
;
714 OSArray
*array
, *bigReturn
;
715 OSCollectionIterator
*iter1
, *iter2
;
716 int i
, numSymbols
, count1
, count2
, count3
;
718 // Setup symbol and string pools
719 for (i
= 0, numSymbols
= 0; i
< numStrCache
; i
++) {
720 sym
= OSSymbol::withCStringNoCopy(strCache
[i
]);
721 if (1 == sym
->getRetainCount()) {
722 cache
[numSymbols
] = OSString::withCStringNoCopy(strCache
[i
]);
723 symCache
[numSymbols
] = sym
;
730 // Test the array iterator
731 spaceCheck
= checkPointSpace();
733 array
= OSArray::withCapacity(numSymbols
);
734 TEST_ASSERT('I', "1a", array
);
737 for (i
= numSymbols
; --i
>= 0; )
738 count1
+= array
->setObject(cache
[i
], 0);
739 TEST_ASSERT('I', "1b", count1
== numSymbols
);
741 iter1
= OSCollectionIterator::withCollection(array
);
742 iter2
= OSCollectionIterator::withCollection(array
);
744 TEST_ASSERT('I', "1c", iter1
);
745 TEST_ASSERT('I', "1d", iter2
);
746 if (iter1
&& iter2
) {
747 count1
= count2
= count3
= 0;
748 for (i
= 0; (str
= (IOString
*) iter1
->getNextObject()); i
++) {
749 bigReturn
= iter2
->nextEntries();
750 count1
+= (bigReturn
->getCount() == 1);
751 count2
+= (cache
[i
] == bigReturn
->getObject(0));
752 count3
+= (cache
[i
] == str
);
754 TEST_ASSERT('I', "1e", count1
== numSymbols
);
755 TEST_ASSERT('I', "1f", count2
== numSymbols
);
756 TEST_ASSERT('I', "1g", count3
== numSymbols
);
757 TEST_ASSERT('I', "1h", iter1
->valid());
758 TEST_ASSERT('I', "1i", iter2
->valid());
761 str
= (OSString
*) array
->__takeObject(0);
762 array
->setObject(str
, 0);
764 TEST_ASSERT('I', "1j", !iter1
->getNextObject());
765 TEST_ASSERT('I', "1k", !iter1
->valid());
768 count1
= count2
= count3
= 0;
771 str
= (OSString
*) iter1
->getNextObject();
772 else if ( (bigReturn
= iter1
->nextEntries()) )
773 str
= (OSString
*) bigReturn
->getObject(0);
779 count1
+= (cache
[i
] == str
);
781 TEST_ASSERT('I', "1l", count1
== numSymbols
);
782 TEST_ASSERT('I', "1m", i
== numSymbols
);
783 TEST_ASSERT('I', "1n", iter1
->valid());
785 TEST_ASSERT('I', "1o", 3 == array
->getRetainCount());
789 if (iter1
) iter1
->release();
790 if (iter2
) iter2
->release();
791 res
= res
&& checkSpace("(I)1", spaceCheck
, 0);
793 // Test the set iterator
794 spaceCheck
= checkPointSpace();
796 set
= OSSet::withCapacity(numSymbols
);
797 TEST_ASSERT('I', "2a", set
);
800 for (i
= 0; i
< numSymbols
; i
++)
801 count1
+= set
->setObject(cache
[i
]);
802 TEST_ASSERT('I', "2b", count1
== numSymbols
);
804 iter1
= OSCollectionIterator::withCollection(set
);
805 iter2
= OSCollectionIterator::withCollection(set
);
807 TEST_ASSERT('I', "2c", iter1
);
808 TEST_ASSERT('I', "2d", iter2
);
809 if (iter1
&& iter2
) {
810 count1
= count2
= count3
= 0;
811 for (i
= 0; (str
= (IOString
*) iter1
->getNextObject()); i
++) {
812 bigReturn
= iter2
->nextEntries();
813 count1
+= (bigReturn
->getCount() == 1);
814 count2
+= (cache
[i
] == bigReturn
->getObject(0));
815 count3
+= (cache
[i
] == str
);
817 TEST_ASSERT('I', "2e", count1
== numSymbols
);
818 TEST_ASSERT('I', "2f", count2
== numSymbols
);
819 TEST_ASSERT('I', "2g", count3
== numSymbols
);
820 TEST_ASSERT('I', "2h", iter1
->valid());
821 TEST_ASSERT('I', "2i", iter2
->valid());
824 count1
= count2
= count3
= 0;
827 str
= (OSString
*) iter1
->getNextObject();
828 else if ( (bigReturn
= iter1
->nextEntries()) )
829 str
= (OSString
*) bigReturn
->getObject(0);
835 count1
+= (cache
[i
] == str
);
837 TEST_ASSERT('I', "2l", count1
== numSymbols
);
838 TEST_ASSERT('I', "2m", i
== numSymbols
);
839 TEST_ASSERT('I', "2n", iter1
->valid());
842 str
= (OSString
*) set
->getAnyObject();
843 (void) set
->__takeObject(str
);
846 TEST_ASSERT('I', "2j", !iter1
->getNextObject());
847 TEST_ASSERT('I', "2k", !iter1
->valid());
849 TEST_ASSERT('I', "2o", 3 == set
->getRetainCount());
853 if (iter1
) iter1
->release();
854 if (iter2
) iter2
->release();
855 res
= res
&& checkSpace("(I)2", spaceCheck
, 0);
857 // Test the dictionary iterator
858 spaceCheck
= checkPointSpace();
860 dict
= OSDictionary::withCapacity(numSymbols
);
861 TEST_ASSERT('I', "3a", dict
);
864 for (i
= 0; i
< numSymbols
; i
++)
865 count1
+= (0 != dict
->setObject(cache
[i
], symCache
[i
]));
866 TEST_ASSERT('I', "3b", count1
== numSymbols
);
868 iter1
= OSCollectionIterator::withCollection(dict
);
869 iter2
= OSCollectionIterator::withCollection(dict
);
871 TEST_ASSERT('I', "3c", iter1
);
872 TEST_ASSERT('I', "3d", iter2
);
873 if (iter1
&& iter2
) {
874 count1
= count2
= count3
= 0;
875 for (i
= 0; (sym
= (const IOSymbol
*) iter1
->getNextObject()); i
++) {
876 bigReturn
= iter2
->nextEntries();
877 count1
+= (bigReturn
->getCount() == 2);
878 count2
+= (cache
[i
] == bigReturn
->getObject(1));
879 count3
+= (symCache
[i
] == sym
);
881 TEST_ASSERT('I', "3e", count1
== numSymbols
);
882 TEST_ASSERT('I', "3f", count2
== numSymbols
);
883 TEST_ASSERT('I', "3g", count3
== numSymbols
);
884 TEST_ASSERT('I', "3h", iter1
->valid());
885 TEST_ASSERT('I', "3i", iter2
->valid());
888 count1
= count2
= count3
= 0;
892 sym
= (const OSSymbol
*) iter1
->getNextObject();
895 else if ( (bigReturn
= iter1
->nextEntries()) ) {
896 sym
= (const OSSymbol
*) bigReturn
->getObject(0);
897 str
= (OSString
*) bigReturn
->getObject(1);
905 count1
+= (symCache
[i
] == sym
);
906 count2
+= (!str
|| cache
[i
] == str
);
908 TEST_ASSERT('I', "3l", count1
== numSymbols
);
909 TEST_ASSERT('I', "3m", count2
== numSymbols
);
910 TEST_ASSERT('I', "3n", i
== numSymbols
);
911 TEST_ASSERT('I', "3o", iter1
->valid());
914 str
= (OSString
*) dict
->__takeObject(symCache
[numSymbols
-1]);
915 dict
->setObject(str
, symCache
[numSymbols
-1]);
917 TEST_ASSERT('I', "3j", !iter1
->getNextObject());
918 TEST_ASSERT('I', "3k", !iter1
->valid());
920 TEST_ASSERT('I', "3p", 3 == dict
->getRetainCount());
924 if (iter1
) iter1
->release();
925 if (iter2
) iter2
->release();
926 res
= res
&& checkSpace("(I)3", spaceCheck
, 0);
928 count1
= count2
= count3
= 0;
929 for (i
= 0; i
< numSymbols
; i
++) {
930 count1
+= (1 == cache
[i
]->getRetainCount());
931 count2
+= (1 == symCache
[i
]->getRetainCount());
933 symCache
[i
]->release();
935 TEST_ASSERT('I', "4a", count1
== numSymbols
);
936 TEST_ASSERT('I', "4b", count2
== numSymbols
);
939 verPrintf(("testIterator: All OSCollectionIterator Tests passed\n"));
941 logPrintf(("testIterator: Some OSCollectionIterator Tests failed\n"));