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> size_type count(const K& x) const; |
14 | |
15 | #include <cassert> |
16 | #include <deque> |
17 | #include <flat_set> |
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. |
28 | template <class M> |
29 | concept CanCount = requires(M m, Transparent<int> k) { m.count(k); }; |
30 | using TransparentSet = std::flat_multiset<int, TransparentComparator>; |
31 | using NonTransparentSet = std::flat_multiset<int, NonTransparentComparator>; |
32 | static_assert(CanCount<TransparentSet>); |
33 | static_assert(CanCount<const TransparentSet>); |
34 | static_assert(!CanCount<NonTransparentSet>); |
35 | static_assert(!CanCount<const NonTransparentSet>); |
36 | |
37 | template <class KeyContainer> |
38 | void test_one() { |
39 | using Key = typename KeyContainer::value_type; |
40 | using M = std::flat_multiset<Key, TransparentComparator, KeyContainer>; |
41 | { |
42 | M m = {"alpha" , "beta" , "beta" , "beta" , "epsilon" , "eta" , "eta" , "gamma" }; |
43 | ASSERT_SAME_TYPE(decltype(m.count(Transparent<std::string>{"abc" })), typename M::size_type); |
44 | ASSERT_SAME_TYPE(decltype(std::as_const(m).count(Transparent<std::string>{"b" })), typename M::size_type); |
45 | assert(m.count(Transparent<std::string>{"alpha" }) == 1); |
46 | assert(m.count(Transparent<std::string>{"beta" }) == 3); |
47 | assert(m.count(Transparent<std::string>{"epsilon" }) == 1); |
48 | assert(m.count(Transparent<std::string>{"eta" }) == 2); |
49 | assert(m.count(Transparent<std::string>{"gamma" }) == 1); |
50 | assert(m.count(Transparent<std::string>{"al" }) == 0); |
51 | assert(m.count(Transparent<std::string>{"" }) == 0); |
52 | assert(m.count(Transparent<std::string>{"g" }) == 0); |
53 | } |
54 | { |
55 | // empty |
56 | M m; |
57 | assert(m.count(Transparent<std::string>{"alpha" }) == 0); |
58 | assert(m.count(Transparent<std::string>{"beta" }) == 0); |
59 | } |
60 | } |
61 | |
62 | void test() { |
63 | test_one<std::vector<std::string>>(); |
64 | test_one<std::deque<std::string>>(); |
65 | test_one<MinSequenceContainer<std::string>>(); |
66 | test_one<std::vector<std::string, min_allocator<std::string>>>(); |
67 | |
68 | { |
69 | bool transparent_used = false; |
70 | TransparentComparator c(transparent_used); |
71 | std::flat_multiset<int, TransparentComparator> m(std::sorted_equivalent, {1, 2, 2, 2, 3, 3, 3, 3}, c); |
72 | assert(!transparent_used); |
73 | auto n = m.count(Transparent<int>{3}); |
74 | assert(n == 4); |
75 | assert(transparent_used); |
76 | } |
77 | { |
78 | // std::string and C string literal |
79 | using M = std::flat_multiset<std::string, std::less<>>; |
80 | M m = {"alpha" , "beta" , "beta" , "epsilon" , "eta" , "gamma" }; |
81 | auto n = m.count("beta" ); |
82 | assert(n == 2); |
83 | } |
84 | } |
85 | |
86 | int main(int, char**) { |
87 | test(); |
88 | |
89 | return 0; |
90 | } |
91 | |