-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtime_utils_test.cpp
More file actions
144 lines (119 loc) · 4.22 KB
/
time_utils_test.cpp
File metadata and controls
144 lines (119 loc) · 4.22 KB
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
#include <time_shield/CpuTickTimer.hpp>
#include <time_shield/time_utils.hpp>
#include <cassert>
#include <cmath>
#include <chrono>
#include <cstdlib>
#include <thread>
/// \brief Basic checks for time utility helpers.
int main() {
using namespace time_shield;
int ns = ns_of_sec();
assert(ns >= 0 && ns < NS_PER_SEC);
int us = us_of_sec();
assert(us >= 0 && us < US_PER_SEC);
int ms = ms_of_sec();
assert(ms >= 0 && ms < MS_PER_SEC);
ts_ms_t t1 = ts_ms();
ts_ms_t t2 = timestamp_ms();
assert(t2 >= t1 && t2 - t1 < MS_PER_SEC);
ts_us_t u1 = ts_us();
ts_us_t u2 = timestamp_us();
assert(u2 >= u1 && u2 - u1 < US_PER_SEC);
const int64_t rt1 = now_realtime_us();
const int64_t rt2 = now_realtime_us();
assert(rt2 >= rt1);
const ts_t mono_sec_1 = monotonic_sec();
const ts_t mono_sec_2 = monotonic_sec();
assert(mono_sec_2 >= mono_sec_1);
const ts_ms_t mono_ms_1 = monotonic_ms();
const ts_ms_t mono_ms_2 = monotonic_ms();
assert(mono_ms_2 >= mono_ms_1);
const ts_us_t mono_us_1 = monotonic_us();
const ts_us_t mono_us_2 = monotonic_us();
assert(mono_us_2 >= mono_us_1);
const ts_ms_t mono_ms_from_us_1 = static_cast<ts_ms_t>(mono_us_1 / 1000);
const ts_ms_t mono_ms_from_us_2 = static_cast<ts_ms_t>(mono_us_2 / 1000);
assert(mono_ms_from_us_2 >= mono_ms_from_us_1);
assert(std::llabs(mono_ms_1 - mono_ms_from_us_1) <= 1);
assert(std::llabs(mono_ms_2 - mono_ms_from_us_2) <= 1);
assert(static_cast<ts_t>(mono_ms_1 / 1000) >= mono_sec_1);
assert(static_cast<ts_t>(mono_ms_2 / 1000) >= mono_sec_2);
assert(std::llabs(static_cast<long long>(mono_sec_1 - static_cast<ts_t>(mono_ms_1 / 1000))) <= 1);
assert(std::llabs(static_cast<long long>(mono_sec_2 - static_cast<ts_t>(mono_ms_2 / 1000))) <= 1);
std::this_thread::sleep_for(std::chrono::milliseconds(2));
const int64_t rt3 = now_realtime_us();
assert(rt3 >= rt2);
const ts_t mono_sec_3 = monotonic_sec();
const ts_ms_t mono_ms_3 = monotonic_ms();
const ts_us_t mono_us_3 = monotonic_us();
assert(mono_sec_3 >= mono_sec_2);
assert(mono_ms_3 >= mono_ms_2);
assert(mono_us_3 >= mono_us_2);
assert(mono_ms_3 - mono_ms_1 >= 1);
assert(mono_us_3 - mono_us_1 >= 1000);
CpuTickTimer timer{};
double first_sample = timer.record_sample();
assert(timer.sample_count() == 1);
assert(timer.total_ticks() >= 0.0);
assert(timer.last_sample_ticks() == first_sample);
assert(!std::isnan(timer.average_ticks()));
timer.stop();
double frozen_elapsed = timer.elapsed();
timer.stop();
assert(timer.elapsed() == frozen_elapsed);
double resumed_sample = timer.record_sample();
assert(resumed_sample == 0.0);
assert(timer.last_sample_ticks() == 0.0);
assert(timer.sample_count() == 1);
double second_sample = timer.record_sample();
assert(second_sample >= 0.0);
assert(timer.sample_count() == 2);
timer.reset_samples();
assert(timer.sample_count() == 0);
assert(std::isnan(timer.average_ticks()));
CpuTickTimer manual_timer{false};
assert(manual_timer.elapsed() == 0.0);
assert(std::isnan(manual_timer.average_ticks()));
assert(manual_timer.sample_count() == 0);
double no_sample = manual_timer.record_sample();
assert(no_sample == 0.0);
assert(manual_timer.sample_count() == 0);
double collected = manual_timer.record_sample();
assert(collected >= 0.0);
assert(manual_timer.sample_count() == 1);
manual_timer.stop();
double manual_frozen = manual_timer.elapsed();
assert(manual_timer.elapsed() == manual_frozen);
manual_timer.restart();
assert(manual_timer.sample_count() == 0);
(void)ns;
(void)us;
(void)ms;
(void)t1;
(void)t2;
(void)u1;
(void)u2;
(void)rt1;
(void)rt2;
(void)rt3;
(void)mono_sec_1;
(void)mono_sec_2;
(void)mono_sec_3;
(void)mono_ms_1;
(void)mono_ms_2;
(void)mono_ms_3;
(void)mono_us_1;
(void)mono_us_2;
(void)mono_us_3;
(void)mono_ms_from_us_1;
(void)mono_ms_from_us_2;
(void)first_sample;
(void)frozen_elapsed;
(void)resumed_sample;
(void)second_sample;
(void)no_sample;
(void)collected;
(void)manual_frozen;
return 0;
}