1 // Boost.Units - A C++ library for zero-overhead dimensional analysis and
2 // unit/quantity manipulation and conversion
4 // Copyright (C) 2008 Matthias Christian Schabel
5 // Copyright (C) 2008 Steven Watanabe
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)
14 \brief test_custom_unit.cpp
17 Make sure that a minimal + - * / unit class is fully functional.
24 #include <boost/units/quantity.hpp>
26 #include <boost/test/minimal.hpp>
28 namespace bu = boost::units;
30 template<int Mass, int Length, int Time>
31 struct simple_unit {};
33 BOOST_TYPEOF_REGISTER_TEMPLATE(simple_unit, (int)(int)(int))
35 template<int Mass, int Length, int Time>
36 simple_unit<Mass, Length, Time> operator+(const simple_unit<Mass, Length, Time>&,
37 const simple_unit<Mass, Length, Time>&)
39 return simple_unit<Mass, Length, Time>();
42 template<int Mass, int Length, int Time>
43 simple_unit<Mass, Length, Time> operator-(const simple_unit<Mass, Length, Time>&,
44 const simple_unit<Mass, Length, Time>&)
46 return simple_unit<Mass, Length, Time>();
49 template<int Mass1, int Length1, int Time1, int Mass2, int Length2, int Time2>
50 simple_unit<Mass1 + Mass2, Length1 + Length2, Time1 + Time2>
51 operator*(const simple_unit<Mass1, Length1, Time1>&,
52 const simple_unit<Mass2, Length2, Time2>&)
54 return simple_unit<Mass1 + Mass2, Length1 + Length2, Time1 + Time2>();
57 template<int Mass1, int Length1, int Time1, int Mass2, int Length2, int Time2>
58 simple_unit<Mass1 - Mass2, Length1 - Length2, Time1 - Time2>
59 operator/(const simple_unit<Mass1, Length1, Time1>&,
60 const simple_unit<Mass2, Length2, Time2>&)
62 return simple_unit<Mass1 - Mass2, Length1 - Length2, Time1 - Time2>();
65 int test_main(int,char *[])
67 bu::quantity<simple_unit<1, 0, 0> > mass = bu::quantity<simple_unit<1, 0, 0> >::from_value(2);
68 bu::quantity<simple_unit<0, 1, 0> > length = bu::quantity<simple_unit<0, 1, 0> >::from_value(4);
70 bu::quantity<simple_unit<1, 1, 0> > ml = mass * length;
71 bu::quantity<simple_unit<1, -1, 0> > m_per_l = mass/length;
73 BOOST_CHECK(ml.value() == 8);
74 BOOST_CHECK(m_per_l.value() == 0.5);
78 BOOST_CHECK(mass.value() == 4);
81 BOOST_CHECK(length.value() == 0);