| 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/systems/si/prefixes.hpp> |
| 25 | #include <boost/units/systems/si/time.hpp> |
| 26 | #include <boost/units/quantity.hpp> |
| 27 | #include <boost/units/io.hpp> |
| 28 | |
| 29 | #include <sstream> |
| 30 | |
| 31 | #include "test_close.hpp" |
| 32 | |
| 33 | namespace bu = boost::units; |
| 34 | namespace si = boost::units::si; |
| 35 | |
| 36 | void test_scaled_to_plain() { |
| 37 | BOOST_CONSTEXPR_OR_CONST bu::quantity<si::time> s1 = 12.5 * si::seconds; |
| 38 | BOOST_CONSTEXPR_OR_CONST bu::quantity<si::time> s2(si::nano * s1); |
| 39 | BOOST_UNITS_TEST_CLOSE(1e-9 * s1.value(), s2.value(), 0.000000001); |
| 40 | } |
| 41 | |
| 42 | void test_plain_to_scaled() { |
| 43 | BOOST_CONSTEXPR_OR_CONST bu::quantity<si::time> s1 = 12.5 * si::seconds; |
| 44 | typedef bu::multiply_typeof_helper<si::nano_type, si::time>::type time_unit; |
| 45 | BOOST_CONSTEXPR_OR_CONST bu::quantity<time_unit> s2(s1); |
| 46 | BOOST_UNITS_TEST_CLOSE(1e9 * s1.value(), s2.value(), 0.000000001); |
| 47 | } |
| 48 | |
| 49 | void test_scaled_to_scaled() { |
| 50 | typedef bu::multiply_typeof_helper<si::mega_type, si::time>::type mega_time_unit; |
| 51 | typedef bu::multiply_typeof_helper<si::micro_type, si::time>::type micro_time_unit; |
| 52 | BOOST_CONSTEXPR_OR_CONST bu::quantity<mega_time_unit> s1(12.5 * si::seconds); |
| 53 | BOOST_CONSTEXPR_OR_CONST bu::quantity<micro_time_unit> s2(s1); |
| 54 | BOOST_UNITS_TEST_CLOSE(1e12 * s1.value(), s2.value(), 0.000000001); |
| 55 | } |
| 56 | |
| 57 | void test_conversion_factor() { |
| 58 | BOOST_UNITS_TEST_CLOSE(conversion_factor(si::nano*si::seconds, si::seconds), 1e-9, 0.000000001); |
| 59 | BOOST_UNITS_TEST_CLOSE(conversion_factor(si::seconds, si::nano*si::seconds), 1e9, 0.000000001); |
| 60 | BOOST_UNITS_TEST_CLOSE(conversion_factor(si::mega*si::seconds, si::micro*si::seconds), 1e12, 0.000000001); |
| 61 | } |
| 62 | |
| 63 | void test_output() { |
| 64 | std::stringstream stream; |
| 65 | stream << si::nano * 12.5 * si::seconds; |
| 66 | BOOST_TEST_EQ(stream.str(), "12.5 ns" ); |
| 67 | } |
| 68 | |
| 69 | void test_output_name() { |
| 70 | std::stringstream stream; |
| 71 | stream << bu::name_format << si::nano * 12.5 * si::seconds; |
| 72 | BOOST_TEST_EQ(stream.str(), "12.5 nanosecond" ); |
| 73 | } |
| 74 | |
| 75 | int main() |
| 76 | { |
| 77 | test_scaled_to_plain(); |
| 78 | test_plain_to_scaled(); |
| 79 | test_scaled_to_scaled(); |
| 80 | test_conversion_factor(); |
| 81 | test_output(); |
| 82 | test_output_name(); |
| 83 | return boost::report_errors(); |
| 84 | } |
| 85 | |