Project

General

Profile

root / branches / compiler / cSharp / ooasCompiler / src / libs / c5 / nunit / linkedlists / HashedLinkedListTest.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

    
27

    
28
namespace C5UnitTests.linkedlists.hashed
29
{
30
  using CollectionOfInt = HashedLinkedList<int>;
31

    
32
  [TestFixture]
33
  public class GenericTesters
34
  {
35
    [Test]
36
    public void TestEvents()
37
    {
38
      Fun<CollectionOfInt> factory = delegate() { return new CollectionOfInt(TenEqualityComparer.Default); };
39
      new C5UnitTests.Templates.Events.ListTester<CollectionOfInt>().Test(factory);
40
    }
41

    
42
    [Test]
43
    public void Extensible()
44
    {
45
      C5UnitTests.Templates.Extensible.Clone.Tester<CollectionOfInt>();
46
      C5UnitTests.Templates.Extensible.Clone.ViewTester<CollectionOfInt>();
47
      C5UnitTests.Templates.Extensible.Serialization.Tester<CollectionOfInt>();
48
      C5UnitTests.Templates.Extensible.Serialization.ViewTester<CollectionOfInt>();
49
    }
50

    
51
    [Test]
52
    public void List()
53
    {
54
      C5UnitTests.Templates.List.Dispose.Tester<CollectionOfInt>();
55
      C5UnitTests.Templates.List.SCG_IList.Tester<CollectionOfInt>();
56
    }
57
  }
58

    
59
  static class Factory
60
  {
61
    public static ICollection<T> New<T>() { return new HashedLinkedList<T>(); }
62
  }
63

    
64

    
65
  namespace Enumerable
66
  {
67
    [TestFixture]
68
    public class Multiops
69
    {
70
      private HashedLinkedList<int> list;
71

    
72
      private Fun<int, bool> always, never, even;
73

    
74

    
75
      [SetUp]
76
      public void Init()
77
      {
78
        list = new HashedLinkedList<int>();
79
        always = delegate { return true; };
80
        never = delegate { return false; };
81
        even = delegate(int i) { return i % 2 == 0; };
82
      }
83

    
84

    
85
      [Test]
86
      public void All()
87
      {
88
        Assert.IsTrue(list.All(always));
89
        Assert.IsTrue(list.All(never));
90
        Assert.IsTrue(list.All(even));
91
        list.Add(8);
92
        Assert.IsTrue(list.All(always));
93
        Assert.IsFalse(list.All(never));
94
        Assert.IsTrue(list.All(even));
95
        list.Add(5);
96
        Assert.IsTrue(list.All(always));
97
        Assert.IsFalse(list.All(never));
98
        Assert.IsFalse(list.All(even));
99
      }
100

    
101

    
102
      [Test]
103
      public void Exists()
104
      {
105
        Assert.IsFalse(list.Exists(always));
106
        Assert.IsFalse(list.Exists(never));
107
        Assert.IsFalse(list.Exists(even));
108
        list.Add(5);
109
        Assert.IsTrue(list.Exists(always));
110
        Assert.IsFalse(list.Exists(never));
111
        Assert.IsFalse(list.Exists(even));
112
        list.Add(8);
113
        Assert.IsTrue(list.Exists(always));
114
        Assert.IsFalse(list.Exists(never));
115
        Assert.IsTrue(list.Exists(even));
116
      }
117

    
118

    
119
      [Test]
120
      public void Apply()
121
      {
122
        int sum = 0;
123
        Act<int> a = delegate(int i) { sum = i + 10 * sum; };
124

    
125
        list.Apply(a);
126
        Assert.AreEqual(0, sum);
127
        sum = 0;
128
        list.Add(5); list.Add(8); list.Add(7); list.Add(5);
129
        list.Apply(a);
130
        Assert.AreEqual(587, sum);
131
      }
132

    
133

    
134
      [TearDown]
135
      public void Dispose() { list = null; }
136
    }
137

    
138

    
139

    
140
    [TestFixture]
141
    public class GetEnumerator
142
    {
143
      private HashedLinkedList<int> list;
144

    
145

    
146
      [SetUp]
147
      public void Init() { list = new HashedLinkedList<int>(); }
148

    
149

    
150
      [Test]
151
      public void Empty()
152
      {
153
        SCG.IEnumerator<int> e = list.GetEnumerator();
154

    
155
        Assert.IsFalse(e.MoveNext());
156
      }
157

    
158

    
159
      [Test]
160
      public void Normal()
161
      {
162
        list.Add(5);
163
        list.Add(8);
164
        list.Add(5);
165
        list.Add(5);
166
        list.Add(10);
167
        list.Add(1);
168

    
169
        SCG.IEnumerator<int> e = list.GetEnumerator();
170

    
171
        Assert.IsTrue(e.MoveNext());
172
        Assert.AreEqual(5, e.Current);
173
        Assert.IsTrue(e.MoveNext());
174
        Assert.AreEqual(8, e.Current);
175
        Assert.IsTrue(e.MoveNext());
176
        Assert.AreEqual(10, e.Current);
177
        Assert.IsTrue(e.MoveNext());
178
        Assert.AreEqual(1, e.Current);
179
        Assert.IsFalse(e.MoveNext());
180
      }
181

    
182

    
183
      [Test]
184
      public void DoDispose()
185
      {
186
        list.Add(5);
187
        list.Add(8);
188
        list.Add(5);
189

    
190
        SCG.IEnumerator<int> e = list.GetEnumerator();
191

    
192
        e.MoveNext();
193
        e.MoveNext();
194
        e.Dispose();
195
      }
196

    
197

    
198
      [Test]
199
      [ExpectedException(typeof(CollectionModifiedException))]
200
      public void MoveNextAfterUpdate()
201
      {
202
        list.Add(5);
203
        list.Add(8);
204
        list.Add(5);
205

    
206
        SCG.IEnumerator<int> e = list.GetEnumerator();
207

    
208
        e.MoveNext();
209
        list.Add(99);
210
        e.MoveNext();
211
      }
212

    
213

    
214

    
215
      [TearDown]
216
      public void Dispose() { list = null; }
217
    }
218
  }
219

    
220
  namespace CollectionOrSink
221
  {
222
    [TestFixture]
223
    public class Formatting
224
    {
225
      ICollection<int> coll;
226
      IFormatProvider rad16;
227
      [SetUp]
228
      public void Init() { coll = Factory.New<int>(); rad16 = new RadixFormatProvider(16); }
229
      [TearDown]
230
      public void Dispose() { coll = null; rad16 = null; }
231
      [Test]
232
      public void Format()
233
      {
234
        Assert.AreEqual("[  ]", coll.ToString());
235
        coll.AddAll<int>(new int[] { -4, 28, 129, 65530 });
236
        Assert.AreEqual("[ -4, 28, 129, 65530 ]", coll.ToString());
237
        Assert.AreEqual("[ -4, 1C, 81, FFFA ]", coll.ToString(null, rad16));
238
        Assert.AreEqual("[ -4, 28, 129... ]", coll.ToString("L14", null));
239
        Assert.AreEqual("[ -4, 1C, 81... ]", coll.ToString("L14", rad16));
240
      }
241
    }
242

    
243
    [TestFixture]
244
    public class CollectionOrSink
245
    {
246
      private HashedLinkedList<int> list;
247

    
248

    
249
      [SetUp]
250
      public void Init() { list = new HashedLinkedList<int>(); }
251

    
252
      [Test]
253
      [ExpectedException(typeof(NullReferenceException))]
254
      public void NullEqualityComparerinConstructor1()
255
      {
256
        new HashedLinkedList<int>(null);
257
      }
258

    
259
      [Test]
260
      public void Choose()
261
      {
262
        list.Add(7);
263
        Assert.AreEqual(7, list.Choose());
264
      }
265

    
266
      [Test]
267
      [ExpectedException(typeof(NoSuchItemException))]
268
      public void BadChoose()
269
      {
270
        list.Choose();
271
      }
272

    
273

    
274
      [Test]
275
      public void CountEtAl()
276
      {
277
        Assert.AreEqual(0, list.Count);
278
        Assert.IsTrue(list.IsEmpty);
279
        Assert.IsFalse(list.AllowsDuplicates);
280
        Assert.IsTrue(list.Add(5));
281
        Assert.AreEqual(1, list.Count);
282
        Assert.IsFalse(list.IsEmpty);
283
        Assert.IsFalse(list.Add(5));
284
        Assert.AreEqual(1, list.Count);
285
        Assert.IsFalse(list.IsEmpty);
286
        Assert.IsTrue(list.Add(8));
287
        Assert.AreEqual(2, list.Count);
288
      }
289

    
290

    
291
      [Test]
292
      public void AddAll()
293
      {
294
        list.Add(3); list.Add(4); list.Add(5);
295

    
296
        HashedLinkedList<int> list2 = new HashedLinkedList<int>();
297

    
298
        list2.AddAll(list);
299
        Assert.IsTrue(IC.eq(list2, 3, 4, 5));
300
        list.AddAll(list2);
301
        Assert.IsTrue(IC.eq(list2, 3, 4, 5));
302
        Assert.IsTrue(IC.eq(list, 3, 4, 5));
303
      }
304

    
305

    
306
      [TearDown]
307
      public void Dispose() { list = null; }
308
    }
309

    
310
    [TestFixture]
311
    public class FindPredicate
312
    {
313
      private HashedLinkedList<int> list;
314
      Fun<int, bool> pred;
315

    
316
      [SetUp]
317
      public void Init()
318
      {
319
        list = new HashedLinkedList<int>(TenEqualityComparer.Default);
320
        pred = delegate(int i) { return i % 5 == 0; };
321
      }
322

    
323
      [TearDown]
324
      public void Dispose() { list = null; }
325

    
326
      [Test]
327
      public void Find()
328
      {
329
        int i;
330
        Assert.IsFalse(list.Find(pred, out i));
331
        list.AddAll<int>(new int[] { 4, 22, 67, 37 });
332
        Assert.IsFalse(list.Find(pred, out i));
333
        list.AddAll<int>(new int[] { 45, 122, 675, 137 });
334
        Assert.IsTrue(list.Find(pred, out i));
335
        Assert.AreEqual(45, i);
336
      }
337

    
338
      [Test]
339
      public void FindLast()
340
      {
341
        int i;
342
        Assert.IsFalse(list.FindLast(pred, out i));
343
        list.AddAll<int>(new int[] { 4, 22, 67, 37 });
344
        Assert.IsFalse(list.FindLast(pred, out i));
345
        list.AddAll<int>(new int[] { 45, 122, 675, 137 });
346
        Assert.IsTrue(list.FindLast(pred, out i));
347
        Assert.AreEqual(675, i);
348
      }
349

    
350
      [Test]
351
      public void FindIndex()
352
      {
353
        Assert.IsFalse(0 <= list.FindIndex(pred));
354
        list.AddAll<int>(new int[] { 4, 22, 67, 37 });
355
        Assert.IsFalse(0 <= list.FindIndex(pred));
356
        list.AddAll<int>(new int[] { 45, 122, 675, 137 });
357
        Assert.AreEqual(4, list.FindIndex(pred));
358
      }
359

    
360
      [Test]
361
      public void FindLastIndex()
362
      {
363
        Assert.IsFalse(0 <= list.FindLastIndex(pred));
364
        list.AddAll<int>(new int[] { 4, 22, 67, 37 });
365
        Assert.IsFalse(0 <= list.FindLastIndex(pred));
366
        list.AddAll<int>(new int[] { 45, 122, 675, 137 });
367
        Assert.AreEqual(6, list.FindLastIndex(pred));
368
      }
369
    }
370

    
371
    [TestFixture]
372
    public class UniqueItems
373
    {
374
      private HashedLinkedList<int> list;
375

    
376
      [SetUp]
377
      public void Init() { list = new HashedLinkedList<int>(); }
378

    
379
      [TearDown]
380
      public void Dispose() { list = null; }
381

    
382
      [Test]
383
      public void Test()
384
      {
385
        Assert.IsTrue(IC.seteq(list.UniqueItems()));
386
        Assert.IsTrue(IC.seteq(list.ItemMultiplicities()));
387
        list.AddAll<int>(new int[] { 7, 9, 7 });
388
        Assert.IsTrue(IC.seteq(list.UniqueItems(), 7, 9));
389
        Assert.IsTrue(IC.seteq(list.ItemMultiplicities(), 7, 1, 9, 1));
390
      }
391
    }
392

    
393
    [TestFixture]
394
    public class ArrayTest
395
    {
396
      private HashedLinkedList<int> list;
397

    
398
      int[] a;
399

    
400

    
401
      [SetUp]
402
      public void Init()
403
      {
404
        list = new HashedLinkedList<int>();
405
        a = new int[10];
406
        for (int i = 0; i < 10; i++)
407
          a[i] = 1000 + i;
408
      }
409

    
410

    
411
      [TearDown]
412
      public void Dispose() { list = null; }
413

    
414

    
415
      private string aeq(int[] a, params int[] b)
416
      {
417
        if (a.Length != b.Length)
418
          return "Lengths differ: " + a.Length + " != " + b.Length;
419

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

    
424
        return "Alles klar";
425
      }
426

    
427

    
428
      [Test]
429
      public void ToArray()
430
      {
431
        Assert.AreEqual("Alles klar", aeq(list.ToArray()));
432
        list.Add(7);
433
        list.Add(8);
434
        Assert.AreEqual("Alles klar", aeq(list.ToArray(), 7, 8));
435
      }
436

    
437

    
438
      [Test]
439
      public void CopyTo()
440
      {
441
        list.CopyTo(a, 1);
442
        Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009));
443
        list.Add(6);
444
        list.CopyTo(a, 2);
445
        Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 1003, 1004, 1005, 1006, 1007, 1008, 1009));
446
        list.Add(4);
447
        list.Add(5);
448
        list.Add(9);
449
        list.CopyTo(a, 4);
450
        Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 1003, 6, 4, 5, 9, 1008, 1009));
451
        list.Clear();
452
        list.Add(7);
453
        list.CopyTo(a, 9);
454
        Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 1003, 6, 4, 5, 9, 1008, 7));
455
      }
456

    
457

    
458
      [Test]
459
      [ExpectedException(typeof(ArgumentOutOfRangeException))]
460
      public void CopyToBad()
461
      {
462
        list.CopyTo(a, 11);
463
      }
464

    
465

    
466
      [Test]
467
      [ExpectedException(typeof(ArgumentOutOfRangeException))]
468
      public void CopyToBad2()
469
      {
470
        list.CopyTo(a, -1);
471
      }
472

    
473

    
474
      [Test]
