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