Project

General

Profile

root / branches / compiler / cSharp / ooasCompiler / src / libs / c5 / nunit / hashing / HashBagTests.cs @ 3

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.bag
27
{
28
  using CollectionOfInt = HashBag<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 HashBag<T>(); }
51
  }
52

    
53

    
54
  [TestFixture]
55
  public class Formatting
56
  {
57
    ICollection<int> coll;
58
    IFormatProvider rad16;
59
    [SetUp]
60
    public void Init() { coll = Factory.New<int>(); rad16 = new RadixFormatProvider(16); }
61
    [TearDown]
62
    public void Dispose() { coll = null; rad16 = null; }
63
    [Test]
64
    public void Format()
65
    {
66
      Assert.AreEqual("{{  }}", coll.ToString());
67
      coll.AddAll<int>(new int[] { -4, 28, 129, 65530, -4, 28 });
68
      Assert.AreEqual("{{ 65530(*1), -4(*2), 28(*2), 129(*1) }}", coll.ToString());
69
      Assert.AreEqual("{{ FFFA(*1), -4(*2), 1C(*2), 81(*1) }}", coll.ToString(null, rad16));
70
      Assert.AreEqual("{{ 65530(*1), -4(*2)... }}", coll.ToString("L18", null));
71
      Assert.AreEqual("{{ FFFA(*1), -4(*2)... }}", coll.ToString("L18", rad16));
72
    }
73
  }
74

    
75
  [TestFixture]
76
  public class Combined
77
  {
78
    private ICollection<KeyValuePair<int, int>> lst;
79

    
80

    
81
    [SetUp]
82
    public void Init()
83
    {
84
      lst = new HashBag<KeyValuePair<int, int>>(new KeyValuePairEqualityComparer<int, int>());
85
      for (int i = 0; i < 10; i++)
86
        lst.Add(new KeyValuePair<int, int>(i, i + 30));
87
    }
88

    
89

    
90
    [TearDown]
91
    public void Dispose() { lst = null; }
92

    
93

    
94
    [Test]
95
    public void Find()
96
    {
97
      KeyValuePair<int, int> p = new KeyValuePair<int, int>(3, 78);
98

    
99
      Assert.IsTrue(lst.Find(ref p));
100
      Assert.AreEqual(3, p.Key);
101
      Assert.AreEqual(33, p.Value);
102
      p = new KeyValuePair<int, int>(13, 78);
103
      Assert.IsFalse(lst.Find(ref p));
104
    }
105

    
106

    
107
    [Test]
108
    public void FindOrAdd()
109
    {
110
      KeyValuePair<int, int> p = new KeyValuePair<int, int>(3, 78);
111
      KeyValuePair<int, int> q = new KeyValuePair<int, int>();
112

    
113
      Assert.IsTrue(lst.FindOrAdd(ref p));
114
      Assert.AreEqual(3, p.Key);
115
      Assert.AreEqual(33, p.Value);
116
      p = new KeyValuePair<int, int>(13, 79);
117
      Assert.IsFalse(lst.FindOrAdd(ref p));
118
      q.Key = 13;
119
      Assert.IsTrue(lst.Find(ref q));
120
      Assert.AreEqual(13, q.Key);
121
      Assert.AreEqual(79, q.Value);
122
    }
123

    
124

    
125
    [Test]
126
    public void Update()
127
    {
128
      KeyValuePair<int, int> p = new KeyValuePair<int, int>(3, 78);
129
      KeyValuePair<int, int> q = new KeyValuePair<int, int>();
130

    
131
      Assert.IsTrue(lst.Update(p));
132
      q.Key = 3;
133
      Assert.IsTrue(lst.Find(ref q));
134
      Assert.AreEqual(3, q.Key);
135
      Assert.AreEqual(78, q.Value);
136
      p = new KeyValuePair<int, int>(13, 78);
137
      Assert.IsFalse(lst.Update(p));
138
    }
139

    
140

    
141
    [Test]
142
    public void UpdateOrAdd1()
143
    {
144
      KeyValuePair<int, int> p = new KeyValuePair<int, int>(3, 78);
145
      KeyValuePair<int, int> q = new KeyValuePair<int, int>();
146

    
147
      Assert.IsTrue(lst.UpdateOrAdd(p));
148
      q.Key = 3;
149
      Assert.IsTrue(lst.Find(ref q));
150
      Assert.AreEqual(3, q.Key);
151
      Assert.AreEqual(78, q.Value);
152
      p = new KeyValuePair<int, int>(13, 79);
153
      Assert.IsFalse(lst.UpdateOrAdd(p));
154
      q.Key = 13;
155
      Assert.IsTrue(lst.Find(ref q));
156
      Assert.AreEqual(13, q.Key);
157
      Assert.AreEqual(79, q.Value);
158
    }
159

    
160
    [Test]
161
    public void UpdateOrAdd2()
162
    {
163
        ICollection<String> coll = new HashBag<String>();
164
        // s1 and s2 are distinct objects but contain the same text:
165
        String old, s1 = "abc", s2 = ("def" + s1).Substring(3);
166
        Assert.IsFalse(coll.UpdateOrAdd(s1, out old));
167
        Assert.AreEqual(null, old);
168
        Assert.IsTrue(coll.UpdateOrAdd(s2, out old));
169
        Assert.IsTrue(Object.ReferenceEquals(s1, old));
170
        Assert.IsFalse(Object.ReferenceEquals(s2, old));
171
    }
172

    
173
    [Test]
174
    public void RemoveWithReturn()
175
    {
176
      KeyValuePair<int, int> p = new KeyValuePair<int, int>(3, 78);
177
      //KeyValuePair<int, int> q = new KeyValuePair<int, int>();
178

    
179
      Assert.IsTrue(lst.Remove(p, out p));
180
      Assert.AreEqual(3, p.Key);
181
      Assert.AreEqual(33, p.Value);
182
      p = new KeyValuePair<int, int>(13, 78);
183
      Assert.IsFalse(lst.Remove(p, out p));
184
    }
185
  }
