-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_main.c
157 lines (151 loc) · 3.5 KB
/
test_main.c
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
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
//#define NDEBUG
#include <assert.h>
#include <errno.h>
#ifdef _INTKEYS
uint32_t inthash(const uint32_t key);
#else
uint32_t hash(const void * __restrict key, size_t keylen);
#endif
#define PERF_ROUNDS 100000
int main(int argc, char **argv)
{
char *input;
char *line;
ssize_t line_len;
size_t line_allocated;
FILE *f;
int verbose = 0;
unsigned i = 1;
uint32_t h;
#if defined bdz && !defined _NOMAP
uint32_t *map;
#elif defined _INTKEYS
int32_t *map;
#endif
if (argc > 1 && strcmp(argv[i], "-v") == 0)
verbose = 1, i++;
input = i > 0 ? argv[i] : "_words1000";
#ifdef bdz
char mapfile[80];
if (input) {
assert(strlen(input) < 80);
strncpy(mapfile, input, 79);
strcat(mapfile, ".map");
} else
strcpy(mapfile, "_words.map");
#endif
#ifndef PERF
# ifdef _INTKEYS
h = inthash(1);
if (verbose)
printf("%u: %u\n", 1, h);
# else
char *w = "englis\0\0";
h = hash(w, strlen(w));
if (verbose)
printf("%s: %d\n", w, h); // false-positive! englis == Luz's
# endif
#endif
#if defined bdz && !defined _NOMAP
size_t lines = 1000;
map = calloc (lines, 4);
i = 0;
// read map file for the indices
f = fopen(mapfile, "r");
if (!f) {
perror("fopen mapfile");
exit(1);
}
errno = 0;
while (1 == fscanf(f, "%u\n", &map[i]) && !errno) {
i++;
if (i >= lines) {
fprintf(stderr, "more than %lu lines in %s\n", lines, mapfile);
lines *= 2;
map = realloc (map, lines * 4);
}
}
fclose(f);
#elif defined _INTKEYS
size_t lines = 1000;
map = calloc (lines, 4);
i = 0;
// read input file for the indices
f = fopen(input, "r");
if (!f) {
perror("fopen input");
exit(1);
}
errno = 0;
while (1 == fscanf(f, "%d\n", &map[i]) && !errno) {
i++;
if (i >= lines) {
fprintf(stderr, "more than %lu lines in %s\n", lines, input);
lines *= 2;
map = realloc (map, lines * 4);
}
}
fclose(f);
#endif
f = fopen(input, "r");
if (!f) {
perror("fopen input");
exit(1);
}
i = 0;
line = NULL;
line_allocated = 0;
while ((line_len = getline(&line, &line_allocated, f)) != -1) {
if (line_len && line[line_len - 1] == '\n') {
--line_len;
line[line_len] = '\0';
}
#ifdef _INTKEYS
int32_t l = atoi(line);
#endif
#ifdef PERF
for (int j=0; j < PERF_ROUNDS; j++) {
#endif
#ifdef _INTKEYS
h = inthash(l);
#else
h = hash(line, strlen(line));
#endif
#ifdef PERF
} // perf loop
#endif
if (verbose)
#if defined _INTKEYS || (defined bdz && !defined _NOMAP)
printf("%s[%u]: %d == %d\n", line, i, (int)h, (int)map[i]);
#else
printf("%s[%u]: %d\n", line, i, h);
#endif
#ifndef PERF
# if (defined chm || defined chm3 || defined _NOMAP) && !defined _INTKEYS
if (h != i && verbose)
printf("%s[%u]: %d != %d\n", line, i, i, h);
assert(h == i);
#else
#if defined _INTKEYS && !defined bdz
if (l != map[h] && verbose)
printf("%s[%u]: %d != %d (%d)\n", line, i, l, map[h], h);
assert(l == map[h]);
#else // bdz
if (map[h] != i && verbose)
printf("%s[%u]: %d != %u (%d)\n", line, i, i, map[h], h);
//assert(map[h] == i); // GH #15
#endif
#endif
#endif
i++;
}
free(line);
#if defined _INTKEYS || (defined bdz && !defined _NOMAP)
free(map);
#endif
fclose(f);
}