init_size.pass.cpp revision 1ae14a374d5e15c60c6d3ae4adbcdf57c5e3a401
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// UNSUPPORTED: c++98, c++03
11
12// <unordered_map>
13
14// template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>,
15//           class Alloc = allocator<pair<const Key, T>>>
16// class unordered_map
17
18// unordered_map(initializer_list<value_type> il, size_type n);
19
20#include <unordered_map>
21#include <string>
22#include <cassert>
23#include <cfloat>
24#include <cmath>
25#include <cstddef>
26
27#include "test_macros.h"
28#include "../../../test_compare.h"
29#include "../../../test_hash.h"
30#include "test_allocator.h"
31#include "min_allocator.h"
32
33int main()
34{
35    {
36        typedef std::unordered_map<int, std::string,
37                                   test_hash<std::hash<int> >,
38                                   test_compare<std::equal_to<int> >,
39                                   test_allocator<std::pair<const int, std::string> >
40                                   > C;
41        typedef std::pair<int, std::string> P;
42        C c({
43                P(1, "one"),
44                P(2, "two"),
45                P(3, "three"),
46                P(4, "four"),
47                P(1, "four"),
48                P(2, "four"),
49            },
50            7
51           );
52        LIBCPP_ASSERT(c.bucket_count() == 7);
53        assert(c.size() == 4);
54        assert(c.at(1) == "one");
55        assert(c.at(2) == "two");
56        assert(c.at(3) == "three");
57        assert(c.at(4) == "four");
58        assert(c.hash_function() == test_hash<std::hash<int> >());
59        assert(c.key_eq() == test_compare<std::equal_to<int> >());
60        assert(c.get_allocator() ==
61               (test_allocator<std::pair<const int, std::string> >()));
62        assert(!c.empty());
63        assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
64        assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
65        assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
66        assert(c.max_load_factor() == 1);
67    }
68    {
69        typedef std::unordered_map<int, std::string,
70                                   test_hash<std::hash<int> >,
71                                   test_compare<std::equal_to<int> >,
72                                   min_allocator<std::pair<const int, std::string> >
73                                   > C;
74        typedef std::pair<int, std::string> P;
75        C c({
76                P(1, "one"),
77                P(2, "two"),
78                P(3, "three"),
79                P(4, "four"),
80                P(1, "four"),
81                P(2, "four"),
82            },
83            7
84           );
85        LIBCPP_ASSERT(c.bucket_count() == 7);
86        assert(c.size() == 4);
87        assert(c.at(1) == "one");
88        assert(c.at(2) == "two");
89        assert(c.at(3) == "three");
90        assert(c.at(4) == "four");
91        assert(c.hash_function() == test_hash<std::hash<int> >());
92        assert(c.key_eq() == test_compare<std::equal_to<int> >());
93        assert(c.get_allocator() ==
94               (min_allocator<std::pair<const int, std::string> >()));
95        assert(!c.empty());
96        assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
97        assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
98        assert(std::fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
99        assert(c.max_load_factor() == 1);
100    }
101}
102