brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.3 KiB · 9862936 Raw
132 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& operator[](K&& x);14 15#include <cassert>16#include <deque>17#include <flat_map>18#include <functional>19#include <vector>20 21#include "MinSequenceContainer.h"22#include "../helpers.h"23#include "test_macros.h"24#include "min_allocator.h"25 26// Constraints:27// The qualified-id Compare::is_transparent is valid and denotes a type.28// is_constructible_v<key_type, K> is true.29// is_constructible_v<mapped_type, Args...> is true.30// is_convertible_v<K&&, const_iterator> and is_convertible_v<K&&, iterator> are both false31template <class M, class Input>32concept CanIndex                      = requires(M m, Input k) { m[k]; };33using TransparentMap                  = std::flat_map<int, double, TransparentComparator>;34using NonTransparentMap               = std::flat_map<int, double, NonTransparentComparator>;35using TransparentNoDefaultCtrValueMap = std::flat_map<int, NoDefaultCtr, TransparentComparator>;36 37static_assert(CanIndex<TransparentMap, ConvertibleTransparent<int>>);38static_assert(!CanIndex<const TransparentMap, ConvertibleTransparent<int>>);39 40static_assert(!CanIndex<NonTransparentMap, NonConvertibleTransparent<int>>);41static_assert(!CanIndex<const NonTransparentMap, NonConvertibleTransparent<int>>);42 43static_assert(!CanIndex<TransparentMap, NonConvertibleTransparent<int>>);44static_assert(!CanIndex<const TransparentMap, NonConvertibleTransparent<int>>);45 46static_assert(!CanIndex<TransparentNoDefaultCtrValueMap, ConvertibleTransparent<int>>);47static_assert(!CanIndex<const TransparentNoDefaultCtrValueMap, ConvertibleTransparent<int>>);48 49static_assert(!CanIndex<TransparentMap, TransparentMap::iterator>);50static_assert(!CanIndex<TransparentMap, TransparentMap::const_iterator>);51 52template <class KeyContainer, class ValueContainer>53constexpr void test() {54  using P = std::pair<int, double>;55  P ar[]  = {56      P(1, 1.5),57      P(2, 2.5),58      P(3, 3.5),59      P(4, 4.5),60      P(5, 5.5),61      P(7, 7.5),62      P(8, 8.5),63  };64  const ConvertibleTransparent<int> one{1};65  const ConvertibleTransparent<int> six{6};66  {67    std::flat_map<int, double, TransparentComparator, KeyContainer, ValueContainer> m(68        ar, ar + sizeof(ar) / sizeof(ar[0]));69    ASSERT_SAME_TYPE(decltype(m[one]), double&);70    assert(m.size() == 7);71    assert(m[one] == 1.5);72    assert(m.size() == 7);73    m[one] = -1.5;74    assert(m[one] == -1.5);75    assert(m.size() == 7);76    assert(m[six] == 0);77    assert(m.size() == 8);78    m[six] = 6.5;79    assert(m[six] == 6.5);80    assert(m.size() == 8);81  }82}83 84constexpr bool test() {85  test<std::vector<int>, std::vector<double>>();86#ifndef __cpp_lib_constexpr_deque87  if (!TEST_IS_CONSTANT_EVALUATED)88#endif89  {90    test<std::deque<int>, std::vector<double>>();91  }92  test<MinSequenceContainer<int>, MinSequenceContainer<double>>();93  test<std::vector<int, min_allocator<int>>, std::vector<double, min_allocator<double>>>();94 95  {96    bool transparent_used = false;97    TransparentComparator c(transparent_used);98    std::flat_map<int, int, TransparentComparator> m(std::sorted_unique, {{1, 1}, {2, 2}, {3, 3}}, c);99    assert(!transparent_used);100    (void)m[ConvertibleTransparent<int>{3}];101    assert(transparent_used);102  }103  {104    // LWG4239 std::string and C string literal105    using M = std::flat_map<std::string, int, std::less<>>;106    M m{{"alpha", 1}, {"beta", 2}, {"epsilon", 1}, {"eta", 3}, {"gamma", 3}};107    int& x = m["alpha"];108    assert(x == 1);109  }110 111  if (!TEST_IS_CONSTANT_EVALUATED) {112    auto index_func = [](auto& m, auto key_arg, auto value_arg) {113      using FlatMap                             = std::decay_t<decltype(m)>;114      using Key                                 = typename FlatMap::key_type;115      const typename FlatMap::mapped_type value = value_arg;116      m[ConvertibleTransparent<Key>{key_arg}]   = value;117    };118    test_emplace_exception_guarantee(index_func);119  }120 121  return true;122}123 124int main(int, char**) {125  test();126#if TEST_STD_VER >= 26127  static_assert(test());128#endif129 130  return 0;131}132