]> git.saurik.com Git - apple/xnu.git/blame_incremental - iokit/Tests/TestCollections.cpp
xnu-7195.50.7.100.1.tar.gz
[apple/xnu.git] / iokit / Tests / TestCollections.cpp
... / ...
CommitLineData
1/*
2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28#if DEBUG
29#include "Tests.h"
30
31#include <libkern/c++/OSArray.h>
32#include <libkern/c++/OSSet.h>
33#include <libkern/c++/OSDictionary.h>
34#include <libkern/c++/OSString.h>
35#include <libkern/c++/OSSymbol.h>
36#include <libkern/c++/OSCollectionIterator.h>
37
38void
39testArray()
40{
41 bool res = true;
42 void *spaceCheck, *spaceCheck2, *spaceCheck3;
43 int i, j, count, count2;
44 OSObject *cache[numStrCache], *str, *sym;
45 OSArray *array1, *array2;
46
47 // Do first test without memory leak tests to initialise the metaclass
48 array1 = OSArray::withCapacity(1);
49 TEST_ASSERT('A', "0a", array1);
50 if (array1) {
51 array1->release();
52 }
53
54 // Grow the symbol pool to maximum
55 for (i = 0; i < numStrCache; i++) {
56 cache[i] = (OSObject *) OSSymbol::withCStringNoCopy(strCache[i]);
57 }
58 for (i = 0; i < numStrCache; i++) {
59 cache[i]->release();
60 }
61
62 // Create and destroy an array
63 spaceCheck = checkPointSpace();
64 array1 = OSArray::withCapacity(1);
65 TEST_ASSERT('A', "1a", array1);
66 if (array1) {
67 TEST_ASSERT('A', "1b", !array1->getCount());
68 TEST_ASSERT('A', "1c", 1 == array1->getCapacity());
69 TEST_ASSERT('A', "1d", 1 == array1->getCapacityIncrement());
70 TEST_ASSERT('A', "1e", 4 == array1->setCapacityIncrement(4));
71 TEST_ASSERT('A', "1f", 4 == array1->getCapacityIncrement());
72 TEST_ASSERT('A', "1g", 8 == array1->ensureCapacity(5));
73
74 spaceCheck2 = checkPointSpace();
75 cache[0] = IOString::withCStringNoCopy(strCache[0]);
76
77 spaceCheck3 = checkPointSpace();
78 TEST_ASSERT('A', "1h", array1->setObject(cache[0]));
79 TEST_ASSERT('A', "1i", cache[0] == array1->getObject(0));
80 cache[0]->release();
81 res = res && checkSpace("(A)1j", spaceCheck3, 0);
82
83 TEST_ASSERT('A', "1k", 1 == array1->getCount());
84 array1->flushCollection();
85 TEST_ASSERT('A', "1l", !array1->getCount());
86 res = res && checkSpace("(A)1m", spaceCheck2, 0);
87
88 array1->release();
89 }
90 res = res && checkSpace("(A)1", spaceCheck, 0);
91
92 // Check the creation of a sizable OSArray from an array of IOObjects
93 // Also check indexing into the array.
94 spaceCheck = checkPointSpace();
95 for (i = 0; i < numStrCache; i++) {
96 cache[i] = OSString::withCStringNoCopy(strCache[i]);
97 }
98 array1 = OSArray::withObjects(cache, numStrCache, numStrCache);
99 TEST_ASSERT('A', "2a", array1);
100 for (i = 0; i < numStrCache; i++) {
101 cache[i]->release();
102 }
103 if (array1) {
104 TEST_ASSERT('A', "2b", numStrCache == (int) array1->getCount());
105 TEST_ASSERT('A', "2c", numStrCache == (int) array1->getCapacity());
106 TEST_ASSERT('A', "2d",
107 numStrCache == (int) array1->getCapacityIncrement());
108
109 for (i = 0; (str = array1->getObject(i)); i++) {
110 if (str != cache[i]) {
111 verPrintf(("testArray(A) test 2e%d failed\n", i));
112 res = false;
113 }
114 }
115 TEST_ASSERT('A', "2f", numStrCache == i);
116 array1->release();
117 }
118 res = res && checkSpace("(A)2", spaceCheck, 0);
119
120 // Test array creation from another array by both the setObject method
121 // and the withArray factory. And test __takeObject code first
122 // with tail removal then with head removal
123 spaceCheck = checkPointSpace();
124 for (i = 0; i < numStrCache; i++) {
125 cache[i] = OSString::withCStringNoCopy(strCache[i]);
126 }
127 array1 = OSArray::withObjects(cache, numStrCache, numStrCache);
128 TEST_ASSERT('A', "3a", array1);
129 for (i = 0; i < numStrCache; i++) {
130 cache[i]->release();
131 }
132 array2 = 0;
133 if (array1) {
134 array2 = OSArray::withCapacity(1);
135 TEST_ASSERT('A', "3b", array2);
136 TEST_ASSERT('A', "3c", !array2->getCount());
137 TEST_ASSERT('A', "3d", array2->setObject(array1));
138 TEST_ASSERT('A', "3e", array1->getCount() == array2->getCount());
139 }
140 if (array2) {
141 count = 0;
142 TEST_ASSERT('A', "3f", numStrCache == (int) array2->getCount());
143 for (i = array2->getCount(); (str = array2->__takeObject(--i));) {
144 if (str != cache[i]) {
145 verPrintf(("testArray(A) test 3g%d failed\n", i));
146 res = false;
147 }
148 count += ((int) array2->getCount() == i);
149 str->release();
150 }
151 TEST_ASSERT('A', "3h", count == numStrCache);
152 TEST_ASSERT('A', "3i", -1 == i);
153 TEST_ASSERT('A', "3j", !array2->getCount());
154
155 spaceCheck2 = checkPointSpace();
156 array2->flushCollection();
157 res = res && checkSpace("(A)3k", spaceCheck2, 0);
158
159 array2->release();
160 array2 = 0;
161 }
162 if (array1) {
163 array2 = OSArray::withArray(array1, numStrCache - 1);
164 TEST_ASSERT('A', "3l", !array2);
165 array2 = OSArray::withArray(array1, array1->getCount());
166 TEST_ASSERT('A', "3m", array2);
167 array1->release();
168 }
169 if (array2) {
170 count = 0;
171 TEST_ASSERT('A', "3o", numStrCache == (int) array2->getCount());
172 for (i = 0; (str = array2->__takeObject(0)); i++) {
173 count += (str == cache[i]);
174 str->release();
175 }
176 TEST_ASSERT('A', "3p", count == numStrCache);
177 TEST_ASSERT('A', "3q", !array2->getCount());
178 array2->release();
179 array2 = 0;
180 }
181 res = res && checkSpace("(A)3", spaceCheck, 0);
182
183 // Test object replacement from one array to another
184 spaceCheck = checkPointSpace();
185 array1 = OSArray::withCapacity(numStrCache);
186 TEST_ASSERT('A', "4a", array1);
187 if (array1) {
188 count = count2 = 0;
189 for (i = 0; i < numStrCache; i++) {
190 str = OSString::withCStringNoCopy(strCache[i]);
191 count += array1->setObject(str);
192 count2 += (str == array1->lastObject());
193 str->release();
194 }
195 TEST_ASSERT('A', "4b", numStrCache == (int) array1->getCount());
196 TEST_ASSERT('A', "4c", count == numStrCache);
197 TEST_ASSERT('A', "4d", count2 == numStrCache);
198 }
199 array2 = OSArray::withCapacity(1);
200 TEST_ASSERT('A', "4e", array2);
201 if (array2) {
202 count = count2 = 0;
203 str = (OSObject *) OSSymbol::withCStringNoCopy(strCache[0]);
204 for (i = 0; i < numStrCache; i++) {
205 sym = (OSObject *) OSSymbol::withCStringNoCopy(strCache[i]);
206 count += array2->setObject(sym, 0);
207 count2 += (str == array2->lastObject());
208 sym->release();
209 }
210 str->release();
211 TEST_ASSERT('A', "4f", numStrCache == (int) array2->getCount());
212 TEST_ASSERT('A', "4g", count == numStrCache);
213 TEST_ASSERT('A', "4h", count2 == numStrCache);
214 }
215 if (array1 && array2) {
216 count = count2 = 0;
217 for (i = array1->getCount() - 1; (sym = array2->__takeObject(0)); i--) {
218 str = array1->replaceObject(sym, i);
219 count += (str != 0);
220 count2 += (sym != str);
221 if (str) {
222 str->release();
223 }
224 if (sym) {
225 sym->release();
226 }
227 }
228 TEST_ASSERT('A', "4k", numStrCache == (int) array1->getCount());
229 TEST_ASSERT('A', "4l", count == numStrCache);
230 TEST_ASSERT('A', "4m", count2 == numStrCache);
231 array1->release();
232 array2->release();
233 } else {
234 if (array1) {
235 array1->release();
236 }
237 if (array2) {
238 array2->release();
239 }
240 }
241 res = res && checkSpace("(A)4", spaceCheck, 0);
242
243 // Test array duplicate removal
244 spaceCheck = checkPointSpace();
245 array1 = OSArray::withCapacity(numStrCache);
246 TEST_ASSERT('A', "5a", array1);
247 if (array1) {
248 for (i = 0; i < numStrCache; i++) {
249 sym = (OSObject *) OSSymbol::withCStringNoCopy(strCache[i]);
250 count += array1->setObject(sym);
251 sym->release();
252 }
253 TEST_ASSERT('A', "5b", numStrCache == (int) array1->getCount());
254
255 // remove duplicates
256 for (i = 0; (sym = array1->getObject(i));) {
257 if (sym->getRetainCount() == 1) {
258 i++;
259 } else {
260 //sym = array1->__takeObject(i);
261 //sym->release();
262 array1->removeObject(i);
263 }
264 }
265 TEST_ASSERT('A', "5c", numStrCache != (int) array1->getCount());
266
267 // check to see that all symbols are really there
268 for (count = 0, i = 0; i < numStrCache; i++) {
269 sym = (OSObject *) OSSymbol::withCStringNoCopy(strCache[i]);
270 for (count2 = false, j = 0; (str = array1->getObject(j)); j++) {
271 if (str == sym) {
272 count2 = true;
273 break;
274 }
275 }
276 count += count2;
277 sym->release();
278 }
279 TEST_ASSERT('A', "5c", count == numStrCache);
280 array1->release();
281 }
282 res = res && checkSpace("(S)5", spaceCheck, 0);
283
284 if (res) {
285 verPrintf(("testArray: All OSArray Tests passed\n"));
286 } else {
287 logPrintf(("testArray: Some OSArray Tests failed\n"));
288 }
289}
290
291void
292testSet()
293{
294 bool res = true;
295 void *spaceCheck, *spaceCheck2, *spaceCheck3;
296 int i, count, count2;
297 OSObject *cache[numStrCache], *str, *sym;
298 OSSet *set1, *set2;
299 OSArray *array;
300
301 // Do first test without memory leak tests to initialise the metaclass
302 set1 = OSSet::withCapacity(1);
303 TEST_ASSERT('S', "0a", set1);
304 if (set1) {
305 set1->release();
306 }
307
308 // Grow the symbol pool to maximum
309 for (i = 0; i < numStrCache; i++) {
310 cache[i] = (OSObject *) OSSymbol::withCStringNoCopy(strCache[i]);
311 }
312 for (i = 0; i < numStrCache; i++) {
313 cache[i]->release();
314 }
315
316 // Create and destroy an set
317 spaceCheck = checkPointSpace();
318 set1 = OSSet::withCapacity(1);
319 TEST_ASSERT('S', "1a", set1);
320 if (set1) {
321 TEST_ASSERT('S', "1b", !set1->getCount());
322 TEST_ASSERT('S', "1c", 1 == set1->getCapacity());
323 TEST_ASSERT('S', "1d", 1 == set1->getCapacityIncrement());
324 TEST_ASSERT('S', "1e", 4 == set1->setCapacityIncrement(4));
325 TEST_ASSERT('S', "1f", 4 == set1->getCapacityIncrement());
326 TEST_ASSERT('S', "1g", 8 == set1->ensureCapacity(5));
327
328 spaceCheck2 = checkPointSpace();
329 cache[0] = IOString::withCStringNoCopy(strCache[0]);
330
331 spaceCheck3 = checkPointSpace();
332 TEST_ASSERT('S', "1h", set1->setObject(cache[0]));
333 TEST_ASSERT('S', "1i", set1->containsObject(cache[0]));
334 TEST_ASSERT('S', "1j", cache[0] == set1->getAnyObject());
335 cache[0]->release();
336 res = res && checkSpace("(S)1k", spaceCheck3, 0);
337
338 TEST_ASSERT('S', "1l", 1 == set1->getCount());
339 set1->flushCollection();
340 TEST_ASSERT('S', "1m", !set1->getCount());
341 res = res && checkSpace("(S)1n", spaceCheck2, 0);
342
343 set1->release();
344 }
345 res = res && checkSpace("(S)1", spaceCheck, 0);
346
347 // Check the creation of a sizable OSSet from an set of IOObjects
348 // Also check member test of set.
349 spaceCheck = checkPointSpace();
350 for (i = 0; i < numStrCache; i++) {
351 cache[i] = OSString::withCStringNoCopy(strCache[i]);
352 }
353 set1 = OSSet::withObjects(cache, numStrCache, numStrCache);
354 TEST_ASSERT('S', "2a", set1);
355 for (i = 0; i < numStrCache; i++) {
356 cache[i]->release();
357 }
358 if (set1) {
359 TEST_ASSERT('S', "2b", numStrCache == (int) set1->getCount());
360 TEST_ASSERT('S', "2c", numStrCache == (int) set1->getCapacity());
361 TEST_ASSERT('S', "2d",
362 numStrCache == (int) set1->getCapacityIncrement());
363
364 count = 0;
365 for (i = set1->getCount(); --i >= 0;) {
366 count += set1->member(cache[i]);
367 }
368
369 TEST_ASSERT('S', "2e", numStrCache == count);
370 set1->release();
371 }
372 res = res && checkSpace("(S)2", spaceCheck, 0);
373
374 // Test set creation from another set by both the setObject method
375 // and the withArray factory. And test __takeObject code first
376 // with tail removal then with head removal
377 spaceCheck = checkPointSpace();
378 for (i = 0; i < numStrCache; i++) {
379 cache[i] = OSString::withCStringNoCopy(strCache[i]);
380 }
381 set1 = OSSet::withObjects(cache, numStrCache, numStrCache);
382 TEST_ASSERT('S', "3a", set1);
383 for (i = 0; i < numStrCache; i++) {
384 cache[i]->release();
385 }
386 set2 = 0;
387 if (set1) {
388 set2 = OSSet::withCapacity(set1->getCount());
389 TEST_ASSERT('S', "3b", set2);
390 TEST_ASSERT('S', "3c", !set2->getCount());
391 TEST_ASSERT('S', "3d", set2->setObject(set1));
392 TEST_ASSERT('S', "3e", set1->getCount() == set2->getCount());
393 }
394 if (set2) {
395 TEST_ASSERT('S', "3f", numStrCache == (int) set2->getCount());
396 count = count2 = 0;
397 while ((str = set2->getAnyObject())) {
398 count += set2->__takeObject(str);
399 count2 += set1->member(str);
400 str->release();
401 }
402 TEST_ASSERT('S', "3g", !set2->getCount());
403 TEST_ASSERT('S', "3h", numStrCache == count);
404 TEST_ASSERT('S', "3i", numStrCache == count2);
405
406 spaceCheck2 = checkPointSpace();
407 set2->flushCollection();
408 res = res && checkSpace("(S)3j", spaceCheck2, 0);
409
410 set2->release();
411 set2 = 0;
412 }
413 if (set1) {
414 set2 = OSSet::withSet(set1, numStrCache - 1);
415 TEST_ASSERT('S', "3k", !set2);
416 set2 = OSSet::withSet(set1, set1->getCount());
417 TEST_ASSERT('S', "3l", set2);
418 set1->release();
419 }
420 if (set2) {
421 TEST_ASSERT('S', "3m", numStrCache == (int) set2->getCount());
422 i = count = count2 = 0;
423 while ((str = set2->getAnyObject())) {
424 count += set2->__takeObject(str);
425 count2 += (cache[i++] == str);
426 str->release();
427 }
428 TEST_ASSERT('S', "3n", !set2->getCount());
429 TEST_ASSERT('S', "3o", numStrCache == count);
430 TEST_ASSERT('S', "3p", numStrCache == count2);
431
432 set2->release();
433 set2 = 0;
434 }
435 res = res && checkSpace("(S)3", spaceCheck, 0);
436
437 // Test duplicate removal
438 spaceCheck = checkPointSpace();
439 set2 = 0;
440 set1 = OSSet::withCapacity(numStrCache);
441 TEST_ASSERT('S', "4a", set1);
442 if (set1) {
443 count = 0;
444 for (i = 0; i < numStrCache; i++) {
445 sym = (OSObject *) OSSymbol::withCStringNoCopy(strCache[i]);
446 count += set1->setObject(sym);
447 sym->release();
448 }
449 TEST_ASSERT('S', "4b", numStrCache != (int) set1->getCount());
450 TEST_ASSERT('S', "4c", count == (int) set1->getCount());
451
452 count = count2 = 0;
453 for (i = 0; i < numStrCache; i++) {
454 sym = (OSObject *) OSSymbol::withCStringNoCopy(strCache[i]);
455 count += set1->member(sym);
456 count2 += sym->getRetainCount();
457 sym->release();
458 }
459 TEST_ASSERT('S', "4d", count == numStrCache);
460 TEST_ASSERT('S', "4e", count2 == numStrCache * 2);
461
462 set2 = OSSet::withSet(set1, 2 * set1->getCount());
463 }
464 TEST_ASSERT('S', "4f", set2);
465 if (set2) {
466 set2->setObject(set1);
467 TEST_ASSERT('S', "4g", set1->getCount() == set2->getCount());
468 set1->release();
469 set2->release();
470 }
471 res = res && checkSpace("(S)4", spaceCheck, 0);
472
473 // Test array duplicate removal
474 spaceCheck = checkPointSpace();
475 array = OSArray::withCapacity(numStrCache);
476 for (i = 0; i < numStrCache; i++) {
477 sym = (OSObject *) OSSymbol::withCStringNoCopy(strCache[i]);
478 count += array->setObject(sym);
479 sym->release();
480 }
481 set1 = OSSet::withArray(array, numStrCache);
482 TEST_ASSERT('S', "5a", set1);
483 if (set1) {
484 TEST_ASSERT('S', "5b", array->getCount() != set1->getCount());
485 array->release();
486
487 count = count2 = set1->getCount();
488 while ((sym = set1->getAnyObject())) {
489 count -= set1->__takeObject(sym);
490 count2 -= sym->getRetainCount();
491 sym->release();
492 }
493 TEST_ASSERT('S', "5c", !count);
494 TEST_ASSERT('S', "5d", !count2);
495 set1->release();
496 }
497 res = res && checkSpace("(S)5", spaceCheck, 0);
498
499 if (res) {
500 verPrintf(("testSet: All OSSet Tests passed\n"));
501 } else {
502 logPrintf(("testSet: Some OSSet Tests failed\n"));
503 }
504}
505
506void
507testDictionary()
508{
509 bool res = true;
510 void *spaceCheck, *spaceCheck2, *spaceCheck3;
511 OSObject *cache[numStrCache];
512 OSString *str;
513 const OSSymbol *symCache[numStrCache], *sym;
514 OSDictionary *dict1, *dict2;
515 int i, numSymbols, count1, count2;
516
517 // Do first test without memory leak tests to initialise the metaclass
518 dict1 = OSDictionary::withCapacity(1);
519 TEST_ASSERT('D', "0a", dict1);
520 if (dict1) {
521 dict1->release();
522 }
523
524 // Grow the symbol pool to maximum
525 for (i = 0; i < numStrCache; i++) {
526 symCache[i] = OSSymbol::withCStringNoCopy(strCache[i]);
527 }
528 for (i = 0; i < numStrCache; i++) {
529 symCache[i]->release();
530 }
531
532 // Create and destroy a dictionary
533 spaceCheck = checkPointSpace();
534 dict1 = OSDictionary::withCapacity(1);
535 TEST_ASSERT('D', "1a", dict1);
536 if (dict1) {
537 TEST_ASSERT('D', "1b", !dict1->getCount());
538 TEST_ASSERT('D', "1c", 1 == dict1->getCapacity());
539 TEST_ASSERT('D', "1d", 1 == dict1->getCapacityIncrement());
540 TEST_ASSERT('D', "1e", 4 == dict1->setCapacityIncrement(4));
541 TEST_ASSERT('D', "1f", 4 == dict1->getCapacityIncrement());
542 TEST_ASSERT('D', "1g", 8 == dict1->ensureCapacity(5));
543
544 spaceCheck2 = checkPointSpace();
545 sym = OSSymbol::withCStringNoCopy(strCache[0]);
546
547 spaceCheck3 = checkPointSpace();
548 TEST_ASSERT('D', "1h", dict1->setObject((OSObject *) sym, sym));
549 TEST_ASSERT('D', "1i", (OSObject *) sym == dict1->getObject(sym));
550 sym->release();
551 TEST_ASSERT('D', "1i", 2 == sym->getRetainCount());
552 res = res && checkSpace("(D)1j", spaceCheck3, 0);
553
554 TEST_ASSERT('D', "1k", 1 == dict1->getCount());
555 dict1->flushCollection();
556 TEST_ASSERT('D', "1l", !dict1->getCount());
557 res = res && checkSpace("(D)1m", spaceCheck2, 0);
558
559 dict1->release();
560 }
561 res = res && checkSpace("(D)1", spaceCheck, 0);
562
563 // Check the creation of a sizable OSDictionary from an array of IOObjects
564 // Also check indexing into the array.
565 spaceCheck = checkPointSpace();
566 for (i = 0, numSymbols = 0; i < numStrCache; i++) {
567 sym = OSSymbol::withCStringNoCopy(strCache[i]);
568 if (1 == sym->getRetainCount()) {
569 symCache[numSymbols++] = sym;
570 } else {
571 sym->release();
572 }
573 }
574 dict1 = OSDictionary::withObjects(
575 (OSObject **) symCache, symCache, numSymbols, numSymbols);
576 TEST_ASSERT('D', "2a", dict1);
577 count1 = count2 = 0;
578 for (i = 0; i < numSymbols; i++) {
579 count1 += (symCache[i]->getRetainCount() == 3);
580 }
581 TEST_ASSERT('D', "2b", count1 == numSymbols);
582 if (dict1) {
583 TEST_ASSERT('D', "2c", numSymbols == (int) dict1->getCount());
584 TEST_ASSERT('D', "2d", numSymbols == (int) dict1->getCapacity());
585 TEST_ASSERT('D', "2e",
586 numSymbols == (int) dict1->getCapacityIncrement());
587
588 for (i = dict1->getCount(); --i >= 0;) {
589 str = (OSString *) dict1->getObject(symCache[i]);
590 if (str != (OSString *) symCache[i]) {
591 verPrintf(("testDictionary(D) test 2f%d failed\n", i));
592 res = false;
593 }
594 }
595 dict1->release();
596 }
597 count1 = count2 = 0;
598 for (i = 0; i < numSymbols; i++) {
599 count1 += (symCache[i]->getRetainCount() == 1);
600 symCache[i]->release();
601 }
602 TEST_ASSERT('D', "2g", count1 == numSymbols);
603 res = res && checkSpace("(D)2", spaceCheck, 0);
604
605 // Check the creation of a sizable Dictionary from an array of IOStrings
606 // Also check searching dictionary use OSString for a key.
607 spaceCheck = checkPointSpace();
608 for (i = 0, numSymbols = 0; i < numStrCache; i++) {
609 sym = OSSymbol::withCStringNoCopy(strCache[i]);
610 if (1 == sym->getRetainCount()) {
611 cache[numSymbols] = OSString::withCStringNoCopy(strCache[i]);
612 symCache[numSymbols] = sym;
613 numSymbols++;
614 } else {
615 sym->release();
616 }
617 }
618 dict1 = OSDictionary::withObjects((OSObject **) symCache,
619 (OSString **) cache,
620 numSymbols, numSymbols);
621 TEST_ASSERT('D', "3a", dict1);
622 count1 = count2 = 0;
623 for (i = 0; i < numSymbols; i++) {
624 count1 += (symCache[i]->getRetainCount() == 3);
625 count2 += (cache[i]->getRetainCount() == 1);
626 }
627 TEST_ASSERT('D', "3b", count1 == numSymbols);
628 TEST_ASSERT('D', "3c", count2 == numSymbols);
629 if (dict1) {
630 count1 = count2 = 0;
631 for (i = 0; i < numSymbols; i++) {
632 str = (OSString *) cache[i];
633 count1 += (symCache[i] == (const OSSymbol *) dict1->getObject(str));
634 count2 += (symCache[i]->getRetainCount() == 3);
635 }
636 TEST_ASSERT('D', "3d", count1 == numSymbols);
637 TEST_ASSERT('D', "3e", count2 == numSymbols);
638
639 count1 = count2 = 0;
640 for (i = 0; i < numSymbols; i++) {
641 const char *cStr = ((OSString *) cache[i])->getCStringNoCopy();
642
643 count1 += (symCache[i] == (const OSSymbol *) dict1->getObject(cStr));
644 count2 += (symCache[i]->getRetainCount() == 3);
645 }
646 TEST_ASSERT('D', "3f", count1 == numSymbols);
647 TEST_ASSERT('D', "3g", count2 == numSymbols);
648
649 dict1->release();
650 }
651 count1 = count2 = 0;
652 for (i = 0; i < numSymbols; i++) {
653 count1 += (symCache[i]->getRetainCount() == 1);
654 count2 += (cache[i]->getRetainCount() == 1);
655 symCache[i]->release();
656 cache[i]->release();
657 }
658 TEST_ASSERT('D', "3h", count1 == numSymbols);
659 res = res && checkSpace("(D)3", spaceCheck, 0);
660
661 // Check the creation of a small dictionary then grow it one item at a time
662 // Create a new dictionary from the old dictionary.
663 // Finally remove each item permanently.
664 spaceCheck = checkPointSpace();
665 for (i = 0, numSymbols = 0; i < numStrCache; i++) {
666 sym = OSSymbol::withCStringNoCopy(strCache[i]);
667 if (1 == sym->getRetainCount()) {
668 cache[numSymbols] = OSString::withCStringNoCopy(strCache[i]);
669 symCache[numSymbols] = sym;
670 numSymbols++;
671 } else {
672 sym->release();
673 }
674 }
675 dict2 = 0;
676 dict1 = OSDictionary::withCapacity(1);
677 TEST_ASSERT('D', "4a", dict1);
678 if (dict1) {
679 count1 = count2 = 0;
680 for (i = 0; i < numSymbols; i++) {
681 sym = symCache[i];
682 count1 += ((OSObject *) sym == dict1->setObject((OSObject *) sym,
683 sym->getCStringNoCopy()));
684 count2 += (sym->getRetainCount() == 3);
685 }
686 TEST_ASSERT('D', "4b", numSymbols == (int) dict1->getCount());
687 TEST_ASSERT('D', "4c", numSymbols == count1);
688 TEST_ASSERT('D', "4d", numSymbols == count2);
689
690 dict2 = OSDictionary::withDictionary(dict1, numSymbols - 1);
691 TEST_ASSERT('D', "4b", !dict2);
692 dict2 = OSDictionary::withDictionary(dict1, numSymbols);
693 }
694 TEST_ASSERT('D', "4e", dict2);
695 if (dict2) {
696 dict1->release(); dict1 = 0;
697
698 TEST_ASSERT('D', "4f", numSymbols == (int) dict2->getCount());
699
700 count1 = count2 = 0;
701 for (i = 0; i < numSymbols; i++) {
702 OSObject *replacedObject;
703
704 sym = symCache[i];
705 str = (OSString *) cache[i];
706 replacedObject = dict2->setObject(str, str);
707 count1 += ((OSString *) sym == replacedObject);
708 replacedObject->release();
709 count2 += (sym->getRetainCount() == 2);
710 str->release();
711 }
712 TEST_ASSERT('D', "4g", numSymbols == count1);
713 TEST_ASSERT('D', "4h", numSymbols == count2);
714
715 count1 = count2 = 0;
716 for (i = 0; i < numSymbols; i++) {
717 sym = symCache[i];
718 str = (OSString *) cache[i];
719 count1 += (str == dict2->__takeObject(sym));
720 str->release();
721 count2 += (sym->getRetainCount() == 1);
722 sym->release();
723 }
724 TEST_ASSERT('D', "4i", numSymbols == count1);
725 TEST_ASSERT('D', "4j", numSymbols == count2);
726 TEST_ASSERT('D', "4k", !dict2->getCount());
727 dict2->release(); dict2 = 0;
728 } else if (dict1) {
729 dict1->release();
730 }
731 res = res && checkSpace("(D)4", spaceCheck, 0);
732
733 if (res) {
734 verPrintf(("testDictionary: All OSDictionary Tests passed\n"));
735 } else {
736 logPrintf(("testDictionary: Some OSDictionary Tests failed\n"));
737 }
738}
739
740void
741testIterator()
742{
743 bool res = true;
744 void *spaceCheck;
745 OSObject *cache[numStrCache];
746 OSString *str = 0;
747 const OSSymbol *symCache[numStrCache], *sym;
748 OSDictionary *dict;
749 OSSet *set;
750 OSArray *array, *bigReturn;
751 OSCollectionIterator *iter1, *iter2;
752 int i, numSymbols, count1, count2, count3;
753
754 // Setup symbol and string pools
755 for (i = 0, numSymbols = 0; i < numStrCache; i++) {
756 sym = OSSymbol::withCStringNoCopy(strCache[i]);
757 if (1 == sym->getRetainCount()) {
758 cache[numSymbols] = OSString::withCStringNoCopy(strCache[i]);
759 symCache[numSymbols] = sym;
760 numSymbols++;
761 } else {
762 sym->release();
763 }
764 }
765
766 // Test the array iterator
767 spaceCheck = checkPointSpace();
768 iter1 = iter2 = 0;
769 array = OSArray::withCapacity(numSymbols);
770 TEST_ASSERT('I', "1a", array);
771 if (array) {
772 count1 = count2 = 0;
773 for (i = numSymbols; --i >= 0;) {
774 count1 += array->setObject(cache[i], 0);
775 }
776 TEST_ASSERT('I', "1b", count1 == numSymbols);
777
778 iter1 = OSCollectionIterator::withCollection(array);
779 iter2 = OSCollectionIterator::withCollection(array);
780 }
781 TEST_ASSERT('I', "1c", iter1);
782 TEST_ASSERT('I', "1d", iter2);
783 if (iter1 && iter2) {
784 count1 = count2 = count3 = 0;
785 for (i = 0; (str = (IOString *) iter1->getNextObject()); i++) {
786 bigReturn = iter2->nextEntries();
787 count1 += (bigReturn->getCount() == 1);
788 count2 += (cache[i] == bigReturn->getObject(0));
789 count3 += (cache[i] == str);
790 }
791 TEST_ASSERT('I', "1e", count1 == numSymbols);
792 TEST_ASSERT('I', "1f", count2 == numSymbols);
793 TEST_ASSERT('I', "1g", count3 == numSymbols);
794 TEST_ASSERT('I', "1h", iter1->valid());
795 TEST_ASSERT('I', "1i", iter2->valid());
796
797 iter1->reset();
798 str = (OSString *) array->__takeObject(0);
799 array->setObject(str, 0);
800 str->release();
801 TEST_ASSERT('I', "1j", !iter1->getNextObject());
802 TEST_ASSERT('I', "1k", !iter1->valid());
803
804 iter1->reset();
805 count1 = count2 = count3 = 0;
806 for (i = 0;; i++) {
807 if (i & 1) {
808 str = (OSString *) iter1->getNextObject();
809 } else if ((bigReturn = iter1->nextEntries())) {
810 str = (OSString *) bigReturn->getObject(0);
811 } else {
812 str = 0;
813 }
814
815 if (!str) {
816 break;
817 }
818 count1 += (cache[i] == str);
819 }
820 TEST_ASSERT('I', "1l", count1 == numSymbols);
821 TEST_ASSERT('I', "1m", i == numSymbols);
822 TEST_ASSERT('I', "1n", iter1->valid());
823
824 TEST_ASSERT('I', "1o", 3 == array->getRetainCount());
825 array->release();
826 }
827
828 if (iter1) {
829 iter1->release();
830 }
831 if (iter2) {
832 iter2->release();
833 }
834 res = res && checkSpace("(I)1", spaceCheck, 0);
835
836 // Test the set iterator
837 spaceCheck = checkPointSpace();
838 iter1 = 0;
839 set = OSSet::withCapacity(numSymbols);
840 TEST_ASSERT('I', "2a", set);
841 if (set) {
842 count1 = count2 = 0;
843 for (i = 0; i < numSymbols; i++) {
844 count1 += set->setObject(cache[i]);
845 }
846 TEST_ASSERT('I', "2b", count1 == numSymbols);
847
848 iter1 = OSCollectionIterator::withCollection(set);
849 iter2 = OSCollectionIterator::withCollection(set);
850 }
851 TEST_ASSERT('I', "2c", iter1);
852 TEST_ASSERT('I', "2d", iter2);
853 if (iter1 && iter2) {
854 count1 = count2 = count3 = 0;
855 for (i = 0; (str = (IOString *) iter1->getNextObject()); i++) {
856 bigReturn = iter2->nextEntries();
857 count1 += (bigReturn->getCount() == 1);
858 count2 += (cache[i] == bigReturn->getObject(0));
859 count3 += (cache[i] == str);
860 }
861 TEST_ASSERT('I', "2e", count1 == numSymbols);
862 TEST_ASSERT('I', "2f", count2 == numSymbols);
863 TEST_ASSERT('I', "2g", count3 == numSymbols);
864 TEST_ASSERT('I', "2h", iter1->valid());
865 TEST_ASSERT('I', "2i", iter2->valid());
866
867 iter1->reset();
868 count1 = count2 = count3 = 0;
869 for (i = 0;; i++) {
870 if (i & 1) {
871 str = (OSString *) iter1->getNextObject();
872 } else if ((bigReturn = iter1->nextEntries())) {
873 str = (OSString *) bigReturn->getObject(0);
874 } else {
875 str = 0;
876 }
877
878 if (!str) {
879 break;
880 }
881 count1 += (cache[i] == str);
882 }
883 TEST_ASSERT('I', "2l", count1 == numSymbols);
884 TEST_ASSERT('I', "2m", i == numSymbols);
885 TEST_ASSERT('I', "2n", iter1->valid());
886
887 iter1->reset();
888 str = (OSString *) set->getAnyObject();
889 (void) set->__takeObject(str);
890 set->setObject(str);
891 str->release();
892 TEST_ASSERT('I', "2j", !iter1->getNextObject());
893 TEST_ASSERT('I', "2k", !iter1->valid());
894
895 TEST_ASSERT('I', "2o", 3 == set->getRetainCount());
896 set->release();
897 }
898
899 if (iter1) {
900 iter1->release();
901 }
902 if (iter2) {
903 iter2->release();
904 }
905 res = res && checkSpace("(I)2", spaceCheck, 0);
906
907 // Test the dictionary iterator
908 spaceCheck = checkPointSpace();
909 iter1 = 0;
910 dict = OSDictionary::withCapacity(numSymbols);
911 TEST_ASSERT('I', "3a", dict);
912 if (dict) {
913 count1 = count2 = 0;
914 for (i = 0; i < numSymbols; i++) {
915 count1 += (0 != dict->setObject(cache[i], symCache[i]));
916 }
917 TEST_ASSERT('I', "3b", count1 == numSymbols);
918
919 iter1 = OSCollectionIterator::withCollection(dict);
920 iter2 = OSCollectionIterator::withCollection(dict);
921 }
922 TEST_ASSERT('I', "3c", iter1);
923 TEST_ASSERT('I', "3d", iter2);
924 if (iter1 && iter2) {
925 count1 = count2 = count3 = 0;
926 for (i = 0; (sym = (const IOSymbol *) iter1->getNextObject()); i++) {
927 bigReturn = iter2->nextEntries();
928 count1 += (bigReturn->getCount() == 2);
929 count2 += (cache[i] == bigReturn->getObject(1));
930 count3 += (symCache[i] == sym);
931 }
932 TEST_ASSERT('I', "3e", count1 == numSymbols);
933 TEST_ASSERT('I', "3f", count2 == numSymbols);
934 TEST_ASSERT('I', "3g", count3 == numSymbols);
935 TEST_ASSERT('I', "3h", iter1->valid());
936 TEST_ASSERT('I', "3i", iter2->valid());
937
938 iter1->reset();
939 count1 = count2 = count3 = 0;
940 i = 0;
941 for (i = 0;; i++) {
942 if (i & 1) {
943 sym = (const OSSymbol *) iter1->getNextObject();
944 str = 0;
945 } else if ((bigReturn = iter1->nextEntries())) {
946 sym = (const OSSymbol *) bigReturn->getObject(0);
947 str = (OSString *) bigReturn->getObject(1);
948 } else {
949 sym = 0;
950 }
951
952 if (!sym) {
953 break;
954 }
955
956 count1 += (symCache[i] == sym);
957 count2 += (!str || cache[i] == str);
958 }
959 TEST_ASSERT('I', "3l", count1 == numSymbols);
960 TEST_ASSERT('I', "3m", count2 == numSymbols);
961 TEST_ASSERT('I', "3n", i == numSymbols);
962 TEST_ASSERT('I', "3o", iter1->valid());
963
964 iter1->reset();
965 str = (OSString *) dict->__takeObject(symCache[numSymbols - 1]);
966 dict->setObject(str, symCache[numSymbols - 1]);
967 str->release();
968 TEST_ASSERT('I', "3j", !iter1->getNextObject());
969 TEST_ASSERT('I', "3k", !iter1->valid());
970
971 TEST_ASSERT('I', "3p", 3 == dict->getRetainCount());
972 dict->release();
973 }
974
975 if (iter1) {
976 iter1->release();
977 }
978 if (iter2) {
979 iter2->release();
980 }
981 res = res && checkSpace("(I)3", spaceCheck, 0);
982
983 count1 = count2 = count3 = 0;
984 for (i = 0; i < numSymbols; i++) {
985 count1 += (1 == cache[i]->getRetainCount());
986 count2 += (1 == symCache[i]->getRetainCount());
987 cache[i]->release();
988 symCache[i]->release();
989 }
990 TEST_ASSERT('I', "4a", count1 == numSymbols);
991 TEST_ASSERT('I', "4b", count2 == numSymbols);
992
993 if (res) {
994 verPrintf(("testIterator: All OSCollectionIterator Tests passed\n"));
995 } else {
996 logPrintf(("testIterator: Some OSCollectionIterator Tests failed\n"));
997 }
998}
999
1000#endif /* DEBUG */