libcxx

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

assign_copy.pass.cpp (6442B)


      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 // unordered_map& operator=(const unordered_map& u);
     17 
     18 #include <algorithm>
     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_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_map<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() == 4);
     66         assert(c.at(1) == "one");
     67         assert(c.at(2) == "two");
     68         assert(c.at(3) == "three");
     69         assert(c.at(4) == "four");
     70         assert(c.hash_function() == test_hash<std::hash<int> >(8));
     71         assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
     72         assert(c.get_allocator() == A(4));
     73         assert(!c.empty());
     74         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
     75         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
     76         assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
     77         assert(c.max_load_factor() == 1);
     78     }
     79     {
     80         typedef std::unordered_map<int, std::string> C;
     81         typedef std::pair<const int, std::string> P;
     82         const P a[] =
     83         {
     84             P(1, "one"),
     85             P(2, "two"),
     86             P(3, "three"),
     87             P(4, "four"),
     88             P(1, "four"),
     89             P(2, "four"),
     90         };
     91         C c(a, a + sizeof(a)/sizeof(a[0]));
     92         C *p = &c;
     93         c = *p;
     94         assert(c.size() == 4);
     95         assert(std::is_permutation(c.begin(), c.end(), a));
     96     }
     97     {
     98         typedef other_allocator<std::pair<const int, std::string> > A;
     99         typedef std::unordered_map<int, std::string,
    100                                    test_hash<std::hash<int> >,
    101                                    test_compare<std::equal_to<int> >,
    102                                    A
    103                                    > C;
    104         typedef std::pair<int, std::string> P;
    105         P a[] =
    106         {
    107             P(1, "one"),
    108             P(2, "two"),
    109             P(3, "three"),
    110             P(4, "four"),
    111             P(1, "four"),
    112             P(2, "four"),
    113         };
    114         C c0(a, a + sizeof(a)/sizeof(a[0]),
    115             7,
    116             test_hash<std::hash<int> >(8),
    117             test_compare<std::equal_to<int> >(9),
    118             A(10)
    119            );
    120         C c(a, a + 2,
    121             7,
    122             test_hash<std::hash<int> >(2),
    123             test_compare<std::equal_to<int> >(3),
    124             A(4)
    125            );
    126         c = c0;
    127         assert(c.bucket_count() >= 5);
    128         assert(c.size() == 4);
    129         assert(c.at(1) == "one");
    130         assert(c.at(2) == "two");
    131         assert(c.at(3) == "three");
    132         assert(c.at(4) == "four");
    133         assert(c.hash_function() == test_hash<std::hash<int> >(8));
    134         assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
    135         assert(c.get_allocator() == A(10));
    136         assert(!c.empty());
    137         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
    138         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
    139         assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
    140         assert(c.max_load_factor() == 1);
    141     }
    142 #if TEST_STD_VER >= 11
    143     {
    144         typedef min_allocator<std::pair<const int, std::string> > A;
    145         typedef std::unordered_map<int, std::string,
    146                                    test_hash<std::hash<int> >,
    147                                    test_compare<std::equal_to<int> >,
    148                                    A
    149                                    > C;
    150         typedef std::pair<int, std::string> P;
    151         P a[] =
    152         {
    153             P(1, "one"),
    154             P(2, "two"),
    155             P(3, "three"),
    156             P(4, "four"),
    157             P(1, "four"),
    158             P(2, "four"),
    159         };
    160         C c0(a, a + sizeof(a)/sizeof(a[0]),
    161             7,
    162             test_hash<std::hash<int> >(8),
    163             test_compare<std::equal_to<int> >(9),
    164             A()
    165            );
    166         C c(a, a + 2,
    167             7,
    168             test_hash<std::hash<int> >(2),
    169             test_compare<std::equal_to<int> >(3),
    170             A()
    171            );
    172         c = c0;
    173         LIBCPP_ASSERT(c.bucket_count() == 7);
    174         assert(c.size() == 4);
    175         assert(c.at(1) == "one");
    176         assert(c.at(2) == "two");
    177         assert(c.at(3) == "three");
    178         assert(c.at(4) == "four");
    179         assert(c.hash_function() == test_hash<std::hash<int> >(8));
    180         assert(c.key_eq() == test_compare<std::equal_to<int> >(9));
    181         assert(c.get_allocator() == A());
    182         assert(!c.empty());
    183         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
    184         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
    185         assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
    186         assert(c.max_load_factor() == 1);
    187     }
    188 #endif
    189 }