xref: /llvm-project/libcxx/test/std/containers/associative/map/map.cons/assign_initializer_list.pass.cpp (revision 5ffe11a9fccfffbabbea5bbcc1cfd4b21af6d631)
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 
test_basic()24 void test_basic() {
25   {
26     typedef std::pair<const int, double> V;
27     std::map<int, double> m =
28                             {
29                                 {20, 1},
30                             };
31     m =
32                             {
33                                 {1, 1},
34                                 {1, 1.5},
35                                 {1, 2},
36                                 {2, 1},
37                                 {2, 1.5},
38                                 {2, 2},
39                                 {3, 1},
40                                 {3, 1.5},
41                                 {3, 2}
42                             };
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     typedef std::pair<const int, double> V;
51     std::map<int, double, std::less<int>, min_allocator<V>> m =
52                             {
53                                 {20, 1},
54                             };
55     m =
56                             {
57                                 {1, 1},
58                                 {1, 1.5},
59                                 {1, 2},
60                                 {2, 1},
61                                 {2, 1.5},
62                                 {2, 2},
63                                 {3, 1},
64                                 {3, 1.5},
65                                 {3, 2}
66                             };
67     assert(m.size() == 3);
68     assert(std::distance(m.begin(), m.end()) == 3);
69     assert(*m.begin() == V(1, 1));
70     assert(*std::next(m.begin()) == V(2, 1));
71     assert(*std::next(m.begin(), 2) == V(3, 1));
72     }
73 }
74 
75 
duplicate_keys_test()76 void duplicate_keys_test() {
77   test_allocator_statistics alloc_stats;
78   typedef std::map<int, int, std::less<int>, test_allocator<std::pair<const int, int> > > Map;
79   {
80     LIBCPP_ASSERT(alloc_stats.alloc_count == 0);
81     Map s({{1, 0}, {2, 0}, {3, 0}}, std::less<int>(), test_allocator<std::pair<const int, int> >(&alloc_stats));
82     LIBCPP_ASSERT(alloc_stats.alloc_count == 3);
83     s = {{4, 0}, {4, 0}, {4, 0}, {4, 0}};
84     LIBCPP_ASSERT(alloc_stats.alloc_count == 1);
85     assert(s.size() == 1);
86     assert(s.begin()->first == 4);
87   }
88   LIBCPP_ASSERT(alloc_stats.alloc_count == 0);
89 }
90 
main(int,char **)91 int main(int, char**)
92 {
93   test_basic();
94   duplicate_keys_test();
95 
96   return 0;
97 }
98