File: hashtabletest.c

package info (click to toggle)
xwayland 2%3A24.1.6-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 11,656 kB
  • sloc: ansic: 202,932; xml: 6,578; sh: 208; makefile: 21
file content (174 lines) | stat: -rw-r--r-- 3,134 bytes parent folder | download | duplicates (2)
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
#ifdef HAVE_DIX_CONFIG_H
#include <dix-config.h>
#endif

#include <misc.h>
#include <stdlib.h>
#include <stdio.h>
#include "hashtable.h"
#include "resource.h"

#include "tests-common.h"

static void
print_xid(void* ptr, void* v)
{
    XID *x = v;
    dbg("%ld", (long)(*x));
}

static void
print_int(void* ptr, void* v)
{
    int *x = v;
    dbg("%d", *x);
}

static void
test1(void)
{
    HashTable h;
    int c;
    int ok = 1;
    const int numKeys = 420;

    dbg("test1\n");
    h = ht_create(sizeof(XID), sizeof(int), ht_resourceid_hash, ht_resourceid_compare, NULL);

    for (c = 0; c < numKeys; ++c) {
      int *dest;
      XID id = c;
      dest = ht_add(h, &id);
      if (dest) {
        *dest = 2 * c;
      }
    }

    if (verbose) {
      dbg("Distribution after insertion\n");
      ht_dump_distribution(h);
      ht_dump_contents(h, print_xid, print_int, NULL);
    }

    for (c = 0; c < numKeys; ++c) {
      XID id = c;
      int* v = ht_find(h, &id);
      if (v) {
        if (*v == 2 * c) {
          // ok
        } else {
          dbg("Key %d doesn't have expected value %d but has %d instead\n",
                 c, 2 * c, *v);
          ok = 0;
        }
      } else {
        ok = 0;
        dbg("Cannot find key %d\n", c);
      }
    }

    if (ok) {
      dbg("%d keys inserted and found\n", c);

      for (c = 0; c < numKeys; ++c) {
        XID id = c;
        ht_remove(h, &id);
      }

      if (verbose) {
        dbg("Distribution after deletion\n");
        ht_dump_distribution(h);
      }
    }

    ht_destroy(h);

    assert(ok);
}

static void
test2(void)
{
    HashTable h;
    int c;
    int ok = 1;
    const int numKeys = 420;

    dbg("test2\n");
    h = ht_create(sizeof(XID), 0, ht_resourceid_hash, ht_resourceid_compare, NULL);

    for (c = 0; c < numKeys; ++c) {
      XID id = c;
      ht_add(h, &id);
    }

    for (c = 0; c < numKeys; ++c) {
      XID id = c;
      if (!ht_find(h, &id)) {
        ok = 0;
        dbg("Cannot find key %d\n", c);
      }
    }

    {
        XID id = c + 1;
        if (ht_find(h, &id)) {
            ok = 0;
            dbg("Could find a key that shouldn't be there\n");
        }
    }

    ht_destroy(h);

    if (ok) {
        dbg("Test with empty keys OK\n");
    } else {
        dbg("Test with empty keys FAILED\n");
    }

    assert(ok);
}

static void
test3(void)
{
    int ok = 1;
    HtGenericHashSetupRec hashSetup = {
        .keySize = 4
    };
    HashTable h;
    dbg("test3\n");
    h = ht_create(4, 0, ht_generic_hash, ht_generic_compare, &hashSetup);

    if (!ht_add(h, "helo") ||
        !ht_add(h, "wrld")) {
        dbg("Could not insert keys\n");
    }

    if (!ht_find(h, "helo") ||
        !ht_find(h, "wrld")) {
        ok = 0;
        dbg("Could not find inserted keys\n");
    }

    if (verbose) {
       dbg("Hash distribution with two strings\n");
       ht_dump_distribution(h);
    }

    ht_destroy(h);

    assert(ok);
}

const testfunc_t*
hashtabletest_test(void)
{
    static const testfunc_t testfuncs[] = {
        test1,
        test2,
        test3,
        NULL,
    };
    return testfuncs;
}