brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.2 KiB · a1a0d6b Raw
99 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_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.28template <class M>29concept CanCount        = requires(M m, Transparent<int> k) { m.count(k); };30using TransparentSet    = std::flat_multiset<int, TransparentComparator>;31using NonTransparentSet = std::flat_multiset<int, NonTransparentComparator>;32static_assert(CanCount<TransparentSet>);33static_assert(CanCount<const TransparentSet>);34static_assert(!CanCount<NonTransparentSet>);35static_assert(!CanCount<const NonTransparentSet>);36 37template <class KeyContainer>38constexpr 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    // empty56    M m;57    assert(m.count(Transparent<std::string>{"alpha"}) == 0);58    assert(m.count(Transparent<std::string>{"beta"}) == 0);59  }60}61 62constexpr bool test() {63  test_one<std::vector<std::string>>();64#ifndef __cpp_lib_constexpr_deque65  if (!TEST_IS_CONSTANT_EVALUATED)66#endif67    test_one<std::deque<std::string>>();68  test_one<MinSequenceContainer<std::string>>();69  test_one<std::vector<std::string, min_allocator<std::string>>>();70 71  {72    bool transparent_used = false;73    TransparentComparator c(transparent_used);74    std::flat_multiset<int, TransparentComparator> m(std::sorted_equivalent, {1, 2, 2, 2, 3, 3, 3, 3}, c);75    assert(!transparent_used);76    auto n = m.count(Transparent<int>{3});77    assert(n == 4);78    assert(transparent_used);79  }80  {81    // std::string and C string literal82    using M = std::flat_multiset<std::string, std::less<>>;83    M m     = {"alpha", "beta", "beta", "epsilon", "eta", "gamma"};84    auto n  = m.count("beta");85    assert(n == 2);86  }87 88  return true;89}90 91int main(int, char**) {92  test();93#if TEST_STD_VER >= 2694  static_assert(test());95#endif96 97  return 0;98}99