test_scaled_unit.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. \file
  12. \brief test_scaled_conversion.cpp
  13. \details
  14. Test unit scaling
  15. Output:
  16. @verbatim
  17. @endverbatim
  18. **/
  19. #define BOOST_TEST_MAIN
  20. #include <boost/units/systems/si/prefixes.hpp>
  21. #include <boost/units/systems/si/time.hpp>
  22. #include <boost/units/quantity.hpp>
  23. #include <boost/units/io.hpp>
  24. #include <sstream>
  25. #include <boost/test/unit_test.hpp>
  26. #include <boost/test/floating_point_comparison.hpp>
  27. namespace bu = boost::units;
  28. namespace si = boost::units::si;
  29. BOOST_AUTO_TEST_CASE(test_scaled_to_plain) {
  30. BOOST_CONSTEXPR_OR_CONST bu::quantity<si::time> s1 = 12.5 * si::seconds;
  31. BOOST_CONSTEXPR_OR_CONST bu::quantity<si::time> s2(si::nano * s1);
  32. BOOST_CHECK_CLOSE_FRACTION(1e-9 * s1.value(), s2.value(), 0.000000001);
  33. }
  34. BOOST_AUTO_TEST_CASE(test_plain_to_scaled) {
  35. BOOST_CONSTEXPR_OR_CONST bu::quantity<si::time> s1 = 12.5 * si::seconds;
  36. typedef bu::multiply_typeof_helper<si::nano_type, si::time>::type time_unit;
  37. BOOST_CONSTEXPR_OR_CONST bu::quantity<time_unit> s2(s1);
  38. BOOST_CHECK_CLOSE_FRACTION(1e9 * s1.value(), s2.value(), 0.000000001);
  39. }
  40. BOOST_AUTO_TEST_CASE(test_scaled_to_scaled) {
  41. typedef bu::multiply_typeof_helper<si::mega_type, si::time>::type mega_time_unit;
  42. typedef bu::multiply_typeof_helper<si::micro_type, si::time>::type micro_time_unit;
  43. BOOST_CONSTEXPR_OR_CONST bu::quantity<mega_time_unit> s1(12.5 * si::seconds);
  44. BOOST_CONSTEXPR_OR_CONST bu::quantity<micro_time_unit> s2(s1);
  45. BOOST_CHECK_CLOSE_FRACTION(1e12 * s1.value(), s2.value(), 0.000000001);
  46. }
  47. BOOST_AUTO_TEST_CASE(test_conversion_factor) {
  48. BOOST_CHECK_CLOSE_FRACTION(conversion_factor(si::nano*si::seconds, si::seconds), 1e-9, 0.000000001);
  49. BOOST_CHECK_CLOSE_FRACTION(conversion_factor(si::seconds, si::nano*si::seconds), 1e9, 0.000000001);
  50. BOOST_CHECK_CLOSE_FRACTION(conversion_factor(si::mega*si::seconds, si::micro*si::seconds), 1e12, 0.000000001);
  51. }
  52. BOOST_AUTO_TEST_CASE(test_output) {
  53. std::stringstream stream;
  54. stream << si::nano * 12.5 * si::seconds;
  55. BOOST_CHECK_EQUAL(stream.str(), "12.5 ns");
  56. }
  57. BOOST_AUTO_TEST_CASE(test_output_name) {
  58. std::stringstream stream;
  59. stream << bu::name_format << si::nano * 12.5 * si::seconds;
  60. BOOST_CHECK_EQUAL(stream.str(), "12.5 nanosecond");
  61. }