collection_comparison_op.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. // (C) Copyright Gennadiy Rozental 2001.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // See http://www.boost.org/libs/test for the library home page.
  6. //
  7. //!@file
  8. //!@brief Collection comparison with enhanced reporting
  9. // ***************************************************************************
  10. #ifndef BOOST_TEST_TOOLS_COLLECTION_COMPARISON_OP_HPP_050815GER
  11. #define BOOST_TEST_TOOLS_COLLECTION_COMPARISON_OP_HPP_050815GER
  12. // Boost.Test
  13. #include <boost/test/tools/assertion.hpp>
  14. #include <boost/test/utils/is_forward_iterable.hpp>
  15. #include <boost/test/utils/is_cstring.hpp>
  16. // Boost
  17. #include <boost/mpl/bool.hpp>
  18. #include <boost/utility/enable_if.hpp>
  19. #include <boost/type_traits/decay.hpp>
  20. #include <boost/test/detail/suppress_warnings.hpp>
  21. //____________________________________________________________________________//
  22. namespace boost {
  23. namespace test_tools {
  24. namespace assertion {
  25. // ************************************************************************** //
  26. // ************* selectors for specialized comparizon routines ************** //
  27. // ************************************************************************** //
  28. template<typename T>
  29. struct specialized_compare : public mpl::false_ {};
  30. template <typename T>
  31. struct is_c_array : public mpl::false_ {};
  32. template<typename T, std::size_t N>
  33. struct is_c_array<T [N]> : public mpl::true_ {};
  34. template<typename T, std::size_t N>
  35. struct is_c_array<T (&)[N]> : public mpl::true_ {};
  36. #define BOOST_TEST_SPECIALIZED_COLLECTION_COMPARE(Col) \
  37. namespace boost { namespace test_tools { namespace assertion { \
  38. template<> \
  39. struct specialized_compare<Col> : public mpl::true_ {}; \
  40. }}} \
  41. /**/
  42. // ************************************************************************** //
  43. // ************** lexicographic_compare ************** //
  44. // ************************************************************************** //
  45. namespace op {
  46. template <typename OP, bool can_be_equal, bool prefer_shorter,
  47. typename Lhs, typename Rhs>
  48. inline
  49. typename boost::enable_if_c<
  50. unit_test::is_forward_iterable<Lhs>::value && !unit_test::is_cstring<Lhs>::value
  51. && unit_test::is_forward_iterable<Rhs>::value && !unit_test::is_cstring<Rhs>::value,
  52. assertion_result>::type
  53. lexicographic_compare( Lhs const& lhs, Rhs const& rhs )
  54. {
  55. assertion_result ar( true );
  56. typedef unit_test::bt_iterator_traits<Lhs> t_Lhs_iterator;
  57. typedef unit_test::bt_iterator_traits<Rhs> t_Rhs_iterator;
  58. typename t_Lhs_iterator::const_iterator first1 = t_Lhs_iterator::begin(lhs);
  59. typename t_Rhs_iterator::const_iterator first2 = t_Rhs_iterator::begin(rhs);
  60. typename t_Lhs_iterator::const_iterator last1 = t_Lhs_iterator::end(lhs);
  61. typename t_Rhs_iterator::const_iterator last2 = t_Rhs_iterator::end(rhs);
  62. std::size_t pos = 0;
  63. for( ; (first1 != last1) && (first2 != last2); ++first1, ++first2, ++pos ) {
  64. assertion_result const& element_ar = OP::eval(*first1, *first2);
  65. if( !can_be_equal && element_ar )
  66. return ar; // a < b
  67. assertion_result const& reverse_ar = OP::eval(*first2, *first1);
  68. if( element_ar && !reverse_ar )
  69. return ar; // a<=b and !(b<=a) => a < b => return true
  70. if( element_ar || !reverse_ar )
  71. continue; // (a<=b and b<=a) or (!(a<b) and !(b<a)) => a == b => keep looking
  72. // !(a<=b) and b<=a => b < a => return false
  73. ar = false;
  74. ar.message() << "\nFailure at position " << pos << ": "
  75. << tt_detail::print_helper(*first1)
  76. << OP::revert()
  77. << tt_detail::print_helper(*first2)
  78. << ". " << element_ar.message();
  79. return ar;
  80. }
  81. if( first1 != last1 ) {
  82. if( prefer_shorter ) {
  83. ar = false;
  84. ar.message() << "\nFirst collection has extra trailing elements.";
  85. }
  86. }
  87. else if( first2 != last2 ) {
  88. if( !prefer_shorter ) {
  89. ar = false;
  90. ar.message() << "\nSecond collection has extra trailing elements.";
  91. }
  92. }
  93. else if( !can_be_equal ) {
  94. ar = false;
  95. ar.message() << "\nCollections appear to be equal.";
  96. }
  97. return ar;
  98. }
  99. template <typename OP, bool can_be_equal, bool prefer_shorter,
  100. typename Lhs, typename Rhs>
  101. inline
  102. typename boost::enable_if_c<
  103. (unit_test::is_cstring<Lhs>::value || unit_test::is_cstring<Rhs>::value),
  104. assertion_result>::type
  105. lexicographic_compare( Lhs const& lhs, Rhs const& rhs )
  106. {
  107. typedef typename unit_test::deduce_cstring<Lhs>::type lhs_char_type;
  108. typedef typename unit_test::deduce_cstring<Rhs>::type rhs_char_type;
  109. return lexicographic_compare<OP, can_be_equal, prefer_shorter>(
  110. lhs_char_type(lhs),
  111. rhs_char_type(rhs));
  112. }
  113. //____________________________________________________________________________//
  114. // ************************************************************************** //
  115. // ************** equality_compare ************** //
  116. // ************************************************************************** //
  117. template <typename OP, typename Lhs, typename Rhs>
  118. inline
  119. typename boost::enable_if_c<
  120. unit_test::is_forward_iterable<Lhs>::value && !unit_test::is_cstring<Lhs>::value
  121. && unit_test::is_forward_iterable<Rhs>::value && !unit_test::is_cstring<Rhs>::value,
  122. assertion_result>::type
  123. element_compare( Lhs const& lhs, Rhs const& rhs )
  124. {
  125. typedef unit_test::bt_iterator_traits<Lhs> t_Lhs_iterator;
  126. typedef unit_test::bt_iterator_traits<Rhs> t_Rhs_iterator;
  127. assertion_result ar( true );
  128. if( t_Lhs_iterator::size(lhs) != t_Rhs_iterator::size(rhs) ) {
  129. ar = false;
  130. ar.message() << "\nCollections size mismatch: " << t_Lhs_iterator::size(lhs) << " != " << t_Rhs_iterator::size(rhs);
  131. return ar;
  132. }
  133. typename t_Lhs_iterator::const_iterator left = t_Lhs_iterator::begin(lhs);
  134. typename t_Rhs_iterator::const_iterator right = t_Rhs_iterator::begin(rhs);
  135. std::size_t pos = 0;
  136. for( ; pos < t_Lhs_iterator::size(lhs); ++left, ++right, ++pos ) {
  137. assertion_result const element_ar = OP::eval( *left, *right );
  138. if( element_ar )
  139. continue;
  140. ar = false;
  141. ar.message() << "\nMismatch at position " << pos << ": "
  142. << tt_detail::print_helper(*left)
  143. << OP::revert()
  144. << tt_detail::print_helper(*right)
  145. << ". " << element_ar.message();
  146. }
  147. return ar;
  148. }
  149. // In case string comparison is branching here
  150. template <typename OP, typename Lhs, typename Rhs>
  151. inline
  152. typename boost::enable_if_c<
  153. (unit_test::is_cstring<Lhs>::value || unit_test::is_cstring<Rhs>::value),
  154. assertion_result>::type
  155. element_compare( Lhs const& lhs, Rhs const& rhs )
  156. {
  157. typedef typename unit_test::deduce_cstring<Lhs>::type lhs_char_type;
  158. typedef typename unit_test::deduce_cstring<Rhs>::type rhs_char_type;
  159. return element_compare<OP>(lhs_char_type(lhs),
  160. rhs_char_type(rhs));
  161. }
  162. //____________________________________________________________________________//
  163. // ************************************************************************** //
  164. // ************** non_equality_compare ************** //
  165. // ************************************************************************** //
  166. template <typename OP, typename Lhs, typename Rhs>
  167. inline assertion_result
  168. non_equality_compare( Lhs const& lhs, Rhs const& rhs )
  169. {
  170. typedef unit_test::bt_iterator_traits<Lhs> t_Lhs_iterator;
  171. typedef unit_test::bt_iterator_traits<Rhs> t_Rhs_iterator;
  172. assertion_result ar( true );
  173. if( t_Lhs_iterator::size(lhs) != t_Rhs_iterator::size(rhs) )
  174. return ar;
  175. typename t_Lhs_iterator::const_iterator left = t_Lhs_iterator::begin(lhs);
  176. typename t_Rhs_iterator::const_iterator right = t_Rhs_iterator::begin(rhs);
  177. typename t_Lhs_iterator::const_iterator end = t_Lhs_iterator::end(lhs);
  178. for( ; left != end; ++left, ++right ) {
  179. if( OP::eval( *left, *right ) )
  180. return ar;
  181. }
  182. ar = false;
  183. ar.message() << "\nCollections appear to be equal";
  184. return ar;
  185. }
  186. //____________________________________________________________________________//
  187. // ************************************************************************** //
  188. // ************** cctraits ************** //
  189. // ************************************************************************** //
  190. // set of collection comparison traits per comparison OP
  191. template<typename OP>
  192. struct cctraits;
  193. template<typename Lhs, typename Rhs>
  194. struct cctraits<op::EQ<Lhs, Rhs> > {
  195. typedef specialized_compare<Lhs> is_specialized;
  196. };
  197. template<typename Lhs, typename Rhs>
  198. struct cctraits<op::NE<Lhs, Rhs> > {
  199. typedef specialized_compare<Lhs> is_specialized;
  200. };
  201. template<typename Lhs, typename Rhs>
  202. struct cctraits<op::LT<Lhs, Rhs> > {
  203. static const bool can_be_equal = false;
  204. static const bool prefer_short = true;
  205. typedef specialized_compare<Lhs> is_specialized;
  206. };
  207. template<typename Lhs, typename Rhs>
  208. struct cctraits<op::LE<Lhs, Rhs> > {
  209. static const bool can_be_equal = true;
  210. static const bool prefer_short = true;
  211. typedef specialized_compare<Lhs> is_specialized;
  212. };
  213. template<typename Lhs, typename Rhs>
  214. struct cctraits<op::GT<Lhs, Rhs> > {
  215. static const bool can_be_equal = false;
  216. static const bool prefer_short = false;
  217. typedef specialized_compare<Lhs> is_specialized;
  218. };
  219. template<typename Lhs, typename Rhs>
  220. struct cctraits<op::GE<Lhs, Rhs> > {
  221. static const bool can_be_equal = true;
  222. static const bool prefer_short = false;
  223. typedef specialized_compare<Lhs> is_specialized;
  224. };
  225. // ************************************************************************** //
  226. // ************** compare_collections ************** //
  227. // ************************************************************************** //
  228. // Overloaded set of functions dispatching to specific implementation of comparison
  229. template <typename Lhs, typename Rhs, typename L, typename R>
  230. inline assertion_result
  231. compare_collections( Lhs const& lhs, Rhs const& rhs, boost::type<op::EQ<L, R> >*, mpl::true_ )
  232. {
  233. return assertion::op::element_compare<op::EQ<L, R> >( lhs, rhs );
  234. }
  235. //____________________________________________________________________________//
  236. template <typename Lhs, typename Rhs, typename L, typename R>
  237. inline assertion_result
  238. compare_collections( Lhs const& lhs, Rhs const& rhs, boost::type<op::EQ<L, R> >*, mpl::false_ )
  239. {
  240. return lhs == rhs;
  241. }
  242. //____________________________________________________________________________//
  243. template <typename Lhs, typename Rhs, typename L, typename R>
  244. inline assertion_result
  245. compare_collections( Lhs const& lhs, Rhs const& rhs, boost::type<op::NE<L, R> >*, mpl::true_ )
  246. {
  247. return assertion::op::non_equality_compare<op::NE<L, R> >( lhs, rhs );
  248. }
  249. //____________________________________________________________________________//
  250. template <typename Lhs, typename Rhs, typename L, typename R>
  251. inline assertion_result
  252. compare_collections( Lhs const& lhs, Rhs const& rhs, boost::type<op::NE<L, R> >*, mpl::false_ )
  253. {
  254. return lhs != rhs;
  255. }
  256. //____________________________________________________________________________//
  257. template <typename OP, typename Lhs, typename Rhs>
  258. inline assertion_result
  259. lexicographic_compare( Lhs const& lhs, Rhs const& rhs )
  260. {
  261. return assertion::op::lexicographic_compare<OP, cctraits<OP>::can_be_equal, cctraits<OP>::prefer_short>( lhs, rhs );
  262. }
  263. //____________________________________________________________________________//
  264. template <typename Lhs, typename Rhs, typename OP>
  265. inline assertion_result
  266. compare_collections( Lhs const& lhs, Rhs const& rhs, boost::type<OP>*, mpl::true_ )
  267. {
  268. return lexicographic_compare<OP>( lhs, rhs );
  269. }
  270. //____________________________________________________________________________//
  271. template <typename Lhs, typename Rhs, typename L, typename R>
  272. inline assertion_result
  273. compare_collections( Lhs const& lhs, Rhs const& rhs, boost::type<op::LT<L, R> >*, mpl::false_ )
  274. {
  275. return lhs < rhs;
  276. }
  277. //____________________________________________________________________________//
  278. template <typename Lhs, typename Rhs, typename L, typename R>
  279. inline assertion_result
  280. compare_collections( Lhs const& lhs, Rhs const& rhs, boost::type<op::LE<L, R> >*, mpl::false_ )
  281. {
  282. return lhs <= rhs;
  283. }
  284. //____________________________________________________________________________//
  285. template <typename Lhs, typename Rhs, typename L, typename R>
  286. inline assertion_result
  287. compare_collections( Lhs const& lhs, Rhs const& rhs, boost::type<op::GT<L, R> >*, mpl::false_ )
  288. {
  289. return lhs > rhs;
  290. }
  291. //____________________________________________________________________________//
  292. template <typename Lhs, typename Rhs, typename L, typename R>
  293. inline assertion_result
  294. compare_collections( Lhs const& lhs, Rhs const& rhs, boost::type<op::GE<L, R> >*, mpl::false_ )
  295. {
  296. return lhs >= rhs;
  297. }
  298. //____________________________________________________________________________//
  299. // ************************************************************************** //
  300. // ********* specialization of comparison operators for collections ********* //
  301. // ************************************************************************** //
  302. #define DEFINE_COLLECTION_COMPARISON( oper, name, rev ) \
  303. template<typename Lhs,typename Rhs> \
  304. struct name<Lhs,Rhs,typename boost::enable_if_c< \
  305. unit_test::is_forward_iterable<Lhs>::value \
  306. && !unit_test::is_cstring_comparable<Lhs>::value \
  307. && unit_test::is_forward_iterable<Rhs>::value \
  308. && !unit_test::is_cstring_comparable<Rhs>::value>::type> { \
  309. public: \
  310. typedef assertion_result result_type; \
  311. typedef unit_test::bt_iterator_traits<Lhs> t_Lhs_iterator_helper; \
  312. typedef unit_test::bt_iterator_traits<Rhs> t_Rhs_iterator_helper; \
  313. \
  314. typedef name<Lhs, Rhs> OP; \
  315. \
  316. typedef typename \
  317. mpl::if_c< \
  318. mpl::or_< \
  319. typename is_c_array<Lhs>::type, \
  320. typename is_c_array<Rhs>::type \
  321. >::value, \
  322. mpl::true_, \
  323. typename \
  324. mpl::if_c<is_same<typename decay<Lhs>::type, \
  325. typename decay<Rhs>::type>::value, \
  326. typename cctraits<OP>::is_specialized, \
  327. mpl::false_>::type \
  328. >::type is_specialized; \
  329. \
  330. typedef name<typename t_Lhs_iterator_helper::value_type, \
  331. typename t_Rhs_iterator_helper::value_type \
  332. > elem_op; \
  333. \
  334. static assertion_result \
  335. eval( Lhs const& lhs, Rhs const& rhs) \
  336. { \
  337. return assertion::op::compare_collections( lhs, rhs, \
  338. (boost::type<elem_op>*)0, \
  339. is_specialized() ); \
  340. } \
  341. \
  342. template<typename PrevExprType> \
  343. static void \
  344. report( std::ostream&, \
  345. PrevExprType const&, \
  346. Rhs const& ) {} \
  347. \
  348. static char const* revert() \
  349. { return " " #rev " "; } \
  350. \
  351. }; \
  352. /**/
  353. BOOST_TEST_FOR_EACH_COMP_OP( DEFINE_COLLECTION_COMPARISON )
  354. #undef DEFINE_COLLECTION_COMPARISON
  355. //____________________________________________________________________________//
  356. } // namespace op
  357. } // namespace assertion
  358. } // namespace test_tools
  359. } // namespace boost
  360. #include <boost/test/detail/enable_warnings.hpp>
  361. #endif // BOOST_TEST_TOOLS_COLLECTION_COMPARISON_OP_HPP_050815GER