brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.8 KiB · b33f644 Raw
145 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> iterator insert(P&& x);16// template<class K> iterator insert(const_iterator hint, P&& x);17 18#include <algorithm>19#include <compare>20#include <concepts>21#include <deque>22#include <flat_map>23#include <functional>24#include <tuple>25 26#include "MinSequenceContainer.h"27#include "../helpers.h"28#include "test_macros.h"29#include "test_iterators.h"30#include "min_allocator.h"31 32// Constraints: is_constructible_v<pair<key_type, mapped_type>, P> is true.33template <class M, class... Args>34concept CanInsert = requires(M m, Args&&... args) { m.insert(std::forward<Args>(args)...); };35 36using Map  = std::flat_multimap<int, double>;37using Iter = Map::const_iterator;38 39static_assert(CanInsert<Map, std::pair<short, double>&&>);40static_assert(CanInsert<Map, Iter, std::pair<short, double>&&>);41static_assert(CanInsert<Map, std::tuple<short, double>&&>);42static_assert(CanInsert<Map, Iter, std::tuple<short, double>&&>);43static_assert(!CanInsert<Map, int>);44static_assert(!CanInsert<Map, Iter, int>);45 46template <class KeyContainer, class ValueContainer>47constexpr void test() {48  using Key   = typename KeyContainer::value_type;49  using Value = typename ValueContainer::value_type;50  using M     = std::flat_multimap<Key, Value, TransparentComparator, KeyContainer, ValueContainer>;51 52  {53    // insert(P&&)54    bool transparent_used = false;55    TransparentComparator c(transparent_used);56    M m(std::sorted_equivalent, {{1, 1}, {2, 2}, {2, 3}, {4, 4}}, c);57    assert(!transparent_used);58 59    std::same_as<typename M::iterator> decltype(auto) res = m.insert(std::pair(ConvertibleTransparent<int>{3}, 3));60 61    assert(res->first == 3);62    assert(res->second == 3);63    //   Unlike flat_set, here we can't use key_compare to compare value_type versus P,64    //   so we must eagerly convert to value_type.65    assert(!transparent_used);66  }67  {68    // insert(const_iterator, P&&)69    bool transparent_used = false;70    TransparentComparator c(transparent_used);71    M m(std::sorted_equivalent, {{1, 1}, {2, 2}, {2, 3}, {4, 4}}, c);72    std::same_as<typename M::iterator> decltype(auto) res =73        m.insert(m.begin(), std::pair(ConvertibleTransparent<int>{3}, 3));74    assert(res->first == 3);75    assert(res->second == 3);76    //   Unlike flat_set, here we can't use key_compare to compare value_type versus P,77    //   so we must eagerly convert to value_type.78    assert(!transparent_used);79  }80}81 82constexpr bool test() {83  test<std::vector<int>, std::vector<double>>();84#ifndef __cpp_lib_constexpr_deque85  if (!TEST_IS_CONSTANT_EVALUATED)86#endif87    test<std::deque<int>, std::vector<double>>();88  test<MinSequenceContainer<int>, MinSequenceContainer<double>>();89  test<std::vector<int, min_allocator<int>>, std::vector<double, min_allocator<double>>>();90 91  {92    // no ambiguity between insert(pos, P&&) and insert(first, last)93    using M = std::flat_multimap<int, int>;94    struct Evil {95      operator M::value_type() const;96      operator M::const_iterator() const;97    };98    std::flat_multimap<int, int> m;99    ASSERT_SAME_TYPE(decltype(m.insert(Evil())), M::iterator);100    ASSERT_SAME_TYPE(decltype(m.insert(m.begin(), Evil())), M::iterator);101    ASSERT_SAME_TYPE(decltype(m.insert(m.begin(), m.end())), void);102  }103 104  if (!TEST_IS_CONSTANT_EVALUATED) {105    {106      auto insert_func = [](auto& m, auto key_arg, auto value_arg) {107        using FlatMap    = std::decay_t<decltype(m)>;108        using tuple_type = std::tuple<typename FlatMap::key_type, typename FlatMap::mapped_type>;109        tuple_type t(key_arg, value_arg);110        m.insert(t);111      };112      test_emplace_exception_guarantee(insert_func);113    }114    {115      auto insert_func_iter = [](auto& m, auto key_arg, auto value_arg) {116        using FlatMap    = std::decay_t<decltype(m)>;117        using tuple_type = std::tuple<typename FlatMap::key_type, typename FlatMap::mapped_type>;118        tuple_type t(key_arg, value_arg);119        m.insert(m.begin(), t);120      };121      test_emplace_exception_guarantee(insert_func_iter);122    }123  }124  {125    // LWG4239 std::string and C string literal126    using M = std::flat_multimap<std::string, int, std::less<>>;127    M m{{"alpha", 1}, {"beta", 2}, {"beta", 1}, {"eta", 3}, {"gamma", 3}};128    auto it = m.insert({"beta", 1});129    assert(it == m.begin() + 3);130    auto it2 = m.insert(m.begin(), {"beta2", 2});131    assert(it2 == m.begin() + 4);132  }133 134  return true;135}136 137int main(int, char**) {138  test();139#if TEST_STD_VER >= 26140  static_assert(test());141#endif142 143  return 0;144}145