File: testhash.c

package info (click to toggle)
nextepc 0.3.10+nods-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 23,304 kB
  • sloc: ansic: 164,982; yacc: 2,767; python: 2,567; makefile: 2,408; lex: 1,732; xml: 795; php: 486; sh: 349; sql: 337; perl: 75; awk: 35; cpp: 6
file content (393 lines) | stat: -rw-r--r-- 9,850 bytes parent folder | download
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#include "testutil.h"
#include "core_hash.h"

#define MAX_LTH 256
#define MAX_DEPTH 11

static int comp_string(const void *str1, const void *str2)
{
    return strcmp(str1,str2);
}

static void dump_hash(hash_t *h, char str[][MAX_LTH]) 
{
    hash_index_t *hi;
    int i = 0;

    for (hi = hash_first(h); hi; hi = hash_next(hi)) {
        const char *key = hash_this_key(hi);
        int len = hash_this_key_len(hi);
        char *val = hash_this_val(hi);

        str[i][0]='\0';
        sprintf(str[i]+strlen(str[i]), "Key %s (%d) Value %s\n",
                 key, len, val);
        i++;
    }
    str[i][0]='\0';
    sprintf(str[i]+strlen(str[i]), "#entries %d\n", i);

    /* Sort the result strings so that they can be checked for expected results easily,
     * without having to worry about platform quirks
     */
    qsort(
        str, /* Pointer to elements */
        i,   /* number of elements */
        MAX_LTH, /* size of one element */
        comp_string /* Pointer to comparison routine */
    );
}

static void sum_hash(hash_t *h, int *pcount, int *keySum, int *valSum) 
{
    hash_index_t *hi;
    void *val, *key;
    int count = 0;

    *keySum = 0;
    *valSum = 0;
    *pcount = 0;
    for (hi = hash_first(h); hi; hi = hash_next(hi)) {
        hash_this(hi, (void*)&key, NULL, &val);
        *valSum += *(int *)val;
        *keySum += *(int *)key;
        count++;
    }
    *pcount=count;
}

static void hash_make_test(abts_case *tc, void *data)
{
    hash_t *h = NULL;

    h = hash_make();
    ABTS_PTR_NOTNULL(tc, h);

    hash_destroy(h);
}

static void hash_set_test(abts_case *tc, void *data)
{
    hash_t *h = NULL;
    char *result = NULL;

    h = hash_make();
    ABTS_PTR_NOTNULL(tc, h);

    hash_set(h, "key", HASH_KEY_STRING, "value");
    result = hash_get(h, "key", HASH_KEY_STRING);
    ABTS_STR_EQUAL(tc, "value", result);

    hash_destroy(h);
}

static void hash_get_or_set_test(abts_case *tc, void *data)
{
    hash_t *h = NULL;
    char *result = NULL;

    h = hash_make();
    ABTS_PTR_NOTNULL(tc, h);

    result = hash_get_or_set(h, "key", HASH_KEY_STRING, "value");
    ABTS_STR_EQUAL(tc, "value", result);

    result = hash_get_or_set(h, "key", HASH_KEY_STRING, "other");
    ABTS_STR_EQUAL(tc, "value", result);

    result = hash_get_or_set(h, "key", HASH_KEY_STRING, NULL);
    ABTS_STR_EQUAL(tc, "value", result);

    hash_set(h, "key", HASH_KEY_STRING, NULL);
    result = hash_get(h, "key", HASH_KEY_STRING);
    ABTS_PTR_EQUAL(tc, NULL, result);

    result = hash_get_or_set(h, "key", HASH_KEY_STRING, NULL);
    ABTS_PTR_EQUAL(tc, NULL, result);

    result = hash_get_or_set(h, "key", HASH_KEY_STRING, "other");
    ABTS_STR_EQUAL(tc, "other", result);

    hash_destroy(h);
}

static void hash_reset(abts_case *tc, void *data)
{
    hash_t *h = NULL;
    char *result = NULL;

    h = hash_make();
    ABTS_PTR_NOTNULL(tc, h);

    hash_set(h, "key", HASH_KEY_STRING, "value");
    result = hash_get(h, "key", HASH_KEY_STRING);
    ABTS_STR_EQUAL(tc, "value", result);

    hash_set(h, "key", HASH_KEY_STRING, "new");
    result = hash_get(h, "key", HASH_KEY_STRING);
    ABTS_STR_EQUAL(tc, "new", result);

    hash_destroy(h);
}

