summaryrefslogtreecommitdiff
path: root/test/testhash.c
blob: 7c3ff6c29b95672b72872205d53ffdab7cf7d0c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/* ====================================================================
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 2000-2002 The Apache Software Foundation.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "Apache" and "Apache Software Foundation" must
 *    not be used to endorse or promote products derived from this
 *    software without prior written permission. For written
 *    permission, please contact apache@apache.org.
 *
 * 5. Products derived from this software may not be called "Apache",
 *    nor may "Apache" appear in their name, without prior written
 *    permission of the Apache Software Foundation.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 */

#include "apr.h"
#include "apr_strings.h"
#include "apr_general.h"
#include "apr_pools.h"
#include "apr_hash.h"
#if APR_HAVE_STDIO_H
#include <stdio.h>
#endif
#if APR_HAVE_STDLIB_H
#include <stdlib.h>
#endif
#if APR_HAVE_STRING_H
#include <string.h>
#endif

static void dump_hash(apr_pool_t *p, apr_hash_t *h) 
{
    apr_hash_index_t *hi;
    char *val, *key;
    apr_ssize_t len;
    int i = 0;

    for (hi = apr_hash_first(p, h); hi; hi = apr_hash_next(hi)) {
        apr_hash_this(hi,(void*) &key, &len, (void*) &val);
        fprintf(stdout, "Key %s (%" APR_SSIZE_T_FMT ") Value %s\n", key, len, val);
        i++;
    }
    if (i != apr_hash_count(h)) 
        fprintf(stderr, "ERROR: #entries (%d) does not match count (%d)\n",
                i, apr_hash_count(h));
    else 
        fprintf(stdout, "#entries %d \n", i);
}

static void sum_hash(apr_pool_t *p, apr_hash_t *h, int *pcount, int *keySum, int *valSum) 
{
    apr_hash_index_t *hi;
    void *val, *key;
    int count = 0;

    *keySum = 0;
    *valSum = 0;
    *pcount = 0;
    for (hi = apr_hash_first(p, h); hi; hi = apr_hash_next(hi)) {
        apr_hash_this(hi, (void*)&key, NULL, &val);
        *valSum += *(int *)val;
        *keySum += *(int *)key;
        count++;
    }
    *pcount=count;
}

