| 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 <limits>
|
|---|
| 25 |
|
|---|
| 26 | #include <boost/units/cmath.hpp>
|
|---|
| 27 |
|
|---|
| 28 | #include "test_header.hpp"
|
|---|
| 29 |
|
|---|
| 30 | namespace bu = boost::units;
|
|---|
| 31 |
|
|---|
| 32 | static volatile double zero = 0;
|
|---|
| 33 |
|
|---|
| 34 | int test_main(int,char *[])
|
|---|
| 35 | {
|
|---|
| 36 | double inf = std::numeric_limits<double>::infinity(),
|
|---|
| 37 | nan = 0.0/zero;
|
|---|
| 38 |
|
|---|
| 39 | // default constructor
|
|---|
| 40 | const bu::quantity<bu::energy> E1(0.0*bu::joules),
|
|---|
| 41 | E2(inf*bu::joules),
|
|---|
| 42 | E3(nan*bu::joules);
|
|---|
| 43 |
|
|---|
| 44 | BOOST_CHECK((bu::isfinite)(E1) == true);
|
|---|
| 45 | BOOST_CHECK((bu::isfinite)(E2) == false);
|
|---|
| 46 | BOOST_CHECK((bu::isfinite)(E3) == false);
|
|---|
| 47 |
|
|---|
| 48 | BOOST_CHECK((bu::isinf)(E1) == false);
|
|---|
| 49 | BOOST_CHECK((bu::isinf)(E2) == true);
|
|---|
| 50 | BOOST_CHECK((bu::isinf)(E3) == false);
|
|---|
| 51 |
|
|---|
| 52 | BOOST_CHECK((bu::isnan)(E1) == false);
|
|---|
| 53 | BOOST_CHECK((bu::isnan)(E2) == false);
|
|---|
| 54 | BOOST_CHECK((bu::isnan)(E3) == true);
|
|---|
| 55 |
|
|---|
| 56 | BOOST_CHECK((bu::isnormal)(E1) == false);
|
|---|
| 57 | BOOST_CHECK((bu::isnormal)(E2) == false);
|
|---|
| 58 | BOOST_CHECK((bu::isnormal)(E3) == false);
|
|---|
| 59 |
|
|---|
| 60 | const bu::quantity<bu::energy> E4(-2.5*bu::joules),
|
|---|
| 61 | E5(2.5*bu::joules);
|
|---|
| 62 |
|
|---|
| 63 | BOOST_CHECK((bu::isgreater)(E4,E5) == false);
|
|---|
| 64 | BOOST_CHECK((bu::isgreater)(E5,E4) == true);
|
|---|
| 65 | BOOST_CHECK((bu::isgreater)(E4,E4) == false);
|
|---|
| 66 | BOOST_CHECK((bu::isgreater)(E3,E4) == false);
|
|---|
| 67 | BOOST_CHECK((bu::isgreater)(E4,E3) == false);
|
|---|
| 68 |
|
|---|
| 69 | BOOST_CHECK((bu::isgreaterequal)(E4,E5) == false);
|
|---|
| 70 | BOOST_CHECK((bu::isgreaterequal)(E5,E4) == true);
|
|---|
| 71 | BOOST_CHECK((bu::isgreaterequal)(E4,E4) == true);
|
|---|
| 72 | BOOST_CHECK((bu::isgreaterequal)(E3,E4) == false);
|
|---|
| 73 | BOOST_CHECK((bu::isgreaterequal)(E4,E3) == false);
|
|---|
| 74 |
|
|---|
| 75 | BOOST_CHECK((bu::isless)(E4,E5) == true);
|
|---|
| 76 | BOOST_CHECK((bu::isless)(E5,E4) == false);
|
|---|
| 77 | BOOST_CHECK((bu::isless)(E4,E4) == false);
|
|---|
| 78 | BOOST_CHECK((bu::isless)(E3,E4) == false);
|
|---|
| 79 | BOOST_CHECK((bu::isless)(E4,E3) == false);
|
|---|
| 80 |
|
|---|
| 81 | BOOST_CHECK((bu::islessequal)(E4,E5) == true);
|
|---|
| 82 | BOOST_CHECK((bu::islessequal)(E5,E4) == false);
|
|---|
| 83 | BOOST_CHECK((bu::islessequal)(E4,E4) == true);
|
|---|
| 84 | BOOST_CHECK((bu::islessequal)(E3,E4) == false);
|
|---|
| 85 | BOOST_CHECK((bu::islessequal)(E4,E3) == false);
|
|---|
| 86 |
|
|---|
| 87 | BOOST_CHECK((bu::islessgreater)(E4,E5) == true);
|
|---|
| 88 | BOOST_CHECK((bu::islessgreater)(E5,E4) == true);
|
|---|
| 89 | BOOST_CHECK((bu::islessgreater)(E4,E4) == false);
|
|---|
| 90 | BOOST_CHECK((bu::islessgreater)(E3,E4) == false);
|
|---|
| 91 | BOOST_CHECK((bu::islessgreater)(E4,E3) == false);
|
|---|
| 92 |
|
|---|
| 93 | BOOST_CHECK((bu::isunordered)(E4,E5) == false);
|
|---|
| 94 | BOOST_CHECK((bu::isunordered)(E5,E4) == false);
|
|---|
| 95 | BOOST_CHECK((bu::isunordered)(E4,E4) == false);
|
|---|
| 96 | BOOST_CHECK((bu::isunordered)(E3,E4) == true);
|
|---|
| 97 | BOOST_CHECK((bu::isunordered)(E4,E3) == true);
|
|---|
| 98 |
|
|---|
| 99 | BOOST_CHECK((bu::abs)(E4) == E5);
|
|---|
| 100 | BOOST_CHECK((bu::ceil)(E4) == -2.0*bu::joules);
|
|---|
| 101 | BOOST_CHECK((bu::copysign)(E4,E5) == E5);
|
|---|
| 102 | BOOST_CHECK((bu::fabs)(E4) == E5);
|
|---|
| 103 | BOOST_CHECK((bu::floor)(E4) == -3.0*bu::joules);
|
|---|
| 104 | BOOST_CHECK((bu::fdim)(E4,E5) == 0.0*bu::joules);
|
|---|
| 105 | BOOST_CHECK((bu::fdim)(E5,E4) == E5-E4);
|
|---|
| 106 |
|
|---|
| 107 | const bu::quantity<bu::length> L1(3.0*bu::meters),
|
|---|
| 108 | L2(4.0*bu::meters);
|
|---|
| 109 | const bu::quantity<bu::area> A1(4.0*bu::square_meters),
|
|---|
| 110 | A2(L1*L2+A1);
|
|---|
| 111 | const bu::quantity<bu::volume> V1(8.0*bu::cubic_meters);
|
|---|
| 112 |
|
|---|
| 113 | #if 0
|
|---|
| 114 | BOOST_CHECK((bu::fma)(L1,L2,A1) == A2);
|
|---|
| 115 | #endif
|
|---|
| 116 |
|
|---|
| 117 | BOOST_CHECK((bu::fmax)(E4,E5) == E5);
|
|---|
| 118 | BOOST_CHECK((bu::fmin)(E4,E5) == E4);
|
|---|
| 119 |
|
|---|
| 120 | // need to test fpclassify
|
|---|
| 121 |
|
|---|
| 122 | BOOST_CHECK(bu::hypot(L1,L2) == 5.0*bu::meters);
|
|---|
| 123 |
|
|---|
| 124 | #if 0
|
|---|
| 125 |
|
|---|
| 126 | // BOOST_CHECK(bu::llrint(E4).value() == bu::detail::llrint(E4.value()));
|
|---|
| 127 | // BOOST_CHECK(bu::llround(E4).value() == bu::detail::llround(E4.value()));
|
|---|
| 128 | BOOST_CHECK((bu::nearbyint)(E4).value() == (bu::detail::nearbyint)(E4.value()));
|
|---|
| 129 | BOOST_CHECK((bu::rint)(E4).value() == (bu::detail::rint)(E4.value()));
|
|---|
| 130 |
|
|---|
| 131 | #endif
|
|---|
| 132 |
|
|---|
| 133 | BOOST_CHECK((bu::nextafter)(E4,E5).value() == (boost::math::nextafter)(E4.value(),E5.value()));
|
|---|
| 134 | BOOST_CHECK((bu::nextafter)(E5,E4).value() == (boost::math::nextafter)(E5.value(),E4.value()));
|
|---|
| 135 |
|
|---|
| 136 | BOOST_CHECK((bu::nexttoward)(E4,E5).value() == (boost::math::nextafter)(E4.value(),E5.value()));
|
|---|
| 137 | BOOST_CHECK((bu::nexttoward)(E5,E4).value() == (boost::math::nextafter)(E5.value(),E4.value()));
|
|---|
| 138 |
|
|---|
| 139 | BOOST_CHECK((bu::round)(E4 - 0.00000000001 * bu::joules) == -3.0*bu::joules);
|
|---|
| 140 | BOOST_CHECK((bu::round)(E5 + 0.00000000001 * bu::joules) == 3.0*bu::joules);
|
|---|
| 141 | BOOST_CHECK((bu::signbit)(E4) != 0);
|
|---|
| 142 | BOOST_CHECK((bu::signbit)(E5) == 0);
|
|---|
| 143 | BOOST_CHECK((bu::trunc)(E4) == -2.0*bu::joules);
|
|---|
| 144 | BOOST_CHECK((bu::trunc)(E5) == 2.0*bu::joules);
|
|---|
| 145 |
|
|---|
| 146 | BOOST_CHECK((bu::fmod)(E4,E5) == -0.0*bu::joules);
|
|---|
| 147 |
|
|---|
| 148 | bu::quantity<bu::energy> pint;
|
|---|
| 149 |
|
|---|
| 150 | BOOST_CHECK((bu::modf)(E4,&pint) == -0.5*bu::joules);
|
|---|
| 151 | BOOST_CHECK(pint == -2.0*bu::joules);
|
|---|
| 152 |
|
|---|
| 153 | int ex;
|
|---|
| 154 | const bu::quantity<bu::energy> E6((bu::frexp)(E4,&ex));
|
|---|
| 155 |
|
|---|
| 156 | BOOST_CHECK(E6 == -0.625*bu::joules);
|
|---|
| 157 | BOOST_CHECK(ex == 2);
|
|---|
| 158 | BOOST_CHECK((bu::ldexp)(E6,ex) == E4);
|
|---|
| 159 |
|
|---|
| 160 | const bu::quantity<bu::dimensionless> E7(1.0);
|
|---|
| 161 |
|
|---|
| 162 | BOOST_CHECK(bu::pow(E7,E7) == 1.0*1.0);
|
|---|
| 163 |
|
|---|
| 164 | const bu::quantity<bu::dimensionless> E8((bu::exp)(E7));
|
|---|
| 165 |
|
|---|
| 166 | BOOST_CHECK(std::abs(E8 - std::exp(1.0)) < .000001);
|
|---|
| 167 | BOOST_CHECK(bu::log(E8) == E7);
|
|---|
| 168 |
|
|---|
| 169 | const bu::quantity<bu::dimensionless> E9(100.0);
|
|---|
| 170 |
|
|---|
| 171 | BOOST_CHECK(bu::log10(E9) == 2.0);
|
|---|
| 172 |
|
|---|
| 173 | BOOST_CHECK(bu::sqrt(A1) == 2.0*bu::meters);
|
|---|
| 174 |
|
|---|
| 175 | BOOST_CHECK(bu::cbrt(V1) == 2.0*bu::meters);
|
|---|
| 176 |
|
|---|
| 177 | BOOST_CHECK(bu::min(L1,L2) == L1);
|
|---|
| 178 |
|
|---|
| 179 | BOOST_CHECK(bu::max(L1,L2) == L2);
|
|---|
| 180 |
|
|---|
| 181 | return 0;
|
|---|
| 182 | }
|
|---|