llvm-project
277 строк · 10.0 Кб
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-exception
6//
7//===----------------------------------------------------------------------===//
8
9// <map>
10
11// class map
12
13// size_type erase(const key_type& k);
14
15#include <map>16#include <cassert>17
18#include "test_macros.h"19#include "min_allocator.h"20
21int main(int, char**)22{
23{24typedef std::map<int, double> M;25typedef std::pair<int, double> P;26typedef M::size_type R;27P ar[] =28{29P(1, 1.5),30P(2, 2.5),31P(3, 3.5),32P(4, 4.5),33P(5, 5.5),34P(6, 6.5),35P(7, 7.5),36P(8, 8.5),37};38M m(ar, ar + sizeof(ar)/sizeof(ar[0]));39assert(m.size() == 8);40R s = m.erase(9);41assert(s == 0);42assert(m.size() == 8);43assert(m.begin()->first == 1);44assert(m.begin()->second == 1.5);45assert(std::next(m.begin())->first == 2);46assert(std::next(m.begin())->second == 2.5);47assert(std::next(m.begin(), 2)->first == 3);48assert(std::next(m.begin(), 2)->second == 3.5);49assert(std::next(m.begin(), 3)->first == 4);50assert(std::next(m.begin(), 3)->second == 4.5);51assert(std::next(m.begin(), 4)->first == 5);52assert(std::next(m.begin(), 4)->second == 5.5);53assert(std::next(m.begin(), 5)->first == 6);54assert(std::next(m.begin(), 5)->second == 6.5);55assert(std::next(m.begin(), 6)->first == 7);56assert(std::next(m.begin(), 6)->second == 7.5);57assert(std::next(m.begin(), 7)->first == 8);58assert(std::next(m.begin(), 7)->second == 8.5);59
60s = m.erase(4);61assert(m.size() == 7);62assert(s == 1);63assert(m.begin()->first == 1);64assert(m.begin()->second == 1.5);65assert(std::next(m.begin())->first == 2);66assert(std::next(m.begin())->second == 2.5);67assert(std::next(m.begin(), 2)->first == 3);68assert(std::next(m.begin(), 2)->second == 3.5);69assert(std::next(m.begin(), 3)->first == 5);70assert(std::next(m.begin(), 3)->second == 5.5);71assert(std::next(m.begin(), 4)->first == 6);72assert(std::next(m.begin(), 4)->second == 6.5);73assert(std::next(m.begin(), 5)->first == 7);74assert(std::next(m.begin(), 5)->second == 7.5);75assert(std::next(m.begin(), 6)->first == 8);76assert(std::next(m.begin(), 6)->second == 8.5);77
78s = m.erase(1);79assert(m.size() == 6);80assert(s == 1);81assert(m.begin()->first == 2);82assert(m.begin()->second == 2.5);83assert(std::next(m.begin())->first == 3);84assert(std::next(m.begin())->second == 3.5);85assert(std::next(m.begin(), 2)->first == 5);86assert(std::next(m.begin(), 2)->second == 5.5);87assert(std::next(m.begin(), 3)->first == 6);88assert(std::next(m.begin(), 3)->second == 6.5);89assert(std::next(m.begin(), 4)->first == 7);90assert(std::next(m.begin(), 4)->second == 7.5);91assert(std::next(m.begin(), 5)->first == 8);92assert(std::next(m.begin(), 5)->second == 8.5);93
94s = m.erase(8);95assert(m.size() == 5);96assert(s == 1);97assert(m.begin()->first == 2);98assert(m.begin()->second == 2.5);99assert(std::next(m.begin())->first == 3);100assert(std::next(m.begin())->second == 3.5);101assert(std::next(m.begin(), 2)->first == 5);102assert(std::next(m.begin(), 2)->second == 5.5);103assert(std::next(m.begin(), 3)->first == 6);104assert(std::next(m.begin(), 3)->second == 6.5);105assert(std::next(m.begin(), 4)->first == 7);106assert(std::next(m.begin(), 4)->second == 7.5);107
108s = m.erase(3);109assert(m.size() == 4);110assert(s == 1);111assert(m.begin()->first == 2);112assert(m.begin()->second == 2.5);113assert(std::next(m.begin())->first == 5);114assert(std::next(m.begin())->second == 5.5);115assert(std::next(m.begin(), 2)->first == 6);116assert(std::next(m.begin(), 2)->second == 6.5);117assert(std::next(m.begin(), 3)->first == 7);118assert(std::next(m.begin(), 3)->second == 7.5);119
120s = m.erase(6);121assert(m.size() == 3);122assert(s == 1);123assert(m.begin()->first == 2);124assert(m.begin()->second == 2.5);125assert(std::next(m.begin())->first == 5);126assert(std::next(m.begin())->second == 5.5);127assert(std::next(m.begin(), 2)->first == 7);128assert(std::next(m.begin(), 2)->second == 7.5);129
130s = m.erase(7);131assert(m.size() == 2);132assert(s == 1);133assert(m.begin()->first == 2);134assert(m.begin()->second == 2.5);135assert(std::next(m.begin())->first == 5);136assert(std::next(m.begin())->second == 5.5);137
138s = m.erase(2);139assert(m.size() == 1);140assert(s == 1);141assert(m.begin()->first == 5);142assert(m.begin()->second == 5.5);143
144s = m.erase(5);145assert(m.size() == 0);146assert(s == 1);147}148#if TEST_STD_VER >= 11149{150typedef std::map<int, double, std::less<int>, min_allocator<std::pair<const int, double>>> M;151typedef std::pair<int, double> P;152typedef M::size_type R;153P ar[] =154{155P(1, 1.5),156P(2, 2.5),157P(3, 3.5),158P(4, 4.5),159P(5, 5.5),160P(6, 6.5),161P(7, 7.5),162P(8, 8.5),163};164M m(ar, ar + sizeof(ar)/sizeof(ar[0]));165assert(m.size() == 8);166R s = m.erase(9);167assert(s == 0);168assert(m.size() == 8);169assert(m.begin()->first == 1);170assert(m.begin()->second == 1.5);171assert(std::next(m.begin())->first == 2);172assert(std::next(m.begin())->second == 2.5);173assert(std::next(m.begin(), 2)->first == 3);174assert(std::next(m.begin(), 2)->second == 3.5);175assert(std::next(m.begin(), 3)->first == 4);176assert(std::next(m.begin(), 3)->second == 4.5);177assert(std::next(m.begin(), 4)->first == 5);178assert(std::next(m.begin(), 4)->second == 5.5);179assert(std::next(m.begin(), 5)->first == 6);180assert(std::next(m.begin(), 5)->second == 6.5);181assert(std::next(m.begin(), 6)->first == 7);182assert(std::next(m.begin(), 6)->second == 7.5);183assert(std::next(m.begin(), 7)->first == 8);184assert(std::next(m.begin(), 7)->second == 8.5);185
186s = m.erase(4);187assert(m.size() == 7);188assert(s == 1);189assert(m.begin()->first == 1);190assert(m.begin()->second == 1.5);191assert(std::next(m.begin())->first == 2);192assert(std::next(m.begin())->second == 2.5);193assert(std::next(m.begin(), 2)->first == 3);194assert(std::next(m.begin(), 2)->second == 3.5);195assert(std::next(m.begin(), 3)->first == 5);196assert(std::next(m.begin(), 3)->second == 5.5);197assert(std::next(m.begin(), 4)->first == 6);198assert(std::next(m.begin(), 4)->second == 6.5);199assert(std::next(m.begin(), 5)->first == 7);200assert(std::next(m.begin(), 5)->second == 7.5);201assert(std::next(m.begin(), 6)->first == 8);202assert(std::next(m.begin(), 6)->second == 8.5);203
204s = m.erase(1);205assert(m.size() == 6);206assert(s == 1);207assert(m.begin()->first == 2);208assert(m.begin()->second == 2.5);209assert(std::next(m.begin())->first == 3);210assert(std::next(m.begin())->second == 3.5);211assert(std::next(m.begin(), 2)->first == 5);212assert(std::next(m.begin(), 2)->second == 5.5);213assert(std::next(m.begin(), 3)->first == 6);214assert(std::next(m.begin(), 3)->second == 6.5);215assert(std::next(m.begin(), 4)->first == 7);216assert(std::next(m.begin(), 4)->second == 7.5);217assert(std::next(m.begin(), 5)->first == 8);218assert(std::next(m.begin(), 5)->second == 8.5);219
220s = m.erase(8);221assert(m.size() == 5);222assert(s == 1);223assert(m.begin()->first == 2);224assert(m.begin()->second == 2.5);225assert(std::next(m.begin())->first == 3);226assert(std::next(m.begin())->second == 3.5);227assert(std::next(m.begin(), 2)->first == 5);228assert(std::next(m.begin(), 2)->second == 5.5);229assert(std::next(m.begin(), 3)->first == 6);230assert(std::next(m.begin(), 3)->second == 6.5);231assert(std::next(m.begin(), 4)->first == 7);232assert(std::next(m.begin(), 4)->second == 7.5);233
234s = m.erase(3);235assert(m.size() == 4);236assert(s == 1);237assert(m.begin()->first == 2);238assert(m.begin()->second == 2.5);239assert(std::next(m.begin())->first == 5);240assert(std::next(m.begin())->second == 5.5);241assert(std::next(m.begin(), 2)->first == 6);242assert(std::next(m.begin(), 2)->second == 6.5);243assert(std::next(m.begin(), 3)->first == 7);244assert(std::next(m.begin(), 3)->second == 7.5);245
246s = m.erase(6);247assert(m.size() == 3);248assert(s == 1);249assert(m.begin()->first == 2);250assert(m.begin()->second == 2.5);251assert(std::next(m.begin())->first == 5);252assert(std::next(m.begin())->second == 5.5);253assert(std::next(m.begin(), 2)->first == 7);254assert(std::next(m.begin(), 2)->second == 7.5);255
256s = m.erase(7);257assert(m.size() == 2);258assert(s == 1);259assert(m.begin()->first == 2);260assert(m.begin()->second == 2.5);261assert(std::next(m.begin())->first == 5);262assert(std::next(m.begin())->second == 5.5);263
264s = m.erase(2);265assert(m.size() == 1);266assert(s == 1);267assert(m.begin()->first == 5);268assert(m.begin()->second == 5.5);269
270s = m.erase(5);271assert(m.size() == 0);272assert(s == 1);273}274#endif275
276return 0;277}
278