/package/boost_1_58_0/libs/phoenix/test/algorithm/querying.cpp

https://gitlab.com/cdeclare/intcrypt · C++ · 293 lines · 243 code · 41 blank · 9 comment · 35 complexity · 5d788df2172883f4e6e16f2f550d1649 MD5 · raw file

  1. /*=============================================================================
  2. Copyright (c) 2005 Dan Marsden
  3. Copyright (c) 2005-2007 Joel de Guzman
  4. Copyright (c) 2007 Hartmut Kaiser
  5. Copyright (c) 2015 John Fletcher
  6. Distributed under the Boost Software License, Version 1.0. (See accompanying
  7. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  8. ==============================================================================*/
  9. #include <boost/phoenix/core.hpp>
  10. #include <boost/phoenix/stl/algorithm/querying.hpp>
  11. #include <boost/detail/lightweight_test.hpp>
  12. #include <boost/assign/list_of.hpp>
  13. #include <boost/config.hpp>
  14. #ifdef BOOST_HAS_HASH
  15. #define _GLIBCXX_PERMIT_BACKWARD_HASH
  16. #include BOOST_HASH_SET_HEADER
  17. #include BOOST_HASH_MAP_HEADER
  18. #define BOOST_PHOENIX_HAS_HASH
  19. #define BOOST_PHOENIX_HASH_NAMESPACE BOOST_STD_EXTENSION_NAMESPACE
  20. #elif defined(BOOST_DINKUMWARE_STDLIB) && (BOOST_DINKUMWARE_STDLIB < 610)
  21. #include <hash_set>
  22. #include <hash_map>
  23. #define BOOST_PHOENIX_HAS_HASH
  24. #define BOOST_PHOENIX_HASH_NAMESPACE stdext
  25. #endif
  26. #include <set>
  27. #include <map>
  28. #include <functional>
  29. namespace
  30. {
  31. struct even
  32. {
  33. bool operator()(const int i) const
  34. {
  35. return i % 2 == 0;
  36. }
  37. };
  38. struct mod_2_comparison
  39. {
  40. bool operator()(
  41. const int lhs,
  42. const int rhs)
  43. {
  44. return lhs % 2 == rhs % 2;
  45. }
  46. };
  47. void find_test()
  48. {
  49. using boost::phoenix::arg_names::arg1;
  50. int array[] = {1,2,3};
  51. BOOST_TEST(boost::phoenix::find(arg1,2)(array) == array + 1);
  52. std::set<int> s(array, array + 3);
  53. BOOST_TEST(boost::phoenix::find(arg1, 2)(s) == s.find(2));
  54. #if !(defined(BOOST_MSVC) && (BOOST_MSVC >= 1900))
  55. std::map<int, int> m = boost::assign::map_list_of(0, 1)(2, 3)(4, 5);
  56. BOOST_TEST(boost::phoenix::find(arg1, 2)(m) == m.find(2));
  57. #endif
  58. #ifdef BOOST_PHOENIX_HAS_HASH
  59. BOOST_PHOENIX_HASH_NAMESPACE::hash_set<int> hs(array, array + 3);
  60. BOOST_TEST(boost::phoenix::find(arg1, 2)(hs) == hs.find(2));
  61. BOOST_PHOENIX_HASH_NAMESPACE::hash_map<int, int> hm = boost::assign::map_list_of(0, 1)(2, 3)(4, 5);
  62. BOOST_TEST(boost::phoenix::find(arg1, 2)(hm) == hm.find(2));
  63. #endif
  64. return;
  65. }
  66. void find_if_test()
  67. {
  68. using boost::phoenix::arg_names::arg1;
  69. int array[] = {1,2,3};
  70. BOOST_TEST(boost::phoenix::find_if(arg1, even())(array) == array + 1);
  71. return;
  72. }
  73. void find_end_test()
  74. {
  75. using boost::phoenix::arg_names::arg1;
  76. using boost::phoenix::arg_names::arg2;
  77. int array[] = {1,2,3,1,2,3,1};
  78. int pattern[] = {1,2,3};
  79. BOOST_TEST(boost::phoenix::find_end(arg1, arg2)(array, pattern) == array + 3);
  80. int pattern2[] = {5,6,5};
  81. BOOST_TEST(boost::phoenix::find_end(arg1, arg2, mod_2_comparison())(array, pattern2) == array + 3);
  82. return;
  83. }
  84. void find_first_of_test()
  85. {
  86. using boost::phoenix::arg_names::arg1;
  87. using boost::phoenix::arg_names::arg2;
  88. int array[] = {1,2,3};
  89. int search_for[] = {2,3,4};
  90. BOOST_TEST(boost::phoenix::find_first_of(arg1, arg2)(array, search_for) == array + 1);
  91. int search_for2[] = {0};
  92. BOOST_TEST(boost::phoenix::find_first_of(arg1, arg2, mod_2_comparison())(array, search_for2) == array + 1);
  93. return;
  94. }
  95. void adjacent_find_test()
  96. {
  97. using boost::phoenix::arg_names::arg1;
  98. int array[] = {0,1,3,4,4};
  99. BOOST_TEST(boost::phoenix::adjacent_find(arg1)(array) == array + 3);
  100. BOOST_TEST(boost::phoenix::adjacent_find(arg1, mod_2_comparison())(array) == array + 1);
  101. return;
  102. }
  103. void count_test()
  104. {
  105. using boost::phoenix::arg_names::arg1;
  106. int array[] = {1,1,0,1,1};
  107. BOOST_TEST(boost::phoenix::count(arg1, 1)(array) == 4);
  108. return;
  109. }
  110. void count_if_test()
  111. {
  112. using boost::phoenix::arg_names::arg1;
  113. int array[] = {1,2,3,4,5};
  114. BOOST_TEST(boost::phoenix::count_if(arg1, even())(array) == 2);
  115. return;
  116. }
  117. void distance_test()
  118. {
  119. using boost::phoenix::arg_names::arg1;
  120. int array[] = {1,1,0,1,1};
  121. BOOST_TEST(boost::phoenix::distance(arg1)(array) == 5);
  122. return;
  123. }
  124. void mismatch_test()
  125. {
  126. using boost::phoenix::arg_names::arg1;
  127. using boost::phoenix::arg_names::arg2;
  128. int array[] = {1,2,3,4,5};
  129. int search[] = {1,2,4};
  130. BOOST_TEST(
  131. boost::phoenix::mismatch(arg1, arg2)(array, search) ==
  132. std::make_pair(array + 2, search + 2));
  133. int search2[] = {1,2,1,1};
  134. BOOST_TEST(
  135. boost::phoenix::mismatch(arg1, arg2, mod_2_comparison())(array, search2)
  136. == std::make_pair(array + 3, search2 + 3));
  137. return;
  138. }
  139. void equal_test()
  140. {
  141. using boost::phoenix::arg_names::arg1;
  142. using boost::phoenix::arg_names::arg2;
  143. int array[] = {1,2,3};
  144. int array2[] = {1,2,3};
  145. int array3[] = {1,2,4};
  146. BOOST_TEST(
  147. boost::phoenix::equal(arg1, arg2)(array, array2));
  148. BOOST_TEST(
  149. !boost::phoenix::equal(arg1, arg2)(array, array3));
  150. BOOST_TEST(
  151. boost::phoenix::equal(arg1, arg2, mod_2_comparison())(array, array2));
  152. BOOST_TEST(
  153. !boost::phoenix::equal(arg1, arg2, mod_2_comparison())(array, array3));
  154. return;
  155. }
  156. void search_test()
  157. {
  158. using boost::phoenix::arg_names::arg1;
  159. using boost::phoenix::arg_names::arg2;
  160. int array[] = {1,2,3,1,2,3};
  161. int pattern[] = {2,3};
  162. BOOST_TEST(
  163. boost::phoenix::search(arg1, arg2)(array, pattern) == array + 1);
  164. int pattern2[] = {1,1};
  165. BOOST_TEST(
  166. boost::phoenix::search(arg1, arg2, mod_2_comparison())(array, pattern2) == array + 2);
  167. return;
  168. }
  169. void lower_bound_test()
  170. {
  171. using boost::phoenix::arg_names::arg1;
  172. int array[] = {1,2,3};
  173. const std::set<int> test_set(array, array + 3);
  174. BOOST_TEST(boost::phoenix::lower_bound(arg1, 2)(array) == array + 1);
  175. BOOST_TEST(boost::phoenix::lower_bound(arg1, 2)(test_set) == test_set.lower_bound(2));
  176. int array2[] = {3,2,1};
  177. const std::set<int, std::greater<int> > test_set2(array2, array2 + 3);
  178. BOOST_TEST(boost::phoenix::lower_bound(arg1, 2, std::greater<int>())(array2) ==
  179. array2 + 1);
  180. BOOST_TEST(boost::phoenix::lower_bound(arg1, 2, std::greater<int>())(test_set2) ==
  181. test_set2.lower_bound(2));
  182. return;
  183. }
  184. void upper_bound_test()
  185. {
  186. using boost::phoenix::arg_names::arg1;
  187. int array[] = {1,2,3};
  188. const std::set<int> test_set(array, array + 3);
  189. BOOST_TEST(upper_bound(arg1, 2)(array) == array + 2);
  190. BOOST_TEST(upper_bound(arg1, 2)(test_set) == test_set.upper_bound(2));
  191. int array2[] = {3,2,1};
  192. const std::set<int, std::greater<int> > test_set2(array2, array2 + 3);
  193. BOOST_TEST(boost::phoenix::upper_bound(arg1, 2, std::greater<int>())(array2) ==
  194. array2 + 2);
  195. BOOST_TEST(boost::phoenix::upper_bound(arg1, 2, std::greater<int>())(test_set2) ==
  196. test_set2.upper_bound(2));
  197. return;
  198. }
  199. void equal_range_test()
  200. {
  201. using boost::phoenix::arg_names::arg1;
  202. int array[] = {1,2,2,3};
  203. const std::set<int> test_set(array, array + 4);
  204. BOOST_TEST(boost::phoenix::equal_range(arg1, 2)(array).first ==
  205. array + 1);
  206. BOOST_TEST(boost::phoenix::equal_range(arg1, 2)(array).second ==
  207. array + 3);
  208. BOOST_TEST(boost::phoenix::equal_range(arg1, 2)(test_set).first ==
  209. test_set.equal_range(2).first);
  210. BOOST_TEST(boost::phoenix::equal_range(arg1, 2)(test_set).second ==
  211. test_set.equal_range(2).second);
  212. int array2[] = {3,2,2,1};
  213. const std::set<int, std::greater<int> > test_set2(array2, array2 + 4);
  214. BOOST_TEST(boost::phoenix::equal_range(arg1, 2, std::greater<int>())(array2).first ==
  215. array2 + 1);
  216. BOOST_TEST(boost::phoenix::equal_range(arg1, 2, std::greater<int>())(array2).second ==
  217. array2 + 3);
  218. BOOST_TEST(boost::phoenix::equal_range(arg1, 2, std::greater<int>())(test_set2).first ==
  219. test_set2.equal_range(2).first);
  220. BOOST_TEST(boost::phoenix::equal_range(arg1, 2, std::greater<int>())(test_set2).second ==
  221. test_set2.equal_range(2).second);
  222. return;
  223. }
  224. void binary_search_test()
  225. {
  226. using boost::phoenix::arg_names::arg1;
  227. int array[] = {1,2,3};
  228. BOOST_TEST(boost::phoenix::binary_search(arg1, 2)(array));
  229. BOOST_TEST(!boost::phoenix::binary_search(arg1, 4)(array));
  230. return;
  231. }
  232. }
  233. int main()
  234. {
  235. find_test();
  236. find_if_test();
  237. find_end_test();
  238. find_first_of_test();
  239. adjacent_find_test();
  240. count_test();
  241. count_if_test();
  242. distance_test();
  243. mismatch_test();
  244. equal_test();
  245. search_test();
  246. lower_bound_test();
  247. upper_bound_test();
  248. equal_range_test();
  249. binary_search_test();
  250. return boost::report_errors();
  251. }