Host C Specs


Directory: src/c/
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 97.6% 41 / 0 / 42
Functions: 100.0% 4 / 0 / 4
Branches: 83.3% 30 / 0 / 36

core/health/hr_day.c
Line Branch Exec Source
1 /**
2 * @file hr_day.c
3 * @brief Today's heart rate by the hour. See hr_day.h.
4 */
5 #include "hr_day.h"
6
7 #include <string.h>
8
9 8 void hr_day_clear(HrDay *day)
10 {
11 8 memset(day, 0, sizeof(*day));
12 8 }
13
14 315 void hr_day_add(HrDay *day, int hour, int bpm)
15 {
16
6/6
✓ Branch 2 → 3 taken 314 times.
✓ Branch 2 → 5 taken 1 time.
✓ Branch 3 → 4 taken 313 times.
✓ Branch 3 → 5 taken 1 time.
✓ Branch 4 → 5 taken 1 time.
✓ Branch 4 → 6 taken 312 times.
315 if (hour < 0 || hour >= HR_DAY_HOURS || bpm <= 0)
17 {
18 3 return;
19 }
20
21 312 HrHour *slot = &day->hours[hour];
22
2/2
✓ Branch 6 → 7 taken 45 times.
✓ Branch 6 → 8 taken 267 times.
312 if (slot->count == UINT8_MAX)
23 {
24 45 return;
25 }
26
27
1/2
✓ Branch 8 → 9 taken 267 times.
✗ Branch 8 → 10 not taken.
267 uint8_t reading = (uint8_t)(bpm > UINT8_MAX ? UINT8_MAX : bpm);
28
3/4
✓ Branch 11 → 12 taken 259 times.
✓ Branch 11 → 13 taken 8 times.
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 259 times.
267 if (slot->count == 0 || reading < slot->low)
29 {
30 8 slot->low = reading;
31 }
32
4/4
✓ Branch 14 → 15 taken 259 times.
✓ Branch 14 → 16 taken 8 times.
✓ Branch 15 → 16 taken 3 times.
✓ Branch 15 → 17 taken 256 times.
267 if (slot->count == 0 || reading > slot->high)
33 {
34 11 slot->high = reading;
35 }
36
37 // 255 readings of 255 is 65025, so the sum always fits
38 267 slot->sum += reading;
39 267 slot->count++;
40 }
41
42 3 int hr_day_hour_avg(const HrDay *day, int hour)
43 {
44
2/4
✓ Branch 2 → 3 taken 3 times.
✗ Branch 2 → 4 not taken.
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 3 times.
3 if (hour < 0 || hour >= HR_DAY_HOURS)
45 {
46 ✗ return -1;
47 }
48
49 3 const HrHour *slot = &day->hours[hour];
50
2/2
✓ Branch 5 → 6 taken 1 time.
✓ Branch 5 → 7 taken 2 times.
3 if (slot->count == 0)
51 {
52 1 return -1;
53 }
54
55 2 return (slot->sum + slot->count / 2) / slot->count;
56 }
57
58 5 void hr_day_stats(const HrDay *day, int *low, int *avg, int *high)
59 {
60 5 *low = -1;
61 5 *avg = -1;
62 5 *high = -1;
63
64 5 uint32_t sum = 0;
65 5 uint32_t count = 0;
66
2/2
✓ Branch 12 → 3 taken 120 times.
✓ Branch 12 → 13 taken 5 times.
125 for (int hour = 0; hour < HR_DAY_HOURS; hour++)
67 {
68 120 const HrHour *slot = &day->hours[hour];
69
2/2
✓ Branch 3 → 4 taken 115 times.
✓ Branch 3 → 5 taken 5 times.
120 if (slot->count == 0)
70 {
71 115 continue;
72 }
73
74
3/4
✓ Branch 5 → 6 taken 2 times.
✓ Branch 5 → 7 taken 3 times.
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 2 times.
5 if (*low < 0 || slot->low < *low)
75 {
76 3 *low = slot->low;
77 }
78
1/2
✓ Branch 8 → 9 taken 5 times.
✗ Branch 8 → 10 not taken.
5 if (slot->high > *high)
79 {
80 5 *high = slot->high;
81 }
82 5 sum += slot->sum;
83 5 count += slot->count;
84 }
85
86
2/2
✓ Branch 13 → 14 taken 3 times.
✓ Branch 13 → 15 taken 2 times.
5 if (count > 0)
87 {
88 3 *avg = (int)((sum + count / 2) / count);
89 }
90 5 }
91