| 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) 2007-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_scaled_conversion.cpp |
| 15 | |
| 16 | \details |
| 17 | Test unit scaling |
| 18 | |
| 19 | Output: |
| 20 | @verbatim |
| 21 | @endverbatim |
| 22 | **/ |
| 23 | |
| 24 | #include <boost/units/quantity.hpp> |
| 25 | #include <boost/units/conversion.hpp> |
| 26 | #include <boost/units/unit.hpp> |
| 27 | #include <boost/units/base_unit.hpp> |
| 28 | #include <boost/units/scaled_base_unit.hpp> |
| 29 | #include <boost/units/scale.hpp> |
| 30 | #include <boost/units/base_dimension.hpp> |
| 31 | #include <boost/units/derived_dimension.hpp> |
| 32 | |
| 33 | #include <boost/core/lightweight_test.hpp> |
| 34 | |
| 35 | #define BOOST_UNITS_CHECK_CLOSE(a, b) BOOST_TEST(std::abs((a) - (b)) < .0000001) |
| 36 | |
| 37 | namespace bu = boost::units; |
| 38 | |
| 39 | struct base_dimension1 : bu::base_dimension<base_dimension1, 1> {}; |
| 40 | struct base_dimension2 : bu::base_dimension<base_dimension2, 2> {}; |
| 41 | struct base_dimension3 : bu::base_dimension<base_dimension3, 3> {}; |
| 42 | |
| 43 | typedef bu::derived_dimension<base_dimension1, 1, base_dimension2, 1, base_dimension3, 1>::type dimension4; |
| 44 | |
| 45 | struct base_unit1 : bu::base_unit<base_unit1, base_dimension1::dimension_type, 1> {}; |
| 46 | struct base_unit2 : bu::base_unit<base_unit2, base_dimension1::dimension_type, 2> {}; |
| 47 | struct base_unit3 : bu::base_unit<base_unit3, base_dimension1::dimension_type, 3> {}; |
| 48 | struct base_unit4 : bu::base_unit<base_unit4, dimension4, 4> {}; |
| 49 | struct base_unit5 : bu::base_unit<base_unit5, base_dimension3::dimension_type, 5> {}; |
| 50 | |
| 51 | typedef bu::scaled_base_unit<base_unit2, bu::scale<10, bu::static_rational<3> > > scaled_base_unit2; |
| 52 | typedef bu::scaled_base_unit<base_unit4, bu::scale<10, bu::static_rational<3> > > scaled_base_unit4; |
| 53 | |
| 54 | BOOST_UNITS_DEFINE_CONVERSION_FACTOR(base_unit1, scaled_base_unit2, double, 5); |
| 55 | BOOST_UNITS_DEFINE_CONVERSION_FACTOR(scaled_base_unit2, base_unit3, double, 3); |
| 56 | |
| 57 | int main() |
| 58 | { |
| 59 | BOOST_UNITS_CHECK_CLOSE(bu::conversion_factor(base_unit1::unit_type(), base_unit2::unit_type()), 5000); |
| 60 | BOOST_UNITS_CHECK_CLOSE(bu::conversion_factor(base_unit2::unit_type(), base_unit3::unit_type()), 0.003); |
| 61 | BOOST_UNITS_CHECK_CLOSE(bu::conversion_factor(scaled_base_unit2::unit_type(), base_unit2::unit_type()), 1000); |
| 62 | |
| 63 | BOOST_UNITS_CHECK_CLOSE(bu::conversion_factor(base_unit4::unit_type()/base_unit5::unit_type(), scaled_base_unit4::unit_type()/base_unit5::unit_type()), 1e-3); |
| 64 | |
| 65 | return boost::report_errors(); |
| 66 | } |
| 67 | |