brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.6 KiB · e4cce76 Raw
107 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// size_type erase(const key_type& k);14 15#include <compare>16#include <concepts>17#include <deque>18#include <flat_map>19#include <functional>20#include <utility>21#include <vector>22 23#include "MinSequenceContainer.h"24#include "../helpers.h"25#include "test_macros.h"26#include "min_allocator.h"27 28template <class KeyContainer, class ValueContainer, class Compare = std::less<>>29constexpr void test() {30  using M = std::flat_map<int, char, Compare, KeyContainer, ValueContainer>;31 32  auto make = [](std::initializer_list<int> il) {33    M m;34    for (int i : il) {35      m.emplace(i, i);36    }37    return m;38  };39  M m = make({1, 2, 3, 4, 5, 6, 7, 8});40  ASSERT_SAME_TYPE(decltype(m.erase(9)), typename M::size_type);41  auto n = m.erase(9);42  assert(n == 0);43  assert(m == make({1, 2, 3, 4, 5, 6, 7, 8}));44  n = m.erase(4);45  assert(n == 1);46  assert(m == make({1, 2, 3, 5, 6, 7, 8}));47  n = m.erase(1);48  assert(n == 1);49  assert(m == make({2, 3, 5, 6, 7, 8}));50  n = m.erase(8);51  assert(n == 1);52  assert(m == make({2, 3, 5, 6, 7}));53  n = m.erase(3);54  assert(n == 1);55  assert(m == make({2, 5, 6, 7}));56  n = m.erase(4);57  assert(n == 0);58  assert(m == make({2, 5, 6, 7}));59  n = m.erase(6);60  assert(n == 1);61  assert(m == make({2, 5, 7}));62  n = m.erase(7);63  assert(n == 1);64  assert(m == make({2, 5}));65  n = m.erase(2);66  assert(n == 1);67  assert(m == make({5}));68  n = m.erase(5);69  assert(n == 1);70  assert(m.empty());71}72 73constexpr bool test() {74  test<std::vector<int>, std::vector<char>>();75  test<std::vector<int>, std::vector<char>, std::greater<>>();76 77#ifndef __cpp_lib_constexpr_deque78  if (!TEST_IS_CONSTANT_EVALUATED)79#endif80  {81    test<std::deque<int>, std::vector<char>>();82  }83  test<MinSequenceContainer<int>, MinSequenceContainer<char>>();84  test<std::vector<int, min_allocator<int>>, std::vector<char, min_allocator<char>>>();85 86  if (!TEST_IS_CONSTANT_EVALUATED) {87    auto erase_function = [](auto& m, auto key_arg) {88      using Map = std::decay_t<decltype(m)>;89      using Key = typename Map::key_type;90      const Key key{key_arg};91      m.erase(key);92    };93    test_erase_exception_guarantee(erase_function);94  }95 96  return true;97}98 99int main(int, char**) {100  test();101#if TEST_STD_VER >= 26102  static_assert(test());103#endif104 105  return 0;106}107