test_constants.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Boost.Units - A C++ library for zero-overhead dimensional analysis and
  2. // unit/quantity manipulation and conversion
  3. //
  4. // Copyright (C) 2003-2009 Matthias Christian Schabel
  5. // Copyright (C) 2007-2009 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_constants.cpp
  13. \details
  14. Test all combinations of operators with the constants.
  15. **/
  16. #include <boost/units/systems/detail/constants.hpp>
  17. #include <boost/units/quantity.hpp>
  18. #include <boost/units/pow.hpp>
  19. #include <boost/units/systems/si/length.hpp>
  20. #include <boost/units/systems/si/time.hpp>
  21. using boost::units::quantity;
  22. using boost::units::si::length;
  23. using boost::units::si::meters;
  24. using boost::units::si::seconds;
  25. using boost::units::static_rational;
  26. using boost::units::pow;
  27. using boost::units::root;
  28. BOOST_UNITS_PHYSICAL_CONSTANT(length_constant, quantity<length>, 2.0 * meters, 0.5 * meters);
  29. template<class T>
  30. void check_same(const T&, const T&);
  31. template<class T>
  32. typename T::value_type unwrap(const boost::units::constant<T>&);
  33. template<class T>
  34. T unwrap(const T&);
  35. #define BOOST_UNITS_CHECK_RESULT(arg1, op, arg2) check_same((arg1) op (arg2), unwrap(arg1) op unwrap(arg2));
  36. void test_add() {
  37. BOOST_UNITS_CHECK_RESULT(length_constant, +, length_constant);
  38. BOOST_UNITS_CHECK_RESULT(length_constant, +, 1.0 * meters);
  39. BOOST_UNITS_CHECK_RESULT(1.0* meters, +, length_constant);
  40. }
  41. void test_subtract() {
  42. BOOST_UNITS_CHECK_RESULT(length_constant, -, length_constant);
  43. BOOST_UNITS_CHECK_RESULT(length_constant, -, 1.0 * meters);
  44. BOOST_UNITS_CHECK_RESULT(1.0* meters, -, length_constant);
  45. }
  46. void test_multiply() {
  47. BOOST_UNITS_CHECK_RESULT(length_constant, *, length_constant);
  48. BOOST_UNITS_CHECK_RESULT(length_constant, *, 1.0 * seconds);
  49. BOOST_UNITS_CHECK_RESULT(1.0 * seconds, *, length_constant);
  50. BOOST_UNITS_CHECK_RESULT(length_constant, *, 1.0);
  51. BOOST_UNITS_CHECK_RESULT(1.0, *, length_constant);
  52. BOOST_UNITS_CHECK_RESULT(length_constant, *, seconds);
  53. BOOST_UNITS_CHECK_RESULT(seconds, *, length_constant);
  54. }
  55. void test_divide() {
  56. BOOST_UNITS_CHECK_RESULT(length_constant, /, length_constant);
  57. BOOST_UNITS_CHECK_RESULT(length_constant, /, 1.0 * seconds);
  58. BOOST_UNITS_CHECK_RESULT(1.0 * seconds, /, length_constant);
  59. BOOST_UNITS_CHECK_RESULT(length_constant, /, 1.0);
  60. BOOST_UNITS_CHECK_RESULT(1.0, /, length_constant);
  61. BOOST_UNITS_CHECK_RESULT(length_constant, /, seconds);
  62. BOOST_UNITS_CHECK_RESULT(seconds, /, length_constant);
  63. }
  64. void test_pow() {
  65. check_same(pow<2>(length_constant), pow<2>(unwrap(length_constant)));
  66. check_same(root<2>(length_constant), root<2>(unwrap(length_constant)));
  67. check_same(pow<5>(length_constant), pow<5>(unwrap(length_constant)));
  68. check_same(root<5>(length_constant), root<5>(unwrap(length_constant)));
  69. check_same(pow<static_rational<2, 3> >(length_constant), pow<static_rational<2, 3> >(unwrap(length_constant)));
  70. check_same(root<static_rational<2, 3> >(length_constant), root<static_rational<2, 3> >(unwrap(length_constant)));
  71. }