1 // Boost.Units - A C++ library for zero-overhead dimensional analysis and
2 // unit/quantity manipulation and conversion
3 //
4 // Copyright (C) 2003-2009 Matthias Christian Schabel
5 // Copyright (C) 2007-2009 Steven Watanabe
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See
8 // accompanying file LICENSE_1_0.txt or copy at
9 // http://www.boost.org/LICENSE_1_0.txt)
10
11 /**
12 \file
13
14 \brief test_constants.cpp
15
16 \details
17 Test all combinations of operators with the constants.
18
19 **/
20
21 #include <boost/units/systems/detail/constants.hpp>
22 #include <boost/units/quantity.hpp>
23 #include <boost/units/pow.hpp>
24 #include <boost/units/systems/si/length.hpp>
25 #include <boost/units/systems/si/time.hpp>
26
27 using boost::units::quantity;
28 using boost::units::si::length;
29 using boost::units::si::meters;
30 using boost::units::si::seconds;
31 using boost::units::static_rational;
32 using boost::units::pow;
33 using boost::units::root;
34
35 BOOST_UNITS_PHYSICAL_CONSTANT(length_constant, quantity<length>, 2.0 * meters, 0.5 * meters);
36
37 template<class T>
38 void check_same(const T&, const T&);
39
40 template<class T>
41 typename T::value_type unwrap(const boost::units::constant<T>&);
42
43 template<class T>
44 T unwrap(const T&);
45
46 #define BOOST_UNITS_CHECK_RESULT(arg1, op, arg2) check_same((arg1) op (arg2), unwrap(arg1) op unwrap(arg2));
47
test_add()48 void test_add() {
49 BOOST_UNITS_CHECK_RESULT(length_constant, +, length_constant);
50 BOOST_UNITS_CHECK_RESULT(length_constant, +, 1.0 * meters);
51 BOOST_UNITS_CHECK_RESULT(1.0* meters, +, length_constant);
52 }
53
test_subtract()54 void test_subtract() {
55 BOOST_UNITS_CHECK_RESULT(length_constant, -, length_constant);
56 BOOST_UNITS_CHECK_RESULT(length_constant, -, 1.0 * meters);
57 BOOST_UNITS_CHECK_RESULT(1.0* meters, -, length_constant);
58 }
59
test_multiply()60 void test_multiply() {
61 BOOST_UNITS_CHECK_RESULT(length_constant, *, length_constant);
62 BOOST_UNITS_CHECK_RESULT(length_constant, *, 1.0 * seconds);
63 BOOST_UNITS_CHECK_RESULT(1.0 * seconds, *, length_constant);
64 BOOST_UNITS_CHECK_RESULT(length_constant, *, 1.0);
65 BOOST_UNITS_CHECK_RESULT(1.0, *, length_constant);
66 BOOST_UNITS_CHECK_RESULT(length_constant, *, seconds);
67 BOOST_UNITS_CHECK_RESULT(seconds, *, length_constant);
68 }
69
test_divide()70 void test_divide() {
71 BOOST_UNITS_CHECK_RESULT(length_constant, /, length_constant);
72 BOOST_UNITS_CHECK_RESULT(length_constant, /, 1.0 * seconds);
73 BOOST_UNITS_CHECK_RESULT(1.0 * seconds, /, length_constant);
74 BOOST_UNITS_CHECK_RESULT(length_constant, /, 1.0);
75 BOOST_UNITS_CHECK_RESULT(1.0, /, length_constant);
76 BOOST_UNITS_CHECK_RESULT(length_constant, /, seconds);
77 BOOST_UNITS_CHECK_RESULT(seconds, /, length_constant);
78 }
79
test_pow()80 void test_pow() {
81 check_same(pow<2>(length_constant), pow<2>(unwrap(length_constant)));
82 check_same(root<2>(length_constant), root<2>(unwrap(length_constant)));
83 check_same(pow<5>(length_constant), pow<5>(unwrap(length_constant)));
84 check_same(root<5>(length_constant), root<5>(unwrap(length_constant)));
85 check_same(pow<static_rational<2, 3> >(length_constant), pow<static_rational<2, 3> >(unwrap(length_constant)));
86 check_same(root<static_rational<2, 3> >(length_constant), root<static_rational<2, 3> >(unwrap(length_constant)));
87 }
88