upload http
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr / test / testhash.c
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "test_apr.h"
18 #include "apr.h"
19 #include "apr_strings.h"
20 #include "apr_general.h"
21 #include "apr_pools.h"
22 #include "apr_hash.h"
23
24 static void dump_hash(apr_pool_t *p, apr_hash_t *h, char *str) 
25 {
26     apr_hash_index_t *hi;
27     char *val, *key;
28     apr_ssize_t len;
29     int i = 0;
30
31     str[0] = '\0';
32
33     for (hi = apr_hash_first(p, h); hi; hi = apr_hash_next(hi)) {
34         apr_hash_this(hi,(void*) &key, &len, (void*) &val);
35         apr_snprintf(str, 8196, "%sKey %s (%" APR_SSIZE_T_FMT ") Value %s\n", 
36                      str, key, len, val);
37         i++;
38     }
39     apr_snprintf(str, 8196, "%s#entries %d\n", str, i);
40 }
41
42 static void sum_hash(apr_pool_t *p, apr_hash_t *h, int *pcount, int *keySum, int *valSum) 
43 {
44     apr_hash_index_t *hi;
45     void *val, *key;
46     int count = 0;
47
48     *keySum = 0;
49     *valSum = 0;
50     *pcount = 0;
51     for (hi = apr_hash_first(p, h); hi; hi = apr_hash_next(hi)) {
52         apr_hash_this(hi, (void*)&key, NULL, &val);
53         *valSum += *(int *)val;
54         *keySum += *(int *)key;
55         count++;
56     }
57     *pcount=count;
58 }
59
60 static void hash_make(CuTest *tc)
61 {
62     apr_hash_t *h = NULL;
63
64     h = apr_hash_make(p);
65     CuAssertPtrNotNull(tc, h);
66 }
67
68 static void hash_set(CuTest *tc)
69 {
70     apr_hash_t *h = NULL;
71     char *result = NULL;
72
73     h = apr_hash_make(p);
74     CuAssertPtrNotNull(tc, h);
75
76     apr_hash_set(h, "key", APR_HASH_KEY_STRING, "value");
77     result = apr_hash_get(h, "key", APR_HASH_KEY_STRING);
78     CuAssertStrEquals(tc, "value", result);
79 }
80
81 static void hash_reset(CuTest *tc)
82 {
83     apr_hash_t *h = NULL;
84     char *result = NULL;
85
86     h = apr_hash_make(p);
87     CuAssertPtrNotNull(tc, h);
88
89     apr_hash_set(h, "key", APR_HASH_KEY_STRING, "value");
90     result = apr_hash_get(h, "key", APR_HASH_KEY_STRING);
91     CuAssertStrEquals(tc, "value", result);
92
93     apr_hash_set(h, "key", APR_HASH_KEY_STRING, "new");
94     result = apr_hash_get(h, "key", APR_HASH_KEY_STRING);
95     CuAssertStrEquals(tc, "new", result);
96 }
97
98 static void same_value(CuTest *tc)
99 {
100     apr_hash_t *h = NULL;
101     char *result = NULL;
102
103     h = apr_hash_make(p);
104     CuAssertPtrNotNull(tc, h);
105
106     apr_hash_set(h, "same1", APR_HASH_KEY_STRING, "same");
107     result = apr_hash_get(h, "same1", APR_HASH_KEY_STRING);
108     CuAssertStrEquals(tc, "same", result);
109
110     apr_hash_set(h, "same2", APR_HASH_KEY_STRING, "same");
111     result = apr_hash_get(h, "same2", APR_HASH_KEY_STRING);
112     CuAssertStrEquals(tc, "same", result);
113 }
114
115 static void key_space(CuTest *tc)
116 {
117     apr_hash_t *h = NULL;
118     char *result = NULL;
119
120     h = apr_hash_make(p);
121     CuAssertPtrNotNull(tc, h);
122
123     apr_hash_set(h, "key with space", APR_HASH_KEY_STRING, "value");
124     result = apr_hash_get(h, "key with space", APR_HASH_KEY_STRING);
125     CuAssertStrEquals(tc, "value", result);
126 }
127
128 /* This is kind of a hack, but I am just keeping an existing test.  This is
129  * really testing apr_hash_first, apr_hash_next, and apr_hash_this which 
130  * should be tested in three separate tests, but this will do for now.
131  */
132 static void hash_traverse(CuTest *tc)
133 {
134     apr_hash_t *h;
135     char str[8196];
136
137     h = apr_hash_make(p);
138     CuAssertPtrNotNull(tc, h);
139
140     apr_hash_set(h, "OVERWRITE", APR_HASH_KEY_STRING, "should not see this");
141     apr_hash_set(h, "FOO3", APR_HASH_KEY_STRING, "bar3");
142     apr_hash_set(h, "FOO3", APR_HASH_KEY_STRING, "bar3");
143     apr_hash_set(h, "FOO1", APR_HASH_KEY_STRING, "bar1");
144     apr_hash_set(h, "FOO2", APR_HASH_KEY_STRING, "bar2");
145     apr_hash_set(h, "FOO4", APR_HASH_KEY_STRING, "bar4");
146     apr_hash_set(h, "SAME1", APR_HASH_KEY_STRING, "same");
147     apr_hash_set(h, "SAME2", APR_HASH_KEY_STRING, "same");
148     apr_hash_set(h, "OVERWRITE", APR_HASH_KEY_STRING, "Overwrite key");
149
150     dump_hash(p, h, str);
151     CuAssertStrEquals(tc, "Key FOO1 (4) Value bar1\n"
152                           "Key FOO2 (4) Value bar2\n"
153                           "Key OVERWRITE (9) Value Overwrite key\n"
154                           "Key FOO3 (4) Value bar3\n"
155                           "Key SAME1 (5) Value same\n"
156                           "Key FOO4 (4) Value bar4\n"
157                           "Key SAME2 (5) Value same\n"
158                           "#entries 7\n", str);
159 }
160
161 /* This is kind of a hack, but I am just keeping an existing test.  This is
162  * really testing apr_hash_first, apr_hash_next, and apr_hash_this which 
163  * should be tested in three separate tests, but this will do for now.
164  */
165 static void summation_test(CuTest *tc)
166 {
167     apr_hash_t *h;
168     int sumKeys, sumVal, trySumKey, trySumVal;
169     int i, j, *val, *key;
170
171     h =apr_hash_make(p);
172     CuAssertPtrNotNull(tc, h);
173
174     sumKeys = 0;
175     sumVal = 0;
176     trySumKey = 0;
177     trySumVal = 0;
178
179     for (i = 0; i < 100; i++) {
180         j = i * 10 + 1;
181         sumKeys += j;
182         sumVal += i;
183         key = apr_palloc(p, sizeof(int));
184         *key = j;
185         val = apr_palloc(p, sizeof(int));
186         *val = i;
187         apr_hash_set(h, key, sizeof(int), val);
188     }
189
190     sum_hash(p, h, &i, &trySumKey, &trySumVal);
191     CuAssertIntEquals(tc, 100, i);
192     CuAssertIntEquals(tc, sumVal, trySumVal);
193     CuAssertIntEquals(tc, sumKeys, trySumKey);
194 }
195
196 static void delete_key(CuTest *tc)
197 {
198     apr_hash_t *h = NULL;
199     char *result = NULL;
200
201     h = apr_hash_make(p);
202     CuAssertPtrNotNull(tc, h);
203
204     apr_hash_set(h, "key", APR_HASH_KEY_STRING, "value");
205     apr_hash_set(h, "key2", APR_HASH_KEY_STRING, "value2");
206
207     result = apr_hash_get(h, "key", APR_HASH_KEY_STRING);
208     CuAssertStrEquals(tc, "value", result);
209
210     result = apr_hash_get(h, "key2", APR_HASH_KEY_STRING);
211     CuAssertStrEquals(tc, "value2", result);
212
213     apr_hash_set(h, "key", APR_HASH_KEY_STRING, NULL);
214
215     result = apr_hash_get(h, "key", APR_HASH_KEY_STRING);
216     CuAssertPtrEquals(tc, NULL, result);
217
218     result = apr_hash_get(h, "key2", APR_HASH_KEY_STRING);
219     CuAssertStrEquals(tc, "value2", result);
220 }
221
222 static void hash_count_0(CuTest *tc)
223 {
224     apr_hash_t *h = NULL;
225     int count;
226
227     h = apr_hash_make(p);
228     CuAssertPtrNotNull(tc, h);
229
230     count = apr_hash_count(h);
231     CuAssertIntEquals(tc, 0, count);
232 }
233
234 static void hash_count_1(CuTest *tc)
235 {
236     apr_hash_t *h = NULL;
237     int count;
238
239     h = apr_hash_make(p);
240     CuAssertPtrNotNull(tc, h);
241
242     apr_hash_set(h, "key", APR_HASH_KEY_STRING, "value");
243
244     count = apr_hash_count(h);
245     CuAssertIntEquals(tc, 1, count);
246 }
247
248 static void hash_count_5(CuTest *tc)
249 {
250     apr_hash_t *h = NULL;
251     int count;
252
253     h = apr_hash_make(p);
254     CuAssertPtrNotNull(tc, h);
255
256     apr_hash_set(h, "key1", APR_HASH_KEY_STRING, "value1");
257     apr_hash_set(h, "key2", APR_HASH_KEY_STRING, "value2");
258     apr_hash_set(h, "key3", APR_HASH_KEY_STRING, "value3");
259     apr_hash_set(h, "key4", APR_HASH_KEY_STRING, "value4");
260     apr_hash_set(h, "key5", APR_HASH_KEY_STRING, "value5");
261
262     count = apr_hash_count(h);
263     CuAssertIntEquals(tc, 5, count);
264 }
265
266 static void overlay_empty(CuTest *tc)
267 {
268     apr_hash_t *base = NULL;
269     apr_hash_t *overlay = NULL;
270     apr_hash_t *result = NULL;
271     int count;
272     char str[8196];
273
274     base = apr_hash_make(p);
275     overlay = apr_hash_make(p);
276     CuAssertPtrNotNull(tc, base);
277     CuAssertPtrNotNull(tc, overlay);
278
279     apr_hash_set(base, "key1", APR_HASH_KEY_STRING, "value1");
280     apr_hash_set(base, "key2", APR_HASH_KEY_STRING, "value2");
281     apr_hash_set(base, "key3", APR_HASH_KEY_STRING, "value3");
282     apr_hash_set(base, "key4", APR_HASH_KEY_STRING, "value4");
283     apr_hash_set(base, "key5", APR_HASH_KEY_STRING, "value5");
284
285     result = apr_hash_overlay(p, overlay, base);
286
287     count = apr_hash_count(result);
288     CuAssertIntEquals(tc, 5, count);
289
290     dump_hash(p, result, str);
291     CuAssertStrEquals(tc, "Key key1 (4) Value value1\n"
292                           "Key key2 (4) Value value2\n"
293                           "Key key3 (4) Value value3\n"
294                           "Key key4 (4) Value value4\n"
295                           "Key key5 (4) Value value5\n"
296                           "#entries 5\n", str);
297 }
298
299 static void overlay_2unique(CuTest *tc)
300 {
301     apr_hash_t *base = NULL;
302     apr_hash_t *overlay = NULL;
303     apr_hash_t *result = NULL;
304     int count;
305     char str[8196];
306
307     base = apr_hash_make(p);
308     overlay = apr_hash_make(p);
309     CuAssertPtrNotNull(tc, base);
310     CuAssertPtrNotNull(tc, overlay);
311
312     apr_hash_set(base, "base1", APR_HASH_KEY_STRING, "value1");
313     apr_hash_set(base, "base2", APR_HASH_KEY_STRING, "value2");
314     apr_hash_set(base, "base3", APR_HASH_KEY_STRING, "value3");
315     apr_hash_set(base, "base4", APR_HASH_KEY_STRING, "value4");
316     apr_hash_set(base, "base5", APR_HASH_KEY_STRING, "value5");
317
318     apr_hash_set(overlay, "overlay1", APR_HASH_KEY_STRING, "value1");
319     apr_hash_set(overlay, "overlay2", APR_HASH_KEY_STRING, "value2");
320     apr_hash_set(overlay, "overlay3", APR_HASH_KEY_STRING, "value3");
321     apr_hash_set(overlay, "overlay4", APR_HASH_KEY_STRING, "value4");
322     apr_hash_set(overlay, "overlay5", APR_HASH_KEY_STRING, "value5");
323
324     result = apr_hash_overlay(p, overlay, base);
325
326     count = apr_hash_count(result);
327     CuAssertIntEquals(tc, 10, count);
328
329     dump_hash(p, result, str);
330     /* I don't know why these are out of order, but they are.  I would probably
331      * consider this a bug, but others should comment.
332      */
333     CuAssertStrEquals(tc, "Key base5 (5) Value value5\n"
334                           "Key overlay1 (8) Value value1\n"
335                           "Key overlay2 (8) Value value2\n"
336                           "Key overlay3 (8) Value value3\n"
337                           "Key overlay4 (8) Value value4\n"
338                           "Key overlay5 (8) Value value5\n"
339                           "Key base1 (5) Value value1\n"
340                           "Key base2 (5) Value value2\n"
341                           "Key base3 (5) Value value3\n"
342                           "Key base4 (5) Value value4\n"
343                           "#entries 10\n", str);
344 }
345
346 static void overlay_same(CuTest *tc)
347 {
348     apr_hash_t *base = NULL;
349     apr_hash_t *result = NULL;
350     int count;
351     char str[8196];
352
353     base = apr_hash_make(p);
354     CuAssertPtrNotNull(tc, base);
355
356     apr_hash_set(base, "base1", APR_HASH_KEY_STRING, "value1");
357     apr_hash_set(base, "base2", APR_HASH_KEY_STRING, "value2");
358     apr_hash_set(base, "base3", APR_HASH_KEY_STRING, "value3");
359     apr_hash_set(base, "base4", APR_HASH_KEY_STRING, "value4");
360     apr_hash_set(base, "base5", APR_HASH_KEY_STRING, "value5");
361
362     result = apr_hash_overlay(p, base, base);
363
364     count = apr_hash_count(result);
365     CuAssertIntEquals(tc, 5, count);
366
367     dump_hash(p, result, str);
368     /* I don't know why these are out of order, but they are.  I would probably
369      * consider this a bug, but others should comment.
370      */
371     CuAssertStrEquals(tc, "Key base5 (5) Value value5\n"
372                           "Key base1 (5) Value value1\n"
373                           "Key base2 (5) Value value2\n"
374                           "Key base3 (5) Value value3\n"
375                           "Key base4 (5) Value value4\n"
376                           "#entries 5\n", str);
377 }
378         
379 CuSuite *testhash(void)
380 {
381     CuSuite *suite = CuSuiteNew("Hash");
382
383     SUITE_ADD_TEST(suite, hash_make);
384     SUITE_ADD_TEST(suite, hash_set);
385     SUITE_ADD_TEST(suite, hash_reset);
386     SUITE_ADD_TEST(suite, same_value);
387     SUITE_ADD_TEST(suite, key_space);
388     SUITE_ADD_TEST(suite, delete_key);
389
390     SUITE_ADD_TEST(suite, hash_count_0);
391     SUITE_ADD_TEST(suite, hash_count_1);
392     SUITE_ADD_TEST(suite, hash_count_5);
393
394     SUITE_ADD_TEST(suite, hash_traverse);
395     SUITE_ADD_TEST(suite, summation_test);
396
397     SUITE_ADD_TEST(suite, overlay_empty);
398     SUITE_ADD_TEST(suite, overlay_2unique);
399     SUITE_ADD_TEST(suite, overlay_same);
400
401     return suite;
402 }
403