brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.5 KiB · 88be747 Raw
125 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<container-compatible-range<value_type> R>14//   void insert_range(R&& rg);15 16#include <algorithm>17#include <deque>18#include <flat_map>19#include <functional>20#include <ranges>21#include <vector>22 23#include "MinSequenceContainer.h"24#include "../helpers.h"25#include "MoveOnly.h"26#include "test_macros.h"27#include "test_iterators.h"28#include "min_allocator.h"29 30// test constraint container-compatible-range31template <class M, class R>32concept CanInsertRange = requires(M m, R&& r) { m.insert_range(std::forward<R>(r)); };33 34using Map = std::flat_map<int, double>;35 36static_assert(CanInsertRange<Map, std::ranges::subrange<std::pair<int, double>*>>);37static_assert(CanInsertRange<Map, std::ranges::subrange<std::pair<short, double>*>>);38static_assert(!CanInsertRange<Map, std::ranges::subrange<int*>>);39static_assert(!CanInsertRange<Map, std::ranges::subrange<double*>>);40 41template <class KeyContainer, class ValueContainer>42constexpr void test() {43  using Key   = typename KeyContainer::value_type;44  using Value = typename ValueContainer::value_type;45 46  {47    using P                 = std::pair<int, int>;48    using M                 = std::flat_map<Key, Value, std::less<Key>, KeyContainer, ValueContainer>;49    using It                = forward_iterator<const P*>;50    M m                     = {{10, 1}, {8, 2}, {5, 3}, {2, 4}, {1, 5}};51    P ar[]                  = {{3, 1}, {1, 2}, {4, 3}, {1, 4}, {5, 5}, {9, 6}};52    std::ranges::subrange r = {It(ar), It(ar + 6)};53    static_assert(std::ranges::common_range<decltype(r)>);54    m.insert_range(r);55    assert((m == M{{1, 5}, {2, 4}, {3, 1}, {4, 3}, {5, 3}, {8, 2}, {9, 6}, {10, 1}}));56  }57  {58    using P                 = std::pair<int, int>;59    using M                 = std::flat_map<Key, Value, std::greater<>, KeyContainer, ValueContainer>;60    using It                = cpp20_input_iterator<const P*>;61    M m                     = {{8, 1}, {5, 2}, {3, 3}, {2, 4}};62    P ar[]                  = {{3, 1}, {1, 2}, {4, 3}, {1, 4}, {5, 5}, {9, 6}};63    std::ranges::subrange r = {It(ar), sentinel_wrapper<It>(It(ar + 6))};64    static_assert(!std::ranges::common_range<decltype(r)>);65    m.insert_range(r);66    assert((m == M{{1, 2}, {2, 4}, {3, 3}, {4, 3}, {5, 2}, {8, 1}, {9, 6}}));67  }68  {69    // The "uniquing" part uses the comparator, not operator==.70    struct ModTen {71      constexpr bool operator()(int a, int b) const { return (a % 10) < (b % 10); }72    };73    using P = std::pair<int, int>;74    using M = std::flat_map<Key, Value, ModTen, KeyContainer, ValueContainer>;75    M m     = {{21, 0}, {43, 0}, {15, 0}, {37, 0}};76    P ar[]  = {{33, 1}, {18, 1}, {55, 1}, {18, 1}, {42, 1}};77    m.insert_range(ar);78    assert((m == M{{21, 0}, {42, 1}, {43, 0}, {15, 0}, {37, 0}, {18, 1}}));79  }80}81 82constexpr bool test() {83  test<std::vector<int>, std::vector<int>>();84#ifndef __cpp_lib_constexpr_deque85  if (!TEST_IS_CONSTANT_EVALUATED)86#endif87  {88    test<std::deque<int>, std::vector<int>>();89  }90  test<MinSequenceContainer<int>, MinSequenceContainer<int>>();91  test<std::vector<int, min_allocator<int>>, std::vector<int, min_allocator<int>>>();92  {93    // Items are forwarded correctly from the input range (P2767).94    std::pair<MoveOnly, MoveOnly> a[] = {{3, 3}, {1, 1}, {4, 4}, {1, 1}, {5, 5}};95    std::flat_map<MoveOnly, MoveOnly> m;96    m.insert_range(a | std::views::as_rvalue);97    std::pair<MoveOnly, MoveOnly> expected[] = {{1, 1}, {3, 3}, {4, 4}, {5, 5}};98    assert(std::ranges::equal(m, expected));99  }100  {101    // The element type of the range doesn't need to be std::pair (P2767).102    std::pair<int, int> pa[] = {{3, 3}, {1, 1}, {4, 4}, {1, 1}, {5, 5}};103    std::vector<std::reference_wrapper<std::pair<int, int>>> a(pa, pa + 5);104    std::flat_map<int, int> m;105    m.insert_range(a);106    std::pair<int, int> expected[] = {{1, 1}, {3, 3}, {4, 4}, {5, 5}};107    assert(std::ranges::equal(m, expected));108  }109  if (!TEST_IS_CONSTANT_EVALUATED) {110    auto insert_func = [](auto& m, const auto& newValues) { m.insert_range(newValues); };111    test_insert_range_exception_guarantee(insert_func);112  }113 114  return true;115}116 117int main(int, char**) {118  test();119#if TEST_STD_VER >= 26120  static_assert(test());121#endif122 123  return 0;124}125