map_copy.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*=============================================================================
  2. Copyright (c) 1999-2003 Jaakko Jarvi
  3. Copyright (c) 2001-2011 Joel de Guzman
  4. Copyright (c) 2006 Dan Marsden
  5. Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. ==============================================================================*/
  8. #include <boost/fusion/container/map/map.hpp>
  9. #include <boost/fusion/container/generation/make_map.hpp>
  10. #include <boost/fusion/container/generation/map_tie.hpp>
  11. #include <boost/detail/lightweight_test.hpp>
  12. #include <boost/fusion/sequence/intrinsic/at.hpp>
  13. #include <boost/fusion/mpl.hpp>
  14. #include <boost/preprocessor/cat.hpp>
  15. #include <boost/mpl/insert_range.hpp>
  16. #include <boost/mpl/vector.hpp>
  17. #include <boost/mpl/begin.hpp>
  18. #include <boost/mpl/equal.hpp>
  19. #include <boost/static_assert.hpp>
  20. #include <string>
  21. struct k1 {};
  22. struct k2 {};
  23. struct k3 {};
  24. struct k4 {};
  25. namespace test_detail
  26. {
  27. // classes with different kinds of conversions
  28. class AA {};
  29. class BB : public AA {};
  30. struct CC { CC() {} CC(const BB&) {} };
  31. struct DD { operator CC() const { return CC(); }; };
  32. }
  33. boost::fusion::map<
  34. boost::fusion::pair<k1, double>,
  35. boost::fusion::pair<k2, double>,
  36. boost::fusion::pair<k3, double>,
  37. boost::fusion::pair<k4, double>
  38. >
  39. foo(int i)
  40. {
  41. return boost::fusion::make_map<k1, k2, k3, k4>(i, i+1, i+2, i+3);
  42. }
  43. void
  44. test()
  45. {
  46. using namespace boost::fusion;
  47. using namespace test_detail;
  48. map<pair<k1, int>, pair<k2, char> > t1(4, 'a');
  49. map<pair<k1, int>, pair<k2, char> > t2(5, 'b');
  50. t2 = t1;
  51. BOOST_TEST(at_c<0>(t1).second == at_c<0>(t2).second);
  52. BOOST_TEST(at_c<1>(t1).second == at_c<1>(t2).second);
  53. map<pair<k1, int>, pair<k2, char const*> > t4(4, "a");
  54. map<pair<k1, long>, pair<k2, std::string> > t3(2, std::string("a"));
  55. t3 = t4;
  56. BOOST_TEST((double)at_c<0>(t4).second == at_c<0>(t3).second);
  57. BOOST_TEST(at_c<1>(t4).second == at_c<1>(t3).second);
  58. // testing copy and assignment with implicit conversions
  59. // between elements testing tie
  60. map<pair<k1, char>, pair<k2, BB*>, pair<k3, BB>, pair<k4, DD> > t;
  61. map<pair<k1, int>, pair<k2, AA*>, pair<k3, CC>, pair<k4, CC> > a(t);
  62. a = t;
  63. int i; char c; double d;
  64. map_tie<k1, k2, k3>(i, c, d) = make_map<k1, k2, k3>(1, 'a', 5.5);
  65. BOOST_TEST(i==1);
  66. BOOST_TEST(c=='a');
  67. BOOST_TEST(d>5.4 && d<5.6);
  68. // returning a map with conversion
  69. foo(2);
  70. }
  71. int
  72. main()
  73. {
  74. test();
  75. return boost::report_errors();
  76. }