libcxx

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

range.pass.cpp (6523B)


      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_map>
     11 
     12 // template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>,
     13 //           class Alloc = allocator<pair<const Key, T>>>
     14 // class unordered_map
     15 
     16 // template <class InputIterator>
     17 //     unordered_map(InputIterator first, InputIterator last);
     18 
     19 #include <unordered_map>
     20 #include <string>
     21 #include <cassert>
     22 #include <cfloat>
     23 #include <cmath>
     24 #include <cstddef>
     25 
     26 #include "test_macros.h"
     27 #include "test_iterators.h"
     28 #include "../../../NotConstructible.h"
     29 #include "../../../test_compare.h"
     30 #include "../../../test_hash.h"
     31 #include "test_allocator.h"
     32 #include "min_allocator.h"
     33 
     34 int main()
     35 {
     36     {
     37         typedef std::unordered_map<int, std::string,
     38                                    test_hash<std::hash<int> >,
     39                                    test_compare<std::equal_to<int> >,
     40                                    test_allocator<std::pair<const int, std::string> >
     41                                    > C;
     42         typedef std::pair<int, std::string> P;
     43         P a[] =
     44         {
     45             P(1, "one"),
     46             P(2, "two"),
     47             P(3, "three"),
     48             P(4, "four"),
     49             P(1, "four"),
     50             P(2, "four"),
     51         };
     52         C c(input_iterator<P*>(a), input_iterator<P*>(a + sizeof(a)/sizeof(a[0])));
     53         assert(c.bucket_count() >= 5);
     54         assert(c.size() == 4);
     55         assert(c.at(1) == "one");
     56         assert(c.at(2) == "two");
     57         assert(c.at(3) == "three");
     58         assert(c.at(4) == "four");
     59         assert(c.hash_function() == test_hash<std::hash<int> >());
     60         assert(c.key_eq() == test_compare<std::equal_to<int> >());
     61         assert(c.get_allocator() ==
     62                (test_allocator<std::pair<const int, std::string> >()));
     63         assert(!c.empty());
     64         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
     65         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
     66         assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
     67         assert(c.max_load_factor() == 1);
     68     }
     69 #if TEST_STD_VER >= 11
     70     {
     71         typedef std::unordered_map<int, std::string,
     72                                    test_hash<std::hash<int> >,
     73                                    test_compare<std::equal_to<int> >,
     74                                    min_allocator<std::pair<const int, std::string> >
     75                                    > C;
     76         typedef std::pair<int, std::string> P;
     77         P a[] =
     78         {
     79             P(1, "one"),
     80             P(2, "two"),
     81             P(3, "three"),
     82             P(4, "four"),
     83             P(1, "four"),
     84             P(2, "four"),
     85         };
     86         C c(input_iterator<P*>(a), input_iterator<P*>(a + sizeof(a)/sizeof(a[0])));
     87         assert(c.bucket_count() >= 5);
     88         assert(c.size() == 4);
     89         assert(c.at(1) == "one");
     90         assert(c.at(2) == "two");
     91         assert(c.at(3) == "three");
     92         assert(c.at(4) == "four");
     93         assert(c.hash_function() == test_hash<std::hash<int> >());
     94         assert(c.key_eq() == test_compare<std::equal_to<int> >());
     95         assert(c.get_allocator() ==
     96                (min_allocator<std::pair<const int, std::string> >()));
     97         assert(!c.empty());
     98         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
     99         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
    100         assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
    101         assert(c.max_load_factor() == 1);
    102     }
    103 #if TEST_STD_VER > 11
    104     {
    105         typedef std::pair<int, std::string> P;
    106         typedef test_allocator<std::pair<const int, std::string>> A;
    107         typedef test_hash<std::hash<int>> HF;
    108         typedef test_compare<std::equal_to<int>> Comp;
    109         typedef std::unordered_map<int, std::string, HF, Comp, A> C;
    110 
    111         P arr[] =
    112         {
    113             P(1, "one"),
    114             P(2, "two"),
    115             P(3, "three"),
    116             P(4, "four"),
    117             P(1, "four"),
    118             P(2, "four"),
    119         };
    120         C c(input_iterator<P*>(arr), input_iterator<P*>(arr + sizeof(arr)/sizeof(arr[0])), 14);
    121         assert(c.bucket_count() >= 14);
    122         assert(c.size() == 4);
    123         assert(c.at(1) == "one");
    124         assert(c.at(2) == "two");
    125         assert(c.at(3) == "three");
    126         assert(c.at(4) == "four");
    127         assert(c.hash_function() == HF());
    128         assert(c.key_eq() == Comp());
    129         assert(c.get_allocator() == A());
    130         assert(!c.empty());
    131         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
    132         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
    133         assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
    134         assert(c.max_load_factor() == 1);
    135     }
    136     {
    137         typedef std::pair<int, std::string> P;
    138         typedef test_allocator<std::pair<const int, std::string>> A;
    139         typedef test_hash<std::hash<int>> HF;
    140         typedef test_compare<std::equal_to<int>> Comp;
    141         typedef std::unordered_map<int, std::string, HF, Comp, A> C;
    142 
    143         P arr[] =
    144         {
    145             P(1, "one"),
    146             P(2, "two"),
    147             P(3, "three"),
    148             P(4, "four"),
    149             P(1, "four"),
    150             P(2, "four"),
    151         };
    152         HF hf(42);
    153         A a(43);
    154         C c(input_iterator<P*>(arr), input_iterator<P*>(arr + sizeof(arr)/sizeof(arr[0])), 14, hf, a);
    155         assert(c.bucket_count() >= 14);
    156         assert(c.size() == 4);
    157         assert(c.at(1) == "one");
    158         assert(c.at(2) == "two");
    159         assert(c.at(3) == "three");
    160         assert(c.at(4) == "four");
    161         assert(c.hash_function() == hf);
    162         assert(!(c.hash_function() == HF()));
    163         assert(c.key_eq() == Comp());
    164         assert(c.get_allocator() == a);
    165         assert(!c.empty());
    166         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
    167         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
    168         assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
    169         assert(c.max_load_factor() == 1);
    170     }
    171 #endif
    172 #endif
    173 }