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

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