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