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// <map>
12
13// class map
14
15// map& operator=(initializer_list<value_type> il);
16
17#include <map>
18#include <cassert>
19
20#include "test_macros.h"
21#include "min_allocator.h"
22#include "test_allocator.h"
23
24void test_basic() {
25 {
26 typedef std::pair<const int, double> V;
27 std::map<int, double> m = {
28 {20, 1},
29 };
30 m = {{1, 1}, {1, 1.5}, {1, 2}, {2, 1}, {2, 1.5}, {2, 2}, {3, 1}, {3, 1.5}, {3, 2}};
31 assert(m.size() == 3);
32 assert(std::distance(m.begin(), m.end()) == 3);
33 assert(*m.begin() == V(1, 1));
34 assert(*std::next(m.begin()) == V(2, 1));
35 assert(*std::next(m.begin(), 2) == V(3, 1));
36 }
37 {
38 typedef std::pair<const int, double> V;
39 std::map<int, double, std::less<int>, min_allocator<V>> m = {
40 {20, 1},
41 };
42 m = {{1, 1}, {1, 1.5}, {1, 2}, {2, 1}, {2, 1.5}, {2, 2}, {3, 1}, {3, 1.5}, {3, 2}};
43 assert(m.size() == 3);
44 assert(std::distance(m.begin(), m.end()) == 3);
45 assert(*m.begin() == V(1, 1));
46 assert(*std::next(m.begin()) == V(2, 1));
47 assert(*std::next(m.begin(), 2) == V(3, 1));
48 }
49}
50
51void duplicate_keys_test() {
52 test_allocator_statistics alloc_stats;
53 typedef std::map<int, int, std::less<int>, test_allocator<std::pair<const int, int> > > Map;
54 {
55 LIBCPP_ASSERT(alloc_stats.alloc_count == 0);
56 Map s({{1, 0}, {2, 0}, {3, 0}}, std::less<int>(), test_allocator<std::pair<const int, int> >(&alloc_stats));
57 LIBCPP_ASSERT(alloc_stats.alloc_count == 3);
58 s = {{4, 0}, {4, 0}, {4, 0}, {4, 0}};
59 LIBCPP_ASSERT(alloc_stats.alloc_count == 1);
60 assert(s.size() == 1);
61 assert(s.begin()->first == 4);
62 }
63 LIBCPP_ASSERT(alloc_stats.alloc_count == 0);
64}
65
66int main(int, char**) {
67 test_basic();
68 duplicate_keys_test();
69
70 return 0;
71}
72

source code of libcxx/test/std/containers/associative/map/map.cons/assign_initializer_list.pass.cpp