Apache HTTPD
testhash.c
Go to the documentation of this file.
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 "testutil.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#define MAX_LTH 256
25#define MAX_DEPTH 11
26
27static int comp_string(const void *str1, const void *str2)
28{
29 return strcmp(str1,str2);
30}
31
32static void dump_hash(apr_pool_t *p, apr_hash_t *h, char str[][MAX_LTH])
33{
35 int i = 0;
36
37 for (hi = apr_hash_first(p, h); hi; hi = apr_hash_next(hi)) {
38 const char *key = apr_hash_this_key(hi);
40 char *val = apr_hash_this_val(hi);
41
42 str[i][0]='\0';
43 apr_snprintf(str[i], MAX_LTH, "%sKey %s (%" APR_SSIZE_T_FMT ") Value %s\n",
44 str[i], key, len, val);
45 i++;
46 }
47 str[i][0]='\0';
48 apr_snprintf(str[i], MAX_LTH, "%s#entries %d\n", str[i], i);
49
50 /* Sort the result strings so that they can be checked for expected results easily,
51 * without having to worry about platform quirks
52 */
53 qsort(
54 str, /* Pointer to elements */
55 i, /* number of elements */
56 MAX_LTH, /* size of one element */
57 comp_string /* Pointer to comparison routine */
58 );
59}
60
61static void sum_hash(apr_pool_t *p, apr_hash_t *h, int *pcount, int *keySum, int *valSum)
62{
64 void *val, *key;
65 int count = 0;
66
67 *keySum = 0;
68 *valSum = 0;
69 *pcount = 0;
70 for (hi = apr_hash_first(p, h); hi; hi = apr_hash_next(hi)) {
71 apr_hash_this(hi, (void*)&key, NULL, &val);
72 *valSum += *(int *)val;
73 *keySum += *(int *)key;
74 count++;
75 }
76 *pcount=count;
77}
78
79static void hash_make(abts_case *tc, void *data)
80{
81 apr_hash_t *h = NULL;
82
83 h = apr_hash_make(p);
85}
86
87static void hash_set(abts_case *tc, void *data)
88{
89 apr_hash_t *h = NULL;
90 char *result = NULL;
91
92 h = apr_hash_make(p);
94
95 apr_hash_set(h, "key", APR_HASH_KEY_STRING, "value");
97 ABTS_STR_EQUAL(tc, "value", result);
98}
99
100static void hash_reset(abts_case *tc, void *data)
101{
102 apr_hash_t *h = NULL;
103 char *result = NULL;
104
105 h = apr_hash_make(p);
106 ABTS_PTR_NOTNULL(tc, h);
107
108 apr_hash_set(h, "key", APR_HASH_KEY_STRING, "value");
110 ABTS_STR_EQUAL(tc, "value", result);
111
112 apr_hash_set(h, "key", APR_HASH_KEY_STRING, "new");
114 ABTS_STR_EQUAL(tc, "new", result);
115}
116
117static void same_value(abts_case *tc, void *data)
118{
119 apr_hash_t *h = NULL;
120 char *result = NULL;
121
122 h = apr_hash_make(p);
123 ABTS_PTR_NOTNULL(tc, h);
124
125 apr_hash_set(h, "same1", APR_HASH_KEY_STRING, "same");
127 ABTS_STR_EQUAL(tc, "same", result);
128
129 apr_hash_set(h, "same2", APR_HASH_KEY_STRING, "same");
131 ABTS_STR_EQUAL(tc, "same", result);
132}
133
134static unsigned int hash_custom( const char *key, apr_ssize_t *klen)
135{
136 unsigned int hash = 0;
137 while( *klen ) {
138 (*klen) --;
139 hash = hash * 33 + key[ *klen ];
140 }
141 return hash;
142}
143
144static void same_value_custom(abts_case *tc, void *data)
145{
146 apr_hash_t *h = NULL;
147 char *result = NULL;
148
150 ABTS_PTR_NOTNULL(tc, h);
151
152 apr_hash_set(h, "same1", 5, "same");
153 result = apr_hash_get(h, "same1", 5);
154 ABTS_STR_EQUAL(tc, "same", result);
155
156 apr_hash_set(h, "same2", 5, "same");
157 result = apr_hash_get(h, "same2", 5);
158 ABTS_STR_EQUAL(tc, "same", result);
159}
160
161static void key_space(abts_case *tc, void *data)
162{
163 apr_hash_t *h = NULL;
164 char *result = NULL;
165
166 h = apr_hash_make(p);
167 ABTS_PTR_NOTNULL(tc, h);
168
169 apr_hash_set(h, "key with space", APR_HASH_KEY_STRING, "value");
170 result = apr_hash_get(h, "key with space", APR_HASH_KEY_STRING);
171 ABTS_STR_EQUAL(tc, "value", result);
172}
173
174static void hash_clear(abts_case *tc, void *data)
175{
176 apr_hash_t *h;
177 int i, *e;
178
179 h = apr_hash_make(p);
180 ABTS_PTR_NOTNULL(tc, h);
181
182 for (i = 1; i <= 10; i++) {
183 e = apr_palloc(p, sizeof(int));
184 *e = i;
185 apr_hash_set(h, e, sizeof(*e), e);
186 }
188 i = apr_hash_count(h);
189 ABTS_INT_EQUAL(tc, 0, i);
190}
191
192/* This is kind of a hack, but I am just keeping an existing test. This is
193 * really testing apr_hash_first, apr_hash_next, and apr_hash_this which
194 * should be tested in three separate tests, but this will do for now.
195 */
196static void hash_traverse(abts_case *tc, void *data)
197{
198 apr_hash_t *h;
200
201 h = apr_hash_make(p);
202 ABTS_PTR_NOTNULL(tc, h);
203
204 apr_hash_set(h, "OVERWRITE", APR_HASH_KEY_STRING, "should not see this");
205 apr_hash_set(h, "FOO3", APR_HASH_KEY_STRING, "bar3");
206 apr_hash_set(h, "FOO3", APR_HASH_KEY_STRING, "bar3");
207 apr_hash_set(h, "FOO1", APR_HASH_KEY_STRING, "bar1");
208 apr_hash_set(h, "FOO2", APR_HASH_KEY_STRING, "bar2");
209 apr_hash_set(h, "FOO4", APR_HASH_KEY_STRING, "bar4");
210 apr_hash_set(h, "SAME1", APR_HASH_KEY_STRING, "same");
211 apr_hash_set(h, "SAME2", APR_HASH_KEY_STRING, "same");
212 apr_hash_set(h, "OVERWRITE", APR_HASH_KEY_STRING, "Overwrite key");
213
215
216 ABTS_STR_EQUAL(tc, "Key FOO1 (4) Value bar1\n", StrArray[0]);
217 ABTS_STR_EQUAL(tc, "Key FOO2 (4) Value bar2\n", StrArray[1]);
218 ABTS_STR_EQUAL(tc, "Key FOO3 (4) Value bar3\n", StrArray[2]);
219 ABTS_STR_EQUAL(tc, "Key FOO4 (4) Value bar4\n", StrArray[3]);
220 ABTS_STR_EQUAL(tc, "Key OVERWRITE (9) Value Overwrite key\n", StrArray[4]);
221 ABTS_STR_EQUAL(tc, "Key SAME1 (5) Value same\n", StrArray[5]);
222 ABTS_STR_EQUAL(tc, "Key SAME2 (5) Value same\n", StrArray[6]);
223 ABTS_STR_EQUAL(tc, "#entries 7\n", StrArray[7]);
224}
225
226/* This is kind of a hack, but I am just keeping an existing test. This is
227 * really testing apr_hash_first, apr_hash_next, and apr_hash_this which
228 * should be tested in three separate tests, but this will do for now.
229 */
230static void summation_test(abts_case *tc, void *data)
231{
232 apr_hash_t *h;
234 int i, j, *val, *key;
235
236 h =apr_hash_make(p);
237 ABTS_PTR_NOTNULL(tc, h);
238
239 sumKeys = 0;
240 sumVal = 0;
241 trySumKey = 0;
242 trySumVal = 0;
243
244 for (i = 0; i < 100; i++) {
245 j = i * 10 + 1;
246 sumKeys += j;
247 sumVal += i;
248 key = apr_palloc(p, sizeof(int));
249 *key = j;
250 val = apr_palloc(p, sizeof(int));
251 *val = i;
252 apr_hash_set(h, key, sizeof(int), val);
253 }
254
256 ABTS_INT_EQUAL(tc, 100, i);
259}
260
261static void delete_key(abts_case *tc, void *data)
262{
263 apr_hash_t *h = NULL;
264 char *result = NULL;
265
266 h = apr_hash_make(p);
267 ABTS_PTR_NOTNULL(tc, h);
268
269 apr_hash_set(h, "key", APR_HASH_KEY_STRING, "value");
270 apr_hash_set(h, "key2", APR_HASH_KEY_STRING, "value2");
271
273 ABTS_STR_EQUAL(tc, "value", result);
274
276 ABTS_STR_EQUAL(tc, "value2", result);
277
279
282
284 ABTS_STR_EQUAL(tc, "value2", result);
285}
286
287static void hash_count_0(abts_case *tc, void *data)
288{
289 apr_hash_t *h = NULL;
290 int count;
291
292 h = apr_hash_make(p);
293 ABTS_PTR_NOTNULL(tc, h);
294
296 ABTS_INT_EQUAL(tc, 0, count);
297}
298
299static void hash_count_1(abts_case *tc, void *data)
300{
301 apr_hash_t *h = NULL;
302 int count;
303
304 h = apr_hash_make(p);
305 ABTS_PTR_NOTNULL(tc, h);
306
307 apr_hash_set(h, "key", APR_HASH_KEY_STRING, "value");
308
310 ABTS_INT_EQUAL(tc, 1, count);
311}
312
313static void hash_count_5(abts_case *tc, void *data)
314{
315 apr_hash_t *h = NULL;
316 int count;
317
318 h = apr_hash_make(p);
319 ABTS_PTR_NOTNULL(tc, h);
320
321 apr_hash_set(h, "key1", APR_HASH_KEY_STRING, "value1");
322 apr_hash_set(h, "key2", APR_HASH_KEY_STRING, "value2");
323 apr_hash_set(h, "key3", APR_HASH_KEY_STRING, "value3");
324 apr_hash_set(h, "key4", APR_HASH_KEY_STRING, "value4");
325 apr_hash_set(h, "key5", APR_HASH_KEY_STRING, "value5");
326
328 ABTS_INT_EQUAL(tc, 5, count);
329}
330
331static void overlay_empty(abts_case *tc, void *data)
332{
336 int count;
338
343
344 apr_hash_set(base, "key1", APR_HASH_KEY_STRING, "value1");
345 apr_hash_set(base, "key2", APR_HASH_KEY_STRING, "value2");
346 apr_hash_set(base, "key3", APR_HASH_KEY_STRING, "value3");
347 apr_hash_set(base, "key4", APR_HASH_KEY_STRING, "value4");
348 apr_hash_set(base, "key5", APR_HASH_KEY_STRING, "value5");
349
351
353 ABTS_INT_EQUAL(tc, 5, count);
354
356
357 ABTS_STR_EQUAL(tc, "Key key1 (4) Value value1\n", StrArray[0]);
358 ABTS_STR_EQUAL(tc, "Key key2 (4) Value value2\n", StrArray[1]);
359 ABTS_STR_EQUAL(tc, "Key key3 (4) Value value3\n", StrArray[2]);
360 ABTS_STR_EQUAL(tc, "Key key4 (4) Value value4\n", StrArray[3]);
361 ABTS_STR_EQUAL(tc, "Key key5 (4) Value value5\n", StrArray[4]);
362 ABTS_STR_EQUAL(tc, "#entries 5\n", StrArray[5]);
363}
364
365static void overlay_2unique(abts_case *tc, void *data)
366{
370 int count;
372
377
378 apr_hash_set(base, "base1", APR_HASH_KEY_STRING, "value1");
379 apr_hash_set(base, "base2", APR_HASH_KEY_STRING, "value2");
380 apr_hash_set(base, "base3", APR_HASH_KEY_STRING, "value3");
381 apr_hash_set(base, "base4", APR_HASH_KEY_STRING, "value4");
382 apr_hash_set(base, "base5", APR_HASH_KEY_STRING, "value5");
383
384 apr_hash_set(overlay, "overlay1", APR_HASH_KEY_STRING, "value1");
385 apr_hash_set(overlay, "overlay2", APR_HASH_KEY_STRING, "value2");
386 apr_hash_set(overlay, "overlay3", APR_HASH_KEY_STRING, "value3");
387 apr_hash_set(overlay, "overlay4", APR_HASH_KEY_STRING, "value4");
388 apr_hash_set(overlay, "overlay5", APR_HASH_KEY_STRING, "value5");
389
391
393 ABTS_INT_EQUAL(tc, 10, count);
394
396
397 ABTS_STR_EQUAL(tc, "Key base1 (5) Value value1\n", StrArray[0]);
398 ABTS_STR_EQUAL(tc, "Key base2 (5) Value value2\n", StrArray[1]);
399 ABTS_STR_EQUAL(tc, "Key base3 (5) Value value3\n", StrArray[2]);
400 ABTS_STR_EQUAL(tc, "Key base4 (5) Value value4\n", StrArray[3]);
401 ABTS_STR_EQUAL(tc, "Key base5 (5) Value value5\n", StrArray[4]);
402 ABTS_STR_EQUAL(tc, "Key overlay1 (8) Value value1\n", StrArray[5]);
403 ABTS_STR_EQUAL(tc, "Key overlay2 (8) Value value2\n", StrArray[6]);
404 ABTS_STR_EQUAL(tc, "Key overlay3 (8) Value value3\n", StrArray[7]);
405 ABTS_STR_EQUAL(tc, "Key overlay4 (8) Value value4\n", StrArray[8]);
406 ABTS_STR_EQUAL(tc, "Key overlay5 (8) Value value5\n", StrArray[9]);
407 ABTS_STR_EQUAL(tc, "#entries 10\n", StrArray[10]);
408}
409
410static void overlay_same(abts_case *tc, void *data)
411{
414 int count;
416
419
420 apr_hash_set(base, "base1", APR_HASH_KEY_STRING, "value1");
421 apr_hash_set(base, "base2", APR_HASH_KEY_STRING, "value2");
422 apr_hash_set(base, "base3", APR_HASH_KEY_STRING, "value3");
423 apr_hash_set(base, "base4", APR_HASH_KEY_STRING, "value4");
424 apr_hash_set(base, "base5", APR_HASH_KEY_STRING, "value5");
425
427
429 ABTS_INT_EQUAL(tc, 5, count);
430
432
433 ABTS_STR_EQUAL(tc, "Key base1 (5) Value value1\n", StrArray[0]);
434 ABTS_STR_EQUAL(tc, "Key base2 (5) Value value2\n", StrArray[1]);
435 ABTS_STR_EQUAL(tc, "Key base3 (5) Value value3\n", StrArray[2]);
436 ABTS_STR_EQUAL(tc, "Key base4 (5) Value value4\n", StrArray[3]);
437 ABTS_STR_EQUAL(tc, "Key base5 (5) Value value5\n", StrArray[4]);
438 ABTS_STR_EQUAL(tc, "#entries 5\n", StrArray[5]);
439}
440
441static void overlay_fetch(abts_case *tc, void *data)
442{
446 int count;
447
452
453 apr_hash_set(base, "base1", APR_HASH_KEY_STRING, "value1");
454 apr_hash_set(base, "base2", APR_HASH_KEY_STRING, "value2");
455 apr_hash_set(base, "base3", APR_HASH_KEY_STRING, "value3");
456 apr_hash_set(base, "base4", APR_HASH_KEY_STRING, "value4");
457 apr_hash_set(base, "base5", APR_HASH_KEY_STRING, "value5");
458
459 apr_hash_set(overlay, "overlay1", APR_HASH_KEY_STRING, "value1");
460 apr_hash_set(overlay, "overlay2", APR_HASH_KEY_STRING, "value2");
461 apr_hash_set(overlay, "overlay3", APR_HASH_KEY_STRING, "value3");
462 apr_hash_set(overlay, "overlay4", APR_HASH_KEY_STRING, "value4");
463 apr_hash_set(overlay, "overlay5", APR_HASH_KEY_STRING, "value5");
464
466
468 ABTS_INT_EQUAL(tc, 10, count);
469
470 ABTS_STR_EQUAL(tc, "value1",
472 ABTS_STR_EQUAL(tc, "value2",
474 ABTS_STR_EQUAL(tc, "value3",
476 ABTS_STR_EQUAL(tc, "value4",
478 ABTS_STR_EQUAL(tc, "value5",
480 ABTS_STR_EQUAL(tc, "value1",
482 ABTS_STR_EQUAL(tc, "value2",
484 ABTS_STR_EQUAL(tc, "value3",
486 ABTS_STR_EQUAL(tc, "value4",
488 ABTS_STR_EQUAL(tc, "value5",
490
491 ABTS_STR_EQUAL(tc, "value1",
493 ABTS_STR_EQUAL(tc, "value2",
495 ABTS_STR_EQUAL(tc, "value3",
497 ABTS_STR_EQUAL(tc, "value4",
499 ABTS_STR_EQUAL(tc, "value5",
501
502 ABTS_STR_EQUAL(tc, "value1",
504 ABTS_STR_EQUAL(tc, "value2",
506 ABTS_STR_EQUAL(tc, "value3",
508 ABTS_STR_EQUAL(tc, "value4",
510 ABTS_STR_EQUAL(tc, "value5",
512}
513
515{
516 suite = ADD_SUITE(suite)
517
519 abts_run_test(suite, hash_set, NULL);
525
529
533
538
539 return suite;
540}
541
const char apr_size_t len
Definition ap_regex.h:187
void abts_run_test(abts_suite *ts, test_func f, void *value)
Definition abts.c:175
#define ABTS_PTR_EQUAL(a, b, c)
Definition abts.h:126
#define ADD_SUITE(suite)
Definition abts.h:67
#define ABTS_PTR_NOTNULL(a, b)
Definition abts.h:125
#define ABTS_STR_EQUAL(a, b, c)
Definition abts.h:123
#define ABTS_INT_EQUAL(a, b, c)
Definition abts.h:109
APR Miscellaneous library routines.
APR Hash Tables.
APR memory allocation.
#define hash(h, r, b, n)
Definition apr_random.c:51
APR Strings library.
ap_conf_vector_t * base
unsigned int count
Definition apr_md5.h:152
apr_bucket * e
apr_size_t size
apr_uint32_t val
Definition apr_atomic.h:66
const char * str2
Definition apr_cstr.h:161
const char * key
void * data
apr_array_header_t ** result
const apr_hash_t * h
Definition apr_hash.h:97
apr_ssize_t * klen
Definition apr_hash.h:71
#define APR_HASH_KEY_STRING
Definition apr_hash.h:47
const apr_hash_t * overlay
Definition apr_hash.h:214
apr_pool_t * p
Definition md_event.c:32
return NULL
Definition mod_so.c:359
int i
Definition mod_so.c:347
static void key_space(abts_case *tc, void *data)
Definition testhash.c:161
#define MAX_LTH
Definition testhash.c:24
abts_suite * testhash(abts_suite *suite)
Definition testhash.c:514
static void overlay_same(abts_case *tc, void *data)
Definition testhash.c:410
static void hash_reset(abts_case *tc, void *data)
Definition testhash.c:100
#define MAX_DEPTH
Definition testhash.c:25
static void hash_traverse(abts_case *tc, void *data)
Definition testhash.c:196
static void summation_test(abts_case *tc, void *data)
Definition testhash.c:230
static int comp_string(const void *str1, const void *str2)
Definition testhash.c:27
static void overlay_2unique(abts_case *tc, void *data)
Definition testhash.c:365
static void dump_hash(apr_pool_t *p, apr_hash_t *h, char str[][256])
Definition testhash.c:32
static void overlay_fetch(abts_case *tc, void *data)
Definition testhash.c:441
static void hash_count_5(abts_case *tc, void *data)
Definition testhash.c:313
static void delete_key(abts_case *tc, void *data)
Definition testhash.c:261
static void sum_hash(apr_pool_t *p, apr_hash_t *h, int *pcount, int *keySum, int *valSum)
Definition testhash.c:61
static unsigned int hash_custom(const char *key, apr_ssize_t *klen)
Definition testhash.c:134
static void overlay_empty(abts_case *tc, void *data)
Definition testhash.c:331
static void same_value_custom(abts_case *tc, void *data)
Definition testhash.c:144
static void hash_clear(abts_case *tc, void *data)
Definition testhash.c:174
static void same_value(abts_case *tc, void *data)
Definition testhash.c:117
static void hash_count_1(abts_case *tc, void *data)
Definition testhash.c:299
static void hash_set(abts_case *tc, void *data)
Definition testhash.c:87
static void hash_make(abts_case *tc, void *data)
Definition testhash.c:79
static void hash_count_0(abts_case *tc, void *data)
Definition testhash.c:287
#define str