xs/test.xs

filter: Statementstmt 100% 156 / 156 Branchbran 93.7% 75 / 80 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 97.8% 231 / 236 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/hamt/hamt_check.h ↑ Summary include/hamt/hamt_entry.h »
1 # Doors for the tests and for nobody else. Each name starts with an
2 # underscore, none is documented, and any may change in any release.
3  
4 MODULE = HAMT PACKAGE = HAMT
5  
6 # Pin the seed, so a test can ask for the same layout twice.
7 void
8 _seed(n)
9 UV n
10 CODE:
11 5 hamt_seed = (uint64_t)n;
12  
13 # Keep only the low `bits` bits of every hash from here on: 0 makes every key
14 # collide, 5 fills one level and then collides. 64 puts the whole hash back.
15 # A container built under one setting must not be read under another.
16 void
17 _hash_bits(bits)
18 UV bits
19 CODE:
20 56 ▶ hamt_hash_set_bits(bits > 64 ? 64 : (unsigned)bits);
Branch: 1 branch
BranchTrueFalseTotal
hamt_hash_set_bits(bits > 64 ? 64 : (unsigned)bits); 55 1 56
21  
22 # The hash of a key that has already been through the key rule, as sixteen
23 # hex digits: a 64-bit number does not fit an IV on every perl.
24 SV *
25 _hash_of(bytes, utf8 = 0)
26 SV *bytes
27 int utf8
28 PREINIT:
29 STRLEN len;
30 const char *p;
31 uint64_t h;
32 CODE:
33 1230 p = SvPVbyte(bytes, len);
34 1230 h = hamt_hash(p, (size_t)len, utf8);
35 1230 RETVAL = newSVpvf("%08lx%08lx",
36 (unsigned long)(h >> 32),
37 (unsigned long)(h & 0xffffffffUL));
38 OUTPUT:
39 RETVAL
40  
41 # An entry made and released in C, reporting what it held on the way: the
42 # key's length, its flag, and whether the key compares equal to itself and
43 # unequal to the same bytes under the other flag. The value's reference count
44 # is the caller's to check before and after.
45 void
46 _entry_probe(bytes, utf8, value)
47 SV *bytes
48 int utf8
49 SV *value
50 PREINIT:
51 STRLEN len;
52 const char *p;
53 uint64_t h;
54 hamt_entry *e;
55 IV held, self_eq, flag_eq;
56 PPCODE:
57 7 p = SvPVbyte(bytes, len);
58 7 h = hamt_hash(p, (size_t)len, utf8);
59 7 e = hamt_entry_new(HAMT_CX, p, (size_t)len, utf8, h, (void *)value);
60 7 ▶ if (!e) croak("HAMT::_entry_probe: out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!e) croak("HAMT::_entry_probe: out of memory"); 1 6 7
61 6 hamt_entry_retain(e);
62 6 hamt_entry_release(HAMT_CX, e);
63 6 held = (IV)SvREFCNT(value);
64 6 self_eq = hamt_entry_key_eq(e, p, (size_t)len, utf8, h);
65 6 flag_eq = hamt_entry_key_eq(e, p, (size_t)len, !utf8,
66 hamt_hash(p, (size_t)len, !utf8));
67 6 hamt_entry_release(HAMT_CX, e);
68 6 ▶ EXTEND(SP, 3);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 3); 0 6 6
69 6 mPUSHi(held);
70 6 mPUSHi(self_eq);
71 6 mPUSHi(flag_eq);
72  
73 # An object blessed into `klass` that carries ext magic, but not this
74 # library's: what a handle from some other extension looks like. Every
75 # method has to refuse it, since its magic points at somebody else's struct.
76 void
77 _foreign_handle(klass)
78 SV *klass
79 PREINIT:
80 static MGVTBL other = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
81 static char payload[64];
82 SV *inner, *rv;
83 CODE:
84 3 inner = newSV(0);
85 3 sv_magicext(inner, NULL, PERL_MAGIC_ext, &other, payload, 0);
86 3 rv = sv_2mortal(newRV_noinc(inner));
87 3 sv_bless(rv, gv_stashsv(klass, GV_ADD));
88 3 ST(0) = rv;
89 3 XSRETURN(1);
90  
91 # How many temporaries perl is holding right now. A walk that calls a sub
92 # for every entry must not let this grow with the walk.
93 void
94 _tmps_ix()
95 CODE:
96 4500 ST(0) = sv_2mortal(newSViv((IV)PL_tmps_ix));
97 4500 XSRETURN(1);
98  
99 # The n-th allocation the library makes from now on fails, as if there were
100 # no memory, and the count is then off again; 0 turns it off. A test walks
101 # n up from one until an operation succeeds, so that every allocation the
102 # operation makes has failed once.
103 void
104 _alloc_fail_in(n)
105 IV n
106 CODE:
107 43037 hamt_alloc_fail_in = n < 0 ? 0 : n;
108  
109 # How many blocks the library holds: every node, every entry, every handle's
110 # struct. Back to where it started means nothing was leaked.
111 void
112 _live_blocks()
113 CODE:
114 21553 ST(0) = sv_2mortal(newSViv((IV)hamt_live_blocks));
115 21553 XSRETURN(1);
116  
117 # How many freed blocks the allocator is holding for reuse. -1 in a build
118 # that has no pool, and -2 in one that has but where it is not open, which
119 # is a fault and must not read as the first.
120 void
121 _pool_held()
122 CODE:
123 #ifndef HAMT_NO_POOL
124 20005 ▶ ST(0) = sv_2mortal(newSViv(hamt_pool_one.open
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = sv_2mortal(newSViv(hamt_pool_one.open 20005 0 20005
125 ? (IV)hamt_pool_held(&hamt_pool_one) : -2));
126 #else
127 ST(0) = sv_2mortal(newSViv(-1));
128 #endif
129 20005 XSRETURN(1);
130  
131 # The core's persistent delete, which the methods leave to set subtraction
132 # (they delete in place on a copy): 1 for a key that was there, 0 for one
133 # that was not, -1 for no memory. The trie that comes back is let go.
134 IV
135 _del_persistent(self, key)
136 SV *self
137 SV *key
138 PREINIT:
139 hamt_map *m;
140 hamt_key k;
141 hamt_node *r;
142 int status;
143 CODE:
144 16 m = hamt_handle(aTHX_ self, &hamt_map_vtbl, "HAMT::_del_persistent", "map");
145 16 hamt_key_from_sv(aTHX_ key, &k);
146 16 r = hamt_map_del(HAMT_CX, m->root, k.p, (size_t)k.len, k.utf8, k.hash, &status);
147 16 ▶ if (r) hamt_node_release(HAMT_CX, r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) hamt_node_release(HAMT_CX, r); 9 7 16
148 16 ▶ RETVAL = status;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = status; 6 10 16
149 OUTPUT:
150 RETVAL
151  
152 # For the checker's own tests: a trie built by hand and broken one way, by
153 # number, and what hamt_map_check says of it. The numbers are the rules of
154 # hamt_check.h; 21 and up are a rule met at another place. 0 is the trie
155 # as built. It is put right again before it is freed. Nothing here is
156 # checked for memory: these are run with memory.
157 IV
158 _map_broken(what)
159 IV what
160 PREINIT:
161 hamt_node *root, *child, *bottom, *chain[13];
162 hamt_entry *e0, *e0b, *e1, *e3, *e32;
163 19 ▶ size_t count = 1;
Branch: 1 branch
BranchTrueFalseTotal
size_t count = 1; 19 0 19
164 int d;
165 CODE:
166 19 e0 = hamt_entry_new(HAMT_CX, "a", 1, 0, 0, NULL);
167 19 e0b = hamt_entry_new(HAMT_CX, "a", 1, 0, 0, NULL);
168 19 e1 = hamt_entry_new(HAMT_CX, "b", 1, 0, 1, NULL);
169 19 e3 = hamt_entry_new(HAMT_CX, "c", 1, 0, 3, NULL);
170 19 e32 = hamt_entry_new(HAMT_CX, "d", 1, 0, 32, NULL);
171 19 ▶ if (what == 4 || what == 10) {
Branch: 2 branches
BranchTrueFalseTotal
if (what == 4 || what == 10) { 18 1 19
if (what == 4 || what == 10) { 1 17 18
172 /* a root over one child at position 0 */
173 2 child = hamt_node_new(HAMT_CX, 4);
174 2 root = hamt_node_new(HAMT_CX, 4);
175 2 root->nodemap = 1;
176 2 root->slot[0] = child;
177 2 ▶ if (what == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (what == 4) { 1 1 2
178 1 child->datamap = 1;
179 1 child->slot[0] = hamt_entry_retain(e0);
180 }
181 else {
182 1 child->datamap = 3;
183 1 child->slot[0] = hamt_entry_retain(e3);
184 1 child->slot[1] = hamt_entry_retain(e32);
185 1 count = 2;
186 }
187 2 RETVAL = hamt_map_check(root, count);
188 }
189 17 ▶ else if (what == 6 || what == 7 || what == 8 || what == 9 || what == 22 || what == 24 || what == 26) {
Branch: 7 branches
BranchTrueFalseTotal
else if (what == 6 || what == 7 || what == 8 || what == 9 || what == 22 || what == 24 || what == 26) { 16 1 17
else if (what == 6 || what == 7 || what == 8 || what == 9 || what == 22 || what == 24 || what == 26) { 15 1 16
else if (what == 6 || what == 7 || what == 8 || what == 9 || what == 22 || what == 24 || what == 26) { 14 1 15
else if (what == 6 || what == 7 || what == 8 || what == 9 || what == 22 || what == 24 || what == 26) { 13 1 14
else if (what == 6 || what == 7 || what == 8 || what == 9 || what == 22 || what == 24 || what == 26) { 12 1 13
else if (what == 6 || what == 7 || what == 8 || what == 9 || what == 22 || what == 24 || what == 26) { 11 1 12
else if (what == 6 || what == 7 || what == 8 || what == 9 || what == 22 || what == 24 || what == 26) { 1 10 11
190 /* a chain of nodes down to depth twelve, each over one child at position 0 */
191 7 bottom = hamt_node_new(HAMT_CX, 4);
192 7 ▶ if (what == 9) {
Branch: 1 branch
BranchTrueFalseTotal
if (what == 9) { 1 6 7
193 1 bottom->datamap = 1;
194 1 bottom->slot[0] = hamt_entry_retain(e0);
195 }
196 else {
197 6 ▶ bottom->ncoll = what == 7 ? 1 : 2;
Branch: 1 branch
BranchTrueFalseTotal
bottom->ncoll = what == 7 ? 1 : 2; 1 5 6
198 6 bottom->slot[0] = hamt_entry_retain(e0);
199 6 ▶ if (what != 7) bottom->slot[1] = hamt_entry_retain(what == 26 ? e3 : e0b);
Branch: 2 branches
BranchTrueFalseTotal
if (what != 7) bottom->slot[1] = hamt_entry_retain(what == 26 ? e3 : e0b); 5 1 6
if (what != 7) bottom->slot[1] = hamt_entry_retain(what == 26 ? e3 : e0b); 1 4 5
200 6 count = bottom->ncoll;
201 }
202 7 chain[12] = bottom;
203 91 ▶ for (d = 11; d >= 0; d--) {
Branch: 1 branch
BranchTrueFalseTotal
for (d = 11; d >= 0; d--) { 84 7 91
204 84 chain[d] = hamt_node_new(HAMT_CX, 4);
205 84 chain[d]->nodemap = 1;
206 84 chain[d]->slot[0] = chain[d + 1];
207 }
208 7 root = chain[0];
209 7 ▶ if (what == 6) bottom->datamap = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (what == 6) bottom->datamap = 1; 1 6 7
210 7 ▶ if (what == 22) e0b->refcnt = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (what == 22) e0b->refcnt = 0; 1 6 7
211 7 ▶ if (what == 24) bottom->slot[1] = NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (what == 24) bottom->slot[1] = NULL; 1 6 7
212 7 RETVAL = hamt_map_check(root, count);
213 7 ▶ if (what == 6) bottom->datamap = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (what == 6) bottom->datamap = 0; 1 6 7
214 7 ▶ if (what == 22) e0b->refcnt = 2;
Branch: 1 branch
BranchTrueFalseTotal
if (what == 22) e0b->refcnt = 2; 1 6 7
215 7 ▶ if (what == 24) bottom->slot[1] = e0b;
Branch: 1 branch
BranchTrueFalseTotal
if (what == 24) bottom->slot[1] = e0b; 1 6 7
216 }
217 else {
218 /* a root holding one entry at position 0 */
219 10 root = hamt_node_new(HAMT_CX, 4);
220 10 root->datamap = 1;
221 10 root->slot[0] = hamt_entry_retain(e0);
222 10 switch (what) {
223 1 case 1: root->refcnt = 0; RETVAL = hamt_map_check(root, 1); root->refcnt = 1; break;
224 1 case 2: root->nodemap = 1; RETVAL = hamt_map_check(root, 1); root->nodemap = 0; break;
225 1 case 3: root->datamap = 0; RETVAL = hamt_map_check(root, 0); root->datamap = 1; break;
226 1 case 5: root->ncoll = 2; RETVAL = hamt_map_check(root, 2); root->ncoll = 0; break;
227 1 case 12: RETVAL = hamt_map_check(root, 2); break;
228 1 case 13: root->slot[0] = e1; RETVAL = hamt_map_check(root, 1); root->slot[0] = e0; break;
229 1 case 21: e0->refcnt = 0; RETVAL = hamt_map_check(root, 1); e0->refcnt = 2; break;
230 1 case 23: root->slot[0] = NULL; RETVAL = hamt_map_check(root, 1); root->slot[0] = e0; break;
231 1 case 25:
232 1 root->datamap = 0; root->nodemap = 1; root->slot[0] = NULL;
233 1 RETVAL = hamt_map_check(root, 1);
234 1 root->nodemap = 0; root->datamap = 1; root->slot[0] = e0;
235 1 break;
236 1 default: RETVAL = hamt_map_check(root, 1); break;
237 }
238 }
239 19 hamt_node_release(HAMT_CX, root);
240 19 hamt_entry_release(HAMT_CX, e0);
241 19 hamt_entry_release(HAMT_CX, e0b);
242 19 hamt_entry_release(HAMT_CX, e1);
243 19 hamt_entry_release(HAMT_CX, e3);
244 19 hamt_entry_release(HAMT_CX, e32);
245 OUTPUT:
246 RETVAL
247  
248 # The same for a vector and hamt_vec_check, by a number of this file's own:
249 # 1 to 9 break the tail or the root, 10 and up the tree. 19 asks the checker
250 # to allow no spare children at all, and 20 gives a table that lies. 0 is
251 # the vector as built.
252 IV
253 _vec_broken(what)
254 IV what
255 PREINIT:
256 hamt_vec v;
257 hamt_vnode *leaf, *leaf2, *node, *kids[2], *keep;
258 void *vals[32];
259 static size_t dummy[32];
260 SV *one;
261 int i;
262 CODE:
263 21 one = sv_2mortal(newSViv(1));
264 693 ▶ for (i = 0; i < 32; i++) vals[i] = one;
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 32; i++) vals[i] = one; 672 21 693
265 21 hamt_vec_init(&v);
266 21 v.tail = hamt_vnode_leaf(HAMT_CX, vals, 1);
267 21 v.count = 1;
268 21 ▶ if (what < 10) {
Branch: 1 branch
BranchTrueFalseTotal
if (what < 10) { 10 11 21
269 10 switch (what) {
270 1 case 1: keep = v.tail; v.tail = NULL; RETVAL = hamt_vec_check(&v); v.tail = keep; break;
271 1 case 2: v.tail->leaf = 0; RETVAL = hamt_vec_check(&v); v.tail->leaf = 1; break;
272 1 case 3: v.tail->len = 0; RETVAL = hamt_vec_check(&v); v.tail->len = 1; break;
273 1 case 4: v.tail->len = 2; RETVAL = hamt_vec_check(&v); v.tail->len = 1; break;
274 1 case 5: v.tail->sizes = dummy; RETVAL = hamt_vec_check(&v); v.tail->sizes = NULL; break;
275 1 case 6: v.tail->refcnt = 0; RETVAL = hamt_vec_check(&v); v.tail->refcnt = 1; break;
276 1 case 7: v.tail->slot[0] = NULL; RETVAL = hamt_vec_check(&v); v.tail->slot[0] = one; break;
277 1 case 8: v.root = v.tail; RETVAL = hamt_vec_check(&v); v.root = NULL; break;
278 1 case 9: v.count = 2; RETVAL = hamt_vec_check(&v); v.count = 1; break;
279 1 default: RETVAL = hamt_vec_check(&v); break;
280 }
281 }
282 11 ▶ else if (what < 18) {
Branch: 1 branch
BranchTrueFalseTotal
else if (what < 18) { 8 3 11
283 /* a root over one full leaf */
284 8 leaf = hamt_vnode_leaf(HAMT_CX, vals, 32);
285 8 node = hamt_vnode_make(HAMT_CX, &leaf, 1, HAMT_VEC_BITS);
286 8 v.root = node;
287 8 v.count = 33;
288 8 switch (what) {
289 1 case 10: v.shift = 7; RETVAL = hamt_vec_check(&v); v.shift = HAMT_VEC_BITS; break;
290 1 case 11: v.shift = 10; RETVAL = hamt_vec_check(&v); v.shift = HAMT_VEC_BITS; break;
291 1 case 12: node->slot[0] = NULL; RETVAL = hamt_vec_check(&v); node->slot[0] = leaf; break;
292 1 case 13: node->refcnt = 0; RETVAL = hamt_vec_check(&v); node->refcnt = 1; break;
293 1 case 14: node->len = 0; RETVAL = hamt_vec_check(&v); node->len = 1; break;
294 1 case 15: node->leaf = 1; RETVAL = hamt_vec_check(&v); node->leaf = 0; break;
295 1 case 16: leaf->sizes = dummy; RETVAL = hamt_vec_check(&v); leaf->sizes = NULL; break;
296 1 default: leaf->slot[0] = NULL; RETVAL = hamt_vec_check(&v); leaf->slot[0] = one; break;
297 }
298 }
299 else {
300 /* a root over two leaves: full ones, or ones holding one value each */
301 3 ▶ leaf = hamt_vnode_leaf(HAMT_CX, vals, what == 18 ? 32 : 1);
Branch: 1 branch
BranchTrueFalseTotal
leaf = hamt_vnode_leaf(HAMT_CX, vals, what == 18 ? 32 : 1); 1 2 3
302 3 ▶ leaf2 = hamt_vnode_leaf(HAMT_CX, vals, what == 18 ? 32 : 1);
Branch: 1 branch
BranchTrueFalseTotal
leaf2 = hamt_vnode_leaf(HAMT_CX, vals, what == 18 ? 32 : 1); 1 2 3
303 3 kids[0] = leaf;
304 3 kids[1] = leaf2;
305 3 node = hamt_vnode_make(HAMT_CX, kids, 2, HAMT_VEC_BITS);
306 3 v.root = node;
307 3 v.count = (size_t)leaf->len + leaf2->len + 1;
308 3 ▶ if (what == 18) { leaf->len = 31; RETVAL = hamt_vec_check(&v); leaf->len = 32; }
Branch: 1 branch
BranchTrueFalseTotal
if (what == 18) { leaf->len = 31; RETVAL = hamt_vec_check(&v); leaf->len = 32; } 1 2 3
309 2 ▶ else if (what == 20) { node->sizes[0] = 5; RETVAL = hamt_vec_check(&v); node->sizes[0] = 1; }
Branch: 1 branch
BranchTrueFalseTotal
else if (what == 20) { node->sizes[0] = 5; RETVAL = hamt_vec_check(&v); node->sizes[0] = 1; } 1 1 2
310 1 else RETVAL = hamt_vec_check_spare(&v, 0);
311 }
312 21 ▶ if (v.root) hamt_vnode_release(HAMT_CX, v.root);
Branch: 1 branch
BranchTrueFalseTotal
if (v.root) hamt_vnode_release(HAMT_CX, v.root); 11 10 21
313 21 hamt_vnode_release(HAMT_CX, v.tail);
314 OUTPUT:
315 RETVAL
316  
317 # The core's persistent put, which the methods leave to the harness (they
318 # put in place on a copy): 1 for a key that was new, 0 for one replaced.
319 # The trie that comes back is let go.
320 IV
321 _put_persistent(self, key, value)
322 SV *self
323 SV *key
324 SV *value
325 PREINIT:
326 hamt_map *m;
327 hamt_key k;
328 hamt_entry *e;
329 hamt_node *r;
330 int added;
331 CODE:
332 15 m = hamt_handle(aTHX_ self, &hamt_map_vtbl, "HAMT::_put_persistent", "map");
333 15 hamt_key_from_sv(aTHX_ key, &k);
334 15 e = hamt_entry_new(HAMT_CX, k.p, (size_t)k.len, k.utf8, k.hash, (void *)value);
335 15 ▶ if (!e) croak("HAMT::_put_persistent: out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!e) croak("HAMT::_put_persistent: out of memory"); 3 12 15
336 12 r = hamt_map_put(HAMT_CX, m->root, e, &added);
337 12 hamt_entry_release(HAMT_CX, e);
338 12 ▶ if (!r) croak("HAMT::_put_persistent: out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!r) croak("HAMT::_put_persistent: out of memory"); 5 7 12
339 7 hamt_node_release(HAMT_CX, r);
340 7 ▶ RETVAL = added;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = added; 0 7 7
341 OUTPUT:
342 RETVAL
343  
344 # How many levels of nodes a vector has over its leaves.
345 IV
346 _vec_height(self)
347 SV *self
348 PREINIT:
349 hamt_vec *v;
350 CODE:
351 5 v = hamt_vec_handle(aTHX_ self, "HAMT::_vec_height");
352 5 ▶ RETVAL = (IV)hamt_vec_height(v);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (IV)hamt_vec_height(v); 0 5 5
353 OUTPUT:
354 RETVAL
« include/hamt/hamt_check.h ↑ Summary include/hamt/hamt_entry.h »