GCC Code Coverage Report


Directory: ./
File: tests/test_real_time_tools.cpp
Date: 2022-06-29 13:58:11
Exec Total Coverage
Lines: 140 153 91.5%
Branches: 203 578 35.1%

Line Branch Exec Source
1 /**
2 * @file test_real_time_tools.cpp
3 * @author Maximilien Naveau (maximilien.naveau@gmail.com)
4 * license License BSD-3-Clause
5 * @copyright Copyright (c) 2019, New York University and Max Planck
6 * Gesellschaft.
7 * @date 2019-05-22
8 *
9 * @brief This file implements a suit of unit tests for the real time tools.
10 * @see
11 * https://git-amd.tuebingen.mpg.de/amd-clmc/ci_example/wikis/catkin:-how-to-implement-unit-tests
12 */
13
14 #include <gtest/gtest.h>
15 #include <fstream>
16 #include <memory>
17 #include "real_time_tools/frequency_manager.hpp"
18 #include "real_time_tools/iostream.hpp"
19 #include "real_time_tools/realtime_check.hpp"
20 #include "real_time_tools/spinner.hpp"
21 #include "real_time_tools/thread.hpp"
22 #include "real_time_tools/timer.hpp"
23
24 // We use this in the unnittest for code simplicity
25 using namespace real_time_tools;
26
27 /**
28 * @brief The DISABLED_TestRealTimeTools class is used to disable test.
29 */
30 class DISABLED_TestRealTimeTools : public ::testing::Test
31 {
32 };
33
34 /**
35 * @brief The TestRealTimeTools class: test suit template for setting up
36 * the unit tests for the Device.
37 */
38 13 class TestRealTimeTools : public ::testing::Test
39 {
40 public:
41 13 TestRealTimeTools() : ::testing::Test()
42 {
43 13 }
44
45 protected:
46 /**
47 * @brief SetUp, is executed before the unit tests
48 */
49 13 void SetUp()
50 {
51 13 }
52
53 /**
54 * @brief TearDown, is executed after teh unit tests
55 */
56 13 void TearDown()
57 {
58 13 }
59 };
60
61 /**
62 * @brief test_timer_constructor tests the constructor of the class
63 */
64
3/6
✓ Branch 8 taken 14 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 14 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 14 times.
✗ Branch 15 not taken.
18 TEST_F(TestRealTimeTools, test_timer_constructor)
65 {
66
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 Timer my_timer;
67
3/12
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✓ Branch 20 taken 1 times.
✗ Branch 21 not taken.
1 ASSERT_EQ(my_timer.get_avg_elapsed_sec(), 0.0);
68
3/12
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✓ Branch 20 taken 1 times.
✗ Branch 21 not taken.
1 ASSERT_EQ(my_timer.get_std_dev_elapsed_sec(), 0.0);
69
3/12
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✓ Branch 21 taken 1 times.
✗ Branch 22 not taken.
1 ASSERT_EQ(my_timer.get_min_elapsed_sec(),
70 1 std::numeric_limits<double>::infinity());
71
3/12
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✓ Branch 21 taken 1 times.
✗ Branch 22 not taken.
1 ASSERT_EQ(my_timer.get_max_elapsed_sec(),
72
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 -std::numeric_limits<double>::infinity());
73 }
74
75
3/6
✓ Branch 8 taken 14 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 14 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 14 times.
✗ Branch 15 not taken.
18 TEST_F(TestRealTimeTools, test_timer_setters)
76 {
77
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 Timer my_timer;
78
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 my_timer.set_memory_size(1);
79 // I do not know what to test here should I access the size of the buffer?
80 1 }
81
82 #ifndef MAC_OS
83
3/6
✓ Branch 8 taken 14 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 14 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 14 times.
✗ Branch 15 not taken.
18 TEST_F(TestRealTimeTools, test_timer_timespec_add_sec)
84 {
85 struct timespec t;
86 1 t.tv_sec = 1;
87 1 t.tv_nsec = 1005601;
88
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 Timer::timespec_add_sec(t, 0.0);
89
3/12
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✓ Branch 19 taken 1 times.
✗ Branch 20 not taken.
1 ASSERT_EQ(t.tv_sec, 1);
90
3/12
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✓ Branch 19 taken 1 times.
✗ Branch 20 not taken.
1 ASSERT_EQ(t.tv_nsec, 1005601);
91 }
92 #endif // MAC_OS
93
94
3/6
✓ Branch 8 taken 14 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 14 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 14 times.
✗ Branch 15 not taken.
18 TEST_F(TestRealTimeTools, test_timer_tic_and_tac_and_sleep)
95 {
96
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 Timer my_timer;
97
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 my_timer.tic();
98
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 my_timer.sleep_sec(1.0);
99
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 double time_slept = my_timer.tac();
100 // This works we got basycally 500 micro sec of error in the sleeping time
101 // in non real time.
102
4/14
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✓ Branch 19 taken 1 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 1 times.
✗ Branch 23 not taken.
1 ASSERT_NEAR(time_slept, 1.0, 0.0005);
103 }
104
105 struct TmpData
106 {
107 Timer timer_;
108 double duration_;
109 };
110
111 void* rt_thread_for_test(void* my_timer_pointer)
112 {
113 TmpData* tmp_data = static_cast<TmpData*>(my_timer_pointer);
114 tmp_data->timer_.tic();
115 tmp_data->timer_.sleep_sec(1.0);
116 tmp_data->duration_ = tmp_data->timer_.tac();
117
118 return nullptr;
119 }
120
121 #ifdef NON_REAL_TIME
122
3/6
✓ Branch 8 taken 14 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 14 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 14 times.
✗ Branch 15 not taken.
14 TEST_F(DISABLED_TestRealTimeTools,
123 test_timer_tic_and_tac_and_sleep_in_real_time)
124 #else // NON_REAL_TIME
125 TEST_F(TestRealTimeTools, test_timer_tic_and_tac_and_sleep_in_real_time)
126 #endif // NON_REAL_TIME
127 {
128 TmpData tmp_data;
129 RealTimeThread thread;
130 thread.create_realtime_thread(&rt_thread_for_test, &tmp_data);
131 thread.join();
132 // This works we got basycally 100 micro sec of error in the sleeping time
133 // in real time. It is 5 times lower than the non real time test.
134 ASSERT_NEAR(tmp_data.duration_, 1.0, 0.0001);
135 }
136
137 1 void* set_bool_to_true(void* data)
138 {
139 1 bool* converted_data = static_cast<bool*>(data);
140 1 *converted_data = true;
141 1 return nullptr;
142 }
143
144
3/6
✓ Branch 8 taken 14 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 14 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 14 times.
✗ Branch 15 not taken.
18 TEST_F(TestRealTimeTools, test_thread_execution)
145 {
146 1 bool data = false;
147
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 RealTimeThread thread;
148
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 thread.create_realtime_thread(set_bool_to_true, &data);
149
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 thread.join();
150 // This works we got basycally 100 micro sec of error in the sleeping time
151 // in real time. It is 5 times lower than the non real time test.
152
3/14
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✓ Branch 21 taken 1 times.
✗ Branch 22 not taken.
✓ Branch 24 taken 1 times.
✗ Branch 25 not taken.
1 ASSERT_TRUE(data);
153 }
154
155
3/6
✓ Branch 8 taken 14 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 14 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 14 times.
✗ Branch 15 not taken.
18 TEST_F(TestRealTimeTools, test_timer_dump)
156 {
157
2/2
✓ Branch 0 taken 1000 times.
✓ Branch 1 taken 1 times.
1001 for (unsigned i = 0; i < 1000; ++i)
158 {
159
1/2
✓ Branch 1 taken 1000 times.
✗ Branch 2 not taken.
2000 Timer my_timer;
160
1/2
✓ Branch 1 taken 1000 times.
✗ Branch 2 not taken.
1000 my_timer.set_memory_size(1);
161
1/2
✓ Branch 1 taken 1000 times.
✗ Branch 2 not taken.
1000 my_timer.tic();
162 1000 double desired_sleeping_time = 0.0001;
163
1/2
✓ Branch 1 taken 1000 times.
✗ Branch 2 not taken.
1000 my_timer.sleep_sec(desired_sleeping_time);
164
1/2
✓ Branch 1 taken 1000 times.
✗ Branch 2 not taken.
1000 double time_slept = my_timer.tac();
165
2/4
✓ Branch 2 taken 1000 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1000 times.
✗ Branch 6 not taken.
1000 my_timer.dump_measurements("/tmp/test_timer_dump.dat");
166
2/4
✓ Branch 1 taken 1000 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1000 times.
✗ Branch 5 not taken.
2000 std::ifstream is("/tmp/test_timer_dump.dat");
167 1000 double data = -1.0;
168 1000 double duration = -1.0;
169 1000 int index = -1;
170 1000 int count = 0;
171
4/6
✓ Branch 1 taken 3000 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 3000 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2000 times.
✓ Branch 7 taken 1000 times.
5000 while (is >> data) // Attempt read into x, return false if it fails
172 {
173
2/2
✓ Branch 0 taken 1000 times.
✓ Branch 1 taken 1000 times.
2000 if (count % 2 == 0)
174 {
175 1000 index = data;
176 }
177 else
178 {
179 1000 duration = data;
180 }
181 2000 ++count;
182 }
183
3/12
✓ Branch 1 taken 1000 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1000 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✓ Branch 19 taken 1000 times.
✗ Branch 20 not taken.
1000 ASSERT_EQ(index, 0);
184
3/12
✓ Branch 1 taken 1000 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1000 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✓ Branch 19 taken 1000 times.
✗ Branch 20 not taken.
1000 ASSERT_EQ(count, 2);
185
4/14
✓ Branch 1 taken 1000 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1000 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✓ Branch 19 taken 1000 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 1000 times.
✗ Branch 23 not taken.
1000 ASSERT_NEAR(duration, time_slept, 1e-8);
186 }
187 }
188
189
3/6
✓ Branch 8 taken 14 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 14 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 14 times.
✗ Branch 15 not taken.
18 TEST_F(TestRealTimeTools, test_time_statistics)
190 {
191
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 Timer my_timer;
192
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 my_timer.set_memory_size(2);
193
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 my_timer.tic();
194
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 my_timer.sleep_sec(0.0001);
195
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 double time_slept = my_timer.tac();
196
3/12
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✓ Branch 20 taken 1 times.
✗ Branch 21 not taken.
1 ASSERT_EQ(my_timer.get_avg_elapsed_sec(), time_slept);
197
3/12
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✓ Branch 20 taken 1 times.
✗ Branch 21 not taken.
1 ASSERT_EQ(my_timer.get_std_dev_elapsed_sec(), 0.0);
198
3/12
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✓ Branch 20 taken 1 times.
✗ Branch 21 not taken.
1 ASSERT_EQ(my_timer.get_min_elapsed_sec(), time_slept);
199
3/12
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✓ Branch 20 taken 1 times.
✗ Branch 21 not taken.
1 ASSERT_EQ(my_timer.get_max_elapsed_sec(), time_slept);
200
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 my_timer.tic();
201
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 my_timer.sleep_sec(0.01);
202
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 double time_slept2 = my_timer.tac();
203 1 double mean = 0.5 * (time_slept + time_slept2);
204 2 double std_dev = std::sqrt(0.5 * (std::pow(time_slept - mean, 2) +
205 2 std::pow(time_slept2 - mean, 2)));
206
3/12
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✓ Branch 20 taken 1 times.
✗ Branch 21 not taken.
1 ASSERT_EQ(my_timer.get_avg_elapsed_sec(), mean);
207
3/12
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✓ Branch 20 taken 1 times.
✗ Branch 21 not taken.
1 ASSERT_EQ(my_timer.get_std_dev_elapsed_sec(), std_dev);
208
3/12
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✓ Branch 20 taken 1 times.
✗ Branch 21 not taken.
1 ASSERT_EQ(my_timer.get_min_elapsed_sec(), time_slept);
209
4/14
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✓ Branch 20 taken 1 times.
✗ Branch 21 not taken.
✓ Branch 23 taken 1 times.
✗ Branch 24 not taken.
1 ASSERT_EQ(my_timer.get_max_elapsed_sec(), time_slept2);
210 }
211
212
3/6
✓ Branch 8 taken 14 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 14 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 14 times.
✗ Branch 15 not taken.
18 TEST_F(TestRealTimeTools, test_iostream_create_directory)
213 {
214
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
2 std::string folder = "/tmp/.real_time_tools_test";
215
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 real_time_tools::create_directory(folder);
216
5/18
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 1 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✓ Branch 28 taken 1 times.
✗ Branch 29 not taken.
✓ Branch 31 taken 1 times.
✗ Branch 32 not taken.
1 ASSERT_TRUE(boost::filesystem::exists(folder));
217 }
218
219
3/6
✓ Branch 8 taken 14 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 14 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 14 times.
✗ Branch 15 not taken.
18 TEST_F(TestRealTimeTools, test_iostream_get_home_dir)
220 {
221
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 std::string home_dir = real_time_tools::get_home_dir();
222
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 std::cout << home_dir << std::endl;
223
4/16
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✓ Branch 24 taken 1 times.
✗ Branch 25 not taken.
✓ Branch 27 taken 1 times.
✗ Branch 28 not taken.
1 ASSERT_TRUE(home_dir != "");
224 }
225
226
3/6
✓ Branch 8 taken 14 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 14 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 14 times.
✗ Branch 15 not taken.
18 TEST_F(TestRealTimeTools, test_iostream_get_current_date_str)
227 {
228 // visual check performed, it seems correct on rt_preempt
229
2/4
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
1 std::cout << real_time_tools::Timer::get_current_date_str() << std::endl;
230 1 }
231
232
3/6
✓ Branch 8 taken 14 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 14 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 14 times.
✗ Branch 15 not taken.
18 TEST_F(TestRealTimeTools, test_spinner_normal_behavior)
233 {
234 // some parameters
235 1 double period_sec = 0.2;
236 1 int iteration = 5;
237
238 // we create the spinner
239
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 real_time_tools::Spinner spinner;
240 1 spinner.set_period(period_sec);
241
242 // we mesaure the time of the loop. This should be roughly the same as:
243 // iteration * period_sec in seconds.
244
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 real_time_tools::Timer timer;
245
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 timer.tic();
246
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 times.
7 for (int i = 0; i <= iteration; ++i)
247 {
248
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 spinner.spin();
249 }
250
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 double time_in_the_loop = timer.tac();
251 1 double therotical_time_in_the_loop =
252 1 period_sec * static_cast<double>(iteration);
253
254
3/12
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✓ Branch 19 taken 1 times.
✗ Branch 20 not taken.
1 ASSERT_NEAR(
255
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 time_in_the_loop, therotical_time_in_the_loop, 0.1 * period_sec);
256 }
257
258
3/6
✓ Branch 8 taken 14 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 14 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 14 times.
✗ Branch 15 not taken.
18 TEST_F(TestRealTimeTools, test_spinner_loop_too_slow)
259 {
260 // some parameters
261 1 double period_sec = 0.2;
262 1 int nb_it = 5;
263
264 // we create the spinner
265
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 real_time_tools::Spinner spinner;
266 1 spinner.set_period(period_sec);
267
268 // we mesaure the time of the loop. This should be roughly the same as:
269 // nb_it * period_sec in seconds.
270
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 real_time_tools::Timer timer;
271
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 timer.tic();
272
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
6 for (int i = 0; i < nb_it; ++i)
273 {
274
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 4 times.
5 if (i == 0)
275 {
276
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 real_time_tools::Timer::sleep_sec(nb_it * period_sec);
277 }
278
1/2
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
5 spinner.spin();
279 }
280
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 double time_in_the_loop = timer.tac();
281 1 double therotical_time_in_the_loop =
282 1 period_sec * static_cast<double>(2 * nb_it - 1);
283
284
3/12
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✓ Branch 19 taken 1 times.
✗ Branch 20 not taken.
1 ASSERT_NEAR(
285
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 time_in_the_loop, therotical_time_in_the_loop, 0.1 * period_sec);
286 }
287
288
3/6
✓ Branch 8 taken 14 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 14 times.
✗ Branch 12 not taken.
✓ Branch 14 taken 14 times.
✗ Branch 15 not taken.
18 TEST_F(TestRealTimeTools, test_frequency_manager)
289 {
290 1 double frequency = 200.0;
291
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 FrequencyManager freq_manager(frequency);
292
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 RealTimeCheck check(frequency, frequency * 2);
293 1 bool success = true;
294
2/2
✓ Branch 0 taken 400 times.
✓ Branch 1 taken 1 times.
401 for (int i = 0; i < 400; i++)
295 {
296
1/2
✓ Branch 1 taken 400 times.
✗ Branch 2 not taken.
400 check.tick();
297
1/2
✓ Branch 1 taken 400 times.
✗ Branch 2 not taken.
400 success = freq_manager.wait();
298
3/12
✓ Branch 1 taken 400 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 400 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✓ Branch 19 taken 400 times.
✗ Branch 20 not taken.
400 ASSERT_EQ(success, true);
299 }
300
4/14
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✓ Branch 22 taken 1 times.
✗ Branch 23 not taken.
1 ASSERT_NEAR(frequency, check.get_average_frequency(), 20);
301
2/2
✓ Branch 0 taken 400 times.
✓ Branch 1 taken 1 times.
401 for (int i = 0; i < 400; i++)
302 {
303
1/2
✓ Branch 1 taken 400 times.
✗ Branch 2 not taken.
400 Timer::sleep_sec(0.006);
304
1/2
✓ Branch 1 taken 400 times.
✗ Branch 2 not taken.
400 success = freq_manager.wait();
305
3/12
✓ Branch 1 taken 400 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 400 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✓ Branch 19 taken 400 times.
✗ Branch 20 not taken.
400 ASSERT_EQ(success, false);
306 }
307
2/4
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 14 times.
✗ Branch 4 not taken.
42 }
308