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