88 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// void reserve(size_type n);16 17#include <unordered_set>18#include <cassert>19 20#include "test_macros.h"21#include "min_allocator.h"22 23template <class C>24void test(const C& c) {25 assert(c.size() == 4);26 assert(c.count(1) == 1);27 assert(c.count(2) == 1);28 assert(c.count(3) == 1);29 assert(c.count(4) == 1);30}31 32void reserve_invariant(std::size_t n) // LWG #215633{34 for (std::size_t i = 0; i < n; ++i) {35 std::unordered_set<std::size_t> c;36 c.reserve(n);37 std::size_t buckets = c.bucket_count();38 for (std::size_t j = 0; j < i; ++j) {39 c.insert(i);40 assert(buckets == c.bucket_count());41 }42 }43}44 45int main(int, char**) {46 {47 typedef std::unordered_set<int> C;48 typedef int P;49 P a[] = {P(1), P(2), P(3), P(4), P(1), P(2)};50 C c(a, a + sizeof(a) / sizeof(a[0]));51 test(c);52 assert(c.bucket_count() >= 5);53 c.reserve(3);54 LIBCPP_ASSERT(c.bucket_count() == 5);55 test(c);56 c.max_load_factor(2);57 c.reserve(3);58 assert(c.bucket_count() >= 2);59 test(c);60 c.reserve(31);61 assert(c.bucket_count() >= 16);62 test(c);63 }64#if TEST_STD_VER >= 1165 {66 typedef std::unordered_set<int, std::hash<int>, std::equal_to<int>, min_allocator<int>> C;67 typedef int P;68 P a[] = {P(1), P(2), P(3), P(4), P(1), P(2)};69 C c(a, a + sizeof(a) / sizeof(a[0]));70 test(c);71 assert(c.bucket_count() >= 5);72 c.reserve(3);73 LIBCPP_ASSERT(c.bucket_count() == 5);74 test(c);75 c.max_load_factor(2);76 c.reserve(3);77 assert(c.bucket_count() >= 2);78 test(c);79 c.reserve(31);80 assert(c.bucket_count() >= 16);81 test(c);82 }83#endif84 reserve_invariant(20);85 86 return 0;87}88