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
« include/hamt/hamt_abi_impl.h ↑ Summary xs/vector.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 38811764 static uint64_t hamt_chk_prefix_mask(unsigned depth) {
28 38811764 unsigned bits = HAMT_BITS * depth;
29 38811764 ▶ 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 */ 38811764 0 38811764
30 }
31  
32 38811764 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 38811764 uint64_t pmask = hamt_chk_prefix_mask(depth);
36  
37 38811764 ▶ if (!n) return HAMT_CHK_NULL_SLOT;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return HAMT_CHK_NULL_SLOT; 1 38811763 38811764
38 38811763 ▶ if (n->refcnt == 0) return HAMT_CHK_REFCNT;
Branch: 1 branch
BranchTrueFalseTotal
if (n->refcnt == 0) return HAMT_CHK_REFCNT; 1 38811762 38811763
39  
40 38811762 ▶ if (n->ncoll) {
Branch: 1 branch
BranchTrueFalseTotal
if (n->ncoll) { 3123928 35687834 38811762
41 3123928 ▶ 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 3123927 3123928
42 3123927 ▶ 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 */ 3123926 1 3123927
if (n->datamap || n->nodemap) return HAMT_CHK_COLL_MAPS; /* # uncoverable condition left count:2 */ 0 3123926 3123926
43 3123926 ▶ if (n->ncoll < 2) return HAMT_CHK_COLL_SMALL;
Branch: 1 branch
BranchTrueFalseTotal
if (n->ncoll < 2) return HAMT_CHK_COLL_SMALL; 1 3123925 3123926
44 16282449 ▶ for (i = 0; i < n->ncoll; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n->ncoll; i++) { 13158528 3123921 16282449
45 13158528 const hamt_entry *e = (const hamt_entry *)n->slot[i];
46 uint32_t j;
47 13158528 ▶ if (!e) return HAMT_CHK_NULL_SLOT;
Branch: 1 branch
BranchTrueFalseTotal
if (!e) return HAMT_CHK_NULL_SLOT; 1 13158527 13158528
48 13158527 ▶ if (e->refcnt == 0) return HAMT_CHK_REFCNT;
Branch: 1 branch
BranchTrueFalseTotal
if (e->refcnt == 0) return HAMT_CHK_REFCNT; 1 13158526 13158527
49 13158526 ▶ 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 13158525 13158526
50 94255822 ▶ for (j = 0; j < i; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < i; j++) { 81097298 13158524 94255822
51 81097298 const hamt_entry *f = (const hamt_entry *)n->slot[j];
52 81097298 ▶ 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 81097297 81097298
53 1 return HAMT_CHK_COLL_DUP;
54 }
55 }
56 3123921 *count += n->ncoll;
57 3123921 return HAMT_CHK_OK;
58 }
59  
60 35687834 ▶ 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 35687833 35687834
61 35687833 ▶ 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 35687832 35687833
62  
63 35687832 nd = hamt_popcnt32(n->datamap);
64 35687832 N = nd + hamt_popcnt32(n->nodemap);
65 35687832 ▶ if (N == 0) return HAMT_CHK_EMPTY_NODE;
Branch: 1 branch
BranchTrueFalseTotal
if (N == 0) return HAMT_CHK_EMPTY_NODE; 1 35687831 35687832
66 35687831 ▶ 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; 35638155 49676 35687831
if (depth > 0 && hamt_node_is_single(n)) return HAMT_CHK_SINGLE_BELOW; 1 35638154 35638155
67  
68 /* Entries: the i-th slot must hold the entry for the i-th set bit of
69 * the datamap, lowest first. */
70 35687830 i = 0;
71 1177698262 ▶ for (pos = 0; pos < 32; pos++) {
Branch: 1 branch
BranchTrueFalseTotal
for (pos = 0; pos < 32; pos++) { 1142010436 35687826 1177698262
72 const hamt_entry *e;
73 1142010436 ▶ if (!(n->datamap & ((uint32_t)1 << pos))) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!(n->datamap & ((uint32_t)1 << pos))) continue; 1125652519 16357917 1142010436
74 16357917 e = (const hamt_entry *)n->slot[i++];
75 16357917 ▶ if (!e) return HAMT_CHK_NULL_SLOT;
Branch: 1 branch
BranchTrueFalseTotal
if (!e) return HAMT_CHK_NULL_SLOT; 1 16357916 16357917
76 16357916 ▶ if (e->refcnt == 0) return HAMT_CHK_REFCNT;
Branch: 1 branch
BranchTrueFalseTotal
if (e->refcnt == 0) return HAMT_CHK_REFCNT; 1 16357915 16357916
77 16357915 ▶ 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 16357914 16357915
78 16357914 ▶ 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 16357913 16357914
79 }
80 35687826 *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 35687826 i = N;
87 1177695474 ▶ for (pos = 0; pos < 32; pos++) {
Branch: 1 branch
BranchTrueFalseTotal
for (pos = 0; pos < 32; pos++) { 1142007735 35687739 1177695474
88 int rc;
89 1142007735 ▶ if (!(n->nodemap & ((uint32_t)1 << pos))) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!(n->nodemap & ((uint32_t)1 << pos))) continue; 1103245651 38762084 1142007735
90 38762084 rc = hamt_node_check((const hamt_node *)n->slot[--i], depth + 1,
91 38762084 prefix | ((uint64_t)pos << (HAMT_BITS * depth)),
92 count);
93 38762084 ▶ if (rc) return rc;
Branch: 1 branch
BranchTrueFalseTotal
if (rc) return rc; 87 38761997 38762084
94 }
95 35687739 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
« include/hamt/hamt_abi_impl.h ↑ Summary xs/vector.xs »