brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.6 KiB · 5a05d37 Raw
105 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_map>10 11// template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>,12//           class Alloc = allocator<pair<const Key, T>>>13// class unordered_map14 15// void rehash(size_type n);16 17#include <unordered_map>18#include <string>19#include <cassert>20 21#include "test_macros.h"22#include "min_allocator.h"23 24template <class C>25void rehash_postcondition(const C& c, std::size_t n) {26  assert(c.bucket_count() >= c.size() / c.max_load_factor() && c.bucket_count() >= n);27}28 29template <class C>30void test(const C& c) {31  assert(c.size() == 4);32  assert(c.at(1) == "one");33  assert(c.at(2) == "two");34  assert(c.at(3) == "three");35  assert(c.at(4) == "four");36}37 38int main(int, char**) {39  {40    typedef std::unordered_map<int, std::string> C;41    typedef std::pair<int, std::string> P;42    P a[] = {43        P(1, "one"),44        P(2, "two"),45        P(3, "three"),46        P(4, "four"),47        P(1, "four"),48        P(2, "four"),49    };50    C c(a, a + sizeof(a) / sizeof(a[0]));51    test(c);52    assert(c.bucket_count() >= 5);53    c.rehash(3);54    rehash_postcondition(c, 3);55    LIBCPP_ASSERT(c.bucket_count() == 5);56    test(c);57    c.max_load_factor(2);58    c.rehash(3);59    rehash_postcondition(c, 3);60    LIBCPP_ASSERT(c.bucket_count() == 3);61    test(c);62    c.rehash(31);63    rehash_postcondition(c, 31);64    LIBCPP_ASSERT(c.bucket_count() == 31);65    test(c);66  }67#if TEST_STD_VER >= 1168  {69    typedef std::unordered_map<int,70                               std::string,71                               std::hash<int>,72                               std::equal_to<int>,73                               min_allocator<std::pair<const int, std::string>>>74        C;75    typedef std::pair<int, std::string> P;76    P a[] = {77        P(1, "one"),78        P(2, "two"),79        P(3, "three"),80        P(4, "four"),81        P(1, "four"),82        P(2, "four"),83    };84    C c(a, a + sizeof(a) / sizeof(a[0]));85    test(c);86    assert(c.bucket_count() >= 5);87    c.rehash(3);88    rehash_postcondition(c, 3);89    LIBCPP_ASSERT(c.bucket_count() == 5);90    test(c);91    c.max_load_factor(2);92    c.rehash(3);93    rehash_postcondition(c, 3);94    LIBCPP_ASSERT(c.bucket_count() == 3);95    test(c);96    c.rehash(31);97    rehash_postcondition(c, 31);98    LIBCPP_ASSERT(c.bucket_count() == 31);99    test(c);100  }101#endif102 103  return 0;104}105