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 /**
12 \file
13 
14 \brief test_custom_unit.cpp
15 
16 \details
17 Make sure that a minimal + - * / unit class is fully functional.
18 
19 Output:
20 @verbatim
21 @endverbatim
22 **/
23 
24 #include <boost/units/quantity.hpp>
25 
26 #include <boost/test/minimal.hpp>
27 
28 namespace bu = boost::units;
29 
30 template<int Mass, int Length, int Time>
31 struct simple_unit {};
32 
33 BOOST_TYPEOF_REGISTER_TEMPLATE(simple_unit, (int)(int)(int))
34 
35 template<int Mass, int Length, int Time>
operator +(const simple_unit<Mass,Length,Time> &,const simple_unit<Mass,Length,Time> &)36 simple_unit<Mass, Length, Time> operator+(const simple_unit<Mass, Length, Time>&,
37                                           const simple_unit<Mass, Length, Time>&)
38 {
39     return simple_unit<Mass, Length, Time>();
40 }
41 
42 template<int Mass, int Length, int Time>
operator -(const simple_unit<Mass,Length,Time> &,const simple_unit<Mass,Length,Time> &)43 simple_unit<Mass, Length, Time> operator-(const simple_unit<Mass, Length, Time>&,
44                                           const simple_unit<Mass, Length, Time>&)
45 {
46     return simple_unit<Mass, Length, Time>();
47 }
48 
49 template<int Mass1, int Length1, int Time1, int Mass2, int Length2, int Time2>
50 simple_unit<Mass1 + Mass2, Length1 + Length2, Time1 + Time2>
operator *(const simple_unit<Mass1,Length1,Time1> &,const simple_unit<Mass2,Length2,Time2> &)51 operator*(const simple_unit<Mass1, Length1, Time1>&,
52           const simple_unit<Mass2, Length2, Time2>&)
53 {
54     return simple_unit<Mass1 + Mass2, Length1 + Length2, Time1 + Time2>();
55 }
56 
57 template<int Mass1, int Length1, int Time1, int Mass2, int Length2, int Time2>
58 simple_unit<Mass1 - Mass2, Length1 - Length2, Time1 - Time2>
operator /(const simple_unit<Mass1,Length1,Time1> &,const simple_unit<Mass2,Length2,Time2> &)59 operator/(const simple_unit<Mass1, Length1, Time1>&,
60           const simple_unit<Mass2, Length2, Time2>&)
61 {
62     return simple_unit<Mass1 - Mass2, Length1 - Length2, Time1 - Time2>();
63 }
64 
test_main(int,char * [])65 int test_main(int,char *[])
66 {
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);
69 
70     bu::quantity<simple_unit<1, 1, 0> > ml = mass * length;
71     bu::quantity<simple_unit<1, -1, 0> > m_per_l = mass/length;
72 
73     BOOST_CHECK(ml.value() == 8);
74     BOOST_CHECK(m_per_l.value() == 0.5);
75 
76     mass += mass;
77 
78     BOOST_CHECK(mass.value() == 4);
79 
80     length -= length;
81     BOOST_CHECK(length.value() == 0);
82 
83     return 0;
84 }
85