]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 | #define BOOST_TEST_MAIN | |
25 | ||
26 | #include <boost/units/systems/si/prefixes.hpp> | |
27 | #include <boost/units/systems/si/time.hpp> | |
28 | #include <boost/units/quantity.hpp> | |
29 | #include <boost/units/io.hpp> | |
30 | ||
31 | #include <sstream> | |
32 | ||
33 | #include <boost/test/unit_test.hpp> | |
34 | #include <boost/test/floating_point_comparison.hpp> | |
35 | ||
36 | namespace bu = boost::units; | |
37 | namespace si = boost::units::si; | |
38 | ||
39 | BOOST_AUTO_TEST_CASE(test_scaled_to_plain) { | |
11fdf7f2 TL |
40 | BOOST_CONSTEXPR_OR_CONST bu::quantity<si::time> s1 = 12.5 * si::seconds; |
41 | BOOST_CONSTEXPR_OR_CONST bu::quantity<si::time> s2(si::nano * s1); | |
7c673cae FG |
42 | BOOST_CHECK_CLOSE_FRACTION(1e-9 * s1.value(), s2.value(), 0.000000001); |
43 | } | |
44 | ||
45 | BOOST_AUTO_TEST_CASE(test_plain_to_scaled) { | |
11fdf7f2 | 46 | BOOST_CONSTEXPR_OR_CONST bu::quantity<si::time> s1 = 12.5 * si::seconds; |
7c673cae | 47 | typedef bu::multiply_typeof_helper<si::nano_type, si::time>::type time_unit; |
11fdf7f2 | 48 | BOOST_CONSTEXPR_OR_CONST bu::quantity<time_unit> s2(s1); |
7c673cae FG |
49 | BOOST_CHECK_CLOSE_FRACTION(1e9 * s1.value(), s2.value(), 0.000000001); |
50 | } | |
51 | ||
52 | BOOST_AUTO_TEST_CASE(test_scaled_to_scaled) { | |
53 | typedef bu::multiply_typeof_helper<si::mega_type, si::time>::type mega_time_unit; | |
54 | typedef bu::multiply_typeof_helper<si::micro_type, si::time>::type micro_time_unit; | |
11fdf7f2 TL |
55 | BOOST_CONSTEXPR_OR_CONST bu::quantity<mega_time_unit> s1(12.5 * si::seconds); |
56 | BOOST_CONSTEXPR_OR_CONST bu::quantity<micro_time_unit> s2(s1); | |
7c673cae FG |
57 | BOOST_CHECK_CLOSE_FRACTION(1e12 * s1.value(), s2.value(), 0.000000001); |
58 | } | |
59 | ||
60 | BOOST_AUTO_TEST_CASE(test_conversion_factor) { | |
61 | BOOST_CHECK_CLOSE_FRACTION(conversion_factor(si::nano*si::seconds, si::seconds), 1e-9, 0.000000001); | |
62 | BOOST_CHECK_CLOSE_FRACTION(conversion_factor(si::seconds, si::nano*si::seconds), 1e9, 0.000000001); | |
63 | BOOST_CHECK_CLOSE_FRACTION(conversion_factor(si::mega*si::seconds, si::micro*si::seconds), 1e12, 0.000000001); | |
64 | } | |
65 | ||
66 | BOOST_AUTO_TEST_CASE(test_output) { | |
67 | std::stringstream stream; | |
68 | stream << si::nano * 12.5 * si::seconds; | |
69 | BOOST_CHECK_EQUAL(stream.str(), "12.5 ns"); | |
70 | } | |
71 | ||
72 | BOOST_AUTO_TEST_CASE(test_output_name) { | |
73 | std::stringstream stream; | |
74 | stream << bu::name_format << si::nano * 12.5 * si::seconds; | |
75 | BOOST_CHECK_EQUAL(stream.str(), "12.5 nanosecond"); | |
76 | } |