186

    
187

    
188

    
189
  [TestFixture]
190
  public class CollectionOrSink
191
  {
192
    private HashBag<int> hashbag;
193

    
194

    
195
    [SetUp]
196
    public void Init() { hashbag = new HashBag<int>(); }
197

    
198
    [Test]
199
    [ExpectedException(typeof(NullReferenceException))]
200
    public void NullEqualityComparerinConstructor1()
201
    {
202
      new HashBag<int>(null);
203
    }
204

    
205
    [Test]
206
    [ExpectedException(typeof(NullReferenceException))]
207
    public void NullEqualityComparerinConstructor2()
208
    {
209
      new HashBag<int>(5, null);
210
    }
211

    
212
    [Test]
213
    [ExpectedException(typeof(NullReferenceException))]
214
    public void NullEqualityComparerinConstructor3()
215
    {
216
      new HashBag<int>(5, 0.5, null);
217
    }
218

    
219
    [Test]
220
    public void Choose()
221
    {
222
      hashbag.Add(7);
223
      Assert.AreEqual(7, hashbag.Choose());
224
    }
225

    
226
    [Test]
227
    [ExpectedException(typeof(NoSuchItemException))]
228
    public void BadChoose()
229
    {
230
      hashbag.Choose();
231
    }
232

    
233

    
234
    [Test]
235
    public void CountEtAl()
236
    {
237
      Assert.IsFalse(hashbag.IsReadOnly);
238
      // Assert.IsFalse(hashbag.SyncRoot == null);
239
      Assert.AreEqual(0, hashbag.Count);
240
      Assert.IsTrue(hashbag.IsEmpty);
241
      Assert.IsTrue(hashbag.AllowsDuplicates);
242
      Assert.IsTrue(hashbag.Add(0));
243
      Assert.AreEqual(1, hashbag.Count);
244
      Assert.IsFalse(hashbag.IsEmpty);
245
      Assert.IsTrue(hashbag.Add(5));
246
      Assert.AreEqual(2, hashbag.Count);
247
      Assert.IsTrue(hashbag.Add(5));
248
      Assert.AreEqual(3, hashbag.Count);
249
      Assert.IsFalse(hashbag.IsEmpty);
250
      Assert.IsTrue(hashbag.Add(8));
251
      Assert.AreEqual(4, hashbag.Count);
252
      Assert.AreEqual(2, hashbag.ContainsCount(5));
253
      Assert.AreEqual(1, hashbag.ContainsCount(8));
254
      Assert.AreEqual(1, hashbag.ContainsCount(0));
255
    }
256

    
257

    
258
    [Test]
259
    public void AddAll()
260
    {
261
      hashbag.Add(3); hashbag.Add(4); hashbag.Add(4); hashbag.Add(5); hashbag.Add(4);
262

    
263
      HashBag<int> hashbag2 = new HashBag<int>();
264

    
265
      hashbag2.AddAll(hashbag);
266
      Assert.IsTrue(IC.seteq(hashbag2, 3, 4, 4, 4, 5));
267
      hashbag.Add(9);
268
      hashbag.AddAll(hashbag2);
269
      Assert.IsTrue(IC.seteq(hashbag2, 3, 4, 4, 4, 5));
270
      Assert.IsTrue(IC.seteq(hashbag, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 9));
271
    }
272

    
273

    
274
    [Test]
275
    public void ContainsCount()
276
    {
277
      Assert.AreEqual(0, hashbag.ContainsCount(5));
278
      hashbag.Add(5);
279
      Assert.AreEqual(1, hashbag.ContainsCount(5));
280
      Assert.AreEqual(0, hashbag.ContainsCount(7));
281
      hashbag.Add(8);
282
      Assert.AreEqual(1, hashbag.ContainsCount(5));
283
      Assert.AreEqual(0, hashbag.ContainsCount(7));
284
      Assert.AreEqual(1, hashbag.ContainsCount(8));
285
      hashbag.Add(5);
286
      Assert.AreEqual(2, hashbag.ContainsCount(5));
287
      Assert.AreEqual(0, hashbag.ContainsCount(7));
288
      Assert.AreEqual(1, hashbag.ContainsCount(8));
289
    }
290

    
291

    
292
    [Test]
293
    public void RemoveAllCopies()
294
    {
295
      hashbag.Add(5); hashbag.Add(7); hashbag.Add(5);
296
      Assert.AreEqual(2, hashbag.ContainsCount(5));
297
      Assert.AreEqual(1, hashbag.ContainsCount(7));
298
      hashbag.RemoveAllCopies(5);
299
      Assert.AreEqual(0, hashbag.ContainsCount(5));
300
      Assert.AreEqual(1, hashbag.ContainsCount(7));
301
      hashbag.Add(5); hashbag.Add(8); hashbag.Add(5);
302
      hashbag.RemoveAllCopies(8);
303
      Assert.IsTrue(IC.eq(hashbag, 7, 5, 5));
304
    }
305

    
306

    
307
    [Test]
308
    public void ContainsAll()
309
    {
310
      HashBag<int> list2 = new HashBag<int>();
311

    
312
      Assert.IsTrue(hashbag.ContainsAll(list2));
313
      list2.Add(4);
314
      Assert.IsFalse(hashbag.ContainsAll(list2));
315
      hashbag.Add(4);
316
      Assert.IsTrue(hashbag.ContainsAll(list2));
317
      hashbag.Add(5);
318
      Assert.IsTrue(hashbag.ContainsAll(list2));
319
      list2.Add(20);
320
      Assert.IsFalse(hashbag.ContainsAll(list2));
321
      hashbag.Add(20);
322
      Assert.IsTrue(hashbag.ContainsAll(list2));
323
      list2.Add(4);
324
      Assert.IsFalse(hashbag.ContainsAll(list2));
325
      hashbag.Add(4);
326
      Assert.IsTrue(hashbag.ContainsAll(list2));
327
    }
328

    
329

    
330
    [Test]
331
    public void RetainAll()
332
    {
333
      HashBag<int> list2 = new HashBag<int>();
334

    
335
      hashbag.Add(4); hashbag.Add(5); hashbag.Add(4); hashbag.Add(6); hashbag.Add(4);
336
      list2.Add(5); list2.Add(4); list2.Add(7); list2.Add(4);
337
      hashbag.RetainAll(list2);
338
      Assert.IsTrue(IC.seteq(hashbag, 4, 4, 5));
339
      hashbag.Add(6);
340
      list2.Clear();
341
      list2.Add(7); list2.Add(8); list2.Add(9);
342
      hashbag.RetainAll(list2);
343
      Assert.IsTrue(IC.eq(hashbag));
344
    }
345

    
346

    
347
    [Test]
348
    public void RemoveAll()
349
    {
350
      HashBag<int> list2 = new HashBag<int>();
351

    
352
      hashbag.Add(4); hashbag.Add(5); hashbag.Add(6); hashbag.Add(4); hashbag.Add(5);
353
      list2.Add(5); list2.Add(4); list2.Add(7); list2.Add(4);
354
      hashbag.RemoveAll(list2);
355
      Assert.IsTrue(IC.seteq(hashbag, 5, 6));
356
      hashbag.Add(5); hashbag.Add(4);
357
      list2.Clear();
358
      list2.Add(6); list2.Add(5);
359
      hashbag.RemoveAll(list2);
360
      Assert.IsTrue(IC.seteq(hashbag, 4, 5));
361
      list2.Clear();
362
      list2.Add(7); list2.Add(8); list2.Add(9);
363
      hashbag.RemoveAll(list2);
364
      Assert.IsTrue(IC.seteq(hashbag, 4, 5));
365
    }
366

    
367

    
368
    [Test]
369
    public void Remove()
370
    {
371
      hashbag.Add(4); hashbag.Add(4); hashbag.Add(5); hashbag.Add(4); hashbag.Add(6);
372
      Assert.IsFalse(hashbag.Remove(2));
373
      Assert.IsTrue(hashbag.Remove(4));
374
      Assert.IsTrue(IC.seteq(hashbag, 4, 4, 5, 6));
375
      hashbag.Add(7);
376
      hashbag.Add(21); hashbag.Add(37); hashbag.Add(53); hashbag.Add(69); hashbag.Add(53); hashbag.Add(85);
377
      Assert.IsTrue(hashbag.Remove(5));
378
      Assert.IsTrue(IC.seteq(hashbag, 4, 4, 6, 7, 21, 37, 53, 53, 69, 85));
379
      Assert.IsFalse(hashbag.Remove(165));
380
      Assert.IsTrue(hashbag.Check());
381
      Assert.IsTrue(IC.seteq(hashbag, 4, 4, 6, 7, 21, 37, 53, 53, 69, 85));
382
      Assert.IsTrue(hashbag.Remove(53));
383
      Assert.IsTrue(IC.seteq(hashbag, 4, 4, 6, 7, 21, 37, 53, 69, 85));
384
      Assert.IsTrue(hashbag.Remove(37));
385
      Assert.IsTrue(IC.seteq(hashbag, 4, 4, 6, 7, 21, 53, 69, 85));
386
      Assert.IsTrue(hashbag.Remove(85));
387
      Assert.IsTrue(IC.seteq(hashbag, 4, 4, 6, 7, 21, 53, 69));
388
    }
389

    
390

    
391
    [TearDown]
392
    public void Dispose() { hashbag = null; }
393
  }
