brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.6 KiB · a624f91 Raw
120 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_set<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_set<Key, std::less<Key>, KeyContainer>;47    using It                = forward_iterator<const int*>;48    M m                     = {10, 8, 5, 2, 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, 2, 3, 4, 5, 8, 9, 10}));54  }55  {56    using M                 = std::flat_set<Key, std::greater<>, KeyContainer>;57    using It                = cpp20_input_iterator<const int*>;58    M m                     = {8, 5, 3, 2};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, 2, 3, 4, 5, 8, 9}));64  }65  {66    // The "uniquing" part uses the comparator, not operator==.67    struct ModTen {68      constexpr bool operator()(int a, int b) const { return (a % 10) < (b % 10); }69    };70    using M  = std::flat_set<Key, ModTen, KeyContainer>;71    M m      = {21, 43, 15, 37};72    int ar[] = {33, 18, 55, 18, 42};73    m.insert_range(ar);74    assert((m == M{21, 42, 43, 15, 37, 18}));75  }76  {77    // was empty78    using M = std::flat_set<Key, std::less<Key>, KeyContainer>;79    M m;80    int ar[] = {3, 1, 4, 1, 5, 9};81    m.insert_range(ar);82    assert((m == M{1, 3, 4, 5, 9}));83  }84}85 86constexpr bool test() {87  test_one<std::vector<int>>();88#ifndef __cpp_lib_constexpr_deque89  if (!TEST_IS_CONSTANT_EVALUATED)90#endif91    test_one<std::deque<int>>();92  test_one<MinSequenceContainer<int>>();93  test_one<std::vector<int, min_allocator<int>>>();94  {95    // Items are forwarded correctly from the input range.96    MoveOnly a[] = {3, 1, 4, 1, 5};97    std::flat_set<MoveOnly> m;98    m.insert_range(a | std::views::as_rvalue);99    MoveOnly expected[] = {1, 3, 4, 5};100    assert(std::ranges::equal(m, expected));101  }102 103  return true;104}105 106void test_exception() {107  auto insert_func = [](auto& m, const auto& newValues) { m.insert_range(newValues); };108  test_insert_range_exception_guarantee(insert_func);109}110 111int main(int, char**) {112  test();113  test_exception();114#if TEST_STD_VER >= 26115  static_assert(test());116#endif117 118  return 0;119}120