brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.3 KiB · a9d8f7e Raw
109 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<container-compatible-range<value_type> R>14//   void insert_range(R&& rg);15 16#include <algorithm>17#include <deque>18#include <flat_set>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 Set = std::flat_multiset<int, double>;35 36static_assert(CanInsertRange<Set, std::ranges::subrange<int*>>);37static_assert(CanInsertRange<Set, std::ranges::subrange<short*>>);38static_assert(!CanInsertRange<Set, std::ranges::subrange<std::pair<int, int>*>>);39static_assert(!CanInsertRange<Set, std::ranges::subrange<std::pair<short, short>*>>);40 41template <class KeyContainer>42constexpr void test_one() {43  using Key = typename KeyContainer::value_type;44 45  {46    using M                 = std::flat_multiset<Key, std::less<Key>, KeyContainer>;47    using It                = forward_iterator<const int*>;48    M m                     = {10, 10, 8, 5, 2, 1, 1};49    int ar[]                = {3, 1, 4, 1, 5, 9};50    std::ranges::subrange r = {It(ar), It(ar + 6)};51    static_assert(std::ranges::common_range<decltype(r)>);52    m.insert_range(r);53    assert((m == M{1, 1, 1, 1, 2, 3, 4, 5, 5, 8, 9, 10, 10}));54  }55  {56    using M                 = std::flat_multiset<Key, std::greater<>, KeyContainer>;57    using It                = cpp20_input_iterator<const int*>;58    M m                     = {10, 10, 8, 5, 2, 1, 1};59    int ar[]                = {3, 1, 4, 1, 5, 9};60    std::ranges::subrange r = {It(ar), sentinel_wrapper<It>(It(ar + 6))};61    static_assert(!std::ranges::common_range<decltype(r)>);62    m.insert_range(r);63    assert((m == M{1, 1, 1, 1, 2, 3, 4, 5, 5, 8, 9, 10, 10}));64  }65  {66    // was empty67    using M = std::flat_multiset<Key, std::less<Key>, KeyContainer>;68    M m;69    int ar[] = {3, 1, 4, 1, 5, 9};70    m.insert_range(ar);71    assert((m == M{1, 1, 3, 4, 5, 9}));72  }73}74 75constexpr bool test() {76  test_one<std::vector<int>>();77#ifndef __cpp_lib_constexpr_deque78  if (!TEST_IS_CONSTANT_EVALUATED)79#endif80    test_one<std::deque<int>>();81  test_one<MinSequenceContainer<int>>();82  test_one<std::vector<int, min_allocator<int>>>();83  {84    // Items are forwarded correctly from the input range.85    MoveOnly a[] = {3, 1, 4, 1, 5};86    std::flat_multiset<MoveOnly> m;87    m.insert_range(a | std::views::as_rvalue);88    MoveOnly expected[] = {1, 1, 3, 4, 5};89    assert(std::ranges::equal(m, expected));90  }91 92  return true;93}94 95void test_exception() {96  auto insert_func = [](auto& m, const auto& newValues) { m.insert_range(newValues); };97  test_insert_range_exception_guarantee(insert_func);98}99 100int main(int, char**) {101  test();102#if TEST_STD_VER >= 26103  static_assert(test());104#endif105  test_exception();106 107  return 0;108}109