forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSetFixture.cs
395 lines (301 loc) · 11.2 KB
/
SetFixture.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
using System;
using System.Collections;
using NUnit.Framework;
namespace Iesi.Collections.Test
{
/// <summary>
/// Summary description for SetFixture.
/// </summary>
public abstract class SetFixture
{
private IList _aInitValues;
private IList _bInitValues;
protected ISet _set;
public static object one = "one";
public static object two = "two";
public static object three = "three";
[SetUp]
public virtual void SetUp()
{
_aInitValues = new ArrayList();
_aInitValues.Add("zero");
_aInitValues.Add("one");
_aInitValues.Add("two");
_aInitValues.Add("three");
_bInitValues = new ArrayList();
_bInitValues.Add("two");
_bInitValues.Add("three");
_bInitValues.Add("four");
_set = CreateInstance();
_set.Add(one);
_set.Add(two);
_set.Add(three);
}
#region System.IClonable Member Tests
[Test]
public void Clone()
{
ISet clonedSet = (ISet) _set.Clone();
Assert.AreEqual(ExpectedType, clonedSet.GetType(), "cloned set should be the same type");
Assert.AreEqual(_set.Count, clonedSet.Count, "set and cloned version should be same");
clonedSet.Add("not in original");
Assert.IsFalse(_set.Count == clonedSet.Count, "adding to clone should not add to original.");
foreach (object obj in _set)
{
Assert.IsTrue(clonedSet.Contains(obj), "cloned set should have same objects as original set.");
}
}
#endregion
#region System.Collections.ICollection Member Tests
[Test]
public void CopyTo()
{
object[] dest = new object[3];
_set.CopyTo(dest, 0);
int count = 0;
foreach (object obj in dest)
{
Assert.IsTrue(_set.Contains(obj), "set should contain the object in the array");
count++;
}
Assert.AreEqual(3, count, "should have 3 items in array");
}
[Test]
public void Count()
{
Assert.AreEqual(3, _set.Count, "should be 3 items");
Assert.AreEqual(0, CreateInstance().Count, "new set should have nothing in it.");
}
#endregion
#region Iesi.Collections.ISet Constructor Tests
[Test]
public void CtorWithDefaults()
{
ArrayList init = new ArrayList(3);
init.Add("one");
init.Add("two");
init.Add("three");
ISet theSet = CreateInstance(init);
Assert.AreEqual(3, init.Count, "3 items in set");
int index = 0;
foreach (object obj in init)
{
Assert.IsTrue(theSet.Contains(obj), "set should contain obj at index = " + index.ToString());
index++;
}
}
#endregion
#region Iesi.Collections.ISet Member Tests
[Test]
public void Add()
{
Assert.IsTrue(_set.Add("four"), "should have added 'four'");
Assert.AreEqual(4, _set.Count, "should have added 'four'");
Assert.IsFalse(_set.Add(two), "'two' was already there");
Assert.AreEqual(4, _set.Count, "object already in set");
}
[Test]
public void AddAll()
{
ArrayList addAll = new ArrayList(3);
addAll.Add("four");
addAll.Add("five");
addAll.Add("four");
Assert.IsTrue(_set.AddAll(addAll), "should have modified set");
Assert.AreEqual(5, _set.Count, "should have added one 'four' and 'five'");
Assert.IsFalse(_set.AddAll(addAll), "all elements already in set");
}
[Test]
public void Clear()
{
_set.Clear();
Assert.AreEqual(0, _set.Count, "should have no items in ISet.");
}
[Test]
public void Contains()
{
Assert.IsTrue(_set.Contains(one), "does contain one");
Assert.IsFalse(_set.Contains("four"), "does not contain 'four'");
}
[Test]
public void ContainsAll()
{
ArrayList all = new ArrayList(2);
all.Add("one");
all.Add("two");
Assert.IsTrue(_set.ContainsAll(all), "should contain 'one' and 'two'");
all.Add("not in there");
Assert.IsFalse(_set.ContainsAll(all), "should not contain the just added 'not in there'");
}
[Test]
public void ExclusiveOr()
{
ISet a = CreateInstance(_aInitValues);
ISet b = CreateInstance(_bInitValues);
ISet ab = Set.ExclusiveOr(a, b);
Assert.AreEqual(3, ab.Count, "contains 3 elements - 'zero', 'one', and 'four'");
Assert.IsTrue(ab.Contains("zero"), "should contain 'zero'");
Assert.IsTrue(ab.Contains("one"), "should contain 'one'");
Assert.IsTrue(ab.Contains("four"), "should contain 'four'");
Assert.IsTrue(a.ContainsAll(_aInitValues), "should not have modified a");
Assert.IsTrue(b.ContainsAll(_bInitValues), "should not have modified b");
ISet aNull = Set.ExclusiveOr(a, null);
Assert.AreEqual(_aInitValues.Count, aNull.Count, "count still same");
Assert.IsTrue(aNull.ContainsAll(_aInitValues), "all A elements kept");
ISet bNull = Set.ExclusiveOr(null, b);
Assert.AreEqual(_bInitValues.Count, bNull.Count, "count still same");
Assert.IsTrue(bNull.ContainsAll(_bInitValues), "all B elements kept");
ISet bothNull = Set.ExclusiveOr(null, null);
Assert.AreEqual(null, bothNull, "two null sets return null set");
}
[Test]
public void Intersect()
{
ISet a = CreateInstance(_aInitValues);
ISet b = CreateInstance(_bInitValues);
ISet ab = Set.Intersect(a, b);
Assert.AreEqual(2, ab.Count, "contains 2 elements - 'two', and 'three'");
Assert.IsTrue(ab.Contains("two"), "should contain 'two'");
Assert.IsTrue(ab.Contains("three"), "should contain 'three'");
Assert.IsTrue(a.ContainsAll(_aInitValues), "should not have modified a");
Assert.IsTrue(b.ContainsAll(_bInitValues), "should not have modified b");
ISet aNull = Set.Intersect(a, null);
Assert.AreEqual(0, aNull.Count, "no elements intersected with null set");
ISet bNull = Set.Intersect(null, b);
Assert.AreEqual(0, bNull.Count, "no elements intersected with null set");
ISet bothNull = Set.Intersect(null, null);
Assert.AreEqual(null, bothNull, "null sets intersect as null set");
}
[Test]
public void IsEmpty()
{
Assert.IsFalse(_set.IsEmpty, "set should have initial values");
Assert.IsTrue(CreateInstance().IsEmpty, "new set is empty");
}
[Test]
public void Minus()
{
ISet a = CreateInstance(_aInitValues);
ISet b = CreateInstance(_bInitValues);
ISet ab = Set.Minus(a, b);
Assert.AreEqual(2, ab.Count, "contains 2 elements - 'zero', and 'one'");
Assert.IsTrue(ab.Contains("zero"), "should contain 'zero'");
Assert.IsTrue(ab.Contains("one"), "should contain 'one'");
Assert.IsTrue(a.ContainsAll(_aInitValues), "should not have modified a");
Assert.IsTrue(b.ContainsAll(_bInitValues), "should not have modified b");
ISet aNull = Set.Minus(a, null);
Assert.IsTrue(aNull.ContainsAll(_aInitValues), "should have removed no elements");
ISet bNull = Set.Minus(null, b);
Assert.AreEqual(null, bNull, "null set remained null");
ISet bothNull = Set.Minus(null, null);
Assert.AreEqual(null, bothNull, "both sets are null");
}
[Test]
public void Remove()
{
Assert.IsTrue(_set.Remove(one), "should have removed 'one'");
Assert.IsFalse(_set.Contains(one), "one should have been removed");
Assert.AreEqual(2, _set.Count, "should be 2 items after one removed.");
Assert.IsFalse(_set.Remove(one), "was already removed.");
}
[Test]
public void RemoveAll()
{
ArrayList all = new ArrayList(2);
all.Add(one);
all.Add("not in there");
Assert.IsTrue(_set.RemoveAll(all), "should have removed an element");
Assert.AreEqual(2, _set.Count, "should be down to 2 elements.");
Assert.IsFalse(_set.RemoveAll(all), "all of the elements already removed so set not modified.");
}
[Test]
public void RetainAll()
{
ArrayList retain = new ArrayList(2);
retain.Add(one);
retain.Add("not in there");
Assert.IsTrue(_set.RetainAll(retain), "set was modified");
Assert.AreEqual(1, _set.Count, "only 1 element retained");
Assert.IsFalse(_set.RetainAll(retain), "set was not modified");
}
[Test]
public void Union()
{
ISet a = CreateInstance(_aInitValues);
ISet b = CreateInstance(_bInitValues);
ISet ab = Set.Union(a, b);
Assert.AreEqual(5, ab.Count, "contains 5 elements - 'zero' through 'four'");
Assert.IsTrue(ab.Contains("zero"), "should contain 'zero'");
Assert.IsTrue(ab.Contains("one"), "should contain 'one'");
Assert.IsTrue(ab.Contains("two"), "should contain 'two'");
Assert.IsTrue(ab.Contains("three"), "should contain 'three'");
Assert.IsTrue(ab.Contains("four"), "should contain 'four'");
Assert.IsTrue(a.ContainsAll(_aInitValues), "should not have modified a");
Assert.IsTrue(b.ContainsAll(_bInitValues), "should not have modified b");
ISet aNull = Set.Union(a, null);
Assert.AreEqual(_aInitValues.Count, aNull.Count, "count not changed");
Assert.IsTrue(aNull.ContainsAll(_aInitValues), "still contains all initial values");
ISet bNull = Set.Union(null, b);
Assert.AreEqual(_bInitValues.Count, bNull.Count, "count not changed");
Assert.IsTrue(bNull.ContainsAll(_bInitValues), "still contains all initial values");
ISet bothNull = Set.Union(null, null);
Assert.AreEqual(null, bothNull, "two nulls intersect as null");
}
#endregion
#region Iesi.Collection.ISet Operator Tests
[Test]
public void ExclusiveOrOperator()
{
ISet a = CreateInstance(_aInitValues);
ISet b = CreateInstance(_bInitValues);
ISet ab = (Set) a ^ (Set) b;
Assert.AreEqual(3, ab.Count, "contains 3 elements - 'zero', 'one', and 'four'");
Assert.IsTrue(ab.Contains("zero"), "should contain 'zero'");
Assert.IsTrue(ab.Contains("one"), "should contain 'one'");
Assert.IsTrue(ab.Contains("four"), "should contain 'four'");
Assert.IsTrue(a.ContainsAll(_aInitValues), "should not have modified a");
Assert.IsTrue(b.ContainsAll(_bInitValues), "should not have modified b");
}
[Test]
public void IntersectOperator()
{
ISet a = CreateInstance(_aInitValues);
ISet b = CreateInstance(_bInitValues);
ISet ab = (Set) a & (Set) b;
Assert.AreEqual(2, ab.Count, "contains 2 elements - 'two', and 'three'");
Assert.IsTrue(ab.Contains("two"), "should contain 'two'");
Assert.IsTrue(ab.Contains("three"), "should contain 'three'");
}
[Test]
public void MinusOperator()
{
ISet a = CreateInstance(_aInitValues);
ISet b = CreateInstance(_bInitValues);
ISet ab = (Set) a - (Set) b;
Assert.AreEqual(2, ab.Count, "contains 2 elements - 'zero', and 'one'");
Assert.IsTrue(ab.Contains("zero"), "should contain 'zero'");
Assert.IsTrue(ab.Contains("one"), "should contain 'one'");
Assert.IsTrue(a.ContainsAll(_aInitValues), "should not have modified a");
Assert.IsTrue(b.ContainsAll(_bInitValues), "should not have modified b");
}
[Test]
public void UnionOperator()
{
ISet a = CreateInstance(_aInitValues);
ISet b = CreateInstance(_bInitValues);
ISet ab = (Set) a | (Set) b;
Assert.AreEqual(5, ab.Count, "contains 5 elements - 'zero' through 'four'");
Assert.IsTrue(ab.Contains("zero"), "should contain 'zero'");
Assert.IsTrue(ab.Contains("one"), "should contain 'one'");
Assert.IsTrue(ab.Contains("two"), "should contain 'two'");
Assert.IsTrue(ab.Contains("three"), "should contain 'three'");
Assert.IsTrue(ab.Contains("four"), "should contain 'four'");
Assert.IsTrue(a.ContainsAll(_aInitValues), "should not have modified a");
Assert.IsTrue(b.ContainsAll(_bInitValues), "should not have modified b");
}
#endregion
protected abstract ISet CreateInstance();
protected abstract ISet CreateInstance(ICollection init);
protected abstract Type ExpectedType { get; }
}
}