134 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_set>10 11// template <class Key, class Hash, class Pred, class Alloc>12// bool13// operator==(const unordered_multiset<Key, Hash, Pred, Alloc>& x,14// const unordered_multiset<Key, Hash, Pred, Alloc>& y);15//16// template <class Key, class Hash, class Pred, class Alloc>17// bool18// operator!=(const unordered_multiset<Key, Hash, Pred, Alloc>& x,19// const unordered_multiset<Key, Hash, Pred, Alloc>& y);20 21#include <unordered_set>22#include <cassert>23#include <cstddef>24 25#include "test_macros.h"26#include "min_allocator.h"27 28#include "test_comparisons.h"29 30int main(int, char**) {31 {32 typedef std::unordered_multiset<int> C;33 typedef int P;34 P a[] = {P(10), P(20), P(20), P(30), P(40), P(50), P(50), P(50), P(60), P(70), P(80)};35 const C c1(std::begin(a), std::end(a));36 const C c2;37 assert(!(c1 == c2));38 assert((c1 != c2));39 }40 {41 typedef std::unordered_multiset<int> C;42 typedef int P;43 P a[] = {P(10), P(20), P(20), P(30), P(40), P(50), P(50), P(50), P(60), P(70), P(80)};44 const C c1(std::begin(a), std::end(a));45 const C c2 = c1;46 assert((c1 == c2));47 assert(!(c1 != c2));48 }49 {50 typedef std::unordered_multiset<int> C;51 typedef int P;52 P a[] = {P(10), P(20), P(20), P(30), P(40), P(50), P(50), P(50), P(60), P(70), P(80)};53 C c1(std::begin(a), std::end(a));54 C c2 = c1;55 c2.rehash(30);56 assert((c1 == c2));57 assert(!(c1 != c2));58 c2.insert(P(90));59 assert(!(c1 == c2));60 assert((c1 != c2));61 c1.insert(P(90));62 assert((c1 == c2));63 assert(!(c1 != c2));64 }65#if TEST_STD_VER >= 1166 {67 typedef std::unordered_multiset<int, std::hash<int>, std::equal_to<int>, min_allocator<int>> C;68 typedef int P;69 P a[] = {P(10), P(20), P(20), P(30), P(40), P(50), P(50), P(50), P(60), P(70), P(80)};70 const C c1(std::begin(a), std::end(a));71 const C c2;72 assert(!(c1 == c2));73 assert((c1 != c2));74 }75 {76 typedef std::unordered_multiset<int, std::hash<int>, std::equal_to<int>, min_allocator<int>> C;77 typedef int P;78 P a[] = {P(10), P(20), P(20), P(30), P(40), P(50), P(50), P(50), P(60), P(70), P(80)};79 const C c1(std::begin(a), std::end(a));80 const C c2 = c1;81 assert((c1 == c2));82 assert(!(c1 != c2));83 }84 {85 typedef std::unordered_multiset<int, std::hash<int>, std::equal_to<int>, min_allocator<int>> C;86 typedef int P;87 P a[] = {P(10), P(20), P(20), P(30), P(40), P(50), P(50), P(50), P(60), P(70), P(80)};88 C c1(std::begin(a), std::end(a));89 C c2 = c1;90 c2.rehash(30);91 assert((c1 == c2));92 assert(!(c1 != c2));93 c2.insert(P(90));94 assert(!(c1 == c2));95 assert((c1 != c2));96 c1.insert(P(90));97 assert((c1 == c2));98 assert(!(c1 != c2));99 }100#endif101 102 // Make sure we take into account the number of times that a key repeats into equality.103 {104 int a[] = {1, 1, 1, 2};105 int b[] = {1, 1, 1, 1, 2};106 107 std::unordered_multiset<int> c1(std::begin(a), std::end(a));108 std::unordered_multiset<int> c2(std::begin(b), std::end(b));109 assert(testEquality(c1, c2, false));110 }111 112 // Make sure we behave properly when a custom key predicate is provided.113 {114 int a[] = {1, 3};115 int b[] = {1, 1};116 // A very poor hash117 struct HashModuloOddness {118 std::size_t operator()(int x) const { return std::hash<int>()(x % 2); }119 };120 // A very poor hash121 struct CompareModuloOddness {122 bool operator()(int x, int y) const { return (x % 2) == (y % 2); }123 };124 125 using Set = std::unordered_multiset<int, HashModuloOddness, CompareModuloOddness>;126 Set c1(std::begin(a), std::end(a));127 Set c2(std::begin(b), std::end(b));128 129 assert(testEquality(c1, c2, false));130 }131 132 return 0;133}134