cons.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  3. Copyright (c) 2005 Eric Niebler
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ==============================================================================*/
  7. #include <string>
  8. #include <boost/detail/lightweight_test.hpp>
  9. #include <boost/fusion/container/list/cons.hpp>
  10. #include <boost/fusion/container/generation/make_cons.hpp>
  11. #include <boost/fusion/container/generation/cons_tie.hpp>
  12. #include <boost/fusion/container/vector/vector.hpp>
  13. #include <boost/fusion/container/generation/make_vector.hpp>
  14. #include <boost/fusion/sequence/comparison/equal_to.hpp>
  15. #include <boost/lambda/lambda.hpp>
  16. #include <boost/fusion/algorithm/iteration/for_each.hpp>
  17. #include <boost/fusion/algorithm/transformation/filter_if.hpp>
  18. #include <boost/fusion/algorithm/transformation/push_front.hpp>
  19. #include <boost/fusion/sequence/io/out.hpp>
  20. #include <boost/type_traits/is_same.hpp>
  21. #include <boost/mpl/lambda.hpp>
  22. int
  23. main()
  24. {
  25. using namespace boost::fusion;
  26. using boost::is_same;
  27. namespace fusion = boost::fusion;
  28. std::cout << tuple_open('[');
  29. std::cout << tuple_close(']');
  30. std::cout << tuple_delimiter(", ");
  31. /// Testing cons
  32. {
  33. std::string hello("hello");
  34. cons<int, cons<std::string> > ns =
  35. make_cons(1, make_cons(hello));
  36. BOOST_TEST((*begin(ns) == 1));
  37. BOOST_TEST((*fusion::next(begin(ns)) == hello));
  38. *begin(ns) += 1;
  39. *fusion::next(begin(ns)) += ' ';
  40. BOOST_TEST((*begin(ns) == 2));
  41. BOOST_TEST((*fusion::next(begin(ns)) == hello + ' '));
  42. for_each(ns, boost::lambda::_1 += ' ');
  43. BOOST_TEST((*begin(ns) == 2 + ' '));
  44. BOOST_TEST((*fusion::next(begin(ns)) == hello + ' ' + ' '));
  45. }
  46. {
  47. BOOST_TEST(
  48. make_cons("hello") == make_vector(std::string("hello"))
  49. );
  50. BOOST_TEST(
  51. make_cons(123, make_cons("hello")) ==
  52. make_vector(123, std::string("hello"))
  53. );
  54. }
  55. {
  56. vector<int, float> t(1, 1.1f);
  57. cons<int, cons<float> > nf =
  58. make_cons(1, make_cons(1.1f));
  59. BOOST_TEST((t == nf));
  60. BOOST_TEST((vector<int>(1) == filter_if<is_same<boost::mpl::_, int> >(nf)));
  61. std::cout << nf << std::endl;
  62. std::cout << filter_if<is_same<boost::mpl::_, int> >(nf) << std::endl;
  63. }
  64. {
  65. int i = 3;
  66. cons<int&> tie(cons_tie(i));
  67. BOOST_TEST((*begin(tie) == 3));
  68. }
  69. {
  70. // This used to trigger a hard compilation error:
  71. cons<cons<int> > xs;
  72. begin(push_front(xs, 3));
  73. }
  74. return boost::report_errors();
  75. }