]>
git.saurik.com Git - apple/xnu.git/blob - libkern/kxld/tests/kxld_array_test.c
2 * Copyright (c) 2009 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_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. 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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
33 #include <mach/mach_init.h>
35 #include "kxld_array.h"
36 #include "kxld_test.h"
37 #include "kxld_util.h"
39 #define kNumStorageTestItems (u_int) (4 * PAGE_SIZE / sizeof(u_int))
42 main(int argc __unused
, char *argv
[] __unused
)
44 kern_return_t rval
= KERN_FAILURE
;
50 u_int storageTestItems
[kNumStorageTestItems
];
53 bzero(&array
, sizeof(array
));
55 kxld_set_logging_callback(kxld_test_log
);
56 kxld_set_logging_callback_data("kxld_array_test", NULL
);
58 kxld_log(0, 0, "%d: Initialize", ++test_num
);
60 titems
= PAGE_SIZE
/ sizeof(u_int
);
61 rval
= kxld_array_init(&array
, sizeof(u_int
), titems
);
62 assert(rval
== KERN_SUCCESS
);
63 assert(array
.nitems
== titems
);
65 kxld_log(0, 0, "%d: Get item", ++test_num
);
67 item
= kxld_array_get_item(&array
, idx
);
69 assert(item
== kxld_array_get_slot(&array
, idx
));
72 item
= kxld_array_get_item(&array
, idx
);
74 assert(item
== kxld_array_get_slot(&array
, idx
));
77 item
= kxld_array_get_item(&array
, idx
);
79 /* We allocated the max number of items that could be stored in a page,
80 * so get_slot() and get_item() are equivalent.
82 assert(item
== kxld_array_get_slot(&array
, idx
));
84 kxld_log(0, 0, "%d: Resize", ++test_num
);
86 titems
= 2 * PAGE_SIZE
/ sizeof(u_int
) + 100;
87 rval
= kxld_array_resize(&array
, titems
);
88 assert(rval
== KERN_SUCCESS
);
89 assert(array
.nitems
== titems
);
91 kxld_log(0, 0, "%d: Get more items", ++test_num
);
93 item
= kxld_array_get_item(&array
, idx
);
95 assert(item
== kxld_array_get_slot(&array
, idx
));
98 item
= kxld_array_get_item(&array
, idx
);
100 assert(item
== kxld_array_get_slot(&array
, idx
));
103 item
= kxld_array_get_item(&array
, idx
);
105 /* We allocated fewer items than could fit in a page, so get_slot() will
106 * return items even when get_item() does not. See below for details.
108 assert(item
!= kxld_array_get_slot(&array
, idx
));
110 kxld_log(0, 0, "%d: Clear and attempt to get an item", ++test_num
);
111 (void) kxld_array_clear(&array
);
112 item
= kxld_array_get_item(&array
, 0);
115 kxld_log(0, 0, "%d: Get slot", ++test_num
);
116 /* The array allocates its internal storage in pages. Because get_slot()
117 * fetches items based on the allocated size, not the logical size, we
118 * calculate the max items get_slot() can retrieve based on page size.
120 titems
= (u_int
) (round_page(titems
* sizeof(u_int
)) / sizeof(u_int
));
122 item
= kxld_array_get_slot(&array
, 0);
124 item
= kxld_array_get_slot(&array
, titems
- 1);
126 item
= kxld_array_get_slot(&array
, titems
);
129 kxld_log(0, 0, "%d: Reinitialize", ++test_num
);
131 titems
= kNumStorageTestItems
;
132 rval
= kxld_array_init(&array
, sizeof(u_int
), titems
);
133 assert(rval
== KERN_SUCCESS
);
134 assert(array
.nitems
== titems
);
136 kxld_log(0, 0, "%d: Storage test - %d insertions and finds",
137 ++test_num
, kNumStorageTestItems
);
138 for (i
= 0; i
< titems
; ++i
) {
139 item
= kxld_array_get_item(&array
, i
);
142 *item
= (u_int
) (random() % UINT_MAX
);
143 storageTestItems
[i
] = *item
;
146 for (i
= 0; i
< titems
; ++i
) {
147 item
= kxld_array_get_item(&array
, i
);
149 assert(*item
== storageTestItems
[i
]);
152 (void) kxld_array_deinit(&array
);
155 kxld_log(0, 0, "All tests passed! Now check for memory leaks...");
157 kxld_print_memory_report();