test_conversion.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. \file
  12. \brief test_conversion.cpp
  13. \details
  14. Test conversion between quantities.
  15. Output:
  16. @verbatim
  17. @endverbatim
  18. **/
  19. #include <boost/units/quantity.hpp>
  20. #include <boost/units/systems/si.hpp>
  21. #include <boost/units/systems/cgs.hpp>
  22. #include <iostream>
  23. #define BOOST_TEST_MAIN
  24. #include <boost/test/unit_test.hpp>
  25. #define BOOST_UNITS_CHECK_CLOSE(a, b) BOOST_CHECK_CLOSE_FRACTION(a, b, .0000001)
  26. namespace bu = boost::units;
  27. typedef bu::si::length si_length;
  28. typedef bu::si::time si_time;
  29. typedef bu::si::mass si_mass;
  30. typedef bu::si::area si_area;
  31. typedef bu::cgs::length cgs_length;
  32. typedef bu::cgs::time cgs_time;
  33. typedef bu::cgs::mass cgs_mass;
  34. typedef bu::cgs::area cgs_area;
  35. typedef bu::multiply_typeof_helper<si_length, cgs_length>::type mixed_length;
  36. typedef bu::multiply_typeof_helper<si_time, cgs_time>::type mixed_time;
  37. typedef bu::divide_typeof_helper<bu::multiply_typeof_helper<si_mass,cgs_area>::type, mixed_time>::type mixed_energy_1;
  38. typedef bu::divide_typeof_helper<bu::multiply_typeof_helper<cgs_mass,mixed_length>::type,
  39. bu::multiply_typeof_helper<cgs_time,cgs_time>::type >::type mixed_energy_2;
  40. BOOST_AUTO_TEST_CASE(test_conversion) {
  41. BOOST_CHECK_EQUAL(1, 1);
  42. BOOST_CONSTEXPR_OR_CONST bu::quantity<mixed_length> a1(2.0 * mixed_length());
  43. BOOST_CONSTEXPR_OR_CONST bu::quantity<si_area> a2(a1);
  44. BOOST_UNITS_CHECK_CLOSE(a2.value(), .02);
  45. BOOST_CONSTEXPR_OR_CONST bu::quantity<mixed_length> a3(a2);
  46. BOOST_UNITS_CHECK_CLOSE(a3.value(), 2.0);
  47. BOOST_CONSTEXPR_OR_CONST bu::quantity<mixed_energy_1> e1(2.0 * mixed_energy_1());
  48. BOOST_CONSTEXPR_OR_CONST bu::quantity<mixed_energy_2> e2(e1);
  49. BOOST_UNITS_CHECK_CLOSE(e2.value(), 20.0);
  50. BOOST_CONSTEXPR_OR_CONST bu::quantity<bu::si::energy> e3(e1);
  51. BOOST_UNITS_CHECK_CLOSE(e3.value(), .0002);
  52. BOOST_CONSTEXPR_OR_CONST bu::quantity<mixed_energy_2> e4(e3);
  53. BOOST_UNITS_CHECK_CLOSE(e4.value(), 20.0);
  54. BOOST_CONSTEXPR_OR_CONST bu::quantity<bu::cgs::force> F0 = 20 * bu::cgs::dyne;
  55. BOOST_UNITS_CHECK_CLOSE(F0.value(), 20.0);
  56. BOOST_CONSTEXPR_OR_CONST bu::quantity<bu::si::force> F3(F0);
  57. BOOST_UNITS_CHECK_CLOSE(F3.value(), 2.0e-4);
  58. BOOST_CONSTEXPR_OR_CONST bu::quantity<bu::si::force> F5(20 * bu::cgs::dyne);
  59. BOOST_UNITS_CHECK_CLOSE(F5.value(), 2.0e-4);
  60. // same type
  61. BOOST_CHECK_EQUAL(boost::units::conversion_factor(si_length(), si_length()), 1.0);
  62. }
  63. BOOST_AUTO_TEST_CASE(test_dimensionless_conversions) {
  64. typedef bu::divide_typeof_helper<bu::cgs::force, bu::si::force>::type mixed_dimensionless;
  65. BOOST_CONSTEXPR_OR_CONST bu::quantity<bu::si::dimensionless> dimensionless_test1(1.0*bu::cgs::dyne/bu::si::newton);
  66. BOOST_CHECK(dimensionless_test1 == 1e-5);
  67. typedef bu::multiply_typeof_helper<bu::si::length, bu::cgs::length>::type m_cm;
  68. typedef bu::divide_typeof_helper<m_cm, m_cm>::type heterogeneous_dimensionless;
  69. BOOST_CONSTEXPR_OR_CONST bu::quantity<heterogeneous_dimensionless> dimensionless_test2(1.0*bu::cgs::dyne/bu::si::newton);
  70. BOOST_CHECK(dimensionless_test2.value() == 1e-5);
  71. BOOST_CONSTEXPR_OR_CONST bu::quantity<bu::divide_typeof_helper<bu::cgs::force, bu::si::force>::type> dimensionless_test3(dimensionless_test2);
  72. BOOST_UNITS_CHECK_CLOSE(dimensionless_test3.value(), 1.0);
  73. BOOST_UNITS_CHECK_CLOSE(boost::units::conversion_factor(mixed_dimensionless(), heterogeneous_dimensionless()), 1e-5);
  74. BOOST_UNITS_CHECK_CLOSE(boost::units::conversion_factor(heterogeneous_dimensionless(), mixed_dimensionless()), 1e5);
  75. //m/cm -> g/kg
  76. 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);
  77. BOOST_CONSTEXPR_OR_CONST bu::quantity<bu::divide_typeof_helper<bu::cgs::mass, bu::si::mass>::type> dimensionless_test5(dimensionless_test4);
  78. BOOST_UNITS_CHECK_CLOSE(dimensionless_test5.value(), 2e5);
  79. }