test_custom_unit.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Boost.Units - A C++ library for zero-overhead dimensional analysis and
  2. // unit/quantity manipulation and conversion
  3. //
  4. // Copyright (C) 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_custom_unit.cpp
  13. \details
  14. Make sure that a minimal + - * / unit class is fully functional.
  15. Output:
  16. @verbatim
  17. @endverbatim
  18. **/
  19. #include <boost/units/quantity.hpp>
  20. #include <boost/test/minimal.hpp>
  21. namespace bu = boost::units;
  22. template<int Mass, int Length, int Time>
  23. struct simple_unit {};
  24. BOOST_TYPEOF_REGISTER_TEMPLATE(simple_unit, (int)(int)(int))
  25. template<int Mass, int Length, int Time>
  26. simple_unit<Mass, Length, Time> operator+(const simple_unit<Mass, Length, Time>&,
  27. const simple_unit<Mass, Length, Time>&)
  28. {
  29. return simple_unit<Mass, Length, Time>();
  30. }
  31. template<int Mass, int Length, int Time>
  32. simple_unit<Mass, Length, Time> operator-(const simple_unit<Mass, Length, Time>&,
  33. const simple_unit<Mass, Length, Time>&)
  34. {
  35. return simple_unit<Mass, Length, Time>();
  36. }
  37. template<int Mass1, int Length1, int Time1, int Mass2, int Length2, int Time2>
  38. simple_unit<Mass1 + Mass2, Length1 + Length2, Time1 + Time2>
  39. operator*(const simple_unit<Mass1, Length1, Time1>&,
  40. const simple_unit<Mass2, Length2, Time2>&)
  41. {
  42. return simple_unit<Mass1 + Mass2, Length1 + Length2, Time1 + Time2>();
  43. }
  44. template<int Mass1, int Length1, int Time1, int Mass2, int Length2, int Time2>
  45. simple_unit<Mass1 - Mass2, Length1 - Length2, Time1 - Time2>
  46. operator/(const simple_unit<Mass1, Length1, Time1>&,
  47. const simple_unit<Mass2, Length2, Time2>&)
  48. {
  49. return simple_unit<Mass1 - Mass2, Length1 - Length2, Time1 - Time2>();
  50. }
  51. int test_main(int,char *[])
  52. {
  53. bu::quantity<simple_unit<1, 0, 0> > mass = bu::quantity<simple_unit<1, 0, 0> >::from_value(2);
  54. bu::quantity<simple_unit<0, 1, 0> > length = bu::quantity<simple_unit<0, 1, 0> >::from_value(4);
  55. bu::quantity<simple_unit<1, 1, 0> > ml = mass * length;
  56. bu::quantity<simple_unit<1, -1, 0> > m_per_l = mass/length;
  57. BOOST_CHECK(ml.value() == 8);
  58. BOOST_CHECK(m_per_l.value() == 0.5);
  59. mass += mass;
  60. BOOST_CHECK(mass.value() == 4);
  61. length -= length;
  62. BOOST_CHECK(length.value() == 0);
  63. return 0;
  64. }