| 1 | // Copyright (C) 2016-2025 Free Software Foundation, Inc. |
| 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 "-D_GLIBCXX_DEBUG" } |
| 19 | // { dg-do run { target c++11 } } |
| 20 | // { dg-skip-if "" { *-*-* } { "-D_GLIBCXX_PARALLEL" } } |
| 21 | |
| 22 | #include <algorithm> |
| 23 | #include <functional> |
| 24 | #include <testsuite_iterators.h> |
| 25 | #include <testsuite_hooks.h> |
| 26 | |
| 27 | using __gnu_test::test_container; |
| 28 | using __gnu_test::forward_iterator_wrapper; |
| 29 | |
| 30 | struct X |
| 31 | { |
| 32 | int val; |
| 33 | |
| 34 | bool odd() const { return val % 2; } |
| 35 | |
| 36 | // Partitioned so that all odd values come before even values: |
| 37 | bool operator<(const X& x) const { return this->odd() && !x.odd(); } |
| 38 | }; |
| 39 | |
| 40 | void |
| 41 | test01() |
| 42 | { |
| 43 | // Test with range that is partitioned, but not sorted. |
| 44 | X seq[] = { 1, 3, 5, 7, 1, 6, 4, 2 }; |
| 45 | test_container<X, forward_iterator_wrapper> c(seq); |
| 46 | |
| 47 | auto part1 = std::upper_bound(c.begin(), c.end(), X{2}); |
| 48 | VERIFY( part1 == c.end() ); |
| 49 | auto part2 = std::upper_bound(c.begin(), c.end(), X{2}, std::less<X>{}); |
| 50 | VERIFY( part2 == c.end() ); |
| 51 | |
| 52 | auto part3 = std::upper_bound(c.begin(), c.end(), X{9}); |
| 53 | VERIFY( part3 != c.end() ); |
| 54 | VERIFY( part3->val == 6 ); |
| 55 | auto part4 = std::upper_bound(c.begin(), c.end(), X{9}, std::less<X>{}); |
| 56 | VERIFY( part3 != c.end() ); |
| 57 | VERIFY( part4->val == 6 ); |
| 58 | } |
| 59 | |
| 60 | struct Y |
| 61 | { |
| 62 | double val; |
| 63 | |
| 64 | // Not irreflexive, so not a strict weak order. |
| 65 | bool operator<(const Y& y) const { return val < (int)y.val; } |
| 66 | }; |
| 67 | |
| 68 | void |
| 69 | test02() |
| 70 | { |
| 71 | // Test that Debug Mode checks don't fire (libstdc++/71545) |
| 72 | |
| 73 | Y seq[] = { -0.1, 1.2, 5.0, 5.2, 5.1, 5.9, 5.5, 6.0 }; |
| 74 | test_container<Y, forward_iterator_wrapper> c(seq); |
| 75 | |
| 76 | auto part1 = std::upper_bound(c.begin(), c.end(), Y{5.5}); |
| 77 | VERIFY( part1 != c.end() ); |
| 78 | VERIFY( part1->val == 6.0 ); |
| 79 | auto part2 = std::upper_bound(c.begin(), c.end(), Y{5.5}, std::less<Y>{}); |
| 80 | VERIFY( part2 != c.end() ); |
| 81 | VERIFY( part2->val == 6.0 ); |
| 82 | |
| 83 | auto part3 = std::upper_bound(c.begin(), c.end(), Y{1.0}); |
| 84 | VERIFY( part3 != c.end() ); |
| 85 | VERIFY( part3->val == 5.0 ); |
| 86 | auto part4 = std::upper_bound(c.begin(), c.end(), Y{1.0}, std::less<Y>{}); |
| 87 | VERIFY( part4 != c.end() ); |
| 88 | VERIFY( part4->val == 5.0 ); |
| 89 | } |
| 90 | |
| 91 | int |
| 92 | main() |
| 93 | { |
| 94 | test01(); |
| 95 | test02(); |
| 96 | } |