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

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