-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhash_fixed-test.c
More file actions
211 lines (154 loc) · 5.1 KB
/
hash_fixed-test.c
File metadata and controls
211 lines (154 loc) · 5.1 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "hash_fixed.h"
#include "hash_uint32.h"
#include "const_mem_pool.h"
#include "hash_test_common.h"
#include "prng.h"
#define words(x) ((x)/sizeof(void*))
static void *__alloc(void *cc,size_t n) {
return malloc(n);
}
static void __dealloc(void *cc,void *m) {
free(m);
}
static bool __filt_even(void *c, void *k, void *v) {
return !! ( *(uint32_t*)k % 2);
}
static void __print(void *c, void *k, void *v) {
fprintf(stdout, "(%u,%u)\n", *(uint32_t*)k, *(uint32_t*)v);
}
void test_hash_fixed_1(void) {
const size_t bkt = 32;
char mem[hash_mem_size_fixed(bkt, 100, sizeof(uint32_t), sizeof(uint32_t))];
fprintf(stderr, "mem size: %zu\n\n", sizeof(mem));
fprintf(stderr, "const_mem_pool size: %zu\n\n", sizeof(struct const_mem_pool));
fprintf(stderr, "hash_size %zu\n\n", hash_size);
fprintf( stderr
, "hash_minimal_mem_size %zu\n\n"
, hash_minimal_mem_size(bkt, 100, sizeof(uint32_t), sizeof(uint32_t))
);
struct hash *h = hash_create_fixed( sizeof(mem)
, mem
, sizeof(uint32_t)
, sizeof(uint32_t)
, bkt
, uint32_hash
, uint32_eq
, uint32_cpy
, uint32_cpy );
fprintf(stdout, "hash allocated? %s\n\n", h ? "yes" : "no");
size_t i = 0;
for(;;i++) {
uint32_t k = i;
uint32_t v = i;
if( !hash_add(h, &k, &v) ) {
break;
}
}
fprintf(stdout, "added something? %s\n\n", i > 0 ? "yes" : "no");
// platform-dependend
fprintf(stderr, "added %zu\n\n", i);
size_t j = 0;
for(; j < i; j++ ) {
uint32_t k = j;
if( !hash_get(h, &k) ) {
break;
}
}
fprintf(stdout, "found all ? %s\n\n", i == j ? "yes" : "no");
print_hash_stat(h);
hash_filter(h, 0, 0);
fprintf(stdout, "\nfiltered off\n\n");
print_hash_stat(h);
for(i=0;;i++) {
uint32_t k = i;
uint32_t v = i;
if( !hash_add(h, &k, &v) ) {
break;
}
}
// platform-dependend
fprintf(stdout, "\nadded: %zu\n\n", i);
print_hash_stat(h);
hash_filter(h, 0, __filt_even);
fprintf(stdout, "\nfiltered even\n\n");
print_hash_stat(h);
fprintf(stdout, "\ntrying shrink -- should make no effect\n\n");
hash_shrink(h, true);
print_hash_stat(h);
hash_destroy(h);
}
static void __alter_u32_u32(void *cc, void *k, void *v, bool n) {
*(uint32_t*)v = *(uint32_t*)cc;
}
struct nested_1_alter_cc {
ranctx *gen;
size_t fhs;
};
static void __hash_fixed_nested_1_alter(void *cc_, void *k, void *v, bool n) {
struct hash *h = 0;
struct nested_1_alter_cc *cc = cc_;
if( n ) {
h = hash_create_fixed( cc->fhs
, v
, sizeof(uint32_t)
, sizeof(uint32_t)
, 64
, uint32_hash
, uint32_eq
, uint32_cpy
, uint32_cpy
);
} else {
h = v;
}
assert(h);
size_t i = 0;
for(; i < 20; i++ ) {
uint32_t key = i;
uint32_t val = ranval(cc->gen) % 100000;
if( hash_alter(h, true, &key, &val, __alter_u32_u32) ) {
uint32_t *v_ = hash_get(h, &key);
fprintf(stdout, "%u (%u,%u)\n", *(uint32_t*)k, key, *v_);
}
}
}
static void __fixed_nested_1_enum_2(void *cc, void *k, void *v) {
fprintf(stdout, "%u (%u,%u)\n", *(uint32_t*)cc, *(uint32_t*)k, *(uint32_t*)v);
}
static void __fixed_nested_1_enum_1(void *cc, void *k, void *v) {
hash_enum(v, k, __fixed_nested_1_enum_2);
}
void test_hash_fixed_nested_1(void) {
ranctx rgen;
raninit(&rgen, 0x010101010101);
char mem[hash_size];
const size_t fhs = hash_mem_size_fixed(64, 8, sizeof(uint32_t), sizeof(uint32_t));
struct hash *c = hash_create( sizeof(mem)
, mem
, sizeof(uint32_t)
, fhs
, 64
, uint32_hash
, uint32_eq
, uint32_cpy
, 0 // not a POD structure, no copy
, 0
, __alloc
, __dealloc
);
const size_t N = 10;
size_t i = 0;
struct nested_1_alter_cc cc = { .gen = &rgen, .fhs = fhs };
for(; i < N; i++ ) {
uint32_t tmp= i;
hash_alter(c, true, &tmp, &cc, __hash_fixed_nested_1_alter);
}
fprintf(stdout, "\n---\n");
hash_enum(c, 0, __fixed_nested_1_enum_1);
hash_destroy(c);
}