]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/std/algorithms/alg.sorting/alg.heap.operations/push.heap/push_heap_comp.pass.cpp
Vendor import of libc++ trunk r300422:
[FreeBSD/FreeBSD.git] / test / std / algorithms / alg.sorting / alg.heap.operations / push.heap / push_heap_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>
13 //   requires ShuffleIterator<Iter>
14 //         && LessThanComparable<Iter::value_type>
15 //   void
16 //   push_heap(Iter first, Iter last);
17
18 #include <algorithm>
19 #include <functional>
20 #include <random>
21 #include <cassert>
22 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
23 #include <memory>
24
25 struct indirect_less
26 {
27     template <class P>
28     bool operator()(const P& x, const P& y)
29         {return *x < *y;}
30 };
31
32 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
33
34 std::mt19937 randomness;
35
36 void test(int N)
37 {
38     int* ia = new int [N];
39     for (int i = 0; i < N; ++i)
40         ia[i] = i;
41     std::shuffle(ia, ia+N, randomness);
42     for (int i = 0; i <= N; ++i)
43     {
44         std::push_heap(ia, ia+i, std::greater<int>());
45         assert(std::is_heap(ia, ia+i, std::greater<int>()));
46     }
47     delete [] ia;
48 }
49
50 int main()
51 {
52     test(1000);
53
54 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
55     {
56     const int N = 1000;
57     std::unique_ptr<int>* ia = new std::unique_ptr<int> [N];
58     for (int i = 0; i < N; ++i)
59         ia[i].reset(new int(i));
60     std::shuffle(ia, ia+N, randomness);
61     for (int i = 0; i <= N; ++i)
62     {
63         std::push_heap(ia, ia+i, indirect_less());
64         assert(std::is_heap(ia, ia+i, indirect_less()));
65     }
66     delete [] ia;
67     }
68 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
69 }