475
      [ExpectedException(typeof(ArgumentOutOfRangeException))]
476
      public void CopyToTooFar()
477
      {
478
        list.Add(3);
479
        list.Add(4);
480
        list.CopyTo(a, 9);
481
      }
482
    }
483

    
484

    
485

    
486
    [TestFixture]
487
    public class Sync
488
    {
489
      private HashedLinkedList<int> list;
490

    
491

    
492
      [SetUp]
493
      public void Init()
494
      {
495
        list = new HashedLinkedList<int>();
496
      }
497

    
498

    
499
      [TearDown]
500
      public void Dispose() { list = null; }
501

    
502

    
503
      [Test]
504
      public void Get()
505
      {
506
        Assert.IsNotNull(((System.Collections.IList)list).SyncRoot);
507
      }
508
    }
509
  }
510

    
511

    
512

    
513

    
514
  namespace EditableCollection
515
  {
516
    [TestFixture]
517
    public class Searching
518
    {
519
      private HashedLinkedList<int> list;
520

    
521

    
522
      [SetUp]
523
      public void Init() { list = new HashedLinkedList<int>(); }
524

    
525

    
526
      [Test]
527
      public void Contains()
528
      {
529
        Assert.IsFalse(list.Contains(5));
530
        list.Add(5);
531
        Assert.IsTrue(list.Contains(5));
532
        Assert.IsFalse(list.Contains(7));
533
        list.Add(8);
534
        list.Add(10);
535
        Assert.IsTrue(list.Contains(5));
536
        Assert.IsFalse(list.Contains(7));
537
        Assert.IsTrue(list.Contains(8));
538
        Assert.IsTrue(list.Contains(10));
539
        list.Remove(8);
540
        Assert.IsTrue(list.Contains(5));
541
        Assert.IsFalse(list.Contains(7));
542
        Assert.IsFalse(list.Contains(8));
543
        Assert.IsTrue(list.Contains(10));
544
      }
545

    
546
      [Test]
547
      public void BadAdd()
548
      {
549
        Assert.IsTrue(list.Add(5));
550
        Assert.IsTrue(list.Add(8));
551
        Assert.IsFalse(list.Add(5));
552
      }
553

    
554

    
555
      [Test]
556
      public void ContainsCount()
557
      {
558
        Assert.AreEqual(0, list.ContainsCount(5));
559
        list.Add(5);
560
        Assert.AreEqual(1, list.ContainsCount(5));
561
        Assert.AreEqual(0, list.ContainsCount(7));
562
        list.Add(8);
563
        Assert.AreEqual(1, list.ContainsCount(5));
564
        Assert.AreEqual(0, list.ContainsCount(7));
565
        Assert.AreEqual(1, list.ContainsCount(8));
566
      }
567

    
568

    
569
      [Test]
570
      public void RemoveAllCopies()
571
      {
572
        list.Add(5); list.Add(7);
573
        Assert.AreEqual(1, list.ContainsCount(5));
574
        Assert.AreEqual(1, list.ContainsCount(7));
575
        list.RemoveAllCopies(5);
576
        Assert.IsTrue(list.Check());
577
        Assert.AreEqual(0, list.ContainsCount(5));
578
        Assert.AreEqual(1, list.ContainsCount(7));
579
        list.Add(5); list.Add(8);
580
        list.RemoveAllCopies(8);
581
        Assert.IsTrue(IC.eq(list, 7, 5));
582
      }
583

    
584

    
585
      [Test]
586
      public void FindAll()
587
      {
588
        Fun<int, bool> f = delegate(int i) { return i % 2 == 0; };
589

    
590
        Assert.IsTrue(list.FindAll(f).IsEmpty);
591
        list.Add(5); list.Add(8); list.Add(10);
592
        Assert.IsTrue(((HashedLinkedList<int>)list.FindAll(f)).Check());
593
        Assert.IsTrue(IC.eq(list.FindAll(f), 8, 10));
594
      }
595

    
596

    
597
      [Test]
598
      public void ContainsAll()
599
      {
600
        HashedLinkedList<int> list2 = new HashedLinkedList<int>();
601

    
602
        Assert.IsTrue(list.ContainsAll(list2));
603
        list2.Add(4);
604
        Assert.IsFalse(list.ContainsAll(list2));
605
        list.Add(4);
606
        Assert.IsTrue(list.ContainsAll(list2));
607
        list.Add(5);
608
        Assert.IsTrue(list.ContainsAll(list2));
609
      }
610

    
611

    
612
      [Test]
613
      public void RetainAll()
614
      {
615
        HashedLinkedList<int> list2 = new HashedLinkedList<int>();
616

    
617
        list.Add(4); list.Add(5); list.Add(6);
618
        list2.Add(5); list2.Add(4); list2.Add(7);
619
        list.RetainAll(list2);
620
        Assert.IsTrue(list.Check());
621
        Assert.IsTrue(IC.eq(list, 4, 5));
622
        list.Add(5); list.Add(4); list.Add(6);
623
        list2.Clear();
624
        list2.Add(5); list2.Add(5); list2.Add(6);
625
        list.RetainAll(list2);
626
        Assert.IsTrue(list.Check());
627
        Assert.IsTrue(IC.eq(list, 5, 6));
628
        list2.Clear();
629
        list2.Add(7); list2.Add(8); list2.Add(9);
630
        list.RetainAll(list2);
631
        Assert.IsTrue(list.Check());
632
        Assert.IsTrue(IC.eq(list));
633
      }
634

    
635

    
636
      [Test]
637
      public void RemoveAll()
638
      {
639
        HashedLinkedList<int> list2 = new HashedLinkedList<int>();
640

    
641
        list.Add(4); list.Add(5); list.Add(6);
642
        list2.Add(5); list2.Add(4); list2.Add(7);
643
        list.RemoveAll(list2);
644
        Assert.IsTrue(list.Check());
645
        Assert.IsTrue(IC.eq(list, 6));
646
        list.Add(5); list.Add(4); list.Add(6);
647
        list2.Clear();
648
        list2.Add(6); list2.Add(5);
649
        list.RemoveAll(list2);
650
        Assert.IsTrue(list.Check());
651
        Assert.IsTrue(IC.eq(list, 4));
652
        list2.Clear();
653
        list2.Add(7); list2.Add(8); list2.Add(9);
654
        list.RemoveAll(list2);
655
        Assert.IsTrue(list.Check());
656
        Assert.IsTrue(IC.eq(list, 4));
657
      }
658

    
659

    
660
      [Test]
661
      public void Remove()
662
      {
663
        list.Add(4); list.Add(5); list.Add(6);
664
        Assert.IsFalse(list.Remove(2));
665
        Assert.IsTrue(list.Check());
666
        Assert.IsTrue(list.Remove(4));
667
        Assert.IsTrue(list.Check());
668
        Assert.IsTrue(IC.eq(list, 5, 6));
669
        Assert.AreEqual(6, list.RemoveLast());
670
        Assert.IsTrue(list.Check());
671
        Assert.IsTrue(IC.eq(list, 5));
672
        list.Add(7);
673
        Assert.AreEqual(5, list.RemoveFirst());
674
        Assert.IsTrue(list.Check());
675
        Assert.IsTrue(IC.eq(list, 7));
676
      }
677

    
678

    
679
      [Test]
680
      public void Clear()
681
      {
682
        list.Add(7); list.Add(6);
683
        list.Clear();
684
        Assert.IsTrue(list.IsEmpty);
685
      }
686

    
687

    
688
      [TearDown]
689
      public void Dispose() { list = null; }
690
    }
691
  }
692

    
693

    
694

    
695

    
696
  namespace IIndexed
697
  {
698
    [TestFixture]
699
    public class Searching
700
    {
701
      private IIndexed<int> dit;
702

    
703

    
704
      [SetUp]
705
      public void Init()
706
      {
707
        dit = new HashedLinkedList<int>();
708
      }
709

    
710

    
711
      [Test]
712
      public void IndexOf()
713
      {
714
        Assert.AreEqual(~0, dit.IndexOf(6));
715
        dit.Add(7);
716
        Assert.AreEqual(~1, dit.IndexOf(6));
717
        Assert.AreEqual(~1, dit.LastIndexOf(6));
718
        Assert.AreEqual(0, dit.IndexOf(7));
719
        dit.Add(5); dit.Add(7); dit.Add(8); dit.Add(7);
720
        Assert.AreEqual(~3, dit.IndexOf(6));
721
        Assert.AreEqual(0, dit.IndexOf(7));
722
        Assert.AreEqual(0, dit.LastIndexOf(7));
723
        Assert.AreEqual(2, dit.IndexOf(8));
724
        Assert.AreEqual(1, dit.LastIndexOf(5));
725
      }
726

    
727

    
728
      [TearDown]
729
      public void Dispose()
730
      {
731
        dit = null;
732
      }
733
    }
734

    
735

    
736

    
737
    [TestFixture]
738
    public class Removing
739
    {
740
      private IIndexed<int> dit;
741

    
742

    
743
      [SetUp]
744
      public void Init()
745
      {
746
        dit = new HashedLinkedList<int>();
747
      }
748

    
749

    
750
      [Test]
751
      public void RemoveAt()
752
      {
753
        dit.Add(5); dit.Add(7); dit.Add(9); dit.Add(1); dit.Add(2);
754
        Assert.AreEqual(7, dit.RemoveAt(1));
755
        Assert.IsTrue(((HashedLinkedList<int>)dit).Check());
756
        Assert.IsTrue(IC.eq(dit, 5, 9, 1, 2));
757
        Assert.AreEqual(5, dit.RemoveAt(0));
758
        Assert.IsTrue(((HashedLinkedList<int>)dit).Check());
759
        Assert.IsTrue(IC.eq(dit, 9, 1, 2));
760
        Assert.AreEqual(2, dit.RemoveAt(2));
761
        Assert.IsTrue(((HashedLinkedList<int>)dit).Check());
762
        Assert.IsTrue(IC.eq(dit, 9, 1));
763
      }
764

    
765

    
766
      [Test]
767
      [ExpectedException(typeof(IndexOutOfRangeException))]
768
      public void RemoveAtBad0()
769
      {
770
        dit.RemoveAt(0);
771
      }
772

    
773

    
774
      [Test]
775
      [ExpectedException(typeof(IndexOutOfRangeException))]
776
      public void RemoveAtBadM1()
777
      {
778
        dit.RemoveAt(-1);
779
      }
780

    
781

    
782
      [Test]
783
      [ExpectedException(typeof(IndexOutOfRangeException))]
784
      public void RemoveAtBad1()
785
      {
786
        dit.Add(8);
787
        dit.RemoveAt(1);
788
      }
789

    
790

    
791
      [Test]
792
      public void RemoveInterval()
793
      {
794
        dit.RemoveInterval(0, 0);
795
        dit.Add(10); dit.Add(20); dit.Add(30); dit.Add(40); dit.Add(50); dit.Add(60);
796
        dit.RemoveInterval(3, 0);
797
        Assert.IsTrue(((HashedLinkedList<int>)dit).Check());
798
        Assert.IsTrue(IC.eq(dit, 10, 20, 30, 40, 50, 60));
799
        dit.RemoveInterval(3, 1);
800
        Assert.IsTrue(((HashedLinkedList<int>)dit).Check());
801
        Assert.IsTrue(IC.eq(dit, 10, 20, 30, 50, 60));
802
        dit.RemoveInterval(1, 3);
803
        Assert.IsTrue(((HashedLinkedList<int>)dit).Check());
804
        Assert.IsTrue(IC.eq(dit, 10, 60));
805
        dit.RemoveInterval(0, 2);
806
        Assert.IsTrue(((HashedLinkedList<int>)dit).Check());
807
        Assert.IsTrue(IC.eq(dit));
808
        dit.Add(10); dit.Add(20); dit.Add(30); dit.Add(40); dit.Add(50); dit.Add(60);
809
        dit.RemoveInterval(0, 2);
810
        Assert.IsTrue(((HashedLinkedList<int>)dit).Check());
811
        Assert.IsTrue(IC.eq(dit, 30, 40, 50, 60));
812
        dit.RemoveInterval(2, 2);
813
        Assert.IsTrue(((HashedLinkedList<int>)dit).Check());
814
        Assert.IsTrue(IC.eq(dit, 30, 40));
815
      }
816

    
817

    
818
      [TearDown]
819
      public void Dispose()
820
      {
821
        dit = null;
822
      }
823
    }
824
  }
825

    
826
  namespace IList_
