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