dte test coverage


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 50.0% high: ≥ 85.0%
Coverage Exec / Excl / Total
Lines: 100.0% 48 / 0 / 48
Functions: 100.0% 10 / 0 / 10
Branches: 100.0% 14 / 2 / 16

src/syntax/bitset.h
Line Branch Exec Source
1 #ifndef SYNTAX_BITSET_H
2 #define SYNTAX_BITSET_H
3
4 #include <limits.h>
5 #include <stdbool.h>
6 #include <stddef.h>
7 #include "util/debug.h"
8 #include "util/macros.h"
9
10 #define BITSET_WORD_BITS BITSIZE(BitSetWord)
11 #define BITSET_BIT_MASK (BITSET_WORD_BITS - 1)
12 #define BITSET_NR_WORDS(bits) (((bits) + BITSET_WORD_BITS - 1) / BITSET_WORD_BITS)
13 #define BITSET_INVERT(set) bitset_invert(set, ARRAYLEN(set))
14
15 typedef unsigned long BitSetWord;
16
17 3750 static inline size_t bitset_word_idx(unsigned char ch)
18 {
19 3750 return ch / BITSET_WORD_BITS;
20 }
21
22 3750 static inline unsigned int bitset_bit_idx(unsigned char ch)
23 {
24 3750 return ch & BITSET_BIT_MASK;
25 }
26
27 2823 static inline BitSetWord bitset_bit(unsigned char ch)
28 {
29 2823 return ((BitSetWord)1) << bitset_bit_idx(ch);
30 }
31
32 642 static inline bool bitset_contains(const BitSetWord *set, unsigned char ch)
33 {
34 642 size_t idx = bitset_word_idx(ch);
35 642 return set[idx] & bitset_bit(ch);
36 }
37
38 1254 static inline void bitset_add(BitSetWord *set, unsigned char ch)
39 {
40 1254 size_t idx = bitset_word_idx(ch);
41 1254 set[idx] |= bitset_bit(ch);
42 1254 }
43
44 933 static inline BitSetWord bitset_word_max(void)
45 {
46 933 BitSetWord m = 0;
47 933 return ~m;
48 }
49
50 // Return a word in which the bit for `ch` and all higher bits are set
51 927 static inline BitSetWord bitset_start_mask(unsigned char ch)
52 {
53 927 return bitset_word_max() << bitset_bit_idx(ch);
54 }
55
56 // Return a word in which the bit for `ch` and all lower bits are set
57 927 static inline BitSetWord bitset_end_mask(unsigned char ch)
58 {
59 927 BitSetWord bit = bitset_bit(ch);
60 927 BUG_ON(bit == 0);
61 927 return bit | (bit - 1);
62 }
63
64 813 static inline void bitset_add_char_range(BitSetWord *set, const char *r)
65 {
66
2/2
✓ Branch 16 → 3 taken 2182 times.
✓ Branch 16 → 17 taken 813 times.
2995 for (size_t i = 0; r[i]; i++) {
67 2182 unsigned int first_char = (unsigned char)r[i];
68
4/4
✓ Branch 3 → 4 taken 977 times.
✓ Branch 3 → 5 taken 1205 times.
✓ Branch 4 → 5 taken 49 times.
✓ Branch 4 → 7 taken 928 times.
2182 if (r[i + 1] != '-' || r[i + 2] == '\0') {
69 // Not a range; set bit for a single character
70 1254 bitset_add(set, first_char);
71 1254 continue;
72 }
73
74 928 i += 2;
75 928 unsigned int last_char = (unsigned char)r[i];
76
2/2
✓ Branch 7 → 8 taken 1 time.
✓ Branch 7 → 9 taken 927 times.
928 if (unlikely(first_char > last_char)) {
77 1 continue;
78 }
79
80 927 size_t first_word = bitset_word_idx(first_char);
81 927 size_t last_word = bitset_word_idx(last_char);
82 927 BitSetWord start_mask = bitset_start_mask(first_char);
83 927 BitSetWord end_mask = bitset_end_mask(last_char);
84
85
2/2
✓ Branch 10 → 11 taken 908 times.
✓ Branch 10 → 12 taken 19 times.
927 if (first_word == last_word) {
86 // Only 1 word affected; use the intersection of both masks
87 908 set[first_word] |= start_mask & end_mask;
88 908 continue;
89 }
90
91 // Set partial ranges in first/last word, then saturate any others
92 19 set[first_word] |= start_mask;
93 19 set[last_word] |= end_mask;
94
2/2
✓ Branch 14 → 13 taken 2 times.
✓ Branch 14 → 15 taken 19 times.
21 for (size_t w = first_word + 1; w < last_word; w++) {
95 2 set[w] = bitset_word_max();
96 }
97 }
98 813 }
99
100 46 static inline void bitset_invert(BitSetWord *set, size_t nr_words)
101 {
102
2/2
✓ Branch 4 → 3 taken 184 times.
✓ Branch 4 → 5 taken 46 times.
230 for (size_t i = 0; i < nr_words; i++) {
103 184 set[i] = ~set[i];
104 }
105 46 }
106
107 #endif
108