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> pair<iterator,iterator> equal_range(const K& x);
14// template<class K> pair<const_iterator,const_iterator> equal_range(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 CanEqualRange = requires(M m, Transparent<int> k) { m.equal_range(k); };
31using TransparentSet = std::flat_set<int, TransparentComparator>;
32using NonTransparentSet = std::flat_set<int, NonTransparentComparator>;
33static_assert(CanEqualRange<TransparentSet>);
34static_assert(CanEqualRange<const TransparentSet>);
35static_assert(!CanEqualRange<NonTransparentSet>);
36static_assert(!CanEqualRange<const NonTransparentSet>);
37
38template <class KeyContainer>
39void test_one() {
40 using Key = typename KeyContainer::value_type;
41 using M = std::flat_set<Key, TransparentComparator, KeyContainer>;
42
43 using R = std::pair<typename M::iterator, typename M::iterator>;
44 using CR = std::pair<typename M::const_iterator, typename M::const_iterator>;
45
46 auto test_found = [](auto&& map, const std::string& expected_key) {
47 auto [first, last] = map.equal_range(Transparent<std::string>{.t: expected_key});
48 assert(last - first == 1);
49 assert(*first == expected_key);
50 };
51
52 auto test_not_found = [](auto&& map, const std::string& expected_key, long expected_offset) {
53 auto [first, last] = map.equal_range(Transparent<std::string>{.t: expected_key});
54 assert(first == last);
55 assert(first - map.begin() == expected_offset);
56 };
57 {
58 M m = {"alpha", "beta", "epsilon", "eta", "gamma"};
59 const auto& cm = m;
60 ASSERT_SAME_TYPE(decltype(m.equal_range(Transparent<std::string>{"abc"})), R);
61 ASSERT_SAME_TYPE(decltype(std::as_const(m).equal_range(Transparent<std::string>{"b"})), CR);
62
63 test_found(m, "alpha");
64 test_found(m, "beta");
65 test_found(m, "epsilon");
66 test_found(m, "eta");
67 test_found(m, "gamma");
68 test_found(cm, "alpha");
69 test_found(cm, "beta");
70 test_found(cm, "epsilon");
71 test_found(cm, "eta");
72 test_found(cm, "gamma");
73
74 test_not_found(m, "charlie", 2);
75 test_not_found(m, "aaa", 0);
76 test_not_found(m, "zzz", 5);
77 test_not_found(cm, "charlie", 2);
78 test_not_found(cm, "aaa", 0);
79 test_not_found(cm, "zzz", 5);
80 }
81 {
82 // empty
83 M m;
84 const auto& cm = m;
85 test_not_found(m, "aaa", 0);
86 test_not_found(cm, "charlie", 0);
87 }
88}
89
90void test() {
91 test_one<std::vector<std::string>>();
92 test_one<std::deque<std::string>>();
93 test_one<MinSequenceContainer<std::string>>();
94 test_one<std::vector<std::string, min_allocator<std::string>>>();
95
96 {
97 bool transparent_used = false;
98 TransparentComparator c(transparent_used);
99 std::flat_set<int, TransparentComparator> m(std::sorted_unique, {1, 2, 3}, c);
100 assert(!transparent_used);
101 auto p = m.equal_range(Transparent<int>{3});
102 assert(p.first != p.second);
103 assert(transparent_used);
104 }
105 {
106 // LWG4239 std::string and C string literal
107 using M = std::flat_set<std::string, std::less<>>;
108 M m{"alpha", "beta", "epsilon", "eta", "gamma"};
109 auto [first, last] = m.equal_range("beta");
110 assert(first == m.begin() + 1);
111 assert(last == m.begin() + 2);
112 }
113}
114
115int main(int, char**) {
116 test();
117
118 return 0;
119}
120

source code of libcxx/test/std/containers/container.adaptors/flat.set/flat.set.operations/equal_range_transparent.pass.cpp