dte test coverage


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 50.0% high: ≥ 85.0%
Coverage Exec / Excl / Total
Lines: 91.3% 21 / 0 / 23
Functions: 100.0% 2 / 0 / 2
Branches: 91.7% 11 / 0 / 12

src/util/array.c
Line Branch Exec Source
1 #include "array.h"
2 #include "log.h"
3 #include "str-util.h"
4 #include "xmalloc.h"
5
6 // This can be used to collect all prefix-matched strings from a "flat" array
7 // (i.e. an array of fixed-length char arrays; *not* pointers to char)
8 20 void collect_strings_from_flat_array (
9 const char *base,
10 size_t nr_elements,
11 size_t element_len,
12 PointerArray *a,
13 StringView prefix
14 ) {
15 20 const char *end = base + (nr_elements * element_len);
16
2/2
✓ Branch 7 → 3 taken 562 times.
✓ Branch 7 → 8 taken 20 times.
582 for (const char *str = base; str < end; str += element_len) {
17
2/2
✓ Branch 3 → 4 taken 317 times.
✓ Branch 3 → 6 taken 245 times.
562 if (str_has_sv_prefix(str, prefix)) {
18 317 ptr_array_append(a, xstrdup(str));
19 }
20 }
21 20 }
22
23 // Return bitflags corresponding to a set of comma-delimited substrings
24 // found in an array. For example, if the string is "str3,str7" and
25 // those 2 substrings are found at array[3] and array[7] respectively,
26 // the returned value will be `1 << 3 | 1 << 7`.
27 9 unsigned int str_to_bitflags (
28 const char *str,
29 const char *base, // Pointer to start of char[nstrs][size] array
30 size_t nstrs,
31 size_t size,
32 bool tolerate_errors // Whether to ignore invalid substrings
33 ) {
34 // Copy `str` into a mutable buffer, so that get_delim_str() can be
35 // used to split (and null-terminate) the comma-delimited substrings
36 9 char buf[512];
37 9 const char *end = memccpy(buf, str, '\0', sizeof(buf));
38
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 6 taken 9 times.
9 if (unlikely(!end)) {
39 LOG_ERROR("flags string too long: %.*s...", 80, str);
40 return 0;
41 }
42
43 9 unsigned int flags = 0;
44
2/2
✓ Branch 16 → 7 taken 20 times.
✓ Branch 16 → 17 taken 8 times.
28 for (size_t pos = 0, len = end - buf - 1; pos < len; ) {
45 20 const char *substr = get_delim_str(buf, &pos, len, ',');
46 20 ssize_t idx = find_str_idx(substr, base, nstrs, size, streq);
47
2/2
✓ Branch 9 → 10 taken 10 times.
✓ Branch 9 → 14 taken 10 times.
20 if (unlikely(idx < 0)) {
48
2/2
✓ Branch 10 → 11 taken 1 time.
✓ Branch 10 → 12 taken 9 times.
10 if (!tolerate_errors) {
49 1 return 0;
50 }
51 9 LOG_WARNING("unrecognized flag string: '%s'", substr);
52 9 continue;
53 }
54 10 flags |= 1u << idx;
55 }
56
57 8 return flags;
58 }
59