| 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<RandomAccessIterator Iter> |
| 12 | // requires ShuffleIterator<Iter> && LessThanComparable<Iter::value_type> |
| 13 | // constexpr void // constexpr in C++20 |
| 14 | // pop_heap(Iter first, Iter last); |
| 15 | |
| 16 | #include <algorithm> |
| 17 | #include <cassert> |
| 18 | #include <functional> |
| 19 | |
| 20 | #include "test_macros.h" |
| 21 | #include "test_iterators.h" |
| 22 | #include "MoveOnly.h" |
| 23 | |
| 24 | template<class T, class Iter> |
| 25 | TEST_CONSTEXPR_CXX20 bool test() |
| 26 | { |
| 27 | T orig[15] = {9,6,9,5,5, 8,9,1,1,3, 5,3,4,7,2}; |
| 28 | T work[15] = {9,6,9,5,5, 8,9,1,1,3, 5,3,4,7,2}; |
| 29 | assert(std::is_heap(orig, orig+15)); |
| 30 | for (int i = 15; i >= 1; --i) { |
| 31 | std::pop_heap(Iter(work), Iter(work+i)); |
| 32 | assert(std::is_heap(work, work+i-1)); |
| 33 | assert(std::max_element(work, work+i-1) == work); |
| 34 | assert(std::is_permutation(work, work+15, orig)); |
| 35 | } |
| 36 | assert(std::is_sorted(work, work+15)); |
| 37 | |
| 38 | { |
| 39 | T input[] = {5, 4, 1, 2, 3}; |
| 40 | assert(std::is_heap(input, input + 5)); |
| 41 | std::pop_heap(Iter(input), Iter(input + 5)); assert(input[4] == 5); |
| 42 | std::pop_heap(Iter(input), Iter(input + 4)); assert(input[3] == 4); |
| 43 | std::pop_heap(Iter(input), Iter(input + 3)); assert(input[2] == 3); |
| 44 | std::pop_heap(Iter(input), Iter(input + 2)); assert(input[1] == 2); |
| 45 | std::pop_heap(Iter(input), Iter(input + 1)); assert(input[0] == 1); |
| 46 | } |
| 47 | return true; |
| 48 | } |
| 49 | |
| 50 | int main(int, char**) |
| 51 | { |
| 52 | test<int, random_access_iterator<int*> >(); |
| 53 | test<int, int*>(); |
| 54 | |
| 55 | #if TEST_STD_VER >= 11 |
| 56 | test<MoveOnly, random_access_iterator<MoveOnly*>>(); |
| 57 | test<MoveOnly, MoveOnly*>(); |
| 58 | #endif |
| 59 | |
| 60 | #if TEST_STD_VER >= 20 |
| 61 | static_assert(test<int, random_access_iterator<int*>>()); |
| 62 | static_assert(test<int, int*>()); |
| 63 | static_assert(test<MoveOnly, random_access_iterator<MoveOnly*>>()); |
| 64 | static_assert(test<MoveOnly, MoveOnly*>()); |
| 65 | #endif |
| 66 | |
| 67 | return 0; |
| 68 | } |
| 69 | |