1
|
/*
|
2
|
Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft
|
3
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
of this software and associated documentation files (the "Software"), to deal
|
5
|
in the Software without restriction, including without limitation the rights
|
6
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
copies of the Software, and to permit persons to whom the Software is
|
8
|
furnished to do so, subject to the following conditions:
|
9
|
|
10
|
The above copyright notice and this permission notice shall be included in
|
11
|
all copies or substantial portions of the Software.
|
12
|
|
13
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
19
|
SOFTWARE.
|
20
|
*/
|
21
|
|
22
|
using System;
|
23
|
using C5;
|
24
|
using NUnit.Framework;
|
25
|
using SCG = System.Collections.Generic;
|
26
|
namespace C5UnitTests.hashtable.set
|
27
|
{
|
28
|
using CollectionOfInt = HashSet<int>;
|
29
|
|
30
|
[TestFixture]
|
31
|
public class GenericTesters
|
32
|
{
|
33
|
[Test]
|
34
|
public void TestEvents()
|
35
|
{
|
36
|
Fun<CollectionOfInt> factory = delegate() { return new CollectionOfInt(TenEqualityComparer.Default); };
|
37
|
new C5UnitTests.Templates.Events.CollectionTester<CollectionOfInt>().Test(factory);
|
38
|
}
|
39
|
|
40
|
[Test]
|
41
|
public void Extensible()
|
42
|
{
|
43
|
C5UnitTests.Templates.Extensible.Clone.Tester<CollectionOfInt>();
|
44
|
C5UnitTests.Templates.Extensible.Serialization.Tester<CollectionOfInt>();
|
45
|
}
|
46
|
}
|
47
|
|
48
|
static class Factory
|
49
|
{
|
50
|
public static ICollection<T> New<T>() { return new HashSet<T>(); }
|
51
|
}
|
52
|
|
53
|
|
54
|
namespace Enumerable
|
55
|
{
|
56
|
[TestFixture]
|
57
|
public class Multiops
|
58
|
{
|
59
|
private HashSet<int> list;
|
60
|
|
61
|
private Fun<int, bool> always, never, even;
|
62
|
|
63
|
|
64
|
[SetUp]
|
65
|
public void Init()
|
66
|
{
|
67
|
list = new HashSet<int>();
|
68
|
always = delegate { return true; };
|
69
|
never = delegate { return false; };
|
70
|
even = delegate(int i) { return i % 2 == 0; };
|
71
|
}
|
72
|
|
73
|
|
74
|
[Test]
|
75
|
public void All()
|
76
|
{
|
77
|
Assert.IsTrue(list.All(always));
|
78
|
Assert.IsTrue(list.All(never));
|
79
|
Assert.IsTrue(list.All(even));
|
80
|
list.Add(0);
|
81
|
Assert.IsTrue(list.All(always));
|
82
|
Assert.IsFalse(list.All(never));
|
83
|
Assert.IsTrue(list.All(even));
|
84
|
list.Add(5);
|
85
|
Assert.IsTrue(list.All(always));
|
86
|
Assert.IsFalse(list.All(never));
|
87
|
Assert.IsFalse(list.All(even));
|
88
|
}
|
89
|
|
90
|
|
91
|
[Test]
|
92
|
public void Exists()
|
93
|
{
|
94
|
Assert.IsFalse(list.Exists(always));
|
95
|
Assert.IsFalse(list.Exists(never));
|
96
|
Assert.IsFalse(list.Exists(even));
|
97
|
list.Add(5);
|
98
|
Assert.IsTrue(list.Exists(always));
|
99
|
Assert.IsFalse(list.Exists(never));
|
100
|
Assert.IsFalse(list.Exists(even));
|
101
|
list.Add(8);
|
102
|
Assert.IsTrue(list.Exists(always));
|
103
|
Assert.IsFalse(list.Exists(never));
|
104
|
Assert.IsTrue(list.Exists(even));
|
105
|
}
|
106
|
|
107
|
|
108
|
[Test]
|
109
|
public void Apply()
|
110
|
{
|
111
|
int sum = 0;
|
112
|
Act<int> a = delegate(int i) { sum = i + 10 * sum; };
|
113
|
|
114
|
list.Apply(a);
|
115
|
Assert.AreEqual(0, sum);
|
116
|
sum = 0;
|
117
|
list.Add(5); list.Add(8); list.Add(7); list.Add(5);
|
118
|
list.Apply(a);
|
119
|
Assert.AreEqual(758, sum);
|
120
|
}
|
121
|
|
122
|
|
123
|
[TearDown]
|
124
|
public void Dispose() { list = null; }
|
125
|
}
|
126
|
|
127
|
|
128
|
|
129
|
[TestFixture]
|
130
|
public class GetEnumerator
|
131
|
{
|
132
|
private HashSet<int> hashset;
|
133
|
|
134
|
|
135
|
[SetUp]
|
136
|
public void Init() { hashset = new HashSet<int>(); }
|
137
|
|
138
|
|
139
|
[Test]
|
140
|
public void Empty()
|
141
|
{
|
142
|
SCG.IEnumerator<int> e = hashset.GetEnumerator();
|
143
|
|
144
|
Assert.IsFalse(e.MoveNext());
|
145
|
}
|
146
|
|
147
|
|
148
|
[Test]
|
149
|
public void Normal()
|
150
|
{
|
151
|
hashset.Add(5);
|
152
|
hashset.Add(8);
|
153
|
hashset.Add(5);
|
154
|
hashset.Add(5);
|
155
|
hashset.Add(10);
|
156
|
hashset.Add(1);
|
157
|
hashset.Add(16);
|
158
|
hashset.Add(18);
|
159
|
hashset.Add(17);
|
160
|
hashset.Add(33);
|
161
|
Assert.IsTrue(IC.seteq(hashset, 1, 5, 8, 10, 16, 17, 18, 33));
|
162
|
}
|
163
|
|
164
|
[Test]
|
165
|
public void DoDispose()
|
166
|
{
|
167
|
hashset.Add(5);
|
168
|
hashset.Add(8);
|
169
|
hashset.Add(5);
|
170
|
|
171
|
SCG.IEnumerator<int> e = hashset.GetEnumerator();
|
172
|
|
173
|
e.MoveNext();
|
174
|
e.MoveNext();
|
175
|
e.Dispose();
|
176
|
}
|
177
|
|
178
|
|
179
|
[Test]
|
180
|
[ExpectedException(typeof(CollectionModifiedException))]
|
181
|
public void MoveNextAfterUpdate()
|
182
|
{
|
183
|
hashset.Add(5);
|
184
|
hashset.Add(8);
|
185
|
hashset.Add(5);
|
186
|
|
187
|
SCG.IEnumerator<int> e = hashset.GetEnumerator();
|
188
|
|
189
|
e.MoveNext();
|
190
|
hashset.Add(99);
|
191
|
e.MoveNext();
|
192
|
}
|
193
|
|
194
|
|
195
|
[TearDown]
|
196
|
public void Dispose() { hashset = null; }
|
197
|
}
|
198
|
}
|
199
|
|
200
|
namespace CollectionOrSink
|
201
|
{
|
202
|
[TestFixture]
|
203
|
public class Formatting
|
204
|
{
|
205
|
ICollection<int> coll;
|
206
|
IFormatProvider rad16;
|
207
|
[SetUp]
|
208
|
public void Init() { coll = Factory.New<int>(); rad16 = new RadixFormatProvider(16); }
|
209
|
[TearDown]
|
210
|
public void Dispose() { coll = null; rad16 = null; }
|
211
|
[Test]
|
212
|
public void Format()
|
213
|
{
|
214
|
Assert.AreEqual("{ }", coll.ToString());
|
215
|
coll.AddAll<int>(new int[] { -4, 28, 129, 65530 });
|
216
|
Assert.AreEqual("{ 65530, -4, 28, 129 }", coll.ToString());
|
217
|
Assert.AreEqual("{ FFFA, -4, 1C, 81 }", coll.ToString(null, rad16));
|
218
|
Assert.AreEqual("{ 65530, -4, ... }", coll.ToString("L14", null));
|
219
|
Assert.AreEqual("{ FFFA, -4, ... }", coll.ToString("L14", rad16));
|
220
|
}
|
221
|
}
|
222
|
|
223
|
[TestFixture]
|
224
|
public class CollectionOrSink
|
225
|
{
|
226
|
private HashSet<int> hashset;
|
227
|
|
228
|
|
229
|
[SetUp]
|
230
|
public void Init() { hashset = new HashSet<int>(); }
|
231
|
|
232
|
[Test]
|
233
|
public void Choose()
|
234
|
{
|
235
|
hashset.Add(7);
|
236
|
Assert.AreEqual(7, hashset.Choose());
|
237
|
}
|
238
|
|
239
|
[Test]
|
240
|
[ExpectedException(typeof(NoSuchItemException))]
|
241
|
public void BadChoose()
|
242
|
{
|
243
|
hashset.Choose();
|
244
|
}
|
245
|
|
246
|
[Test]
|
247
|
public void CountEtAl()
|
248
|
{
|
249
|
Assert.AreEqual(0, hashset.Count);
|
250
|
Assert.IsTrue(hashset.IsEmpty);
|
251
|
Assert.IsFalse(hashset.AllowsDuplicates);
|
252
|
Assert.IsTrue(hashset.Add(0));
|
253
|
Assert.AreEqual(1, hashset.Count);
|
254
|
Assert.IsFalse(hashset.IsEmpty);
|
255
|
Assert.IsTrue(hashset.Add(5));
|
256
|
Assert.AreEqual(2, hashset.Count);
|
257
|
Assert.IsFalse(hashset.Add(5));
|
258
|
Assert.AreEqual(2, hashset.Count);
|
259
|
Assert.IsFalse(hashset.IsEmpty);
|
260
|
Assert.IsTrue(hashset.Add(8));
|
261
|
Assert.AreEqual(3, hashset.Count);
|
262
|
}
|
263
|
|
264
|
|
265
|
[Test]
|
266
|
public void AddAll()
|
267
|
{
|
268
|
hashset.Add(3); hashset.Add(4); hashset.Add(5);
|
269
|
|
270
|
HashSet<int> hashset2 = new HashSet<int>();
|
271
|
|
272
|
hashset2.AddAll(hashset);
|
273
|
Assert.IsTrue(IC.seteq(hashset2, 3, 4, 5));
|
274
|
hashset.Add(9);
|
275
|
hashset.AddAll(hashset2);
|
276
|
Assert.IsTrue(IC.seteq(hashset2, 3, 4, 5));
|
277
|
Assert.IsTrue(IC.seteq(hashset, 3, 4, 5, 9));
|
278
|
}
|
279
|
|
280
|
|
281
|
[TearDown]
|
282
|
public void Dispose() { hashset = null; }
|
283
|
}
|
284
|
|
285
|
[TestFixture]
|
286
|
public class FindPredicate
|
287
|
{
|
288
|
private HashSet<int> list;
|
289
|
Fun<int, bool> pred;
|
290
|
|
291
|
[SetUp]
|
292
|
public void Init()
|
293
|
{
|
294
|
list = new HashSet<int>(TenEqualityComparer.Default);
|
295
|
pred = delegate(int i) { return i % 5 == 0; };
|
296
|
}
|
297
|
|
298
|
[TearDown]
|
299
|
public void Dispose() { list = null; }
|
300
|
|
301
|
[Test]
|
302
|
public void Find()
|
303
|
{
|
304
|
int i;
|
305
|
Assert.IsFalse(list.Find(pred, out i));
|
306
|
list.AddAll<int>(new int[] { 4, 22, 67, 37 });
|
307
|
Assert.IsFalse(list.Find(pred, out i));
|
308
|
list.AddAll<int>(new int[] { 45, 122, 675, 137 });
|
309
|
Assert.IsTrue(list.Find(pred, out i));
|
310
|
Assert.AreEqual(45, i);
|
311
|
}
|
312
|
}
|
313
|
|
314
|
[TestFixture]
|
315
|
public class UniqueItems
|
316
|
{
|
317
|
private HashSet<int> list;
|
318
|
|
319
|
[SetUp]
|
320
|
public void Init() { list = new HashSet<int>(); }
|
321
|
|
322
|
[TearDown]
|
323
|
public void Dispose() { list = null; }
|
324
|
|
325
|
[Test]
|
326
|
public void Test()
|
327
|
{
|
328
|
Assert.IsTrue(IC.seteq(list.UniqueItems()));
|
329
|
Assert.IsTrue(IC.seteq(list.ItemMultiplicities()));
|
330
|
list.AddAll<int>(new int[] { 7, 9, 7 });
|
331
|
Assert.IsTrue(IC.seteq(list.UniqueItems(), 7, 9));
|
332
|
Assert.IsTrue(IC.seteq(list.ItemMultiplicities(), 7, 1, 9, 1));
|
333
|
}
|
334
|
}
|
335
|
|
336
|
[TestFixture]
|
337
|
public class ArrayTest
|
338
|
{
|
339
|
private HashSet<int> hashset;
|
340
|
|
341
|
int[] a;
|
342
|
|
343
|
|
344
|
[SetUp]
|
345
|
public void Init()
|
346
|
{
|
347
|
hashset = new HashSet<int>();
|
348
|
a = new int[10];
|
349
|
for (int i = 0; i < 10; i++)
|
350
|
a[i] = 1000 + i;
|
351
|
}
|
352
|
|
353
|
|
354
|
[TearDown]
|
355
|
public void Dispose() { hashset = null; }
|
356
|
|
357
|
|
358
|
private string aeq(int[] a, params int[] b)
|
359
|
{
|
360
|
if (a.Length != b.Length)
|
361
|
return "Lengths differ: " + a.Length + " != " + b.Length;
|
362
|
|
363
|
for (int i = 0; i < a.Length; i++)
|
364
|
if (a[i] != b[i])
|
365
|
return String.Format("{0}'th elements differ: {1} != {2}", i, a[i], b[i]);
|
366
|
|
367
|
return "Alles klar";
|
368
|
}
|
369
|
|
370
|
|
371
|
[Test]
|
372
|
public void ToArray()
|
373
|
{
|
374
|
Assert.AreEqual("Alles klar", aeq(hashset.ToArray()));
|
375
|
hashset.Add(7);
|
376
|
hashset.Add(3);
|
377
|
hashset.Add(10);
|
378
|
|
379
|
int[] r = hashset.ToArray();
|
380
|
|
381
|
Array.Sort(r);
|
382
|
Assert.AreEqual("Alles klar", aeq(r, 3, 7, 10));
|
383
|
}
|
384
|
|
385
|
|
386
|
[Test]
|
387
|
public void CopyTo()
|
388
|
{
|
389
|
//Note: for small ints the itemequalityComparer is the identity!
|
390
|
hashset.CopyTo(a, 1);
|
391
|
Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009));
|
392
|
hashset.Add(6);
|
393
|
hashset.CopyTo(a, 2);
|
394
|
Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 1003, 1004, 1005, 1006, 1007, 1008, 1009));
|
395
|
hashset.Add(4);
|
396
|
hashset.Add(9);
|
397
|
hashset.CopyTo(a, 4);
|
398
|
|
399
|
//TODO: make test independent on onterequalityComparer
|
400
|
Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 1003, 6, 9, 4, 1007, 1008, 1009));
|
401
|
hashset.Clear();
|
402
|
hashset.Add(7);
|
403
|
hashset.CopyTo(a, 9);
|
404
|
Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 1003, 6, 9, 4, 1007, 1008, 7));
|
405
|
}
|
406
|
|
407
|
|
408
|
[Test]
|
409
|
[ExpectedException(typeof(ArgumentOutOfRangeException))]
|
410
|
public void CopyToBad()
|
411
|
{
|
412
|
hashset.CopyTo(a, 11);
|
413
|
}
|
414
|
|
415
|
|
416
|
[Test]
|
417
|
[ExpectedException(typeof(ArgumentOutOfRangeException))]
|
418
|
public void CopyToBad2()
|
419
|
{
|
420
|
hashset.CopyTo(a, -1);
|
421
|
}
|
422
|
|
423
|
|
424
|
[Test]
|
425
|
[ExpectedException(typeof(ArgumentOutOfRangeException))]
|
426
|
public void CopyToTooFar()
|
427
|
{
|
428
|
hashset.Add(3);
|
429
|
hashset.Add(8);
|
430
|
hashset.CopyTo(a, 9);
|
431
|
}
|
432
|
}
|
433
|
}
|
434
|
|
435
|
namespace EditableCollection
|
436
|
{
|
437
|
[TestFixture]
|
438
|
public class Collision
|
439
|
{
|
440
|
HashSet<int> hashset;
|
441
|
|
442
|
|
443
|
[SetUp]
|
444
|
public void Init()
|
445
|
{
|
446
|
hashset = new HashSet<int>();
|
447
|
}
|
448
|
|
449
|
|
450
|
[Test]
|
451
|
public void SingleCollision()
|
452
|
{
|
453
|
hashset.Add(7);
|
454
|
hashset.Add(7 - 1503427877);
|
455
|
|
456
|
//foreach (int cell in hashset) Console.WriteLine("A: {0}", cell);
|
457
|
hashset.Remove(7);
|
458
|
Assert.IsTrue(hashset.Contains(7 - 1503427877));
|
459
|
}
|
460
|
|
461
|
|
462
|
[TearDown]
|
463
|
public void Dispose()
|
464
|
{
|
465
|
hashset = null;
|
466
|
}
|
467
|
}
|
468
|
|
469
|
|
470
|
|
471
|
[TestFixture]
|
472
|
public class Searching
|
473
|
{
|
474
|
private HashSet<int> hashset;
|
475
|
|
476
|
|
477
|
[SetUp]
|
478
|
public void Init() { hashset = new HashSet<int>(); }
|
479
|
|
480
|
|
481
|
[Test]
|
482
|
[ExpectedException(typeof(NullReferenceException))]
|
483
|
public void NullEqualityComparerinConstructor1()
|
484
|
{
|
485
|
new HashSet<int>(null);
|
486
|
}
|
487
|
|
488
|
[Test]
|
489
|
[ExpectedException(typeof(NullReferenceException))]
|
490
|
public void NullEqualityComparerinConstructor2()
|
491
|
{
|
492
|
new HashSet<int>(5, null);
|
493
|
}
|
494
|
|
495
|
[Test]
|
496
|
[ExpectedException(typeof(NullReferenceException))]
|
497
|
public void NullEqualityComparerinConstructor3()
|
498
|
{
|
499
|
new HashSet<int>(5, 0.5, null);
|
500
|
}
|
501
|
|
502
|
[Test]
|
503
|
public void Contains()
|
504
|
{
|
505
|
Assert.IsFalse(hashset.Contains(5));
|
506
|
hashset.Add(5);
|
507
|
Assert.IsTrue(hashset.Contains(5));
|
508
|
Assert.IsFalse(hashset.Contains(7));
|
509
|
hashset.Add(8);
|
510
|
hashset.Add(10);
|
511
|
Assert.IsTrue(hashset.Contains(5));
|
512
|
Assert.IsFalse(hashset.Contains(7));
|
513
|
Assert.IsTrue(hashset.Contains(8));
|
514
|
Assert.IsTrue(hashset.Contains(10));
|
515
|
hashset.Remove(8);
|
516
|
Assert.IsTrue(hashset.Contains(5));
|
517
|
Assert.IsFalse(hashset.Contains(7));
|
518
|
Assert.IsFalse(hashset.Contains(8));
|
519
|
Assert.IsTrue(hashset.Contains(10));
|
520
|
hashset.Add(0); hashset.Add(16); hashset.Add(32); hashset.Add(48); hashset.Add(64);
|
521
|
Assert.IsTrue(hashset.Contains(0));
|
522
|
Assert.IsTrue(hashset.Contains(16));
|
523
|
Assert.IsTrue(hashset.Contains(32));
|
524
|
Assert.IsTrue(hashset.Contains(48));
|
525
|
Assert.IsTrue(hashset.Contains(64));
|
526
|
Assert.IsTrue(hashset.Check());
|
527
|
|
528
|
int i = 0, j = i;
|
529
|
|
530
|
Assert.IsTrue(hashset.Find(ref i));
|
531
|
Assert.AreEqual(j, i);
|
532
|
j = i = 16;
|
533
|
Assert.IsTrue(hashset.Find(ref i));
|
534
|
Assert.AreEqual(j, i);
|
535
|
j = i = 32;
|
536
|
Assert.IsTrue(hashset.Find(ref i));
|
537
|
Assert.AreEqual(j, i);
|
538
|
j = i = 48;
|
539
|
Assert.IsTrue(hashset.Find(ref i));
|
540
|
Assert.AreEqual(j, i);
|
541
|
j = i = 64;
|
542
|
Assert.IsTrue(hashset.Find(ref i));
|
543
|
Assert.AreEqual(j, i);
|
544
|
j = i = 80;
|
545
|
Assert.IsFalse(hashset.Find(ref i));
|
546
|
Assert.AreEqual(j, i);
|
547
|
}
|
548
|
|
549
|
|
550
|
[Test]
|
551
|
public void Many()
|
552
|
{
|
553
|
int j = 7373;
|
554
|
int[] a = new int[j];
|
555
|
|
556
|
for (int i = 0; i < j; i++)
|
557
|
{
|
558
|
hashset.Add(3 * i + 1);
|
559
|
a[i] = 3 * i + 1;
|
560
|
}
|
561
|
|
562
|
Assert.IsTrue(IC.seteq(hashset, a));
|
563
|
}
|
564
|
|
565
|
|
566
|
[Test]
|
567
|
public void ContainsCount()
|
568
|
{
|
569
|
Assert.AreEqual(0, hashset.ContainsCount(5));
|
570
|
hashset.Add(5);
|
571
|
Assert.AreEqual(1, hashset.ContainsCount(5));
|
572
|
Assert.AreEqual(0, hashset.ContainsCount(7));
|
573
|
hashset.Add(8);
|
574
|
Assert.AreEqual(1, hashset.ContainsCount(5));
|
575
|
Assert.AreEqual(0, hashset.ContainsCount(7));
|
576
|
Assert.AreEqual(1, hashset.ContainsCount(8));
|
577
|
hashset.Add(5);
|
578
|
Assert.AreEqual(1, hashset.ContainsCount(5));
|
579
|
Assert.AreEqual(0, hashset.ContainsCount(7));
|
580
|
Assert.AreEqual(1, hashset.ContainsCount(8));
|
581
|
}
|
582
|
|
583
|
|
584
|
[Test]
|
585
|
public void RemoveAllCopies()
|
586
|
{
|
587
|
hashset.Add(5); hashset.Add(7); hashset.Add(5);
|
588
|
Assert.AreEqual(1, hashset.ContainsCount(5));
|
589
|
Assert.AreEqual(1, hashset.ContainsCount(7));
|
590
|
hashset.RemoveAllCopies(5);
|
591
|
Assert.AreEqual(0, hashset.ContainsCount(5));
|
592
|
Assert.AreEqual(1, hashset.ContainsCount(7));
|
593
|
hashset.Add(5); hashset.Add(8); hashset.Add(5);
|
594
|
hashset.RemoveAllCopies(8);
|
595
|
Assert.IsTrue(IC.eq(hashset, 7, 5));
|
596
|
}
|
597
|
|
598
|
|
599
|
[Test]
|
600
|
public void ContainsAll()
|
601
|
{
|
602
|
HashSet<int> list2 = new HashSet<int>();
|
603
|
|
604
|
Assert.IsTrue(hashset.ContainsAll(list2));
|
605
|
list2.Add(4);
|
606
|
Assert.IsFalse(hashset.ContainsAll(list2));
|
607
|
hashset.Add(4);
|
608
|
Assert.IsTrue(hashset.ContainsAll(list2));
|
609
|
hashset.Add(5);
|
610
|
Assert.IsTrue(hashset.ContainsAll(list2));
|
611
|
list2.Add(20);
|
612
|
Assert.IsFalse(hashset.ContainsAll(list2));
|
613
|
hashset.Add(20);
|
614
|
Assert.IsTrue(hashset.ContainsAll(list2));
|
615
|
}
|
616
|
|
617
|
|
618
|
[Test]
|
619
|
public void RetainAll()
|
620
|
{
|
621
|
HashSet<int> list2 = new HashSet<int>();
|
622
|
|
623
|
hashset.Add(4); hashset.Add(5); hashset.Add(6);
|
624
|
list2.Add(5); list2.Add(4); list2.Add(7);
|
625
|
hashset.RetainAll(list2);
|
626
|
Assert.IsTrue(IC.seteq(hashset, 4, 5));
|
627
|
hashset.Add(6);
|
628
|
list2.Clear();
|
629
|
list2.Add(7); list2.Add(8); list2.Add(9);
|
630
|
hashset.RetainAll(list2);
|
631
|
Assert.IsTrue(IC.seteq(hashset));
|
632
|
}
|
633
|
|
634
|
//Bug in RetainAll reported by Chris Fesler
|
635
|
//The result has different bitsc
|
636
|
[Test]
|
637
|
public void RetainAll2()
|
638
|
{
|
639
|
int LARGE_ARRAY_SIZE = 30;
|
640
|
int LARGE_ARRAY_MID = 15;
|
641
|
string[] _largeArrayOne = new string[LARGE_ARRAY_SIZE];
|
642
|
string[] _largeArrayTwo = new string[LARGE_ARRAY_SIZE];
|
643
|
for (int i = 0; i < LARGE_ARRAY_SIZE; i++)
|
644
|
{
|
645
|
_largeArrayOne[i] = "" + i;
|
646
|
_largeArrayTwo[i] = "" + (i + LARGE_ARRAY_MID);
|
647
|
}
|
648
|
|
649
|
HashSet<string> setOne = new HashSet<string>();
|
650
|
setOne.AddAll(_largeArrayOne);
|
651
|
|
652
|
HashSet<string> setTwo = new HashSet<string>();
|
653
|
setTwo.AddAll(_largeArrayTwo);
|
654
|
|
655
|
setOne.RetainAll(setTwo);
|
656
|
|
657
|
Assert.IsTrue(setOne.Check(),"setOne check fails");
|
658
|
|
659
|
for (int i = LARGE_ARRAY_MID; i < LARGE_ARRAY_SIZE; i++)
|
660
|
Assert.IsTrue(setOne.Contains(_largeArrayOne[i]), "missing " + i);
|
661
|
}
|
662
|
|
663
|
[Test]
|
664
|
public void RemoveAll()
|
665
|
{
|
666
|
HashSet<int> list2 = new HashSet<int>();
|
667
|
|
668
|
hashset.Add(4); hashset.Add(5); hashset.Add(6);
|
669
|
list2.Add(5); list2.Add(7); list2.Add(4);
|
670
|
hashset.RemoveAll(list2);
|
671
|
Assert.IsTrue(IC.eq(hashset, 6));
|
672
|
hashset.Add(5); hashset.Add(4);
|
673
|
list2.Clear();
|
674
|
list2.Add(6); list2.Add(5);
|
675
|
hashset.RemoveAll(list2);
|
676
|
Assert.IsTrue(IC.eq(hashset, 4));
|
677
|
list2.Clear();
|
678
|
list2.Add(7); list2.Add(8); list2.Add(9);
|
679
|
hashset.RemoveAll(list2);
|
680
|
Assert.IsTrue(IC.eq(hashset, 4));
|
681
|
}
|
682
|
|
683
|
|
684
|
[Test]
|
685
|
public void Remove()
|
686
|
{
|
687
|
hashset.Add(4); hashset.Add(4); hashset.Add(5); hashset.Add(4); hashset.Add(6);
|
688
|
Assert.IsFalse(hashset.Remove(2));
|
689
|
Assert.IsTrue(hashset.Remove(4));
|
690
|
Assert.IsTrue(IC.seteq(hashset, 5, 6));
|
691
|
hashset.Add(7);
|
692
|
hashset.Add(21); hashset.Add(37); hashset.Add(53); hashset.Add(69); hashset.Add(85);
|
693
|
Assert.IsTrue(hashset.Remove(5));
|
694
|
Assert.IsTrue(IC.seteq(hashset, 6, 7, 21, 37, 53, 69, 85));
|
695
|
Assert.IsFalse(hashset.Remove(165));
|
696
|
Assert.IsTrue(IC.seteq(hashset, 6, 7, 21, 37, 53, 69, 85));
|
697
|
Assert.IsTrue(hashset.Remove(53));
|
698
|
Assert.IsTrue(IC.seteq(hashset, 6, 7, 21, 37, 69, 85));
|
699
|
Assert.IsTrue(hashset.Remove(37));
|
700
|
Assert.IsTrue(IC.seteq(hashset, 6, 7, 21, 69, 85));
|
701
|
Assert.IsTrue(hashset.Remove(85));
|
702
|
Assert.IsTrue(IC.seteq(hashset, 6, 7, 21, 69));
|
703
|
}
|
704
|
|
705
|
|
706
|
[Test]
|
707
|
public void Clear()
|
708
|
{
|
709
|
hashset.Add(7); hashset.Add(7);
|
710
|
hashset.Clear();
|
711
|
Assert.IsTrue(hashset.IsEmpty);
|
712
|
}
|
713
|
|
714
|
|
715
|
[TearDown]
|
716
|
public void Dispose() { hashset = null; }
|
717
|
}
|
718
|
|
719
|
[TestFixture]
|
720
|
public class Combined
|
721
|
{
|
722
|
private ICollection<KeyValuePair<int, int>> lst;
|
723
|
|
724
|
|
725
|
[SetUp]
|
726
|
public void Init()
|
727
|
{
|
728
|
lst = new HashSet<KeyValuePair<int, int>>(new KeyValuePairEqualityComparer<int, int>());
|
729
|
for (int i = 0; i < 10; i++)
|
730
|
lst.Add(new KeyValuePair<int, int>(i, i + 30));
|
731
|
}
|
732
|
|
733
|
|
734
|
[TearDown]
|
735
|
public void Dispose() { lst = null; }
|
736
|
|
737
|
|
738
|
[Test]
|
739
|
public void Find()
|
740
|
{
|
741
|
KeyValuePair<int, int> p = new KeyValuePair<int, int>(3, 78);
|
742
|
|
743
|
Assert.IsTrue(lst.Find(ref p));
|
744
|
Assert.AreEqual(3, p.Key);
|
745
|
Assert.AreEqual(33, p.Value);
|
746
|
p = new KeyValuePair<int, int>(13, 78);
|
747
|
Assert.IsFalse(lst.Find(ref p));
|
748
|
}
|
749
|
|
750
|
|
751
|
[Test]
|
752
|
public void FindOrAdd()
|
753
|
{
|
754
|
KeyValuePair<int, int> p = new KeyValuePair<int, int>(3, 78);
|
755
|
KeyValuePair<int, int> q = new KeyValuePair<int, int>();
|
756
|
|
757
|
Assert.IsTrue(lst.FindOrAdd(ref p));
|
758
|
Assert.AreEqual(3, p.Key);
|
759
|
Assert.AreEqual(33, p.Value);
|
760
|
p = new KeyValuePair<int, int>(13, 79);
|
761
|
Assert.IsFalse(lst.FindOrAdd(ref p));
|
762
|
q.Key = 13;
|
763
|
Assert.IsTrue(lst.Find(ref q));
|
764
|
Assert.AreEqual(13, q.Key);
|
765
|
Assert.AreEqual(79, q.Value);
|
766
|
}
|
767
|
|
768
|
|
769
|
[Test]
|
770
|
public void Update()
|
771
|
{
|
772
|
KeyValuePair<int, int> p = new KeyValuePair<int, int>(3, 78);
|
773
|
KeyValuePair<int, int> q = new KeyValuePair<int, int>();
|
774
|
|
775
|
Assert.IsTrue(lst.Update(p));
|
776
|
q.Key = 3;
|
777
|
Assert.IsTrue(lst.Find(ref q));
|
778
|
Assert.AreEqual(3, q.Key);
|
779
|
Assert.AreEqual(78, q.Value);
|
780
|
p = new KeyValuePair<int, int>(13, 78);
|
781
|
Assert.IsFalse(lst.Update(p));
|
782
|
}
|
783
|
|
784
|
|
785
|
[Test]
|
786
|
public void UpdateOrAdd1()
|
787
|
{
|
788
|
KeyValuePair<int, int> p = new KeyValuePair<int, int>(3, 78);
|
789
|
KeyValuePair<int, int> q = new KeyValuePair<int, int>();
|
790
|
|
791
|
Assert.IsTrue(lst.UpdateOrAdd(p));
|
792
|
q.Key = 3;
|
793
|
Assert.IsTrue(lst.Find(ref q));
|
794
|
Assert.AreEqual(3, q.Key);
|
795
|
Assert.AreEqual(78, q.Value);
|
796
|
p = new KeyValuePair<int, int>(13, 79);
|
797
|
Assert.IsFalse(lst.UpdateOrAdd(p));
|
798
|
q.Key = 13;
|
799
|
Assert.IsTrue(lst.Find(ref q));
|
800
|
Assert.AreEqual(13, q.Key);
|
801
|
Assert.AreEqual(79, q.Value);
|
802
|
}
|
803
|
|
804
|
[Test]
|
805
|
public void UpdateOrAdd2()
|
806
|
{
|
807
|
ICollection<String> coll = new HashSet<String>();
|
808
|
// s1 and s2 are distinct objects but contain the same text:
|
809
|
String old, s1 = "abc", s2 = ("def" + s1).Substring(3);
|
810
|
Assert.IsFalse(coll.UpdateOrAdd(s1, out old));
|
811
|
Assert.AreEqual(null, old);
|
812
|
Assert.IsTrue(coll.UpdateOrAdd(s2, out old));
|
813
|
Assert.IsTrue(Object.ReferenceEquals(s1, old));
|
814
|
Assert.IsFalse(Object.ReferenceEquals(s2, old));
|
815
|
}
|
816
|
|
817
|
[Test]
|
818
|
public void RemoveWithReturn()
|
819
|
{
|
820
|
KeyValuePair<int, int> p = new KeyValuePair<int, int>(3, 78);
|
821
|
//KeyValuePair<int,int> q = new KeyValuePair<int,int>();
|
822
|
|
823
|
Assert.IsTrue(lst.Remove(p, out p));
|
824
|
Assert.AreEqual(3, p.Key);
|
825
|
Assert.AreEqual(33, p.Value);
|
826
|
p = new KeyValuePair<int, int>(13, 78);
|
827
|
Assert.IsFalse(lst.Remove(p, out p));
|
828
|
}
|
829
|
}
|
830
|
|
831
|
|
832
|
}
|
833
|
|
834
|
|
835
|
|
836
|
|
837
|
namespace HashingAndEquals
|
838
|
{
|
839
|
[TestFixture]
|
840
|
public class IEditableCollection
|
841
|
{
|
842
|
private ICollection<int> dit, dat, dut;
|
843
|
|
844
|
|
845
|
[SetUp]
|
846
|
public void Init()
|
847
|
{
|
848
|
dit = new HashSet<int>();
|
849
|
dat = new HashSet<int>();
|
850
|
dut = new HashSet<int>();
|
851
|
}
|
852
|
|
853
|
|
854
|
[Test]
|
855
|
public void EmptyEmpty()
|
856
|
{
|
857
|
Assert.IsTrue(dit.UnsequencedEquals(dat));
|
858
|
}
|
859
|
|
860
|
|
861
|
[Test]
|
862
|
public void EmptyNonEmpty()
|
863
|
{
|
864
|
dit.Add(3);
|
865
|
Assert.IsFalse(dit.UnsequencedEquals(dat));
|
866
|
Assert.IsFalse(dat.UnsequencedEquals(dit));
|
867
|
}
|
868
|
|
869
|
|
870
|
[Test]
|
871
|
public void HashVal()
|
872
|
{
|
873
|
Assert.AreEqual(CHC.unsequencedhashcode(), dit.GetUnsequencedHashCode());
|
874
|
dit.Add(3);
|
875
|
Assert.AreEqual(CHC.unsequencedhashcode(3), dit.GetUnsequencedHashCode());
|
876
|
dit.Add(7);
|
877
|
Assert.AreEqual(CHC.unsequencedhashcode(3, 7), dit.GetUnsequencedHashCode());
|
878
|
Assert.AreEqual(CHC.unsequencedhashcode(), dut.GetUnsequencedHashCode());
|
879
|
dut.Add(3);
|
880
|
Assert.AreEqual(CHC.unsequencedhashcode(3), dut.GetUnsequencedHashCode());
|
881
|
dut.Add(7);
|
882
|
Assert.AreEqual(CHC.unsequencedhashcode(7, 3), dut.GetUnsequencedHashCode());
|
883
|
}
|
884
|
|
885
|
|
886
|
[Test]
|
887
|
public void EqualHashButDifferent()
|
888
|
{
|
889
|
dit.Add(-1657792980); dit.Add(-1570288808);
|
890
|
dat.Add(1862883298); dat.Add(-272461342);
|
891
|
Assert.AreEqual(dit.GetUnsequencedHashCode(), dat.GetUnsequencedHashCode());
|
892
|
Assert.IsFalse(dit.UnsequencedEquals(dat));
|
893
|
}
|
894
|
|
895
|
|
896
|
[Test]
|
897
|
public void Normal()
|
898
|
{
|
899
|
dit.Add(3);
|
900
|
dit.Add(7);
|
901
|
dat.Add(3);
|
902
|
Assert.IsFalse(dit.UnsequencedEquals(dat));
|
903
|
Assert.IsFalse(dat.UnsequencedEquals(dit));
|
904
|
dat.Add(7);
|
905
|
Assert.IsTrue(dit.UnsequencedEquals(dat));
|
906
|
Assert.IsTrue(dat.UnsequencedEquals(dit));
|
907
|
}
|
908
|
|
909
|
|
910
|
[Test]
|
911
|
public void WrongOrder()
|
912
|
{
|
913
|
dit.Add(3);
|
914
|
dut.Add(3);
|
915
|
Assert.IsTrue(dit.UnsequencedEquals(dut));
|
916
|
Assert.IsTrue(dut.UnsequencedEquals(dit));
|
917
|
dit.Add(7);
|
918
|
dut.Add(7);
|
919
|
Assert.IsTrue(dit.UnsequencedEquals(dut));
|
920
|
Assert.IsTrue(dut.UnsequencedEquals(dit));
|
921
|
}
|
922
|
|
923
|
|
924
|
[Test]
|
925
|
public void Reflexive()
|
926
|
{
|
927
|
Assert.IsTrue(dit.UnsequencedEquals(dit));
|
928
|
dit.Add(3);
|
929
|
Assert.IsTrue(dit.UnsequencedEquals(dit));
|
930
|
dit.Add(7);
|
931
|
Assert.IsTrue(dit.UnsequencedEquals(dit));
|
932
|
}
|
933
|
|
934
|
|
935
|
[TearDown]
|
936
|
public void Dispose()
|
937
|
{
|
938
|
dit = null;
|
939
|
dat = null;
|
940
|
dut = null;
|
941
|
}
|
942
|
}
|
943
|
|
944
|
|
945
|
|
946
|
[TestFixture]
|
947
|
public class MultiLevelUnorderedOfUnOrdered
|
948
|
{
|
949
|
private ICollection<int> dit, dat, dut;
|
950
|
|
951
|
private ICollection<ICollection<int>> Dit, Dat, Dut;
|
952
|
|
953
|
|
954
|
[SetUp]
|
955
|
public void Init()
|
956
|
{
|
957
|
dit = new HashSet<int>();
|
958
|
dat = new HashSet<int>();
|
959
|
dut = new HashSet<int>();
|
960
|
dit.Add(2); dit.Add(1);
|
961
|
dat.Add(1); dat.Add(2);
|
962
|
dut.Add(3);
|
963
|
Dit = new HashSet<ICollection<int>>();
|
964
|
Dat = new HashSet<ICollection<int>>();
|
965
|
Dut = new HashSet<ICollection<int>>();
|
966
|
}
|
967
|
|
968
|
|
969
|
[Test]
|
970
|
public void Check()
|
971
|
{
|
972
|
Assert.IsTrue(dit.UnsequencedEquals(dat));
|
973
|
Assert.IsFalse(dit.UnsequencedEquals(dut));
|
974
|
}
|
975
|
|
976
|
|
977
|
[Test]
|
978
|
public void Multi()
|
979
|
{
|
980
|
Dit.Add(dit); Dit.Add(dut); Dit.Add(dit);
|
981
|
Dat.Add(dut); Dat.Add(dit); Dat.Add(dat);
|
982
|
Assert.IsTrue(Dit.UnsequencedEquals(Dat));
|
983
|
Assert.IsFalse(Dit.UnsequencedEquals(Dut));
|
984
|
}
|
985
|
|
986
|
|
987
|
[TearDown]
|
988
|
public void Dispose()
|
989
|
{
|
990
|
dit = dat = dut = null;
|
991
|
Dit = Dat = Dut = null;
|
992
|
}
|
993
|
}
|
994
|
|
995
|
|
996
|
|
997
|
[TestFixture]
|
998
|
public class MultiLevelOrderedOfUnOrdered
|
999
|
{
|
1000
|
private ICollection<int> dit, dat, dut;
|
1001
|
|
1002
|
private ISequenced<ICollection<int>> Dit, Dat, Dut;
|
1003
|
|
1004
|
|
1005
|
[SetUp]
|
1006
|
public void Init()
|
1007
|
{
|
1008
|
dit = new HashSet<int>();
|
1009
|
dat = new HashSet<int>();
|
1010
|
dut = new HashSet<int>();
|
1011
|
dit.Add(2); dit.Add(1);
|
1012
|
dat.Add(1); dat.Add(2);
|
1013
|
dut.Add(3);
|
1014
|
Dit = new LinkedList<ICollection<int>>();
|
1015
|
Dat = new LinkedList<ICollection<int>>();
|
1016
|
Dut = new LinkedList<ICollection<int>>();
|
1017
|
}
|
1018
|
|
1019
|
|
1020
|
[Test]
|
1021
|
public void Check()
|
1022
|
{
|
1023
|
Assert.IsTrue(dit.UnsequencedEquals(dat));
|
1024
|
Assert.IsFalse(dit.UnsequencedEquals(dut));
|
1025
|
}
|
1026
|
|
1027
|
|
1028
|
[Test]
|
1029
|
public void Multi()
|
1030
|
{
|
1031
|
Dit.Add(dit); Dit.Add(dut); Dit.Add(dit);
|
1032
|
Dat.Add(dut); Dat.Add(dit); Dat.Add(dat);
|
1033
|
Dut.Add(dit); Dut.Add(dut); Dut.Add(dat);
|
1034
|
Assert.IsFalse(Dit.SequencedEquals(Dat));
|
1035
|
Assert.IsTrue(Dit.SequencedEquals(Dut));
|
1036
|
}
|
1037
|
|
1038
|
|
1039
|
[TearDown]
|
1040
|
public void Dispose()
|
1041
|
{
|
1042
|
dit = dat = dut = null;
|
1043
|
Dit = Dat = Dut = null;
|
1044
|
}
|
1045
|
}
|
1046
|
|
1047
|
|
1048
|
|
1049
|
[TestFixture]
|
1050
|
public class MultiLevelUnOrderedOfOrdered
|
1051
|
{
|
1052
|
private ISequenced<int> dit, dat, dut, dot;
|
1053
|
|
1054
|
private ICollection<ISequenced<int>> Dit, Dat, Dut, Dot;
|
1055
|
|
1056
|
|
1057
|
[SetUp]
|
1058
|
public void Init()
|
1059
|
{
|
1060
|
dit = new LinkedList<int>();
|
1061
|
dat = new LinkedList<int>();
|
1062
|
dut = new LinkedList<int>();
|
1063
|
dot = new LinkedList<int>();
|
1064
|
dit.Add(2); dit.Add(1);
|
1065
|
dat.Add(1); dat.Add(2);
|
1066
|
dut.Add(3);
|
1067
|
dot.Add(2); dot.Add(1);
|
1068
|
Dit = new HashSet<ISequenced<int>>();
|
1069
|
Dat = new HashSet<ISequenced<int>>();
|
1070
|
Dut = new HashSet<ISequenced<int>>();
|
1071
|
Dot = new HashSet<ISequenced<int>>();
|
1072
|
}
|
1073
|
|
1074
|
|
1075
|
[Test]
|
1076
|
public void Check()
|
1077
|
{
|
1078
|
Assert.IsFalse(dit.SequencedEquals(dat));
|
1079
|
Assert.IsTrue(dit.SequencedEquals(dot));
|
1080
|
Assert.IsFalse(dit.SequencedEquals(dut));
|
1081
|
}
|
1082
|
|
1083
|
|
1084
|
[Test]
|
1085
|
public void Multi()
|
1086
|
{
|
1087
|
Dit.Add(dit); Dit.Add(dut);//Dit.Add(dit);
|
1088
|
Dat.Add(dut); Dat.Add(dit); Dat.Add(dat);
|
1089
|
Dut.Add(dot); Dut.Add(dut);//Dut.Add(dit);
|
1090
|
Dot.Add(dit); Dot.Add(dit); Dot.Add(dut);
|
1091
|
Assert.IsTrue(Dit.UnsequencedEquals(Dit));
|
1092
|
Assert.IsTrue(Dit.UnsequencedEquals(Dut));
|
1093
|
Assert.IsFalse(Dit.UnsequencedEquals(Dat));
|
1094
|
Assert.IsTrue(Dit.UnsequencedEquals(Dot));
|
1095
|
}
|
1096
|
|
1097
|
|
1098
|
[TearDown]
|
1099
|
public void Dispose()
|
1100
|
{
|
1101
|
dit = dat = dut = dot = null;
|
1102
|
Dit = Dat = Dut = Dot = null;
|
1103
|
}
|
1104
|
}
|
1105
|
}
|
1106
|
}
|