libcxx

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

assign_copy.pass.cpp (7600B)


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