827
  {
828
    [TestFixture]
829
    public class Searching
830
    {
831
      private IList<int> lst;
832

    
833

    
834
      [SetUp]
835
      public void Init() { lst = new HashedLinkedList<int>(); }
836

    
837

    
838
      [TearDown]
839
      public void Dispose() { lst = null; }
840

    
841

    
842
      [Test]
843
      [ExpectedException(typeof(NoSuchItemException))]
844
      public void FirstBad()
845
      {
846
        int f = lst.First;
847
      }
848

    
849

    
850
      [Test]
851
      [ExpectedException(typeof(NoSuchItemException))]
852
      public void LastBad()
853
      {
854
        int f = lst.Last;
855
      }
856

    
857

    
858
      [Test]
859
      public void FirstLast()
860
      {
861
        lst.Add(19);
862
        Assert.AreEqual(19, lst.First);
863
        Assert.AreEqual(19, lst.Last);
864
        lst.Add(34); lst.InsertFirst(12);
865
        Assert.AreEqual(12, lst.First);
866
        Assert.AreEqual(34, lst.Last);
867
      }
868

    
869

    
870
      [Test]
871
      public void This()
872
      {
873
        lst.Add(34);
874
        Assert.AreEqual(34, lst[0]);
875
        lst[0] = 56;
876
        Assert.AreEqual(56, lst.First);
877
        lst.Add(7); lst.Add(77); lst.Add(777); lst.Add(7777);
878
        lst[0] = 45; lst[2] = 78; lst[4] = 101;
879
        Assert.IsTrue(IC.eq(lst, 45, 7, 78, 777, 101));
880
      }
881

    
882
      [Test]
883
      public void ThisWithUpdates()
884
      {
885
        HashedLinkedList<KeyValuePair<int, int>> pairlist = new HashedLinkedList<KeyValuePair<int, int>>(new KeyValuePairEqualityComparer<int, int>());
886
        pairlist.Add(new KeyValuePair<int, int>(10, 50));
887
        pairlist.Add(new KeyValuePair<int, int>(11, 51));
888
        pairlist.Add(new KeyValuePair<int, int>(12, 52));
889
        pairlist.Add(new KeyValuePair<int, int>(13, 53));
890
        pairlist[2] = new KeyValuePair<int, int>(12, 102);
891
        Assert.IsTrue(pairlist.Check());
892
        Assert.AreEqual(new KeyValuePair<int, int>(12, 102), pairlist[2]);
893
        pairlist[2] = new KeyValuePair<int, int>(22, 202);
894
        Assert.IsTrue(pairlist.Check());
895
        Assert.AreEqual(new KeyValuePair<int, int>(22, 202), pairlist[2]);
896
        pairlist[1] = new KeyValuePair<int, int>(12, 303);
897
        Assert.IsTrue(pairlist.Check());
898
        Assert.AreEqual(new KeyValuePair<int, int>(12, 303), pairlist[1]);
899
        Assert.AreEqual(new KeyValuePair<int, int>(22, 202), pairlist[2]);
900
      }
901

    
902
      [Test]
903
      [ExpectedException(typeof(ArgumentException))]
904
      public void ThisWithUpdatesBad()
905
      {
906
        HashedLinkedList<KeyValuePair<int, int>> pairlist = new HashedLinkedList<KeyValuePair<int, int>>(new KeyValuePairEqualityComparer<int, int>());
907
        pairlist.Add(new KeyValuePair<int, int>(10, 50));
908
        pairlist.Add(new KeyValuePair<int, int>(11, 51));
909
        pairlist.Add(new KeyValuePair<int, int>(12, 52));
910
        pairlist.Add(new KeyValuePair<int, int>(13, 53));
911
        pairlist[2] = new KeyValuePair<int, int>(11, 102);
912
      }
913

    
914
      [Test]
915
      [ExpectedException(typeof(IndexOutOfRangeException))]
916
      public void ThisBadEmptyGet()
917
      {
918
        int f = lst[0];
919
      }
920

    
921

    
922
      [Test]
923
      [ExpectedException(typeof(IndexOutOfRangeException))]
924
      public void ThisBadLowGet()
925
      {
926
        lst.Add(7);
927

    
928
        int f = lst[-1];
929
      }
930

    
931

    
932
      [Test]
933
      [ExpectedException(typeof(IndexOutOfRangeException))]
934
      public void ThisBadHiGet()
935
      {
936
        lst.Add(6);
937

    
938
        int f = lst[1];
939
      }
940

    
941

    
942
      [Test]
943
      [ExpectedException(typeof(IndexOutOfRangeException))]
944
      public void ThisBadEmptySet()
945
      {
946
        lst[0] = 4;
947
      }
948

    
949

    
950
      [Test]
951
      [ExpectedException(typeof(IndexOutOfRangeException))]
952
      public void ThisBadLowSet()
953
      {
954
        lst.Add(7);
955
        lst[-1] = 9;
956
      }
957

    
958

    
959
      [Test]
960
      [ExpectedException(typeof(IndexOutOfRangeException))]
961
      public void ThisBadHiSet()
962
      {
963
        lst.Add(6);
964
        lst[1] = 11;
965
      }
966
    }
967

    
968

    
969
    [TestFixture]
970
    public class Combined
971
    {
972
      private IList<KeyValuePair<int, int>> lst;
973

    
974

    
975
      [SetUp]
976
      public void Init()
977
      {
978
        lst = new HashedLinkedList<KeyValuePair<int, int>>(new KeyValuePairEqualityComparer<int, int>());
979
        for (int i = 0; i < 10; i++)
980
          lst.Add(new KeyValuePair<int, int>(i, i + 30));
981
      }
982

    
983

    
984
      [TearDown]
985
      public void Dispose() { lst = null; }
986

    
987

    
988
      [Test]
989
      public void Find()
990
      {
991
        KeyValuePair<int, int> p = new KeyValuePair<int, int>(3, 78);
992

    
993
        Assert.IsTrue(lst.Find(ref p));
994
        Assert.AreEqual(3, p.Key);
995
        Assert.AreEqual(33, p.Value);
996
        p = new KeyValuePair<int, int>(13, 78);
997
        Assert.IsFalse(lst.Find(ref p));
998
      }
999

    
1000

    
1001
      [Test]
1002
      public void FindOrAdd()
1003
      {
1004
        KeyValuePair<int, int> p = new KeyValuePair<int, int>(3, 78);
1005

    
1006
        Assert.IsTrue(lst.FindOrAdd(ref p));
1007
        Assert.AreEqual(3, p.Key);
1008
        Assert.AreEqual(33, p.Value);
1009
        p = new KeyValuePair<int, int>(13, 79);
1010
        Assert.IsFalse(lst.FindOrAdd(ref p));
1011
        Assert.AreEqual(13, lst[10].Key);
1012
        Assert.AreEqual(79, lst[10].Value);
1013
      }
1014

    
1015

    
1016
      [Test]
1017
      public void Update()
1018
      {
1019
        KeyValuePair<int, int> p = new KeyValuePair<int, int>(3, 78);
1020

    
1021
        Assert.IsTrue(lst.Update(p));
1022
        Assert.AreEqual(3, lst[3].Key);
1023
        Assert.AreEqual(78, lst[3].Value);
1024
        p = new KeyValuePair<int, int>(13, 78);
1025
        Assert.IsFalse(lst.Update(p));
1026
      }
1027

    
1028

    
1029
      [Test]
1030
      public void UpdateOrAdd1()
1031
      {
1032
        KeyValuePair<int, int> p = new KeyValuePair<int, int>(3, 78);
1033

    
1034
        Assert.IsTrue(lst.UpdateOrAdd(p));
1035
        Assert.AreEqual(3, lst[3].Key);
1036
        Assert.AreEqual(78, lst[3].Value);
1037
        p = new KeyValuePair<int, int>(13, 79);
1038
        Assert.IsFalse(lst.UpdateOrAdd(p));
1039
        Assert.AreEqual(13, lst[10].Key);
1040
        Assert.AreEqual(79, lst[10].Value);
1041
      }
1042

    
1043
      [Test]
1044
      public void UpdateOrAdd2()
1045
      {
1046
          ICollection<String> coll = new HashedLinkedList<String>();
1047
          // s1 and s2 are distinct objects but contain the same text:
1048
          String old, s1 = "abc", s2 = ("def" + s1).Substring(3);
1049
          Assert.IsFalse(coll.UpdateOrAdd(s1, out old));
1050
          Assert.AreEqual(null, old);
1051
          Assert.IsTrue(coll.UpdateOrAdd(s2, out old));
1052
          Assert.IsTrue(Object.ReferenceEquals(s1, old));
1053
          Assert.IsFalse(Object.ReferenceEquals(s2, old));
1054
      }
1055

    
1056
      [Test]
1057
      public void RemoveWithReturn()
1058
      {
1059
        KeyValuePair<int, int> p = new KeyValuePair<int, int>(3, 78);
1060

    
1061
        Assert.IsTrue(lst.Remove(p, out p));
1062
        Assert.AreEqual(3, p.Key);
1063
        Assert.AreEqual(33, p.Value);
1064
        Assert.AreEqual(4, lst[3].Key);
1065
        Assert.AreEqual(34, lst[3].Value);
1066
        p = new KeyValuePair<int, int>(13, 78);
1067
        Assert.IsFalse(lst.Remove(p, out p));
1068
      }
1069
    }
1070

    
1071

    
1072
    [TestFixture]
1073
    public class Inserting
1074
    {
1075
      private IList<int> lst;
1076

    
1077

    
1078
      [SetUp]
1079
      public void Init() { lst = new HashedLinkedList<int>(); }
1080

    
1081

    
1082
      [TearDown]
1083
      public void Dispose() { lst = null; }
1084

    
1085

    
1086
      [Test]
1087
      public void Insert()
1088
      {
1089
        lst.Insert(0, 5);
1090
        Assert.IsTrue(IC.eq(lst, 5));
1091
        lst.Insert(0, 7);
1092
        Assert.IsTrue(IC.eq(lst, 7, 5));
1093
        lst.Insert(1, 4);
1094
        Assert.IsTrue(IC.eq(lst, 7, 4, 5));
1095
        lst.Insert(3, 2);
1096
        Assert.IsTrue(IC.eq(lst, 7, 4, 5, 2));
1097
      }
1098

    
1099
      [Test]
1100
      [ExpectedException(typeof(DuplicateNotAllowedException))]
1101
      public void InsertDuplicate()
1102
      {
1103
        lst.Insert(0, 5);
1104
        Assert.IsTrue(IC.eq(lst, 5));
1105
        lst.Insert(0, 7);
1106
        Assert.IsTrue(IC.eq(lst, 7, 5));
1107
        lst.Insert(1, 5);
1108
      }
1109

    
1110
      [Test]
1111
      public void InsertAllDuplicate1()
1112
      {
1113
        lst.Insert(0, 3);
1114
        Assert.IsTrue(IC.eq(lst, 3));
1115
        lst.Insert(0, 7);
1116
        Assert.IsTrue(IC.eq(lst, 7, 3));
1117
        try
1118
        {
1119
          lst.InsertAll<int>(1, new int[] { 1, 2, 3, 4 });
1120
        }
1121
        catch (DuplicateNotAllowedException)
1122
        {
1123
        }
1124
        Assert.IsTrue(lst.Check());
1125
        Assert.IsTrue(IC.eq(lst, 7, 1, 2, 3));
1126
      }
1127

    
1128
      [Test]
1129
      public void InsertAllDuplicate2()
1130
      {
1131
        lst.Insert(0, 3);
1132
        Assert.IsTrue(IC.eq(lst, 3));
1133
        lst.Insert(0, 7);
1134
        Assert.IsTrue(IC.eq(lst, 7, 3));
1135
        try
1136
        {
1137
          lst.InsertAll<int>(1, new int[] { 5, 6, 5, 8 });
1138
        }
1139
        catch (DuplicateNotAllowedException)
1140
        {
1141
        }
1142
        Assert.IsTrue(lst.Check());
1143
        Assert.IsTrue(IC.eq(lst, 7, 5, 6, 3));
1144
      }
1145

    
1146
      [Test]
1147
      [ExpectedException(typeof(IndexOutOfRangeException))]
1148
      public void BadInsertLow()
1149
      {
1150
        lst.Add(7);
1151
        lst.Insert(-1, 9);
1152
      }
1153

    
1154

    
1155
      [Test]
1156
      [ExpectedException(typeof(IndexOutOfRangeException))]
1157
      public void BadInsertHi()
1158
      {
1159
        lst.Add(6);
1160
        lst.Insert(2, 11);
1161
      }
1162

    
1163

    
1164
      [Test]
1165
      public void FIFO()
1166
      {
1167
        for (int i = 0; i < 7; i++)
1168
          lst.Add(2 * i);
1169

    
1170
        Assert.IsTrue(lst.FIFO);
1171
        Assert.AreEqual(0, lst.Remove());
1172
        Assert.AreEqual(2, lst.Remove());
1173
        lst.FIFO = false;
1174
        Assert.AreEqual(12, lst.Remove());
1175
        Assert.AreEqual(10, lst.Remove());
1176
        lst.FIFO = true;
1177
        Assert.AreEqual(4, lst.Remove());
1178
        Assert.AreEqual(6, lst.Remove());
1179
      }
1180

    
1181

    
1182
      [Test]
1183
      public void InsertFirstLast()
1184
      {
1185
        lst.InsertFirst(4);
1186
        lst.InsertLast(5);
1187
        lst.InsertFirst(14);
1188
        lst.InsertLast(15);
1189
        lst.InsertFirst(24);
1190
        lst.InsertLast(25);
1191
        lst.InsertFirst(34);
1192
        lst.InsertLast(55);
1193
        Assert.IsTrue(IC.eq(lst, 34, 24, 14, 4, 5, 15, 25, 55));
1194
      }
1195

    
1196

    
1197
      [Test]
1198
      public void InsertFirst()
1199
      {
1200
        lst.Add(2);
1201
        lst.Add(3);
1202
        lst.Add(4);
1203
        lst.Add(5);
1204
        lst.ViewOf(2).InsertFirst(7);
1205
        Assert.IsTrue(lst.Check());
1206
        Assert.IsTrue(IC.eq(lst, 7, 2, 3, 4, 5));
1207
        lst.ViewOf(3).InsertFirst(8);
1208
        Assert.IsTrue(lst.Check());
1209
        Assert.IsTrue(IC.eq(lst, 7, 2, 8, 3, 4, 5));
1210
        lst.ViewOf(5).InsertFirst(9);
1211
        Assert.IsTrue(lst.Check());
1212
        Assert.IsTrue(IC.eq(lst, 7, 2, 8, 3, 4, 9, 5));
1213
      }
1214

    
1215

    
1216
      [Test]
1217
      public void BadViewOf()
1218
      {
1219
        lst.Add(2);
1220
        lst.Add(3);
1221
        lst.Add(2);
1222
        lst.Add(5);
1223
        Assert.IsNull(lst.ViewOf(4));
1224
      }
1225

    
1226

    
1227
      [Test]
1228
      public void InsertAfter()
1229
      {
1230
        lst.Add(1);
1231
        lst.Add(2);
1232
        lst.Add(3);
1233
        lst.Add(4);
1234
        lst.Add(5);
1235
        lst.LastViewOf(2).InsertLast(7);
1236
        Assert.IsTrue(lst.Check());
1237
        Assert.IsTrue(IC.eq(lst, 1, 2, 7, 3, 4, 5));
1238
        lst.LastViewOf(1).InsertLast(8);
1239
        Assert.IsTrue(lst.Check());
1240
        Assert.IsTrue(IC.eq(lst, 1, 8, 2, 7, 3, 4, 5));
1241
        lst.LastViewOf(5).InsertLast(9);
1242
        Assert.IsTrue(lst.Check());
1243
        Assert.IsTrue(IC.eq(lst, 1, 8, 2, 7, 3, 4, 5, 9));
1244
      }
1245

    
1246

    
1247
      [Test]
1248
      public void BadInsertAfter()
1249
      {
1250
        lst.Add(2);
1251
        lst.Add(3);
1252
        lst.Add(6);
1253
        lst.Add(5);
1254
        Assert.IsNull(lst.ViewOf(4));
1255
      }
1256

    
1257

    
1258
      [Test]
1259
      public void InsertAll()
1260
      {
1261
        lst.Add(1);
1262
        lst.Add(2);
1263
        lst.Add(3);
1264
        lst.Add(4);
1265

    
1266
        IList<int> lst2 = new HashedLinkedList<int>();
1267

    
1268
        lst2.Add(7); lst2.Add(8); lst2.Add(9);
1269
        lst.InsertAll(0, lst2);
1270
        Assert.IsTrue(lst.Check());
1271
        Assert.IsTrue(IC.eq(lst, 7, 8, 9, 1, 2, 3, 4));
1272
        lst.RemoveAll(lst2);
1273
        lst.InsertAll(4, lst2);
1274
        Assert.IsTrue(lst.Check());
1275
        Assert.IsTrue(IC.eq(lst, 1, 2, 3, 4, 7, 8, 9));
1276
        lst.RemoveAll(lst2);
1277
        lst.InsertAll(2, lst2);
1278
        Assert.IsTrue(lst.Check());
1279
        Assert.IsTrue(IC.eq(lst, 1, 2, 7, 8, 9, 3, 4));
1280
      }
1281

    
1282
      [Test]
1283
      [ExpectedException(typeof(DuplicateNotAllowedException))]
1284
      public void InsertAllBad()
1285
      {
1286
        lst.Add(1);
1287
        lst.Add(2);
1288
        lst.Add(3);
1289
        lst.Add(4);
1290

    
1291
        IList<int> lst2 = new HashedLinkedList<int>();
1292

    
1293
        lst2.Add(5); lst2.Add(2); lst2.Add(9);
1294
        lst.InsertAll(0, lst2);
1295
      }
1296

    
1297

    
1298
      [Test]
1299
      public void Map()
1300
      {
1301
        Fun<int, string> m = delegate(int i) { return "<<" + i + ">>"; };
1302
        IList<string> r = lst.Map(m);
1303

    
1304
        Assert.IsTrue(((HashedLinkedList<string>)r).Check());
1305
        Assert.IsTrue(r.IsEmpty);
1306
        lst.Add(1);
1307
        lst.Add(2);
1308
        lst.Add(3);
1309
        lst.Add(4);
1310
        r = lst.Map(m);
1311
        Assert.IsTrue(((HashedLinkedList<string>)r).Check());
1312
        Assert.AreEqual(4, r.Count);
1313
        for (int i = 0; i < 4; i++)
1314
          Assert.AreEqual("<<" + (i + 1) + ">>", r[i]);
1315
      }
1316
      [Test]
1317
      [ExpectedException(typeof(CollectionModifiedException))]
1318
      public void BadMapper()
1319
      {
1320
        lst.Add(1);
1321
        lst.Add(2);
1322
        lst.Add(3);
1323
        Fun<int, bool> m = delegate(int i) { if (i == 2) lst.Add(7); return true; };
1324
        lst.Map(m);
1325
      }
1326

    
1327
      [Test]
1328
      [ExpectedException(typeof(CollectionModifiedException))]
1329
      public void ModifyingFindAll()
1330
      {
1331
        lst.Add(1);
1332
        lst.Add(2);
1333
        lst.Add(3);
1334
        Fun<int, bool> m = delegate(int i) { if (i == 2) lst.Add(7); return true; };
1335
        lst.FindAll(m);
1336
      }
1337

    
1338
      [Test]
1339
      [ExpectedException(typeof(CollectionModifiedException))]
1340
      public void BadMapperView()
1341
      {
1342
        lst = lst.View(0, 0);
1343
        lst.Add(1);
1344
        lst.Add(2);
1345
        lst.Add(3);
1346
        Fun<int, bool> m = delegate(int i) { if (i == 2) lst.Add(7); return true; };
1347
        lst.Map(m);
1348
      }
1349

    
1350
      [Test]
1351
      [ExpectedException(typeof(CollectionModifiedException))]
1352
      public void ModifyingFindAllView()
1353
      {
1354
        lst = lst.View(0, 0);
1355
        lst.Add(1);
1356
        lst.Add(2);
1357
        lst.Add(3);
1358
        Fun<int, bool> m = delegate(int i) { if (i == 2) lst.Add(7); return true; };
1359
        lst.FindAll(m);
1360
      }
1361

    
1362

    
1363
      [Test]
1364
      [ExpectedException(typeof(NoSuchItemException))]
1365
      public void BadRemove() { lst.Remove(); }
1366

    
1367
      [Test]
1368
      [ExpectedException(typeof(NoSuchItemException))]
1369
      public void BadRemoveFirst() { lst.RemoveFirst(); }
1370

    
1371
      [Test]
1372
      [ExpectedException(typeof(NoSuchItemException))]
1373
      public void BadRemoveLast() { lst.RemoveLast(); }
1374

    
1375

    
1376
      [Test]
1377
      public void RemoveFirstLast()
1378
      {
1379
        lst.Add(1);
1380
        lst.Add(2);
1381
        lst.Add(3);
1382
        lst.Add(4);
1383
        Assert.AreEqual(1, lst.RemoveFirst());
1384
        Assert.AreEqual(4, lst.RemoveLast());
1385
        Assert.AreEqual(2, lst.RemoveFirst());
1386
        Assert.AreEqual(3, lst.RemoveLast());
1387
        Assert.IsTrue(lst.IsEmpty);
1388
      }
1389

    
1390

    
1391
      [Test]
1392
      public void Reverse()
1393
      {
1394
        for (int i = 0; i < 10; i++)
1395
          lst.Add(i);
1396

    
1397
        lst.Reverse();
1398
        Assert.IsTrue(lst.Check());
1399
        Assert.IsTrue(IC.eq(lst, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0));
1400
        lst.View(0, 3).Reverse();
1401
        Assert.IsTrue(lst.Check());
1402
        Assert.IsTrue(IC.eq(lst, 7, 8, 9, 6, 5, 4, 3, 2, 1, 0));
1403
        lst.View(7, 0).Reverse();
1404
        Assert.IsTrue(lst.Check());
1405
        Assert.IsTrue(IC.eq(lst, 7, 8, 9, 6, 5, 4, 3, 2, 1, 0));
1406
        lst.View(7, 3).Reverse();
1407
        Assert.IsTrue(lst.Check());
1408
        Assert.IsTrue(IC.eq(lst, 7, 8, 9, 6, 5, 4, 3, 0, 1, 2));
1409
        lst.View(5, 1).Reverse();
1410
        Assert.IsTrue(lst.Check());
1411
        Assert.IsTrue(IC.eq(lst, 7, 8, 9, 6, 5, 4, 3, 0, 1, 2));
1412
      }
1413

    
1414

    
1415
      [Test]
1416
      [ExpectedException(typeof(ArgumentOutOfRangeException))]
1417
      public void BadReverse()
1418
      {
1419
        for (int i = 0; i < 10; i++)
1420
          lst.Add(i);
1421

    
1422
        lst.View(8, 3).Reverse();
1423
      }
1424
    }
1425

    
1426

    
1427

    
1428
    [TestFixture]
1429
    public class SortingTest
1430
    {
1431
      private IList<int> lst;
1432

    
1433

    
1434
      [SetUp]
1435
      public void Init() { lst = new HashedLinkedList<int>(); }
1436

    
1437

    
1438
      [TearDown]
1439
      public void Dispose() { lst = null; }
1440

    
1441

    
1442
      [Test]
1443
      public void Sort()
1444
      {
1445
        lst.Add(5); lst.Add(6); lst.Add(55); lst.Add(7); lst.Add(3);
1446
        Assert.IsFalse(lst.IsSorted(new IC()));
1447
        lst.Sort(new IC());
1448
        Assert.IsTrue(lst.Check(), "Check ");
1449
        Assert.IsTrue(lst.IsSorted());
1450
        Assert.IsTrue(lst.IsSorted(new IC()));
1451
        Assert.IsTrue(IC.eq(lst, 3, 5, 6, 7, 55));
1452
      }
1453
    }
1454

    
1455
    [TestFixture]
1456
    public class ShuffleTests
1457
    {
1458
      private IList<int> lst;
1459

    
1460

    
1461
      [SetUp]
1462
      public void Init() { lst = new HashedLinkedList<int>(); }
1463

    
1464

    
1465
      [TearDown]
1466
      public void Dispose() { lst = null; }
1467

    
1468

    
1469
      [Test]
1470
      public void Shuffle()
1471
      {
1472
        lst.Add(5); lst.Add(6); lst.Add(5); lst.Add(7); lst.Add(3);
1473
        for (int i = 0; i < 100; i++)
1474
        {
1475
          lst.Shuffle(new C5Random(i + 1));
1476
          Assert.IsTrue(lst.Check(), "Check " + i);
1477
          int[] lst2 = lst.ToArray();
1478
          Sorting.IntroSort<int>(lst2);
1479
          Assert.IsTrue(IC.eq(lst2, 3, 5, 6, 7), "Contents " + i);
1480
        }
1481
      }
1482
    }
1483

    
1484
    [TestFixture]
1485
    public class AddingThenRemoving
1486
    {
1487
        [Test]
1488
        public void AddingThenRemovingTest1()
1489
        {
1490
            // bug20070911:
1491
            HashedLinkedList<int> test = new HashedLinkedList<int>();
1492
            for (int i = 0; i < 33; i++)
1493
            {
1494
                test.Add(i);
1495
            } // for
1496

    
1497
            for (int i = 0; i < 33; i++)
1498
            {
1499
                test.Remove(i);
1500
            } // for
1501
            Assert.IsTrue(test.IsEmpty);
1502
            for (int count = 0; count < 520; count++)
1503
            {
1504
                HashedLinkedList<int> hll = new HashedLinkedList<int>();
1505
                for (int i = 1; i <= count; i++)
1506
                {
1507
                    hll.Add(i);
1508
                } 
1509
                Assert.AreEqual(count, hll.Count);
1510
                for (int i = 1; i <= count; i++)
1511
                {
1512
                    hll.Remove(i);
1513
                } 
1514
                Assert.IsTrue(hll.IsEmpty);
1515
            }
1516
        }
1517

    
1518
        [Test]
1519
        public void AddingThenRemovingTest2()
1520
        {
1521
            // bug20070911:
1522
            HashedLinkedList<int> test = new HashedLinkedList<int>();
1523
            for (int i = 0; i < 33; i++)
1524
            {
1525
                test.Add(i);
1526
            } // for
1527

    
1528
            for (int i = 32; i >= 0; i--)
1529
            {
1530
                test.Remove(i);
1531
            } // for
1532
            Assert.IsTrue(test.IsEmpty);
1533
            for (int count = 0; count < 520; count++)
1534
            {
1535
                HashedLinkedList<int> hll = new HashedLinkedList<int>();
1536
                for (int i = 1; i <= count; i++)
1537
                {
1538
                    hll.Add(i);
1539
                }
1540
                Assert.AreEqual(count, hll.Count);
1541
                for (int i = count; i >= 1; i--)
1542
                {
1543
                    hll.Remove(i);
1544
                }
1545
                Assert.IsTrue(hll.IsEmpty);
1546
            }
1547
        }
1548
    }
1549
  }
