You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
libcxx_old/test/std/containers/associative/map/map.modifiers/erase_key.pass.cpp

278 lines
9.5 KiB
C++

//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// <map>
// class map
// size_type erase(const key_type& k);
#include <map>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
int main(int, char**)
{
{
typedef std::map<int, double> M;
typedef std::pair<int, double> P;
typedef M::size_type R;
P ar[] =
{
P(1, 1.5),
P(2, 2.5),
P(3, 3.5),
P(4, 4.5),
P(5, 5.5),
P(6, 6.5),
P(7, 7.5),
P(8, 8.5),
};
M m(ar, ar + sizeof(ar)/sizeof(ar[0]));
assert(m.size() == 8);
R s = m.erase(9);
assert(s == 0);
assert(m.size() == 8);
assert(m.begin()->first == 1);
assert(m.begin()->second == 1.5);
assert(next(m.begin())->first == 2);
assert(next(m.begin())->second == 2.5);
assert(next(m.begin(), 2)->first == 3);
assert(next(m.begin(), 2)->second == 3.5);
assert(next(m.begin(), 3)->first == 4);
assert(next(m.begin(), 3)->second == 4.5);
assert(next(m.begin(), 4)->first == 5);
assert(next(m.begin(), 4)->second == 5.5);
assert(next(m.begin(), 5)->first == 6);
assert(next(m.begin(), 5)->second == 6.5);
assert(next(m.begin(), 6)->first == 7);
assert(next(m.begin(), 6)->second == 7.5);
assert(next(m.begin(), 7)->first == 8);
assert(next(m.begin(), 7)->second == 8.5);
s = m.erase(4);
assert(m.size() == 7);
assert(s == 1);
assert(m.begin()->first == 1);
assert(m.begin()->second == 1.5);
assert(next(m.begin())->first == 2);
assert(next(m.begin())->second == 2.5);
assert(next(m.begin(), 2)->first == 3);
assert(next(m.begin(), 2)->second == 3.5);
assert(next(m.begin(), 3)->first == 5);
assert(next(m.begin(), 3)->second == 5.5);
assert(next(m.begin(), 4)->first == 6);
assert(next(m.begin(), 4)->second == 6.5);
assert(next(m.begin(), 5)->first == 7);
assert(next(m.begin(), 5)->second == 7.5);
assert(next(m.begin(), 6)->first == 8);
assert(next(m.begin(), 6)->second == 8.5);
s = m.erase(1);
assert(m.size() == 6);
assert(s == 1);
assert(m.begin()->first == 2);
assert(m.begin()->second == 2.5);
assert(next(m.begin())->first == 3);
assert(next(m.begin())->second == 3.5);
assert(next(m.begin(), 2)->first == 5);
assert(next(m.begin(), 2)->second == 5.5);
assert(next(m.begin(), 3)->first == 6);
assert(next(m.begin(), 3)->second == 6.5);
assert(next(m.begin(), 4)->first == 7);
assert(next(m.begin(), 4)->second == 7.5);
assert(next(m.begin(), 5)->first == 8);
assert(next(m.begin(), 5)->second == 8.5);
s = m.erase(8);
assert(m.size() == 5);
assert(s == 1);
assert(m.begin()->first == 2);
assert(m.begin()->second == 2.5);
assert(next(m.begin())->first == 3);
assert(next(m.begin())->second == 3.5);
assert(next(m.begin(), 2)->first == 5);
assert(next(m.begin(), 2)->second == 5.5);
assert(next(m.begin(), 3)->first == 6);
assert(next(m.begin(), 3)->second == 6.5);
assert(next(m.begin(), 4)->first == 7);
assert(next(m.begin(), 4)->second == 7.5);
s = m.erase(3);
assert(m.size() == 4);
assert(s == 1);
assert(m.begin()->first == 2);
assert(m.begin()->second == 2.5);
assert(next(m.begin())->first == 5);
assert(next(m.begin())->second == 5.5);
assert(next(m.begin(), 2)->first == 6);
assert(next(m.begin(), 2)->second == 6.5);
assert(next(m.begin(), 3)->first == 7);
assert(next(m.begin(), 3)->second == 7.5);
s = m.erase(6);
assert(m.size() == 3);
assert(s == 1);
assert(m.begin()->first == 2);
assert(m.begin()->second == 2.5);
assert(next(m.begin())->first == 5);
assert(next(m.begin())->second == 5.5);
assert(next(m.begin(), 2)->first == 7);
assert(next(m.begin(), 2)->second == 7.5);
s = m.erase(7);
assert(m.size() == 2);
assert(s == 1);
assert(m.begin()->first == 2);
assert(m.begin()->second == 2.5);
assert(next(m.begin())->first == 5);
assert(next(m.begin())->second == 5.5);
s = m.erase(2);
assert(m.size() == 1);
assert(s == 1);
assert(m.begin()->first == 5);
assert(m.begin()->second == 5.5);
s = m.erase(5);
assert(m.size() == 0);
assert(s == 1);
}
#if TEST_STD_VER >= 11
{
typedef std::map<int, double, std::less<int>, min_allocator<std::pair<const int, double>>> M;
typedef std::pair<int, double> P;
typedef M::size_type R;
P ar[] =
{
P(1, 1.5),
P(2, 2.5),
P(3, 3.5),
P(4, 4.5),
P(5, 5.5),
P(6, 6.5),
P(7, 7.5),
P(8, 8.5),
};
M m(ar, ar + sizeof(ar)/sizeof(ar[0]));
assert(m.size() == 8);
R s = m.erase(9);
assert(s == 0);
assert(m.size() == 8);
assert(m.begin()->first == 1);
assert(m.begin()->second == 1.5);
assert(next(m.begin())->first == 2);
assert(next(m.begin())->second == 2.5);
assert(next(m.begin(), 2)->first == 3);
assert(next(m.begin(), 2)->second == 3.5);
assert(next(m.begin(), 3)->first == 4);
assert(next(m.begin(), 3)->second == 4.5);
assert(next(m.begin(), 4)->first == 5);
assert(next(m.begin(), 4)->second == 5.5);
assert(next(m.begin(), 5)->first == 6);
assert(next(m.begin(), 5)->second == 6.5);
assert(next(m.begin(), 6)->first == 7);
assert(next(m.begin(), 6)->second == 7.5);
assert(next(m.begin(), 7)->first == 8);
assert(next(m.begin(), 7)->second == 8.5);
s = m.erase(4);
assert(m.size() == 7);
assert(s == 1);
assert(m.begin()->first == 1);
assert(m.begin()->second == 1.5);
assert(next(m.begin())->first == 2);
assert(next(m.begin())->second == 2.5);
assert(next(m.begin(), 2)->first == 3);
assert(next(m.begin(), 2)->second == 3.5);
assert(next(m.begin(), 3)->first == 5);
assert(next(m.begin(), 3)->second == 5.5);
assert(next(m.begin(), 4)->first == 6);
assert(next(m.begin(), 4)->second == 6.5);
assert(next(m.begin(), 5)->first == 7);
assert(next(m.begin(), 5)->second == 7.5);
assert(next(m.begin(), 6)->first == 8);
assert(next(m.begin(), 6)->second == 8.5);
s = m.erase(1);
assert(m.size() == 6);
assert(s == 1);
assert(m.begin()->first == 2);
assert(m.begin()->second == 2.5);
assert(next(m.begin())->first == 3);
assert(next(m.begin())->second == 3.5);
assert(next(m.begin(), 2)->first == 5);
assert(next(m.begin(), 2)->second == 5.5);
assert(next(m.begin(), 3)->first == 6);
assert(next(m.begin(), 3)->second == 6.5);
assert(next(m.begin(), 4)->first == 7);
assert(next(m.begin(), 4)->second == 7.5);
assert(next(m.begin(), 5)->first == 8);
assert(next(m.begin(), 5)->second == 8.5);
s = m.erase(8);
assert(m.size() == 5);
assert(s == 1);
assert(m.begin()->first == 2);
assert(m.begin()->second == 2.5);
assert(next(m.begin())->first == 3);
assert(next(m.begin())->second == 3.5);
assert(next(m.begin(), 2)->first == 5);
assert(next(m.begin(), 2)->second == 5.5);
assert(next(m.begin(), 3)->first == 6);
assert(next(m.begin(), 3)->second == 6.5);
assert(next(m.begin(), 4)->first == 7);
assert(next(m.begin(), 4)->second == 7.5);
s = m.erase(3);
assert(m.size() == 4);
assert(s == 1);
assert(m.begin()->first == 2);
assert(m.begin()->second == 2.5);
assert(next(m.begin())->first == 5);
assert(next(m.begin())->second == 5.5);
assert(next(m.begin(), 2)->first == 6);
assert(next(m.begin(), 2)->second == 6.5);
assert(next(m.begin(), 3)->first == 7);
assert(next(m.begin(), 3)->second == 7.5);
s = m.erase(6);
assert(m.size() == 3);
assert(s == 1);
assert(m.begin()->first == 2);
assert(m.begin()->second == 2.5);
assert(next(m.begin())->first == 5);
assert(next(m.begin())->second == 5.5);
assert(next(m.begin(), 2)->first == 7);
assert(next(m.begin(), 2)->second == 7.5);
s = m.erase(7);
assert(m.size() == 2);
assert(s == 1);
assert(m.begin()->first == 2);
assert(m.begin()->second == 2.5);
assert(next(m.begin())->first == 5);
assert(next(m.begin())->second == 5.5);
s = m.erase(2);
assert(m.size() == 1);
assert(s == 1);
assert(m.begin()->first == 5);
assert(m.begin()->second == 5.5);
s = m.erase(5);
assert(m.size() == 0);
assert(s == 1);
}
#endif
return 0;
}