querying.cpp 8.9 KB

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