querying_find.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. #include BOOST_PHOENIX_HASH_SET_HEADER
  16. #include BOOST_PHOENIX_HASH_MAP_HEADER
  17. #endif
  18. #ifdef BOOST_PHOENIX_HAS_UNORDERED_SET_AND_MAP
  19. #include BOOST_PHOENIX_UNORDERED_SET_HEADER
  20. #include BOOST_PHOENIX_UNORDERED_MAP_HEADER
  21. #endif
  22. #include <set>
  23. #include <map>
  24. #include <functional>
  25. namespace
  26. {
  27. struct even
  28. {
  29. bool operator()(const int i) const
  30. {
  31. return i % 2 == 0;
  32. }
  33. };
  34. struct mod_2_comparison
  35. {
  36. bool operator()(
  37. const int lhs,
  38. const int rhs)
  39. {
  40. return lhs % 2 == rhs % 2;
  41. }
  42. };
  43. void find_test()
  44. {
  45. using boost::phoenix::arg_names::arg1;
  46. int array[] = {1,2,3};
  47. BOOST_TEST(boost::phoenix::find(arg1,2)(array) == array + 1);
  48. std::set<int> s(array, array + 3);
  49. BOOST_TEST(boost::phoenix::find(arg1, 2)(s) == s.find(2));
  50. //#if !(defined(BOOST_MSVC) && (BOOST_MSVC >= 1900))
  51. //std::map<int, int> m = boost::assign::map_list_of(0, 1)(2, 3)(4, 5).to_container(m);
  52. std::map<int, int> m = boost::assign::map_list_of(0, 1)(2, 3)(4, 5).
  53. convert_to_container<std::map<int, int> >();
  54. BOOST_TEST(boost::phoenix::find(arg1, 2)(m) == m.find(2));
  55. //#endif
  56. #ifdef BOOST_PHOENIX_HAS_HASH
  57. BOOST_PHOENIX_HASH_NAMESPACE::hash_set<int> hs(array, array + 3);
  58. BOOST_TEST(boost::phoenix::find(arg1, 2)(hs) == hs.find(2));
  59. BOOST_PHOENIX_HASH_NAMESPACE::hash_map<int, int> hm = boost::assign::map_list_of(0, 1)(2, 3)(4, 5).
  60. convert_to_container<BOOST_PHOENIX_HASH_NAMESPACE::hash_map<int, int> >();
  61. BOOST_TEST(boost::phoenix::find(arg1, 2)(hm) == hm.find(2));
  62. #endif
  63. #ifdef BOOST_PHOENIX_HAS_UNORDERED_SET_AND_MAP
  64. std::unordered_set<int> us(array, array + 3);
  65. BOOST_TEST(boost::phoenix::find(arg1, 2)(us) == us.find(2));
  66. std::unordered_map<int, int> um = boost::assign::map_list_of(0, 1)(2, 3)(4, 5).
  67. convert_to_container<std::unordered_map<int, int> >();
  68. BOOST_TEST(boost::phoenix::find(arg1, 2)(um) == um.find(2));
  69. #endif
  70. return;
  71. }
  72. }
  73. int main()
  74. {
  75. find_test();
  76. return boost::report_errors();
  77. }