13 #include <gtest/gtest.h> 14 #include "../../include/ecl/time/timestamp.hpp" 20 #ifdef ECL_HAS_TIMESTAMP 38 TEST(TimeStampTests,construction) {
41 TimeStamp time_pair(3,123456789L);
42 EXPECT_FLOAT_EQ(3.123456789,time_pair);
43 TimeStamp time_double(3.00001);
44 EXPECT_FLOAT_EQ(3.00001,time_double);
47 TEST(TimeStampTests,timestamps) {
56 EXPECT_FLOAT_EQ(3.000001425,time);
57 time.stamp(4.00000142);
58 EXPECT_FLOAT_EQ(4.00000142,time);
61 TEST(TimeStampTests,copyConstruction) {
62 TimeStamp time(3,100);
63 TimeStamp time_copy(time);
64 EXPECT_EQ(3,time_copy.sec());
65 EXPECT_EQ(100,time_copy.nsec());
68 TEST(TimeStampTests,copyAssignment) {
69 TimeStamp time(3,100);
72 EXPECT_EQ(3,time_copy.sec());
73 EXPECT_EQ(100,time_copy.nsec());
76 TEST(TimeStampTests,comparisonOperators) {
77 TimeStamp time(3,100);
78 TimeStamp time_copy(time);
79 EXPECT_TRUE(time==time_copy);
80 EXPECT_FALSE(time!=time_copy);
81 EXPECT_TRUE(time<=time_copy);
82 EXPECT_TRUE(time>=time_copy);
83 EXPECT_FALSE(time<time_copy);
84 EXPECT_FALSE(time>time_copy);
86 EXPECT_FALSE(time==time_copy);
87 EXPECT_TRUE(time!=time_copy);
88 EXPECT_FALSE(time<=time_copy);
89 EXPECT_TRUE(time>=time_copy);
90 EXPECT_FALSE(time<time_copy);
91 EXPECT_TRUE(time>time_copy);
94 TEST(TimeStampTests,mathematicalOperators) {
95 TimeStamp time, time_copy;
96 time.stamp(1,100100100L);
97 time_copy.stamp(1,900100100L);
99 EXPECT_EQ(3,time.sec());
100 EXPECT_EQ(200200,time.nsec());
101 time.stamp(1,100100100L);
102 time = time + time_copy;
103 EXPECT_EQ(3,time.sec());
104 EXPECT_EQ(200200,time.nsec());
105 time.stamp(2,100100100L);
107 EXPECT_EQ(0,time.sec());
108 EXPECT_EQ(200000000,time.nsec());
109 time.stamp(2,100100100L);
110 time = time - time_copy;
111 EXPECT_EQ(0,time.sec());
112 EXPECT_EQ(200000000,time.nsec());
115 TEST(TimeStampTests,negatives) {
117 std::cout <<
"*****************************************************************************" << std::endl;
118 std::cout <<
"* Negatives" << std::endl;
119 std::cout <<
"*****************************************************************************" << std::endl;
124 ecl::TimeStamp time_2_3(2,300000000L), time_n_2_3(-2, -300000000L);
125 ecl::TimeStamp time_1_3(1,300000000L), time_n_1_3(-1,-300000000L);
126 ecl::TimeStamp time_0_3(0,300000000L), time_n_0_3(0,-300000000L);
127 ecl::TimeStamp time_2_7(2,700000000L), time_n_2_7(-2,-700000000L);
128 ecl::TimeStamp time_1_7(1,700000000L), time_n_1_7(-1,-700000000L);
129 ecl::TimeStamp time_0_7(0,700000000L), time_n_0_7(0,-700000000L);
133 std::vector<TimeStamp> timestamps = { TimeStamp(1.7),
139 std::cout <<
"Operator <<\n ";
140 for (
const TimeStamp& timestamp : timestamps) {
141 std::cout << timestamp <<
" ";
143 std::cout << std::endl;
145 EXPECT_EQ(1, timestamps[0].sec());
146 EXPECT_EQ(700000000, timestamps[0].nsec());
147 EXPECT_EQ(0, timestamps[1].sec());
148 EXPECT_EQ(700000000, timestamps[1].nsec());
149 EXPECT_EQ(0, timestamps[2].sec());
150 EXPECT_EQ(-700000000, timestamps[2].nsec());
151 EXPECT_EQ(-1, timestamps[3].sec());
152 EXPECT_EQ(-700000000, timestamps[3].nsec());
154 std::vector<float> double_representations;
155 for (
const TimeStamp& timestamp : timestamps) {
156 double_representations.push_back(timestamp);
159 std::cout <<
"Operator double()\n ";
160 for (
const float& d : double_representations ) {
161 std::cout << d <<
" ";
163 std::cout << std::endl;
165 EXPECT_FLOAT_EQ(1.7, double_representations[0]);
166 EXPECT_FLOAT_EQ(0.7, double_representations[1]);
167 EXPECT_FLOAT_EQ(-0.7, double_representations[2]);
168 EXPECT_FLOAT_EQ(-1.7, double_representations[3]);
173 double_representations.clear();
174 double_representations.push_back(ecl::TimeStamp(1.3) - ecl::TimeStamp(5.1));
175 double_representations.push_back(ecl::TimeStamp(1.3) - ecl::TimeStamp(5.8));
176 double_representations.push_back(ecl::TimeStamp(1.3) - ecl::TimeStamp(1.5));
177 double_representations.push_back(ecl::TimeStamp(-1.3) - ecl::TimeStamp(1.5));
178 double_representations.push_back(ecl::TimeStamp(-1.3) - ecl::TimeStamp(-0.8));
179 double_representations.push_back(ecl::TimeStamp(-1.3) - ecl::TimeStamp(-5.8));
181 std::cout <<
"Operator -\n ";
182 std::cout << (ecl::TimeStamp(1.3) - ecl::TimeStamp(5.1)) <<
" ";
183 std::cout << (ecl::TimeStamp(1.3) - ecl::TimeStamp(5.8)) <<
" ";
184 std::cout << (ecl::TimeStamp(1.3) - ecl::TimeStamp(1.5)) <<
" ";
185 std::cout << (ecl::TimeStamp(-1.3) - ecl::TimeStamp(1.5)) <<
" ";
186 std::cout << (ecl::TimeStamp(-1.3) - ecl::TimeStamp(-0.8)) <<
" ";
187 std::cout << (ecl::TimeStamp(-1.3) - ecl::TimeStamp(-5.8)) <<
" ";
188 std::cout << std::endl;
190 EXPECT_FLOAT_EQ(-3.8, double_representations[0]);
191 EXPECT_FLOAT_EQ(-4.5, double_representations[1]);
192 EXPECT_FLOAT_EQ(-0.2, double_representations[2]);
193 EXPECT_FLOAT_EQ(-2.8, double_representations[3]);
194 EXPECT_FLOAT_EQ(-0.5, double_representations[4]);
195 EXPECT_FLOAT_EQ(4.5, double_representations[5]);
200 double_representations.clear();
202 t = time_1_3; t -= ecl::TimeStamp(5.1);
203 double_representations.push_back(t);
204 t = time_1_3; t -= ecl::TimeStamp(5.8);
205 double_representations.push_back(t);
206 t = time_1_3; t -= ecl::TimeStamp(1.5);
207 double_representations.push_back(t);
208 t = time_n_1_3; t -= ecl::TimeStamp(1.5);
209 double_representations.push_back(t);
210 t = time_n_1_3; t -= ecl::TimeStamp(-0.8);
211 double_representations.push_back(t);
212 t = time_n_1_3; t -= ecl::TimeStamp(-5.8);
213 double_representations.push_back(t);
215 std::cout <<
"Operator -=\n ";
216 for (
const double& d : double_representations) {
217 std::cout << d <<
" ";
219 std::cout << std::endl;
221 EXPECT_FLOAT_EQ(-3.8, double_representations[0]);
222 EXPECT_FLOAT_EQ(-4.5, double_representations[1]);
223 EXPECT_FLOAT_EQ(-0.2, double_representations[2]);
224 EXPECT_FLOAT_EQ(-2.8, double_representations[3]);
225 EXPECT_FLOAT_EQ(-0.5, double_representations[4]);
226 EXPECT_FLOAT_EQ(4.5, double_representations[5]);
230 double_representations.clear(); timestamps.clear();
231 double_representations.push_back(time_1_3 + time_n_2_3); timestamps.push_back(time_1_3 + time_n_2_3);
232 double_representations.push_back(time_1_3 + time_n_2_7); timestamps.push_back(time_1_3 + time_n_2_7);
233 double_representations.push_back(time_1_3 + time_n_1_7); timestamps.push_back(time_1_3 + time_n_1_7);
234 double_representations.push_back(time_0_3 + time_n_0_7); timestamps.push_back(time_0_3 + time_n_0_7);
235 double_representations.push_back(time_1_3 + time_n_0_7); timestamps.push_back(time_1_3 + time_n_0_7);
236 double_representations.push_back(time_2_3 + time_n_0_7); timestamps.push_back(time_2_3 + time_n_0_7);
237 double_representations.push_back(time_n_1_3 + time_n_1_7); timestamps.push_back(time_n_1_3 + time_n_1_7);
238 double_representations.push_back(time_n_1_7 + time_n_1_7); timestamps.push_back(time_n_1_7 + time_n_1_7);
240 std::cout <<
"Operator +" << std::endl;
241 std::cout <<
" 1.3 + (-2.3): " << double_representations[0] << std::endl;
242 std::cout <<
" 1.3 + (-2.7): " << double_representations[1] << std::endl;
243 std::cout <<
" 1.3 + (-1.7): " << double_representations[2] << std::endl;
244 std::cout <<
" 0.3 + (-0.7): " << double_representations[3] << std::endl;
245 std::cout <<
" 1.3 + (-0.7): " << double_representations[4] << std::endl;
246 std::cout <<
" 2.3 + (-0.7): " << double_representations[5] << std::endl;
247 std::cout <<
" -1.3 + (-1.7): " << double_representations[6] << std::endl;
248 std::cout <<
" -1.7 + (-1.7): " << double_representations[7] << std::endl;
250 EXPECT_FLOAT_EQ(-1.0, double_representations[0]); EXPECT_EQ(0, timestamps[0].nsec());
251 EXPECT_FLOAT_EQ(-1.4, double_representations[1]); EXPECT_EQ(-400000000, timestamps[1].nsec());
252 EXPECT_FLOAT_EQ(-0.4, double_representations[2]); EXPECT_EQ(-400000000, timestamps[2].nsec());
253 EXPECT_FLOAT_EQ(-0.4, double_representations[3]); EXPECT_EQ(-400000000, timestamps[3].nsec());
254 EXPECT_FLOAT_EQ(0.6, double_representations[4]); EXPECT_EQ(600000000, timestamps[4].nsec());
255 EXPECT_FLOAT_EQ(1.6, double_representations[5]); EXPECT_EQ(600000000, timestamps[5].nsec());
256 EXPECT_FLOAT_EQ(-3.0, double_representations[6]); EXPECT_EQ(000000000, timestamps[6].nsec());
257 EXPECT_FLOAT_EQ(-3.4, double_representations[7]); EXPECT_EQ(-400000000, timestamps[7].nsec());
262 double_representations.clear(); timestamps.clear();
263 t = time_1_3; t += time_n_2_3;
264 double_representations.push_back(t); timestamps.push_back(t);
265 t = time_1_3; t += time_n_2_7;
266 double_representations.push_back(t); timestamps.push_back(t);
267 t = time_1_3; t += time_n_1_7;
268 double_representations.push_back(t); timestamps.push_back(t);
269 t = time_0_3; t += time_n_0_7;
270 double_representations.push_back(t); timestamps.push_back(t);
271 t = time_1_3; t += time_n_0_7;
272 double_representations.push_back(t); timestamps.push_back(t);
273 t = time_2_3; t += time_n_0_7;
274 double_representations.push_back(t); timestamps.push_back(t);
275 t = time_n_1_3; t += time_n_1_7;
276 double_representations.push_back(t); timestamps.push_back(t);
277 t = time_n_1_7; t += time_n_1_7;
278 double_representations.push_back(t); timestamps.push_back(t);
280 std::cout <<
"Operator +=" << std::endl;
281 std::cout <<
" 1.3 + (-2.3): " << double_representations[0] << std::endl;
282 std::cout <<
" 1.3 + (-2.7): " << double_representations[1] << std::endl;
283 std::cout <<
" 1.3 + (-1.7): " << double_representations[2] << std::endl;
284 std::cout <<
" 0.3 + (-0.7): " << double_representations[3] << std::endl;
285 std::cout <<
" 1.3 + (-0.7): " << double_representations[4] << std::endl;
286 std::cout <<
" 2.3 + (-0.7): " << double_representations[5] << std::endl;
287 std::cout <<
" -1.3 + (-1.7): " << double_representations[6] << std::endl;
288 std::cout <<
" -1.7 + (-1.7): " << double_representations[7] << std::endl;
290 EXPECT_FLOAT_EQ(-1.0, double_representations[0]); EXPECT_EQ(0, timestamps[0].nsec());
291 EXPECT_FLOAT_EQ(-1.4, double_representations[1]); EXPECT_EQ(-400000000, timestamps[1].nsec());
292 EXPECT_FLOAT_EQ(-0.4, double_representations[2]); EXPECT_EQ(-400000000, timestamps[2].nsec());
293 EXPECT_FLOAT_EQ(-0.4, double_representations[3]); EXPECT_EQ(-400000000, timestamps[3].nsec());
294 EXPECT_FLOAT_EQ(0.6, double_representations[4]); EXPECT_EQ(600000000, timestamps[4].nsec());
295 EXPECT_FLOAT_EQ(1.6, double_representations[5]); EXPECT_EQ(600000000, timestamps[5].nsec());
296 EXPECT_FLOAT_EQ(-3.0, double_representations[6]); EXPECT_EQ(000000000, timestamps[6].nsec());
297 EXPECT_FLOAT_EQ(-3.4, double_representations[7]); EXPECT_EQ(-400000000, timestamps[7].nsec());
302 std::vector<bool> comparisons;
303 comparisons.push_back(time_n_1_3 < time_n_2_3);
304 comparisons.push_back(time_n_1_3 < time_2_3);
305 comparisons.push_back(time_1_3 < time_n_2_3);
306 comparisons.push_back(time_1_3 < time_2_3);
307 comparisons.push_back(time_n_2_3 < time_n_1_3);
308 comparisons.push_back(time_n_2_3 < time_1_3);
309 comparisons.push_back(time_2_3 < time_n_1_3);
310 comparisons.push_back(time_2_3 < time_1_3);
311 comparisons.push_back(time_n_0_3 < time_n_0_7);
312 comparisons.push_back(time_n_0_3 < time_0_7);
313 comparisons.push_back(time_0_3 < time_n_0_7);
314 comparisons.push_back(time_0_3 < time_0_7);
315 comparisons.push_back(time_n_0_7 < time_n_0_3);
316 comparisons.push_back(time_n_0_7 < time_0_3);
317 comparisons.push_back(time_0_7 < time_n_0_3);
318 comparisons.push_back(time_0_7 < time_0_3);
320 std::cout <<
"Operator <" << std::endl;
321 std::cout <<
" -1.3 < -2.3: " << (comparisons[0] ? std::string(
"true") : std::string(
"false")) << std::endl;
322 std::cout <<
" -1.3 < 2.3: " << (comparisons[1] ? std::string(
"true") : std::string(
"false")) << std::endl;
323 std::cout <<
" 1.3 < -2.3: " << (comparisons[2] ? std::string(
"true") : std::string(
"false")) << std::endl;
324 std::cout <<
" 1.3 < 2.3: " << (comparisons[3] ? std::string(
"true") : std::string(
"false")) << std::endl;
325 std::cout <<
" -2.3 < -1.3: " << (comparisons[4] ? std::string(
"true") : std::string(
"false")) << std::endl;
326 std::cout <<
" -2.3 < 1.3: " << (comparisons[5] ? std::string(
"true") : std::string(
"false")) << std::endl;
327 std::cout <<
" 2.3 < -1.3: " << (comparisons[6] ? std::string(
"true") : std::string(
"false")) << std::endl;
328 std::cout <<
" 2.3 < 1.3: " << (comparisons[7] ? std::string(
"true") : std::string(
"false")) << std::endl;
329 std::cout <<
" -0.3 < -0.7: " << (comparisons[8] ? std::string(
"true") : std::string(
"false")) << std::endl;
330 std::cout <<
" -0.3 < 0.7: " << (comparisons[9] ? std::string(
"true") : std::string(
"false")) << std::endl;
331 std::cout <<
" 0.3 < -0.7: " << (comparisons[10] ? std::string(
"true") : std::string(
"false")) << std::endl;
332 std::cout <<
" 0.3 < 0.7: " << (comparisons[11] ? std::string(
"true") : std::string(
"false")) << std::endl;
333 std::cout <<
" -0.7 < -0.3: " << (comparisons[12] ? std::string(
"true") : std::string(
"false")) << std::endl;
334 std::cout <<
" -0.7 < 0.3: " << (comparisons[13] ? std::string(
"true") : std::string(
"false")) << std::endl;
335 std::cout <<
" 0.7 < -0.3: " << (comparisons[14] ? std::string(
"true") : std::string(
"false")) << std::endl;
336 std::cout <<
" 0.7 < 0.3: " << (comparisons[15] ? std::string(
"true") : std::string(
"false")) << std::endl;
338 std::vector<bool> expected = {
false,
true,
false,
true,
true,
true,
false,
false,
false,
true,
false,
true,
true,
true,
false,
false };
339 for (
unsigned int i = 0; i < expected.size(); ++i ) {
341 EXPECT_TRUE(comparisons[i]);
343 EXPECT_FALSE(comparisons[i]);
355 int main(
int argc,
char **argv) {
357 testing::InitGoogleTest(&argc,argv);
358 return RUN_ALL_TESTS();
TEST(TimeDataTests, container)
int main(int argc, char **argv)