hashtabletest.c revision 35c4bbdf
1#ifdef HAVE_DIX_CONFIG_H
2#include <dix-config.h>
3#endif
4
5#include <misc.h>
6#include <stdlib.h>
7#include <stdio.h>
8#include "hashtable.h"
9#include "resource.h"
10
11static void
12print_xid(void* ptr, void* v)
13{
14    XID *x = v;
15    printf("%ld", (long)(*x));
16}
17
18static void
19print_int(void* ptr, void* v)
20{
21    int *x = v;
22    printf("%d", *x);
23}
24
25static int
26test1(void)
27{
28    HashTable h;
29    int c;
30    int ok = 1;
31    const int numKeys = 420;
32
33    printf("test1\n");
34    h = ht_create(sizeof(XID), sizeof(int), ht_resourceid_hash, ht_resourceid_compare, NULL);
35
36    for (c = 0; c < numKeys; ++c) {
37      int *dest;
38      XID id = c;
39      dest = ht_add(h, &id);
40      if (dest) {
41        *dest = 2 * c;
42      }
43    }
44
45    printf("Distribution after insertion\n");
46    ht_dump_distribution(h);
47    ht_dump_contents(h, print_xid, print_int, NULL);
48
49    for (c = 0; c < numKeys; ++c) {
50      XID id = c;
51      int* v = ht_find(h, &id);
52      if (v) {
53        if (*v == 2 * c) {
54          // ok
55        } else {
56          printf("Key %d doesn't have expected value %d but has %d instead\n",
57                 c, 2 * c, *v);
58          ok = 0;
59        }
60      } else {
61        ok = 0;
62        printf("Cannot find key %d\n", c);
63      }
64    }
65
66    if (ok) {
67      printf("%d keys inserted and found\n", c);
68
69      for (c = 0; c < numKeys; ++c) {
70        XID id = c;
71        ht_remove(h, &id);
72      }
73
74      printf("Distribution after deletion\n");
75      ht_dump_distribution(h);
76    }
77
78    ht_destroy(h);
79
80    return ok;
81}
82
83static int
84test2(void)
85{
86    HashTable h;
87    int c;
88    int ok = 1;
89    const int numKeys = 420;
90
91    printf("test2\n");
92    h = ht_create(sizeof(XID), 0, ht_resourceid_hash, ht_resourceid_compare, NULL);
93
94    for (c = 0; c < numKeys; ++c) {
95      XID id = c;
96      ht_add(h, &id);
97    }
98
99    for (c = 0; c < numKeys; ++c) {
100      XID id = c;
101      if (!ht_find(h, &id)) {
102        ok = 0;
103        printf("Cannot find key %d\n", c);
104      }
105    }
106
107    {
108        XID id = c + 1;
109        if (ht_find(h, &id)) {
110            ok = 0;
111            printf("Could find a key that shouldn't be there\n");
112        }
113    }
114
115    ht_destroy(h);
116
117    if (ok) {
118        printf("Test with empty keys OK\n");
119    } else {
120        printf("Test with empty keys FAILED\n");
121    }
122
123    return ok;
124}
125
126static int
127test3(void)
128{
129    int ok = 1;
130    HtGenericHashSetupRec hashSetup = {
131        .keySize = 4
132    };
133    HashTable h;
134    printf("test3\n");
135    h = ht_create(4, 0, ht_generic_hash, ht_generic_compare, &hashSetup);
136
137    if (!ht_add(h, "helo") ||
138        !ht_add(h, "wrld")) {
139        printf("Could not insert keys\n");
140    }
141
142    if (!ht_find(h, "helo") ||
143        !ht_find(h, "wrld")) {
144        ok = 0;
145        printf("Could not find inserted keys\n");
146    }
147
148    printf("Hash distribution with two strings\n");
149    ht_dump_distribution(h);
150
151    ht_destroy(h);
152
153    return ok;
154}
155
156int
157main(void)
158{
159    int ok = test1();
160    ok = ok && test2();
161    ok = ok && test3();
162
163    return ok ? 0 : 1;
164}
165