1550

    
1551
  namespace Range
1552
  {
1553
    [TestFixture]
1554
    public class Range
1555
    {
1556
      private IList<int> lst;
1557

    
1558

    
1559
      [SetUp]
1560
      public void Init() { lst = new HashedLinkedList<int>(); }
1561

    
1562

    
1563
      [TearDown]
1564
      public void Dispose() { lst = null; }
1565

    
1566

    
1567
      [Test]
1568
      public void GetRange()
1569
      {
1570
        //Assert.IsTrue(IC.eq(lst[0, 0)));
1571
        for (int i = 0; i < 10; i++) lst.Add(i);
1572

    
1573
        Assert.IsTrue(IC.eq(lst[0, 3], 0, 1, 2));
1574
        Assert.IsTrue(IC.eq(lst[3, 3], 3, 4, 5));
1575
        Assert.IsTrue(IC.eq(lst[6, 3], 6, 7, 8));
1576
        Assert.IsTrue(IC.eq(lst[6, 4], 6, 7, 8, 9));
1577
      }
1578

    
1579

    
1580
      [Test]
1581
      public void Backwards()
1582
      {
1583
        for (int i = 0; i < 10; i++) lst.Add(i);
1584

    
1585
        Assert.IsTrue(IC.eq(lst.Backwards(), 9, 8, 7, 6, 5, 4, 3, 2, 1, 0));
1586
        Assert.IsTrue(IC.eq(lst[0, 3].Backwards(), 2, 1, 0));
1587
        Assert.IsTrue(IC.eq(lst[3, 3].Backwards(), 5, 4, 3));
1588
        Assert.IsTrue(IC.eq(lst[6, 4].Backwards(), 9, 8, 7, 6));
1589
      }
1590

    
1591

    
1592
      [Test]
1593
      public void DirectionAndCount()
1594
      {
1595
        for (int i = 0; i < 10; i++) lst.Add(i);
1596

    
1597
        Assert.AreEqual(EnumerationDirection.Forwards, lst.Direction);
1598
        Assert.AreEqual(EnumerationDirection.Forwards, lst[3, 7].Direction);
1599
        Assert.AreEqual(EnumerationDirection.Backwards, lst[3, 7].Backwards().Direction);
1600
        Assert.AreEqual(EnumerationDirection.Backwards, lst.Backwards().Direction);
1601
        Assert.AreEqual(4, lst[3, 4].Count);
1602
        Assert.AreEqual(4, lst[3, 4].Backwards().Count);
1603
        Assert.AreEqual(10, lst.Backwards().Count);
1604
      }
1605

    
1606

    
1607
      [Test]
1608
      [ExpectedException(typeof(CollectionModifiedException))]
1609
      public void MoveNextAfterUpdate()
1610
      {
1611
        for (int i = 0; i < 10; i++) lst.Add(i);
1612

    
1613
        foreach (int i in lst)
1614
        {
1615
          lst.Add(45 + i);
1616
        }
1617
      }
1618
    }
1619
  }
1620

    
1621

    
1622

    
1623

    
1624
  namespace View
