1 /*
2 * unittests/core/Set.cc
3 * Apto
4 *
5 * Created by David on 2/14/11.
6 * Copyright 2011 David Michael Bryson. All rights reserved.
7 * http://programerror.com/software/apto
8 *
9 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
10 * following conditions are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
13 * following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
15 * following disclaimer in the documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of David Michael Bryson, nor the names of contributors may be used to endorse or promote
17 * products derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY DAVID MICHAEL BRYSON AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL DAVID MICHAEL BRYSON OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
25 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 * Authors: David M. Bryson <david@programerror.com>
28 *
29 */
30
31 #include "apto/core/Array.h"
32 #include "apto/core/ArrayUtils.h"
33 #include "apto/core/Set.h"
34
35 #include "gtest/gtest.h"
36
37
38 // Set<int, HashBTree>
39 // --------------------------------------------------------------------------------------------------------------
40
TEST(CoreHashBTreeSet,Construction)41 TEST(CoreHashBTreeSet, Construction) {
42 Apto::Set<int, Apto::DefaultHashBTree> set;
43 EXPECT_EQ(0, set.GetSize());
44 }
45
46
TEST(CoreHashBTreeSet,Insertion)47 TEST(CoreHashBTreeSet, Insertion) {
48 Apto::Set<int, Apto::DefaultHashBTree> set;
49 for (int i = 0; i < 5; i++) set.Insert(i);
50 EXPECT_EQ(5, set.GetSize());
51
52 set.Insert(4);
53 EXPECT_EQ(5, set.GetSize());
54
55 for (int i = 0; i < 5; i++) EXPECT_TRUE(set.Has(i));
56
57 set.Insert(5);
58 EXPECT_EQ(6, set.GetSize());
59 }
60
61
TEST(CoreHashBTreeSet,Assignment)62 TEST(CoreHashBTreeSet, Assignment) {
63 Apto::Set<int, Apto::DefaultHashBTree> set1;
64 for (int i = 0; i < 4; i++) set1.Insert(i);
65
66 Apto::Set<int, Apto::DefaultHashBTree> set2;
67 for (int i = 0; i < 4; i++) set2.Insert(i + 2);
68 EXPECT_TRUE(set2.Has(5));
69
70 set2 = set1;
71 for (int i = 0; i < 4; i++) EXPECT_TRUE(set2.Has(i));
72 EXPECT_FALSE(set2.Has(5));
73
74 Apto::Set<int, Apto::DefaultHashBTree> set3(set1);
75 for (int i = 0; i < 4; i++) EXPECT_TRUE(set3.Has(i));
76 }
77
78
TEST(CoreHashBTreeSet,Removal)79 TEST(CoreHashBTreeSet, Removal) {
80 Apto::Set<int, Apto::DefaultHashBTree> set;
81 for (int i = 0; i < 5; i++) set.Insert(i);
82 EXPECT_EQ(5, set.GetSize());
83
84 set.Remove(4);
85 EXPECT_EQ(4, set.GetSize());
86 }
87
88
TEST(CoreHashBTreeSet,Comparison)89 TEST(CoreHashBTreeSet, Comparison) {
90 Apto::Set<int, Apto::DefaultHashBTree> set1;
91 for (int i = 0; i < 4; i++) set1.Insert(i);
92 EXPECT_TRUE(set1 == set1);
93 EXPECT_FALSE(set1 != set1);
94
95 Apto::Set<int, Apto::DefaultHashBTree> set2;
96 for (int i = 0; i < 4; i++) set2.Insert(i + 2);
97 EXPECT_TRUE(set2 == set2);
98 EXPECT_FALSE(set2 != set2);
99
100 EXPECT_FALSE(set1 == set2);
101 EXPECT_TRUE(set1 != set2);
102 EXPECT_FALSE(set2 == set1);
103 EXPECT_TRUE(set2 != set1);
104
105 set2 = set1;
106 EXPECT_FALSE(set1 != set2);
107 EXPECT_TRUE(set1 == set2);
108 EXPECT_FALSE(set2 != set1);
109 EXPECT_TRUE(set2 == set1);
110
111 Apto::Set<int, Apto::DefaultHashBTree> set3(set1);
112 EXPECT_FALSE(set1 != set3);
113 EXPECT_TRUE(set1 == set3);
114 EXPECT_FALSE(set3 != set1);
115 EXPECT_TRUE(set3 == set1);
116
117 set3.Insert(5);
118 EXPECT_FALSE(set1 == set3);
119 EXPECT_TRUE(set1 != set3);
120 EXPECT_FALSE(set3 == set1);
121 EXPECT_TRUE(set3 != set1);
122 }
123
124
TEST(CoreHashBTreeSet,Iteration)125 TEST(CoreHashBTreeSet, Iteration) {
126 Apto::Set<int, Apto::DefaultHashBTree> set1;
127 for (int i = 0; i < 4; i++) set1.Insert(i);
128
129 Apto::Array<int> array(set1.GetSize());
130 Apto::Set<int, Apto::DefaultHashBTree>::Iterator it = set1.Begin();
131 for (int i = 0; it.Next(); i++) array[i] = *it.Get();
132 QSort(array);
133 for (int i = 0; i < array.GetSize(); i++) EXPECT_EQ(i, array[i]);
134
135 const Apto::Set<int, Apto::DefaultHashBTree>& cset1 = set1;
136 Apto::Set<int, Apto::DefaultHashBTree>::ConstIterator cit = cset1.Begin();
137 for (int i = 0; cit.Next(); i++) array[i] = *cit.Get();
138 QSort(array);
139 for (int i = 0; i < array.GetSize(); i++) EXPECT_EQ(i, array[i]);
140 }
141
142
143 // Set<int, HashBTree, Multi>
144 // --------------------------------------------------------------------------------------------------------------
145
TEST(CoreHashBTreeMulitSet,Construction)146 TEST(CoreHashBTreeMulitSet, Construction) {
147 Apto::Set<int, Apto::DefaultHashBTree, Apto::Multi> set;
148 EXPECT_EQ(0, set.GetSize());
149 }
150
151
TEST(CoreHashBTreeMulitSet,Insertion)152 TEST(CoreHashBTreeMulitSet, Insertion) {
153 Apto::Set<int, Apto::DefaultHashBTree, Apto::Multi> set;
154 for (int i = 0; i < 5; i++) set.Insert(i);
155 EXPECT_EQ(5, set.GetSize());
156
157 set.Insert(4);
158 EXPECT_EQ(6, set.GetSize());
159
160 for (int i = 0; i < 5; i++) EXPECT_TRUE(set.Has(i));
161
162 set.Insert(5);
163 EXPECT_EQ(7, set.GetSize());
164 }
165
166
TEST(CoreHashBTreeMulitSet,Assignment)167 TEST(CoreHashBTreeMulitSet, Assignment) {
168 Apto::Set<int, Apto::DefaultHashBTree, Apto::Multi> set1;
169 for (int i = 0; i < 4; i++) {
170 set1.Insert(i);
171 set1.Insert(i);
172 }
173 EXPECT_EQ(8, set1.GetSize());
174
175 Apto::Set<int, Apto::DefaultHashBTree, Apto::Multi> set2;
176 for (int i = 0; i < 4; i++) {
177 set2.Insert(i + 2);
178 set2.Insert(i + 2);
179 set2.Insert(i + 2);
180 }
181 EXPECT_TRUE(set2.Has(5));
182 EXPECT_EQ(3, set2.Count(5));
183 EXPECT_EQ(12, set2.GetSize());
184
185 set2 = set1;
186 EXPECT_EQ(8, set2.GetSize());
187 for (int i = 0; i < 4; i++) EXPECT_TRUE(set2.Has(i));
188 EXPECT_FALSE(set2.Has(5));
189
190 Apto::Set<int, Apto::DefaultHashBTree, Apto::Multi> set3(set1);
191 EXPECT_EQ(8, set3.GetSize());
192 for (int i = 0; i < 4; i++) {
193 EXPECT_TRUE(set3.Has(i));
194 EXPECT_EQ(2, set3.Count(i));
195 }
196 }
197
198
TEST(CoreHashBTreeMulitSet,Removal)199 TEST(CoreHashBTreeMulitSet, Removal) {
200 Apto::Set<int, Apto::DefaultHashBTree, Apto::Multi> set;
201 for (int i = 0; i < 5; i++) set.Insert(i);
202 EXPECT_EQ(5, set.GetSize());
203
204 set.Insert(4);
205 EXPECT_EQ(6, set.GetSize());
206
207 set.Remove(4);
208 EXPECT_EQ(5, set.GetSize());
209 EXPECT_TRUE(set.Has(4));
210
211 set.Remove(4);
212 EXPECT_EQ(4, set.GetSize());
213 EXPECT_FALSE(set.Has(4));
214 }
215
216
TEST(CoreHashBTreeMulitSet,Comparison)217 TEST(CoreHashBTreeMulitSet, Comparison) {
218 Apto::Set<int, Apto::DefaultHashBTree, Apto::Multi> set1;
219 for (int i = 0; i < 4; i++) set1.Insert(i);
220 set1.Insert(3);
221 EXPECT_TRUE(set1 == set1);
222 EXPECT_FALSE(set1 != set1);
223
224 Apto::Set<int, Apto::DefaultHashBTree, Apto::Multi> set2;
225 for (int i = 0; i < 4; i++) set2.Insert(i + 2);
226 EXPECT_TRUE(set2 == set2);
227 EXPECT_FALSE(set2 != set2);
228
229 EXPECT_FALSE(set1 == set2);
230 EXPECT_TRUE(set1 != set2);
231 EXPECT_FALSE(set2 == set1);
232 EXPECT_TRUE(set2 != set1);
233
234 set2 = set1;
235 EXPECT_FALSE(set1 != set2);
236 EXPECT_TRUE(set1 == set2);
237 EXPECT_FALSE(set2 != set1);
238 EXPECT_TRUE(set2 == set1);
239
240 Apto::Set<int, Apto::DefaultHashBTree, Apto::Multi> set3(set1);
241 EXPECT_FALSE(set1 != set3);
242 EXPECT_TRUE(set1 == set3);
243 EXPECT_FALSE(set3 != set1);
244 EXPECT_TRUE(set3 == set1);
245
246 set3.Insert(5);
247 EXPECT_FALSE(set1 == set3);
248 EXPECT_TRUE(set1 != set3);
249 EXPECT_FALSE(set3 == set1);
250 EXPECT_TRUE(set3 != set1);
251 }
252
253
TEST(CoreHashBTreeMulitSet,Iteration)254 TEST(CoreHashBTreeMulitSet, Iteration) {
255 Apto::Set<int, Apto::DefaultHashBTree, Apto::Multi> set1;
256 for (int i = 0; i < 8; i += 2) {
257 set1.Insert(i);
258 set1.Insert(i);
259 }
260
261 Apto::Array<int> array(set1.GetSize());
262 Apto::Set<int, Apto::DefaultHashBTree, Apto::Multi>::Iterator it = set1.Begin();
263 for (int i = 0; it.Next(); i++) array[i] = *it.Get();
264 QSort(array);
265 for (int i = 0; i < array.GetSize(); i += 2) {
266 EXPECT_EQ(i, array[i]);
267 EXPECT_EQ(i, array[i + 1]);
268 }
269
270 const Apto::Set<int, Apto::DefaultHashBTree, Apto::Multi>& cset1 = set1;
271 Apto::Set<int, Apto::DefaultHashBTree, Apto::Multi>::ConstIterator cit = cset1.Begin();
272 for (int i = 0; cit.Next(); i++) array[i] = *cit.Get();
273 QSort(array);
274 for (int i = 0; i < array.GetSize(); i += 2) {
275 EXPECT_EQ(i, array[i]);
276 EXPECT_EQ(i, array[i + 1]);
277 }
278 }
279
280