| 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 |
// <unordered_map> |
| 10 |
|
| 11 |
// template <class Key, class T, class Hash, class Pred, class Alloc> |
| 12 |
// bool |
| 13 |
// operator==(const unordered_multimap<Key, T, Hash, Pred, Alloc>& x, |
| 14 |
// const unordered_multimap<Key, T, Hash, Pred, Alloc>& y); |
| 15 |
// |
| 16 |
// template <class Key, class T, class Hash, class Pred, class Alloc> |
| 17 |
// bool |
| 18 |
// operator!=(const unordered_multimap<Key, T, Hash, Pred, Alloc>& x, |
| 19 |
// const unordered_multimap<Key, T, Hash, Pred, Alloc>& y); |
| 20 |
|
| 21 |
#include <unordered_map> |
| 22 |
#include <string> |
| 23 |
#include <cassert> |
| 24 |
#include <iterator> |
| 25 |
|
| 26 |
#include "test_macros.h" |
| 27 |
#include "min_allocator.h" |
| 28 |
|
| 29 |
#include "test_comparisons.h" |
| 30 |
|
| 31 |
int main(int, char**) |
| 32 |
{ |
| 33 |
{ |
| 34 |
typedef std::unordered_multimap<int, std::string> C; |
| 35 |
typedef std::pair<int, std::string> P; |
| 36 |
P a[] = |
| 37 |
{ |
| 38 |
P(10, "ten"), |
| 39 |
P(20, "twenty"), |
| 40 |
P(20, "twenty 2"), |
| 41 |
P(30, "thirty"), |
| 42 |
P(40, "forty"), |
| 43 |
P(50, "fifty"), |
| 44 |
P(50, "fifty 2"), |
| 45 |
P(50, "fifty 3"), |
| 46 |
P(60, "sixty"), |
| 47 |
P(70, "seventy"), |
| 48 |
P(80, "eighty"), |
| 49 |
}; |
| 50 |
const C c1(std::begin(a), std::end(a)); |
| 51 |
const C c2; |
| 52 |
assert(testEquality(c1, c2, false)); |
| 53 |
} |
| 54 |
{ |
| 55 |
typedef std::unordered_multimap<int, std::string> C; |
| 56 |
typedef std::pair<int, std::string> P; |
| 57 |
P a[] = |
| 58 |
{ |
| 59 |
P(10, "ten"), |
| 60 |
P(20, "twenty"), |
| 61 |
P(20, "twenty 2"), |
| 62 |
P(30, "thirty"), |
| 63 |
P(40, "forty"), |
| 64 |
P(50, "fifty"), |
| 65 |
P(50, "fifty 2"), |
| 66 |
P(50, "fifty 3"), |
| 67 |
P(60, "sixty"), |
| 68 |
P(70, "seventy"), |
| 69 |
P(80, "eighty"), |
| 70 |
}; |
| 71 |
const C c1(std::begin(a), std::end(a)); |
| 72 |
const C c2 = c1; |
| 73 |
assert(testEquality(c1, c2, true)); |
| 74 |
} |
| 75 |
{ |
| 76 |
typedef std::unordered_multimap<int, std::string> C; |
| 77 |
typedef std::pair<int, std::string> P; |
| 78 |
P a[] = |
| 79 |
{ |
| 80 |
P(10, "ten"), |
| 81 |
P(20, "twenty"), |
| 82 |
P(20, "twenty 2"), |
| 83 |
P(30, "thirty"), |
| 84 |
P(40, "forty"), |
| 85 |
P(50, "fifty"), |
| 86 |
P(50, "fifty 2"), |
| 87 |
P(50, "fifty 3"), |
| 88 |
P(60, "sixty"), |
| 89 |
P(70, "seventy"), |
| 90 |
P(80, "eighty"), |
| 91 |
}; |
| 92 |
C c1(std::begin(a), std::end(a)); |
| 93 |
C c2 = c1; |
| 94 |
c2.rehash(30); |
| 95 |
assert(testEquality(c1, c2, true)); |
| 96 |
c2.insert(P(90, "ninety")); |
| 97 |
assert(testEquality(c1, c2, false)); |
| 98 |
c1.insert(P(90, "ninety")); |
| 99 |
assert(testEquality(c1, c2, true)); |
| 100 |
} |
| 101 |
{ |
| 102 |
typedef std::unordered_multimap<int, std::string> C; |
| 103 |
typedef std::pair<int, std::string> P; |
| 104 |
P a[] = |
| 105 |
{ |
| 106 |
P(10, "ten"), |
| 107 |
P(20, "twenty"), |
| 108 |
P(20, "twenty 2"), |
| 109 |
P(30, "thirty"), |
| 110 |
P(40, "forty"), |
| 111 |
P(50, "fifty"), |
| 112 |
P(50, "fifty 2"), |
| 113 |
P(50, "fifty 3"), |
| 114 |
P(60, "sixty"), |
| 115 |
P(70, "seventy"), |
| 116 |
P(80, "eighty"), |
| 117 |
}; |
| 118 |
C c1(std::begin(a), std::end(a)); |
| 119 |
C c2 = c1; |
| 120 |
assert(testEquality(c1, c2, true)); |
| 121 |
c1.insert(P(70, "seventy 2")); |
| 122 |
c2.insert(P(80, "eighty 2")); |
| 123 |
assert(testEquality(c1, c2, false)); |
| 124 |
} |
| 125 |
#if TEST_STD_VER >= 11 |
| 126 |
{ |
| 127 |
typedef std::unordered_multimap<int, std::string, std::hash<int>, std::equal_to<int>, |
| 128 |
min_allocator<std::pair<const int, std::string>>> C; |
| 129 |
typedef std::pair<int, std::string> P; |
| 130 |
P a[] = |
| 131 |
{ |
| 132 |
P(10, "ten"), |
| 133 |
P(20, "twenty"), |
| 134 |
P(20, "twenty 2"), |
| 135 |
P(30, "thirty"), |
| 136 |
P(40, "forty"), |
| 137 |
P(50, "fifty"), |
| 138 |
P(50, "fifty 2"), |
| 139 |
P(50, "fifty 3"), |
| 140 |
P(60, "sixty"), |
| 141 |
P(70, "seventy"), |
| 142 |
P(80, "eighty"), |
| 143 |
}; |
| 144 |
const C c1(std::begin(a), std::end(a)); |
| 145 |
const C c2; |
| 146 |
assert(testEquality(c1, c2, false)); |
| 147 |
} |
| 148 |
{ |
| 149 |
typedef std::unordered_multimap<int, std::string, std::hash<int>, std::equal_to<int>, |
| 150 |
min_allocator<std::pair<const int, std::string>>> C; |
| 151 |
typedef std::pair<int, std::string> P; |
| 152 |
P a[] = |
| 153 |
{ |
| 154 |
P(10, "ten"), |
| 155 |
P(20, "twenty"), |
| 156 |
P(20, "twenty 2"), |
| 157 |
P(30, "thirty"), |
| 158 |
P(40, "forty"), |
| 159 |
P(50, "fifty"), |
| 160 |
P(50, "fifty 2"), |
| 161 |
P(50, "fifty 3"), |
| 162 |
P(60, "sixty"), |
| 163 |
P(70, "seventy"), |
| 164 |
P(80, "eighty"), |
| 165 |
}; |
| 166 |
const C c1(std::begin(a), std::end(a)); |
| 167 |
const C c2 = c1; |
| 168 |
assert(testEquality(c1, c2, true)); |
| 169 |
} |
| 170 |
{ |
| 171 |
typedef std::unordered_multimap<int, std::string, std::hash<int>, std::equal_to<int>, |
| 172 |
min_allocator<std::pair<const int, std::string>>> C; |
| 173 |
typedef std::pair<int, std::string> P; |
| 174 |
P a[] = |
| 175 |
{ |
| 176 |
P(10, "ten"), |
| 177 |
P(20, "twenty"), |
| 178 |
P(20, "twenty 2"), |
| 179 |
P(30, "thirty"), |
| 180 |
P(40, "forty"), |
| 181 |
P(50, "fifty"), |
| 182 |
P(50, "fifty 2"), |
| 183 |
P(50, "fifty 3"), |
| 184 |
P(60, "sixty"), |
| 185 |
P(70, "seventy"), |
| 186 |
P(80, "eighty"), |
| 187 |
}; |
| 188 |
C c1(std::begin(a), std::end(a)); |
| 189 |
C c2 = c1; |
| 190 |
c2.rehash(30); |
| 191 |
assert(testEquality(c1, c2, true)); |
| 192 |
c2.insert(P(90, "ninety")); |
| 193 |
assert(testEquality(c1, c2, false)); |
| 194 |
c1.insert(P(90, "ninety")); |
| 195 |
assert(testEquality(c1, c2, true)); |
| 196 |
} |
| 197 |
{ |
| 198 |
typedef std::unordered_multimap<int, std::string, std::hash<int>, std::equal_to<int>, |
| 199 |
min_allocator<std::pair<const int, std::string>>> C; |
| 200 |
typedef std::pair<int, std::string> P; |
| 201 |
P a[] = |
| 202 |
{ |
| 203 |
P(10, "ten"), |
| 204 |
P(20, "twenty"), |
| 205 |
P(20, "twenty 2"), |
| 206 |
P(30, "thirty"), |
| 207 |
P(40, "forty"), |
| 208 |
P(50, "fifty"), |
| 209 |
P(50, "fifty 2"), |
| 210 |
P(50, "fifty 3"), |
| 211 |
P(60, "sixty"), |
| 212 |
P(70, "seventy"), |
| 213 |
P(80, "eighty"), |
| 214 |
}; |
| 215 |
C c1(std::begin(a), std::end(a)); |
| 216 |
C c2 = c1; |
| 217 |
assert(testEquality(c1, c2, true)); |
| 218 |
c1.insert(P(70, "seventy 2")); |
| 219 |
c2.insert(P(80, "eighty 2")); |
| 220 |
assert(testEquality(c1, c2, false)); |
| 221 |
} |
| 222 |
#endif |
| 223 |
|
| 224 |
// Make sure we take into account the number of times that a key repeats into equality. |
| 225 |
{ |
| 226 |
typedef std::pair<int, char> P; |
| 227 |
P a[] = {P(1, 'a'), P(1, 'b'), P(1, 'd'), P(2, 'b')}; |
| 228 |
P b[] = {P(1, 'a'), P(1, 'b'), P(1, 'b'), P(1, 'd'), P(2, 'b')}; |
| 229 |
|
| 230 |
std::unordered_multimap<int, char> c1(std::begin(a), std::end(a)); |
| 231 |
std::unordered_multimap<int, char> c2(std::begin(b), std::end(b)); |
| 232 |
assert(testEquality(c1, c2, false)); |
| 233 |
} |
| 234 |
|
| 235 |
// Make sure we incorporate the values into the equality of the maps. |
| 236 |
// If we were to compare only the keys (including how many time each key repeats), |
| 237 |
// the following test would fail cause only the values differ. |
| 238 |
{ |
| 239 |
typedef std::pair<int, char> P; |
| 240 |
P a[] = {P(1, 'a'), P(1, 'b'), P(1, 'd'), P(2, 'b')}; |
| 241 |
P b[] = {P(1, 'a'), P(1, 'b'), P(1, 'E'), P(2, 'b')}; |
| 242 |
// ^ different here |
| 243 |
|
| 244 |
std::unordered_multimap<int, char> c1(std::begin(a), std::end(a)); |
| 245 |
std::unordered_multimap<int, char> c2(std::begin(b), std::end(b)); |
| 246 |
assert(testEquality(c1, c2, false)); |
| 247 |
} |
| 248 |
|
| 249 |
return 0; |
| 250 |
} |