394

    
395
  [TestFixture]
396
  public class FindPredicate
397
  {
398
    private HashBag<int> list;
399
    Fun<int, bool> pred;
400

    
401
    [SetUp]
402
    public void Init()
403
    {
404
      list = new HashBag<int>(TenEqualityComparer.Default);
405
      pred = delegate(int i) { return i % 5 == 0; };
406
    }
407

    
408
    [TearDown]
409
    public void Dispose() { list = null; }
410

    
411
    [Test]
412
    public void Find()
413
    {
414
      int i;
415
      Assert.IsFalse(list.Find(pred, out i));
416
      list.AddAll<int>(new int[] { 4, 22, 67, 37 });
417
      Assert.IsFalse(list.Find(pred, out i));
418
      list.AddAll<int>(new int[] { 45, 122, 675, 137 });
419
      Assert.IsTrue(list.Find(pred, out i));
420
      Assert.AreEqual(45, i);
421
    }
422

    
423
  }
424

    
425
  [TestFixture]
426
  public class UniqueItems
427
  {
428
    private HashBag<int> list;
429

    
430
    [SetUp]
431
    public void Init() { list = new HashBag<int>(); }
432

    
433
    [TearDown]
434
    public void Dispose() { list = null; }
435

    
436
    [Test]
437
    public void Test()
438
    {
439
      Assert.IsTrue(IC.seteq(list.UniqueItems()));
440
      Assert.IsTrue(IC.seteq(list.ItemMultiplicities()));
441
      list.AddAll<int>(new int[] { 7, 9, 7 });
442
      Assert.IsTrue(IC.seteq(list.UniqueItems(), 7, 9));
443
      Assert.IsTrue(IC.seteq(list.ItemMultiplicities(), 7, 2, 9, 1));
444
    }
445
  }
