brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.3 KiB · 013e052 Raw
119 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_multiset14 15// iterator erase(const_iterator first, const_iterator last)16 17#include <unordered_set>18#include <cassert>19#include <iterator>20 21#include "test_macros.h"22#include "min_allocator.h"23 24int main(int, char**) {25  {26    typedef std::unordered_multiset<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, 2);32    C::iterator k       = c.erase(i, i);33    assert(k == i);34    assert(c.size() == 6);35    assert(c.count(1) == 2);36    assert(c.count(2) == 2);37    assert(c.count(3) == 1);38    assert(c.count(4) == 1);39 40    k = c.erase(i, j);41    assert(c.size() == 4);42    assert(c.count(1) == 2);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 starting within a bucket51    struct MyHash {52      size_t operator()(size_t v) const { return v; }53    };54    std::unordered_multiset<size_t, MyHash> map;55    size_t collision_val = 2 + map.bucket_count(); // try to get a bucket collision56    map.rehash(3);57    map.insert(1);58    map.insert(collision_val);59    map.insert(2);60    LIBCPP_ASSERT(map.bucket(2) == map.bucket(collision_val));61 62    auto erase = map.equal_range(2);63    map.erase(erase.first, erase.second);64    for (const auto& v : map)65      assert(v == 1 || v == collision_val);66  }67  { // Make sure that we're properly updating the bucket list when we're erasing to the end68    std::unordered_multiset<int> m;69    m.insert(1);70    m.insert(2);71 72    {73      auto pair = m.equal_range(1);74      assert(pair.first != pair.second);75      m.erase(pair.first, pair.second);76    }77 78    {79      auto pair = m.equal_range(2);80      assert(pair.first != pair.second);81      m.erase(pair.first, pair.second);82    }83 84    m.insert(3);85    assert(m.size() == 1);86    assert(*m.begin() == 3);87    assert(++m.begin() == m.end());88  }89#if TEST_STD_VER >= 1190  {91    typedef std::unordered_multiset<int, std::hash<int>, std::equal_to<int>, min_allocator<int>> C;92    typedef int P;93    P a[] = {P(1), P(2), P(3), P(4), P(1), P(2)};94    C c(a, a + sizeof(a) / sizeof(a[0]));95    C::const_iterator i = c.find(2);96    C::const_iterator j = std::next(i, 2);97    C::iterator k       = c.erase(i, i);98    assert(k == i);99    assert(c.size() == 6);100    assert(c.count(1) == 2);101    assert(c.count(2) == 2);102    assert(c.count(3) == 1);103    assert(c.count(4) == 1);104 105    k = c.erase(i, j);106    assert(c.size() == 4);107    assert(c.count(1) == 2);108    assert(c.count(3) == 1);109    assert(c.count(4) == 1);110 111    k = c.erase(c.cbegin(), c.cend());112    assert(c.size() == 0);113    assert(k == c.end());114  }115#endif116 117  return 0;118}119