]> git.saurik.com Git - apple/xnu.git/blob - libkern/kxld/tests/kxld_dict_test.c
xnu-1456.1.26.tar.gz
[apple/xnu.git] / libkern / kxld / tests / kxld_dict_test.c
1 /*
2 * Copyright (c) 2007-2008 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 <stdlib.h>
30 #include <stdio.h>
31 #include <strings.h>
32
33 #include "kxld_dict.h"
34 #include "kxld_util.h"
35
36 #define KEYLEN 40
37 #define STRESSNUM 10000
38
39 typedef struct {
40 char * key;
41 int * value;
42 } Stress;
43
44
45 void kxld_test_log(KXLDLogSubsystem sys, KXLDLogLevel level,
46 const char *format, va_list ap, void *user_data);
47
48 void
49 kxld_test_log(KXLDLogSubsystem sys __unused, KXLDLogLevel level __unused,
50 const char *format, va_list ap, void *user_data __unused)
51 {
52 va_list args;
53
54 va_copy(args, ap);
55 vfprintf(stderr, format, args);
56 fprintf(stderr, "\n");
57 va_end(args);
58 }
59
60 int
61 main(int argc __unused, char *argv[] __unused)
62 {
63 kern_return_t result = KERN_SUCCESS;
64 KXLDDict dict;
65 int a1 = 1, a2 = 3, i = 0, j = 0;
66 void * b = NULL;
67 u_int test_num = 0;
68 u_long size = 0;
69 Stress stress_test[STRESSNUM];
70
71 kxld_set_logging_callback(kxld_test_log);
72
73 bzero(&dict, sizeof(dict));
74
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);
79 assert(size == 0);
80
81 fprintf(stderr, "%d: Find nonexistant key\n", ++test_num);
82 b = kxld_dict_find(&dict, "hi");
83 assert(b == NULL);
84
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);
91 assert(size == 1);
92
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);
99 assert(size == 1);
100
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");
105 assert(b == NULL);
106 size = kxld_dict_get_num_entries(&dict);
107 assert(size == 0);
108
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);
120 assert(size == 2);
121
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");
126 assert(b == NULL);
127 kxld_dict_remove(&dict, "hi", &b);
128 assert(b == NULL);
129 size = kxld_dict_get_num_entries(&dict);
130 assert(size == 1);
131
132 fprintf(stderr, "%d: Stress test - %d insertions and finds\n", ++test_num, STRESSNUM);
133
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));
139
140 *tmp_value = i;
141 for (j = 0; j < KEYLEN; ++j) {
142 tmp_key[j] = (rand() % 26) + 'a';
143 }
144 tmp_key[KEYLEN] = '\0';
145
146 kxld_dict_insert(&dict, tmp_key, tmp_value);
147 stress_test[i].key = tmp_key;
148 stress_test[i].value = tmp_value;
149 }
150
151 for (i = 0; i < STRESSNUM; ++i) {
152 int target_value;
153 void * tmp_value;
154 char * key = stress_test[i].key;
155
156 target_value = *stress_test[i].value;
157 tmp_value = kxld_dict_find(&dict, key);
158 assert(target_value == *(int *)tmp_value);
159
160 kxld_free(stress_test[i].key, sizeof(char) * (KEYLEN + 1));
161 kxld_free(stress_test[i].value, sizeof(int));
162 }
163
164 fprintf(stderr, "%d: Destroy\n", ++test_num);
165 kxld_dict_deinit(&dict);
166
167 fprintf(stderr, "\nAll tests passed! Now check for memory leaks...\n");
168
169 kxld_print_memory_report();
170
171 return 0;
172 }
173