forked from gazebosim/gz-math
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStopwatch_TEST.cc
185 lines (152 loc) · 5.42 KB
/
Stopwatch_TEST.cc
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* Copyright (C) 2018 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <gtest/gtest.h>
#include <thread>
#include "gz/math/Stopwatch.hh"
using namespace gz;
/////////////////////////////////////////////////
// Helper function that runs a few tests
void runTimer(math::Stopwatch &_time)
{
// Windows uses a system_clock for std::this_thread::sleep_for. This can
// cause incorrect sleep durations. So, we add some room for error on
// windows.
std::chrono::duration<int, std::milli> handleSteadyClock =
std::chrono::milliseconds(0);
#ifdef _WIN32
handleSteadyClock += std::chrono::milliseconds(100);
#endif
// Start the timer
EXPECT_TRUE(_time.Start());
// The timer should be running.
EXPECT_TRUE(_time.Running());
// The start time should be greater than the stop time.
EXPECT_GT(_time.StartTime(), _time.StopTime());
// The elapsed stop time should still be zero.
EXPECT_EQ(math::clock::duration::zero(),
_time.ElapsedStopTime());
// Wait for some time...
std::this_thread::sleep_for(std::chrono::milliseconds(50));
// Now the elapsed time should be greater than or equal to the time slept.
EXPECT_GE(_time.ElapsedRunTime() + handleSteadyClock,
std::chrono::milliseconds(50));
// Stop the timer.
EXPECT_TRUE(_time.Stop());
// The timer should not be running.
EXPECT_FALSE(_time.Running());
// The stop time should be greater than the start time.
EXPECT_GT(_time.StopTime(), _time.StartTime());
// The elapsed time should still be greater than the time slept.
EXPECT_GE(_time.ElapsedRunTime() + handleSteadyClock,
std::chrono::milliseconds(50));
// Save the elapsed time.
auto elapsedTime = _time.ElapsedRunTime();
// The timer is now stopped, let's sleep some more.
std::this_thread::sleep_for(std::chrono::milliseconds(50));
// The elapsed stop time should be greater than or equal to the time
// slept.
EXPECT_GE(_time.ElapsedStopTime() + handleSteadyClock,
std::chrono::milliseconds(50));
// The elapsed time should be the same.
EXPECT_EQ(elapsedTime, _time.ElapsedRunTime());
// Start the timer again.
EXPECT_TRUE(_time.Start());
// Store the elapsed stop time.
auto elapsedStopTime = _time.ElapsedStopTime();
// The timer should be running.
EXPECT_TRUE(_time.Running());
// Sleep for some time.
std::this_thread::sleep_for(std::chrono::milliseconds(50));
// The elapsed stop time should remain the same
EXPECT_EQ(elapsedStopTime, _time.ElapsedStopTime());
// The elapsed time should be greater than the previous elapsed time.
EXPECT_GT(_time.ElapsedRunTime(), elapsedTime);
// The elapsed time should be greater than or equal to the the previous
// two sleep times.
EXPECT_GE(_time.ElapsedRunTime() + handleSteadyClock,
std::chrono::milliseconds(100));
}
/////////////////////////////////////////////////
TEST(Stopwatch, Constructor)
{
math::Stopwatch watch;
EXPECT_FALSE(watch.Running());
EXPECT_EQ(watch.StopTime(), watch.StartTime());
EXPECT_EQ(math::clock::duration::zero(), watch.ElapsedRunTime());
EXPECT_EQ(math::clock::duration::zero(), watch.ElapsedStopTime());
runTimer(watch);
math::Stopwatch watch2(watch);
EXPECT_EQ(watch, watch2);
math::Stopwatch watch3(std::move(watch2));
EXPECT_EQ(watch, watch3);
}
/////////////////////////////////////////////////
TEST(Stopwatch, EqualOperator)
{
math::Stopwatch watch;
math::Stopwatch watch2;
math::Stopwatch watch3;
EXPECT_EQ(watch, watch2);
EXPECT_EQ(watch, watch3);
runTimer(watch);
runTimer(watch2);
runTimer(watch3);
EXPECT_NE(watch, watch2);
EXPECT_NE(watch, watch3);
watch2 = watch;
EXPECT_EQ(watch, watch2);
watch3 = std::move(watch2);
EXPECT_EQ(watch, watch3);
}
/////////////////////////////////////////////////
TEST(Stopwatch, StartStopReset)
{
math::Stopwatch watch;
runTimer(watch);
watch.Reset();
EXPECT_FALSE(watch.Running());
EXPECT_EQ(watch.StopTime(), watch.StartTime());
EXPECT_EQ(math::clock::duration::zero(), watch.ElapsedRunTime());
EXPECT_EQ(math::clock::duration::zero(), watch.ElapsedStopTime());
runTimer(watch);
EXPECT_TRUE(watch.Running());
watch.Start(true);
EXPECT_TRUE(watch.Running());
EXPECT_LT(watch.StopTime(), watch.StartTime());
EXPECT_NE(math::clock::duration::zero(), watch.ElapsedRunTime());
EXPECT_EQ(math::clock::duration::zero(), watch.ElapsedStopTime());
}
/////////////////////////////////////////////////
TEST(Stopwatch, FailStartStop)
{
math::Stopwatch watch;
// Can't stop while not running
EXPECT_FALSE(watch.Stop());
EXPECT_FALSE(watch.Running());
// Can start while not running
EXPECT_TRUE(watch.Start());
EXPECT_TRUE(watch.Running());
// Can't start while running
EXPECT_FALSE(watch.Start());
EXPECT_TRUE(watch.Running());
// Can stop while running
EXPECT_TRUE(watch.Stop());
EXPECT_FALSE(watch.Running());
// Can start while not running
EXPECT_TRUE(watch.Start());
EXPECT_TRUE(watch.Running());
}