1
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
19
#include "apr_strings.h"
20
#include "apr_general.h"
21
#include "apr_pools.h"
24
static void dump_hash(apr_pool_t *p, apr_hash_t *h, char *str)
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",
39
apr_snprintf(str, 8196, "%s#entries %d\n", str, i);
42
static void sum_hash(apr_pool_t *p, apr_hash_t *h, int *pcount, int *keySum, int *valSum)
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;
60
static void hash_make(abts_case *tc, void *data)
65
ABTS_PTR_NOTNULL(tc, h);
68
static void hash_set(abts_case *tc, void *data)
74
ABTS_PTR_NOTNULL(tc, h);
76
apr_hash_set(h, "key", APR_HASH_KEY_STRING, "value");
77
result = apr_hash_get(h, "key", APR_HASH_KEY_STRING);
78
ABTS_STR_EQUAL(tc, "value", result);
81
static void hash_reset(abts_case *tc, void *data)
87
ABTS_PTR_NOTNULL(tc, h);
89
apr_hash_set(h, "key", APR_HASH_KEY_STRING, "value");
90
result = apr_hash_get(h, "key", APR_HASH_KEY_STRING);
91
ABTS_STR_EQUAL(tc, "value", result);
93
apr_hash_set(h, "key", APR_HASH_KEY_STRING, "new");
94
result = apr_hash_get(h, "key", APR_HASH_KEY_STRING);
95
ABTS_STR_EQUAL(tc, "new", result);
98
static void same_value(abts_case *tc, void *data)
100
apr_hash_t *h = NULL;
103
h = apr_hash_make(p);
104
ABTS_PTR_NOTNULL(tc, h);
106
apr_hash_set(h, "same1", APR_HASH_KEY_STRING, "same");
107
result = apr_hash_get(h, "same1", APR_HASH_KEY_STRING);
108
ABTS_STR_EQUAL(tc, "same", result);
110
apr_hash_set(h, "same2", APR_HASH_KEY_STRING, "same");
111
result = apr_hash_get(h, "same2", APR_HASH_KEY_STRING);
112
ABTS_STR_EQUAL(tc, "same", result);
115
static unsigned int hash_custom( const char *key, apr_ssize_t *klen)
117
unsigned int hash = 0;
120
hash = hash * 33 + key[ *klen ];
125
static void same_value_custom(abts_case *tc, void *data)
127
apr_hash_t *h = NULL;
130
h = apr_hash_make_custom(p, hash_custom);
131
ABTS_PTR_NOTNULL(tc, h);
133
apr_hash_set(h, "same1", 5, "same");
134
result = apr_hash_get(h, "same1", 5);
135
ABTS_STR_EQUAL(tc, "same", result);
137
apr_hash_set(h, "same2", 5, "same");
138
result = apr_hash_get(h, "same2", 5);
139
ABTS_STR_EQUAL(tc, "same", result);
142
static void key_space(abts_case *tc, void *data)
144
apr_hash_t *h = NULL;
147
h = apr_hash_make(p);
148
ABTS_PTR_NOTNULL(tc, h);
150
apr_hash_set(h, "key with space", APR_HASH_KEY_STRING, "value");
151
result = apr_hash_get(h, "key with space", APR_HASH_KEY_STRING);
152
ABTS_STR_EQUAL(tc, "value", result);
155
/* This is kind of a hack, but I am just keeping an existing test. This is
156
* really testing apr_hash_first, apr_hash_next, and apr_hash_this which
157
* should be tested in three separate tests, but this will do for now.
159
static void hash_traverse(abts_case *tc, void *data)
164
h = apr_hash_make(p);
165
ABTS_PTR_NOTNULL(tc, h);
167
apr_hash_set(h, "OVERWRITE", APR_HASH_KEY_STRING, "should not see this");
168
apr_hash_set(h, "FOO3", APR_HASH_KEY_STRING, "bar3");
169
apr_hash_set(h, "FOO3", APR_HASH_KEY_STRING, "bar3");
170
apr_hash_set(h, "FOO1", APR_HASH_KEY_STRING, "bar1");
171
apr_hash_set(h, "FOO2", APR_HASH_KEY_STRING, "bar2");
172
apr_hash_set(h, "FOO4", APR_HASH_KEY_STRING, "bar4");
173
apr_hash_set(h, "SAME1", APR_HASH_KEY_STRING, "same");
174
apr_hash_set(h, "SAME2", APR_HASH_KEY_STRING, "same");
175
apr_hash_set(h, "OVERWRITE", APR_HASH_KEY_STRING, "Overwrite key");
177
dump_hash(p, h, str);
178
ABTS_STR_EQUAL(tc, "Key FOO1 (4) Value bar1\n"
179
"Key FOO2 (4) Value bar2\n"
180
"Key OVERWRITE (9) Value Overwrite key\n"
181
"Key FOO3 (4) Value bar3\n"
182
"Key SAME1 (5) Value same\n"
183
"Key FOO4 (4) Value bar4\n"
184
"Key SAME2 (5) Value same\n"
185
"#entries 7\n", str);
188
/* This is kind of a hack, but I am just keeping an existing test. This is
189
* really testing apr_hash_first, apr_hash_next, and apr_hash_this which
190
* should be tested in three separate tests, but this will do for now.
192
static void summation_test(abts_case *tc, void *data)
195
int sumKeys, sumVal, trySumKey, trySumVal;
196
int i, j, *val, *key;
199
ABTS_PTR_NOTNULL(tc, h);
206
for (i = 0; i < 100; i++) {
210
key = apr_palloc(p, sizeof(int));
212
val = apr_palloc(p, sizeof(int));
214
apr_hash_set(h, key, sizeof(int), val);
217
sum_hash(p, h, &i, &trySumKey, &trySumVal);
218
ABTS_INT_EQUAL(tc, 100, i);
219
ABTS_INT_EQUAL(tc, sumVal, trySumVal);
220
ABTS_INT_EQUAL(tc, sumKeys, trySumKey);
223
static void delete_key(abts_case *tc, void *data)
225
apr_hash_t *h = NULL;
228
h = apr_hash_make(p);
229
ABTS_PTR_NOTNULL(tc, h);
231
apr_hash_set(h, "key", APR_HASH_KEY_STRING, "value");
232
apr_hash_set(h, "key2", APR_HASH_KEY_STRING, "value2");
234
result = apr_hash_get(h, "key", APR_HASH_KEY_STRING);
235
ABTS_STR_EQUAL(tc, "value", result);
237
result = apr_hash_get(h, "key2", APR_HASH_KEY_STRING);
238
ABTS_STR_EQUAL(tc, "value2", result);
240
apr_hash_set(h, "key", APR_HASH_KEY_STRING, NULL);
242
result = apr_hash_get(h, "key", APR_HASH_KEY_STRING);
243
ABTS_PTR_EQUAL(tc, NULL, result);
245
result = apr_hash_get(h, "key2", APR_HASH_KEY_STRING);
246
ABTS_STR_EQUAL(tc, "value2", result);
249
static void hash_count_0(abts_case *tc, void *data)
251
apr_hash_t *h = NULL;
254
h = apr_hash_make(p);
255
ABTS_PTR_NOTNULL(tc, h);
257
count = apr_hash_count(h);
258
ABTS_INT_EQUAL(tc, 0, count);
261
static void hash_count_1(abts_case *tc, void *data)
263
apr_hash_t *h = NULL;
266
h = apr_hash_make(p);
267
ABTS_PTR_NOTNULL(tc, h);
269
apr_hash_set(h, "key", APR_HASH_KEY_STRING, "value");
271
count = apr_hash_count(h);
272
ABTS_INT_EQUAL(tc, 1, count);
275
static void hash_count_5(abts_case *tc, void *data)
277
apr_hash_t *h = NULL;
280
h = apr_hash_make(p);
281
ABTS_PTR_NOTNULL(tc, h);
283
apr_hash_set(h, "key1", APR_HASH_KEY_STRING, "value1");
284
apr_hash_set(h, "key2", APR_HASH_KEY_STRING, "value2");
285
apr_hash_set(h, "key3", APR_HASH_KEY_STRING, "value3");
286
apr_hash_set(h, "key4", APR_HASH_KEY_STRING, "value4");
287
apr_hash_set(h, "key5", APR_HASH_KEY_STRING, "value5");
289
count = apr_hash_count(h);
290
ABTS_INT_EQUAL(tc, 5, count);
293
static void overlay_empty(abts_case *tc, void *data)
295
apr_hash_t *base = NULL;
296
apr_hash_t *overlay = NULL;
297
apr_hash_t *result = NULL;
301
base = apr_hash_make(p);
302
overlay = apr_hash_make(p);
303
ABTS_PTR_NOTNULL(tc, base);
304
ABTS_PTR_NOTNULL(tc, overlay);
306
apr_hash_set(base, "key1", APR_HASH_KEY_STRING, "value1");
307
apr_hash_set(base, "key2", APR_HASH_KEY_STRING, "value2");
308
apr_hash_set(base, "key3", APR_HASH_KEY_STRING, "value3");
309
apr_hash_set(base, "key4", APR_HASH_KEY_STRING, "value4");
310
apr_hash_set(base, "key5", APR_HASH_KEY_STRING, "value5");
312
result = apr_hash_overlay(p, overlay, base);
314
count = apr_hash_count(result);
315
ABTS_INT_EQUAL(tc, 5, count);
317
dump_hash(p, result, str);
318
ABTS_STR_EQUAL(tc, "Key key1 (4) Value value1\n"
319
"Key key2 (4) Value value2\n"
320
"Key key3 (4) Value value3\n"
321
"Key key4 (4) Value value4\n"
322
"Key key5 (4) Value value5\n"
323
"#entries 5\n", str);
326
static void overlay_2unique(abts_case *tc, void *data)
328
apr_hash_t *base = NULL;
329
apr_hash_t *overlay = NULL;
330
apr_hash_t *result = NULL;
334
base = apr_hash_make(p);
335
overlay = apr_hash_make(p);
336
ABTS_PTR_NOTNULL(tc, base);
337
ABTS_PTR_NOTNULL(tc, overlay);
339
apr_hash_set(base, "base1", APR_HASH_KEY_STRING, "value1");
340
apr_hash_set(base, "base2", APR_HASH_KEY_STRING, "value2");
341
apr_hash_set(base, "base3", APR_HASH_KEY_STRING, "value3");
342
apr_hash_set(base, "base4", APR_HASH_KEY_STRING, "value4");
343
apr_hash_set(base, "base5", APR_HASH_KEY_STRING, "value5");
345
apr_hash_set(overlay, "overlay1", APR_HASH_KEY_STRING, "value1");
346
apr_hash_set(overlay, "overlay2", APR_HASH_KEY_STRING, "value2");
347
apr_hash_set(overlay, "overlay3", APR_HASH_KEY_STRING, "value3");
348
apr_hash_set(overlay, "overlay4", APR_HASH_KEY_STRING, "value4");
349
apr_hash_set(overlay, "overlay5", APR_HASH_KEY_STRING, "value5");
351
result = apr_hash_overlay(p, overlay, base);
353
count = apr_hash_count(result);
354
ABTS_INT_EQUAL(tc, 10, count);
356
dump_hash(p, result, str);
357
/* I don't know why these are out of order, but they are. I would probably
358
* consider this a bug, but others should comment.
360
ABTS_STR_EQUAL(tc, "Key base5 (5) Value value5\n"
361
"Key overlay1 (8) Value value1\n"
362
"Key overlay2 (8) Value value2\n"
363
"Key overlay3 (8) Value value3\n"
364
"Key overlay4 (8) Value value4\n"
365
"Key overlay5 (8) Value value5\n"
366
"Key base1 (5) Value value1\n"
367
"Key base2 (5) Value value2\n"
368
"Key base3 (5) Value value3\n"
369
"Key base4 (5) Value value4\n"
370
"#entries 10\n", str);
373
static void overlay_same(abts_case *tc, void *data)
375
apr_hash_t *base = NULL;
376
apr_hash_t *result = NULL;
380
base = apr_hash_make(p);
381
ABTS_PTR_NOTNULL(tc, base);
383
apr_hash_set(base, "base1", APR_HASH_KEY_STRING, "value1");
384
apr_hash_set(base, "base2", APR_HASH_KEY_STRING, "value2");
385
apr_hash_set(base, "base3", APR_HASH_KEY_STRING, "value3");
386
apr_hash_set(base, "base4", APR_HASH_KEY_STRING, "value4");
387
apr_hash_set(base, "base5", APR_HASH_KEY_STRING, "value5");
389
result = apr_hash_overlay(p, base, base);
391
count = apr_hash_count(result);
392
ABTS_INT_EQUAL(tc, 5, count);
394
dump_hash(p, result, str);
395
/* I don't know why these are out of order, but they are. I would probably
396
* consider this a bug, but others should comment.
398
ABTS_STR_EQUAL(tc, "Key base5 (5) Value value5\n"
399
"Key base1 (5) Value value1\n"
400
"Key base2 (5) Value value2\n"
401
"Key base3 (5) Value value3\n"
402
"Key base4 (5) Value value4\n"
403
"#entries 5\n", str);
406
abts_suite *testhash(abts_suite *suite)
408
suite = ADD_SUITE(suite)
410
abts_run_test(suite, hash_make, NULL);
411
abts_run_test(suite, hash_set, NULL);
412
abts_run_test(suite, hash_reset, NULL);
413
abts_run_test(suite, same_value, NULL);
414
abts_run_test(suite, same_value_custom, NULL);
415
abts_run_test(suite, key_space, NULL);
416
abts_run_test(suite, delete_key, NULL);
418
abts_run_test(suite, hash_count_0, NULL);
419
abts_run_test(suite, hash_count_1, NULL);
420
abts_run_test(suite, hash_count_5, NULL);
422
abts_run_test(suite, hash_traverse, NULL);
423
abts_run_test(suite, summation_test, NULL);
425
abts_run_test(suite, overlay_empty, NULL);
426
abts_run_test(suite, overlay_2unique, NULL);
427
abts_run_test(suite, overlay_same, NULL);