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> iterator lower_bound(const K& x);
14// template<class K> const_iterator lower_bound(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 CanLowerBound = requires(M m, Transparent<int> k) { m.lower_bound(k); };
31using TransparentMap = std::flat_map<int, double, TransparentComparator>;
32using NonTransparentMap = std::flat_map<int, double, NonTransparentComparator>;
33static_assert(CanLowerBound<TransparentMap>);
34static_assert(CanLowerBound<const TransparentMap>);
35static_assert(!CanLowerBound<NonTransparentMap>);
36static_assert(!CanLowerBound<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 M m = {{"alpha", 1}, {"beta", 2}, {"epsilon", 3}, {"eta", 4}, {"gamma", 5}};
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_lower_bound = [&](auto&& map, const std::string& expected_key, long expected_offset) {
50 auto iter = map.lower_bound(Transparent<std::string>{.t: expected_key});
51 assert(iter - map.begin() == expected_offset);
52 };
53
54 test_lower_bound(m, "abc", 0);
55 test_lower_bound(m, "alpha", 0);
56 test_lower_bound(m, "beta", 1);
57 test_lower_bound(m, "bets", 2);
58 test_lower_bound(m, "charlie", 2);
59 test_lower_bound(m, "echo", 2);
60 test_lower_bound(m, "epsilon", 2);
61 test_lower_bound(m, "eta", 3);
62 test_lower_bound(m, "gamma", 4);
63 test_lower_bound(m, "golf", 5);
64 test_lower_bound(m, "zzz", 5);
65
66 test_lower_bound(cm, "abc", 0);
67 test_lower_bound(cm, "alpha", 0);
68 test_lower_bound(cm, "beta", 1);
69 test_lower_bound(cm, "bets", 2);
70 test_lower_bound(cm, "charlie", 2);
71 test_lower_bound(cm, "echo", 2);
72 test_lower_bound(cm, "epsilon", 2);
73 test_lower_bound(cm, "eta", 3);
74 test_lower_bound(cm, "gamma", 4);
75 test_lower_bound(cm, "golf", 5);
76 test_lower_bound(cm, "zzz", 5);
77}
78
79int main(int, char**) {
80 test<std::vector<std::string>, std::vector<int>>();
81 test<std::deque<std::string>, std::vector<int>>();
82 test<MinSequenceContainer<std::string>, MinSequenceContainer<int>>();
83 test<std::vector<std::string, min_allocator<std::string>>, std::vector<int, min_allocator<int>>>();
84
85 {
86 bool transparent_used = false;
87 TransparentComparator c(transparent_used);
88 std::flat_map<int, int, TransparentComparator> m(std::sorted_unique, {{1, 1}, {2, 2}, {3, 3}}, c);
89 assert(!transparent_used);
90 auto it = m.lower_bound(Transparent<int>{3});
91 assert(it != m.end());
92 assert(transparent_used);
93 }
94 {
95 // LWG4239 std::string and C string literal
96 using M = std::flat_map<std::string, int, std::less<>>;
97 M m{{"alpha", 1}, {"beta", 2}, {"epsilon", 1}, {"eta", 3}, {"gamma", 3}};
98 auto it = m.lower_bound("charlie");
99 assert(it == m.begin() + 2);
100 }
101
102 return 0;
103}
104

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