]>
git.saurik.com Git - apple/xnu.git/blob - libkern/kxld/tests/kxld_dict_test.c
2 * Copyright (c) 2007-2008 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@
32 #include "kxld_dict.h"
33 #include "kxld_test.h"
36 #define STRESSNUM 10000
44 main(int argc __unused
, char *argv
[] __unused
)
46 kern_return_t result
= KERN_SUCCESS
;
48 int a1
= 1, a2
= 3, i
= 0, j
= 0;
52 Stress stress_test
[STRESSNUM
];
54 kxld_set_logging_callback(kxld_test_log
);
55 kxld_set_logging_callback_data("kxld_dict_test", NULL
);
57 bzero(&dict
, sizeof(dict
));
59 kxld_log(0, 0, "%d: Initialize", ++test_num
);
60 result
= kxld_dict_init(&dict
, kxld_dict_string_hash
, kxld_dict_string_cmp
, 10);
61 assert(result
== KERN_SUCCESS
);
62 size
= kxld_dict_get_num_entries(&dict
);
65 kxld_log(0, 0, "%d: Find nonexistant key", ++test_num
);
66 b
= kxld_dict_find(&dict
, "hi");
69 kxld_log(0, 0, "%d: Insert and find", ++test_num
);
70 result
= kxld_dict_insert(&dict
, "hi", &a1
);
71 assert(result
== KERN_SUCCESS
);
72 b
= kxld_dict_find(&dict
, "hi");
73 assert(b
&& *(int*)b
== a1
);
74 size
= kxld_dict_get_num_entries(&dict
);
77 kxld_log(0, 0, "%d: Insert same key with different values", ++test_num
);
78 result
= kxld_dict_insert(&dict
, "hi", &a2
);
79 assert(result
== KERN_SUCCESS
);
80 b
= kxld_dict_find(&dict
, "hi");
81 assert(b
&& *(int*)b
== a2
);
82 size
= kxld_dict_get_num_entries(&dict
);
85 kxld_log(0, 0, "%d: Clear and find of nonexistant key", ++test_num
);
86 kxld_dict_clear(&dict
);
87 result
= kxld_dict_init(&dict
, kxld_dict_string_hash
, kxld_dict_string_cmp
, 10);
88 assert(result
== KERN_SUCCESS
);
89 b
= kxld_dict_find(&dict
, "hi");
91 size
= kxld_dict_get_num_entries(&dict
);
94 kxld_log(0, 0, "%d: Insert multiple keys", ++test_num
);
95 result
= kxld_dict_insert(&dict
, "hi", &a1
);
96 assert(result
== KERN_SUCCESS
);
97 result
= kxld_dict_insert(&dict
, "hello", &a2
);
98 assert(result
== KERN_SUCCESS
);
99 b
= kxld_dict_find(&dict
, "hi");
100 assert(result
== KERN_SUCCESS
);
101 assert(b
&& *(int*)b
== a1
);
102 b
= kxld_dict_find(&dict
, "hello");
103 assert(b
&& *(int*)b
== a2
);
104 size
= kxld_dict_get_num_entries(&dict
);
107 kxld_log(0, 0, "%d: Remove keys", ++test_num
);
108 kxld_dict_remove(&dict
, "hi", &b
);
109 assert(b
&& *(int*)b
== a1
);
110 b
= kxld_dict_find(&dict
, "hi");
112 kxld_dict_remove(&dict
, "hi", &b
);
114 size
= kxld_dict_get_num_entries(&dict
);
117 kxld_log(0, 0, "%d: Stress test - %d insertions and finds", ++test_num
, STRESSNUM
);
119 kxld_dict_clear(&dict
);
120 result
= kxld_dict_init(&dict
, kxld_dict_string_hash
, kxld_dict_string_cmp
, 10);
121 assert(result
== KERN_SUCCESS
);
122 for (i
= 0; i
< STRESSNUM
; ++i
) {
123 int * tmp_value
= kxld_alloc(sizeof(int));
124 char * tmp_key
= kxld_alloc(sizeof(char) * (KEYLEN
+ 1));
127 for (j
= 0; j
< KEYLEN
; ++j
) {
128 tmp_key
[j
] = (random() % 26) + 'a';
130 tmp_key
[KEYLEN
] = '\0';
132 kxld_dict_insert(&dict
, tmp_key
, tmp_value
);
133 stress_test
[i
].key
= tmp_key
;
134 stress_test
[i
].value
= tmp_value
;
137 for (i
= 0; i
< STRESSNUM
; ++i
) {
140 char * key
= stress_test
[i
].key
;
142 target_value
= *stress_test
[i
].value
;
143 tmp_value
= kxld_dict_find(&dict
, key
);
144 assert(target_value
== *(int *)tmp_value
);
146 kxld_free(stress_test
[i
].key
, sizeof(char) * (KEYLEN
+ 1));
147 kxld_free(stress_test
[i
].value
, sizeof(int));
150 kxld_log(0, 0, "%d: Destroy", ++test_num
);
151 kxld_dict_deinit(&dict
);
153 kxld_log(0, 0, "\nAll tests passed! Now check for memory leaks...");
155 kxld_print_memory_report();