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@
33 #include "kxld_dict.h"
34 #include "kxld_util.h"
37 #define STRESSNUM 10000
45 void kxld_test_log(KXLDLogSubsystem sys
, KXLDLogLevel level
,
46 const char *format
, va_list ap
, void *user_data
);
49 kxld_test_log(KXLDLogSubsystem sys __unused
, KXLDLogLevel level __unused
,
50 const char *format
, va_list ap
, void *user_data __unused
)
55 vfprintf(stderr
, format
, args
);
56 fprintf(stderr
, "\n");
61 main(int argc __unused
, char *argv
[] __unused
)
63 kern_return_t result
= KERN_SUCCESS
;
65 int a1
= 1, a2
= 3, i
= 0, j
= 0;
69 Stress stress_test
[STRESSNUM
];
71 kxld_set_logging_callback(kxld_test_log
);
73 bzero(&dict
, sizeof(dict
));
75 fprintf(stderr
, "%d: Initialize\n", ++test_num
);
76 result
= kxld_dict_init(&dict
, kxld_dict_string_hash
, kxld_dict_string_cmp
, 10);
77 assert(result
== KERN_SUCCESS
);
78 size
= kxld_dict_get_num_entries(&dict
);
81 fprintf(stderr
, "%d: Find nonexistant key\n", ++test_num
);
82 b
= kxld_dict_find(&dict
, "hi");
85 fprintf(stderr
, "%d: Insert and find\n", ++test_num
);
86 result
= kxld_dict_insert(&dict
, "hi", &a1
);
87 assert(result
== KERN_SUCCESS
);
88 b
= kxld_dict_find(&dict
, "hi");
89 assert(b
&& *(int*)b
== a1
);
90 size
= kxld_dict_get_num_entries(&dict
);
93 fprintf(stderr
, "%d: Insert same key with different values\n", ++test_num
);
94 result
= kxld_dict_insert(&dict
, "hi", &a2
);
95 assert(result
== KERN_SUCCESS
);
96 b
= kxld_dict_find(&dict
, "hi");
97 assert(b
&& *(int*)b
== a2
);
98 size
= kxld_dict_get_num_entries(&dict
);
101 fprintf(stderr
, "%d: Clear and find of nonexistant key\n", ++test_num
);
102 kxld_dict_clear(&dict
);
103 result
= kxld_dict_init(&dict
, kxld_dict_string_hash
, kxld_dict_string_cmp
, 10);
104 b
= kxld_dict_find(&dict
, "hi");
106 size
= kxld_dict_get_num_entries(&dict
);
109 fprintf(stderr
, "%d: Insert multiple keys\n", ++test_num
);
110 result
= kxld_dict_insert(&dict
, "hi", &a1
);
111 assert(result
== KERN_SUCCESS
);
112 result
= kxld_dict_insert(&dict
, "hello", &a2
);
113 assert(result
== KERN_SUCCESS
);
114 b
= kxld_dict_find(&dict
, "hi");
115 assert(result
== KERN_SUCCESS
);
116 assert(b
&& *(int*)b
== a1
);
117 b
= kxld_dict_find(&dict
, "hello");
118 assert(b
&& *(int*)b
== a2
);
119 size
= kxld_dict_get_num_entries(&dict
);
122 fprintf(stderr
, "%d: Remove keys\n", ++test_num
);
123 kxld_dict_remove(&dict
, "hi", &b
);
124 assert(b
&& *(int*)b
== a1
);
125 b
= kxld_dict_find(&dict
, "hi");
127 kxld_dict_remove(&dict
, "hi", &b
);
129 size
= kxld_dict_get_num_entries(&dict
);
132 fprintf(stderr
, "%d: Stress test - %d insertions and finds\n", ++test_num
, STRESSNUM
);
134 kxld_dict_clear(&dict
);
135 result
= kxld_dict_init(&dict
, kxld_dict_string_hash
, kxld_dict_string_cmp
, 10);
136 for (i
= 0; i
< STRESSNUM
; ++i
) {
137 int * tmp_value
= kxld_alloc(sizeof(int));
138 char * tmp_key
= kxld_alloc(sizeof(char) * (KEYLEN
+ 1));
141 for (j
= 0; j
< KEYLEN
; ++j
) {
142 tmp_key
[j
] = (rand() % 26) + 'a';
144 tmp_key
[KEYLEN
] = '\0';
146 kxld_dict_insert(&dict
, tmp_key
, tmp_value
);
147 stress_test
[i
].key
= tmp_key
;
148 stress_test
[i
].value
= tmp_value
;
151 for (i
= 0; i
< STRESSNUM
; ++i
) {
154 char * key
= stress_test
[i
].key
;
156 target_value
= *stress_test
[i
].value
;
157 tmp_value
= kxld_dict_find(&dict
, key
);
158 assert(target_value
== *(int *)tmp_value
);
160 kxld_free(stress_test
[i
].key
, sizeof(char) * (KEYLEN
+ 1));
161 kxld_free(stress_test
[i
].value
, sizeof(int));
164 fprintf(stderr
, "%d: Destroy\n", ++test_num
);
165 kxld_dict_deinit(&dict
);
167 fprintf(stderr
, "\nAll tests passed! Now check for memory leaks...\n");
169 kxld_print_memory_report();