104 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: c++03, c++11, c++14, c++17, c++2010 11// <flat_map>12 13// class flat_multimap14 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.30template <class M>31concept CanCount = requires(M m, Transparent<int> k) { m.count(k); };32using TransparentMap = std::flat_multimap<int, double, TransparentComparator>;33using NonTransparentMap = std::flat_multimap<int, double, NonTransparentComparator>;34static_assert(CanCount<TransparentMap>);35static_assert(CanCount<const TransparentMap>);36static_assert(!CanCount<NonTransparentMap>);37static_assert(!CanCount<const NonTransparentMap>);38 39template <class KeyContainer, class ValueContainer>40constexpr 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 66constexpr bool test() {67 test<std::vector<std::string>, std::vector<int>>();68#ifndef __cpp_lib_constexpr_deque69 if (!TEST_IS_CONSTANT_EVALUATED)70#endif71 test<std::deque<std::string>, std::vector<int>>();72 test<MinSequenceContainer<std::string>, MinSequenceContainer<int>>();73 test<std::vector<std::string, min_allocator<std::string>>, std::vector<int, min_allocator<int>>>();74 75 {76 bool transparent_used = false;77 TransparentComparator c(transparent_used);78 std::flat_multimap<int, int, TransparentComparator> m(79 std::sorted_equivalent, {{1, 1}, {2, 2}, {2, 2}, {3, 3}, {3, 3}}, c);80 assert(!transparent_used);81 auto n = m.count(Transparent<int>{3});82 assert(n == 2);83 assert(transparent_used);84 }85 {86 // LWG4239 std::string and C string literal87 using M = std::flat_multimap<std::string, int, std::less<>>;88 M m{{"alpha", 1}, {"beta", 2}, {"beta", 1}, {"eta", 3}, {"gamma", 3}};89 assert(m.count("beta") == 2);90 assert(m.count("charlie") == 0);91 }92 93 return true;94}95 96int main(int, char**) {97 test();98#if TEST_STD_VER >= 2699 static_assert(test());100#endif101 102 return 0;103}104