-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathctrie_test.c
160 lines (136 loc) · 3.57 KB
/
ctrie_test.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
158
159
160
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdbool.h>
#include <string.h>
#include <time.h>
#include <sys/stat.h>
#include <fts.h>
#include "ctrie.h"
#define SZ (100000)
#define KEYSZ 200
#define xstr(s) str(s)
#define str(s) #s
static uint32_t hash(const char key[]) {
size_t length;
size_t i;
uint32_t h = 0;
if (key == NULL) {
return 0;
}
length = strlen(key);
for (i = 0; i < length; ++i) {
h = (31 * h) + key[i];
}
h ^= (h >> 20) ^ (h >> 12);
return h ^ (h >> 7) ^ (h >> 4);
}
void ctrie_test_insert() {
PTR(ctrie_t) ctrie = ctrie_new();
if (!ctrie) {
perror("ctrie_new");
abort();
}
clock_t insert = 0;
clock_t gen = 0;
char **inputs = malloc(sizeof(char*) * SZ);
if (!inputs) {
perror("malloc");
abort();
}
//FILE *urandom = fopen("/dev/urandom", "r");
for (size_t i = 0; i < SZ; ++i) {
clock_t genstart = clock();
inputs[i] = malloc(sizeof(char) * (KEYSZ + 1));
if (!inputs[i]) {
perror("malloc");
abort();
}
//size_t inputidx = 0;
//while (inputidx < KEYSZ) {
// int c = fgetc(urandom);
// if (c > 'a' && c <= 'z') {
// inputs[i][inputidx] = c;
// inputidx++;
// }
//}
//inputs[i][KEYSZ] = '\0';
snprintf(inputs[i], KEYSZ + 1, "%" xstr(KEYSZ) "zd", i);
gen += (clock() - genstart);
value_t value;
value.counter = hash(inputs[i]);
value.flags = i;
//fprintf(stderr, "inserting key %zd: %s (hash: 0x%x)...\n", i, inputs[i], hash(inputs[i]));
clock_t start = clock();
if (ctrie_insert(ctrie, inputs[i], &value) != INSERT_SUCCESS) {
fprintf(stderr, "Failed to insert key %s\n", inputs[i]);
abort();
}
insert += (clock() - start);
#if 0
value_t output;
lookup_result_t res = ctrie_lookup(ctrie, inputs[i], &output);
if (res != LOOKUP_FOUND) {
fprintf(stderr, "You idiot! I just inserted %s, but it's not there. Obviously you suck at this.\n", inputs[i]);
ctrie_print(ctrie);
abort();
}
#endif
}
fprintf(stderr, "time gen: %Lf\n", ((long double)gen) / CLOCKS_PER_SEC);
fprintf(stderr, "time insert: %Lf\n", ((long double)insert) / CLOCKS_PER_SEC);
bool failed = false;
clock_t lookup = 0;
for (size_t i = 0; i < SZ; ++i) {
value_t val;
clock_t start = clock();
lookup_result_t res = ctrie_lookup(ctrie, inputs[i], &val);
lookup += (clock() - start);
if (res != LOOKUP_FOUND) {
fprintf(stderr, "Failed to find entry %s in ctrie\n", inputs[i]);
continue;
}
if (val.flags != i) {
fprintf(stderr, "value flags of entry %s were modified to %x\n", inputs[i], val.flags);
failed = true;
}
}
fprintf(stderr, "time lookup: %Lf\n", ((long double)lookup) / CLOCKS_PER_SEC);
if (failed) {
ctrie_print(ctrie);
}
}
void ctrie_add_filetree() {
PTR(ctrie_t) ctrie = ctrie_new();
if (!ctrie) {
perror("ctrie_new");
abort();
}
clock_t insert = 0;
char* const path[] = {".", NULL};
FTS* fts = fts_open(path, FTS_NOCHDIR, 0);
if (!fts) {
perror("fts_open");
abort();
}
FTSENT* node;
size_t i = 0;
while ((node = fts_read(fts)) != NULL) {
value_t value;
value.counter = ++i;
value.flags = node->fts_info;
clock_t start = clock();
ctrie_insert(ctrie, node->fts_path, &value);
insert += (clock() - start);
}
fprintf(stderr, "insert time: %Lf\n", ((long double) insert) / CLOCKS_PER_SEC);
fts_close(fts);
ctrie_print(ctrie);
}
int main() {
ctrie_test_insert();
ctrie_add_filetree();
}