test_lambda.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. // $Id: test_lambda.cpp 27 2008-06-16 14:50:58Z maehne $
  11. ////////////////////////////////////////////////////////////////////////
  12. ///
  13. /// \file test_lambda.hpp
  14. ///
  15. /// \brief Unit test for checking the usage of Boost.Units' quantity,
  16. /// unit, and absolute types in functors created with the
  17. /// Boost.Lambda library.
  18. ///
  19. /// \author Torsten Maehne
  20. /// \date 2008-06-16
  21. ///
  22. /// This unit test contains a check for each operator action, for
  23. /// which a specialization of Boost.Lambda's return type deduction
  24. /// system is made in lambda.hpp, i.e., for the operators defined for
  25. /// Boost.Units' quantity, unit, and absolute types.
  26. ///
  27. ////////////////////////////////////////////////////////////////////////
  28. #include <boost/function.hpp>
  29. #include <boost/units/lambda.hpp>
  30. #include <boost/units/absolute.hpp>
  31. #include <boost/units/systems/si/temperature.hpp>
  32. #include "test_header.hpp"
  33. namespace bl = boost::lambda;
  34. namespace bu = boost::units;
  35. namespace si = boost::units::si;
  36. int test_main(int, char *[])
  37. {
  38. ////////////////////////////////////////////////////////////////////////
  39. // Test for Boost.Lambda working with overloaded operators defined
  40. // in <boost/units/quantity.hpp>
  41. ////////////////////////////////////////////////////////////////////////
  42. bu::quantity<bu::length> lvar = 0.0 * bu::meter;
  43. bu::quantity<bu::dimensionless> dlvar = 3.0;
  44. // quantity<Unit, Y> += quantity<Unit2, YY>
  45. boost::function<bu::quantity<bu::length> (bu::quantity<bu::length>)>
  46. f = (bl::var(lvar) += bl::_1);
  47. lvar = 1.0 * bu::meter;
  48. BOOST_CHECK((f(2.0 * bu::meter) == 3.0 * bu::meter));
  49. BOOST_CHECK((f(6.0 * bu::meter) == 9.0 * bu::meter));
  50. // quantity<BOOST_UNITS_DIMENSIONLESS_UNIT(System), Y> += quantity<BOOST_UNITS_DIMENSIONLESS_UNIT(System), Y>
  51. dlvar = 4.0;
  52. BOOST_CHECK(((bl::var(dlvar) += bl::_1)(3.0) == 7.0));
  53. // quantity<Unit, Y> -= quantity<Unit2, YY>
  54. lvar = 3.0 * bu::meter;
  55. BOOST_CHECK((f(-2.0 * bu::meter) == 1.0 * bu::meter));
  56. BOOST_CHECK((f(6.0 * bu::meter) == 7.0 * bu::meter));
  57. // quantity<BOOST_UNITS_DIMENSIONLESS_UNIT(System), Y> -= quantity<BOOST_UNITS_DIMENSIONLESS_UNIT(System), Y>
  58. dlvar = 4.0;
  59. BOOST_CHECK(((bl::var(dlvar) -= bl::_1)(3.0) == 1.0));
  60. // quantity<Unit, Y> *= quantity<Unit2, YY>
  61. dlvar = 2.0;
  62. BOOST_CHECK(((bl::var(dlvar) *= bl::_1)(3.0) == 6.0));
  63. // quantity<Unit, Y> /= quantity<Unit2, YY>
  64. dlvar = 6.0;
  65. BOOST_CHECK(((bl::var(dlvar) /= bl::_1)(3.0) == 2.0));
  66. // quantity<Unit, Y> *= Y
  67. lvar = 3.0 * bu::meter;
  68. BOOST_CHECK(((bl::var(lvar) *= bl::_1)(2.0) == 6.0 * bu::meter));
  69. // quantity<Unit, Y> /= Y
  70. lvar = 6.0 * bu::meter;
  71. BOOST_CHECK(((bl::var(lvar) /= bl::_1)(3.0) == 2.0 * bu::meter));
  72. // unit<Dim, System> * Y
  73. BOOST_CHECK(((bl::_1 * bl::_2)(bu::meter, 2.0) == 2.0 * bu::meter));
  74. BOOST_CHECK(((bu::meter * bl::_1)(2.0) == 2.0 * bu::meter));
  75. // unit<Dim, System> / Y
  76. BOOST_CHECK(((bl::_1 / bl::_2)(bu::meter, 0.5) == 2.0 * bu::meter));
  77. BOOST_CHECK(((bu::meter / bl::_1)(0.5 * bu::second) == 2.0 * bu::meter_per_second));
  78. // Y * unit<Dim, System>
  79. BOOST_CHECK(((bl::_1 * bl::_2)(2.0, bu::meter) == 2.0 * bu::meter));
  80. BOOST_CHECK(((bl::_1 * bu::meter)(2.0 / bu::second) == 2.0 * bu::meter_per_second));
  81. // Y / unit<Dim, System>
  82. BOOST_CHECK(((bl::_1 / bl::_2)(3.5, bu::second) == 3.5 / bu::second));
  83. BOOST_CHECK(((bl::_1 / bu::second)(3.5 * bu::meter) == 3.5 * bu::meter_per_second));
  84. // quantity<Unit, X> * X
  85. BOOST_CHECK(((bl::_1 * bl::_2)(2.0, 3.0 * bu::meter) == 6.0 * bu::meter));
  86. // X * quantity<Unit, X>
  87. BOOST_CHECK(((bl::_1 * bl::_2)(4.0 * bu::joule, 2.0) == 8.0 * bu::joule));
  88. // quantity<Unit, X> / X
  89. BOOST_CHECK(((bl::_1 / bl::_2)(4.0 * bu::joule, 2.0) == 2.0 * bu::joule));
  90. // X / quantity<Unit, X>
  91. BOOST_CHECK(((3.0 / bl::_1)(2.0 * bu::second) == 1.5 / bu::second));
  92. // unit<Dim1, System1> * quantity<Unit2, Y>
  93. BOOST_CHECK(((bl::_1 * bl::_2)(bu::meter, 12.0 / bu::second) == 12.0 * bu::meter_per_second));
  94. BOOST_CHECK(((bu::meter * bl::_1)(12.0 / bu::second) == 12.0 * bu::meter_per_second));
  95. // unit<Dim1, System1> / quantity<Unit2, Y>
  96. BOOST_CHECK(((bl::_1 / bl::_2)(bu::meter, 0.5 * bu::second) == 2.0 * bu::meter_per_second));
  97. BOOST_CHECK(((bu::meter / bl::_1)(0.25 * bu::second) == 4.0 * bu::meter_per_second));
  98. // quantity<Unit1, Y> * unit<Dim2, System2>
  99. BOOST_CHECK(((bl::_1 * bl::_2)(2.0 / bu::second, bu::meter) == 2.0 * bu::meter_per_second));
  100. BOOST_CHECK(((bl::_1 * bu::meter)(12.0 / bu::second) == 12.0 * bu::meter_per_second));
  101. // quantity<Unit1, Y> / unit<Dim2, System2>
  102. BOOST_CHECK(((bl::_1 / bl::_2)(3.5 * bu::meter, bu::second) == 3.5 * bu::meter_per_second));
  103. BOOST_CHECK(((bl::_1 / bu::second)(5.0 * bu::second) == 5.0));
  104. // +quantity<Unit, Y>
  105. BOOST_CHECK(((+bl::_1)(5.0 * bu::second) == 5.0 * bu::second));
  106. // -quantity<Unit, Y>
  107. BOOST_CHECK(((-bl::_1)(5.0 * bu::second) == -5.0 * bu::second));
  108. // quantity<Unit1, X> + quantity<Unit2, Y>
  109. BOOST_CHECK(((bl::_1 + bl::_2)(2.0 * bu::meter, 4.0 * bu::meter) == 6.0 * bu::meter));
  110. // quantity<dimensionless, X> + Y
  111. BOOST_CHECK(((bl::_1 + 1.0f)(bu::quantity<bu::dimensionless>(2.0)) == 3.0));
  112. // X + quantity<dimensionless, Y>
  113. BOOST_CHECK(((1.0f + bl::_1)(bu::quantity<bu::dimensionless>(1.0)) == 2.0));
  114. // quantity<Unit1, X> - quantity<Unit2, Y>
  115. BOOST_CHECK(((bl::_1 - bl::_2)(2.0 * bu::meter, 4.0 * bu::meter) == -2.0 * bu::meter));
  116. // quantity<dimensionless, X> - Y
  117. BOOST_CHECK(((bl::_1 - 2.0f)(bu::quantity<bu::dimensionless>(1.0)) == -1.0));
  118. // X - quantity<dimensionless, Y>
  119. BOOST_CHECK(((2.0f - bl::_1)(bu::quantity<bu::dimensionless>(1.0)) == 1.0));
  120. // quantity<Unit1, X> * quantity<Unit2, Y>
  121. BOOST_CHECK(((bl::_1 * bl::_2)(2.0 * bu::kilogram, 4.0 * bu::meter_per_second) == 8.0 * bu::kilogram * bu::meter_per_second));
  122. // quantity<Unit1, X> / quantity<Unit2, Y>
  123. BOOST_CHECK(((bl::_1 / bl::_2)(2.0 * bu::meter_per_second, 4.0 * bu::meter_per_second) == 0.5));
  124. // quantity<Unit, X> == quantity<Unit, Y>
  125. BOOST_CHECK(((bl::_1 == bl::_2)(2.0 * bu::meter, 2.0 * bu::meter) == true));
  126. BOOST_CHECK(((bl::_1 == bl::_2)(2.0 * bu::meter, 3.0 * bu::meter) == false));
  127. // quantity<Unit, X> != quantity<Unit, Y>
  128. BOOST_CHECK(((bl::_1 != bl::_2)(2.0 * bu::meter, 2.0 * bu::meter) == false));
  129. BOOST_CHECK(((bl::_1 != bl::_2)(2.0 * bu::meter, 3.0 * bu::meter) == true));
  130. // quantity<Unit, X> < quantity<Unit, Y>
  131. BOOST_CHECK(((bl::_1 < bl::_2)(2.0 * bu::meter, 2.0 * bu::meter) == false));
  132. BOOST_CHECK(((bl::_1 < bl::_2)(2.0 * bu::meter, 3.0 * bu::meter) == true));
  133. // quantity<Unit, X> <= quantity<Unit, Y>
  134. BOOST_CHECK(((bl::_1 <= bl::_2)(2.0 * bu::meter, 2.0 * bu::meter) == true));
  135. BOOST_CHECK(((bl::_1 <= bl::_2)(2.0 * bu::meter, 3.0 * bu::meter) == true));
  136. BOOST_CHECK(((bl::_1 <= bl::_2)(4.0 * bu::meter, 3.0 * bu::meter) == false));
  137. // quantity<Unit, X> > quantity<Unit, Y>
  138. BOOST_CHECK(((bl::_1 > bl::_2)(2.0 * bu::meter, 2.0 * bu::meter) == false));
  139. BOOST_CHECK(((bl::_1 > bl::_2)(2.0 * bu::meter, 3.0 * bu::meter) == false));
  140. BOOST_CHECK(((bl::_1 > bl::_2)(4.0 * bu::meter, 3.0 * bu::meter) == true));
  141. // quantity<Unit, X> >= quantity<Unit, Y>
  142. BOOST_CHECK(((bl::_1 >= bl::_2)(2.0 * bu::meter, 2.0 * bu::meter) == true));
  143. BOOST_CHECK(((bl::_1 >= bl::_2)(2.0 * bu::meter, 3.0 * bu::meter) == false));
  144. BOOST_CHECK(((bl::_1 >= bl::_2)(4.0 * bu::meter, 3.0 * bu::meter) == true));
  145. ////////////////////////////////////////////////////////////////////////
  146. // Test for Boost.Lambda working with overloaded operators defined
  147. // in <boost/units/unit.hpp>
  148. ////////////////////////////////////////////////////////////////////////
  149. // +unit<Dim, System>
  150. BOOST_CHECK(((+bl::_1)(bu::meter) == bu::meter));
  151. // -unit<Dim, System>
  152. BOOST_CHECK(((-bl::_1)(bu::meter) == bu::meter));
  153. // unit<Dim1, System1> + unit<Dim2, System2>
  154. BOOST_CHECK(((bl::_1 + bu::meter)(bu::meter) == bu::meter));
  155. BOOST_CHECK(((bu::meter + bl::_1)(bu::meter) == bu::meter));
  156. BOOST_CHECK(((bl::_1 + bl::_2)(bu::meter, bu::meter) == bu::meter));
  157. // unit<Dim1, System1> - unit<Dim2, System2>
  158. BOOST_CHECK(((bl::_1 - bl::_2)(bu::meter, bu::meter) == bu::meter));
  159. BOOST_CHECK(((bl::_1 - bu::meter)(bu::meter) == bu::meter));
  160. BOOST_CHECK(((bu::meter - bl::_1)(bu::meter) == bu::meter));
  161. // unit<Dim1, System1> * unit<Dim2, System2>
  162. BOOST_CHECK(((bl::_1 * bl::_2)(bu::meter, bu::meter) == bu::meter * bu::meter));
  163. BOOST_CHECK(((bl::_1 * bu::meter)(bu::meter) == bu::meter * bu::meter));
  164. // unit<Dim1, System1> / unit<Dim2, System2>
  165. BOOST_CHECK(((bl::_1 / bl::_2)(bu::meter, bu::second) == bu::meter_per_second));
  166. BOOST_CHECK(((bl::_1 / bu::second)(bu::meter) == bu::meter_per_second));
  167. // unit<Dim1, System1> == unit<Dim2, System2>
  168. BOOST_CHECK(((bl::_1 == bu::meter)(bu::meter) == true));
  169. BOOST_CHECK(((bl::_1 == bu::meter)(bu::second) == false));
  170. // unit<Dim1, System1> != unit<Dim2, System2>
  171. BOOST_CHECK(((bl::_1 != bu::meter)(bu::meter) == false));
  172. BOOST_CHECK(((bl::_1 != bu::meter)(bu::second) == true));
  173. ////////////////////////////////////////////////////////////////////////
  174. // Test for Boost.Lambda working with overloaded operators defined
  175. // in <boost/units/absolute.hpp>
  176. ////////////////////////////////////////////////////////////////////////
  177. // absolute<Y> += Y
  178. bu::quantity<bu::absolute<si::temperature> > Ta = 270.0 * bu::absolute<si::temperature>();
  179. (Ta += bl::_1)(30.0 * si::kelvin);
  180. BOOST_CHECK(( Ta == 300.0 * bu::absolute<si::temperature>()));
  181. // absolute<Y> -= Y
  182. Ta = 270 * bu::absolute<si::temperature>();
  183. (Ta -= bl::_1)(-30.0 * si::kelvin);
  184. BOOST_CHECK(( Ta == 300.0 * bu::absolute<si::temperature>()));
  185. // absolute<Y> + Y
  186. BOOST_CHECK(((270.0 * bu::absolute<si::temperature>() + bl::_1)(30.0 * si::kelvin) == 300.0 * bu::absolute<si::temperature>()));
  187. // Y + absolute<Y>
  188. BOOST_CHECK(((bl::_1 + 270.0 * bu::absolute<si::temperature>())(30.0 * si::kelvin) == 300.0 * bu::absolute<si::temperature>()));
  189. // absolute<Y> - Y
  190. BOOST_CHECK(((270.0 * bu::absolute<si::temperature>() - bl::_1)(30.0 * si::kelvin) == 240.0 * bu::absolute<si::temperature>()));
  191. // absolute<Y> - absolute<Y>
  192. BOOST_CHECK(((bl::_1 - 270.0 * bu::absolute<si::temperature>())(300.0 * bu::absolute<si::temperature>()) == 30.0 * si::kelvin));
  193. // T * absolute<unit<D, S> >
  194. BOOST_CHECK(((bl::_1 * bu::absolute<si::temperature>())(300.0) == 300.0 * bu::absolute<si::temperature>()));
  195. BOOST_CHECK(((bl::_1 * bl::_2)(300.0, bu::absolute<si::temperature>()) == 300.0 * bu::absolute<si::temperature>()));
  196. // absolute<unit<D, S> > * T
  197. BOOST_CHECK(((bu::absolute<si::temperature>() * bl::_1)(300.0) == 300.0 * bu::absolute<si::temperature>()));
  198. BOOST_CHECK(((bl::_1 * bl::_2)(bu::absolute<si::temperature>(), 300.0) == 300.0 * bu::absolute<si::temperature>()));
  199. return 0;
  200. }