brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.1 KiB · c908f5d Raw
123 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// iterator erase(iterator position);14// iterator erase(const_iterator position);15 16#include <compare>17#include <concepts>18#include <deque>19#include <flat_set>20#include <functional>21#include <utility>22#include <vector>23 24#include "MinSequenceContainer.h"25#include "../helpers.h"26#include "test_macros.h"27#include "min_allocator.h"28 29template <class KeyContainer>30constexpr void test_one() {31  using Key = typename KeyContainer::value_type;32  using M   = std::flat_set<Key, std::less<Key>, KeyContainer>;33  using I   = M::iterator;34 35  int ar[] = {36      1,37      2,38      3,39      4,40      5,41      6,42      7,43      8,44  };45  M m(ar, ar + sizeof(ar) / sizeof(ar[0]));46 47  auto make = [](std::initializer_list<int> il) {48    M m2;49    for (int i : il) {50      m2.emplace(i);51    }52    return m2;53  };54  assert(m.size() == 8);55  assert(m == make({1, 2, 3, 4, 5, 6, 7, 8}));56  std::same_as<I> decltype(auto) i1 = m.erase(std::next(m.cbegin(), 3));57  assert(m.size() == 7);58  assert(i1 == std::next(m.begin(), 3));59  assert(m == make({1, 2, 3, 5, 6, 7, 8}));60 61  std::same_as<I> decltype(auto) i2 = m.erase(std::next(m.begin(), 0));62  assert(m.size() == 6);63  assert(i2 == m.begin());64  assert(m == make({2, 3, 5, 6, 7, 8}));65 66  std::same_as<I> decltype(auto) i3 = m.erase(std::next(m.cbegin(), 5));67  assert(m.size() == 5);68  assert(i3 == m.end());69  assert(m == make({2, 3, 5, 6, 7}));70 71  std::same_as<I> decltype(auto) i4 = m.erase(std::next(m.begin(), 1));72  assert(m.size() == 4);73  assert(i4 == std::next(m.begin()));74  assert(m == make({2, 5, 6, 7}));75 76  std::same_as<I> decltype(auto) i5 = m.erase(std::next(m.cbegin(), 2));77  assert(m.size() == 3);78  assert(i5 == std::next(m.begin(), 2));79  assert(m == make({2, 5, 7}));80 81  std::same_as<I> decltype(auto) i6 = m.erase(std::next(m.begin(), 2));82  assert(m.size() == 2);83  assert(i6 == std::next(m.begin(), 2));84  assert(m == make({2, 5}));85 86  std::same_as<I> decltype(auto) i7 = m.erase(std::next(m.cbegin(), 0));87  assert(m.size() == 1);88  assert(i7 == std::next(m.begin(), 0));89  assert(m == make({5}));90 91  std::same_as<I> decltype(auto) i8 = m.erase(m.begin());92  assert(m.size() == 0);93  assert(i8 == m.begin());94  assert(i8 == m.end());95}96 97constexpr bool test() {98  test_one<std::vector<int>>();99#ifndef __cpp_lib_constexpr_deque100  if (!TEST_IS_CONSTANT_EVALUATED)101#endif102    test_one<std::deque<int>>();103  test_one<MinSequenceContainer<int>>();104  test_one<std::vector<int, min_allocator<int>>>();105 106  return true;107}108 109void test_exception() {110  auto erase_function = [](auto& m, auto) { m.erase(m.begin() + 2); };111  test_erase_exception_guarantee(erase_function);112}113 114int main(int, char**) {115  test();116  test_exception();117#if TEST_STD_VER >= 26118  static_assert(test());119#endif120 121  return 0;122}123