|
13 | 13 | // template <class InputIterator> |
14 | 14 | // void insert(InputIterator first, InputIterator last); |
15 | 15 |
|
16 | | -#include <map> |
| 16 | +#include <array> |
17 | 17 | #include <cassert> |
| 18 | +#include <map> |
18 | 19 |
|
19 | | -#include "test_macros.h" |
20 | | -#include "test_iterators.h" |
21 | 20 | #include "min_allocator.h" |
| 21 | +#include "test_iterators.h" |
| 22 | +#include "test_macros.h" |
22 | 23 |
|
23 | | -int main(int, char**) { |
24 | | - { |
25 | | - typedef std::multimap<int, double> M; |
26 | | - typedef std::pair<int, double> P; |
27 | | - P ar[] = { |
28 | | - P(1, 1), |
29 | | - P(1, 1.5), |
30 | | - P(1, 2), |
31 | | - P(2, 1), |
32 | | - P(2, 1.5), |
33 | | - P(2, 2), |
34 | | - P(3, 1), |
35 | | - P(3, 1.5), |
36 | | - P(3, 2), |
37 | | - }; |
38 | | - M m; |
39 | | - m.insert(cpp17_input_iterator<P*>(ar), cpp17_input_iterator<P*>(ar + sizeof(ar) / sizeof(ar[0]))); |
40 | | - assert(m.size() == 9); |
41 | | - assert(m.begin()->first == 1); |
42 | | - assert(m.begin()->second == 1); |
43 | | - assert(std::next(m.begin())->first == 1); |
44 | | - assert(std::next(m.begin())->second == 1.5); |
45 | | - assert(std::next(m.begin(), 2)->first == 1); |
46 | | - assert(std::next(m.begin(), 2)->second == 2); |
47 | | - assert(std::next(m.begin(), 3)->first == 2); |
48 | | - assert(std::next(m.begin(), 3)->second == 1); |
49 | | - assert(std::next(m.begin(), 4)->first == 2); |
50 | | - assert(std::next(m.begin(), 4)->second == 1.5); |
51 | | - assert(std::next(m.begin(), 5)->first == 2); |
52 | | - assert(std::next(m.begin(), 5)->second == 2); |
53 | | - assert(std::next(m.begin(), 6)->first == 3); |
54 | | - assert(std::next(m.begin(), 6)->second == 1); |
55 | | - assert(std::next(m.begin(), 7)->first == 3); |
56 | | - assert(std::next(m.begin(), 7)->second == 1.5); |
57 | | - assert(std::next(m.begin(), 8)->first == 3); |
58 | | - assert(std::next(m.begin(), 8)->second == 2); |
| 24 | +template <class Iter, class Alloc> |
| 25 | +void test_alloc() { |
| 26 | + { // Check that an empty range works correctly |
| 27 | + { // Without elements in the container |
| 28 | + using Map = std::multimap<int, int, std::less<int>, Alloc>; |
| 29 | + |
| 30 | + std::array<std::pair<const int, int>, 0> arr; |
| 31 | + |
| 32 | + Map map; |
| 33 | + map.insert(Iter(arr.data()), Iter(arr.data() + arr.size())); |
| 34 | + assert(map.size() == 0); |
| 35 | + assert(map.begin() == map.end()); |
| 36 | + } |
| 37 | + { // With 1 element in the container |
| 38 | + using Map = std::multimap<int, int, std::less<int>, Alloc>; |
| 39 | + using Pair = std::pair<const int, int>; |
| 40 | + |
| 41 | + std::array<Pair, 0> arr; |
| 42 | + |
| 43 | + Map map; |
| 44 | + map.insert(Pair(0, 0)); |
| 45 | + map.insert(Iter(arr.data()), Iter(arr.data() + arr.size())); |
| 46 | + assert(map.size() == 1); |
| 47 | + assert(*std::next(map.begin(), 0) == Pair(0, 0)); |
| 48 | + assert(std::next(map.begin(), 1) == map.end()); |
| 49 | + } |
| 50 | + { // With multiple elements in the container |
| 51 | + using Map = std::multimap<int, int, std::less<int>, Alloc>; |
| 52 | + using Pair = std::pair<const int, int>; |
| 53 | + |
| 54 | + std::array<Pair, 0> arr; |
| 55 | + |
| 56 | + Map map; |
| 57 | + map.insert(Pair(0, 0)); |
| 58 | + map.insert(Pair(1, 1)); |
| 59 | + map.insert(Pair(2, 2)); |
| 60 | + map.insert(Iter(arr.data()), Iter(arr.data() + arr.size())); |
| 61 | + assert(map.size() == 3); |
| 62 | + assert(*std::next(map.begin(), 0) == Pair(0, 0)); |
| 63 | + assert(*std::next(map.begin(), 1) == Pair(1, 1)); |
| 64 | + assert(*std::next(map.begin(), 2) == Pair(2, 2)); |
| 65 | + assert(std::next(map.begin(), 3) == map.end()); |
| 66 | + } |
59 | 67 | } |
60 | | -#if TEST_STD_VER >= 11 |
61 | | - { |
62 | | - typedef std::multimap<int, double, std::less<int>, min_allocator<std::pair<const int, double>>> M; |
63 | | - typedef std::pair<int, double> P; |
64 | | - P ar[] = { |
65 | | - P(1, 1), |
66 | | - P(1, 1.5), |
67 | | - P(1, 2), |
68 | | - P(2, 1), |
69 | | - P(2, 1.5), |
70 | | - P(2, 2), |
71 | | - P(3, 1), |
72 | | - P(3, 1.5), |
73 | | - P(3, 2), |
74 | | - }; |
75 | | - M m; |
76 | | - m.insert(cpp17_input_iterator<P*>(ar), cpp17_input_iterator<P*>(ar + sizeof(ar) / sizeof(ar[0]))); |
77 | | - assert(m.size() == 9); |
78 | | - assert(m.begin()->first == 1); |
79 | | - assert(m.begin()->second == 1); |
80 | | - assert(std::next(m.begin())->first == 1); |
81 | | - assert(std::next(m.begin())->second == 1.5); |
82 | | - assert(std::next(m.begin(), 2)->first == 1); |
83 | | - assert(std::next(m.begin(), 2)->second == 2); |
84 | | - assert(std::next(m.begin(), 3)->first == 2); |
85 | | - assert(std::next(m.begin(), 3)->second == 1); |
86 | | - assert(std::next(m.begin(), 4)->first == 2); |
87 | | - assert(std::next(m.begin(), 4)->second == 1.5); |
88 | | - assert(std::next(m.begin(), 5)->first == 2); |
89 | | - assert(std::next(m.begin(), 5)->second == 2); |
90 | | - assert(std::next(m.begin(), 6)->first == 3); |
91 | | - assert(std::next(m.begin(), 6)->second == 1); |
92 | | - assert(std::next(m.begin(), 7)->first == 3); |
93 | | - assert(std::next(m.begin(), 7)->second == 1.5); |
94 | | - assert(std::next(m.begin(), 8)->first == 3); |
95 | | - assert(std::next(m.begin(), 8)->second == 2); |
| 68 | + { // Check that 1 element is inserted correctly |
| 69 | + { // Without elements in the container |
| 70 | + using Map = std::multimap<int, int, std::less<int>, Alloc>; |
| 71 | + using Pair = std::pair<const int, int>; |
| 72 | + |
| 73 | + Pair arr[] = {Pair(1, 1)}; |
| 74 | + |
| 75 | + Map map; |
| 76 | + map.insert(Iter(std::begin(arr)), Iter(std::end(arr))); |
| 77 | + assert(map.size() == 1); |
| 78 | + assert(*std::next(map.begin(), 0) == Pair(1, 1)); |
| 79 | + assert(std::next(map.begin(), 1) == map.end()); |
| 80 | + } |
| 81 | + { // With 1 element in the container - a different key |
| 82 | + using Map = std::multimap<int, int, std::less<int>, Alloc>; |
| 83 | + using Pair = std::pair<const int, int>; |
| 84 | + |
| 85 | + Pair arr[] = {Pair(1, 1)}; |
| 86 | + |
| 87 | + Map map; |
| 88 | + map.insert(Pair(0, 0)); |
| 89 | + map.insert(Iter(std::begin(arr)), Iter(std::end(arr))); |
| 90 | + assert(map.size() == 2); |
| 91 | + assert(*std::next(map.begin(), 0) == Pair(0, 0)); |
| 92 | + assert(*std::next(map.begin(), 1) == Pair(1, 1)); |
| 93 | + assert(std::next(map.begin(), 2) == map.end()); |
| 94 | + } |
| 95 | + { // With 1 element in the container - the same key |
| 96 | + using Map = std::multimap<int, int, std::less<int>, Alloc>; |
| 97 | + using Pair = std::pair<const int, int>; |
| 98 | + |
| 99 | + Pair arr[] = {Pair(1, 1)}; |
| 100 | + |
| 101 | + Map map; |
| 102 | + map.insert(Pair(1, 1)); |
| 103 | + map.insert(Iter(std::begin(arr)), Iter(std::end(arr))); |
| 104 | + assert(map.size() == 2); |
| 105 | + assert(*std::next(map.begin(), 0) == Pair(1, 1)); |
| 106 | + assert(*std::next(map.begin(), 1) == Pair(1, 1)); |
| 107 | + assert(std::next(map.begin(), 2) == map.end()); |
| 108 | + } |
| 109 | + { // With multiple elements in the container |
| 110 | + using Map = std::multimap<int, int, std::less<int>, Alloc>; |
| 111 | + using Pair = std::pair<const int, int>; |
| 112 | + |
| 113 | + Pair arr[] = {Pair(1, 1)}; |
| 114 | + |
| 115 | + Map map; |
| 116 | + map.insert(Pair(0, 0)); |
| 117 | + map.insert(Pair(1, 1)); |
| 118 | + map.insert(Pair(2, 2)); |
| 119 | + map.insert(Iter(std::begin(arr)), Iter(std::end(arr))); |
| 120 | + assert(map.size() == 4); |
| 121 | + assert(*std::next(map.begin(), 0) == Pair(0, 0)); |
| 122 | + assert(*std::next(map.begin(), 1) == Pair(1, 1)); |
| 123 | + assert(*std::next(map.begin(), 2) == Pair(1, 1)); |
| 124 | + assert(*std::next(map.begin(), 3) == Pair(2, 2)); |
| 125 | + assert(std::next(map.begin(), 4) == map.end()); |
| 126 | + } |
96 | 127 | } |
97 | | -#endif |
| 128 | + { // Check that multiple elements are inserted correctly |
| 129 | + { // Without elements in the container |
| 130 | + using Map = std::multimap<int, int, std::less<int>, Alloc>; |
| 131 | + using Pair = std::pair<const int, int>; |
| 132 | + |
| 133 | + Pair arr[] = {Pair(1, 1), Pair(1, 1), Pair(3, 3)}; |
| 134 | + |
| 135 | + Map map; |
| 136 | + map.insert(Iter(std::begin(arr)), Iter(std::end(arr))); |
| 137 | + assert(map.size() == 3); |
| 138 | + assert(*std::next(map.begin(), 0) == Pair(1, 1)); |
| 139 | + assert(*std::next(map.begin(), 1) == Pair(1, 1)); |
| 140 | + assert(*std::next(map.begin(), 2) == Pair(3, 3)); |
| 141 | + assert(std::next(map.begin(), 3) == map.end()); |
| 142 | + } |
| 143 | + { // With 1 element in the container - a different key |
| 144 | + using Map = std::multimap<int, int, std::less<int>, Alloc>; |
| 145 | + using Pair = std::pair<const int, int>; |
| 146 | + |
| 147 | + Pair arr[] = {Pair(1, 1), Pair(1, 1), Pair(3, 3)}; |
| 148 | + |
| 149 | + Map map; |
| 150 | + map.insert(Pair(0, 0)); |
| 151 | + map.insert(Iter(std::begin(arr)), Iter(std::end(arr))); |
| 152 | + assert(map.size() == 4); |
| 153 | + assert(*std::next(map.begin(), 0) == Pair(0, 0)); |
| 154 | + assert(*std::next(map.begin(), 1) == Pair(1, 1)); |
| 155 | + assert(*std::next(map.begin(), 2) == Pair(1, 1)); |
| 156 | + assert(*std::next(map.begin(), 3) == Pair(3, 3)); |
| 157 | + assert(std::next(map.begin(), 4) == map.end()); |
| 158 | + } |
| 159 | + { // With 1 element in the container - the same key |
| 160 | + using Map = std::multimap<int, int, std::less<int>, Alloc>; |
| 161 | + using Pair = std::pair<const int, int>; |
| 162 | + |
| 163 | + Pair arr[] = {Pair(1, 1), Pair(2, 2), Pair(3, 3)}; |
| 164 | + |
| 165 | + Map map; |
| 166 | + map.insert(Pair(1, 1)); |
| 167 | + map.insert(Iter(std::begin(arr)), Iter(std::end(arr))); |
| 168 | + assert(map.size() == 4); |
| 169 | + assert(*std::next(map.begin(), 0) == Pair(1, 1)); |
| 170 | + assert(*std::next(map.begin(), 1) == Pair(1, 1)); |
| 171 | + assert(*std::next(map.begin(), 2) == Pair(2, 2)); |
| 172 | + assert(*std::next(map.begin(), 3) == Pair(3, 3)); |
| 173 | + assert(std::next(map.begin(), 4) == map.end()); |
| 174 | + } |
| 175 | + { // With multiple elements in the container |
| 176 | + using Map = std::multimap<int, int, std::less<int>, Alloc>; |
| 177 | + using Pair = std::pair<const int, int>; |
| 178 | + |
| 179 | + Pair arr[] = {Pair(1, 1), Pair(3, 3), Pair(4, 4)}; |
| 180 | + |
| 181 | + Map map; |
| 182 | + map.insert(Pair(0, 0)); |
| 183 | + map.insert(Pair(1, 1)); |
| 184 | + map.insert(Pair(2, 2)); |
| 185 | + map.insert(Iter(std::begin(arr)), Iter(std::end(arr))); |
| 186 | + assert(map.size() == 6); |
| 187 | + assert(*std::next(map.begin(), 0) == Pair(0, 0)); |
| 188 | + assert(*std::next(map.begin(), 1) == Pair(1, 1)); |
| 189 | + assert(*std::next(map.begin(), 2) == Pair(1, 1)); |
| 190 | + assert(*std::next(map.begin(), 3) == Pair(2, 2)); |
| 191 | + assert(*std::next(map.begin(), 4) == Pair(3, 3)); |
| 192 | + assert(*std::next(map.begin(), 5) == Pair(4, 4)); |
| 193 | + assert(std::next(map.begin(), 6) == map.end()); |
| 194 | + } |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +void test() { |
| 199 | + test_alloc<cpp17_input_iterator<std::pair<const int, int>*>, std::allocator<std::pair<const int, int> > >(); |
| 200 | + test_alloc<cpp17_input_iterator<std::pair<const int, int>*>, min_allocator<std::pair<const int, int> > >(); |
| 201 | +} |
| 202 | + |
| 203 | +int main(int, char**) { |
| 204 | + test(); |
98 | 205 |
|
99 | 206 | return 0; |
100 | 207 | } |
0 commit comments