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> size_type count(const K& x) const;
14
15#include <cassert>
16#include <deque>
17#include <flat_map>
18#include <functional>
19#include <string>
20#include <utility>
21
22#include "MinSequenceContainer.h"
23#include "../helpers.h"
24#include "test_macros.h"
25#include "min_allocator.h"
26
27// Constraints: The qualified-id Compare::is_transparent is valid and denotes a type.
28template <class M>
29concept CanCount = requires(M m, Transparent<int> k) { m.count(k); };
30using TransparentMap = std::flat_map<int, double, TransparentComparator>;
31using NonTransparentMap = std::flat_map<int, double, NonTransparentComparator>;
32static_assert(CanCount<TransparentMap>);
33static_assert(CanCount<const TransparentMap>);
34static_assert(!CanCount<NonTransparentMap>);
35static_assert(!CanCount<const NonTransparentMap>);
36
37template <class KeyContainer, class ValueContainer>
38void test() {
39 using Key = typename KeyContainer::value_type;
40 using Value = typename ValueContainer::value_type;
41 using M = std::flat_map<Key, Value, TransparentComparator, KeyContainer, ValueContainer>;
42
43 M m = {{"alpha", 1}, {"beta", 2}, {"epsilon", 3}, {"eta", 4}, {"gamma", 5}};
44 ASSERT_SAME_TYPE(decltype(m.count(Transparent<std::string>{"abc"})), typename M::size_type);
45 ASSERT_SAME_TYPE(decltype(std::as_const(m).count(Transparent<std::string>{"b"})), typename M::size_type);
46 assert(m.count(Transparent<std::string>{"alpha"}) == 1);
47 assert(m.count(Transparent<std::string>{"beta"}) == 1);
48 assert(m.count(Transparent<std::string>{"epsilon"}) == 1);
49 assert(m.count(Transparent<std::string>{"eta"}) == 1);
50 assert(m.count(Transparent<std::string>{"gamma"}) == 1);
51 assert(m.count(Transparent<std::string>{"al"}) == 0);
52 assert(m.count(Transparent<std::string>{""}) == 0);
53 assert(m.count(Transparent<std::string>{"g"}) == 0);
54}
55
56int main(int, char**) {
57 test<std::vector<std::string>, std::vector<int>>();
58 test<std::deque<std::string>, std::vector<int>>();
59 test<MinSequenceContainer<std::string>, MinSequenceContainer<int>>();
60 test<std::vector<std::string, min_allocator<std::string>>, std::vector<int, min_allocator<int>>>();
61
62 {
63 bool transparent_used = false;
64 TransparentComparator c(transparent_used);
65 std::flat_map<int, int, TransparentComparator> m(std::sorted_unique, {{1, 1}, {2, 2}, {3, 3}}, c);
66 assert(!transparent_used);
67 auto n = m.count(Transparent<int>{3});
68 assert(n == 1);
69 assert(transparent_used);
70 }
71 {
72 // LWG4239 std::string and C string literal
73 using M = std::flat_map<std::string, int, std::less<>>;
74 M m{{"alpha", 1}, {"beta", 2}, {"epsilon", 1}, {"eta", 3}, {"gamma", 3}};
75 assert(m.count("alpha") == 1);
76 assert(m.count("charlie") == 0);
77 }
78
79 return 0;
80}
81

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