]>
Commit | Line | Data |
---|---|---|
99dee823 | 1 | // Copyright (C) 2020-2021 Free Software Foundation, Inc. |
bc464641 PP |
2 | // |
3 | // This file is part of the GNU ISO C++ Library. This library is free | |
4 | // software; you can redistribute it and/or modify it under the | |
5 | // terms of the GNU General Public License as published by the | |
6 | // Free Software Foundation; either version 3, or (at your option) | |
7 | // any later version. | |
8 | ||
9 | // This library is distributed in the hope that it will be useful, | |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | // GNU General Public License for more details. | |
13 | ||
14 | // You should have received a copy of the GNU General Public License along | |
15 | // with this library; see the file COPYING3. If not see | |
16 | // <http://www.gnu.org/licenses/>. | |
17 | ||
18 | // { dg-options "-std=gnu++2a" } | |
19 | // { dg-do run { target c++2a } } | |
20 | // { dg-require-cstdint "" } | |
21 | ||
22 | #include <algorithm> | |
23 | #include <random> | |
24 | #include <testsuite_hooks.h> | |
25 | #include <testsuite_iterators.h> | |
26 | ||
27 | using __gnu_test::test_container; | |
28 | using __gnu_test::test_range; | |
29 | using __gnu_test::random_access_iterator_wrapper; | |
30 | ||
31 | namespace ranges = std::ranges; | |
32 | ||
33 | template<template<typename, template<typename> typename> typename container> | |
34 | void | |
35 | test01() | |
36 | { | |
37 | int x[50]; | |
38 | ||
39 | auto pred = std::greater{}; | |
40 | auto proj = [] (int a) { return -a; }; | |
41 | for (int i = 0; i < 50; i++) | |
42 | { | |
43 | std::iota(x, x+50, 1); | |
44 | container<int, random_access_iterator_wrapper> rx(x); | |
45 | ||
46 | std::ranlux48_base g(i); | |
47 | ranges::shuffle(rx, g); | |
48 | ||
49 | auto iter = ranges::make_heap(rx, pred, proj); | |
50 | VERIFY( iter == rx.end() ); | |
51 | VERIFY( ranges::is_heap(rx, pred, proj) ); | |
52 | VERIFY( ranges::is_heap_until(rx, pred, proj) == rx.end() ); | |
53 | ||
54 | iter = ranges::pop_heap(rx, pred, proj); | |
55 | VERIFY( iter == rx.end() ); | |
56 | VERIFY( *(iter-1) == 50 ); | |
57 | VERIFY( ranges::is_heap_until(rx, pred, proj) == iter-1 ); | |
58 | ||
59 | iter = ranges::pop_heap(rx.begin(), iter-1, pred, proj); | |
60 | VERIFY( iter+1 == rx.end() ); | |
61 | VERIFY( *(iter-1) == 49 ); | |
62 | VERIFY( ranges::is_heap_until(rx, pred, proj) == iter-1 ); | |
63 | ||
64 | *(iter-1) = i; | |
65 | iter = ranges::push_heap(rx.begin(), iter, pred, proj); | |
66 | VERIFY( iter+1 == rx.end() ); | |
67 | VERIFY( ranges::is_heap_until(rx, pred, proj) == iter ); | |
68 | ||
69 | *iter = 2*i; | |
70 | iter = ranges::push_heap(rx.begin(), rx.end(), pred, proj); | |
71 | VERIFY( iter == rx.end() ); | |
72 | VERIFY( ranges::is_heap_until(rx, pred, proj) == iter ); | |
73 | ||
74 | *(rx.begin()+1) *= -1; | |
75 | VERIFY( !ranges::is_heap(rx, pred, proj) ); | |
76 | *(rx.begin()+1) *= -1; | |
77 | VERIFY( ranges::is_heap(rx, pred, proj) ); | |
78 | ||
79 | iter = ranges::sort_heap(rx, pred, proj); | |
80 | VERIFY( iter == rx.end() ); | |
81 | VERIFY( ranges::is_sorted(rx, pred, proj) ); | |
82 | } | |
83 | } | |
84 | ||
85 | constexpr bool | |
86 | test02() | |
87 | { | |
88 | bool ok = true; | |
89 | int x[] = {1,2,3,4,5}; | |
90 | ranges::make_heap(x); | |
91 | ranges::pop_heap(x); | |
92 | x[4] = 7; | |
93 | ranges::push_heap(x); | |
94 | ok &= ranges::is_heap(x); | |
95 | ok &= ranges::is_heap_until(x) == x+5; | |
96 | ranges::sort_heap(x); | |
97 | ok &= ranges::equal(x, (int[]){1,2,3,4,7}); | |
98 | return ok; | |
99 | } | |
100 | ||
101 | int | |
102 | main() | |
103 | { | |
104 | test01<test_range>(); | |
105 | test01<test_container>(); | |
106 | static_assert(test02()); | |
107 | } |