1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 // <algorithm>
10
11 // template<BidirectionalIterator Iter>
12 // requires ShuffleIterator<Iter>
13 // && LessThanComparable<Iter::value_type>
14 // void
15 // inplace_merge(Iter first, Iter middle, Iter last);
16
17 #include <algorithm>
18 #include <cassert>
19 #include <random>
20 #include <vector>
21
22 #include "count_new.h"
23 #include "test_iterators.h"
24 #include "test_macros.h"
25
26 #if TEST_STD_VER >= 11
27 struct S {
SS28 S() : i_(0) {}
SS29 S(int i) : i_(i) {}
30
SS31 S(const S& rhs) : i_(rhs.i_) {}
SS32 S( S&& rhs) : i_(rhs.i_) { rhs.i_ = -1; }
33
operator =S34 S& operator =(const S& rhs) { i_ = rhs.i_; return *this; }
operator =S35 S& operator =( S&& rhs) { i_ = rhs.i_; rhs.i_ = -2; assert(this != &rhs); return *this; }
operator =S36 S& operator =(int i) { i_ = i; return *this; }
37
operator <S38 bool operator <(const S& rhs) const { return i_ < rhs.i_; }
operator ==S39 bool operator ==(const S& rhs) const { return i_ == rhs.i_; }
operator ==S40 bool operator ==(int i) const { return i_ == i; }
41
setS42 void set(int i) { i_ = i; }
43
44 int i_;
45 };
46 #endif
47
48 std::mt19937 randomness;
49
50 template <class Iter>
51 void
test_one(unsigned N,unsigned M)52 test_one(unsigned N, unsigned M)
53 {
54 typedef typename std::iterator_traits<Iter>::value_type value_type;
55 assert(M <= N);
56 value_type* ia = new value_type[N];
57 for (unsigned i = 0; i < N; ++i)
58 ia[i] = i;
59 std::shuffle(ia, ia+N, randomness);
60 std::sort(ia, ia+M);
61 std::sort(ia+M, ia+N);
62 std::inplace_merge(Iter(ia), Iter(ia+M), Iter(ia+N));
63 if(N > 0)
64 {
65 assert(ia[0] == 0);
66 assert(ia[N-1] == static_cast<value_type>(N-1));
67 assert(std::is_sorted(ia, ia+N));
68 }
69 delete [] ia;
70 }
71
72 template <class Iter>
73 void
test(unsigned N)74 test(unsigned N)
75 {
76 test_one<Iter>(N, 0);
77 test_one<Iter>(N, N/4);
78 test_one<Iter>(N, N/2);
79 test_one<Iter>(N, 3*N/4);
80 test_one<Iter>(N, N);
81 }
82
83 template <class Iter>
84 void
test()85 test()
86 {
87 test_one<Iter>(0, 0);
88 test_one<Iter>(1, 0);
89 test_one<Iter>(1, 1);
90 test_one<Iter>(2, 0);
91 test_one<Iter>(2, 1);
92 test_one<Iter>(2, 2);
93 test_one<Iter>(3, 0);
94 test_one<Iter>(3, 1);
95 test_one<Iter>(3, 2);
96 test_one<Iter>(3, 3);
97 test<Iter>(4);
98 test<Iter>(100);
99 test<Iter>(1000);
100 }
101
main(int,char **)102 int main(int, char**)
103 {
104 test<bidirectional_iterator<int*> >();
105 test<random_access_iterator<int*> >();
106 test<int*>();
107
108 #if TEST_STD_VER >= 11
109 test<bidirectional_iterator<S*> >();
110 test<random_access_iterator<S*> >();
111 test<S*>();
112 #endif
113
114 #if TEST_STD_VER >= 11 && !defined(TEST_HAS_NO_EXCEPTIONS)
115 {
116 std::vector<int> vec(150, 3);
117 getGlobalMemCounter()->throw_after = 0;
118 std::inplace_merge(vec.begin(), vec.begin() + 100, vec.end());
119 assert(std::all_of(vec.begin(), vec.end(), [](int i) { return i == 3; }));
120 }
121 #endif // TEST_STD_VER >= 11 && !defined(TEST_HAS_NO_EXCEPTIONS)
122
123 return 0;
124 }
125