int main(int argc, const char *const argv[])
{
    apr_pool_t *cntxt;
    apr_hash_t *h, *h2, *h3, *h4;

    int i, j, *val, *key;
    char *result;
    int sumKeys, sumVal, trySumKey, trySumVal;

    apr_initialize();
    atexit(apr_terminate);

    apr_pool_create(&cntxt, NULL);

    /* table defaults  */
    h = apr_hash_make(cntxt);
    if (h == NULL)  {
        fprintf(stderr, "ERROR: can not allocate HASH!\n");
        exit(-1);
    }

    apr_hash_set(h, "OVERWRITE", APR_HASH_KEY_STRING, "should not see this");
    apr_hash_set(h, "FOO3", APR_HASH_KEY_STRING, "bar3");
    apr_hash_set(h, "FOO3", APR_HASH_KEY_STRING, "bar3");
    apr_hash_set(h, "FOO1", APR_HASH_KEY_STRING, "bar1");
    apr_hash_set(h, "FOO2", APR_HASH_KEY_STRING, "bar2");
    apr_hash_set(h, "FOO4", APR_HASH_KEY_STRING, "bar4");
    apr_hash_set(h, "SAME1", APR_HASH_KEY_STRING, "same");
    apr_hash_set(h, "SAME2", APR_HASH_KEY_STRING, "same");
    apr_hash_set(h, "OVERWRITE", APR_HASH_KEY_STRING, "Overwrite key");

    result = apr_hash_get(h, "FOO2", APR_HASH_KEY_STRING);
    if (strcmp(result, "bar2"))
        fprintf(stderr, "ERROR:apr_hash_get FOO2 = %s (should be bar2)\n",
                result);

    result = apr_hash_get(h, "SAME2", APR_HASH_KEY_STRING);
    if (strcmp(result, "same"))
        fprintf(stderr, "ERROR:apr_hash_get SAME2 = %s (should be same)\n",
                result);

    result = apr_hash_get(h, "OVERWRITE", APR_HASH_KEY_STRING);
    if (strcmp(result, "Overwrite key"))
        fprintf(stderr, 
             "ERROR:apr_hash_get OVERWRITE = %s (should be 'Overwrite key')\n",
             result);

    result = apr_hash_get(h, "NOTTHERE", APR_HASH_KEY_STRING);
    if (result)
       fprintf(stderr, "ERROR:apr_hash_get NOTTHERE = %s (should be NULL)\n",
               result);
        
    result=apr_hash_get(h, "FOO3", APR_HASH_KEY_STRING);
    if (strcmp(result, "bar3"))
        fprintf(stderr, "ERROR:apr_hash_get FOO3 = %s (should be bar3)\n",
                result);
        
    dump_hash(cntxt, h);

    h2 =apr_hash_make(cntxt);
    if (h2 == NULL)  {
        fprintf(stderr, "ERROR: can not allocate HASH!\n");
        exit(-1);
    }
    sumKeys = 0;
    sumVal = 0;
    trySumKey = 0;
    trySumVal = 0;

    for (i = 0; i < 100; i++) {
        j = i * 10 + 1;
        sumKeys += j;
        sumVal += i;
        key = apr_palloc(cntxt, sizeof(int));
        *key = j;
        val = apr_palloc(cntxt, sizeof(int));
        *val = i;
        apr_hash_set(h2, key, sizeof(int), val);
    }

    sum_hash(cntxt, h2, &i, &trySumKey, &trySumVal);
    if (i==100) {
       fprintf(stdout, "All keys accounted for\n");
    } else {
       fprintf(stderr, "ERROR: Only got %d (out of 100)\n",i);
    }
    if (trySumVal != sumVal) {
       fprintf(stderr, "ERROR:Values don't add up Got %d expected %d\n",
               trySumVal, sumVal);
    }
    if (trySumKey != sumKeys) {
       fprintf(stderr, "ERROR:Keys don't add up Got %d expected %d\n",
               trySumKey, sumKeys);
    }

    j=891;
    apr_hash_set(h2, &j, sizeof(int), NULL);
    
    if (apr_hash_get(h2, &j, sizeof(int))) {
      fprintf(stderr, "ERROR: Delete not working\n");
    } else {
      fprintf(stdout, "Delete working\n");
    }
    sum_hash(cntxt, h2, &i, &trySumKey, &trySumVal);

    sumKeys -= 891;
    sumVal -= 89;

    if (i==99) {
       fprintf(stdout, "All keys accounted for.. Delete OK\n");
    } else {
       fprintf(stderr, "Only got %d (out of 99) Delete Not OK\n", i);
    }
    if (trySumVal != sumVal) {
       fprintf(stderr, "ERROR:Values don't add up Got %d expected %d\n",
               trySumVal, sumVal);
    }
    if (trySumKey != sumKeys) {
       fprintf(stderr, "ERROR:Keys don't add up Got %d expected %d\n",
               trySumKey, sumKeys);
    }

    /* test overlay */
    h3 = apr_hash_make(cntxt);
    /* test with blank hash tables */
    h4 = apr_hash_overlay(cntxt, h3, h);
    
    if (apr_hash_count(h4) != apr_hash_count(h)) {
        fprintf(stderr,
                "ERROR: overlay not working with blank overlay as overlay\n");
        dump_hash(cntxt, h4);
    }

    h4 = apr_hash_overlay(cntxt, h, h3);
    if (apr_hash_count(h4) != apr_hash_count(h)) {
        fprintf(stderr,
                "ERROR: overlay not working with blank overlay as base\n");
        dump_hash(cntxt, h4);
    }

    h4 = apr_hash_overlay(cntxt, h, h2);
    if (apr_hash_count(h4) != (apr_hash_count(h) + apr_hash_count(h2)))
        fprintf(stderr,
                "ERROR: overlay not working when overlaying 2 unique hashs\n");

    h4 = apr_hash_overlay(cntxt, h, h);
    if (apr_hash_count(h4) != apr_hash_count(h))  {
        fprintf(stderr,
                "ERROR: overlay not working when overlaying same hash\n");
        dump_hash(cntxt, h4);
    }
        
    result = apr_hash_get(h4, "FOO2", APR_HASH_KEY_STRING);
    if (strcmp(result, "bar2"))
        fprintf(stderr, "ERROR:apr_hash_get FOO2 = %s (should be bar2)\n",
                result);

    result = apr_hash_get(h4, "SAME2", APR_HASH_KEY_STRING);
    if (strcmp(result, "same"))
        fprintf(stderr, "ERROR:apr_hash_get SAME2 = %s (should be same)\n",
                result);
    
    result = apr_hash_get(h4, "OVERWRITE", APR_HASH_KEY_STRING);
    if (strcmp(result, "Overwrite key"))
        fprintf(stderr,
             "ERROR:apr_hash_get OVERWRITE = %s (should be 'Overwrite key')\n",
             result);

    result = apr_hash_get(h4, "NOTTHERE", APR_HASH_KEY_STRING);
    if (result)
        fprintf(stderr, "ERROR:apr_hash_get NOTTHERE = %s (should be NULL)\n",
                result);
        
    result = apr_hash_get(h4, "FOO3", APR_HASH_KEY_STRING);
    if (strcmp(result, "bar3"))
        fprintf(stderr, "ERROR:apr_hash_get FOO3 = %s (should be bar3)\n",
                result);

    apr_hash_set(h4, "FOO3", sizeof(int), NULL);              
    result = apr_hash_get(h4, "FOO3", APR_HASH_KEY_STRING);
    if (result)
        fprintf(stderr,
        "ERROR:apr_hash_get FOO3 = %s (should be NULL, we just deleted it!)\n",
        result);

    return 0;
}