static void same_value(abts_case *tc, void *data)
{
    hash_t *h = NULL;
    char *result = NULL;

    h = hash_make();
    ABTS_PTR_NOTNULL(tc, h);

    hash_set(h, "same1", HASH_KEY_STRING, "same");
    result = hash_get(h, "same1", HASH_KEY_STRING);
    ABTS_STR_EQUAL(tc, "same", result);

    hash_set(h, "same2", HASH_KEY_STRING, "same");
    result = hash_get(h, "same2", HASH_KEY_STRING);
    ABTS_STR_EQUAL(tc, "same", result);

    hash_destroy(h);
}

static unsigned int hash_custom( const char *key, int *klen)
{
    unsigned int hash = 0;
    unsigned int len = *klen;
    while( len ) {
        (len) --;
        hash = hash * 33 + key[ len ];
    }
    return hash;
}

static void same_value_custom(abts_case *tc, void *data)
{
    hash_t *h = NULL;
    char *result = NULL;

    h = hash_make_custom(hash_custom);
    ABTS_PTR_NOTNULL(tc, h);

    hash_set(h, "same1", 5, "same");
    result = hash_get(h, "same1", 5);
    ABTS_STR_EQUAL(tc, "same", result);

    hash_set(h, "same2", 5, "same");
    result = hash_get(h, "same2", 5);
    ABTS_STR_EQUAL(tc, "same", result);

    hash_destroy(h);
}

static void key_space(abts_case *tc, void *data)
{
    hash_t *h = NULL;
    char *result = NULL;

    h = hash_make();
    ABTS_PTR_NOTNULL(tc, h);

    hash_set(h, "key with space", HASH_KEY_STRING, "value");
    result = hash_get(h, "key with space", HASH_KEY_STRING);
    ABTS_STR_EQUAL(tc, "value", result);

    hash_destroy(h);
}

static void hash_clear_test(abts_case *tc, void *data)
{
    hash_t *h;
    int num[10], i;
    int c;

    h = hash_make();
    ABTS_PTR_NOTNULL(tc, h);

    for (i = 0; i < 10; i++)
    {
        num[i] = i;
        hash_set(h, &num[i], sizeof(num[i]), &num[i]);
    }
    c = hash_count(h);
    ABTS_INT_EQUAL(tc, 10, c);

    hash_clear(h);
    c = hash_count(h);
    ABTS_INT_EQUAL(tc, 0, c);

    hash_destroy(h);
}

/* This is kind of a hack, but I am just keeping an existing test.  This is
 * really testing hash_first, hash_next, and hash_this which 
 * should be tested in three separate tests, but this will do for now.
 */
static void hash_traverse(abts_case *tc, void *data)
{
    hash_t *h;
    char StrArray[MAX_DEPTH][MAX_LTH];

    h = hash_make();
    ABTS_PTR_NOTNULL(tc, h);

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

    dump_hash(h, StrArray);

    ABTS_STR_EQUAL(tc, "Key FOO1 (4) Value bar1\n", StrArray[0]);
    ABTS_STR_EQUAL(tc, "Key FOO2 (4) Value bar2\n", StrArray[1]);
    ABTS_STR_EQUAL(tc, "Key FOO3 (4) Value bar3\n", StrArray[2]);
    ABTS_STR_EQUAL(tc, "Key FOO4 (4) Value bar4\n", StrArray[3]);
    ABTS_STR_EQUAL(tc, "Key OVERWRITE (9) Value Overwrite key\n", StrArray[4]);
    ABTS_STR_EQUAL(tc, "Key SAME1 (5) Value same\n", StrArray[5]);
    ABTS_STR_EQUAL(tc, "Key SAME2 (5) Value same\n", StrArray[6]);
    ABTS_STR_EQUAL(tc, "#entries 7\n", StrArray[7]);

    hash_destroy(h);
}

/* This is kind of a hack, but I am just keeping an existing test.  This is
 * really testing hash_first, hash_next, and hash_this which 
 * should be tested in three separate tests, but this will do for now.
 */