1625
  {
1626
    [TestFixture]
1627
    public class Simple
1628
    {
1629
      HashedLinkedList<int> list;
1630
      HashedLinkedList<int> view;
1631

    
1632

    
1633
      [SetUp]
1634
      public void Init()
1635
      {
1636
        list = new HashedLinkedList<int>();
1637
        list.Add(0); list.Add(1); list.Add(2); list.Add(3);
1638
        view = (HashedLinkedList<int>)list.View(1, 2);
1639
      }
1640

    
1641

    
1642
      [TearDown]
1643
      public void Dispose()
1644
      {
1645
        list = null;
1646
        view = null;
1647
      }
1648

    
1649

    
1650
      void check()
1651
      {
1652
        Assert.IsTrue(list.Check());
1653
        Assert.IsTrue(view.Check());
1654
      }
1655

    
1656
      [Test]
1657
      public void InsertPointer()
1658
      {
1659
        IList<int> view2 = list.View(2, 0);
1660
        list.Insert(view2, 7);
1661
        check();
1662
        list.Insert(list, 8);
1663
        check();
1664
        view.Insert(view2, 9);
1665
        check();
1666
        view.Insert(list.View(3, 2), 10);
1667
        check();
1668
        view.Insert(list.ViewOf(0), 11);
1669
        check();
1670
        Assert.IsTrue(IC.eq(list, 0, 11, 1, 9, 7, 2, 10, 3, 8));
1671
        Assert.IsTrue(IC.eq(view, 11, 1, 9, 7, 2, 10));
1672
      }
1673

    
1674
      [Test]
1675
      [ExpectedException(typeof(IndexOutOfRangeException))]
1676
      public void InsertPointerBad1()
1677
      {
1678
        view.Insert(list.View(0, 0), 7);
1679
      }
1680

    
1681
      [Test]
1682
      [ExpectedException(typeof(IndexOutOfRangeException))]
1683
      public void InsertPointerBad2()
1684
      {
1685
        view.Insert(list, 7);
1686
      }
1687

    
1688
      [Test]
1689
      [ExpectedException(typeof(IncompatibleViewException))]
1690
      public void InsertPointerBad3()
1691
      {
1692
        list.Insert(new ArrayList<int>(), 7);
1693
      }
1694

    
1695
      [Test]
1696
      [ExpectedException(typeof(IncompatibleViewException))]
1697
      public void InsertPointerBad4()
1698
      {
1699
        list.Insert(new ArrayList<int>().View(0, 0), 7);
1700
      }
1701

    
1702
      [Test]
1703
      public void Span()
1704
      {
1705
        IList<int> span = list.View(1, 0).Span(list.View(2, 0));
1706
        Assert.IsTrue(span.Check());
1707
        Assert.AreEqual(1, span.Offset);
1708
        Assert.AreEqual(1, span.Count);
1709
        span = list.View(0, 2).Span(list.View(2, 2));
1710
        Assert.IsTrue(span.Check());
1711
        Assert.AreEqual(0, span.Offset);
1712
        Assert.AreEqual(4, span.Count);
1713
        span = list.View(3, 1).Span(list.View(1, 1));
1714
        Assert.IsNull(span);
1715
      }
1716

    
1717
      [Test]
1718
      public void ViewOf()
1719
      {
1720
        for (int i = 0; i < 4; i++)
1721
          list.Add(i);
1722
        IList<int> v = view.ViewOf(2);
1723
        Assert.IsTrue(v.Check());
1724
        Assert.IsTrue(IC.eq(v, 2));
1725
        Assert.AreEqual(2, v.Offset);
1726
        v = list.ViewOf(2);
1727
        Assert.IsTrue(v.Check());
1728
        Assert.IsTrue(IC.eq(v, 2));
1729
        Assert.AreEqual(2, v.Offset);
1730
        v = list.LastViewOf(2);
1731
        Assert.IsTrue(v.Check());
1732
        Assert.IsTrue(IC.eq(v, 2));
1733
        Assert.AreEqual(2, v.Offset);
1734
      }
1735

    
1736
      [Test]
1737
      public void BadViewOf()
1738
      {
1739
        Assert.IsNull(view.ViewOf(5));
1740
        Assert.IsNull(view.LastViewOf(5));
1741
        Assert.IsNull(view.ViewOf(3));
1742
        Assert.IsNull(view.LastViewOf(3));
1743
        Assert.IsNull(view.ViewOf(0));
1744
        Assert.IsNull(view.LastViewOf(0));
1745
      }
1746

    
1747

    
1748
      [Test]
1749
      public void ArrayStuff()
1750
      {
1751
        Assert.IsTrue(IC.eq(view.ToArray(), 1, 2));
1752
        int[] extarray = new int[5];
1753
        view.CopyTo(extarray, 2);
1754
        Assert.IsTrue(IC.eq(extarray, 0, 0, 1, 2, 0));
1755
      }
1756

    
1757
      [Test]
1758
      public void Add()
1759
      {
1760
        check();
1761
        Assert.IsTrue(IC.eq(list, 0, 1, 2, 3));
1762
        Assert.IsTrue(IC.eq(view, 1, 2));
1763
        view.InsertFirst(10);
1764
        check();
1765
        Assert.IsTrue(IC.eq(list, 0, 10, 1, 2, 3));
1766
        Assert.IsTrue(IC.eq(view, 10, 1, 2));
1767
        view.Clear();
1768
        Assert.IsFalse(view.IsReadOnly);
1769
        Assert.IsFalse(view.AllowsDuplicates);
1770
        Assert.IsTrue(view.IsEmpty);
1771
        check();
1772
        Assert.IsTrue(IC.eq(list, 0, 3));
1773
        Assert.IsTrue(IC.eq(view));
1774
        view.Add(8);
1775
        Assert.IsFalse(view.IsEmpty);
1776
        Assert.IsFalse(view.AllowsDuplicates);
1777
        Assert.IsFalse(view.IsReadOnly);
1778
        check();
1779
        Assert.IsTrue(IC.eq(list, 0, 8, 3));
1780
        Assert.IsTrue(IC.eq(view, 8));
1781
        view.Add(12);
1782
        check();
1783
        Assert.IsTrue(IC.eq(list, 0, 8, 12, 3));
1784
        Assert.IsTrue(IC.eq(view, 8, 12));
1785
        view./*ViewOf(12).*/InsertLast(15);
1786
        check();
1787
        Assert.IsTrue(IC.eq(list, 0, 8, 12, 15, 3));
1788
        Assert.IsTrue(IC.eq(view, 8, 12, 15));
1789
        view.ViewOf(12).InsertFirst(18);
1790
        check();
1791
        Assert.IsTrue(IC.eq(list, 0, 8, 18, 12, 15, 3));
1792
        Assert.IsTrue(IC.eq(view, 8, 18, 12, 15));
1793

    
1794
        HashedLinkedList<int> lst2 = new HashedLinkedList<int>();
1795

    
1796
        lst2.Add(90); lst2.Add(92);
1797
        view.AddAll(lst2);
1798
        check();
1799
        Assert.IsTrue(IC.eq(list, 0, 8, 18, 12, 15, 90, 92, 3));
1800
        Assert.IsTrue(IC.eq(view, 8, 18, 12, 15, 90, 92));
1801
        view.InsertLast(66);
1802
        check();
1803
        Assert.IsTrue(IC.eq(list, 0, 8, 18, 12, 15, 90, 92, 66, 3));
1804
        Assert.IsTrue(IC.eq(view, 8, 18, 12, 15, 90, 92, 66));
1805
      }
1806

    
1807

    
1808
      [Test]
1809
      public void Bxxx()
1810
      {
1811
        Assert.IsTrue(IC.eq(view.Backwards(), 2, 1));
1812
        Assert.AreSame(list, view.Underlying);
1813
        Assert.IsNull(list.Underlying);
1814
        Assert.AreEqual(EnumerationDirection.Forwards, view.Direction);
1815
        Assert.AreEqual(EnumerationDirection.Backwards, view.Backwards().Direction);
1816
        Assert.AreEqual(0, list.Offset);
1817
        Assert.AreEqual(1, view.Offset);
1818
      }
1819

    
1820

    
1821
      [Test]
1822
      public void Contains()
1823
      {
1824
        Assert.IsTrue(view.Contains(1));
1825
        Assert.IsFalse(view.Contains(0));
1826

    
1827
        HashedLinkedList<int> lst2 = new HashedLinkedList<int>();
1828

    
1829
        lst2.Add(2);
1830
        Assert.IsTrue(view.ContainsAll(lst2));
1831
        lst2.Add(3);
1832
        Assert.IsFalse(view.ContainsAll(lst2));
1833
        Assert.AreEqual(Speed.Constant, view.ContainsSpeed);
1834
        Assert.AreEqual(2, view.Count);
1835
        view.Add(1);
1836
        Assert.AreEqual(1, view.ContainsCount(2));
1837
        Assert.AreEqual(1, view.ContainsCount(1));
1838
        Assert.AreEqual(2, view.Count);
1839
      }
1840

    
1841

    
1842
      [Test]
1843
      public void CreateView()
1844
      {
1845
        HashedLinkedList<int> view2 = (HashedLinkedList<int>)view.View(1, 0);
1846

    
1847
        Assert.AreSame(list, view2.Underlying);
1848
      }
1849

    
1850

    
1851
      [Test]
1852
      public void FIFO()
1853
      {
1854
        Assert.IsTrue(view.FIFO);
1855
        view.Add(23); view.Add(24); view.Add(25);
1856
        check();
1857
        Assert.IsTrue(IC.eq(view, 1, 2, 23, 24, 25));
1858
        Assert.AreEqual(1, view.Remove());
1859
        check();
1860
        Assert.IsTrue(IC.eq(view, 2, 23, 24, 25));
1861
        view.FIFO = false;
1862
        Assert.IsFalse(view.FIFO);
1863
        Assert.AreEqual(25, view.Remove());
1864
        check();
1865
        Assert.IsTrue(IC.eq(view, 2, 23, 24));
1866
      }
1867

    
1868

    
1869
      [Test]
1870
      public void MapEtc()
1871
      {
1872
        HashedLinkedList<double> dbl = (HashedLinkedList<double>)view.Map(new Fun<int, double>(delegate(int i) { return i / 10.0; }));
1873

    
1874
        Assert.IsTrue(dbl.Check());
1875
        Assert.AreEqual(0.1, dbl[0]);
1876
        Assert.AreEqual(0.2, dbl[1]);
1877
        for (int i = 0; i < 10; i++) view.Add(i);
1878

    
1879
        HashedLinkedList<int> list2 = (HashedLinkedList<int>)view.FindAll(new Fun<int, bool>(delegate(int i) { return i % 4 == 1; }));
1880

    
1881
        Assert.IsTrue(list2.Check());
1882
        Assert.IsTrue(IC.eq(list2, 1, 5, 9));
1883
      }
1884

    
1885

    
1886
      [Test]
1887
      public void FL()
1888
      {
1889
        Assert.AreEqual(1, view.First);
1890
        Assert.AreEqual(2, view.Last);
1891
      }
1892

    
1893

    
1894
      [Test]
1895
      public void Indexing()
1896
      {
1897
        list.Clear();
1898
        for (int i = 0; i < 20; i++) list.Add(i);
1899

    
1900
        view = (HashedLinkedList<int>)list.View(5, 7);
1901
        for (int i = 0; i < 7; i++) Assert.AreEqual(i + 5, view[i]);
1902

    
1903
        for (int i = 0; i < 7; i++) Assert.AreEqual(i, view.IndexOf(i + 5));
1904

    
1905
        for (int i = 0; i < 7; i++) Assert.AreEqual(i, view.LastIndexOf(i + 5));
1906
      }
1907

    
1908

    
1909
      [Test]
1910
      public void Insert()
1911
      {
1912
        view.Insert(0, 34);
1913
        view.Insert(1, 35);
1914
        view.Insert(4, 36);
1915
        Assert.IsTrue(view.Check());
1916
        Assert.IsTrue(IC.eq(view, 34, 35, 1, 2, 36));
1917

    
1918
        IList<int> list2 = new HashedLinkedList<int>();
1919

    
1920
        list2.Add(40); list2.Add(41);
1921
        view.InsertAll(3, list2);
1922
        Assert.IsTrue(view.Check());
1923
        Assert.IsTrue(IC.eq(view, 34, 35, 1, 40, 41, 2, 36));
1924
      }
1925

    
1926

    
1927
      [Test]
1928
      public void Sort()
1929
      {
1930
        view.Add(45); view.Add(47); view.Add(46); view.Add(48);
1931
        Assert.IsFalse(view.IsSorted(new IC()));
1932
        view.Sort(new IC());
1933
        check();
1934
        Assert.IsTrue(IC.eq(list, 0, 1, 2, 45, 46, 47, 48, 3));
1935
        Assert.IsTrue(IC.eq(view, 1, 2, 45, 46, 47, 48));
1936
      }
1937

    
1938

    
1939
      [Test]
1940
      public void Remove()
1941
      {
1942
        view.Add(1); view.Add(5); view.Add(3); view.Add(1); view.Add(3); view.Add(0);
1943
        Assert.IsTrue(IC.eq(view, 1, 2, 5));
1944
        Assert.IsTrue(view.Remove(1));
1945
        check();
1946
        Assert.IsTrue(IC.eq(view, 2, 5));
1947
        Assert.IsFalse(view.Remove(1));
1948
        check();
1949
        Assert.IsTrue(IC.eq(view, 2, 5));
1950
        Assert.IsFalse(view.Remove(0));
1951
        check();
1952
        Assert.IsTrue(IC.eq(view, 2, 5));
1953
        view.RemoveAllCopies(3);
1954
        check();
1955
        Assert.IsTrue(IC.eq(view, 2, 5));
1956
        Assert.IsTrue(IC.eq(list, 0, 2, 5, 3));
1957
        view.Add(1); view.Add(5); view.Add(3); view.Add(1); view.Add(3); view.Add(0);
1958
        Assert.IsTrue(IC.eq(view, 2, 5, 1));
1959

    
1960
        HashedLinkedList<int> l2 = new HashedLinkedList<int>();
1961

    
1962
        l2.Add(1); l2.Add(2); l2.Add(2); l2.Add(3); l2.Add(1);
1963
        view.RemoveAll(l2);
1964
        check();
1965
        Assert.IsTrue(IC.eq(view, 5));
1966
        view.RetainAll(l2);
1967
        check();
1968
        Assert.IsTrue(IC.eq(view));
1969
        view.Add(2); view.Add(4); view.Add(5);
1970
        Assert.AreEqual(2, view.RemoveAt(0));
1971
        Assert.AreEqual(5, view.RemoveAt(1));
1972
        Assert.AreEqual(4, view.RemoveAt(0));
1973
        check();
1974
        Assert.IsTrue(IC.eq(view));
1975
        view.Add(8); view.Add(6); view.Add(78);
1976
        Assert.AreEqual(8, view.RemoveFirst());
1977
        Assert.AreEqual(78, view.RemoveLast());
1978
        view.Add(2); view.Add(5); view.Add(3); view.Add(1);
1979
        view.RemoveInterval(1, 2);
1980
        check();
1981
        Assert.IsTrue(IC.eq(view, 6, 1));
1982
      }
1983

    
1984

    
1985
      [Test]
1986
      public void Reverse()
1987
      {
1988
        view.Clear();
1989
        for (int i = 0; i < 10; i++) view.Add(10 + i);
1990

    
1991
        view.View(3, 4).Reverse();
1992
        check();
1993
        Assert.IsTrue(IC.eq(view, 10, 11, 12, 16, 15, 14, 13, 17, 18, 19));
1994
        view.Reverse();
1995
        Assert.IsTrue(IC.eq(view, 19, 18, 17, 13, 14, 15, 16, 12, 11, 10));
1996
        Assert.IsTrue(IC.eq(list, 0, 19, 18, 17, 13, 14, 15, 16, 12, 11, 10, 3));
1997
      }
1998

    
1999

    
2000
      [Test]
2001
      public void Slide()
2002
      {
2003
        view.Slide(1);
2004
        check();
2005
        Assert.IsTrue(IC.eq(view, 2, 3));
2006
        view.Slide(-2);
2007
        check();
2008
        Assert.IsTrue(IC.eq(view, 0, 1));
2009
        view.Slide(0, 3);
2010
        check();
2011
        Assert.IsTrue(IC.eq(view, 0, 1, 2));
2012
        view.Slide(2, 1);
2013
        check();
2014
        Assert.IsTrue(IC.eq(view, 2));
2015
        view.Slide(-1, 0);
2016
        check();
2017
        Assert.IsTrue(IC.eq(view));
2018
        view.Add(28);
2019
        Assert.IsTrue(IC.eq(list, 0, 28, 1, 2, 3));
2020
      }
2021
      [Test]
2022
      public void Iterate()
2023
      {
2024
        list.Clear();
2025
        check();
2026
        view = null;
2027
        foreach (int i in new int[] { 2, 4, 8, 13, 6, 1, 10, 11 }) list.Add(i);
2028

    
2029
        view = (HashedLinkedList<int>)list.View(list.Count - 2, 2);
2030
        int j = 666;
2031
        while (true)
2032
        {
2033
          check();
2034
          //Console.WriteLine("View: {0}:  {1} --> {2}", view.Count, view.First, view.Last);
2035
          if ((view.Last - view.First) % 2 == 1)
2036
            view.Insert(1, j++);
2037
          check();
2038
          if (view.Offset == 0)
2039
            break;
2040
          else
2041
            view.Slide(-1, 2);
2042
        }
2043
        //foreach (int cell in list) Console.Write(" " + cell);
2044
        //Assert.IsTrue(list.Check());
2045
        Assert.IsTrue(IC.eq(list, 2, 4, 8, 668, 13, 6, 1, 667, 10, 666, 11));
2046
      }
2047

    
2048

    
2049
      [Test]
2050
      public void SyncRoot()
2051
      {
2052
        Assert.AreSame(((System.Collections.IList)view).SyncRoot, ((System.Collections.IList)list).SyncRoot);
2053
      }
2054
    }
2055

    
2056
    [TestFixture]
2057
    public class MultipleViews
2058
    {
2059
      IList<int> list;
2060
      IList<int>[][] views;
2061
      [SetUp]
2062
      public void Init()
2063
      {
2064
        list = new HashedLinkedList<int>();
2065
        for (int i = 0; i < 6; i++)
2066
          list.Add(i);
2067
        views = new IList<int>[7][];
2068
        for (int i = 0; i < 7; i++)
2069
        {
2070
          views[i] = new IList<int>[7 - i];
2071
          for (int j = 0; j < 7 - i; j++)
2072
            views[i][j] = list.View(i, j);
2073
        }
2074
      }
2075
      [TearDown]
2076
      public void Dispose()
2077
      {
2078
        list = null;
2079
        views = null;
2080
      }
2081
      [Test]
2082
      public void Insert()
2083
      {
2084
        Assert.IsTrue(list.Check(), "list check before insert");
2085
        list.Insert(3, 777);
2086
        Assert.IsTrue(list.Check(), "list check after insert");
2087
        for (int i = 0; i < 7; i++)
2088
          for (int j = 0; j < 7 - i; j++)
2089
          {
2090
            Assert.AreEqual(i < 3 || (i == 3 && j == 0) ? i : i + 1, views[i][j].Offset, "view[" + i + "][" + j + "] offset");
2091
            Assert.AreEqual(i < 3 && i + j > 3 ? j + 1 : j, views[i][j].Count, "view[" + i + "][" + j + "] count");
2092
          }
2093
      }
2094
      [Test]
2095
      public void RemoveAt()
2096
      {
2097
        Assert.IsTrue(list.Check(), "list check before remove");
2098
        list.RemoveAt(3);
2099
        Assert.IsTrue(list.Check(), "list check after remove");
2100
        for (int i = 0; i < 7; i++)
2101
          for (int j = 0; j < 7 - i; j++)
2102
          {
2103
            Assert.AreEqual(i <= 3 ? i : i - 1, views[i][j].Offset, "view[" + i + "][" + j + "] offset");
2104
            Assert.AreEqual(i <= 3 && i + j > 3 ? j - 1 : j, views[i][j].Count, "view[" + i + "][" + j + "] count");
2105
          }
2106
      }
2107

    
2108
      [Test]
2109
      public void RemoveInterval()
2110
      {
2111
        Assert.IsTrue(list.Check(), "list check before remove");
2112
        list.RemoveInterval(3, 2);
2113
        Assert.IsTrue(list.Check(), "list check after remove");
2114
        for (int i = 0; i < 7; i++)
2115
          for (int j = 0; j < 7 - i; j++)
2116
          {
2117
            Assert.AreEqual(i <= 3 ? i : i <= 5 ? 3 : i - 2, views[i][j].Offset, "view[" + i + "][" + j + "] offset");
2118
            Assert.AreEqual(j == 0 ? 0 : i <= 3 && i + j > 4 ? j - 2 : i > 4 || i + j <= 3 ? j : j - 1, views[i][j].Count, "view[" + i + "][" + j + "] count");
2119
          }
2120
      }
2121

    
2122
      [Test]
2123
      public void InsertAtEnd()
2124
      {
2125
        Assert.IsTrue(list.Check(), "list check before insert");
2126
        list.InsertLast(777);
2127
        Assert.IsTrue(list.Check(), "list check after insert");
2128
        for (int i = 0; i < 7; i++)
2129
          for (int j = 0; j < 7 - i; j++)
2130
          {
2131
            Assert.AreEqual(i, views[i][j].Offset, "view[" + i + "][" + j + "] offset");
2132
            Assert.AreEqual(j, views[i][j].Count, "view[" + i + "][" + j + "] count");
2133
          }
2134
      }
2135
      [Test]
2136
      public void RemoveAtEnd()
2137
      {
2138
        Assert.IsTrue(list.Check(), "list check before remove");
2139
        list.RemoveAt(5);
2140
        Assert.IsTrue(list.Check(), "list check after remove");
2141
        for (int i = 0; i < 7; i++)
2142
          for (int j = 0; j < 7 - i; j++)
2143
          {
2144
            Assert.AreEqual(i <= 5 ? i : i - 1, views[i][j].Offset, "view[" + i + "][" + j + "] offset");
2145
            Assert.AreEqual(i <= 5 && i + j > 5 ? j - 1 : j, views[i][j].Count, "view[" + i + "][" + j + "] count");
2146
          }
2147
      }
2148
      [Test]
2149
      public void InsertAtStart()
2150
      {
2151
        Assert.IsTrue(list.Check(), "list check before insert");
2152
        list.Insert(0, 777);
2153
        Assert.IsTrue(list.Check(), "list check after insert");
2154
        for (int i = 0; i < 7; i++)
2155
          for (int j = 0; j < 7 - i; j++)
2156
          {
2157
            Assert.AreEqual(i == 0 && j == 0 ? 0 : i + 1, views[i][j].Offset, "view[" + i + "][" + j + "] offset");
2158
            Assert.AreEqual(j, views[i][j].Count, "view[" + i + "][" + j + "] count");
2159
          }
2160
      }
2161
      [Test]
2162
      public void RemoveAtStart()
2163
      {
2164
        Assert.IsTrue(list.Check(), "list check before remove");
2165
        list.RemoveAt(0);
2166
        Assert.IsTrue(list.Check(), "list check after remove");
2167
        for (int i = 0; i < 7; i++)
2168
          for (int j = 0; j < 7 - i; j++)
2169
          {
2170
            Assert.AreEqual(i == 0 ? i : i - 1, views[i][j].Offset, "view[" + i + "][" + j + "] offset");
2171
            Assert.AreEqual(i == 0 && j > 0 ? j - 1 : j, views[i][j].Count, "view[" + i + "][" + j + "] count");
2172
          }
2173
      }
2174
      [Test]
2175
      public void Clear()
2176
      {
2177
        Assert.IsTrue(list.Check(), "list check before clear");
2178
        //for (int i = 0; i < 7; i++)
2179
        //for (int j = 0; j < 7 - i; j++)
2180
        //Console.WriteLine("// view[{0}][{1}] : {2}", i, j, ((HashedLinkedList<int>) views[i][j]).GetHashCode());
2181
        views[2][3].Clear();
2182
        Assert.IsTrue(list.Check(), "list check after clear");
2183
        for (int i = 0; i < 7; i++)
2184
          for (int j = 0; j < 7 - i; j++)
2185
          {
2186
            Assert.AreEqual(i < 2 ? i : i < 6 ? 2 : i - 3, views[i][j].Offset, "view[" + i + "][" + j + "] offset");
2187
            Assert.AreEqual(s(i, j), views[i][j].Count, "view[" + i + "][" + j + "] count");
2188
          }
2189
      }
2190

    
2191
      private int s(int i, int j)
2192
      {
2193
        if (j == 0) return 0;
2194
        int k = i + j - 1; //end
2195
        if (i > 4 || k <= 1) return j;
2196
        if (i >= 2) return k > 4 ? k - 4 : 0;
2197
        if (i <= 2) return k >= 4 ? j - 3 : 2 - i;
2198
        return -1;
2199
      }
2200
      [Test]
2201
      public void InsertAll()
2202
      {
2203
        IList<int> list2 = new HashedLinkedList<int>();
2204
        for (int i = 0; i < 5; i++) { list2.Add(100 + i); }
2205
        Assert.IsTrue(list.Check(), "list check before insertAll");
2206
        list.InsertAll(3, list2);
2207
        Assert.IsTrue(list.Check(), "list check after insertAll");
2208
        for (int i = 0; i < 7; i++)
2209
          for (int j = 0; j < 7 - i; j++)
2210
          {
2211
            Assert.AreEqual(i < 3 || (i == 3 && j == 0) ? i : i + 5, views[i][j].Offset, "view[" + i + "][" + j + "] offset");
2212
            Assert.AreEqual(i < 3 && i + j > 3 ? j + 5 : j, views[i][j].Count, "view[" + i + "][" + j + "] count");
2213
          }
2214
      }
2215

    
2216
      [Test]
2217
      public void AddAll()
2218
      {
2219
        IList<int> list2 = new HashedLinkedList<int>();
2220
        for (int i = 0; i < 5; i++) { list2.Add(100 + i); }
2221
        Assert.IsTrue(list.Check(), "list check before AddAll");
2222
        list.View(1, 2).AddAll(list2);
2223
        Assert.IsTrue(list.Check(), "list check after AddAll");
2224
        for (int i = 0; i < 7; i++)
2225
          for (int j = 0; j < 7 - i; j++)
2226
          {
2227
            Assert.AreEqual(i < 3 || (i == 3 && j == 0) ? i : i + 5, views[i][j].Offset, "view[" + i + "][" + j + "] offset");
2228
            Assert.AreEqual(i < 3 && i + j > 3 ? j + 5 : j, views[i][j].Count, "view[" + i + "][" + j + "] count");
2229
          }
2230
      }
2231

    
2232
      [Test]
2233
      public void Remove()
2234
      {
2235
        for (int i = 0; i < 7; i++)
2236
        {
2237
          for (int j = 0; j < 7 - i; j++)
2238
          {
2239
            list = new HashedLinkedList<int>();
2240
            for (int k = 0; k < 6; k++) list.Add(k);
2241
            HashedLinkedList<int> v = (HashedLinkedList<int>)list.View(i, j);
2242
            list.Remove(3);
2243
            Assert.IsTrue(list.Check(), "list check after Remove, i=" + i + ", j=" + j);
2244
          }
2245
        }
2246
      }
2247
      [Test]
2248
      public void RemoveAll1()
2249
      {
2250
        IList<int> list2 = new HashedLinkedList<int>();
2251
        list2.Add(1); list2.Add(3); list2.Add(4);
2252

    
2253
        for (int i = 0; i < 7; i++)
2254
        {
2255
          for (int j = 0; j < 7 - i; j++)
2256
          {
2257
            list = new HashedLinkedList<int>();
2258
            for (int k = 0; k < 6; k++) list.Add(k);
2259
            HashedLinkedList<int> v = (HashedLinkedList<int>)list.View(i, j);
2260
            list.RemoveAll(list2);
2261
            Assert.IsTrue(list.Check(), "list check after RemoveAll, i=" + i + ", j=" + j);
2262
          }
2263
        }
2264
      }
2265
      [Test]
2266
      public void RemoveAll2()
2267
      {
2268
        IList<int> list2 = new HashedLinkedList<int>();
2269
        list2.Add(1); list2.Add(3); list2.Add(4);
2270
        Assert.IsTrue(list.Check(), "list check before RemoveAll");
2271
        list.RemoveAll(list2);
2272

    
2273
        Assert.AreEqual(0, views[0][0].Offset, "view [0][0] offset");
2274
        Assert.AreEqual(0, views[0][1].Offset, "view [0][1] offset");
2275
        Assert.AreEqual(0, views[0][2].Offset, "view [0][2] offset");
2276
        Assert.AreEqual(0, views[0][3].Offset, "view [0][3] offset");
2277
        Assert.AreEqual(0, views[0][4].Offset, "view [0][4] offset");
2278
        Assert.AreEqual(0, views[0][5].Offset, "view [0][5] offset");
2279
        Assert.AreEqual(0, views[0][6].Offset, "view [0][6] offset");
2280
        Assert.AreEqual(1, views[1][0].Offset, "view [1][0] offset");
2281
        Assert.AreEqual(1, views[1][1].Offset, "view [1][1] offset");
2282
        Assert.AreEqual(1, views[1][2].Offset, "view [1][2] offset");
2283
        Assert.AreEqual(1, views[1][3].Offset, "view [1][3] offset");
2284
        Assert.AreEqual(1, views[1][4].Offset, "view [1][4] offset");
2285
        Assert.AreEqual(1, views[1][5].Offset, "view [1][5] offset");
2286
        Assert.AreEqual(1, views[2][0].Offset, "view [2][0] offset");
2287
        Assert.AreEqual(1, views[2][1].Offset, "view [2][1] offset");
2288
        Assert.AreEqual(1, views[2][2].Offset, "view [2][2] offset");
2289
        Assert.AreEqual(1, views[2][3].Offset, "view [2][3] offset");
2290
        Assert.AreEqual(1, views[2][4].Offset, "view [2][4] offset");
2291
        Assert.AreEqual(2, views[3][0].Offset, "view [3][0] offset");
2292
        Assert.AreEqual(2, views[3][1].Offset, "view [3][1] offset");
2293
        Assert.AreEqual(2, views[3][2].Offset, "view [3][2] offset");
2294
        Assert.AreEqual(2, views[3][3].Offset, "view [3][3] offset");
2295
        Assert.AreEqual(2, views[4][0].Offset, "view [4][0] offset");
2296
        Assert.AreEqual(2, views[4][1].Offset, "view [4][1] offset");
2297
        Assert.AreEqual(2, views[4][2].Offset, "view [4][2] offset");
2298
        Assert.AreEqual(2, views[5][0].Offset, "view [5][0] offset");
2299
        Assert.AreEqual(2, views[5][1].Offset, "view [5][1] offset");
2300
        Assert.AreEqual(3, views[6][0].Offset, "view [6][0] offset");
2301

    
2302
        Assert.AreEqual(0, views[0][0].Count, "view [0][0] count");
2303
        Assert.AreEqual(1, views[0][1].Count, "view [0][1] count");
2304
        Assert.AreEqual(1, views[0][2].Count, "view [0][2] count");
2305
        Assert.AreEqual(2, views[0][3].Count, "view [0][3] count");
2306
        Assert.AreEqual(2, views[0][4].Count, "view [0][4] count");
2307
        Assert.AreEqual(2, views[0][5].Count, "view [0][5] count");
2308
        Assert.AreEqual(3, views[0][6].Count, "view [0][6] count");
2309
        Assert.AreEqual(0, views[1][0].Count, "view [1][0] count");
2310
        Assert.AreEqual(0, views[1][1].Count, "view [1][1] count");
2311
        Assert.AreEqual(1, views[1][2].Count, "view [1][2] count");
2312
        Assert.AreEqual(1, views[1][3].Count, "view [1][3] count");
2313
        Assert.AreEqual(1, views[1][4].Count, "view [1][4] count");
2314
        Assert.AreEqual(2, views[1][5].Count, "view [1][5] count");
2315
        Assert.AreEqual(0, views[2][0].Count, "view [2][0] count");
2316
        Assert.AreEqual(1, views[2][1].Count, "view [2][1] count");
2317
        Assert.AreEqual(1, views[2][2].Count, "view [2][2] count");
2318
        Assert.AreEqual(1, views[2][3].Count, "view [2][3] count");
2319
        Assert.AreEqual(2, views[2][4].Count, "view [2][4] count");
2320
        Assert.AreEqual(0, views[3][0].Count, "view [3][0] count");
2321
        Assert.AreEqual(0, views[3][1].Count, "view [3][1] count");
2322
        Assert.AreEqual(0, views[3][2].Count, "view [3][2] count");
2323
        Assert.AreEqual(1, views[3][3].Count, "view [3][3] count");
2324
        Assert.AreEqual(0, views[4][0].Count, "view [4][0] count");
2325
        Assert.AreEqual(0, views[4][1].Count, "view [4][1] count");
2326
        Assert.AreEqual(1, views[4][2].Count, "view [4][2] count");
2327
        Assert.AreEqual(0, views[5][0].Count, "view [5][0] count");
2328
        Assert.AreEqual(1, views[5][1].Count, "view [5][1] count");
2329
        Assert.AreEqual(0, views[6][0].Count, "view [6][0] count");
2330

    
2331
        Assert.IsTrue(list.Check(), "list check after RemoveAll");
2332
      }
2333

    
2334
      [Test]
2335
      public void RetainAll()
2336
      {
2337
        IList<int> list2 = new HashedLinkedList<int>();
2338
        list2.Add(2); list2.Add(4); list2.Add(5);
2339
        Assert.IsTrue(list.Check(), "list check before RetainAll");
2340
        list.RetainAll(list2);
2341
        Assert.AreEqual(0, views[0][0].Offset, "view [0][0] offset");
2342
        Assert.AreEqual(0, views[0][1].Offset, "view [0][1] offset");
2343
        Assert.AreEqual(0, views[0][2].Offset, "view [0][2] offset");
2344
        Assert.AreEqual(0, views[0][3].Offset, "view [0][3] offset");
2345
        Assert.AreEqual(0, views[0][4].Offset, "view [0][4] offset");
2346
        Assert.AreEqual(0, views[0][5].Offset, "view [0][5] offset");
2347
        Assert.AreEqual(0, views[0][6].Offset, "view [0][6] offset");
2348
        Assert.AreEqual(0, views[1][0].Offset, "view [1][0] offset");
2349
        Assert.AreEqual(0, views[1][1].Offset, "view [1][1] offset");
2350
        Assert.AreEqual(0, views[1][2].Offset, "view [1][2] offset");
2351
        Assert.AreEqual(0, views[1][3].Offset, "view [1][3] offset");
2352
        Assert.AreEqual(0, views[1][4].Offset, "view [1][4] offset");
2353
        Assert.AreEqual(0, views[1][5].Offset, "view [1][5] offset");
2354
        Assert.AreEqual(0, views[2][0].Offset, "view [2][0] offset");
2355
        Assert.AreEqual(0, views[2][1].Offset, "view [2][1] offset");
2356
        Assert.AreEqual(0, views[2][2].Offset, "view [2][2] offset");
2357
        Assert.AreEqual(0, views[2][3].Offset, "view [2][3] offset");
2358
        Assert.AreEqual(0, views[2][4].Offset, "view [2][4] offset");
2359
        Assert.AreEqual(1, views[3][0].Offset, "view [3][0] offset");
2360
        Assert.AreEqual(1, views[3][1].Offset, "view [3][1] offset");
2361
        Assert.AreEqual(1, views[3][2].Offset, "view [3][2] offset");
2362
        Assert.AreEqual(1, views[3][3].Offset, "view [3][3] offset");
2363
        Assert.AreEqual(1, views[4][0].Offset, "view [4][0] offset");
2364
        Assert.AreEqual(1, views[4][1].Offset, "view [4][1] offset");
2365
        Assert.AreEqual(1, views[4][2].Offset, "view [4][2] offset");
2366
        Assert.AreEqual(2, views[5][0].Offset, "view [5][0] offset");
2367
        Assert.AreEqual(2, views[5][1].Offset, "view [5][1] offset");
2368
        Assert.AreEqual(3, views[6][0].Offset, "view [6][0] offset");
2369

    
2370
        Assert.AreEqual(0, views[0][0].Count, "view [0][0] count");
2371
        Assert.AreEqual(0, views[0][1].Count, "view [0][1] count");
2372
        Assert.AreEqual(0, views[0][2].Count, "view [0][2] count");
2373
        Assert.AreEqual(1, views[0][3].Count, "view [0][3] count");
2374
        Assert.AreEqual(1, views[0][4].Count, "view [0][4] count");
2375
        Assert.AreEqual(2, views[0][5].Count, "view [0][5] count");
2376
        Assert.AreEqual(3, views[0][6].Count, "view [0][6] count");
2377
        Assert.AreEqual(0, views[1][0].Count, "view [1][0] count");
2378
        Assert.AreEqual(0, views[1][1].Count, "view [1][1] count");
2379
        Assert.AreEqual(1, views[1][2].Count, "view [1][2] count");
2380
        Assert.AreEqual(1, views[1][3].Count, "view [1][3] count");
2381
        Assert.AreEqual(2, views[1][4].Count, "view [1][4] count");
2382
        Assert.AreEqual(3, views[1][5].Count, "view [1][5] count");
2383
        Assert.AreEqual(0, views[2][0].Count, "view [2][0] count");
2384
        Assert.AreEqual(1, views[2][1].Count, "view [2][1] count");
2385
        Assert.AreEqual(1, views[2][2].Count, "view [2][2] count");
2386
        Assert.AreEqual(2, views[2][3].Count, "view [2][3] count");
2387
        Assert.AreEqual(3, views[2][4].Count, "view [2][4] count");
2388
        Assert.AreEqual(0, views[3][0].Count, "view [3][0] count");
2389
        Assert.AreEqual(0, views[3][1].Count, "view [3][1] count");
2390
        Assert.AreEqual(1, views[3][2].Count, "view [3][2] count");
2391
        Assert.AreEqual(2, views[3][3].Count, "view [3][3] count");
2392
        Assert.AreEqual(0, views[4][0].Count, "view [4][0] count");
2393
        Assert.AreEqual(1, views[4][1].Count, "view [4][1] count");
2394
        Assert.AreEqual(2, views[4][2].Count, "view [4][2] count");
2395
        Assert.AreEqual(0, views[5][0].Count, "view [5][0] count");
2396
        Assert.AreEqual(1, views[5][1].Count, "view [5][1] count");
2397
        Assert.AreEqual(0, views[6][0].Count, "view [6][0] count");
2398

    
2399

    
2400
        Assert.IsTrue(list.Check(), "list check after RetainAll");
2401
      }
2402

    
2403
      [Test]
2404
      public void RemoveAllCopies()
2405
      {
2406
        IList<int> list2 = new HashedLinkedList<int>();
2407
        list2.Add(0); list2.Add(2); list2.Add(82); list2.Add(92); list2.Add(5); list2.Add(2); list2.Add(1);
2408
        for (int i = 0; i < 7; i++)
2409
        {
2410
          for (int j = 0; j < 7 - i; j++)
2411
          {
2412
            list = new HashedLinkedList<int>();
2413
            list.AddAll(list2);
2414
            HashedLinkedList<int> v = (HashedLinkedList<int>)list.View(i, j);
2415
            list.RemoveAllCopies(2);
2416
            Assert.IsTrue(list.Check(), "list check after RemoveAllCopies, i=" + i + ", j=" + j);
2417
          }
2418
        }
2419
      }
2420

    
2421
      private void checkDisposed(bool reverse, int start, int count)
2422
      {
2423
        int k = 0;
2424
        for (int i = 0; i < 7; i++)
2425
          for (int j = 0; j < 7 - i; j++)
2426
          {
2427
            if (i + j <= start || i >= start + count || (i <= start && i + j >= start + count) || (reverse && start <= i && start + count >= i + j))
2428
            {
2429
              try
2430
              {
2431
                k = views[i][j].Count;
2432
              }
2433
              catch (ViewDisposedException)
2434
              {
2435
                Assert.Fail("view[" + i + "][" + j + "] threw");
2436
              }
2437
              Assert.AreEqual(j, views[i][j].Count, "view[" + i + "][" + j + "] size");
2438
              if (reverse && ((j > 0 && start <= i && start + count >= i + j) || (j == 0 && start < i && start + count > i)))
2439
                Assert.AreEqual(start + (start + count - i - j), views[i][j].Offset, "view[" + i + "][" + j + "] offset (mirrored)");
2440
              else
2441
                Assert.AreEqual(i, views[i][j].Offset, "view[" + i + "][" + j + "] offset");
2442
            }
2443
            else
2444
            {
2445
              try
2446
              {
2447
                k = views[i][j].Count;
2448
                Assert.Fail("view[" + i + "][" + j + "] no throw");
2449
              }
2450
              catch (ViewDisposedException) { }
2451
            }
2452
          }
2453
      }
2454

    
2455
      [Test]
2456
      public void Reverse()
2457
      {
2458
        int start = 2, count = 3;
2459
        IList<int> list2 = list.View(start, count);
2460
        Assert.IsTrue(list.Check(), "list check before Reverse");
2461
        list2.Reverse();
2462
        Assert.IsTrue(list.Check(), "list check after Reverse");
2463
        checkDisposed(true, start, count);
2464
      }
2465

    
2466
      [Test]
2467
      public void Sort()
2468
      {
2469
        int start = 2, count = 3;
2470
        IList<int> list2 = list.View(start, count);
2471
        Assert.IsTrue(list.Check(), "list check before Sort");
2472
        list2.Sort();
2473
        Assert.IsTrue(list.Check(), "list check after Sort");
2474
        checkDisposed(false, start, count);
2475
      }
2476
      [Test]
2477
      public void Shuffle()
2478
      {
2479
        int start = 2, count = 3;
2480
        IList<int> list2 = list.View(start, count);
2481
        Assert.IsTrue(list.Check(), "list check before Shuffle");
2482
        list2.Shuffle();
2483
        Assert.IsTrue(list.Check(), "list check after Shuffle");
2484
        checkDisposed(false, start, count);
2485
      }
2486

    
2487

    
2488
    }
2489

    
2490
  }
