| 1 |
/* |
| 2 |
* COPYRIGHT (c) International Business Machines Corp. 2021 |
| 3 |
* |
| 4 |
* This program is provided under the terms of the Common Public License, |
| 5 |
* version 1.0 (CPL-1.0). Any use, reproduction or distribution for this |
| 6 |
* software constitutes recipient's acceptance of CPL-1.0 terms which can be |
| 7 |
* found in the file LICENSE file or at |
| 8 |
* https://opensource.org/licenses/cpl1.0.php |
| 9 |
*/ |
| 10 |
#include "hashmap.h" |
| 11 |
#include "unittest.h" |
| 12 |
#include <stdio.h> |
| 13 |
#include <stdlib.h> |
| 14 |
#include <errno.h> |
| 15 |
#include <unistd.h> |
| 16 |
#include <limits.h> |
| 17 |
#include "platform.h" |
| 18 |
|
| 19 |
static int testhashcollisionexpansion(void) |
| 20 |
{ |
| 21 |
CK_ULONG values[] = |
| 22 |
{ |
| 23 |
/* First 12 values that should end up in the same bucket if |
| 24 |
simplest hash function is used. Make sure they also end |
| 25 |
up in the same bucket after the first expansion. */ |
| 26 |
0x100, 0x200, 0x300, 0x400, 0x500, 0x600, 0x700, 0x800, 0x900, |
| 27 |
0xa00, 0xb00, |
| 28 |
/* Next 12 values that should also end up in the same bucket |
| 29 |
after the expansion. */ |
| 30 |
0x1100, 0x1200, 0x1300, 0x1400, 0x1500, 0x1600, 0x1700, 0x1800, |
| 31 |
0x1900, 0x0a00, 0x1b00 |
| 32 |
}; |
| 33 |
union hashmap_value val = { .ulVal = 0 }; |
| 34 |
struct hashmap *h; |
| 35 |
unsigned long i; |
| 36 |
int res = 0; |
| 37 |
|
| 38 |
h = hashmap_new(); |
| 39 |
if (!h) { |
| 40 |
fprintf(stderr, "Failed to allocate mesh hash\n"); |
| 41 |
return -1; |
| 42 |
} |
| 43 |
for (i = 0; i < ARRAYSIZE(values) / 2; ++i) { |
| 44 |
if (hashmap_add(h, values[i], val, NULL)) { |
| 45 |
fprintf(stderr, "Failed to add element %lu to meshhash\n", i); |
| 46 |
res = -1; |
| 47 |
goto out; |
| 48 |
} |
| 49 |
} |
| 50 |
for (i = 0; i < ARRAYSIZE(values) / 2; ++i) { |
| 51 |
if (!hashmap_find(h, values[i], NULL)) { |
| 52 |
fprintf(stderr, "Lost element %lu (%lu) in hashmap\n", |
| 53 |
i, values[i]); |
| 54 |
res = -1; |
| 55 |
goto out; |
| 56 |
} |
| 57 |
} |
| 58 |
for ( ; i < ARRAYSIZE(values); ++i) { |
| 59 |
if (hashmap_add(h, values[i], val, NULL)) { |
| 60 |
fprintf(stderr, "Failed to add element %lu to meshhash\n", i); |
| 61 |
res = -1; |
| 62 |
goto out; |
| 63 |
} |
| 64 |
} |
| 65 |
for (i = 0; i < ARRAYSIZE(values); ++i) { |
| 66 |
if (!hashmap_find(h, values[i], NULL)) { |
| 67 |
fprintf(stderr, |
| 68 |
"Lost element %lu (%lu) in hashmap after expansion\n", |
| 69 |
i, values[i]); |
| 70 |
res = -1; |
| 71 |
goto out; |
| 72 |
} |
| 73 |
} |
| 74 |
out: |
| 75 |
hashmap_free(h, NULL); |
| 76 |
return res; |
| 77 |
} |
| 78 |
|
| 79 |
static int compareulong(const void *a, const void *b) |
| 80 |
{ |
| 81 |
unsigned long la = *(const unsigned long *)a; |
| 82 |
unsigned long lb = *(const unsigned long *)b; |
| 83 |
|
| 84 |
if (la < lb) |
| 85 |
return -1; |
| 86 |
if (la == lb) |
| 87 |
return 0; |
| 88 |
return 1; |
| 89 |
} |
| 90 |
|
| 91 |
static int testhashrandom(unsigned long seed, unsigned long iterations) |
| 92 |
{ |
| 93 |
unsigned long i, *arr, mid, unsucccnt; |
| 94 |
union hashmap_value val = { .ulVal = 0}; |
| 95 |
struct hashmap *h; |
| 96 |
int res = 0; |
| 97 |
|
| 98 |
h = hashmap_new(); |
| 99 |
if (!h) { |
| 100 |
fprintf(stderr, "Could not allocate hashmap\n"); |
| 101 |
return -1; |
| 102 |
} |
| 103 |
arr = calloc(iterations, sizeof(unsigned long)); |
| 104 |
if (!arr) { |
| 105 |
fprintf(stderr, "Failed to allocate data array\n"); |
| 106 |
res = -1; |
| 107 |
goto out; |
| 108 |
} |
| 109 |
srandom(seed); |
| 110 |
for (i = 0; i < iterations; ++i) |
| 111 |
arr[i] = random(); |
| 112 |
for (i = 0; i < iterations; ++i) { |
| 113 |
if (hashmap_add(h, arr[i], val, NULL)) { |
| 114 |
fprintf(stderr, "Failed to add %lu to hash\n", arr[i]); |
| 115 |
res = -1; |
| 116 |
goto out; |
| 117 |
} |
| 118 |
} |
| 119 |
/* successful searches */ |
| 120 |
for (i = 0; i < iterations; ++i) { |
| 121 |
if (!hashmap_find(h, arr[i], NULL)) { |
| 122 |
fprintf(stderr, "Failed to find %lu in hash\n", arr[i]); |
| 123 |
res = -1; |
| 124 |
goto out; |
| 125 |
} |
| 126 |
} |
| 127 |
/* unsuccessful searches */ |
| 128 |
unsucccnt = 0; |
| 129 |
qsort(arr, iterations, sizeof(unsigned long), compareulong); |
| 130 |
for (i = 0; i < iterations - 1; ++i) { |
| 131 |
mid = arr[i] + (arr[i + 1] - arr[i]) / 2; |
| 132 |
if (mid != arr[i]) { |
| 133 |
++unsucccnt; |
| 134 |
if (hashmap_find(h, mid, NULL)) { |
| 135 |
fprintf(stderr, "Found non-existing element %lu in hash\n", |
| 136 |
mid); |
| 137 |
res = -1; |
| 138 |
goto out; |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
fprintf(stderr, "Performed %lu (expected) unsuccessful searches\n", |
| 143 |
unsucccnt); |
| 144 |
out: |
| 145 |
free(arr); |
| 146 |
hashmap_free(h, NULL); |
| 147 |
return res; |
| 148 |
} |
| 149 |
|
| 150 |
static int parseulong(const char *str, unsigned long *res) |
| 151 |
{ |
| 152 |
unsigned long tmp; |
| 153 |
char *endptr; |
| 154 |
|
| 155 |
errno = 0; |
| 156 |
tmp = strtoul(str, &endptr, 0); |
| 157 |
if (*endptr || (tmp == ULONG_MAX && errno == ERANGE)) |
| 158 |
return 1; |
| 159 |
*res = tmp; |
| 160 |
return 0; |
| 161 |
} |
| 162 |
|
| 163 |
int main(int argc, char **argv) |
| 164 |
{ |
| 165 |
unsigned long seed = 0, iterations = 100; |
| 166 |
static struct option long_options[] = |
| 167 |
{ |
| 168 |
{"seed", required_argument, 0, 's'}, |
| 169 |
{"iterations", required_argument, 0, 'i'}, |
| 170 |
{0, 0, 0, 0 } |
| 171 |
}; |
| 172 |
int c; |
| 173 |
|
| 174 |
while (1) { |
| 175 |
c = getopt_long(argc, argv, "s:i:", long_options, NULL); |
| 176 |
if (c == -1) |
| 177 |
break; |
| 178 |
switch(c) { |
| 179 |
case 's': |
| 180 |
if (parseulong(optarg, &seed)) { |
| 181 |
fprintf(stderr, "Seed could not be parsed!\n"); |
| 182 |
return TEST_SKIP; |
| 183 |
} |
| 184 |
break; |
| 185 |
case 'i': |
| 186 |
if (parseulong(optarg, &iterations)) { |
| 187 |
fprintf(stderr, "Iterations could not be parsed!\n"); |
| 188 |
return TEST_SKIP; |
| 189 |
} |
| 190 |
break; |
| 191 |
default: |
| 192 |
printf("USAGE: %s [-s|--seed <num>] [-i|--iterations <num>]\n", |
| 193 |
argv[0]); |
| 194 |
printf("where the parameters configure the random hash test:\n"); |
| 195 |
printf("-s or --seed specifies the random seed\n"); |
| 196 |
printf("-i or --iterations specifies the number of iterations to perform\n"); |
| 197 |
return TEST_SKIP; |
| 198 |
} |
| 199 |
} |
| 200 |
if (testhashcollisionexpansion()) |
| 201 |
return TEST_FAIL; |
| 202 |
if (testhashrandom(seed, iterations)) |
| 203 |
return TEST_FAIL; |
| 204 |
/* hashmap_delete function is untested since it is unused so far */ |
| 205 |
return TEST_PASS; |
| 206 |
} |