/Src/Dependencies/Boost/libs/range/test/algorithm.cpp

http://hadesmem.googlecode.com/ · C++ · 471 lines · 295 code · 97 blank · 79 comment · 3 complexity · 612f8eafdc6e9140f7be55c11d804adf MD5 · raw file

  1. // Boost.Range library
  2. //
  3. // Copyright Thorsten Ottosen 2006. Use, modification and
  4. // distribution is subject to the Boost Software License, Version
  5. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // For more information, see http://www.boost.org/libs/range/
  9. //
  10. // (C) Copyright Eric Niebler 2004.
  11. // Use, modification and distribution are subject to the
  12. // Boost Software License, Version 1.0. (See accompanying file
  13. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  14. /*
  15. Revision history:
  16. 13 December 2004 : Initial version.
  17. */
  18. #ifdef _MSC_VER
  19. // The 'secure' library warnings produce so much noise that it makes it
  20. // impossible to see more useful warnings.
  21. #define _SCL_SECURE_NO_WARNINGS
  22. #endif
  23. #ifdef _MSC_VER
  24. // counting_iterator generates a warning about truncating an integer
  25. #pragma warning(push)
  26. #pragma warning(disable : 4244)
  27. #endif
  28. #include <boost/iterator/counting_iterator.hpp>
  29. #ifdef _MSC_VER
  30. template ::boost::counting_iterator<int>;
  31. #pragma warning(pop)
  32. #endif
  33. #include <boost/assign.hpp>
  34. #include <boost/array.hpp>
  35. #include <boost/bind.hpp>
  36. #include <boost/range/numeric.hpp>
  37. #include <boost/range/algorithm.hpp>
  38. #include <boost/range/value_type.hpp>
  39. #include <boost/range/size_type.hpp>
  40. #include <boost/range/size.hpp>
  41. #include <boost/test/test_tools.hpp>
  42. #include <boost/test/unit_test.hpp>
  43. #include <boost/iterator/iterator_traits.hpp>
  44. #include <algorithm>
  45. #include <cstdlib>
  46. #include <set>
  47. #include <list>
  48. #include <vector>
  49. #include <iterator>
  50. #include <functional>
  51. ///////////////////////////////////////////////////////////////////////////////
  52. // dummy function object, used with algorithms
  53. //
  54. struct null_fun
  55. {
  56. template<typename T>
  57. void operator()(T const &t) const
  58. {
  59. }
  60. };
  61. ///////////////////////////////////////////////////////////////////////////////
  62. // dummy predicate, used with algorithms
  63. //
  64. struct null_pred
  65. {
  66. template<typename T>
  67. bool operator()(T const &t) const
  68. {
  69. return t == T();
  70. }
  71. };
  72. ///////////////////////////////////////////////////////////////////////////////
  73. // dummy unary op, used with algorithms
  74. //
  75. struct null_op1
  76. {
  77. template<typename T>
  78. T const & operator()(T const & t) const
  79. {
  80. return t;
  81. }
  82. };
  83. ///////////////////////////////////////////////////////////////////////////////
  84. // dummy binary op, used with algorithms
  85. //
  86. struct null_op2
  87. {
  88. template<typename T,typename U>
  89. T const & operator()(T const & t, U const & u) const
  90. {
  91. return t;
  92. }
  93. };
  94. template<typename Rng>
  95. void test_random_algorithms(Rng & rng, std::random_access_iterator_tag)
  96. {
  97. typedef BOOST_DEDUCED_TYPENAME boost::range_iterator<Rng>::type iterator;
  98. typedef BOOST_DEDUCED_TYPENAME boost::range_value<Rng>::type value_type;
  99. typedef BOOST_DEDUCED_TYPENAME boost::range_size<Rng>::type size_type;
  100. typedef BOOST_DEDUCED_TYPENAME boost::iterator_category<iterator>::type iterator_category;
  101. // just make sure these compile (for now)
  102. if(0)
  103. {
  104. boost::random_shuffle(rng);
  105. // Must be a value since random_shuffle must take the generator by
  106. // reference to match the standard.
  107. null_op1 rng_generator;
  108. boost::random_shuffle(rng, rng_generator);
  109. boost::sort(rng);
  110. boost::sort(rng, std::less<value_type>());
  111. boost::stable_sort(rng);
  112. boost::stable_sort(rng, std::less<value_type>());
  113. boost::partial_sort(rng, boost::begin(rng));
  114. boost::partial_sort(rng, boost::begin(rng), std::less<value_type>());
  115. boost::nth_element(rng, boost::begin(rng));
  116. boost::nth_element(rng, boost::begin(rng), std::less<value_type>());
  117. boost::push_heap(rng);
  118. boost::push_heap(rng, std::less<value_type>());
  119. boost::pop_heap(rng);
  120. boost::pop_heap(rng, std::less<value_type>());
  121. boost::make_heap(rng);
  122. boost::make_heap(rng, std::less<value_type>());
  123. boost::sort_heap(rng);
  124. boost::sort_heap(rng, std::less<value_type>());
  125. }
  126. }
  127. template<typename Rng>
  128. void test_random_algorithms(Rng & rng, std::input_iterator_tag)
  129. {
  130. // no-op
  131. }
  132. template<typename Rng>
  133. void test_algorithms(Rng & rng)
  134. {
  135. typedef BOOST_DEDUCED_TYPENAME boost::range_iterator<Rng>::type iterator;
  136. typedef BOOST_DEDUCED_TYPENAME boost::range_value<Rng>::type value_type;
  137. typedef BOOST_DEDUCED_TYPENAME boost::range_size<Rng>::type size_type;
  138. typedef BOOST_DEDUCED_TYPENAME boost::iterator_category<iterator>::type iterator_category;
  139. // just make sure these compile (for now)
  140. if(0)
  141. {
  142. value_type val = value_type();
  143. value_type rng2[] = {value_type(),value_type(),value_type()};
  144. typedef value_type* iterator2;
  145. value_type out[100] = {};
  146. typedef value_type* out_iterator;
  147. null_fun f = null_fun();
  148. iterator i = iterator();
  149. bool b = bool();
  150. out_iterator o = out_iterator();
  151. size_type s = size_type();
  152. f = boost::for_each(rng, null_fun());
  153. i = boost::find(rng, val);
  154. i = boost::find_if(rng, null_pred());
  155. i = boost::find_end(rng, rng2);
  156. i = boost::find_end(rng, rng2, std::equal_to<value_type>());
  157. i = boost::find_first_of(rng, rng2);
  158. i = boost::find_first_of(rng, rng2, std::equal_to<value_type>());
  159. i = boost::adjacent_find(rng);
  160. i = boost::adjacent_find(rng, std::equal_to<value_type>());
  161. s = boost::count(rng, val);
  162. s = boost::count_if(rng, null_pred());
  163. std::pair<iterator,iterator2> p1;
  164. p1 = boost::mismatch(rng, rng2);
  165. p1 = boost::mismatch(rng, rng2, std::equal_to<value_type>());
  166. b = boost::equal(rng, rng2);
  167. b = boost::equal(rng, rng2, std::equal_to<value_type>());
  168. i = boost::search(rng, rng2);
  169. i = boost::search(rng, rng2, std::equal_to<value_type>());
  170. o = boost::copy(rng, boost::begin(out));
  171. o = boost::copy_backward(rng, boost::end(out));
  172. o = boost::transform(rng, boost::begin(out), null_op1());
  173. o = boost::transform(rng, rng2, boost::begin(out), null_op2());
  174. boost::replace(rng, val, val);
  175. boost::replace_if(rng, null_pred(), val);
  176. /*
  177. o = boost::replace_copy(rng, boost::begin(out), val, val);
  178. o = boost::replace_copy_if(rng, boost::begin(out), null_pred(), val);
  179. */
  180. boost::fill(rng, val);
  181. //
  182. // size requires RandomAccess
  183. //
  184. //boost::fill_n(rng, boost::size(rng), val);
  185. //boost::fill_n(rng, std::distance(boost::begin(rng),boost::end(rng)),val);
  186. boost::generate(rng, &std::rand);
  187. //
  188. // size requires RandomAccess
  189. //
  190. //boost::generate_n(rng, boost::size(rng), &std::rand);
  191. //boost::generate_n(rng,std::distance(boost::begin(rng),boost::end(rng)), &std::rand);
  192. i = boost::remove(rng, val);
  193. i = boost::remove_if(rng, null_pred());
  194. /*
  195. o = boost::remove_copy(rng, boost::begin(out), val);
  196. o = boost::remove_copy_if(rng, boost::begin(out), null_pred());
  197. */
  198. typename boost::range_return<Rng, boost::return_begin_found>::type rrng = boost::unique(rng);
  199. rrng = boost::unique(rng, std::equal_to<value_type>());
  200. /*
  201. o = boost::unique_copy(rng, boost::begin(out));
  202. o = boost::unique_copy(rng, boost::begin(out), std::equal_to<value_type>());
  203. */
  204. boost::reverse(rng);
  205. /*
  206. o = boost::reverse_copy(rng, boost::begin(out));
  207. */
  208. boost::rotate(rng, boost::begin(rng));
  209. /*
  210. o = boost::rotate_copy(rng, boost::begin(rng), boost::begin(out));
  211. */
  212. i = boost::partition(rng, null_pred());
  213. i = boost::stable_partition(rng, null_pred());
  214. /*
  215. o = boost::partial_sort_copy(rng, out);
  216. o = boost::partial_sort_copy(rng, out, std::less<value_type>());
  217. */
  218. i = boost::lower_bound(rng, val);
  219. i = boost::lower_bound(rng, val, std::less<value_type>());
  220. i = boost::upper_bound(rng, val);
  221. i = boost::upper_bound(rng, val, std::less<value_type>());
  222. std::pair<iterator,iterator> p2;
  223. p2 = boost::equal_range(rng, val);
  224. p2 = boost::equal_range(rng, val, std::less<value_type>());
  225. b = boost::binary_search(rng, val);
  226. b = boost::binary_search(rng, val, std::less<value_type>());
  227. boost::inplace_merge(rng, boost::begin(rng));
  228. boost::inplace_merge(rng, boost::begin(rng), std::less<value_type>());
  229. b = boost::includes(rng, rng2);
  230. b = boost::includes(rng, rng2, std::equal_to<value_type>());
  231. o = boost::set_union(rng, rng2, boost::begin(out));
  232. o = boost::set_union(rng, rng2, boost::begin(out), std::equal_to<value_type>());
  233. o = boost::set_intersection(rng, rng2, boost::begin(out));
  234. o = boost::set_intersection(rng, rng2, boost::begin(out), std::equal_to<value_type>());
  235. o = boost::set_difference(rng, rng2, boost::begin(out));
  236. o = boost::set_difference(rng, rng2, boost::begin(out), std::equal_to<value_type>());
  237. o = boost::set_symmetric_difference(rng, rng2, boost::begin(out));
  238. o = boost::set_symmetric_difference(rng, rng2, boost::begin(out), std::equal_to<value_type>());
  239. i = boost::min_element(rng);
  240. i = boost::min_element(rng, std::less<value_type>());
  241. i = boost::max_element(rng);
  242. i = boost::max_element(rng, std::less<value_type>());
  243. b = boost::lexicographical_compare(rng, rng);
  244. b = boost::lexicographical_compare(rng, rng, std::equal_to<value_type>());
  245. b = boost::next_permutation(rng);
  246. b = boost::next_permutation(rng, std::less<value_type>());
  247. b = boost::prev_permutation(rng);
  248. b = boost::prev_permutation(rng, std::less<value_type>());
  249. /////////////////////////////////////////////////////////////////////
  250. // numeric algorithms
  251. /////////////////////////////////////////////////////////////////////
  252. val = boost::accumulate( rng, val );
  253. val = boost::accumulate( rng, val, null_op2() );
  254. val = boost::inner_product( rng, rng, val );
  255. val = boost::inner_product( rng, rng, val,
  256. null_op2(), null_op2() );
  257. o = boost::partial_sum( rng, boost::begin(out) );
  258. o = boost::partial_sum( rng, boost::begin(out), null_op2() );
  259. o = boost::adjacent_difference( rng, boost::begin(out) );
  260. o = boost::adjacent_difference( rng, boost::begin(out),
  261. null_op2() );
  262. }
  263. // test the algorithms that require a random-access range
  264. test_random_algorithms(rng, iterator_category());
  265. }
  266. int* addr(int &i) { return &i; }
  267. bool true_(int) { return true; }
  268. ///////////////////////////////////////////////////////////////////////////////
  269. // test_main
  270. //
  271. void simple_compile_test()
  272. {
  273. // int_iterator
  274. typedef ::boost::counting_iterator<int> int_iterator;
  275. // define come containers
  276. std::list<int> my_list(int_iterator(1),int_iterator(6));
  277. std::vector<int> my_vector(int_iterator(1),int_iterator(6));
  278. std::pair<std::vector<int>::iterator,std::vector<int>::iterator> my_pair(my_vector.begin(),my_vector.end());
  279. // test the algorithms with list and const list
  280. test_algorithms(my_list);
  281. test_algorithms(my_vector);
  282. test_algorithms(my_pair);
  283. std::vector<int> v;
  284. std::vector<int>& cv = v;
  285. using namespace boost;
  286. #define BOOST_RANGE_RETURNS_TEST( function_name, cont ) \
  287. function_name (cont); \
  288. function_name <return_found> (cont); \
  289. function_name <return_next> (cont); \
  290. function_name <return_prior> (cont); \
  291. function_name <return_begin_found> (cont); \
  292. function_name <return_begin_next> (cont); \
  293. function_name <return_begin_prior> (cont); \
  294. function_name <return_found_end> (cont); \
  295. function_name <return_next_end>(cont); \
  296. function_name <return_prior_end>(cont);
  297. BOOST_RANGE_RETURNS_TEST( adjacent_find, cv );
  298. BOOST_RANGE_RETURNS_TEST( adjacent_find, v );
  299. BOOST_RANGE_RETURNS_TEST( max_element, cv );
  300. BOOST_RANGE_RETURNS_TEST( max_element, v );
  301. BOOST_RANGE_RETURNS_TEST( min_element, cv );
  302. BOOST_RANGE_RETURNS_TEST( min_element, v );
  303. BOOST_RANGE_RETURNS_TEST( unique, v );
  304. #undef BOOST_RANGE_RETURNS_TEST
  305. #define BOOST_RANGE_RETURNS_TEST1( function_name, cont, arg1 ) \
  306. function_name (cont, arg1); \
  307. function_name <return_found> (cont, arg1); \
  308. function_name <return_next> (cont, arg1); \
  309. function_name <return_prior> (cont, arg1); \
  310. function_name <return_begin_found> (cont, arg1); \
  311. function_name <return_begin_next> (cont, arg1); \
  312. function_name <return_begin_prior> (cont, arg1); \
  313. function_name <return_found_end> (cont, arg1); \
  314. function_name <return_next_end>(cont, arg1); \
  315. function_name <return_prior_end>(cont, arg1);
  316. BOOST_RANGE_RETURNS_TEST1( adjacent_find, cv, std::less<int>() );
  317. BOOST_RANGE_RETURNS_TEST1( adjacent_find, v, std::less<int>() );
  318. BOOST_RANGE_RETURNS_TEST1( find, cv, 0 );
  319. BOOST_RANGE_RETURNS_TEST1( find, v, 0 );
  320. BOOST_RANGE_RETURNS_TEST1( find_end, cv, cv );
  321. BOOST_RANGE_RETURNS_TEST1( find_end, cv, v );
  322. BOOST_RANGE_RETURNS_TEST1( find_end, v, cv );
  323. BOOST_RANGE_RETURNS_TEST1( find_end, v, v );
  324. BOOST_RANGE_RETURNS_TEST1( find_first_of, cv, cv );
  325. BOOST_RANGE_RETURNS_TEST1( find_first_of, cv, v );
  326. BOOST_RANGE_RETURNS_TEST1( find_first_of, v, cv );
  327. BOOST_RANGE_RETURNS_TEST1( find_first_of, v, v );
  328. BOOST_RANGE_RETURNS_TEST1( find_if, cv, std::negate<int>() );
  329. BOOST_RANGE_RETURNS_TEST1( find_if, v, std::negate<int>() );
  330. BOOST_RANGE_RETURNS_TEST1( search, cv, cv );
  331. BOOST_RANGE_RETURNS_TEST1( search, cv, v );
  332. BOOST_RANGE_RETURNS_TEST1( search, v, cv );
  333. BOOST_RANGE_RETURNS_TEST1( search, v, v );
  334. BOOST_RANGE_RETURNS_TEST1( remove, v, 0 );
  335. BOOST_RANGE_RETURNS_TEST1( remove_if, v, std::negate<int>() );
  336. BOOST_RANGE_RETURNS_TEST1( lower_bound, cv, 0 );
  337. BOOST_RANGE_RETURNS_TEST1( lower_bound, v, 0 );
  338. BOOST_RANGE_RETURNS_TEST1( max_element, cv, std::less<int>() );
  339. BOOST_RANGE_RETURNS_TEST1( max_element, v, std::less<int>() );
  340. BOOST_RANGE_RETURNS_TEST1( min_element, cv, std::less<int>() );
  341. BOOST_RANGE_RETURNS_TEST1( min_element, v, std::less<int>() );
  342. BOOST_RANGE_RETURNS_TEST1( upper_bound, cv, 0 );
  343. BOOST_RANGE_RETURNS_TEST1( upper_bound, v, 0 );
  344. BOOST_RANGE_RETURNS_TEST1( partition, cv, std::negate<int>() );
  345. BOOST_RANGE_RETURNS_TEST1( partition, v, std::negate<int>() );
  346. BOOST_RANGE_RETURNS_TEST1( stable_partition, cv, std::negate<int>() );
  347. BOOST_RANGE_RETURNS_TEST1( stable_partition, v, std::negate<int>() );
  348. #undef BOOST_RANGE_RETURNS_TEST1
  349. #define BOOST_RANGE_RETURNS_TEST2( function_name, arg1, arg2 ) \
  350. function_name (v, arg1, arg2); \
  351. function_name <return_found> (v, arg1, arg2); \
  352. function_name <return_next> (v, arg1, arg2); \
  353. function_name <return_prior> (v, arg1, arg2); \
  354. function_name <return_begin_found> (v, arg1, arg2); \
  355. function_name <return_begin_next> (v, arg1, arg2); \
  356. function_name <return_begin_prior> (v, arg1, arg2); \
  357. function_name <return_found_end> (v, arg1, arg2); \
  358. function_name <return_next_end>(v, arg1, arg2); \
  359. function_name <return_prior_end>(v, arg1, arg2);
  360. BOOST_RANGE_RETURNS_TEST2( find_end, v, std::less<int>() );
  361. BOOST_RANGE_RETURNS_TEST2( find_first_of, v, std::less<int>() );
  362. BOOST_RANGE_RETURNS_TEST2( search, v, std::less<int>() );
  363. BOOST_RANGE_RETURNS_TEST2( lower_bound, 0, std::less<int>() );
  364. BOOST_RANGE_RETURNS_TEST2( upper_bound, 0, std::less<int>() );
  365. #undef BOOST_RANGE_RETURNS_TEST2
  366. }
  367. using boost::unit_test::test_suite;
  368. test_suite* init_unit_test_suite( int argc, char* argv[] )
  369. {
  370. using namespace boost;
  371. test_suite* test = BOOST_TEST_SUITE( "Range Test Suite - Algorithm" );
  372. test->add( BOOST_TEST_CASE( &simple_compile_test ) );
  373. return test;
  374. }