]>
Commit | Line | Data |
---|---|---|
b0d623f7 A |
1 | /* |
2 | * Copyright (c) 2007-2008 Apple Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ | |
0a7de745 | 5 | * |
b0d623f7 A |
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. | |
0a7de745 | 14 | * |
b0d623f7 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
0a7de745 | 17 | * |
b0d623f7 A |
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. | |
0a7de745 | 25 | * |
b0d623f7 A |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
27 | */ | |
28 | #include <assert.h> | |
29 | #include <stdlib.h> | |
b0d623f7 A |
30 | #include <strings.h> |
31 | ||
32 | #include "kxld_dict.h" | |
6d2010ae | 33 | #include "kxld_test.h" |
b0d623f7 A |
34 | |
35 | #define KEYLEN 40 | |
36 | #define STRESSNUM 10000 | |
37 | ||
38 | typedef struct { | |
0a7de745 A |
39 | char * key; |
40 | int * value; | |
b0d623f7 A |
41 | } Stress; |
42 | ||
0a7de745 | 43 | int |
b0d623f7 A |
44 | main(int argc __unused, char *argv[] __unused) |
45 | { | |
0a7de745 A |
46 | kern_return_t result = KERN_SUCCESS; |
47 | KXLDDict dict; | |
48 | int a1 = 1, a2 = 3, i = 0, j = 0; | |
49 | void * b = NULL; | |
50 | u_int test_num = 0; | |
51 | u_long size = 0; | |
52 | Stress stress_test[STRESSNUM]; | |
53 | ||
54 | kxld_set_logging_callback(kxld_test_log); | |
55 | kxld_set_logging_callback_data("kxld_dict_test", NULL); | |
56 | ||
57 | bzero(&dict, sizeof(dict)); | |
58 | ||
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); | |
63 | assert(size == 0); | |
64 | ||
65 | kxld_log(0, 0, "%d: Find nonexistant key", ++test_num); | |
66 | b = kxld_dict_find(&dict, "hi"); | |
67 | assert(b == NULL); | |
68 | ||
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); | |
75 | assert(size == 1); | |
76 | ||
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); | |
83 | assert(size == 1); | |
84 | ||
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"); | |
90 | assert(b == NULL); | |
91 | size = kxld_dict_get_num_entries(&dict); | |
92 | assert(size == 0); | |
93 | ||
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); | |
105 | assert(size == 2); | |
b0d623f7 | 106 | |
0a7de745 A |
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"); | |
111 | assert(b == NULL); | |
112 | kxld_dict_remove(&dict, "hi", &b); | |
113 | assert(b == NULL); | |
114 | size = kxld_dict_get_num_entries(&dict); | |
115 | assert(size == 1); | |
116 | ||
117 | kxld_log(0, 0, "%d: Stress test - %d insertions and finds", ++test_num, STRESSNUM); | |
118 | ||
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)); | |
125 | ||
126 | *tmp_value = i; | |
127 | for (j = 0; j < KEYLEN; ++j) { | |
128 | tmp_key[j] = (random() % 26) + 'a'; | |
129 | } | |
130 | tmp_key[KEYLEN] = '\0'; | |
131 | ||
132 | kxld_dict_insert(&dict, tmp_key, tmp_value); | |
133 | stress_test[i].key = tmp_key; | |
134 | stress_test[i].value = tmp_value; | |
135 | } | |
136 | ||
137 | for (i = 0; i < STRESSNUM; ++i) { | |
138 | int target_value; | |
139 | void * tmp_value; | |
140 | char * key = stress_test[i].key; | |
141 | ||
142 | target_value = *stress_test[i].value; | |
143 | tmp_value = kxld_dict_find(&dict, key); | |
144 | assert(target_value == *(int *)tmp_value); | |
145 | ||
146 | kxld_free(stress_test[i].key, sizeof(char) * (KEYLEN + 1)); | |
147 | kxld_free(stress_test[i].value, sizeof(int)); | |
148 | } | |
149 | ||
150 | kxld_log(0, 0, "%d: Destroy", ++test_num); | |
151 | kxld_dict_deinit(&dict); | |
152 | ||
153 | kxld_log(0, 0, "\nAll tests passed! Now check for memory leaks..."); | |
154 | ||
155 | kxld_print_memory_report(); | |
156 | ||
157 | return 0; | |
158 | } |