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// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
10
11// <flat_set>
12
13// template<class K> iterator upper_bound(const K& x);
14// template<class K> const_iterator upper_bound(const K& x) const;
15
16#include <cassert>
17#include <deque>
18#include <flat_set>
19#include <functional>
20#include <string>
21#include <utility>
22
23#include "MinSequenceContainer.h"
24#include "../helpers.h"
25#include "test_macros.h"
26#include "min_allocator.h"
27
28// Constraints: The qualified-id Compare::is_transparent is valid and denotes a type.
29template <class M>
30concept CanUpperBound = requires(M m, Transparent<int> k) { m.upper_bound(k); };
31using TransparentSet = std::flat_multiset<int, TransparentComparator>;
32using NonTransparentSet = std::flat_multiset<int, NonTransparentComparator>;
33static_assert(CanUpperBound<TransparentSet>);
34static_assert(CanUpperBound<const TransparentSet>);
35static_assert(!CanUpperBound<NonTransparentSet>);
36static_assert(!CanUpperBound<const NonTransparentSet>);
37
38template <class KeyContainer>
39void test_one() {
40 using Key = typename KeyContainer::value_type;
41 using M = std::flat_multiset<Key, TransparentComparator, KeyContainer>;
42
43 {
44 M m = {"alpha", "alpha", "beta", "epsilon", "epsilon", "epsilon", "eta", "gamma"};
45 const auto& cm = m;
46 ASSERT_SAME_TYPE(decltype(m.lower_bound(Transparent<std::string>{"abc"})), typename M::iterator);
47 ASSERT_SAME_TYPE(decltype(std::as_const(m).lower_bound(Transparent<std::string>{"b"})), typename M::const_iterator);
48
49 auto test_upper_bound = [&](auto&& set, const std::string& expected_key, long expected_offset) {
50 auto iter = set.upper_bound(Transparent<std::string>{.t: expected_key});
51 assert(iter - set.begin() == expected_offset);
52 };
53
54 test_upper_bound(m, "abc", 0);
55 test_upper_bound(m, "alpha", 2);
56 test_upper_bound(m, "beta", 3);
57 test_upper_bound(m, "bets", 3);
58 test_upper_bound(m, "charlie", 3);
59 test_upper_bound(m, "echo", 3);
60 test_upper_bound(m, "epsilon", 6);
61 test_upper_bound(m, "eta", 7);
62 test_upper_bound(m, "gamma", 8);
63 test_upper_bound(m, "golf", 8);
64 test_upper_bound(m, "zzz", 8);
65
66 test_upper_bound(cm, "abc", 0);
67 test_upper_bound(cm, "alpha", 2);
68 test_upper_bound(cm, "beta", 3);
69 test_upper_bound(cm, "bets", 3);
70 test_upper_bound(cm, "charlie", 3);
71 test_upper_bound(cm, "echo", 3);
72 test_upper_bound(cm, "epsilon", 6);
73 test_upper_bound(cm, "eta", 7);
74 test_upper_bound(cm, "gamma", 8);
75 test_upper_bound(cm, "golf", 8);
76 test_upper_bound(cm, "zzz", 8);
77 }
78 {
79 // empty
80 M m;
81 auto iter = m.upper_bound(Transparent<std::string>{.t: "a"});
82 assert(iter == m.end());
83 }
84}
85
86void test() {
87 test_one<std::vector<std::string>>();
88 test_one<std::deque<std::string>>();
89 test_one<MinSequenceContainer<std::string>>();
90 test_one<std::vector<std::string, min_allocator<std::string>>>();
91
92 {
93 bool transparent_used = false;
94 TransparentComparator c(transparent_used);
95 std::flat_multiset<int, TransparentComparator> m(std::sorted_equivalent, {1, 1, 1, 2, 3}, c);
96 assert(!transparent_used);
97 auto it = m.upper_bound(Transparent<int>{2});
98 assert(it != m.end());
99 assert(transparent_used);
100 }
101 {
102 // std::string and C string literal
103 using M = std::flat_multiset<std::string, std::less<>>;
104 M m = {"alpha", "beta", "beta", "epsilon", "eta", "gamma"};
105 auto it = m.upper_bound("beta");
106 assert(it == m.begin() + 3);
107 }
108}
109
110int main(int, char**) {
111 test();
112
113 return 0;
114}
115

source code of libcxx/test/std/containers/container.adaptors/flat.multiset/flat.multiset.operations/upper_bound_transparent.pass.cpp