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