Apply patch for [CVE-2012-2677][boost] ordered_malloc() overflow
[external/boost.git] / libs / units / test / test_constants.cpp
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 /** 
12 \file
13     
14 \brief test_constants.cpp
15
16 \details
17 Test all combinations of operators with the constants.
18
19 **/
20
21 #include <boost/units/systems/detail/constants.hpp>
22 #include <boost/units/quantity.hpp>
23 #include <boost/units/systems/si/length.hpp>
24 #include <boost/units/systems/si/time.hpp>
25
26 using boost::units::quantity;
27 using boost::units::si::length;
28 using boost::units::si::meters;
29 using boost::units::si::seconds;
30
31 BOOST_UNITS_PHYSICAL_CONSTANT(length_constant, quantity<length>, 2.0 * meters, 0.5 * meters);
32
33 template<class T>
34 void check_same(const T&, const T&);
35
36 template<class T>
37 typename T::value_type unwrap(const boost::units::constant<T>&);
38
39 template<class T>
40 T unwrap(const T&);
41
42 #define BOOST_UNITS_CHECK_RESULT(arg1, op, arg2) check_same((arg1) op (arg2), unwrap(arg1) op unwrap(arg2));
43
44 void test_add() {
45     BOOST_UNITS_CHECK_RESULT(length_constant, +, length_constant);
46     BOOST_UNITS_CHECK_RESULT(length_constant, +, 1.0 * meters);
47     BOOST_UNITS_CHECK_RESULT(1.0* meters, +, length_constant);
48 }
49
50 void test_subtract() {
51     BOOST_UNITS_CHECK_RESULT(length_constant, -, length_constant);
52     BOOST_UNITS_CHECK_RESULT(length_constant, -, 1.0 * meters);
53     BOOST_UNITS_CHECK_RESULT(1.0* meters, -, length_constant);
54 }
55
56 void test_multiply() {
57     BOOST_UNITS_CHECK_RESULT(length_constant, *, length_constant);
58     BOOST_UNITS_CHECK_RESULT(length_constant, *, 1.0 * seconds);
59     BOOST_UNITS_CHECK_RESULT(1.0 * seconds, *, length_constant);
60     BOOST_UNITS_CHECK_RESULT(length_constant, *, 1.0);
61     BOOST_UNITS_CHECK_RESULT(1.0, *, length_constant);
62     BOOST_UNITS_CHECK_RESULT(length_constant, *, seconds);
63     BOOST_UNITS_CHECK_RESULT(seconds, *, length_constant);
64 }
65
66 void test_divide() {
67     BOOST_UNITS_CHECK_RESULT(length_constant, /, length_constant);
68     BOOST_UNITS_CHECK_RESULT(length_constant, /, 1.0 * seconds);
69     BOOST_UNITS_CHECK_RESULT(1.0 * seconds, /, length_constant);
70     BOOST_UNITS_CHECK_RESULT(length_constant, /, 1.0);
71     BOOST_UNITS_CHECK_RESULT(1.0, /, length_constant);
72     BOOST_UNITS_CHECK_RESULT(length_constant, /, seconds);
73     BOOST_UNITS_CHECK_RESULT(seconds, /, length_constant);
74 }