1 | // Boost.Units - A C++ library for zero-overhead dimensional analysis and |
2 | // unit/quantity manipulation and conversion |
3 | // |
4 | // Copyright (C) 2003-2008 Matthias Christian Schabel |
5 | // Copyright (C) 2008 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 | #include <boost/core/lightweight_test.hpp> |
12 | |
13 | #include <boost/units/base_dimension.hpp> |
14 | #include <boost/units/base_unit.hpp> |
15 | #include <boost/units/make_system.hpp> |
16 | #include <boost/units/scaled_base_unit.hpp> |
17 | #include <boost/units/conversion.hpp> |
18 | #include <boost/units/unit.hpp> |
19 | |
20 | struct dimension1_tag : boost::units::base_dimension<dimension1_tag, 1> {}; |
21 | struct dimension2_tag : boost::units::base_dimension<dimension2_tag, 2> {}; |
22 | |
23 | typedef dimension1_tag::dimension_type dimension1; |
24 | typedef dimension2_tag::dimension_type dimension2; |
25 | typedef boost::mpl::times<dimension1, dimension2>::type dimension12; |
26 | |
27 | struct unit1_tag : boost::units::base_unit<unit1_tag, dimension1, 1> {}; |
28 | struct unit2_tag : boost::units::base_unit<unit2_tag, dimension1, 2> {}; |
29 | struct unit3_tag : boost::units::base_unit<unit3_tag, dimension1, 3> {}; |
30 | struct unit4_tag : boost::units::base_unit<unit4_tag, dimension2, 4> {}; |
31 | struct unit5_tag : boost::units::base_unit<unit5_tag, dimension12, 5> {}; |
32 | struct unit6_tag : boost::units::base_unit<unit6_tag, dimension1, 6> {}; |
33 | struct unit7_tag : boost::units::base_unit<unit7_tag, dimension1, 7> {}; |
34 | |
35 | BOOST_UNITS_DEFINE_CONVERSION_FACTOR(unit1_tag, unit2_tag, double, 2.0); |
36 | BOOST_UNITS_DEFINE_CONVERSION_FACTOR(unit2_tag, unit3_tag, double, 3.0); |
37 | BOOST_UNITS_DEFINE_CONVERSION_FACTOR(unit6_tag, unit3_tag, double, 5.0); |
38 | BOOST_UNITS_DEFINE_CONVERSION_FACTOR(unit7_tag, unit1_tag, double, 7.0); |
39 | |
40 | typedef boost::units::multiply_typeof_helper<unit3_tag::unit_type, unit4_tag::unit_type>::type unit34_type; |
41 | BOOST_UNITS_DEFINE_CONVERSION_FACTOR(unit5_tag, unit34_type , double, 5.0); |
42 | |
43 | template<class BaseUnit> |
44 | struct make_unit { |
45 | typedef boost::units::unit< |
46 | typename BaseUnit::dimension_type, |
47 | typename boost::units::make_system<BaseUnit>::type> type; |
48 | }; |
49 | |
50 | BOOST_UNITS_DEFAULT_CONVERSION(unit1_tag, unit2_tag); |
51 | BOOST_UNITS_DEFAULT_CONVERSION(unit3_tag, unit2_tag); |
52 | BOOST_UNITS_DEFAULT_CONVERSION(unit5_tag, unit34_type); |
53 | BOOST_UNITS_DEFAULT_CONVERSION(unit6_tag, make_unit<unit3_tag>::type); |
54 | BOOST_UNITS_DEFAULT_CONVERSION(unit7_tag, make_unit<unit1_tag>::type); |
55 | |
56 | int main() { |
57 | BOOST_CONSTEXPR_OR_CONST double value1 = boost::units::conversion_factor(unit3_tag::unit_type(), unit1_tag::unit_type()); |
58 | BOOST_TEST(std::abs(value1 - 1.0/6.0) < .0000000001); |
59 | BOOST_CONSTEXPR_OR_CONST double value2 = boost::units::conversion_factor(unit5_tag::unit_type() / unit4_tag::unit_type(), unit1_tag::unit_type()); |
60 | BOOST_TEST(std::abs(value2 - 5.0/6.0) < .0000000001); |
61 | typedef boost::units::scaled_base_unit<unit5_tag, boost::units::scale<2, boost::units::static_rational<1> > > scaled_unit5_tag; |
62 | BOOST_CONSTEXPR_OR_CONST double value3 = boost::units::conversion_factor(scaled_unit5_tag::unit_type() / unit4_tag::unit_type(), unit1_tag::unit_type()); |
63 | BOOST_TEST(std::abs(value3 - 10.0/6.0) < .0000000001); |
64 | |
65 | // check homogeneous unit conversions |
66 | BOOST_CONSTEXPR_OR_CONST double value4 = boost::units::conversion_factor(make_unit<unit3_tag>::type(), make_unit<unit1_tag>::type()); |
67 | BOOST_TEST(std::abs(value4 - 1.0/6.0) < .0000000001); |
68 | BOOST_CONSTEXPR_OR_CONST double value5 = boost::units::conversion_factor(unit3_tag::unit_type(), make_unit<unit1_tag>::type()); |
69 | BOOST_TEST(std::abs(value5 - 1.0/6.0) < .0000000001); |
70 | BOOST_CONSTEXPR_OR_CONST double value6 = boost::units::conversion_factor(make_unit<unit3_tag>::type(), unit1_tag::unit_type()); |
71 | BOOST_TEST(std::abs(value6 - 1.0/6.0) < .0000000001); |
72 | |
73 | // check chained homogeneous conversions |
74 | BOOST_CONSTEXPR_OR_CONST double value7 = boost::units::conversion_factor(unit6_tag::unit_type(), unit7_tag::unit_type()); |
75 | BOOST_TEST(std::abs(value7 - 5.0/42.0) < .0000000001); |
76 | |
77 | return boost::report_errors(); |
78 | } |
79 | |