test_operators_comparison.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // Copyright (c) 2018-2019 Cem Bassoy
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // The authors gratefully acknowledge the support of
  8. // Fraunhofer and Google in producing this work
  9. // which started as a Google Summer of Code project.
  10. //
  11. #include <boost/numeric/ublas/tensor/operators_comparison.hpp>
  12. #include <boost/numeric/ublas/tensor/operators_arithmetic.hpp>
  13. #include <boost/numeric/ublas/tensor/tensor.hpp>
  14. #include <boost/test/unit_test.hpp>
  15. #include <boost/multiprecision/cpp_bin_float.hpp>
  16. #include "utility.hpp"
  17. using double_extended = boost::multiprecision::cpp_bin_float_double_extended;
  18. using test_types = zip<int,long,float,double,double_extended>::with_t<boost::numeric::ublas::first_order, boost::numeric::ublas::last_order>;
  19. struct fixture {
  20. using extents_type = boost::numeric::ublas::basic_extents<std::size_t>;
  21. fixture()
  22. : extents{
  23. extents_type{}, // 0
  24. extents_type{1,1}, // 1
  25. extents_type{1,2}, // 2
  26. extents_type{2,1}, // 3
  27. extents_type{2,3}, // 4
  28. extents_type{2,3,1}, // 5
  29. extents_type{4,1,3}, // 6
  30. extents_type{1,2,3}, // 7
  31. extents_type{4,2,3}, // 8
  32. extents_type{4,2,3,5}} // 9
  33. {
  34. }
  35. std::vector<extents_type> extents;
  36. };
  37. BOOST_AUTO_TEST_SUITE(test_tensor_comparison, * boost::unit_test::depends_on("test_tensor"))
  38. BOOST_FIXTURE_TEST_CASE_TEMPLATE( test_tensor_comparison, value, test_types, fixture)
  39. {
  40. using namespace boost::numeric;
  41. using value_type = typename value::first_type;
  42. using layout_type = typename value::second_type;
  43. using tensor_type = ublas::tensor<value_type, layout_type>;
  44. auto check = [](auto const& e)
  45. {
  46. auto t = tensor_type (e);
  47. auto t2 = tensor_type (e);
  48. auto v = value_type {};
  49. std::iota(t.begin(), t.end(), v);
  50. std::iota(t2.begin(), t2.end(), v+2);
  51. BOOST_CHECK( t == t );
  52. BOOST_CHECK( t != t2 );
  53. if(t.empty())
  54. return;
  55. BOOST_CHECK(!(t < t));
  56. BOOST_CHECK(!(t > t));
  57. BOOST_CHECK( t < t2 );
  58. BOOST_CHECK( t2 > t );
  59. BOOST_CHECK( t <= t );
  60. BOOST_CHECK( t >= t );
  61. BOOST_CHECK( t <= t2 );
  62. BOOST_CHECK( t2 >= t );
  63. BOOST_CHECK( t2 >= t2 );
  64. BOOST_CHECK( t2 >= t );
  65. };
  66. for(auto const& e : extents)
  67. check(e);
  68. auto e0 = extents.at(0);
  69. auto e1 = extents.at(1);
  70. auto e2 = extents.at(2);
  71. auto b = false;
  72. BOOST_CHECK_NO_THROW ( b = (tensor_type(e0) == tensor_type(e0)));
  73. BOOST_CHECK_NO_THROW ( b = (tensor_type(e1) == tensor_type(e2)));
  74. BOOST_CHECK_NO_THROW ( b = (tensor_type(e0) == tensor_type(e2)));
  75. BOOST_CHECK_NO_THROW ( b = (tensor_type(e1) != tensor_type(e2)));
  76. BOOST_CHECK_THROW ( b = (tensor_type(e1) >= tensor_type(e2)), std::runtime_error );
  77. BOOST_CHECK_THROW ( b = (tensor_type(e1) <= tensor_type(e2)), std::runtime_error );
  78. BOOST_CHECK_THROW ( b = (tensor_type(e1) < tensor_type(e2)), std::runtime_error );
  79. BOOST_CHECK_THROW ( b = (tensor_type(e1) > tensor_type(e2)), std::runtime_error );
  80. }
  81. BOOST_FIXTURE_TEST_CASE_TEMPLATE( test_tensor_comparison_with_tensor_expressions, value, test_types, fixture)
  82. {
  83. using namespace boost::numeric;
  84. using value_type = typename value::first_type;
  85. using layout_type = typename value::second_type;
  86. using tensor_type = ublas::tensor<value_type, layout_type>;
  87. auto check = [](auto const& e)
  88. {
  89. auto t = tensor_type (e);
  90. auto t2 = tensor_type (e);
  91. auto v = value_type {};
  92. std::iota(t.begin(), t.end(), v);
  93. std::iota(t2.begin(), t2.end(), v+2);
  94. BOOST_CHECK( t == t );
  95. BOOST_CHECK( t != t2 );
  96. if(t.empty())
  97. return;
  98. BOOST_CHECK( !(t < t) );
  99. BOOST_CHECK( !(t > t) );
  100. BOOST_CHECK( t < (t2+t) );
  101. BOOST_CHECK( (t2+t) > t );
  102. BOOST_CHECK( t <= (t+t) );
  103. BOOST_CHECK( (t+t2) >= t );
  104. BOOST_CHECK( (t2+t2+2) >= t);
  105. BOOST_CHECK( 2*t2 > t );
  106. BOOST_CHECK( t < 2*t2 );
  107. BOOST_CHECK( 2*t2 > t);
  108. BOOST_CHECK( 2*t2 >= t2 );
  109. BOOST_CHECK( t2 <= 2*t2);
  110. BOOST_CHECK( 3*t2 >= t );
  111. };
  112. for(auto const& e : extents)
  113. check(e);
  114. auto e0 = extents.at(0);
  115. auto e1 = extents.at(1);
  116. auto e2 = extents.at(2);
  117. auto b = false;
  118. BOOST_CHECK_NO_THROW (b = tensor_type(e0) == (tensor_type(e0) + tensor_type(e0)) );
  119. BOOST_CHECK_NO_THROW (b = tensor_type(e1) == (tensor_type(e2) + tensor_type(e2)) );
  120. BOOST_CHECK_NO_THROW (b = tensor_type(e0) == (tensor_type(e2) + 2) );
  121. BOOST_CHECK_NO_THROW (b = tensor_type(e1) != (2 + tensor_type(e2)) );
  122. BOOST_CHECK_NO_THROW (b = (tensor_type(e0) + tensor_type(e0)) == tensor_type(e0) );
  123. BOOST_CHECK_NO_THROW (b = (tensor_type(e2) + tensor_type(e2)) == tensor_type(e1) );
  124. BOOST_CHECK_NO_THROW (b = (tensor_type(e2) + 2) == tensor_type(e0) );
  125. BOOST_CHECK_NO_THROW (b = (2 + tensor_type(e2)) != tensor_type(e1) );
  126. BOOST_CHECK_THROW (b = tensor_type(e1) >= (tensor_type(e2) + tensor_type(e2)), std::runtime_error );
  127. BOOST_CHECK_THROW (b = tensor_type(e1) <= (tensor_type(e2) + tensor_type(e2)), std::runtime_error );
  128. BOOST_CHECK_THROW (b = tensor_type(e1) < (tensor_type(e2) + tensor_type(e2)), std::runtime_error );
  129. BOOST_CHECK_THROW (b = tensor_type(e1) > (tensor_type(e2) + tensor_type(e2)), std::runtime_error );
  130. BOOST_CHECK_THROW (b = tensor_type(e1) >= (tensor_type(e2) + 2), std::runtime_error );
  131. BOOST_CHECK_THROW (b = tensor_type(e1) <= (2 + tensor_type(e2)), std::runtime_error );
  132. BOOST_CHECK_THROW (b = tensor_type(e1) < (tensor_type(e2) + 3), std::runtime_error );
  133. BOOST_CHECK_THROW (b = tensor_type(e1) > (4 + tensor_type(e2)), std::runtime_error );
  134. }
  135. BOOST_FIXTURE_TEST_CASE_TEMPLATE( test_tensor_comparison_with_scalar, value, test_types, fixture)
  136. {
  137. using namespace boost::numeric;
  138. using value_type = typename value::first_type;
  139. using layout_type = typename value::second_type;
  140. using tensor_type = ublas::tensor<value_type, layout_type>;
  141. auto check = [](auto const& e)
  142. {
  143. BOOST_CHECK( tensor_type(e,value_type{2}) == tensor_type(e,value_type{2}) );
  144. BOOST_CHECK( tensor_type(e,value_type{2}) != tensor_type(e,value_type{1}) );
  145. if(e.empty())
  146. return;
  147. BOOST_CHECK( !(tensor_type(e,2) < 2) );
  148. BOOST_CHECK( !(tensor_type(e,2) > 2) );
  149. BOOST_CHECK( (tensor_type(e,2) >= 2) );
  150. BOOST_CHECK( (tensor_type(e,2) <= 2) );
  151. BOOST_CHECK( (tensor_type(e,2) == 2) );
  152. BOOST_CHECK( (tensor_type(e,2) != 3) );
  153. BOOST_CHECK( !(2 > tensor_type(e,2)) );
  154. BOOST_CHECK( !(2 < tensor_type(e,2)) );
  155. BOOST_CHECK( (2 <= tensor_type(e,2)) );
  156. BOOST_CHECK( (2 >= tensor_type(e,2)) );
  157. BOOST_CHECK( (2 == tensor_type(e,2)) );
  158. BOOST_CHECK( (3 != tensor_type(e,2)) );
  159. BOOST_CHECK( !( tensor_type(e,2)+3 < 5) );
  160. BOOST_CHECK( !( tensor_type(e,2)+3 > 5) );
  161. BOOST_CHECK( ( tensor_type(e,2)+3 >= 5) );
  162. BOOST_CHECK( ( tensor_type(e,2)+3 <= 5) );
  163. BOOST_CHECK( ( tensor_type(e,2)+3 == 5) );
  164. BOOST_CHECK( ( tensor_type(e,2)+3 != 6) );
  165. BOOST_CHECK( !( 5 > tensor_type(e,2)+3) );
  166. BOOST_CHECK( !( 5 < tensor_type(e,2)+3) );
  167. BOOST_CHECK( ( 5 >= tensor_type(e,2)+3) );
  168. BOOST_CHECK( ( 5 <= tensor_type(e,2)+3) );
  169. BOOST_CHECK( ( 5 == tensor_type(e,2)+3) );
  170. BOOST_CHECK( ( 6 != tensor_type(e,2)+3) );
  171. BOOST_CHECK( !( tensor_type(e,2)+tensor_type(e,3) < 5) );
  172. BOOST_CHECK( !( tensor_type(e,2)+tensor_type(e,3) > 5) );
  173. BOOST_CHECK( ( tensor_type(e,2)+tensor_type(e,3) >= 5) );
  174. BOOST_CHECK( ( tensor_type(e,2)+tensor_type(e,3) <= 5) );
  175. BOOST_CHECK( ( tensor_type(e,2)+tensor_type(e,3) == 5) );
  176. BOOST_CHECK( ( tensor_type(e,2)+tensor_type(e,3) != 6) );
  177. BOOST_CHECK( !( 5 > tensor_type(e,2)+tensor_type(e,3)) );
  178. BOOST_CHECK( !( 5 < tensor_type(e,2)+tensor_type(e,3)) );
  179. BOOST_CHECK( ( 5 >= tensor_type(e,2)+tensor_type(e,3)) );
  180. BOOST_CHECK( ( 5 <= tensor_type(e,2)+tensor_type(e,3)) );
  181. BOOST_CHECK( ( 5 == tensor_type(e,2)+tensor_type(e,3)) );
  182. BOOST_CHECK( ( 6 != tensor_type(e,2)+tensor_type(e,3)) );
  183. };
  184. for(auto const& e : extents)
  185. check(e);
  186. }
  187. BOOST_AUTO_TEST_SUITE_END()