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 /**
12 \file
13 
14 \brief test_units_1.cpp
15 
16 \details
17 Test unit class.
18 
19 Output:
20 @verbatim
21 @endverbatim
22 **/
23 
24 #include "test_header.hpp"
25 
26 #include <boost/units/pow.hpp>
27 
28 namespace bu = boost::units;
29 
30 static const double E_ = 2.718281828459045235360287471352662497757;
31 
test_main(int,char * [])32 int test_main(int,char *[])
33 {
34     // default constructor
35     const bu::quantity<bu::energy>          E1;
36     BOOST_CHECK(E1.value() == double());
37 
38     // value_type constructor
39     const bu::quantity<bu::energy>          E2(E_*bu::joules);
40     BOOST_CHECK(E2.value() == E_);
41 
42     // copy constructor
43     const bu::quantity<bu::energy>          E3(E2);
44     BOOST_CHECK(E3.value() == E_);
45 
46     // operator=
47     const bu::quantity<bu::energy>          E4 = E2;
48     BOOST_CHECK(E4.value() == E_);
49 
50     // implicit copy constructor value_type conversion
51     const bu::quantity<bu::energy,float>    E5(E2);
52     BOOST_UNITS_CHECK_CLOSE(E5.value(),float(E_));
53 
54     // implicit operator= value_type conversion
55     //const bu::quantity<bu::energy,float>    E7 = E2;
56     //BOOST_UNITS_CHECK_CLOSE(E7.value(),float(E_));
57 
58     //const bu::quantity<bu::energy,long>     E8 = E2;
59     //BOOST_CHECK(E8.value() == long(E_));
60 
61     // const construction
62     bu::quantity<bu::energy>                E9(E2);
63     BOOST_CHECK(E9.value() == E_);
64 
65     // value assignment
66     bu::quantity_cast<double&>(E9) = 1.5;
67     BOOST_CHECK(E9.value() == 1.5);
68 
69     // value assignment with implicit value_type conversion
70     bu::quantity_cast<double&>(E9) = 2;
71     BOOST_CHECK(E9.value() == double(2));
72 
73     // operator+=(this_type)
74     E9 = 2.0*bu::joules;
75     E9 += E9;
76     BOOST_CHECK(E9.value() == 4.0);
77 
78     // operator-=(this_type)
79     E9 = 2.0*bu::joules;
80     E9 -= E9;
81     BOOST_CHECK(E9.value() == 0.0);
82 
83     // operator*=(value_type)
84     E9 = 2.0*bu::joules;
85     E9 *= 2.0;
86     BOOST_CHECK(E9.value() == 4.0);
87 
88     // operator/=(value_type)
89     E9 = 2.0*bu::joules;
90     E9 /= 2.0;
91     BOOST_CHECK(E9.value() == 1.0);
92 
93     // static construct quantity from value_type
94     const bu::quantity<bu::energy>      E(bu::quantity<bu::energy>::from_value(2.5));
95     BOOST_CHECK(E.value() == 2.5);
96 
97     // quantity_cast
98 
99     // unit * scalar
100     BOOST_CHECK(bu::joules*2.0 == bu::quantity<bu::energy>::from_value(2.0));
101 
102     // unit / scalar
103     BOOST_CHECK(bu::joules/2.0 == bu::quantity<bu::energy>::from_value(0.5));
104 
105     // scalar * unit
106     BOOST_CHECK(2.0*bu::joules == bu::quantity<bu::energy>::from_value(2.0));
107 
108     // scalar / unit
109     BOOST_CHECK(2.0/bu::joules == bu::quantity<bu::inverse_energy>::from_value(2.0));
110 
111     //  quantity * scalar
112     BOOST_CHECK(E*2.0 == bu::quantity<bu::energy>::from_value(5.0));
113 
114     //  quantity / scalar
115     BOOST_CHECK(E/2.0 == bu::quantity<bu::energy>::from_value(1.25));
116 
117     // scalar * quantity
118     BOOST_CHECK(2.0*E == bu::quantity<bu::energy>::from_value(5.0));
119 
120     // scalar / quantity
121     BOOST_CHECK(2.0/E == bu::quantity<bu::inverse_energy>::from_value(0.8));
122 
123     const bu::quantity<bu::length>      L(1.0*bu::meters);
124     const bu::quantity<bu::mass>        M(2.0*bu::kilograms);
125     const bu::quantity<bu::time>        T(3.0*bu::seconds);
126     const bu::quantity<bu::velocity>    V(bu::quantity<bu::velocity>::from_value(4.0));
127 
128     // unit * quantity
129     BOOST_CHECK(bu::seconds*V == 4.0*bu::meters);
130 
131     // unit / quantity
132     BOOST_CHECK(bu::meters/V == 0.25*bu::seconds);
133 
134     // quantity * unit
135     BOOST_CHECK(V*bu::seconds == 4.0*bu::meters);
136 
137     // quantity / unit
138     BOOST_CHECK(V/bu::meters == 4.0/bu::seconds);
139 
140     // +quantity
141     BOOST_CHECK(+V == 4.0*bu::meters_per_second);
142 
143     // -quantity
144     BOOST_CHECK(-V == -4.0*bu::meters_per_second);
145 
146     // quantity + quantity
147     BOOST_CHECK(V+V == 8.0*bu::meters_per_second);
148 
149     // quantity - quantity
150     BOOST_CHECK(V-V == 0.0*bu::meters_per_second);
151 
152     // quantity * quantity
153     BOOST_CHECK(V*T == 12.0*bu::meters);
154 
155     // quantity / quantity
156     BOOST_CHECK(L/V == 0.25*bu::seconds);
157 
158     const bu::quantity<bu::area>    A(2.0*bu::square_meters);
159     const bu::quantity<bu::volume>  VL(1.0*bu::cubic_meters);
160 
161     // integer power of quantity
162     BOOST_CHECK(2.0*bu::pow<2>(L) == A);
163 
164     // rational power of quantity
165     BOOST_CHECK((bu::pow< bu::static_rational<2,3> >(VL) == 0.5*A));
166 
167     // integer root of quantity
168     BOOST_CHECK(bu::root<2>(A) == std::sqrt(2.0)*L);
169 
170     // rational root of quantity
171     BOOST_CHECK((bu::root< bu::static_rational<3,2> >(VL) == 0.5*A));
172 
173     const bu::quantity<bu::area>    A1(0.0*bu::square_meters),
174                                     A2(0.0*bu::square_meters),
175                                     A3(1.0*bu::square_meters),
176                                     A4(-1.0*bu::square_meters);
177 
178     // operator==
179     BOOST_CHECK((A1 == A2) == true);
180     BOOST_CHECK((A1 == A3) == false);
181 
182     // operator!=
183     BOOST_CHECK((A1 != A2) == false);
184     BOOST_CHECK((A1 != A3) == true);
185 
186     // operator<
187     BOOST_CHECK((A1 < A2) == false);
188     BOOST_CHECK((A1 < A3) == true);
189 
190     // operator<=
191     BOOST_CHECK((A1 <= A2) == true);
192     BOOST_CHECK((A1 <= A3) == true);
193 
194     // operator>
195     BOOST_CHECK((A1 > A2) == false);
196     BOOST_CHECK((A1 > A4) == true);
197 
198     // operator>=
199     BOOST_CHECK((A1 >= A2) == true);
200     BOOST_CHECK((A1 >= A4) == true);
201 
202     return 0;
203 }
204