1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
#include "utest.h"
#include "models/timer.hpp"
uint64_t _timestamp_ms_value = 0;
uint64_t monotonic_timestamp_ms() {
return _timestamp_ms_value;
}
#define MILISEC 1
#define SEC (1000 * MILISEC)
#define MIN (60 * SEC)
#define HOUR (60 * MIN)
#define DAY (60 * HOUR)
UTEST(timer, stopwatch_mode) {
FreqTimer timer;
_timestamp_ms_value = SEC;
timer.toggleStart();
ASSERT_TRUE(timer.running());
_timestamp_ms_value += 2 * SEC;
ASSERT_EQ(2 * SEC, timer.time_ms());
ASSERT_NEAR(30.0, timer.record_floor(), 1e-7);
_timestamp_ms_value += SEC;
timer.toggleStart(); // stop
ASSERT_FALSE(timer.running());
_timestamp_ms_value += 10 * SEC;
timer.toggleStart(); // start
_timestamp_ms_value += SEC;
ASSERT_EQ(4 * SEC, timer.time_ms());
}
UTEST(timer, timer_mode) {
FreqTimer timer;
_timestamp_ms_value = SEC;
// set preset
ASSERT_FALSE(timer.has_preset());
timer.increment_preset_ms(1 * SEC);
timer.increment_preset_ms(9 * SEC);
ASSERT_NEAR(6.0, timer.record_floor(), 1e-7);
ASSERT_FALSE(timer.running());
ASSERT_TRUE(timer.has_preset());
// counts down
ASSERT_EQ(10 * SEC, timer.preset_ms());
timer.toggleStart();
_timestamp_ms_value += 5 * SEC;
ASSERT_EQ(5 * SEC, timer.time_ms());
_timestamp_ms_value += 6 * SEC;
ASSERT_EQ(-SEC, timer.time_ms());
// reset returns to preset
timer.reset();
ASSERT_FALSE(timer.running());
ASSERT_EQ(10 * SEC, timer.time_ms());
timer.increment_preset_ms(SEC);
ASSERT_EQ(SEC, timer.preset_ms());
// increment causes reset
timer.toggleStart();
ASSERT_TRUE(timer.running());
timer.increment_preset_ms(2 * SEC);
ASSERT_FALSE(timer.running());
ASSERT_EQ(2 * SEC, timer.preset_ms());
}
UTEST(timer, display_value) {
// The display value should count up in stopwatch mode
FreqTimer timer;
_timestamp_ms_value = 0;
timer.toggleStart();
_timestamp_ms_value = 10 * SEC + 100;
std::string display_val = timer.counter_display_value();
ASSERT_STREQ("00 : 00 : 10.1", display_val.c_str());
timer.reset();
timer.toggleStart();
_timestamp_ms_value += 95 * HOUR + 59 * MIN + 48 * SEC + 800 * MILISEC;
display_val = timer.counter_display_value();
ASSERT_STREQ("95 : 59 : 48.8", display_val.c_str());
timer.reset();
timer.toggleStart();
_timestamp_ms_value += 99 * HOUR + MILISEC;
display_val = timer.counter_display_value();
ASSERT_STREQ("MAX TIME EXCEEDED", display_val.c_str());
// The display value should countdown if the timer has a preset
timer.increment_preset_ms(10 * SEC);
display_val = timer.counter_display_value();
ASSERT_STREQ("00 : 00 : 10.0", display_val.c_str());
timer.toggleStart();
_timestamp_ms_value += 6 * SEC;
display_val = timer.counter_display_value();
ASSERT_STREQ("00 : 00 : 04.0", display_val.c_str());
_timestamp_ms_value += 5 * SEC;
display_val = timer.counter_display_value();
ASSERT_STREQ("+ 00 : 00 : 01.0", display_val.c_str());
}
UTEST_MAIN();
|