astrotypes  0.0
TimeTest.cpp
Go to the documentation of this file.
1 /*
2  * MIT License
3  *
4  * Copyright (c) 2018 PulsarSearchSoft
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 
25 #include "../TimeTest.h"
26 
27 namespace pss {
28 namespace astrotypes {
29 namespace units {
30 namespace test {
31 
32 void TimeTest::SetUp() {}
33 
35 
37 
39 
40 TEST(TimeTest, test_seconds)
41 {
42  std::chrono::seconds chrono_seconds(10);
43  std::chrono::milliseconds chrono_milliseconds(1000);
44  Quantity<Seconds, int> local_seconds(chrono_seconds);
45 
46  ASSERT_EQ(chrono_seconds.count(), local_seconds.value());
47  local_seconds = Quantity<Seconds, int>(chrono_milliseconds);
48  ASSERT_EQ(1, local_seconds.value());
49 }
50 
51 TEST(TimeTest, operator_equal_chrono_quantity)
52 {
53  boost::units::quantity<Seconds, double> b(10 * seconds);
54  const std::chrono::seconds chrono_seconds(10);
55 
56  ASSERT_TRUE(chrono_seconds == b);
57  ASSERT_TRUE(b == chrono_seconds);
58  ASSERT_FALSE(chrono_seconds != b);
59  ASSERT_FALSE(b != chrono_seconds);
60 
61 }
62 
63 TEST(TimeTest, chrono_duration_operator_less_than_quantity)
64 {
65  boost::units::quantity<Seconds, double> b(10 * seconds);
66  ASSERT_FALSE(std::chrono::milliseconds(10001) < b);
67  ASSERT_FALSE(std::chrono::milliseconds(10000) < b);
68  ASSERT_TRUE(std::chrono::milliseconds(9999) < b);
69 
70  ASSERT_FALSE(std::chrono::seconds(11) < b);
71  ASSERT_FALSE(std::chrono::seconds(10) < b);
72  ASSERT_TRUE(std::chrono::seconds(9) < b);
73 }
74 
75 TEST(TimeTest, chrono_duration_operator_less_than_equal_quantity)
76 {
77  boost::units::quantity<Seconds, double> b(10 * seconds);
78  ASSERT_FALSE(std::chrono::milliseconds(10001) <= b);
79  ASSERT_TRUE(std::chrono::milliseconds(10000) <= b);
80  ASSERT_TRUE(std::chrono::milliseconds(9999) <= b);
81 
82  ASSERT_FALSE(std::chrono::seconds(11) <= b);
83  ASSERT_TRUE(std::chrono::seconds(10) <= b);
84  ASSERT_TRUE(std::chrono::seconds(9) <= b);
85 }
86 
87 TEST(TimeTest, chrono_duration_operator_greater_than_quantity)
88 {
89  boost::units::quantity<Seconds, double> b(10 * seconds);
90  ASSERT_TRUE(std::chrono::milliseconds(10001) > b);
91  ASSERT_FALSE(std::chrono::milliseconds(10000) > b);
92  ASSERT_FALSE(std::chrono::milliseconds(9999) > b);
93 
94  ASSERT_TRUE(std::chrono::seconds(11) > b);
95  ASSERT_FALSE(std::chrono::seconds(10) > b);
96  ASSERT_FALSE(std::chrono::seconds(9) > b);
97 }
98 
99 TEST(TimeTest, chrono_duration_operator_greater_than_equal_quantity)
100 {
101  boost::units::quantity<Seconds, double> b(10 * seconds);
102  ASSERT_TRUE(std::chrono::milliseconds(10001) >= b);
103  ASSERT_TRUE(std::chrono::milliseconds(10000) >= b);
104  ASSERT_FALSE(std::chrono::milliseconds(9999) >= b);
105 
106  ASSERT_TRUE(std::chrono::seconds(11) >= b);
107  ASSERT_TRUE(std::chrono::seconds(10) >= b);
108  ASSERT_FALSE(std::chrono::seconds(9) >= b);
109 }
110 
111 
112 TEST(TimeTest, quantity_operator_plus)
113 {
114  typedef boost::units::quantity<Seconds, double> BoostType;
115  const std::chrono::seconds chrono_seconds(10);
116  const BoostType a(100 * seconds);
117 
118  BoostType b = a + chrono_seconds;
119  ASSERT_EQ( b, BoostType(110 * seconds) );
120 
121 }
122 
123 TEST(TimeTest, duration_operator_plus)
124 {
125  typedef boost::units::quantity<Seconds, double> BoostType;
126  const std::chrono::seconds chrono_seconds(100);
127  const BoostType a(10 * seconds);
128 
129  std::chrono::seconds c = chrono_seconds + a;
130  ASSERT_EQ( c, std::chrono::seconds(110) );
131 }
132 
133 TEST(TimeTest, quantity_operator_minus)
134 {
135  typedef boost::units::quantity<Seconds, double> BoostType;
136  std::chrono::seconds chrono_seconds(10);
137  BoostType a(100 * seconds);
138 
139  BoostType b = a - chrono_seconds;
140  ASSERT_EQ( b, BoostType(90 * seconds) );
141 
142 }
143 
144 TEST(TimeTest, duration_operator_minus)
145 {
146  typedef boost::units::quantity<Seconds, double> BoostType;
147  std::chrono::seconds chrono_seconds(100);
148  BoostType a(10 * seconds);
149 
150  std::chrono::seconds c = chrono_seconds - a;
151  ASSERT_EQ( c, std::chrono::seconds(90) );
152 }
153 
154 TEST(TimeTest, quantity_operator_plus_equal)
155 {
156  typedef boost::units::quantity<Seconds, double> BoostType;
157  std::chrono::seconds chrono_seconds(10);
158  BoostType a(100 * seconds);
159 
160  a += chrono_seconds;
161  ASSERT_EQ( a, BoostType(110 * seconds) );
162 
163 }
164 
165 TEST(TimeTest, duration_operator_plus_equal)
166 {
167  typedef boost::units::quantity<Seconds, double> BoostType;
168  std::chrono::seconds chrono_seconds(10);
169  BoostType b(100 * seconds);
170 
171  chrono_seconds += b;
172  ASSERT_EQ( chrono_seconds, std::chrono::seconds(110) );
173 }
174 
175 TEST(TimeTest, quantity_operator_minus_equal)
176 {
177  typedef boost::units::quantity<Seconds, double> BoostType;
178  std::chrono::seconds chrono_seconds(10);
179  BoostType a(100 * seconds);
180 
181  a -= chrono_seconds;
182  ASSERT_EQ( a, BoostType(90 * seconds) );
183 }
184 
185 TEST(TimeTest, duration_operator_minus_equal)
186 {
187  typedef boost::units::quantity<Seconds, double> BoostType;
188  std::chrono::seconds chrono_seconds(100);
189  BoostType b(10 * seconds);
190 
191  chrono_seconds -= b;
192  ASSERT_EQ( chrono_seconds, std::chrono::seconds(90) );
193 }
194 
195 TEST(TimeTest, quantity_operator_divide)
196 {
197  typedef boost::units::quantity<MilliSeconds, double> BoostType;
198  const std::chrono::seconds chrono_seconds(10);
199  const BoostType a(100 * seconds);
200 
201  auto res = a / chrono_seconds;
202  ASSERT_EQ( res, 10.0 );
203 }
204 
205 TEST(TimeTest, duration_operator_divide)
206 {
207  typedef boost::units::quantity<MilliSeconds, double> BoostType;
208  const std::chrono::seconds chrono_seconds(100);
209  const BoostType a(10 * seconds);
210 
211  auto res = chrono_seconds / a;
212  ASSERT_EQ( res, 10.0 );
213 }
214 
215 TEST(TimeTest, Quantity_operator_construct)
216 {
219  Quantity<MilliSeconds, double> expect(100 * seconds);
220  ASSERT_EQ(b, expect);
221 
222  // from a boost::units::quantity
223  boost::units::quantity<MilliSeconds, double> c(a);
224  ASSERT_EQ(c, expect);
225 }
226 
227 TEST(TimeTest, Quantity_operator_equal_assign)
228 {
230  Quantity<MilliSeconds, double> expect(100 * seconds);
232  ASSERT_NE(b, expect);
233  b = a;
234  ASSERT_EQ(b, expect);
235 
236  // with a boost::quantity
237  boost::units::quantity<boost::units::si::time, double> c(200 * seconds);
238  Quantity<MilliSeconds, double> expect2(200 * seconds);
239  b = c;
240  ASSERT_EQ(b, expect2);
241 }
242 
243 TEST(TimeTest, duration_cast_milliseconds_to_seconds)
244 {
245  // chrono -> boost
246  typedef boost::units::quantity<Seconds, double> BoostType;
247  std::chrono::duration<double, std::ratio<1,1000>> d(100);
248  auto conversion = duration_cast<BoostType>(d);
249  static_assert(std::is_same<decltype(conversion), BoostType>::value, "wrong type returned by cast");
250  BoostType bs(100 * milliseconds);
251  ASSERT_EQ(conversion.value(), bs.value());
252 
253  // boost -> chrono
254  typedef std::chrono::duration<double, std::ratio<1>> ChronoType;
255  boost::units::quantity<MilliSeconds, double> q(100 * seconds);;
256  auto chrono_conversion = duration_cast<ChronoType>(q);
257  static_assert(std::is_same<decltype(chrono_conversion), ChronoType>::value, "wrong type returned by cast");
258  ASSERT_EQ(1000 * chrono_conversion.count(), q.value());
259 }
260 
261 TEST(TimeTest, duration_cast_seconds_to_seconds)
262 {
263  // chrono -> boost
264  typedef boost::units::quantity<Seconds, double> BoostType;
265  std::chrono::duration<double, std::ratio<1,1>> d(100);
266  auto conversion = duration_cast<BoostType>(d);
267  static_assert(std::is_same<decltype(conversion), BoostType>::value, "wrong type returned by cast");
268  BoostType bs(100 * seconds);
269  ASSERT_EQ(conversion.value(), bs.value());
270 
271  // boost -> chrono
272  typedef std::chrono::duration<double, std::ratio<1>> ChronoType;
273  boost::units::quantity<Seconds, double> q(10 * seconds);;
274  auto chrono_conversion = duration_cast<ChronoType>(q);
275  static_assert(std::is_same<decltype(chrono_conversion), ChronoType>::value, "wrong type returned by cast");
276  ASSERT_EQ(chrono_conversion.count(), q.value());
277 }
278 
279 TEST(TimeTest, duration_cast_const_seconds_to_seconds)
280 {
281  // chrono -> boost
282  typedef boost::units::quantity<Seconds, double> BoostType;
283  const std::chrono::duration<double, std::ratio<1,1>> d(100);
284  auto conversion = duration_cast<BoostType>(d);
285  static_assert(std::is_same<decltype(conversion), BoostType>::value, "wrong type returned by cast");
286  BoostType bs(100 * seconds);
287  ASSERT_EQ(conversion.value(), bs.value());
288 
289  // boost -> boost
290  BoostType bs2 = duration_cast<BoostType>(bs);
291  ASSERT_EQ(bs2.value(), bs.value());
292 
293  // boost -> chrono
294  typedef std::chrono::duration<double, std::ratio<1>> ChronoType;
295  const boost::units::quantity<Seconds, double> q(10 * seconds);;
296  auto chrono_conversion = duration_cast<ChronoType>(q);
297  static_assert(std::is_same<decltype(chrono_conversion), ChronoType>::value, "wrong type returned by cast");
298  ASSERT_EQ(chrono_conversion.count(), q.value());
299 }
300 
301 TEST(TimeTest, duration_cast_const_seconds_to_milliseconds)
302 {
303  // chrono -> boost
304  typedef boost::units::quantity<MilliSeconds, double> BoostType;
305  const std::chrono::duration<double, std::ratio<1,1>> d(100);
306  auto conversion = duration_cast<BoostType>(d);
307  static_assert(std::is_same<decltype(conversion), BoostType>::value, "wrong type returned by cast");
308  BoostType bs(100 * seconds);
309  ASSERT_EQ(conversion.value(), bs.value());
310 
311  // boost -> chrono
312  typedef std::chrono::duration<double, std::milli> ChronoType;
313  boost::units::quantity<Seconds, double> q(100 * seconds);;
314  auto chrono_conversion = duration_cast<ChronoType>(q);
315  static_assert(std::is_same<decltype(chrono_conversion), typename std::remove_cv<ChronoType>::type>::value, "wrong type returned by cast");
316  ASSERT_EQ(chrono_conversion.count(), 1000 * q.value());
317 
318  // boost -> boost
319  const BoostType bs2 = duration_cast<BoostType>(q);
320  ASSERT_EQ(bs2.value(), q.value() * 1000);
321 }
322 
323 TEST(TimeTest, duration_cast_seconds_to_milliseconds)
324 {
325  // chrono -> boost
326  typedef boost::units::quantity<MilliSeconds, double> BoostType;
327  std::chrono::duration<double, std::ratio<1,1>> d(100);
328  auto conversion = duration_cast<BoostType>(d);
329  static_assert(std::is_same<decltype(conversion), BoostType>::value, "wrong type returned by cast");
330  BoostType bs(100 * seconds);
331  ASSERT_EQ(conversion.value(), bs.value());
332 
333  // boost -> chrono
334  typedef std::chrono::duration<double, std::milli> ChronoType;
335  boost::units::quantity<Seconds, double> q(100 * seconds);;
336  auto chrono_conversion = duration_cast<ChronoType>(q);
337  static_assert(std::is_same<decltype(chrono_conversion), ChronoType>::value, "wrong type returned by cast");
338  ASSERT_EQ(chrono_conversion.count(), 1000 * q.value());
339 
340  // boost -> boost
341  const BoostType bs2 = duration_cast<BoostType>(q);
342  ASSERT_EQ(bs2.value(), q.value() * 1000);
343 }
344 
345 TEST(TimeTest, duration_cast_milliseconds_to_milliseconds)
346 {
347  // chrono -> boost
348  typedef boost::units::quantity<MilliSeconds, double> BoostType;
349  std::chrono::duration<double, std::milli> d(100);
350  auto conversion = duration_cast<BoostType>(d);
351  static_assert(std::is_same<decltype(conversion), BoostType>::value, "wrong type returned by cast");
352  BoostType bs(100 * milliseconds);
353  ASSERT_EQ(conversion.value(), bs.value());
354 
355  // boost -> chrono
356  typedef std::chrono::duration<double, std::milli> ChronoType;
357  boost::units::quantity<MilliSeconds, double> q(10 * seconds);;
358  auto chrono_conversion = duration_cast<ChronoType>(q);
359  static_assert(std::is_same<decltype(chrono_conversion), ChronoType>::value, "wrong type returned by cast");
360  ASSERT_EQ(chrono_conversion.count(), q.value());
361 }
362 
363 } // namespace test
364 } // namespace units
365 } // namespace astrotypes
366 } // namespace pss
367 
TEST(TimeTest, test_seconds)
Definition: TimeTest.cpp:40
constexpr std::enable_if< boost::units::is_quantity< BoostQuantity >::value &&boost::units::is_unit_of_dimension< typename BoostQuantity::unit_type, boost::units::time_dimension >::value &&!is_equivalent< BoostQuantity, std::chrono::duration< ChronoNumericalRep, PeriodType > >::value, BoostQuantity >::type duration_cast(const std::chrono::duration< ChronoNumericalRep, PeriodType > &duration)
Mimic the std::duration_cast to convert to/from boost::units::quantity tyeps.