File: test_hash.c

package info (click to toggle)
libstrophe 0.14.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,356 kB
  • sloc: ansic: 16,695; makefile: 301; sh: 61
file content (158 lines) | stat: -rw-r--r-- 3,907 bytes parent folder | download | duplicates (3)
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
/* SPDX-License-Identifier: MIT OR GPL-3.0-only */
/* test_hash.c
** libstrophe XMPP client library -- self-test for the hash-table implementation
**
** Copyright (C) 2005-2009 Collecta, Inc.
**
**  This software is provided AS-IS with no warranty, either express
**  or implied.
**
** This program is dual licensed under the MIT or GPLv3 licenses.
*/

#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "strophe.h"
#include "common.h"
#include "hash.h"
#include "test.h"

#define TABLESIZE 100
#define TESTSIZE 500

/* static test data */
const char *keys[] = {"foo", "bar", "baz", "quux", "xyzzy"};
const char *values[] = {"wuzzle", "mug", "canonical", "rosebud", "lottery"};
const int nkeys = ARRAY_SIZE(keys);

int main(int argc, char **argv)
{
    xmpp_ctx_t *ctx;
    hash_t *table, *clone;
    hash_iterator_t *iter;
    unsigned int seed;
    const char *key;
    char *result;
    int err = 0;
    int i;

    /* initialize random numbers */
    if (argc > 2) {
        /* use a seed from the command line */
        seed = (unsigned int)atoi(argv[1]);
    } else {
        seed = (unsigned int)clock();
    }
    /* using random seed 'seed' */
    srand(seed);

    /* allocate a default context */
    ctx = xmpp_ctx_new(NULL, NULL);
    if (ctx == NULL) {
        /* ctx allocation failed! */
        return -1;
    }

    /* allocate a hash table */
    table = hash_new(ctx, TABLESIZE, strophe_free);
    if (table == NULL) {
        /* table allocation failed! */
        return 1;
    }

    /* test insertion */
    for (i = 0; i < nkeys; i++) {
        err = hash_add(table, keys[i], strophe_strdup(ctx, values[i]));
        if (err)
            return err;
    }

    /* test key count */
    if (hash_num_keys(table) != nkeys) {
        /* wrong number of keys in table! */
        return 1;
    }

    /* test replacing old values */
    for (i = 0; i < nkeys; i++) {
        err = hash_add(table, keys[0], strophe_strdup(ctx, values[i]));
        if (err)
            return err;
        if (hash_num_keys(table) != nkeys)
            return 1;
        result = hash_get(table, keys[0]);
        if (result == NULL)
            return 1;
        if (strcmp(result, values[i]) != 0)
            return 1;
    }
    /* restore value for the 1st key */
    hash_add(table, keys[0], strophe_strdup(ctx, values[0]));

    /* test cloning */
    clone = hash_clone(table);

    /* test lookup */
    for (i = 0; i < nkeys; i++) {
        result = hash_get(clone, keys[i]);
        if (result == NULL) {
            /* lookup failed! */
            return 1;
        }
        if (strcmp(values[i], result)) {
            /* lookup returned incorrect value! */
            return 1;
        }
    }

    /* test key iterator */
    iter = hash_iter_new(clone);
    if (iter == NULL) {
        /* iterator allocation failed! */
        return 1;
    }
    for (i = 0; i < nkeys; i++) {
        key = hash_iter_next(iter);
        printf("key: '%s'\n", key);
    }
    key = hash_iter_next(iter);
    if (key != NULL) {
        /* extra keys returned! */
        return 1;
    }
    key = hash_iter_next(iter);
    if (key != NULL) {
        /* extra keys returned! */
        return 1;
    }
    hash_iter_release(iter);

    /* release the hash table */
    hash_release(table);

    /* test drops */
    hash_drop(clone, keys[2]);
    if (hash_get(clone, keys[2]) != NULL)
        return 1;
    hash_drop(clone, keys[1]);
    hash_drop(clone, keys[4]);
    if (hash_get(clone, keys[4]) != NULL)
        return 1;
    if (hash_get(clone, keys[1]) != NULL)
        return 1;
    /* keys 0,3 should still be available */
    if (hash_get(clone, keys[0]) == NULL)
        return 1;
    if (hash_get(clone, keys[3]) == NULL)
        return 1;

    /* release our clone */
    hash_release(clone);

    /* release our library context */
    xmpp_ctx_free(ctx);

    return err;
}