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