llvm-project
197 строк · 7.2 Кб
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
12// template<class InputIterator,
13// class Compare = less<iter-value-type<InputIterator>>,
14// class Allocator = allocator<iter-value-type<InputIterator>>>
15// map(InputIterator, InputIterator,
16// Compare = Compare(), Allocator = Allocator())
17// -> map<iter-value-type<InputIterator>, Compare, Allocator>;
18// template<class Key, class Compare = less<Key>, class Allocator = allocator<Key>>
19// map(initializer_list<Key>, Compare = Compare(), Allocator = Allocator())
20// -> map<Key, Compare, Allocator>;
21// template<class InputIterator, class Allocator>
22// map(InputIterator, InputIterator, Allocator)
23// -> map<iter-value-type<InputIterator>, less<iter-value-type<InputIterator>>, Allocator>;
24// template<class Key, class Allocator>
25// map(initializer_list<Key>, Allocator)
26// -> map<Key, less<Key>, Allocator>;
27//
28// template<ranges::input_range R, class Compare = less<range-key-type<R>,
29// class Allocator = allocator<range-to-alloc-type<R>>>
30// map(from_range_t, R&&, Compare = Compare(), Allocator = Allocator())
31// -> map<range-key-type<R>, range-mapped-type<R>, Compare, Allocator>; // C++23
32//
33// template<ranges::input_range R, class Allocator>
34// map(from_range_t, R&&, Allocator)
35// -> map<range-key-type<R>, range-mapped-type<R>, less<range-key-type<R>>, Allocator>; // C++23
36
37#include <algorithm> // std::equal
38#include <array>
39#include <cassert>
40#include <climits> // INT_MAX
41#include <functional>
42#include <map>
43#include <type_traits>
44
45#include "deduction_guides_sfinae_checks.h"
46#include "test_allocator.h"
47
48using P = std::pair<int, long>;
49using PC = std::pair<const int, long>;
50
51int main(int, char**)
52{
53{
54const P arr[] = { {1,1L}, {2,2L}, {1,1L}, {INT_MAX,1L}, {3,1L} };
55std::map m(std::begin(arr), std::end(arr));
56
57ASSERT_SAME_TYPE(decltype(m), std::map<int, long>);
58const PC expected_m[] = { {1,1L}, {2,2L}, {3,1L}, {INT_MAX,1L} };
59assert(std::equal(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
60}
61
62{
63const P arr[] = { {1,1L}, {2,2L}, {1,1L}, {INT_MAX,1L}, {3,1L} };
64std::map m(std::begin(arr), std::end(arr), std::greater<int>());
65
66ASSERT_SAME_TYPE(decltype(m), std::map<int, long, std::greater<int>>);
67const PC expected_m[] = { {INT_MAX,1L}, {3,1L}, {2,2L}, {1,1L} };
68assert(std::equal(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
69}
70
71{
72const P arr[] = { {1,1L}, {2,2L}, {1,1L}, {INT_MAX,1L}, {3,1L} };
73std::map m(std::begin(arr), std::end(arr), std::greater<int>(), test_allocator<PC>(0, 42));
74
75ASSERT_SAME_TYPE(decltype(m), std::map<int, long, std::greater<int>, test_allocator<PC>>);
76const PC expected_m[] = { {INT_MAX,1L}, {3,1L}, {2,2L}, {1,1L} };
77assert(std::equal(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
78assert(m.get_allocator().get_id() == 42);
79}
80
81{
82std::map<int, long> source;
83std::map m(source);
84ASSERT_SAME_TYPE(decltype(m), decltype(source));
85assert(m.size() == 0);
86}
87
88{
89std::map<int, long> source;
90std::map m{source}; // braces instead of parens
91ASSERT_SAME_TYPE(decltype(m), decltype(source));
92assert(m.size() == 0);
93}
94
95{
96std::map<int, long> source;
97std::map m(source, std::map<int, long>::allocator_type());
98ASSERT_SAME_TYPE(decltype(m), decltype(source));
99assert(m.size() == 0);
100}
101
102{
103std::map m{ P{1,1L}, P{2,2L}, P{1,1L}, P{INT_MAX,1L}, P{3,1L} };
104
105ASSERT_SAME_TYPE(decltype(m), std::map<int, long>);
106const PC expected_m[] = { {1,1L}, {2,2L}, {3,1L}, {INT_MAX,1L} };
107assert(std::equal(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
108}
109
110{
111std::map m({ P{1,1L}, P{2,2L}, P{1,1L}, P{INT_MAX,1L}, P{3,1L} }, std::greater<int>());
112
113ASSERT_SAME_TYPE(decltype(m), std::map<int, long, std::greater<int>>);
114const PC expected_m[] = { {INT_MAX,1L}, {3,1L}, {2,2L}, {1,1L} };
115assert(std::equal(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
116}
117
118{
119std::map 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));
120
121ASSERT_SAME_TYPE(decltype(m), std::map<int, long, std::greater<int>, test_allocator<PC>>);
122const PC expected_m[] = { {INT_MAX,1L}, {3,1L}, {2,2L}, {1,1L} };
123assert(std::equal(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
124assert(m.get_allocator().get_id() == 43);
125}
126
127{
128const P arr[] = { {1,1L}, {2,2L}, {1,1L}, {INT_MAX,1L}, {3,1L} };
129std::map m(std::begin(arr), std::end(arr), test_allocator<PC>(0, 44));
130
131ASSERT_SAME_TYPE(decltype(m), std::map<int, long, std::less<int>, test_allocator<PC>>);
132const PC expected_m[] = { {1,1L}, {2,2L}, {3,1L}, {INT_MAX,1L} };
133assert(std::equal(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
134assert(m.get_allocator().get_id() == 44);
135}
136
137{
138std::map m({ P{1,1L}, P{2,2L}, P{1,1L}, P{INT_MAX,1L}, P{3,1L} }, test_allocator<PC>(0, 45));
139
140ASSERT_SAME_TYPE(decltype(m), std::map<int, long, std::less<int>, test_allocator<PC>>);
141const PC expected_m[] = { {1,1L}, {2,2L}, {3,1L}, {INT_MAX,1L} };
142assert(std::equal(m.begin(), m.end(), std::begin(expected_m), std::end(expected_m)));
143assert(m.get_allocator().get_id() == 45);
144}
145
146{
147// Examples from LWG3025
148std::map m{std::pair{1, 1}, {2, 2}, {3, 3}};
149ASSERT_SAME_TYPE(decltype(m), std::map<int, int>);
150
151std::map m2{m.begin(), m.end()};
152ASSERT_SAME_TYPE(decltype(m2), std::map<int, int>);
153}
154
155{
156// Examples from LWG3531
157std::map m1{{std::pair{1, 2}, {3, 4}}, std::less<int>()};
158ASSERT_SAME_TYPE(decltype(m1), std::map<int, int>);
159
160using value_type = std::pair<const int, int>;
161std::map m2{{value_type{1, 2}, {3, 4}}, std::less<int>()};
162ASSERT_SAME_TYPE(decltype(m2), std::map<int, int>);
163}
164
165#if TEST_STD_VER >= 23
166{
167using Range = std::array<P, 0>;
168using Comp = std::greater<int>;
169using DefaultComp = std::less<int>;
170using Alloc = test_allocator<PC>;
171
172{ // (from_range, range)
173std::map c(std::from_range, Range());
174static_assert(std::is_same_v<decltype(c), std::map<int, long>>);
175}
176
177{ // (from_range, range, comp)
178std::map c(std::from_range, Range(), Comp());
179static_assert(std::is_same_v<decltype(c), std::map<int, long, Comp>>);
180}
181
182{ // (from_range, range, comp, alloc)
183std::map c(std::from_range, Range(), Comp(), Alloc());
184static_assert(std::is_same_v<decltype(c), std::map<int, long, Comp, Alloc>>);
185}
186
187{ // (from_range, range, alloc)
188std::map c(std::from_range, Range(), Alloc());
189static_assert(std::is_same_v<decltype(c), std::map<int, long, DefaultComp, Alloc>>);
190}
191}
192#endif
193
194AssociativeContainerDeductionGuidesSfinaeAway<std::map, std::map<int, long>>();
195
196return 0;
197}
198