blob: e451e68b72d7f541547ba82e32ed373d4b849239 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001//===----------------------------------------------------------------------===//
2//
Howard Hinnantf5256e12010-05-11 21:36:01 +00003// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// <algorithm>
11
Howard Hinnanteb564e72010-08-22 00:08:10 +000012// template<RandomAccessIterator Iter, StrictWeakOrder<auto, Iter::value_type> Compare>
13// requires ShuffleIterator<Iter> && CopyConstructible<Compare>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000014// void
15// sort_heap(Iter first, Iter last, Compare comp);
16
17#include <algorithm>
18#include <functional>
19#include <cassert>
Howard Hinnant73d21a42010-09-04 23:28:19 +000020#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000021#include <memory>
22
23struct indirect_less
24{
25 template <class P>
26 bool operator()(const P& x, const P& y)
27 {return *x < *y;}
28};
29
Howard Hinnant73d21a42010-09-04 23:28:19 +000030#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000031
32void test(unsigned N)
33{
34 int* ia = new int [N];
35 for (int i = 0; i < N; ++i)
36 ia[i] = i;
37 std::random_shuffle(ia, ia+N);
38 std::make_heap(ia, ia+N, std::greater<int>());
39 std::sort_heap(ia, ia+N, std::greater<int>());
40 assert(std::is_sorted(ia, ia+N, std::greater<int>()));
41 delete [] ia;
42}
43
44int main()
45{
46 test(0);
47 test(1);
48 test(2);
49 test(3);
50 test(10);
51 test(1000);
52
Howard Hinnant73d21a42010-09-04 23:28:19 +000053#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000054 {
55 const int N = 1000;
56 std::unique_ptr<int>* ia = new std::unique_ptr<int> [N];
57 for (int i = 0; i < N; ++i)
58 ia[i].reset(new int(i));
59 std::random_shuffle(ia, ia+N);
60 std::make_heap(ia, ia+N, indirect_less());
61 std::sort_heap(ia, ia+N, indirect_less());
62 assert(std::is_sorted(ia, ia+N, indirect_less()));
63 delete [] ia;
64 }
Howard Hinnant73d21a42010-09-04 23:28:19 +000065#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000066}