brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.7 KiB · 1f049a2 Raw
102 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// <unordered_set>10 11// template <class Value, class Hash = hash<Value>, class Pred = equal_to<Value>,12//           class Alloc = allocator<Value>>13// class unordered_set14 15// iterator erase(const_iterator first, const_iterator last)16 17#include <unordered_set>18#include <algorithm>19#include <cassert>20 21#include "test_macros.h"22#include "min_allocator.h"23 24int main(int, char**) {25  {26    typedef std::unordered_set<int> C;27    typedef int P;28    P a[] = {P(1), P(2), P(3), P(4), P(1), P(2)};29    C c(a, a + sizeof(a) / sizeof(a[0]));30    C::const_iterator i = c.find(2);31    C::const_iterator j = std::next(i);32    C::iterator k       = c.erase(i, i);33    assert(k == i);34    assert(c.size() == 4);35    assert(c.count(1) == 1);36    assert(c.count(2) == 1);37    assert(c.count(3) == 1);38    assert(c.count(4) == 1);39 40    k = c.erase(i, j);41    assert(c.size() == 3);42    assert(c.count(1) == 1);43    assert(c.count(3) == 1);44    assert(c.count(4) == 1);45 46    k = c.erase(c.cbegin(), c.cend());47    assert(c.size() == 0);48    assert(k == c.end());49  }50  { // Make sure that we're properly updating the bucket list when we're erasing to the end51    std::unordered_set<int> m;52    m.insert(1);53    m.insert(2);54 55    {56      auto pair = m.equal_range(1);57      assert(pair.first != pair.second);58      m.erase(pair.first, pair.second);59    }60 61    {62      auto pair = m.equal_range(2);63      assert(pair.first != pair.second);64      m.erase(pair.first, pair.second);65    }66 67    m.insert(3);68    assert(m.size() == 1);69    assert(*m.begin() == 3);70    assert(++m.begin() == m.end());71  }72#if TEST_STD_VER >= 1173  {74    typedef std::unordered_set<int, std::hash<int>, std::equal_to<int>, min_allocator<int>> C;75    typedef int P;76    P a[] = {P(1), P(2), P(3), P(4), P(1), P(2)};77    C c(a, a + sizeof(a) / sizeof(a[0]));78    C::const_iterator i = c.find(2);79    C::const_iterator j = std::next(i);80    C::iterator k       = c.erase(i, i);81    assert(k == i);82    assert(c.size() == 4);83    assert(c.count(1) == 1);84    assert(c.count(2) == 1);85    assert(c.count(3) == 1);86    assert(c.count(4) == 1);87 88    k = c.erase(i, j);89    assert(c.size() == 3);90    assert(c.count(1) == 1);91    assert(c.count(3) == 1);92    assert(c.count(4) == 1);93 94    k = c.erase(c.cbegin(), c.cend());95    assert(c.size() == 0);96    assert(k == c.end());97  }98#endif99 100  return 0;101}102