2491

    
2492
  namespace HashingAndEquals
2493
  {
2494
    [TestFixture]
2495
    public class ISequenced
2496
    {
2497
      private ISequenced<int> dit, dat, dut;
2498

    
2499

    
2500
      [SetUp]
2501
      public void Init()
2502
      {
2503
        dit = new HashedLinkedList<int>();
2504
        dat = new HashedLinkedList<int>();
2505
        dut = new HashedLinkedList<int>();
2506
      }
2507

    
2508

    
2509
      [Test]
2510
      public void EmptyEmpty()
2511
      {
2512
        Assert.IsTrue(dit.SequencedEquals(dat));
2513
      }
2514

    
2515

    
2516
      [Test]
2517
      public void EmptyNonEmpty()
2518
      {
2519
        dit.Add(3);
2520
        Assert.IsFalse(dit.SequencedEquals(dat));
2521
        Assert.IsFalse(dat.SequencedEquals(dit));
2522
      }
2523

    
2524
      [Test]
2525
      public void HashVal()
2526
      {
2527
        Assert.AreEqual(CHC.sequencedhashcode(), dit.GetSequencedHashCode());
2528
        dit.Add(3);
2529
        Assert.AreEqual(CHC.sequencedhashcode(3), dit.GetSequencedHashCode());
2530
        dit.Add(7);
2531
        Assert.AreEqual(CHC.sequencedhashcode(3, 7), dit.GetSequencedHashCode());
2532
        Assert.AreEqual(CHC.sequencedhashcode(), dut.GetSequencedHashCode());
2533
        dut.Add(7);
2534
        Assert.AreEqual(CHC.sequencedhashcode(7), dut.GetSequencedHashCode());
2535
        dut.Add(3);
2536
        Assert.AreEqual(CHC.sequencedhashcode(7, 3), dut.GetSequencedHashCode());
2537
      }
2538

    
2539

    
2540
      [Test]
2541
      public void EqualHashButDifferent()
2542
      {
2543
        dit.Add(0); dit.Add(31);
2544
        dat.Add(1); dat.Add(0);
2545
        Assert.AreEqual(dit.GetSequencedHashCode(), dat.GetSequencedHashCode());
2546
        Assert.IsFalse(dit.SequencedEquals(dat));
2547
      }
2548

    
2549

    
2550
      [Test]
2551
      public void Normal()
2552
      {
2553
        dit.Add(3);
2554
        dit.Add(7);
2555
        dat.Add(3);
2556
        Assert.IsFalse(dit.SequencedEquals(dat));
2557
        Assert.IsFalse(dat.SequencedEquals(dit));
2558
        dat.Add(7);
2559
        Assert.IsTrue(dit.SequencedEquals(dat));
2560
        Assert.IsTrue(dat.SequencedEquals(dit));
2561
      }
2562

    
2563

    
2564
      [Test]
2565
      public void WrongOrder()
2566
      {
2567
        dit.Add(3);
2568
        dut.Add(3);
2569
        Assert.IsTrue(dit.SequencedEquals(dut));
2570
        Assert.IsTrue(dut.SequencedEquals(dit));
2571
        dit.Add(7);
2572
        ((HashedLinkedList<int>)dut).InsertFirst(7);
2573
        Assert.IsFalse(dit.SequencedEquals(dut));
2574
        Assert.IsFalse(dut.SequencedEquals(dit));
2575
      }
2576

    
2577

    
2578
      [Test]
2579
      public void Reflexive()
2580
      {
2581
        Assert.IsTrue(dit.SequencedEquals(dit));
2582
        dit.Add(3);
2583
        Assert.IsTrue(dit.SequencedEquals(dit));
2584
        dit.Add(7);
2585
        Assert.IsTrue(dit.SequencedEquals(dit));
2586
      }
2587

    
2588

    
2589
      [TearDown]
2590
      public void Dispose()
2591
      {
2592
        dit = null;
2593
        dat = null;
2594
        dut = null;
2595
      }
2596
    }
2597

    
2598

    
2599

    
2600
    [TestFixture]
2601
    public class IEditableCollection
2602
    {
2603
      private ICollection<int> dit, dat, dut;
2604

    
2605

    
2606
      [SetUp]
2607
      public void Init()
2608
      {
2609
        dit = new HashedLinkedList<int>();
2610
        dat = new HashedLinkedList<int>();
2611
        dut = new HashedLinkedList<int>();
2612
      }
2613

    
2614

    
2615
      [Test]
2616
      public void EmptyEmpty()
2617
      {
2618
        Assert.IsTrue(dit.UnsequencedEquals(dat));
2619
      }
2620

    
2621

    
2622
      [Test]
2623
      public void EmptyNonEmpty()
2624
      {
2625
        dit.Add(3);
2626
        Assert.IsFalse(dit.UnsequencedEquals(dat));
2627
        Assert.IsFalse(dat.UnsequencedEquals(dit));
2628
      }
2629

    
2630

    
2631
      [Test]
2632
      public void HashVal()
2633
      {
2634
        Assert.AreEqual(CHC.unsequencedhashcode(), dit.GetUnsequencedHashCode());
2635
        dit.Add(3);
2636
        Assert.AreEqual(CHC.unsequencedhashcode(3), dit.GetUnsequencedHashCode());
2637
        dit.Add(7);
2638
        Assert.AreEqual(CHC.unsequencedhashcode(3, 7), dit.GetUnsequencedHashCode());
2639
        Assert.AreEqual(CHC.unsequencedhashcode(), dut.GetUnsequencedHashCode());
2640
        dut.Add(3);
2641
        Assert.AreEqual(CHC.unsequencedhashcode(3), dut.GetUnsequencedHashCode());
2642
        dut.Add(7);
2643
        Assert.AreEqual(CHC.unsequencedhashcode(7, 3), dut.GetUnsequencedHashCode());
2644
      }
2645

    
2646

    
2647
      [Test]
2648
      public void EqualHashButDifferent()
2649
      {
2650
        dit.Add(-1657792980); dit.Add(-1570288808);
2651
        dat.Add(1862883298); dat.Add(-272461342);
2652
        Assert.AreEqual(dit.GetUnsequencedHashCode(), dat.GetUnsequencedHashCode());
2653
        Assert.IsFalse(dit.UnsequencedEquals(dat));
2654
      }
2655

    
2656

    
2657
      [Test]
2658
      public void Normal()
2659
      {
2660
        dit.Add(3);
2661
        dit.Add(7);
2662
        dat.Add(3);
2663
        Assert.IsFalse(dit.UnsequencedEquals(dat));
2664
        Assert.IsFalse(dat.UnsequencedEquals(dit));
2665
        dat.Add(7);
2666
        Assert.IsTrue(dit.UnsequencedEquals(dat));
2667
        Assert.IsTrue(dat.UnsequencedEquals(dit));
2668
      }
2669

    
2670

    
2671
      [Test]
2672
      public void WrongOrder()
2673
      {
2674
        dit.Add(3);
2675
        dut.Add(3);
2676
        Assert.IsTrue(dit.UnsequencedEquals(dut));
2677
        Assert.IsTrue(dut.UnsequencedEquals(dit));
2678
        dit.Add(7);
2679
        dut.Add(7);
2680
        Assert.IsTrue(dit.UnsequencedEquals(dut));
2681
        Assert.IsTrue(dut.UnsequencedEquals(dit));
2682
      }
2683

    
2684

    
2685
      [Test]
2686
      public void Reflexive()
2687
      {
2688
        Assert.IsTrue(dit.UnsequencedEquals(dit));
2689
        dit.Add(3);
2690
        Assert.IsTrue(dit.UnsequencedEquals(dit));
2691
        dit.Add(7);
2692
        Assert.IsTrue(dit.UnsequencedEquals(dit));
2693
      }
2694

    
2695

    
2696
      [TearDown]
2697
      public void Dispose()
2698
      {
2699
        dit = null;
2700
        dat = null;
2701
        dut = null;
2702
      }
2703
    }
2704

    
2705

    
2706

    
2707
    [TestFixture]
2708
    public class MultiLevelUnorderedOfUnOrdered
2709
    {
2710
      private ICollection<int> dit, dat, dut;
2711

    
2712
      private ICollection<ICollection<int>> Dit, Dat, Dut;
2713

    
2714

    
2715
      [SetUp]
2716
      public void Init()
2717
      {
2718
        dit = new HashedLinkedList<int>();
2719
        dat = new HashedLinkedList<int>();
2720
        dut = new HashedLinkedList<int>();
2721
        dit.Add(2); dit.Add(1);
2722
        dat.Add(1); dat.Add(2);
2723
        dut.Add(3);
2724
        Dit = new HashedLinkedList<ICollection<int>>();
2725
        Dat = new HashedLinkedList<ICollection<int>>();
2726
        Dut = new HashedLinkedList<ICollection<int>>();
2727
      }
2728

    
2729

    
2730
      [Test]
2731
      public void Check()
2732
      {
2733
        Assert.IsTrue(dit.UnsequencedEquals(dat));
2734
        Assert.IsFalse(dit.UnsequencedEquals(dut));
2735
      }
2736

    
2737

    
2738
      [Test]
2739
      public void Multi()
2740
      {
2741
        Dit.Add(dit); Dit.Add(dut); Dit.Add(dit);
2742
        Dat.Add(dut); Dat.Add(dit); Dat.Add(dat);
2743
        Assert.IsTrue(Dit.UnsequencedEquals(Dat));
2744
        Assert.IsFalse(Dit.UnsequencedEquals(Dut));
2745
      }
2746

    
2747

    
2748
      [TearDown]
2749
      public void Dispose()
2750
      {
2751
        dit = dat = dut = null;
2752
        Dit = Dat = Dut = null;
2753
      }
2754
    }
2755

    
2756

    
2757

    
2758
    [TestFixture]
2759
    public class MultiLevelOrderedOfUnOrdered
2760
    {
2761
      private ICollection<int> dit, dat, dut;
2762

    
2763
      private ISequenced<ICollection<int>> Dit, Dat, Dut;
2764

    
2765

    
2766
      [SetUp]
2767
      public void Init()
2768
      {
2769
        dit = new HashedLinkedList<int>();
2770
        dat = new HashedLinkedList<int>();
2771
        dut = new HashedLinkedList<int>();
2772
        dit.Add(2); dit.Add(1);
2773
        dat.Add(1); dat.Add(2);
2774
        dut.Add(3);
2775
        Dit = new HashedLinkedList<ICollection<int>>();
2776
        Dat = new HashedLinkedList<ICollection<int>>();
2777
        Dut = new HashedLinkedList<ICollection<int>>();
2778
      }
2779

    
2780

    
2781
      [Test]
2782
      public void Check()
2783
      {
2784
        Assert.IsTrue(dit.UnsequencedEquals(dat));
2785
        Assert.IsFalse(dit.UnsequencedEquals(dut));
2786
      }
2787

    
2788

    
2789
      [Test]
2790
      public void Multi()
2791
      {
2792
        Dit.Add(dit); Dit.Add(dut); Dit.Add(dit);
2793
        Dat.Add(dut); Dat.Add(dit); Dat.Add(dat);
2794
        Dut.Add(dit); Dut.Add(dut); Dut.Add(dat);
2795
        Assert.IsFalse(Dit.SequencedEquals(Dat));
2796
        Assert.IsTrue(Dit.SequencedEquals(Dut));
2797
      }
2798

    
2799

    
2800
      [TearDown]
2801
      public void Dispose()
2802
      {
2803
        dit = dat = dut = null;
2804
        Dit = Dat = Dut = null;
2805
      }
2806
    }
2807

    
2808

    
2809

    
2810
    [TestFixture]
2811
    public class MultiLevelUnOrderedOfOrdered
2812
    {
2813
      private ISequenced<int> dit, dat, dut, dot;
2814

    
2815
      private ICollection<ISequenced<int>> Dit, Dat, Dut, Dot;
2816

    
2817

    
2818
      [SetUp]
2819
      public void Init()
2820
      {
2821
        dit = new HashedLinkedList<int>();
2822
        dat = new HashedLinkedList<int>();
2823
        dut = new HashedLinkedList<int>();
2824
        dot = new HashedLinkedList<int>();
2825
        dit.Add(2); dit.Add(1);
2826
        dat.Add(1); dat.Add(2);
2827
        dut.Add(3);
2828
        dot.Add(2); dot.Add(1);
2829
        Dit = new HashedLinkedList<ISequenced<int>>();
2830
        Dat = new HashedLinkedList<ISequenced<int>>();
2831
        Dut = new HashedLinkedList<ISequenced<int>>();
2832
        Dot = new HashedLinkedList<ISequenced<int>>();
2833
      }
2834

    
2835

    
2836
      [Test]
2837
      public void Check()
2838
      {
2839
        Assert.IsFalse(dit.SequencedEquals(dat));
2840
        Assert.IsTrue(dit.SequencedEquals(dot));
2841
        Assert.IsFalse(dit.SequencedEquals(dut));
2842
      }
2843

    
2844

    
2845
      [Test]
2846
      public void Multi()
2847
      {
2848
        Dit.Add(dit); Dit.Add(dut); Dit.Add(dit);
2849
        Dat.Add(dut); Dat.Add(dit); Dat.Add(dat);
2850
        Dut.Add(dot); Dut.Add(dut); Dut.Add(dit);
2851
        Dot.Add(dit); Dot.Add(dit); Dot.Add(dut);
2852
        Assert.IsTrue(Dit.UnsequencedEquals(Dut));
2853
        Assert.IsFalse(Dit.UnsequencedEquals(Dat));
2854
        Assert.IsTrue(Dit.UnsequencedEquals(Dot));
2855
      }
2856

    
2857

    
2858
      [TearDown]
2859
      public void Dispose()
2860
      {
2861
        dit = dat = dut = dot = null;
2862
        Dit = Dat = Dut = Dot = null;
2863
      }
2864
    }
2865

    
2866

    
2867

    
2868
    [TestFixture]
2869
    public class MultiLevelOrderedOfOrdered
2870
    {
2871
      private ISequenced<int> dit, dat, dut, dot;
2872

    
2873
      private ISequenced<ISequenced<int>> Dit, Dat, Dut, Dot;
2874

    
2875

    
2876
      [SetUp]
2877
      public void Init()
2878
      {
2879
        dit = new HashedLinkedList<int>();
2880
        dat = new HashedLinkedList<int>();
2881
        dut = new HashedLinkedList<int>();
2882
        dot = new HashedLinkedList<int>();
2883
        dit.Add(2); dit.Add(1); //{2,1}
2884
        dat.Add(1); dat.Add(2); //{1,2}
2885
        dut.Add(3);            //{3}
2886
        dot.Add(2); dot.Add(1); //{2,1}
2887
        Dit = new HashedLinkedList<ISequenced<int>>();
2888
        Dat = new HashedLinkedList<ISequenced<int>>();
2889
        Dut = new HashedLinkedList<ISequenced<int>>();
2890
        Dot = new HashedLinkedList<ISequenced<int>>();
2891
      }
2892

    
2893

    
2894
      [Test]
2895
      public void Check()
2896
      {
2897
        Assert.IsFalse(dit.SequencedEquals(dat));
2898
        Assert.IsTrue(dit.SequencedEquals(dot));
2899
        Assert.IsFalse(dit.SequencedEquals(dut));
2900
      }
2901

    
2902

    
2903
      [Test]
2904
      public void Multi()
2905
      {
2906
        Dit.Add(dit); Dit.Add(dut); Dit.Add(dit); // {{2,1},{3}}
2907
        Dat.Add(dut); Dat.Add(dit); Dat.Add(dat); // {{3},{2,1},{1,2}}
2908
        Dut.Add(dot); Dut.Add(dut); Dut.Add(dit); // {{2,1},{3}}
2909
        Dot.Add(dit); Dot.Add(dit); Dot.Add(dut); // {{2,1},{3}}
2910
        Assert.IsTrue(Dit.SequencedEquals(Dut));
2911
        Assert.IsFalse(Dit.SequencedEquals(Dat));
2912
        Assert.IsTrue(Dit.SequencedEquals(Dot));
2913
      }
2914

    
2915

    
2916
      [TearDown]
2917
      public void Dispose()
2918
      {
2919
        dit = dat = dut = dot = null;
2920
        Dit = Dat = Dut = Dot = null;
2921
      }
2922
    }
2923
  }
2924
}