11 #define BOOST_TEST_MODULE ( quantities_test )
12 #include <boost/test/unit_test.hpp>
21 #include <type_traits>
30 static_assert(!util::quantities::concepts::details::has_unit_v<double>);
47 static_assert(!util::quantities::concepts::details::is_quantity_v<double>);
64 static_assert(!util::quantities::concepts::details::has_quantity_v<double>);
81 static_assert( util::quantities::second::isCompatibleValue<double>());
82 static_assert( util::quantities::second::isCompatibleValue<float>());
83 static_assert( util::quantities::second::isCompatibleValue<int>());
92 static_assert( util::quantities::second::hasCompatibleValue<double>());
93 static_assert( util::quantities::second::hasCompatibleValue<float>());
94 static_assert( util::quantities::second::hasCompatibleValue<int>());
110 (util::quantities::microsecond::sameBaseUnitAs<util::quantities::second>());
115 (!util::quantities::microsecond::sameUnitAs<util::quantities::second>());
121 using namespace util::quantities::time_literals;
125 BOOST_TEST(t == -4_us);
128 "Positive sign converts to a different type!"
130 BOOST_TEST(+t == -4_us);
133 "Negative sign converts to a different type!"
135 BOOST_TEST(-t == 4_us);
138 "Negative sign converts to a different type!"
140 BOOST_TEST(t.abs() == 4.0_us);
148 using namespace util::quantities::time_literals;
155 BOOST_TEST(t_s.value() == 7.0);
159 BOOST_TEST(t_us == 7
'000'000.0_us);
162 BOOST_TEST(t == 7.0_s);
164 static_assert(std::is_same<
180 BOOST_TEST( t_us == t_us);
181 BOOST_TEST(!(t_us != t_us));
182 BOOST_TEST( (t_us >= t_us));
183 BOOST_TEST( (t_us <= t_us));
184 BOOST_TEST(!(t_us > t_us));
185 BOOST_TEST(!(t_us < t_us));
188 BOOST_TEST( t_us != t_ns);
189 BOOST_TEST(!(t_us == t_ns));
190 BOOST_TEST( (t_us != t_ns));
191 BOOST_TEST( (t_us >= t_ns));
192 BOOST_TEST(!(t_us <= t_ns));
193 BOOST_TEST( (t_us > t_ns));
194 BOOST_TEST(!(t_us < t_ns));
197 BOOST_TEST( t_us == t2_ns);
198 BOOST_TEST(!(t_us != t2_ns));
199 BOOST_TEST( (t_us >= t2_ns));
200 BOOST_TEST( (t_us <= t2_ns));
201 BOOST_TEST(!(t_us > t2_ns));
202 BOOST_TEST(!(t_us < t2_ns));
204 BOOST_TEST( t_ns != t2_ns);
205 BOOST_TEST(!(t_ns == t2_ns));
206 BOOST_TEST( (t_ns != t2_ns));
207 BOOST_TEST(!(t_ns >= t2_ns));
208 BOOST_TEST( (t_ns <= t2_ns));
209 BOOST_TEST(!(t_ns > t2_ns));
210 BOOST_TEST( (t_ns < t2_ns));
221 using namespace util::quantities::time_literals;
227 auto const twice_t = 2.0 * t;
231 "Multiplication by a scalar converts to a different type!"
233 BOOST_TEST(twice_t == 6.0_s);
235 auto const t_twice = t * 2.0;
239 "Multiplication by a scalar converts to a different type!"
241 BOOST_TEST(twice_t == 6.0_s);
245 "Division by a scalar converts to a different type!"
247 BOOST_TEST(twice_t / 2.0 == 3.0_s);
250 std::is_same<decltype(twice_t / t),
double>(),
251 "Division by a scalar is not the base type!"
253 BOOST_TEST(twice_t / t == 2.0);
256 std::is_same<decltype(t / 300_us),
double>(),
257 "Division by a scalar is not the base type!"
259 BOOST_TEST(t / 300_us == 10
'000.0);
261 } // test_quantities_multiply_scalar()
264 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
265 void test_quantities_addition() {
267 using namespace util::quantities::time_literals;
270 // sum and difference
273 // 5_s + 700_ms; // ERROR!
274 // 5_s + 0.7; // ERROR!
278 std::is_same<std::decay_t<decltype(45_s + 5_s)>, util::quantities::seconds>(),
279 "Addition converts to a different type!"
281 BOOST_TEST(45_s + 5_s == 50_s);
284 std::is_same<decltype(5_s - 55_s), util::quantities::seconds>(),
285 "Subtraction converts to a different type!"
287 BOOST_TEST(5_s - 55_s == -50_s);
290 constexpr util::quantities::seconds t = 45_s;
293 <std::decay_t<decltype(t.plus(5000_ms))>, util::quantities::seconds>(),
294 "Addition converts to a different type!"
296 BOOST_TEST(t.plus(5000_ms) == 50_s);
297 BOOST_TEST(t == 45_s);
300 std::is_same<decltype(t.minus(55000_ms)), util::quantities::seconds>(),
301 "Subtraction converts to a different type!"
303 BOOST_TEST(t.minus(55000_ms) == -10_s);
304 BOOST_TEST(t == 45_s);
307 } // test_quantities_addition()
310 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
311 void test_quantities_increment() {
313 using namespace util::quantities::time_literals;
316 // increment and decrement by a quantity
318 util::quantities::seconds t { 0.05 };
322 std::is_same<decltype(t += 0.05_s), util::quantities::seconds&>(),
323 "Increment converts to a different type!"
325 BOOST_TEST(t == 0.1_s);
329 std::is_same<decltype(t -= 0.05_s), util::quantities::seconds&>(),
330 "Decrement converts to a different type!"
332 BOOST_TEST(t == 0.05_s);
336 std::is_same<decltype(t += 50_ms), util::quantities::seconds&>(),
337 "Increment converts to a different type!"
339 BOOST_TEST(t == 0.1_s);
343 std::is_same<decltype(t -= 50_ms), util::quantities::seconds&>(),
344 "Decrement converts to a different type!"
346 BOOST_TEST(t == 0.05_s);
348 } // test_quantities_multiply_scalar()
351 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
352 void test_quantities_scale() {
354 using namespace util::quantities::time_literals;
356 util::quantities::microseconds t { 11.0 };
362 std::is_same<decltype(t *= 2.0), util::quantities::microseconds&>(),
363 "Scaling converts to a different type!"
365 BOOST_TEST(t == 22.0_us);
369 std::is_same<decltype(t /= 2.0), util::quantities::microseconds&>(),
370 "Scaling (division) converts to a different type!"
372 BOOST_TEST(t == 11.0_us);
374 } // test_quantities_scale()
377 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
378 void test_quantities_literals() {
380 using namespace util::quantities::time_literals;
382 constexpr util::quantities::second t1 = 7_s;
383 static_assert(t1.value() == 7.0, "Literal assignment failed.");
385 constexpr util::quantities::microsecond t2 = 7_s;
386 static_assert(t2.value() == 7000000.0, "Literal conversion failed.");
388 util::quantities::microsecond t3;
390 BOOST_TEST(t3.value() == 7000000.0);
391 BOOST_TEST(t3 == 7000000_us);
393 static_assert(7000000_us == 7_s, "Literal conversion failed.");
395 } // test_quantities_literals()
398 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
399 void test_quantities() {
401 using namespace util::quantities::time_literals;
403 // ---------------------------------------------------------------------------
404 // default constructor
406 // BOOST_TEST_CHECKPOINT("Default constructor");
407 util::quantities::microseconds t1; // can't
do much with
this except assigning
416 BOOST_TEST(t1.value() == 4.0);
422 BOOST_TEST(t2 == 7.0_us);
433 using namespace util::quantities::time_literals;
440 static_assert(t1.value() == 10.0,
"value()");
441 static_assert(
double(t1) == 10.0,
"explicit conversion to plain number");
442 static_assert(+t1 == 10_us,
"unary +");
443 static_assert(-t1 == -10_us,
"unary -");
444 static_assert(t1.abs() == 10_us,
"abs()");
446 static_assert( (t1 == t1 ),
"comparison");
447 static_assert(!(t1 == t2 ),
"comparison");
448 static_assert(!(t1 == t_ns ),
"comparison");
449 static_assert( (t1 == t1_ns),
"comparison");
451 static_assert(!(t1 != t1 ),
"comparison");
452 static_assert( (t1 != t2 ),
"comparison");
453 static_assert( (t1 != t_ns ),
"comparison");
454 static_assert(!(t1 != t1_ns),
"comparison");
456 static_assert( (t1 >= t1 ),
"comparison");
457 static_assert(!(t1 >= t2 ),
"comparison");
458 static_assert( (t1 >= t_ns ),
"comparison");
459 static_assert( (t1 >= t1_ns),
"comparison");
461 static_assert(!(t1 < t1 ),
"comparison");
462 static_assert( (t1 < t2 ),
"comparison");
463 static_assert(!(t1 < t_ns ),
"comparison");
464 static_assert(!(t1 < t1_ns),
"comparison");
466 static_assert( (t1 <= t1 ),
"comparison");
467 static_assert( (t1 <= t2 ),
"comparison");
468 static_assert(!(t1 <= t_ns ),
"comparison");
469 static_assert( (t1 <= t1_ns),
"comparison");
471 static_assert(!(t1 > t1 ),
"comparison");
472 static_assert(!(t1 > t2 ),
"comparison");
473 static_assert( (t1 > t_ns ),
"comparison");
474 static_assert(!(t1 > t1_ns),
"comparison");
476 static_assert(t1 * 2.0 == 20.0_us,
"scaling");
477 static_assert(2.0 * t1 == 20.0_us,
"scaling");
478 static_assert(t1 / 2.0 == 5.0_us,
"scaling");
489 using namespace util::quantities::time_literals;
492 constexpr
auto expected = 3.0_ms;
493 static_assert(std::is_same<std::decay_t<decltype(expected)>,
milliseconds>());
495 auto q = util::quantities::makeQuantity<milliseconds>(
"3.0 ms");
496 static_assert(std::is_same<std::decay_t<decltype(
q)>,
milliseconds>());
499 BOOST_TEST(
q.value() == expected.value(),
tol);
501 q = util::quantities::makeQuantity<milliseconds>(
" 3.0ms ");
502 BOOST_TEST(
q.value() == expected.value(),
tol);
504 q = util::quantities::makeQuantity<milliseconds>(
"3ms");
505 BOOST_TEST(
q.value() == expected.value(),
tol);
507 q = util::quantities::makeQuantity<milliseconds>(
"3000 us");
508 BOOST_TEST(
q.value() == expected.value(),
tol);
510 q = util::quantities::makeQuantity<milliseconds>(
"0.03e+2 ms");
511 BOOST_TEST(
q.value() == expected.value(),
tol);
513 q = util::quantities::makeQuantity<milliseconds>(
"+3ms");
514 BOOST_TEST(
q.value() == expected.value(),
tol);
516 q = util::quantities::makeQuantity<milliseconds>(
"+3E-3s");
517 BOOST_TEST(
q.value() == expected.value(),
tol);
519 q = util::quantities::makeQuantity<milliseconds>(
"3",
true);
520 BOOST_TEST(
q.value() == expected.value(),
tol);
522 q = util::quantities::makeQuantity<milliseconds>(
"3.0",
true);
523 BOOST_TEST(
q.value() == expected.value(),
tol);
525 q = util::quantities::makeQuantity<milliseconds>(
"30e-1",
true);
526 BOOST_TEST(
q.value() == expected.value(),
tol);
528 BOOST_CHECK_THROW(util::quantities::makeQuantity<milliseconds>(
"3"),
531 BOOST_CHECK_THROW(util::quantities::makeQuantity<milliseconds>(
"3 kg"),
534 BOOST_CHECK_THROW(util::quantities::makeQuantity<milliseconds>(
"3 dumbs"),
537 BOOST_CHECK_THROW(util::quantities::makeQuantity<milliseconds>(
"three ms"),
540 BOOST_CHECK_THROW(util::quantities::makeQuantity<milliseconds>(
"3.zero ms"),
void test_quantities_increment()
constexpr bool is_quantity_v
Trait: true if Q is a Quantity specialization.
static constexpr bool isCompatibleValue()
Returns whether U is a value type compatible with value_t.
void test_quantities_addition()
void test_quantities_scale()
void test_quantities_conversions()
void test_quantities_multiply_scalar()
constexpr OQ convertInto() const
Convert this quantity into the specified one.
decltype(auto) constexpr to_string(T &&obj)
ADL-aware version of std::to_string.
A value measured in the specified unit.
constexpr bool has_unit_v
Trait: true if U is a ScaledUnit-based object.
constexpr bool has_quantity_v
Trait: true if Q is a Quantity-based object.
millisecond milliseconds
Alias for common language habits.
Numeric variable proxies with embedded unit of measurement.
String representing a quantity has no unit.
void test_quantities_sign()
Dimensioned variables representing space or time quantities.
Functions pulling in STL customization if available.
static constexpr bool sameBaseUnitAs()
Returns whether this quantity has the same base unit as OU.
String representing a quantity has an invalid number.
void test_quantities_comparisons()
static constexpr bool hasCompatibleValue()
void test_constexpr_operations()
void test_quantities_literals()