1 //===----------------------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // <algorithm>
11
12 // template<BidirectionalIterator InIter, BidirectionalIterator OutIter>
13 // requires OutputIterator<OutIter, RvalueOf<InIter::reference>::type>
14 // OutIter
15 // move_backward(InIter first, InIter last, OutIter result);
16
17 #include <algorithm>
18 #include <cassert>
19 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
20 #include <memory>
21 #endif
22
23 #include "test_iterators.h"
24
25 template <class InIter, class OutIter>
26 void
test()27 test()
28 {
29 const unsigned N = 1000;
30 int ia[N];
31 for (unsigned i = 0; i < N; ++i)
32 ia[i] = i;
33 int ib[N] = {0};
34
35 OutIter r = std::move_backward(InIter(ia), InIter(ia+N), OutIter(ib+N));
36 assert(base(r) == ib);
37 for (unsigned i = 0; i < N; ++i)
38 assert(ia[i] == ib[i]);
39 }
40
41 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
42
43 template <class InIter, class OutIter>
44 void
test1()45 test1()
46 {
47 const unsigned N = 100;
48 std::unique_ptr<int> ia[N];
49 for (unsigned i = 0; i < N; ++i)
50 ia[i].reset(new int(i));
51 std::unique_ptr<int> ib[N];
52
53 OutIter r = std::move_backward(InIter(ia), InIter(ia+N), OutIter(ib+N));
54 assert(base(r) == ib);
55 for (unsigned i = 0; i < N; ++i)
56 assert(*ib[i] == i);
57 }
58
59 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
60
main()61 int main()
62 {
63 test<bidirectional_iterator<const int*>, bidirectional_iterator<int*> >();
64 test<bidirectional_iterator<const int*>, random_access_iterator<int*> >();
65 test<bidirectional_iterator<const int*>, int*>();
66
67 test<random_access_iterator<const int*>, bidirectional_iterator<int*> >();
68 test<random_access_iterator<const int*>, random_access_iterator<int*> >();
69 test<random_access_iterator<const int*>, int*>();
70
71 test<const int*, bidirectional_iterator<int*> >();
72 test<const int*, random_access_iterator<int*> >();
73 test<const int*, int*>();
74
75 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
76 test1<bidirectional_iterator<std::unique_ptr<int>*>, bidirectional_iterator<std::unique_ptr<int>*> >();
77 test1<bidirectional_iterator<std::unique_ptr<int>*>, random_access_iterator<std::unique_ptr<int>*> >();
78 test1<bidirectional_iterator<std::unique_ptr<int>*>, std::unique_ptr<int>*>();
79
80 test1<random_access_iterator<std::unique_ptr<int>*>, bidirectional_iterator<std::unique_ptr<int>*> >();
81 test1<random_access_iterator<std::unique_ptr<int>*>, random_access_iterator<std::unique_ptr<int>*> >();
82 test1<random_access_iterator<std::unique_ptr<int>*>, std::unique_ptr<int>*>();
83
84 test1<std::unique_ptr<int>*, bidirectional_iterator<std::unique_ptr<int>*> >();
85 test1<std::unique_ptr<int>*, random_access_iterator<std::unique_ptr<int>*> >();
86 test1<std::unique_ptr<int>*, std::unique_ptr<int>*>();
87 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
88 }
89