brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.1 KiB · 7be6fd7 Raw
137 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> mapped_type&       at(const K& x);14// template<class K> const mapped_type& at(const K& x) const;15 16#include <cassert>17#include <deque>18#include <flat_map>19#include <functional>20#include <stdexcept>21 22#include "../helpers.h"23#include "min_allocator.h"24#include "MinSequenceContainer.h"25#include "test_macros.h"26 27// Constraints: The qualified-id Compare::is_transparent is valid and denotes a type.28template <class M>29concept CanAt           = requires(M m, Transparent<int> k) { m.at(k); };30using TransparentMap    = std::flat_map<int, double, TransparentComparator>;31using NonTransparentMap = std::flat_map<int, double, NonTransparentComparator>;32static_assert(CanAt<TransparentMap>);33static_assert(CanAt<const TransparentMap>);34static_assert(!CanAt<NonTransparentMap>);35static_assert(!CanAt<const NonTransparentMap>);36 37template <class KeyContainer, class ValueContainer>38constexpr void test() {39  using P = std::pair<int, double>;40  P ar[]  = {41      P(1, 1.5),42      P(2, 2.5),43      P(3, 3.5),44      P(4, 4.5),45      P(5, 5.5),46      P(7, 7.5),47      P(8, 8.5),48  };49  const Transparent<int> one{1};50  {51    std::flat_map<int, double, TransparentComparator, KeyContainer, ValueContainer> m(52        ar, ar + sizeof(ar) / sizeof(ar[0]));53    ASSERT_SAME_TYPE(decltype(m.at(one)), double&);54    assert(m.size() == 7);55    assert(m.at(one) == 1.5);56    m.at(one) = -1.5;57    assert(m.at(Transparent<int>{1}) == -1.5);58    assert(m.at(Transparent<int>{2}) == 2.5);59    assert(m.at(Transparent<int>{3}) == 3.5);60    assert(m.at(Transparent<int>{4}) == 4.5);61    assert(m.at(Transparent<int>{5}) == 5.5);62#ifndef TEST_HAS_NO_EXCEPTIONS63    if (!TEST_IS_CONSTANT_EVALUATED) {64      try {65        TEST_IGNORE_NODISCARD m.at(Transparent<int>{6});66        assert(false);67      } catch (std::out_of_range&) {68      }69    }70#endif71    assert(m.at(Transparent<int>{7}) == 7.5);72    assert(m.at(Transparent<int>{8}) == 8.5);73    assert(m.size() == 7);74  }75  {76    const std::flat_map<int, double, TransparentComparator, KeyContainer, ValueContainer> m(77        ar, ar + sizeof(ar) / sizeof(ar[0]));78    ASSERT_SAME_TYPE(decltype(m.at(one)), const double&);79    assert(m.size() == 7);80    assert(m.at(Transparent<int>{1}) == 1.5);81    assert(m.at(Transparent<int>{2}) == 2.5);82    assert(m.at(Transparent<int>{3}) == 3.5);83    assert(m.at(Transparent<int>{4}) == 4.5);84    assert(m.at(Transparent<int>{5}) == 5.5);85#ifndef TEST_HAS_NO_EXCEPTIONS86    if (!TEST_IS_CONSTANT_EVALUATED) {87      try {88        TEST_IGNORE_NODISCARD m.at(Transparent<int>{6});89        assert(false);90      } catch (std::out_of_range&) {91      }92    }93#endif94    assert(m.at(Transparent<int>{7}) == 7.5);95    assert(m.at(Transparent<int>{8}) == 8.5);96    assert(m.size() == 7);97  }98}99 100constexpr bool test() {101  test<std::vector<int>, std::vector<double>>();102#ifndef __cpp_lib_constexpr_deque103  if (!TEST_IS_CONSTANT_EVALUATED)104#endif105  {106    test<std::deque<int>, std::vector<double>>();107  }108  test<MinSequenceContainer<int>, MinSequenceContainer<double>>();109  test<std::vector<int, min_allocator<int>>, std::vector<double, min_allocator<double>>>();110  {111    bool transparent_used = false;112    TransparentComparator c(transparent_used);113    std::flat_map<int, int, TransparentComparator> m(std::sorted_unique, {{1, 1}, {2, 2}, {3, 3}}, c);114    assert(!transparent_used);115    TEST_IGNORE_NODISCARD m.at(Transparent<int>{3});116    assert(transparent_used);117  }118  {119    // LWG4239 std::string and C string literal120    using M = std::flat_map<std::string, int, std::less<>>;121    M m{{"alpha", 1}, {"beta", 2}, {"epsilon", 1}, {"eta", 3}, {"gamma", 3}};122    int& x = m.at("alpha");123    assert(x == 1);124  }125 126  return true;127}128 129int main(int, char**) {130  test();131#if TEST_STD_VER >= 26132  static_assert(test());133#endif134 135  return 0;136}137