95 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// 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>38constexpr void 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 56constexpr bool test() {57 test<std::vector<std::string>, std::vector<int>>();58#ifndef __cpp_lib_constexpr_deque59 if (!TEST_IS_CONSTANT_EVALUATED)60#endif61 {62 test<std::deque<std::string>, std::vector<int>>();63 }64 test<MinSequenceContainer<std::string>, MinSequenceContainer<int>>();65 test<std::vector<std::string, min_allocator<std::string>>, std::vector<int, min_allocator<int>>>();66 67 {68 bool transparent_used = false;69 TransparentComparator c(transparent_used);70 std::flat_map<int, int, TransparentComparator> m(std::sorted_unique, {{1, 1}, {2, 2}, {3, 3}}, c);71 assert(!transparent_used);72 auto n = m.count(Transparent<int>{3});73 assert(n == 1);74 assert(transparent_used);75 }76 {77 // LWG4239 std::string and C string literal78 using M = std::flat_map<std::string, int, std::less<>>;79 M m{{"alpha", 1}, {"beta", 2}, {"epsilon", 1}, {"eta", 3}, {"gamma", 3}};80 assert(m.count("alpha") == 1);81 assert(m.count("charlie") == 0);82 }83 84 return true;85}86 87int main(int, char**) {88 test();89#if TEST_STD_VER >= 2690 static_assert(test());91#endif92 93 return 0;94}95