advance_test.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright (C) 2017 Michel Morin.
  2. //
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #include <vector>
  7. #include <list>
  8. #include <boost/container/slist.hpp>
  9. #include <boost/core/lightweight_test.hpp>
  10. #include <boost/iterator/advance.hpp>
  11. #include <boost/iterator/transform_iterator.hpp>
  12. int twice(int x) { return x + x; }
  13. template <typename Iterator>
  14. void test_advance(Iterator it_from, Iterator it_to, int n)
  15. {
  16. boost::advance(it_from, n);
  17. BOOST_TEST(it_from == it_to);
  18. }
  19. int main()
  20. {
  21. int array[3] = {1, 2, 3};
  22. int* ptr1 = array;
  23. int* ptr2 = array + 3;
  24. {
  25. test_advance(ptr1, ptr2, 3);
  26. test_advance(ptr2, ptr1, -3);
  27. test_advance(
  28. boost::make_transform_iterator(ptr1, twice)
  29. , boost::make_transform_iterator(ptr2, twice)
  30. , 3
  31. );
  32. test_advance(
  33. boost::make_transform_iterator(ptr2, twice)
  34. , boost::make_transform_iterator(ptr1, twice)
  35. , -3
  36. );
  37. }
  38. {
  39. std::vector<int> ints(ptr1, ptr2);
  40. test_advance(ints.begin(), ints.end(), 3);
  41. test_advance(ints.end(), ints.begin(), -3);
  42. test_advance(
  43. boost::make_transform_iterator(ints.begin(), twice)
  44. , boost::make_transform_iterator(ints.end(), twice)
  45. , 3
  46. );
  47. test_advance(
  48. boost::make_transform_iterator(ints.end(), twice)
  49. , boost::make_transform_iterator(ints.begin(), twice)
  50. , -3
  51. );
  52. }
  53. {
  54. std::list<int> ints(ptr1, ptr2);
  55. test_advance(ints.begin(), ints.end(), 3);
  56. test_advance(ints.end(), ints.begin(), -3);
  57. test_advance(
  58. boost::make_transform_iterator(ints.begin(), twice)
  59. , boost::make_transform_iterator(ints.end(), twice)
  60. , 3
  61. );
  62. test_advance(
  63. boost::make_transform_iterator(ints.end(), twice)
  64. , boost::make_transform_iterator(ints.begin(), twice)
  65. , -3
  66. );
  67. }
  68. {
  69. boost::container::slist<int> ints(ptr1, ptr2);
  70. test_advance(ints.begin(), ints.end(), 3);
  71. test_advance(
  72. boost::make_transform_iterator(ints.begin(), twice)
  73. , boost::make_transform_iterator(ints.end(), twice)
  74. , 3
  75. );
  76. }
  77. return boost::report_errors();
  78. }