timestamp.cpp
Go to the documentation of this file.
1 
8 /*****************************************************************************
9 ** Includes
10 *****************************************************************************/
11 
12 #include <cstdlib>
13 #include <gtest/gtest.h>
14 #include "../../include/ecl/time/timestamp.hpp"
15 
16 /*****************************************************************************
17 ** Platform Check
18 *****************************************************************************/
19 
20 #ifdef ECL_HAS_TIMESTAMP
21 
22 /*****************************************************************************
23 ** Using
24 *****************************************************************************/
25 
26 using ecl::TimeStamp;
27 
28 /*****************************************************************************
29 ** Variables
30 *****************************************************************************/
31 
32 bool verbose = false;
33 
34 /*****************************************************************************
35 ** Tests
36 *****************************************************************************/
37 
38 TEST(TimeStampTests,construction) {
39  TimeStamp time;
40  SUCCEED();
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);
45 }
46 
47 TEST(TimeStampTests,timestamps) {
48  TimeStamp time;
49  time.stamp();
50  long t = time.sec();
51  t = time.msec();
52  t = time.usec();
53  t = time.nsec();
54  SUCCEED();
55  time.stamp(3,1425);
56  EXPECT_FLOAT_EQ(3.000001425,time);
57  time.stamp(4.00000142);
58  EXPECT_FLOAT_EQ(4.00000142,time);
59 }
60 
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());
66 }
67 
68 TEST(TimeStampTests,copyAssignment) {
69  TimeStamp time(3,100);
70  TimeStamp time_copy;
71  time_copy = time;
72  EXPECT_EQ(3,time_copy.sec());
73  EXPECT_EQ(100,time_copy.nsec());
74 }
75 
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);
85  time.stamp();
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);
92 }
93 
94 TEST(TimeStampTests,mathematicalOperators) {
95  TimeStamp time, time_copy;
96  time.stamp(1,100100100L);
97  time_copy.stamp(1,900100100L);
98  time += time_copy;
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);
106  time -= time_copy;
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());
113 }
114 
115 TEST(TimeStampTests,negatives) {
116  if ( verbose ) {
117  std::cout << "*****************************************************************************" << std::endl;
118  std::cout << "* Negatives" << std::endl;
119  std::cout << "*****************************************************************************" << std::endl;
120  }
121  /****************************************
122  ** Variables
123  ****************************************/
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);
130  /****************************************
131  ** Constructor & Operator <<
132  ****************************************/
133  std::vector<TimeStamp> timestamps = { TimeStamp(1.7),
134  TimeStamp(0.7),
135  TimeStamp(-0.7),
136  TimeStamp(-1.7),
137  };
138  if ( verbose ) {
139  std::cout << "Operator <<\n ";
140  for ( const TimeStamp& timestamp : timestamps) {
141  std::cout << timestamp << " ";
142  }
143  std::cout << std::endl;
144  }
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());
153 
154  std::vector<float> double_representations;
155  for ( const TimeStamp& timestamp : timestamps) {
156  double_representations.push_back(timestamp);
157  }
158  if ( verbose ) {
159  std::cout << "Operator double()\n ";
160  for ( const float& d : double_representations ) {
161  std::cout << d << " ";
162  }
163  std::cout << std::endl;
164  }
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]);
169 
170  /****************************************
171  ** Operator -
172  ****************************************/
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));
180  if ( verbose ) {
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;
189  }
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]);
196 
197  /****************************************
198  ** Operator -=
199  ****************************************/
200  double_representations.clear();
201  ecl::TimeStamp t;
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);
214  if ( verbose ) {
215  std::cout << "Operator -=\n ";
216  for (const double& d : double_representations) {
217  std::cout << d << " ";
218  }
219  std::cout << std::endl;
220  }
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]);
227  /****************************************
228  ** Operator +
229  ****************************************/
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);
239  if ( verbose ) {
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;
249  }
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());
258 
259  /****************************************
260  ** Operator +=
261  ****************************************/
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);
279  if ( verbose ) {
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;
289  }
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());
298 
299  /****************************************
300  ** Operator <
301  ****************************************/
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);
319  if ( verbose ) {
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;
337  }
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 ) {
340  if ( expected[i] ) {
341  EXPECT_TRUE(comparisons[i]);
342  } else {
343  EXPECT_FALSE(comparisons[i]);
344  }
345  }
346  // TODO other comparison tests - didn't have to change that code for negative numbers
347 }
348 
349 #endif /* ECL_HAS_TIMESTAMP */
350 
351 /*****************************************************************************
352 ** Main program
353 *****************************************************************************/
354 
355 int main(int argc, char **argv) {
356 
357  testing::InitGoogleTest(&argc,argv);
358  return RUN_ALL_TESTS();
359 }
TEST(TimeDataTests, container)
int main(int argc, char **argv)
Definition: timestamp.cpp:355


ecl_time
Author(s): Daniel Stonier
autogenerated on Mon Jun 10 2019 13:08:15