1/*
2 * Copyright (c) 2009 Apple 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#include <assert.h>
29#include <limits.h>
30#include <stdlib.h>
31#include <strings.h>
32
33#include <mach/mach_init.h>
34
35#include "kxld_array.h"
36#include "kxld_test.h"
37#include "kxld_util.h"
38
39#define kNumStorageTestItems (u_int) (4 * PAGE_SIZE / sizeof(u_int))
40
41int
42main(int argc __unused, char *argv[] __unused)
43{
44    kern_return_t rval = KERN_FAILURE;
45    KXLDArray array;
46    u_int *item = 0;
47    u_int test_num = 0;
48    u_int idx = 0;
49    u_int titems = 0;
50    u_int storageTestItems[kNumStorageTestItems];
51    u_int i = 0;
52
53    bzero(&array, sizeof(array));
54
55    kxld_set_logging_callback(kxld_test_log);
56    kxld_set_logging_callback_data("kxld_array_test", NULL);
57
58    kxld_log(0, 0, "%d: Initialize", ++test_num);
59
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);
64
65    kxld_log(0, 0, "%d: Get item", ++test_num);
66    idx = 0;
67    item = kxld_array_get_item(&array, idx);
68    assert(item);
69    assert(item == kxld_array_get_slot(&array, idx));
70
71    idx = titems - 1;
72    item = kxld_array_get_item(&array, idx);
73    assert(item);
74    assert(item == kxld_array_get_slot(&array, idx));
75
76    idx = titems;
77    item = kxld_array_get_item(&array, idx);
78    assert(!item);
79    /* We allocated the max number of items that could be stored in a page,
80     * so get_slot() and get_item() are equivalent.
81     */
82    assert(item == kxld_array_get_slot(&array, idx));
83
84    kxld_log(0, 0, "%d: Resize", ++test_num);
85
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);
90
91    kxld_log(0, 0, "%d: Get more items", ++test_num);
92    idx = 0;
93    item = kxld_array_get_item(&array, idx);
94    assert(item);
95    assert(item == kxld_array_get_slot(&array, idx));
96
97    idx = titems - 1;
98    item = kxld_array_get_item(&array, idx);
99    assert(item);
100    assert(item == kxld_array_get_slot(&array, idx));
101
102    idx = titems;
103    item = kxld_array_get_item(&array, idx);
104    assert(!item);
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.
107     */
108    assert(item != kxld_array_get_slot(&array, idx));
109
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);
113    assert(!item);
114
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.
119     */
120    titems = (u_int) (round_page(titems * sizeof(u_int)) / sizeof(u_int));
121    assert(!item);
122    item = kxld_array_get_slot(&array, 0);
123    assert(item);
124    item = kxld_array_get_slot(&array, titems - 1);
125    assert(item);
126    item = kxld_array_get_slot(&array, titems);
127    assert(!item);
128
129    kxld_log(0, 0, "%d: Reinitialize", ++test_num);
130
131    titems = kNumStorageTestItems;
132    rval = kxld_array_init(&array, sizeof(u_int), titems);
133    assert(rval == KERN_SUCCESS);
134    assert(array.nitems == titems);
135
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);
140        assert(item);
141
142        *item = (u_int) (random() % UINT_MAX);
143        storageTestItems[i] = *item;
144    }
145
146    for (i = 0; i < titems; ++i) {
147        item = kxld_array_get_item(&array, i);
148        assert(item);
149        assert(*item == storageTestItems[i]);
150    }
151
152    (void) kxld_array_deinit(&array);
153
154    kxld_log(0, 0, " ");
155    kxld_log(0, 0, "All tests passed!  Now check for memory leaks...");
156
157    kxld_print_memory_report();
158
159    return 0;
160}
161