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
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(CuTest *tc)
65 CuAssertPtrNotNull(tc, h);
68 static void hash_set(CuTest *tc)
74 CuAssertPtrNotNull(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 CuAssertStrEquals(tc, "value", result);
81 static void hash_reset(CuTest *tc)
87 CuAssertPtrNotNull(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 CuAssertStrEquals(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 CuAssertStrEquals(tc, "new", result);
98 static void same_value(CuTest *tc)
100 apr_hash_t *h = NULL;
103 h = apr_hash_make(p);
104 CuAssertPtrNotNull(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 CuAssertStrEquals(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 CuAssertStrEquals(tc, "same", result);
115 static void key_space(CuTest *tc)
117 apr_hash_t *h = NULL;
120 h = apr_hash_make(p);
121 CuAssertPtrNotNull(tc, h);
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);
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.
132 static void hash_traverse(CuTest *tc)
137 h = apr_hash_make(p);
138 CuAssertPtrNotNull(tc, h);
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");
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);
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.
165 static void summation_test(CuTest *tc)
168 int sumKeys, sumVal, trySumKey, trySumVal;
169 int i, j, *val, *key;
172 CuAssertPtrNotNull(tc, h);
179 for (i = 0; i < 100; i++) {
183 key = apr_palloc(p, sizeof(int));
185 val = apr_palloc(p, sizeof(int));
187 apr_hash_set(h, key, sizeof(int), val);
190 sum_hash(p, h, &i, &trySumKey, &trySumVal);
191 CuAssertIntEquals(tc, 100, i);
192 CuAssertIntEquals(tc, sumVal, trySumVal);
193 CuAssertIntEquals(tc, sumKeys, trySumKey);
196 static void delete_key(CuTest *tc)
198 apr_hash_t *h = NULL;
201 h = apr_hash_make(p);
202 CuAssertPtrNotNull(tc, h);
204 apr_hash_set(h, "key", APR_HASH_KEY_STRING, "value");
205 apr_hash_set(h, "key2", APR_HASH_KEY_STRING, "value2");
207 result = apr_hash_get(h, "key", APR_HASH_KEY_STRING);
208 CuAssertStrEquals(tc, "value", result);
210 result = apr_hash_get(h, "key2", APR_HASH_KEY_STRING);
211 CuAssertStrEquals(tc, "value2", result);
213 apr_hash_set(h, "key", APR_HASH_KEY_STRING, NULL);
215 result = apr_hash_get(h, "key", APR_HASH_KEY_STRING);
216 CuAssertPtrEquals(tc, NULL, result);
218 result = apr_hash_get(h, "key2", APR_HASH_KEY_STRING);
219 CuAssertStrEquals(tc, "value2", result);
222 static void hash_count_0(CuTest *tc)
224 apr_hash_t *h = NULL;
227 h = apr_hash_make(p);
228 CuAssertPtrNotNull(tc, h);
230 count = apr_hash_count(h);
231 CuAssertIntEquals(tc, 0, count);
234 static void hash_count_1(CuTest *tc)
236 apr_hash_t *h = NULL;
239 h = apr_hash_make(p);
240 CuAssertPtrNotNull(tc, h);
242 apr_hash_set(h, "key", APR_HASH_KEY_STRING, "value");
244 count = apr_hash_count(h);
245 CuAssertIntEquals(tc, 1, count);
248 static void hash_count_5(CuTest *tc)
250 apr_hash_t *h = NULL;
253 h = apr_hash_make(p);
254 CuAssertPtrNotNull(tc, h);
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");
262 count = apr_hash_count(h);
263 CuAssertIntEquals(tc, 5, count);
266 static void overlay_empty(CuTest *tc)
268 apr_hash_t *base = NULL;
269 apr_hash_t *overlay = NULL;
270 apr_hash_t *result = NULL;
274 base = apr_hash_make(p);
275 overlay = apr_hash_make(p);
276 CuAssertPtrNotNull(tc, base);
277 CuAssertPtrNotNull(tc, overlay);
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");
285 result = apr_hash_overlay(p, overlay, base);
287 count = apr_hash_count(result);
288 CuAssertIntEquals(tc, 5, count);
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);
299 static void overlay_2unique(CuTest *tc)
301 apr_hash_t *base = NULL;
302 apr_hash_t *overlay = NULL;
303 apr_hash_t *result = NULL;
307 base = apr_hash_make(p);
308 overlay = apr_hash_make(p);
309 CuAssertPtrNotNull(tc, base);
310 CuAssertPtrNotNull(tc, overlay);
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");
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");
324 result = apr_hash_overlay(p, overlay, base);
326 count = apr_hash_count(result);
327 CuAssertIntEquals(tc, 10, count);
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.
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);
346 static void overlay_same(CuTest *tc)
348 apr_hash_t *base = NULL;
349 apr_hash_t *result = NULL;
353 base = apr_hash_make(p);
354 CuAssertPtrNotNull(tc, base);
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");
362 result = apr_hash_overlay(p, base, base);
364 count = apr_hash_count(result);
365 CuAssertIntEquals(tc, 5, count);
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.
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);
379 CuSuite *testhash(void)
381 CuSuite *suite = CuSuiteNew("Hash");
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);
390 SUITE_ADD_TEST(suite, hash_count_0);
391 SUITE_ADD_TEST(suite, hash_count_1);
392 SUITE_ADD_TEST(suite, hash_count_5);
394 SUITE_ADD_TEST(suite, hash_traverse);
395 SUITE_ADD_TEST(suite, summation_test);
397 SUITE_ADD_TEST(suite, overlay_empty);
398 SUITE_ADD_TEST(suite, overlay_2unique);
399 SUITE_ADD_TEST(suite, overlay_same);