static void summation_test(abts_case *tc, void *data)
{
    hash_t *h;
    int sumKeys, sumVal, trySumKey, trySumVal;
    int i, j, val[100], key[100];

    h = hash_make();
    ABTS_PTR_NOTNULL(tc, h);

    sumKeys = 0;
    sumVal = 0;
    trySumKey = 0;
    trySumVal = 0;

    for (i = 0; i < 100; i++) {
        j = i * 10 + 1;
        sumKeys += j;
        sumVal += i;
        key[i] = j;
        val[i] = i;
        hash_set(h, &key[i], sizeof(int), &val[i]);
    }

    sum_hash(h, &i, &trySumKey, &trySumVal);
    ABTS_INT_EQUAL(tc, 100, i);
    ABTS_INT_EQUAL(tc, sumVal, trySumVal);
    ABTS_INT_EQUAL(tc, sumKeys, trySumKey);

    hash_destroy(h);
}

static void delete_key(abts_case *tc, void *data)
{
    hash_t *h = NULL;
    char *result = NULL;

    h = hash_make();
    ABTS_PTR_NOTNULL(tc, h);

    hash_set(h, "key", HASH_KEY_STRING, "value");
    hash_set(h, "key2", HASH_KEY_STRING, "value2");

    result = hash_get(h, "key", HASH_KEY_STRING);
    ABTS_STR_EQUAL(tc, "value", result);

    result = hash_get(h, "key2", HASH_KEY_STRING);
    ABTS_STR_EQUAL(tc, "value2", result);

    hash_set(h, "key", HASH_KEY_STRING, NULL);

    result = hash_get(h, "key", HASH_KEY_STRING);
    ABTS_PTR_EQUAL(tc, NULL, result);

    result = hash_get(h, "key2", HASH_KEY_STRING);
    ABTS_STR_EQUAL(tc, "value2", result);

    hash_destroy(h);
}

static void hash_count_0(abts_case *tc, void *data)
{
    hash_t *h = NULL;
    int count;

    h = hash_make();
    ABTS_PTR_NOTNULL(tc, h);

    count = hash_count(h);
    ABTS_INT_EQUAL(tc, 0, count);

    hash_destroy(h);
}

static void hash_count_1(abts_case *tc, void *data)
{
    hash_t *h = NULL;
    int count;

    h = hash_make();
    ABTS_PTR_NOTNULL(tc, h);

    hash_set(h, "key", HASH_KEY_STRING, "value");

    count = hash_count(h);
    ABTS_INT_EQUAL(tc, 1, count);

    hash_destroy(h);
}

static void hash_count_5(abts_case *tc, void *data)
{
    hash_t *h = NULL;
    int count;

    h = hash_make();
    ABTS_PTR_NOTNULL(tc, h);

    hash_set(h, "key1", HASH_KEY_STRING, "value1");
    hash_set(h, "key2", HASH_KEY_STRING, "value2");
    hash_set(h, "key3", HASH_KEY_STRING, "value3");
    hash_set(h, "key4", HASH_KEY_STRING, "value4");
    hash_set(h, "key5", HASH_KEY_STRING, "value5");

    count = hash_count(h);
    ABTS_INT_EQUAL(tc, 5, count);

    hash_destroy(h);
}

abts_suite *testhash(abts_suite *suite)
{
    suite = ADD_SUITE(suite)

    abts_run_test(suite, hash_make_test, NULL);
    abts_run_test(suite, hash_set_test, NULL);
    abts_run_test(suite, hash_get_or_set_test, NULL);
    abts_run_test(suite, hash_reset, NULL);
    abts_run_test(suite, same_value, NULL);
    abts_run_test(suite, same_value_custom, NULL);
    abts_run_test(suite, key_space, NULL);
    abts_run_test(suite, delete_key, NULL);

    abts_run_test(suite, hash_count_0, NULL);
    abts_run_test(suite, hash_count_1, NULL);
    abts_run_test(suite, hash_count_5, NULL);

    abts_run_test(suite, hash_clear_test, NULL);
    abts_run_test(suite, hash_traverse, NULL);
    abts_run_test(suite, summation_test, NULL);

    return suite;
}