libcxx

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

copy_alloc.pass.cpp (6941B)


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