xref: /llvm-project/libcxx/test/std/containers/associative/multimap/multimap.cons/deduct.pass.cpp (revision dd15c2723cd29574c341ec189409a6c83fbefb04)
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 // <map>
10 // UNSUPPORTED: c++03, c++11, c++14
11 // UNSUPPORTED: libcpp-no-deduction-guides
12 
13 // template<class InputIterator,
14 //          class Compare = less<iter-value-type<InputIterator>>,
15 //          class Allocator = allocator<iter-value-type<InputIterator>>>
16 // multimap(InputIterator, InputIterator,
17 //          Compare = Compare(), Allocator = Allocator())
18 //   -> multimap<iter-value-type<InputIterator>, Compare, Allocator>;
19 // template<class Key, class Compare = less<Key>, class Allocator = allocator<Key>>
20 // multimap(initializer_list<Key>, Compare = Compare(), Allocator = Allocator())
21 //   -> multimap<Key, Compare, Allocator>;
22 // template<class InputIterator, class Allocator>
23 // multimap(InputIterator, InputIterator, Allocator)
24 //   -> multimap<iter-value-type<InputIterator>, less<iter-value-type<InputIterator>>, Allocator>;
25 // template<class Key, class Allocator>
26 // multimap(initializer_list<Key>, Allocator)
27 //   -> multimap<Key, less<Key>, Allocator>;
28 
29 #include <algorithm> // std::equal
30 #include <cassert>
31 #include <climits> // INT_MAX
32 #include <functional>
33 #include <map>
34 #include <type_traits>
35 
36 #include "test_allocator.h"
37 
38 using P = std::pair<int, long>;
39 using PC = std::pair<const int, long>;
40 
41 int main(int, char**)
42 {
43     {
44     const P arr[] = { {1,1L}, {2,2L}, {1,1L}, {INT_MAX,1L}, {3,1L} };
45     std::multimap m(std::begin(arr), std::end(arr));
46 
47     ASSERT_SAME_TYPE(decltype(m), std::multimap<int, long>);
48     const PC expected_m[] = { {1,1L}, {1,1L}, {2,2L}, {3,1L}, {INT_MAX,1L} };
49     assert(std::equal(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
50     }
51 
52     {
53     const P arr[] = { {1,1L}, {2,2L}, {1,1L}, {INT_MAX,1L}, {3,1L} };
54     std::multimap m(std::begin(arr), std::end(arr), std::greater<int>());
55 
56     ASSERT_SAME_TYPE(decltype(m), std::multimap<int, long, std::greater<int>>);
57     const PC expected_m[] = { {INT_MAX,1L}, {3,1L}, {2,2L}, {1,1L}, {1,1L} };
58     assert(std::equal(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
59     }
60 
61     {
62     const P arr[] = { {1,1L}, {2,2L}, {1,1L}, {INT_MAX,1L}, {3,1L} };
63     std::multimap m(std::begin(arr), std::end(arr), std::greater<int>(), test_allocator<PC>(0, 42));
64 
65     ASSERT_SAME_TYPE(decltype(m), std::multimap<int, long, std::greater<int>, test_allocator<PC>>);
66     const PC expected_m[] = { {INT_MAX,1L}, {3,1L}, {2,2L}, {1,1L}, {1,1L} };
67     assert(std::equal(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
68     assert(m.get_allocator().get_id() == 42);
69     }
70 
71     {
72     std::multimap<int, long> source;
73     std::multimap m(source);
74     ASSERT_SAME_TYPE(decltype(m), decltype(source));
75     assert(m.size() == 0);
76     }
77 
78     {
79     std::multimap<int, long> source;
80     std::multimap m{source};  // braces instead of parens
81     ASSERT_SAME_TYPE(decltype(m), decltype(source));
82     assert(m.size() == 0);
83     }
84 
85     {
86     std::multimap<int, long> source;
87     std::multimap m(source, std::map<int, long>::allocator_type());
88     ASSERT_SAME_TYPE(decltype(m), decltype(source));
89     assert(m.size() == 0);
90     }
91 
92     {
93     std::multimap m{ P{1,1L}, P{2,2L}, P{1,1L}, P{INT_MAX,1L}, P{3,1L} };
94 
95     ASSERT_SAME_TYPE(decltype(m), std::multimap<int, long>);
96     const PC expected_m[] = { {1,1L}, {1,1L}, {2,2L}, {3,1L}, {INT_MAX,1L} };
97     assert(std::equal(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
98     }
99 
100     {
101     std::multimap m({ P{1,1L}, P{2,2L}, P{1,1L}, P{INT_MAX,1L}, P{3,1L} }, std::greater<int>());
102 
103     ASSERT_SAME_TYPE(decltype(m), std::multimap<int, long, std::greater<int>>);
104     const PC expected_m[] = { {INT_MAX,1L}, {3,1L}, {2,2L}, {1,1L}, {1,1L} };
105     assert(std::equal(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
106     }
107 
108     {
109     std::multimap m({ P{1,1L}, P{2,2L}, P{1,1L}, P{INT_MAX,1L}, P{3,1L} }, std::greater<int>(), test_allocator<PC>(0, 43));
110 
111     ASSERT_SAME_TYPE(decltype(m), std::multimap<int, long, std::greater<int>, test_allocator<PC>>);
112     const PC expected_m[] = { {INT_MAX,1L}, {3,1L}, {2,2L}, {1,1L}, {1,1L} };
113     assert(std::equal(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
114     assert(m.get_allocator().get_id() == 43);
115     }
116 
117     {
118     const P arr[] = { {1,1L}, {2,2L}, {1,1L}, {INT_MAX,1L}, {3,1L} };
119     std::multimap m(std::begin(arr), std::end(arr), test_allocator<PC>(0, 44));
120 
121     ASSERT_SAME_TYPE(decltype(m), std::multimap<int, long, std::less<int>, test_allocator<PC>>);
122     const PC expected_m[] = { {1,1L}, {1,1L}, {2,2L}, {3,1L}, {INT_MAX,1L} };
123     assert(std::equal(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
124     assert(m.get_allocator().get_id() == 44);
125     }
126 
127     {
128     std::multimap m({ P{1,1L}, P{2,2L}, P{1,1L}, P{INT_MAX,1L}, P{3,1L} }, test_allocator<PC>(0, 45));
129 
130     ASSERT_SAME_TYPE(decltype(m), std::multimap<int, long, std::less<int>, test_allocator<PC>>);
131     const PC expected_m[] = { {1,1L}, {1,1L}, {2,2L}, {3,1L}, {INT_MAX,1L} };
132     assert(std::equal(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
133     assert(m.get_allocator().get_id() == 45);
134     }
135 
136     {
137     // Examples from LWG3025
138     std::multimap m{std::pair{1, 1}, {2, 2}, {3, 3}};
139     ASSERT_SAME_TYPE(decltype(m), std::multimap<int, int>);
140 
141     std::multimap m2{m.begin(), m.end()};
142     ASSERT_SAME_TYPE(decltype(m2), std::multimap<int, int>);
143     }
144 
145     {
146     // Examples from LWG3531
147     std::multimap m1{{std::pair{1, 2}, {3, 4}}, std::less<int>()};
148     ASSERT_SAME_TYPE(decltype(m1), std::multimap<int, int>);
149 
150     using value_type = std::pair<const int, int>;
151     std::multimap m2{{value_type{1, 2}, {3, 4}}, std::less<int>()};
152     ASSERT_SAME_TYPE(decltype(m2), std::multimap<int, int>);
153     }
154 
155     return 0;
156 }
157