1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9// UNSUPPORTED: c++03
10
11// <unordered_map>
12
13// template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>,
14// class Alloc = allocator<pair<const Key, T>>>
15// class unordered_map
16
17// unordered_map& operator=(initializer_list<value_type> il);
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_compare.h"
28#include "../../../test_hash.h"
29#include "min_allocator.h"
30
31int main(int, char**) {
32 {
33 typedef std::allocator<std::pair<const int, std::string> > A;
34 typedef std::unordered_map<int, std::string, test_hash<int>, test_equal_to<int>, A > C;
35 typedef std::pair<int, std::string> P;
36 C c = {
37 P(4, "four"),
38 P(1, "four"),
39 P(2, "four"),
40 };
41 c = {
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 assert(c.bucket_count() >= 5);
50 assert(c.size() == 4);
51 assert(c.at(1) == "one");
52 assert(c.at(2) == "two");
53 assert(c.at(3) == "three");
54 assert(c.at(4) == "four");
55 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
56 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
57 assert(fabs(c.load_factor() - (float)c.size() / c.bucket_count()) < FLT_EPSILON);
58 assert(c.max_load_factor() == 1);
59 }
60 {
61 typedef min_allocator<std::pair<const int, std::string> > A;
62 typedef std::unordered_map<int, std::string, test_hash<int>, test_equal_to<int>, A > C;
63 typedef std::pair<int, std::string> P;
64 C c = {
65 P(4, "four"),
66 P(1, "four"),
67 P(2, "four"),
68 };
69 c = {
70 P(1, "one"),
71 P(2, "two"),
72 P(3, "three"),
73 P(4, "four"),
74 P(1, "four"),
75 P(2, "four"),
76 };
77 assert(c.bucket_count() >= 5);
78 assert(c.size() == 4);
79 assert(c.at(1) == "one");
80 assert(c.at(2) == "two");
81 assert(c.at(3) == "three");
82 assert(c.at(4) == "four");
83 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
84 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
85 assert(fabs(c.load_factor() - (float)c.size() / c.bucket_count()) < FLT_EPSILON);
86 assert(c.max_load_factor() == 1);
87 }
88
89 return 0;
90}
91

source code of libcxx/test/std/containers/unord/unord.map/unord.map.cnstr/assign_init.pass.cpp