446

    
447
  [TestFixture]
448
  public class ArrayTest
449
  {
450
    private HashBag<int> hashbag;
451

    
452
    int[] a;
453

    
454

    
455
    [SetUp]
456
    public void Init()
457
    {
458
      hashbag = new HashBag<int>();
459
      a = new int[10];
460
      for (int i = 0; i < 10; i++)
461
        a[i] = 1000 + i;
462
    }
463

    
464

    
465
    [TearDown]
466
    public void Dispose() { hashbag = null; }
467

    
468

    
469
    private string aeq(int[] a, params int[] b)
470
    {
471
      if (a.Length != b.Length)
472
        return "Lengths differ: " + a.Length + " != " + b.Length;
473

    
474
      for (int i = 0; i < a.Length; i++)
475
        if (a[i] != b[i])
476
          return String.Format("{0}'th elements differ: {1} != {2}", i, a[i], b[i]);
477

    
478
      return "Alles klar";
479
    }
480

    
481

    
482
    [Test]
483
    public void ToArray()
484
    {
485
      Assert.AreEqual("Alles klar", aeq(hashbag.ToArray()));
486
      hashbag.Add(7);
487
      hashbag.Add(3);
488
      hashbag.Add(10);
489
      hashbag.Add(3);
490

    
491
      int[] r = hashbag.ToArray();
492

    
493
      Array.Sort(r);
494
      Assert.AreEqual("Alles klar", aeq(r, 3, 3, 7, 10));
495
    }
496

    
497

    
498
    [Test]
499
    public void CopyTo()
500
    {
501
      //Note: for small ints the itemequalityComparer is the identity!
502
      hashbag.CopyTo(a, 1);
503
      Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009));
