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 | /** |
12 | \file |
13 | |
14 | \brief test_conversion.cpp |
15 | |
16 | \details |
17 | Test conversion between quantities. |
18 | |
19 | Output: |
20 | @verbatim |
21 | @endverbatim |
22 | **/ |
23 | |
24 | #include <boost/units/quantity.hpp> |
25 | #include <boost/units/systems/si.hpp> |
26 | #include <boost/units/systems/cgs.hpp> |
27 | |
28 | #include <iostream> |
29 | |
30 | #include "test_close.hpp" |
31 | |
32 | #define BOOST_UNITS_CHECK_CLOSE(a, b) BOOST_UNITS_TEST_CLOSE((a), (b), .0000001) |
33 | |
34 | namespace bu = boost::units; |
35 | |
36 | typedef bu::si::length si_length; |
37 | typedef bu::si::time si_time; |
38 | typedef bu::si::mass si_mass; |
39 | typedef bu::si::area si_area; |
40 | |
41 | typedef bu::cgs::length cgs_length; |
42 | typedef bu::cgs::time cgs_time; |
43 | typedef bu::cgs::mass cgs_mass; |
44 | typedef bu::cgs::area cgs_area; |
45 | |
46 | typedef bu::multiply_typeof_helper<si_length, cgs_length>::type mixed_length; |
47 | typedef bu::multiply_typeof_helper<si_time, cgs_time>::type mixed_time; |
48 | |
49 | typedef bu::divide_typeof_helper<bu::multiply_typeof_helper<si_mass,cgs_area>::type, mixed_time>::type mixed_energy_1; |
50 | typedef bu::divide_typeof_helper<bu::multiply_typeof_helper<cgs_mass,mixed_length>::type, |
51 | bu::multiply_typeof_helper<cgs_time,cgs_time>::type >::type mixed_energy_2; |
52 | |
53 | void test_conversion() { |
54 | BOOST_TEST_EQ(1, 1); |
55 | BOOST_CONSTEXPR_OR_CONST bu::quantity<mixed_length> a1(2.0 * mixed_length()); |
56 | BOOST_CONSTEXPR_OR_CONST bu::quantity<si_area> a2(a1); |
57 | |
58 | BOOST_UNITS_CHECK_CLOSE(a2.value(), .02); |
59 | |
60 | BOOST_CONSTEXPR_OR_CONST bu::quantity<mixed_length> a3(a2); |
61 | |
62 | BOOST_UNITS_CHECK_CLOSE(a3.value(), 2.0); |
63 | |
64 | BOOST_CONSTEXPR_OR_CONST bu::quantity<mixed_energy_1> e1(2.0 * mixed_energy_1()); |
65 | BOOST_CONSTEXPR_OR_CONST bu::quantity<mixed_energy_2> e2(e1); |
66 | |
67 | BOOST_UNITS_CHECK_CLOSE(e2.value(), 20.0); |
68 | |
69 | BOOST_CONSTEXPR_OR_CONST bu::quantity<bu::si::energy> e3(e1); |
70 | BOOST_UNITS_CHECK_CLOSE(e3.value(), .0002); |
71 | BOOST_CONSTEXPR_OR_CONST bu::quantity<mixed_energy_2> e4(e3); |
72 | BOOST_UNITS_CHECK_CLOSE(e4.value(), 20.0); |
73 | |
74 | BOOST_CONSTEXPR_OR_CONST bu::quantity<bu::cgs::force> F0 = 20 * bu::cgs::dyne; |
75 | BOOST_UNITS_CHECK_CLOSE(F0.value(), 20.0); |
76 | |
77 | BOOST_CONSTEXPR_OR_CONST bu::quantity<bu::si::force> F3(F0); |
78 | BOOST_UNITS_CHECK_CLOSE(F3.value(), 2.0e-4); |
79 | |
80 | BOOST_CONSTEXPR_OR_CONST bu::quantity<bu::si::force> F5(20 * bu::cgs::dyne); |
81 | BOOST_UNITS_CHECK_CLOSE(F5.value(), 2.0e-4); |
82 | |
83 | // same type |
84 | BOOST_TEST_EQ(boost::units::conversion_factor(si_length(), si_length()), 1.0); |
85 | } |
86 | |
87 | void test_dimensionless_conversions() { |
88 | typedef bu::divide_typeof_helper<bu::cgs::force, bu::si::force>::type mixed_dimensionless; |
89 | |
90 | BOOST_CONSTEXPR_OR_CONST bu::quantity<bu::si::dimensionless> dimensionless_test1(1.0*bu::cgs::dyne/bu::si::newton); |
91 | BOOST_TEST(dimensionless_test1 == 1e-5); |
92 | |
93 | typedef bu::multiply_typeof_helper<bu::si::length, bu::cgs::length>::type m_cm; |
94 | typedef bu::divide_typeof_helper<m_cm, m_cm>::type heterogeneous_dimensionless; |
95 | BOOST_CONSTEXPR_OR_CONST bu::quantity<heterogeneous_dimensionless> dimensionless_test2(1.0*bu::cgs::dyne/bu::si::newton); |
96 | BOOST_TEST(dimensionless_test2.value() == 1e-5); |
97 | BOOST_CONSTEXPR_OR_CONST bu::quantity<bu::divide_typeof_helper<bu::cgs::force, bu::si::force>::type> dimensionless_test3(dimensionless_test2); |
98 | BOOST_UNITS_CHECK_CLOSE(dimensionless_test3.value(), 1.0); |
99 | |
100 | BOOST_UNITS_CHECK_CLOSE(boost::units::conversion_factor(mixed_dimensionless(), heterogeneous_dimensionless()), 1e-5); |
101 | BOOST_UNITS_CHECK_CLOSE(boost::units::conversion_factor(heterogeneous_dimensionless(), mixed_dimensionless()), 1e5); |
102 | |
103 | |
104 | //m/cm -> g/kg |
105 | BOOST_CONSTEXPR_OR_CONST bu::quantity<bu::divide_typeof_helper<bu::si::length, bu::cgs::length>::type> dimensionless_test4(2.0 * bu::si::meters / bu::cgs::centimeters); |
106 | BOOST_CONSTEXPR_OR_CONST bu::quantity<bu::divide_typeof_helper<bu::cgs::mass, bu::si::mass>::type> dimensionless_test5(dimensionless_test4); |
107 | BOOST_UNITS_CHECK_CLOSE(dimensionless_test5.value(), 2e5); |
108 | } |
109 | |
110 | int main() |
111 | { |
112 | test_conversion(); |
113 | test_dimensionless_conversions(); |
114 | return boost::report_errors(); |
115 | } |
116 | |