include/hamt/hamt_check.h

filter: Statementstmt 100% 50 / 50 Branchbran 95.3% 61 / 64 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 97.3% 111 / 114 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/vector.xs ↑ Summary xs/test.xs »
1 #ifndef HAMT_CHECK_H
2 #define HAMT_CHECK_H
3  
4 /* Walks a whole trie and says whether it is one this library could have
5 * built. For the tests: it visits every node, so it costs what the map
6 * holds, and nothing in the library calls it.
7 *
8 * 0 when every rule holds. Otherwise the number of the first rule that did
9 * not, from the list below, so a failure names what broke. */
10  
11 #define HAMT_CHK_OK 0
12 #define HAMT_CHK_REFCNT 1 /* a node or entry with a count of zero */
13 #define HAMT_CHK_MAPS_OVERLAP 2 /* a position in both maps */
14 #define HAMT_CHK_EMPTY_NODE 3 /* a node holding nothing */
15 #define HAMT_CHK_SINGLE_BELOW 4 /* one entry, no children, not the root */
16 #define HAMT_CHK_COLL_SHALLOW 5 /* a collision node above depth 12 */
17 #define HAMT_CHK_COLL_MAPS 6 /* a collision node with a map bit set */
18 #define HAMT_CHK_COLL_SMALL 7 /* a collision node with fewer than two */
19 #define HAMT_CHK_COLL_DUP 8 /* two equal keys in a collision node */
20 #define HAMT_CHK_TOO_DEEP 9 /* an indexing node at depth 12 */
21 #define HAMT_CHK_ENTRY_PATH 10 /* an entry not where its hash puts it */
22 #define HAMT_CHK_NULL_SLOT 11 /* a slot holding nothing */
23 #define HAMT_CHK_COUNT 12 /* the entries do not total the count */
24 #define HAMT_CHK_ORDER 13 /* entries or children out of order */
25  
26 /* The hash bits that every entry under a node at this depth must share. */
27 40138882 static uint64_t hamt_chk_prefix_mask(unsigned depth) {
28 40138882 unsigned bits = HAMT_BITS * depth;
29 40138882 ▶ return bits >= 64 ? ~(uint64_t)0 : (((uint64_t)1 << bits) - 1); /* # uncoverable condition right count:1 */
Branch: 1 branch
BranchTrueFalseTotal
return bits >= 64 ? ~(uint64_t)0 : (((uint64_t)1 << bits) - 1); /* # uncoverable condition right count:1 */ 40138882 0 40138882
30 }
31  
32 40138882 static int hamt_node_check(const hamt_node *n, unsigned depth, uint64_t prefix,
33 size_t *count) {
34 uint32_t nd, N, i, pos;
35 40138882 uint64_t pmask = hamt_chk_prefix_mask(depth);
36  
37 40138882 ▶ if (!n) return HAMT_CHK_NULL_SLOT;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return HAMT_CHK_NULL_SLOT; 1 40138881 40138882
38 40138881 ▶ if (n->refcnt == 0) return HAMT_CHK_REFCNT;
Branch: 1 branch
BranchTrueFalseTotal
if (n->refcnt == 0) return HAMT_CHK_REFCNT; 1 40138880 40138881
39  
40 40138880 ▶ if (n->ncoll) {
Branch: 1 branch
BranchTrueFalseTotal
if (n->ncoll) { 3236534 36902346 40138880
41 3236534 ▶ if (depth < HAMT_MAX_DEPTH) return HAMT_CHK_COLL_SHALLOW;
Branch: 1 branch
BranchTrueFalseTotal
if (depth < HAMT_MAX_DEPTH) return HAMT_CHK_COLL_SHALLOW; 1 3236533 3236534
42 3236533 ▶ if (n->datamap || n->nodemap) return HAMT_CHK_COLL_MAPS; /* # uncoverable condition left count:2 */
Branch: 2 branches
BranchTrueFalseTotal
if (n->datamap || n->nodemap) return HAMT_CHK_COLL_MAPS; /* # uncoverable condition left count:2 */ 3236532 1 3236533
if (n->datamap || n->nodemap) return HAMT_CHK_COLL_MAPS; /* # uncoverable condition left count:2 */ 0 3236532 3236532
43 3236532 ▶ if (n->ncoll < 2) return HAMT_CHK_COLL_SMALL;
Branch: 1 branch
BranchTrueFalseTotal
if (n->ncoll < 2) return HAMT_CHK_COLL_SMALL; 1 3236531 3236532
44 16520704 ▶ for (i = 0; i < n->ncoll; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n->ncoll; i++) { 13284177 3236527 16520704
45 13284177 const hamt_entry *e = (const hamt_entry *)n->slot[i];
46 uint32_t j;
47 13284177 ▶ if (!e) return HAMT_CHK_NULL_SLOT;
Branch: 1 branch
BranchTrueFalseTotal
if (!e) return HAMT_CHK_NULL_SLOT; 1 13284176 13284177
48 13284176 ▶ if (e->refcnt == 0) return HAMT_CHK_REFCNT;
Branch: 1 branch
BranchTrueFalseTotal
if (e->refcnt == 0) return HAMT_CHK_REFCNT; 1 13284175 13284176
49 13284175 ▶ if ((e->hash & pmask) != prefix) return HAMT_CHK_ENTRY_PATH;
Branch: 1 branch
BranchTrueFalseTotal
if ((e->hash & pmask) != prefix) return HAMT_CHK_ENTRY_PATH; 1 13284174 13284175
50 93726841 ▶ for (j = 0; j < i; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < i; j++) { 80442668 13284173 93726841
51 80442668 const hamt_entry *f = (const hamt_entry *)n->slot[j];
52 80442668 ▶ if (hamt_entry_key_eq(f, e->key, e->klen, e->utf8, e->hash))
Branch: 1 branch
BranchTrueFalseTotal
if (hamt_entry_key_eq(f, e->key, e->klen, e->utf8, e->hash)) 1 80442667 80442668
53 1 return HAMT_CHK_COLL_DUP;
54 }
55 }
56 3236527 *count += n->ncoll;
57 3236527 return HAMT_CHK_OK;
58 }
59  
60 36902346 ▶ if (depth >= HAMT_MAX_DEPTH) return HAMT_CHK_TOO_DEEP;
Branch: 1 branch
BranchTrueFalseTotal
if (depth >= HAMT_MAX_DEPTH) return HAMT_CHK_TOO_DEEP; 1 36902345 36902346
61 36902345 ▶ if (n->datamap & n->nodemap) return HAMT_CHK_MAPS_OVERLAP;
Branch: 1 branch
BranchTrueFalseTotal
if (n->datamap & n->nodemap) return HAMT_CHK_MAPS_OVERLAP; 1 36902344 36902345
62  
63 36902344 nd = hamt_popcnt32(n->datamap);
64 36902344 N = nd + hamt_popcnt32(n->nodemap);
65 36902344 ▶ if (N == 0) return HAMT_CHK_EMPTY_NODE;
Branch: 1 branch
BranchTrueFalseTotal
if (N == 0) return HAMT_CHK_EMPTY_NODE; 1 36902343 36902344
66 36902343 ▶ if (depth > 0 && hamt_node_is_single(n)) return HAMT_CHK_SINGLE_BELOW;
Branch: 2 branches
BranchTrueFalseTotal
if (depth > 0 && hamt_node_is_single(n)) return HAMT_CHK_SINGLE_BELOW; 36852667 49676 36902343
if (depth > 0 && hamt_node_is_single(n)) return HAMT_CHK_SINGLE_BELOW; 1 36852666 36852667
67  
68 /* Entries: the i-th slot must hold the entry for the i-th set bit of
69 * the datamap, lowest first. */
70 36902342 i = 0;
71 1217777158 ▶ for (pos = 0; pos < 32; pos++) {
Branch: 1 branch
BranchTrueFalseTotal
for (pos = 0; pos < 32; pos++) { 1180874820 36902338 1217777158
72 const hamt_entry *e;
73 1180874820 ▶ if (!(n->datamap & ((uint32_t)1 << pos))) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!(n->datamap & ((uint32_t)1 << pos))) continue; 1164642552 16232268 1180874820
74 16232268 e = (const hamt_entry *)n->slot[i++];
75 16232268 ▶ if (!e) return HAMT_CHK_NULL_SLOT;
Branch: 1 branch
BranchTrueFalseTotal
if (!e) return HAMT_CHK_NULL_SLOT; 1 16232267 16232268
76 16232267 ▶ if (e->refcnt == 0) return HAMT_CHK_REFCNT;
Branch: 1 branch
BranchTrueFalseTotal
if (e->refcnt == 0) return HAMT_CHK_REFCNT; 1 16232266 16232267
77 16232266 ▶ if ((e->hash & pmask) != prefix) return HAMT_CHK_ENTRY_PATH;
Branch: 1 branch
BranchTrueFalseTotal
if ((e->hash & pmask) != prefix) return HAMT_CHK_ENTRY_PATH; 1 16232265 16232266
78 16232265 ▶ if (HAMT_IDX(e->hash, depth) != pos) return HAMT_CHK_ORDER;
Branch: 1 branch
BranchTrueFalseTotal
if (HAMT_IDX(e->hash, depth) != pos) return HAMT_CHK_ORDER; 1 16232264 16232265
79 }
80 36902338 *count += nd;
81  
82 /* Children: the last slot holds the child for the lowest set bit of the
83 * nodemap, and they run backwards from there. Each child is checked
84 * against the prefix its position gives it, so a child in the wrong
85 * slot fails on its own entries. */
86 36902338 i = N;
87 1217774370 ▶ for (pos = 0; pos < 32; pos++) {
Branch: 1 branch
BranchTrueFalseTotal
for (pos = 0; pos < 32; pos++) { 1180872119 36902251 1217774370
88 int rc;
89 1180872119 ▶ if (!(n->nodemap & ((uint32_t)1 << pos))) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!(n->nodemap & ((uint32_t)1 << pos))) continue; 1140782917 40089202 1180872119
90 40089202 rc = hamt_node_check((const hamt_node *)n->slot[--i], depth + 1,
91 40089202 prefix | ((uint64_t)pos << (HAMT_BITS * depth)),
92 count);
93 40089202 ▶ if (rc) return rc;
Branch: 1 branch
BranchTrueFalseTotal
if (rc) return rc; 87 40089115 40089202
94 }
95 36902251 return HAMT_CHK_OK;
96 }
97  
98 50619 static int hamt_map_check(const hamt_node *root, size_t count) {
99 50619 size_t seen = 0;
100 int rc;
101 50619 ▶ if (!root) return count == 0 ? HAMT_CHK_OK : HAMT_CHK_COUNT; /* # uncoverable condition right count:2 */
Branch: 2 branches
BranchTrueFalseTotal
if (!root) return count == 0 ? HAMT_CHK_OK : HAMT_CHK_COUNT; /* # uncoverable condition right count:2 */ 939 49680 50619
if (!root) return count == 0 ? HAMT_CHK_OK : HAMT_CHK_COUNT; /* # uncoverable condition right count:2 */ 939 0 939
102 49680 rc = hamt_node_check(root, 0, 0, &seen);
103 49680 ▶ if (rc) return rc;
Branch: 1 branch
BranchTrueFalseTotal
if (rc) return rc; 17 49663 49680
104 49663 ▶ return seen == count ? HAMT_CHK_OK : HAMT_CHK_COUNT;
Branch: 1 branch
BranchTrueFalseTotal
return seen == count ? HAMT_CHK_OK : HAMT_CHK_COUNT; 49662 1 49663
105 }
106  
107 #endif
« xs/vector.xs ↑ Summary xs/test.xs »