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

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