504
      hashbag.Add(6);
505
      hashbag.CopyTo(a, 2);
506
      Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 1003, 1004, 1005, 1006, 1007, 1008, 1009));
507
      hashbag.Add(4);
508
      hashbag.Add(6);
509
      hashbag.Add(9);
510
      hashbag.CopyTo(a, 4);
511

    
512
      //TODO: make independent of interequalityComparer
513
      Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 1003, 6, 6, 9, 4, 1008, 1009));
514
      hashbag.Clear();
515
      hashbag.Add(7);
516
      hashbag.CopyTo(a, 9);
517
      Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 1003, 6, 6, 9, 4, 1008, 7));
518
    }
519

    
520

    
521
    [Test]
522
    [ExpectedException(typeof(ArgumentOutOfRangeException))]
523
    public void CopyToBad()
524
    {
525
      hashbag.CopyTo(a, 11);
526
    }
527

    
528

    
529
    [Test]
530
    [ExpectedException(typeof(ArgumentOutOfRangeException))]
531
    public void CopyToBad2()
532
    {
533
      hashbag.CopyTo(a, -1);
534
    }
535

    
536

    
537
    [Test]
538
    [ExpectedException(typeof(ArgumentOutOfRangeException))]
539
    public void CopyToTooFar()
540
    {
541
      hashbag.Add(3);
542
      hashbag.Add(8);
543
      hashbag.CopyTo(a, 9);
544
    }
545
  }
546

    
547

    
548

    
549
  [TestFixture]
550
  public class HashingEquals
551
  {
552
    private ICollection<int> h1, h2;
553

    
554

    
555
    [SetUp]
556
    public void Init()
557
    {
558
      h1 = new HashBag<int>();
559
      h2 = new LinkedList<int>();
560
    }
561

    
562

    
563
    [TearDown]
564
    public void Dispose()
565
    {
566
      h1 = h2 = null;
567
    }
568

    
569

    
570
    [Test]
571
    public void Hashing()
572
    {
573
      Assert.AreEqual(h1.GetUnsequencedHashCode(), h2.GetUnsequencedHashCode());
574
      h1.Add(7);
575
      h2.Add(9);
576
      Assert.IsTrue(h1.GetUnsequencedHashCode() != h2.GetUnsequencedHashCode());
577
      h2.Add(7);
578
      h1.Add(9);
579
      Assert.IsTrue(h1.GetUnsequencedHashCode() == h2.GetUnsequencedHashCode());
580
    }
581

    
582

    
583
    [Test]
584
    public void Equals()
585
    {
586
      Assert.IsTrue(h1.UnsequencedEquals(h2));
587
//            Code 1550734257, Pair (3 x 1602896434, 2 x 1186320090) number 169185 matched oth
588
//er pair (3 x -1615223932, 2 x 1019546595)
589
      h1.Add(1602896434);
590
      h1.Add(1186320090);
591
      h1.Add(1602896434);
592
      h1.Add(1186320090);
593
      h1.Add(1602896434);
594
      h2.Add(-1615223932);
595
      h2.Add(1019546595);
596
      h2.Add(-1615223932);
597
      h2.Add(1019546595);
598
      h2.Add(-1615223932);
599
      Assert.IsTrue(h1.GetUnsequencedHashCode() == h2.GetUnsequencedHashCode());
600
      Assert.IsTrue(!h1.UnsequencedEquals(h2));
601
      h1.Clear();
602
      h2.Clear();
603
      h1.Add(1);
604
      h1.Add(2);
605
      h2.Add(2);
606
      h2.Add(1);
607
      Assert.IsTrue(h1.GetUnsequencedHashCode() == h2.GetUnsequencedHashCode());
608
      Assert.IsTrue(h1.UnsequencedEquals(h2));
609
    }
610
  }
611
}