1//
2// HashSetTest.cpp
3//
4// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
5// and Contributors.
6//
7// SPDX-License-Identifier: BSL-1.0
8//
9
10
11#include "HashSetTest.h"
12#include "Poco/CppUnit/TestCaller.h"
13#include "Poco/CppUnit/TestSuite.h"
14#include "Poco/HashSet.h"
15#include <set>
16
17
18using Poco::Hash;
19using Poco::HashSet;
20
21
22HashSetTest::HashSetTest(const std::string& rName): CppUnit::TestCase(rName)
23{
24}
25
26
27HashSetTest::~HashSetTest()
28{
29}
30
31
32void HashSetTest::testInsert()
33{
34 const int N = 1000;
35
36 HashSet<int, Hash<int> > hs;
37
38 assertTrue (hs.empty());
39
40 for (int i = 0; i < N; ++i)
41 {
42 std::pair<HashSet<int, Hash<int> >::Iterator, bool> res = hs.insert(i);
43 assertTrue (*res.first == i);
44 assertTrue (res.second);
45 HashSet<int, Hash<int> >::Iterator it = hs.find(i);
46 assertTrue (it != hs.end());
47 assertTrue (*it == i);
48 assertTrue (hs.size() == i + 1);
49 }
50
51 assertTrue (!hs.empty());
52
53 for (int i = 0; i < N; ++i)
54 {
55 HashSet<int, Hash<int> >::Iterator it = hs.find(i);
56 assertTrue (it != hs.end());
57 assertTrue (*it == i);
58 }
59
60 for (int i = 0; i < N; ++i)
61 {
62 std::pair<HashSet<int, Hash<int> >::Iterator, bool> res = hs.insert(i);
63 assertTrue (*res.first == i);
64 assertTrue (!res.second);
65 }
66}
67
68
69void HashSetTest::testErase()
70{
71 const int N = 1000;
72
73 HashSet<int, Hash<int> > hs;
74
75 for (int i = 0; i < N; ++i)
76 {
77 hs.insert(i);
78 }
79 assertTrue (hs.size() == N);
80
81 for (int i = 0; i < N; i += 2)
82 {
83 hs.erase(i);
84 HashSet<int, Hash<int> >::Iterator it = hs.find(i);
85 assertTrue (it == hs.end());
86 }
87 assertTrue (hs.size() == N/2);
88
89 for (int i = 0; i < N; i += 2)
90 {
91 HashSet<int, Hash<int> >::Iterator it = hs.find(i);
92 assertTrue (it == hs.end());
93 }
94
95 for (int i = 1; i < N; i += 2)
96 {
97 HashSet<int, Hash<int> >::Iterator it = hs.find(i);
98 assertTrue (it != hs.end());
99 assertTrue (*it == i);
100 }
101
102 for (int i = 0; i < N; i += 2)
103 {
104 hs.insert(i);
105 }
106
107 for (int i = 0; i < N; ++i)
108 {
109 HashSet<int, Hash<int> >::Iterator it = hs.find(i);
110 assertTrue (it != hs.end());
111 assertTrue (*it == i);
112 }
113}
114
115
116void HashSetTest::testIterator()
117{
118 const int N = 1000;
119
120 HashSet<int, Hash<int> > hs;
121
122 for (int i = 0; i < N; ++i)
123 {
124 hs.insert(i);
125 }
126
127 std::set<int> values;
128 HashSet<int, Hash<int> >::Iterator it = hs.begin();
129 while (it != hs.end())
130 {
131 assertTrue (values.find(*it) == values.end());
132 values.insert(*it);
133 ++it;
134 }
135
136 assertTrue (values.size() == N);
137}
138
139
140void HashSetTest::testConstIterator()
141{
142 const int N = 1000;
143
144 HashSet<int, Hash<int> > hs;
145
146 for (int i = 0; i < N; ++i)
147 {
148 hs.insert(i);
149 }
150
151 std::set<int> values;
152 HashSet<int, Hash<int> >::ConstIterator it = hs.begin();
153 while (it != hs.end())
154 {
155 assertTrue (values.find(*it) == values.end());
156 values.insert(*it);
157 ++it;
158 }
159
160 assertTrue (values.size() == N);
161}
162
163
164void HashSetTest::setUp()
165{
166}
167
168
169void HashSetTest::tearDown()
170{
171}
172
173
174CppUnit::Test* HashSetTest::suite()
175{
176 CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("HashSetTest");
177
178 CppUnit_addTest(pSuite, HashSetTest, testInsert);
179 CppUnit_addTest(pSuite, HashSetTest, testErase);
180 CppUnit_addTest(pSuite, HashSetTest, testIterator);
181 CppUnit_addTest(pSuite, HashSetTest, testConstIterator);
182
183 return pSuite;
184}
185