test_forward_iterator.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Boost.TypeErasure library
  2. //
  3. // Copyright 2011 Steven Watanabe
  4. //
  5. // Distributed under the Boost Software License Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // $Id$
  10. #include <boost/type_erasure/any.hpp>
  11. #include <boost/type_erasure/builtin.hpp>
  12. #include <boost/type_erasure/operators.hpp>
  13. #include <boost/type_erasure/any_cast.hpp>
  14. #include <boost/type_erasure/iterator.hpp>
  15. #include <boost/type_erasure/same_type.hpp>
  16. #include <boost/type_erasure/binding_of.hpp>
  17. #include <boost/mpl/vector.hpp>
  18. #include <boost/concept_check.hpp>
  19. #define BOOST_TEST_MAIN
  20. #include <boost/test/unit_test.hpp>
  21. using namespace boost::type_erasure;
  22. BOOST_AUTO_TEST_CASE(test_basic)
  23. {
  24. typedef boost::mpl::vector<
  25. forward_iterator<>,
  26. same_type<forward_iterator<>::value_type, int>
  27. > test_concept;
  28. std::vector<int> vec(10);
  29. any<test_concept> x(vec.begin());
  30. any<test_concept> y(vec.end());
  31. for(int i = 0; x != y; ++x, ++i) {
  32. *x = i;
  33. }
  34. int expected[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  35. BOOST_CHECK_EQUAL_COLLECTIONS(vec.begin(), vec.end(), &expected[0], &expected[0] + 10);
  36. BOOST_MPL_ASSERT((boost::is_same<any<test_concept>::value_type, int>));
  37. BOOST_MPL_ASSERT((boost::is_same<any<test_concept>::reference, int&>));
  38. BOOST_MPL_ASSERT((boost::is_same<any<test_concept>::pointer, int*>));
  39. BOOST_MPL_ASSERT((boost::is_same<any<test_concept>::difference_type, std::ptrdiff_t>));
  40. BOOST_MPL_ASSERT((boost::is_same<any<test_concept>::iterator_category, std::forward_iterator_tag>));
  41. }
  42. BOOST_AUTO_TEST_CASE(test_any_value_type)
  43. {
  44. typedef boost::mpl::vector<
  45. forward_iterator<>,
  46. same_type<forward_iterator<>::value_type, _a>,
  47. copy_constructible<_a>,
  48. assignable<_a>,
  49. incrementable<_a>
  50. > test_concept;
  51. std::vector<int> vec(10);
  52. any<test_concept> x(vec.begin());
  53. any<test_concept> y(vec.end());
  54. for(any<test_concept, _a> i = *x; x != y; ++x, ++i) {
  55. *x = i;
  56. }
  57. int expected[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  58. BOOST_CHECK_EQUAL_COLLECTIONS(vec.begin(), vec.end(), &expected[0], &expected[0] + 10);
  59. BOOST_MPL_ASSERT((boost::is_same<any<test_concept>::value_type, any<test_concept, _a> >));
  60. BOOST_MPL_ASSERT((boost::is_same<any<test_concept>::reference, any<test_concept, _a&> >));
  61. BOOST_MPL_ASSERT((boost::is_same<any<test_concept>::pointer, any<test_concept, _a>*>));
  62. BOOST_MPL_ASSERT((boost::is_same<any<test_concept>::difference_type, std::ptrdiff_t>));
  63. BOOST_MPL_ASSERT((boost::is_same<any<test_concept>::iterator_category, std::forward_iterator_tag>));
  64. }
  65. BOOST_AUTO_TEST_CASE(test_relaxed)
  66. {
  67. typedef boost::mpl::vector<
  68. forward_iterator<>,
  69. same_type<forward_iterator<>::value_type, int>,
  70. relaxed
  71. > test_concept;
  72. std::vector<int> vec(10);
  73. any<test_concept> x(vec.begin());
  74. any<test_concept> y(vec.end());
  75. for(int i = 0; x != y; ++x, ++i) {
  76. *x = i;
  77. }
  78. int expected[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  79. BOOST_CHECK_EQUAL_COLLECTIONS(vec.begin(), vec.end(), &expected[0], &expected[0] + 10);
  80. BOOST_MPL_ASSERT((boost::is_same<any<test_concept>::value_type, int>));
  81. BOOST_MPL_ASSERT((boost::is_same<any<test_concept>::reference, int&>));
  82. BOOST_MPL_ASSERT((boost::is_same<any<test_concept>::pointer, int*>));
  83. BOOST_MPL_ASSERT((boost::is_same<any<test_concept>::difference_type, std::ptrdiff_t>));
  84. BOOST_MPL_ASSERT((boost::is_same<any<test_concept>::iterator_category, std::forward_iterator_tag>));
  85. BOOST_CONCEPT_ASSERT((boost::ForwardIterator<any<test_concept> >));
  86. }