]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/std/algorithms/alg.sorting/alg.sort/stable.sort/stable_sort_comp.pass.cpp
Vendor import of libc++ trunk r290819:
[FreeBSD/FreeBSD.git] / test / std / algorithms / alg.sorting / alg.sort / stable.sort / stable_sort_comp.pass.cpp
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<RandomAccessIterator Iter, StrictWeakOrder<auto, Iter::value_type> Compare>
13 //   requires ShuffleIterator<Iter>
14 //         && CopyConstructible<Compare>
15 //   void
16 //   stable_sort(Iter first, Iter last, Compare comp);
17
18 #include <algorithm>
19 #include <functional>
20 #include <vector>
21 #include <cassert>
22 #include <cstddef>
23 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
24 #include <memory>
25
26 struct indirect_less
27 {
28     template <class P>
29     bool operator()(const P& x, const P& y)
30         {return *x < *y;}
31 };
32
33 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
34
35 struct first_only
36 {
37     bool operator()(const std::pair<int, int>& x, const std::pair<int, int>& y)
38     {
39         return x.first < y.first;
40     }
41 };
42
43 void test()
44 {
45     typedef std::pair<int, int> P;
46     const int N = 1000;
47     const int M = 10;
48     std::vector<P> v(N);
49     int x = 0;
50     int ver = 0;
51     for (int i = 0; i < N; ++i)
52     {
53         v[i] = P(x, ver);
54         if (++x == M)
55         {
56             x = 0;
57             ++ver;
58         }
59     }
60     for (int i = 0; i < N - M; i += M)
61     {
62         std::random_shuffle(v.begin() + i, v.begin() + i + M);
63     }
64     std::stable_sort(v.begin(), v.end(), first_only());
65     assert(std::is_sorted(v.begin(), v.end()));
66 }
67
68 int main()
69 {
70     test();
71
72 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
73     {
74     std::vector<std::unique_ptr<int> > v(1000);
75     for (int i = 0; static_cast<std::size_t>(i) < v.size(); ++i)
76         v[i].reset(new int(i));
77     std::stable_sort(v.begin(), v.end(), indirect_less());
78     assert(std::is_sorted(v.begin(), v.end(), indirect_less()));
79     assert(*v[0] == 0);
80     assert(*v[1] == 1);
81     assert(*v[2] == 2);
82     }
83 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
84 }