libcxx

libcxx mirror with random patches
git clone https://git.neptards.moe/neptards/libcxx.git
Log | Files | Refs

rehash.pass.cpp (2575B)


      1 //===----------------------------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 // <unordered_set>
     11 
     12 // template <class Value, class Hash = hash<Value>, class Pred = equal_to<Value>,
     13 //           class Alloc = allocator<Value>>
     14 // class unordered_set
     15 
     16 // void rehash(size_type n);
     17 
     18 #include <unordered_set>
     19 #include <cassert>
     20 
     21 #include "test_macros.h"
     22 #include "min_allocator.h"
     23 
     24 template <class C>
     25 void rehash_postcondition(const C& c, size_t n)
     26 {
     27     assert(c.bucket_count() >= c.size() / c.max_load_factor() && c.bucket_count() >= n);
     28 }
     29 
     30 template <class C>
     31 void test(const C& c)
     32 {
     33     assert(c.size() == 4);
     34     assert(c.count(1) == 1);
     35     assert(c.count(2) == 1);
     36     assert(c.count(3) == 1);
     37     assert(c.count(4) == 1);
     38 }
     39 
     40 int main()
     41 {
     42     {
     43         typedef std::unordered_set<int> C;
     44         typedef int P;
     45         P a[] =
     46         {
     47             P(1),
     48             P(2),
     49             P(3),
     50             P(4),
     51             P(1),
     52             P(2)
     53         };
     54         C c(a, a + sizeof(a)/sizeof(a[0]));
     55         test(c);
     56         assert(c.bucket_count() >= 5);
     57         c.rehash(3);
     58         rehash_postcondition(c, 3);
     59         LIBCPP_ASSERT(c.bucket_count() == 5);
     60         test(c);
     61         c.max_load_factor(2);
     62         c.rehash(3);
     63         rehash_postcondition(c, 3);
     64         LIBCPP_ASSERT(c.bucket_count() == 3);
     65         test(c);
     66         c.rehash(31);
     67         rehash_postcondition(c, 31);
     68         LIBCPP_ASSERT(c.bucket_count() == 31);
     69         test(c);
     70     }
     71 #if TEST_STD_VER >= 11
     72     {
     73         typedef std::unordered_set<int, std::hash<int>,
     74                                       std::equal_to<int>, min_allocator<int>> C;
     75         typedef int P;
     76         P a[] =
     77         {
     78             P(1),
     79             P(2),
     80             P(3),
     81             P(4),
     82             P(1),
     83             P(2)
     84         };
     85         C c(a, a + sizeof(a)/sizeof(a[0]));
     86         test(c);
     87         assert(c.bucket_count() >= 5);
     88         c.rehash(3);
     89         rehash_postcondition(c, 3);
     90         LIBCPP_ASSERT(c.bucket_count() == 5);
     91         test(c);
     92         c.max_load_factor(2);
     93         c.rehash(3);
     94         rehash_postcondition(c, 3);
     95         LIBCPP_ASSERT(c.bucket_count() == 3);
     96         test(c);
     97         c.rehash(31);
     98         rehash_postcondition(c, 31);
     99         LIBCPP_ASSERT(c.bucket_count() == 31);
    100         test(c);
    101     }
    102 #endif
    103 }