Jlm
Loading...
Searching...
No Matches
HashSetTests.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2022 Nico Reißmann <nico.reissmann@gmail.com>
3 * See COPYING for terms of redistribution.
4 */
5
6#include <gtest/gtest.h>
7
9
10#include <memory>
11
12TEST(HashSetTests, TestInt)
13{
14 jlm::util::HashSet<int> hashSet({ 0, 1, 2, 3, 4, 5, 6, 7 });
15
16 EXPECT_TRUE(hashSet.Size());
17 EXPECT_TRUE(hashSet.Contains(3));
18 EXPECT_FALSE(hashSet.Contains(8));
19
20 EXPECT_TRUE(hashSet.insert(8));
21 EXPECT_TRUE(hashSet.Contains(8));
22 EXPECT_FALSE(hashSet.insert(8));
23
24 EXPECT_TRUE(hashSet.Remove(1));
25 EXPECT_FALSE(hashSet.Contains(1));
26 EXPECT_FALSE(hashSet.Remove(1));
27
28 int sum = 0;
29 for (auto & item : hashSet.Items())
30 sum += item;
31 EXPECT_EQ(sum, 0 + 2 + 3 + 4 + 5 + 6 + 7 + 8);
32
33 jlm::util::HashSet<int> hashSet2({ 8, 9 });
34 hashSet.UnionWith(hashSet2);
35 EXPECT_EQ(hashSet.Size(), 9u);
36
37 auto numRemoved = hashSet.RemoveWhere(
38 [](int n) -> bool
39 {
40 return n % 2 != 0;
41 });
42 EXPECT_EQ(numRemoved, 4u);
43
44 hashSet.Clear();
45 EXPECT_EQ(hashSet.Size(), 0u);
46}
47
48TEST(HashSetTests, TestUniquePointer)
49{
51
52 hashSet.insert(std::make_unique<int>(0));
53 EXPECT_EQ(hashSet.Size(), 1u);
54
55 hashSet.Remove(std::make_unique<int>(0));
56 EXPECT_EQ(hashSet.Size(), 1u);
57
58 hashSet.Clear();
59 EXPECT_EQ(hashSet.Size(), 0u);
60}
61
62TEST(HashSetTests, TestPair)
63{
64 jlm::util::HashSet<std::pair<int, int>> hashSet{ { 1, 10 }, { 5, 50 } };
65
66 // Inserting new value
67 auto result = hashSet.insert({ 7, 70 });
68 EXPECT_TRUE(result);
69 EXPECT_EQ(hashSet.Size(), 3u);
70
71 // Try inserting already inserted
72 result = hashSet.insert({ 1, 10 });
73 EXPECT_FALSE(result);
74 EXPECT_EQ(hashSet.Size(), 3u);
75
76 // Contains is only true in the correct order
77 EXPECT_TRUE(hashSet.Contains({ 5, 50 }));
78 EXPECT_FALSE(hashSet.Contains({ 50, 5 }));
79
80 // Removing works
81 result = hashSet.Remove({ 5, 50 });
82 EXPECT_TRUE(result);
83 EXPECT_EQ(hashSet.Size(), 2u);
84 result = hashSet.Remove({ 5, 50 });
85 EXPECT_FALSE(result);
86 EXPECT_EQ(hashSet.Size(), 2u);
87}
88
89TEST(HashSetTests, TestIsSubsetOf)
90{
91 jlm::util::HashSet<int> set12({ 1, 2 });
92 jlm::util::HashSet<int> set123({ 1, 2, 3 });
93 jlm::util::HashSet<int> set1234({ 1, 2, 3, 4 });
94
95 EXPECT_TRUE(set12.IsSubsetOf(set12));
96 EXPECT_TRUE(set12.IsSubsetOf(set123));
97 EXPECT_TRUE(set12.IsSubsetOf(set1234));
98 EXPECT_FALSE(set123.IsSubsetOf(set12));
99 EXPECT_TRUE(set123.IsSubsetOf(set1234));
100 EXPECT_FALSE(set1234.IsSubsetOf(set12));
101 EXPECT_FALSE(set1234.IsSubsetOf(set123));
102}
103
104TEST(HashSetTests, TestUnionWith)
105{
106 using namespace jlm::util;
107
108 HashSet<int> set12({ 1, 2 });
109 HashSet<int> set123({ 1, 2, 3 });
110 HashSet<int> set45({ 4, 5 });
111
112 // Unioning with a subset should not change anything
113 bool result = set123.UnionWith(set12);
114 EXPECT_FALSE(result);
115 EXPECT_EQ(set123.Size(), 3u);
116
117 // Putting {1, 2, 3} into {1, 2} should make it grow
118 result = set12.UnionWith(set123);
119 EXPECT_TRUE(result);
120 EXPECT_EQ(set12.Size(), 3u);
121 EXPECT_EQ(set12, set123);
122
123 // Unioning again does nothing
124 result = set12.UnionWith(set123);
125 EXPECT_FALSE(result);
126
127 // Test union and clear
128 result = set45.UnionWithAndClear(set123);
129 EXPECT_TRUE(result);
130 EXPECT_EQ(set45.Size(), 5u);
131 EXPECT_TRUE(set123.IsEmpty());
132}
133
134TEST(HashSetTests, TestIntersectWith)
135{
136 using namespace jlm::util;
137
138 HashSet<int> set12({ 1, 2 });
139 HashSet<int> set123({ 1, 2, 3 });
140 HashSet<int> set45({ 4, 5 });
141
142 set123.IntersectWith(set12);
143 EXPECT_EQ(set123, set12);
144
145 set123.IntersectWith(set45);
146 EXPECT_EQ(set123.Size(), 0u);
147
148 set123.insert(1);
149 set123.insert(2);
150 set123.insert(3);
151 set123.IntersectWithAndClear(set12);
152
153 EXPECT_EQ(set123.Size(), 2u);
154 EXPECT_EQ(set12.Size(), 0u);
155}
156
157TEST(HashSetTests, TestDifferenceWith)
158{
159 using namespace jlm::util;
160
161 HashSet<int> set12({ 1, 2 });
162 HashSet<int> set123({ 1, 2, 3 });
163 HashSet<int> set45({ 4, 5 });
164
165 const auto set12Copy = set12;
166
167 set123.DifferenceWith(set12); // {1, 2, 3} - {1, 2}
168 EXPECT_EQ(set123.Size(), 1u);
169 EXPECT_TRUE(set123.Contains(3));
170
171 // set12 was not touched
172 EXPECT_EQ(set12, set12Copy);
173
174 // Create the set {0, 1, 3}
175 set123.insert(0);
176 set123.insert(1);
177
178 set12.DifferenceWith(set123); // {1, 2} - {0, 1, 3}
179 EXPECT_EQ(set12.Size(), 1u);
180 EXPECT_TRUE(set12.Contains(2));
181
182 // Difference with other sets becomes empty
183 set45.DifferenceWith(set123);
184 set45.DifferenceWith(set12);
185 EXPECT_EQ(set45.Size(), 2u);
186
187 // We handle the case where both sets are the same set without crashing
188 set45.DifferenceWith(set45);
189 EXPECT_TRUE(set45.IsEmpty());
190}
TEST(HashSetTests, TestInt)
void DifferenceWith(const HashSet< ItemType > &other)
Definition HashSet.hpp:302
void Clear() noexcept
Definition HashSet.hpp:138
bool insert(ItemType item)
Definition HashSet.hpp:210
std::size_t Size() const noexcept
Definition HashSet.hpp:187
void IntersectWith(const HashSet< ItemType > &other)
Definition HashSet.hpp:269
bool UnionWithAndClear(HashSet< ItemType > &other)
Definition HashSet.hpp:252
bool Remove(ItemType item)
Definition HashSet.hpp:332
bool UnionWith(const HashSet< ItemType > &other)
Definition HashSet.hpp:236