Project

General

Profile

root / branches / compiler / cSharp / ooasCompiler / src / libs / c5 / nunit / arrays / HashedArrayListTest.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.arrays.hashed
29
{
30
  using CollectionOfInt = HashedArrayList<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 HashedArrayList<T>(); }
62
  }
63

    
64
  namespace Events
65
  {
66
    class TenEqualityComparer : SCG.IEqualityComparer<int>
67
    {
68
      TenEqualityComparer() { }
69
      public static TenEqualityComparer Default { get { return new TenEqualityComparer(); } }
70
      public int GetHashCode(int item) { return (item / 10).GetHashCode(); }
71
      public bool Equals(int item1, int item2) { return item1 / 10 == item2 / 10; }
72
    }
73

    
74
    [TestFixture]
75
    public class IList_
76
    {
77
      private HashedArrayList<int> list;
78
      CollectionEventList<int> seen;
79

    
80
      [SetUp]
81
      public void Init()
82
      {
83
        list = new HashedArrayList<int>(TenEqualityComparer.Default);
84
        seen = new CollectionEventList<int>(IntEqualityComparer.Default);
85
      }
86

    
87
      private void listen() { seen.Listen(list, EventTypeEnum.All); }
88

    
89
      [Test]
90
      public void SetThis()
91
      {
92
        list.Add(4); list.Add(56); list.Add(8);
93
        listen();
94
        list[1] = 45;
95
        seen.Check(new CollectionEvent<int>[] {
96
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(56, 1), list),
97
          new CollectionEvent<int>(EventTypeEnum.RemovedAt, new ItemAtEventArgs<int>(56,1), list),
98
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(45, 1), list),
99
          new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(45,1), list),
100
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)
101
          });
102
      }
103

    
104
      [Test]
105
      public void Insert()
106
      {
107
        list.Add(4); list.Add(56); list.Add(8);
108
        listen();
109
        list.Insert(1, 45);
110
        seen.Check(new CollectionEvent<int>[] {
111
          new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(45,1), list),
112
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(45, 1), list),
113
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)
114
          });
115
      }
116

    
117
      [Test]
118
      public void InsertAll()
119
      {
120
        list.Add(4); list.Add(56); list.Add(8);
121
        listen();
122
        list.InsertAll<int>(1, new int[] { 666, 777, 888 });
123
        //seen.Print(Console.Error);
124
        seen.Check(new CollectionEvent<int>[] {
125
          new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(666,1), list),
126
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(666, 1), list),
127
          new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(777,2), list),
128
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(777, 1), list),
129
          new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(888,3), list),
130
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(888, 1), list),
131
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)
132
          });
133
        list.InsertAll<int>(1, new int[] {});
134
        seen.Check(new CollectionEvent<int>[] {});
135
      }
136

    
137
      [Test]
138
      public void InsertFirstLast()
139
      {
140
        list.Add(4); list.Add(56); list.Add(18);
141
        listen();
142
        list.InsertFirst(45);
143
        seen.Check(new CollectionEvent<int>[] {
144
          new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(45,0), list),
145
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(45, 1), list),
146
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)
147
          });
148
        list.InsertLast(88);
149
        seen.Check(new CollectionEvent<int>[] {
150
          new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(88,4), list),
151
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(88, 1), list),
152
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)
153
          });
154
      }
155

    
156
      [Test]
157
      public void Remove()
158
      {
159
        list.Add(4); list.Add(56); list.Add(8);
160
        listen();
161
        list.Remove();
162
        seen.Check(new CollectionEvent<int>[] {
163
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(56, 1), list),
164
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)});
165
      }
166

    
167
      [Test]
168
      public void RemoveFirst()
169
      {
170
        list.Add(4); list.Add(56); list.Add(8);
171
        listen();
172
        list.RemoveFirst();
173
        seen.Check(new CollectionEvent<int>[] {
174
          new CollectionEvent<int>(EventTypeEnum.RemovedAt, new ItemAtEventArgs<int>(4,0), list),
175
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(4, 1), list),
176
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)});
177
      }
178

    
179
      [Test]
180
      public void RemoveLast()
181
      {
182
        list.Add(4); list.Add(56); list.Add(8);
183
        listen();
184
        list.RemoveLast();
185
        seen.Check(new CollectionEvent<int>[] {
186
          new CollectionEvent<int>(EventTypeEnum.RemovedAt, new ItemAtEventArgs<int>(56,1), list),
187
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(56, 1), list),
188
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)});
189
      }
190

    
191
      [Test]
192
      public void Reverse()
193
      {
194
        list.Add(4); list.Add(56); list.Add(8);
195
        listen();
196
        list.Reverse();
197
        seen.Check(new CollectionEvent<int>[] {
198
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)
199
        });
200
        list.View(1, 0).Reverse();
201
        seen.Check(new CollectionEvent<int>[] {});
202
      }
203

    
204

    
205
      [Test]
206
      public void Sort()
207
      {
208
        list.Add(4); list.Add(56); list.Add(8);
209
        listen();
210
        list.Sort();
211
        seen.Check(new CollectionEvent<int>[] {
212
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)
213
        });
214
        list.View(1, 0).Sort();
215
        seen.Check(new CollectionEvent<int>[] {});
216
      }
217

    
218
      [Test]
219
      public void Shuffle()
220
      {
221
        list.Add(4); list.Add(56); list.Add(8);
222
        listen();
223
        list.Shuffle();
224
        seen.Check(new CollectionEvent<int>[] {
225
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)
226
        });
227
        list.View(1, 0).Shuffle();
228
        seen.Check(new CollectionEvent<int>[] {});
229
      }
230

    
231
      [Test]
232
      public void RemoveAt()
233
      {
234
        list.Add(4); list.Add(56); list.Add(8);
235
        listen();
236
        list.RemoveAt(1);
237
        seen.Check(new CollectionEvent<int>[] {
238
          new CollectionEvent<int>(EventTypeEnum.RemovedAt, new ItemAtEventArgs<int>(56,1), list),
239
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(56, 1), list),
240
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)});
241
      }
242

    
243
      [Test]
244
      public void RemoveInterval()
245
      {
246
        list.Add(4); list.Add(56); list.Add(18);
247
        listen();
248
        list.RemoveInterval(1, 2);
249
        seen.Check(new CollectionEvent<int>[] {
250
           new CollectionEvent<int>(EventTypeEnum.Cleared, new ClearedRangeEventArgs(false,2,1), list),
251
         new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)
252
        });
253
        list.RemoveInterval(1, 0);
254
        seen.Check(new CollectionEvent<int>[] {});
255
      }
256

    
257
      [Test]
258
      public void Update()
259
      {
260
        list.Add(4); list.Add(56); list.Add(8);
261
        listen();
262
        list.Update(53);
263
        seen.Check(new CollectionEvent<int>[] {
264
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(56, 1), list),
265
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(53, 1), list),
266
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)
267
          });
268
        list.Update(67);
269
        seen.Check(new CollectionEvent<int>[] {});
270
      }
271

    
272
      [Test]
273
      public void FindOrAdd()
274
      {
275
        list.Add(4); list.Add(56); list.Add(8);
276
        listen();
277
        int val = 53;
278
        list.FindOrAdd(ref val);
279
        seen.Check(new CollectionEvent<int>[] {});
280
        val = 67;
281
        list.FindOrAdd(ref val);
282
        seen.Check(new CollectionEvent<int>[] { 
283
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(67, 1), list),
284
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)
285
        });
286
      }
287

    
288
      [Test]
289
      public void UpdateOrAdd()
290
      {
291
        list.Add(4); list.Add(56); list.Add(8);
292
        listen();
293
        int val = 53;
294
        list.UpdateOrAdd(val);
295
        seen.Check(new CollectionEvent<int>[] {
296
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(56, 1), list),
297
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(53, 1), list),
298
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)
299
        });
300
        val = 67;
301
        list.UpdateOrAdd(val);
302
        seen.Check(new CollectionEvent<int>[] { 
303
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(67, 1), list),
304
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)
305
        });
306
        list.UpdateOrAdd(51, out val);
307
        seen.Check(new CollectionEvent<int>[] {
308
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(53, 1), list),
309
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(51, 1), list),
310
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)
311
        });
312
        val = 67;
313
        list.UpdateOrAdd(81, out val);
314
        seen.Check(new CollectionEvent<int>[] { 
315
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(81, 1), list),
316
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)
317
        });
318
      }
319

    
320
      [Test]
321
      public void RemoveItem()
322
      {
323
        list.Add(4); list.Add(56); list.Add(18);
324
        listen();
325
        list.Remove(53);
326
        seen.Check(new CollectionEvent<int>[] {
327
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(56, 1), list),
328
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)});
329
        list.Remove(11);
330
        seen.Check(new CollectionEvent<int>[] {
331
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(18, 1), list),
332
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)});
333
      }
334

    
335
      [Test]
336
      public void RemoveAll()
337
      {
338
        for (int i = 0; i < 10; i++)
339
        {
340
          list.Add(10 * i + 5);
341
        }
342
        listen();
343
        list.RemoveAll<int>(new int[] { 32, 187, 45 });
344
        //TODO: the order depends on internals of the HashSet
345
        seen.Check(new CollectionEvent<int>[] {
346
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(35, 1), list),
347
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(45, 1), list),
348
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)});
349
        list.RemoveAll<int>(new int[] { 200, 300 });
350
        seen.Check(new CollectionEvent<int>[] {});
351
      }
352

    
353
      [Test]
354
      public void Clear()
355
      {
356
        list.Add(4); list.Add(56); list.Add(18);
357
        listen();
358
        list.View(1, 1).Clear();
359
        seen.Check(new CollectionEvent<int>[] {
360
          new CollectionEvent<int>(EventTypeEnum.Cleared, new ClearedRangeEventArgs(false,1,1), list),
361
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)
362
        });
363
        list.Clear();
364
        seen.Check(new CollectionEvent<int>[] {
365
          new CollectionEvent<int>(EventTypeEnum.Cleared, new ClearedRangeEventArgs(true,2,0), list),
366
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)
367
        });
368
        list.Clear();
369
        seen.Check(new CollectionEvent<int>[] {});
370
      }
371

    
372
      [Test]
373
      public void ListDispose()
374
      {
375
        list.Add(4); list.Add(56); list.Add(18);
376
        listen();
377
        list.View(1, 1).Dispose();
378
        seen.Check(new CollectionEvent<int>[] {});
379
        list.Dispose();
380
        seen.Check(new CollectionEvent<int>[] {
381
          new CollectionEvent<int>(EventTypeEnum.Cleared, new ClearedRangeEventArgs(true,3,0), list),
382
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)
383
        });
384
        list.Dispose();
385
        seen.Check(new CollectionEvent<int>[] {});
386
      }
387

    
388

    
389
      [Test]
390
      public void RetainAll()
391
      {
392
        for (int i = 0; i < 10; i++)
393
        {
394
          list.Add(10 * i + 5);
395
        }
396
        listen();
397
        list.RetainAll<int>(new int[] { 32, 187, 45, 62, 82, 95, 2 });
398
        seen.Check(new CollectionEvent<int>[] {
399
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(15, 1), list),
400
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(25, 1), list),
401
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(55, 1), list),
402
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(75, 1), list),
403
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)});
404
        list.RetainAll<int>(new int[] { 32, 187, 45, 62, 82, 95, 2 });
405
        seen.Check(new CollectionEvent<int>[] {});
406
      }
407

    
408
      [Test]
409
      public void RemoveAllCopies()
410
      {
411
        for (int i = 0; i < 10; i++)
412
        {
413
          list.Add(3 * i + 5);
414
        }
415
        listen();
416
        list.RemoveAllCopies(14);
417
        seen.Check(new CollectionEvent<int>[] {
418
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(11, 1), list),
419
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)});
420
        list.RemoveAllCopies(14);
421
        seen.Check(new CollectionEvent<int>[] {});
422
      }
423

    
424
      [Test]
425
      public void Add()
426
      {
427
        listen();
428
        seen.Check(new CollectionEvent<int>[0]);
429
        list.Add(23);
430
        seen.Check(new CollectionEvent<int>[] {
431
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(23, 1), list),
432
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)});
433
      }
434

    
435
      [Test]
436
      public void AddAll()
437
      {
438
        for (int i = 0; i < 10; i++)
439
        {
440
          list.Add(10 * i + 5);
441
        }
442
        listen();
443
        list.AddAll<int>(new int[] { 145, 56, 167 });
444
        seen.Check(new CollectionEvent<int>[] {
445
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(145, 1), list),
446
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(167, 1), list),
447
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)});
448
        list.AddAll<int>(new int[] { });
449
        seen.Check(new CollectionEvent<int>[] {});
450
      }
451

    
452
      [TearDown]
453
      public void Dispose() { list = null; seen = null; }
454
    }
455

    
456
    [TestFixture]
457
    public class StackQueue
458
    {
459

    
460
      private ArrayList<int> list;
461
      CollectionEventList<int> seen;
462

    
463
      [SetUp]
464
      public void Init()
465
      {
466
        list = new ArrayList<int>(TenEqualityComparer.Default);
467
        seen = new CollectionEventList<int>(IntEqualityComparer.Default);
468
      }
469

    
470
      private void listen() { seen.Listen(list, EventTypeEnum.All); }
471

    
472
      [Test]
473
      public void EnqueueDequeue()
474
      {
475
        listen();
476
        list.Enqueue(67);
477
        seen.Check(new CollectionEvent<int>[] {
478
          new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(67,0), list),
479
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(67, 1), list),
480
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)});
481
        list.Enqueue(2);
482
        seen.Check(new CollectionEvent<int>[] {
483
          new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(2,1), list),
484
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(2, 1), list),
485
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)});
486
        list.Dequeue();
487
        seen.Check(new CollectionEvent<int>[] {
488
          new CollectionEvent<int>(EventTypeEnum.RemovedAt, new ItemAtEventArgs<int>(67,0), list),
489
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(67, 1), list),
490
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)});
491
        list.Dequeue();
492
        seen.Check(new CollectionEvent<int>[] {
493
          new CollectionEvent<int>(EventTypeEnum.RemovedAt, new ItemAtEventArgs<int>(2,0), list),
494
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(2, 1), list),
495
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)});
496
      }
497

    
498
      [Test]
499
      public void PushPop()
500
      {
501
        listen();
502
        seen.Check(new CollectionEvent<int>[0]);
503
        list.Push(23);
504
        seen.Check(new CollectionEvent<int>[] {
505
          new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(23,0), list),
506
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(23, 1), list),
507
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)});
508
        list.Push(-12);
509
        seen.Check(new CollectionEvent<int>[] {
510
          new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(-12,1), list),
511
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(-12, 1), list),
512
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)});
513
        list.Pop();
514
        seen.Check(new CollectionEvent<int>[] {
515
          new CollectionEvent<int>(EventTypeEnum.RemovedAt, new ItemAtEventArgs<int>(-12,1), list),
516
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(-12, 1), list),
517
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)});
518
        list.Pop();
519
        seen.Check(new CollectionEvent<int>[] {
520
          new CollectionEvent<int>(EventTypeEnum.RemovedAt, new ItemAtEventArgs<int>(23,0), list),
521
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(23, 1), list),
522
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), list)});
523
      }
524

    
525
      [TearDown]
526
      public void Dispose() { list = null; seen = null; }
527
    }
528

    
529

    
530
  }
531

    
532
  namespace Safety
533
  {
534
    /// <summary>
535
    /// Tests to see if the collection classes are robust for enumerable arguments that throw exceptions.
536
    /// </summary>
537
    [TestFixture]
538
    public class BadEnumerable
539
    {
540
      private HashedArrayList<int> list;
541

    
542
      [SetUp]
543
      public void Init()
544
      {
545
        list = new HashedArrayList<int>();
546
      }
547

    
548
      [Test]
549
      public void InsertAll()
550
      {
551
        list.Add(4); list.Add(56); list.Add(18);
552
        try
553
        {
554
          list.InsertAll<int>(1, new BadEnumerable<int>(new BadEnumerableException(), 91, 81, 71));
555
          Assert.Fail("Should not get here");
556
        }
557
        catch (BadEnumerableException) { }
558
        Assert.IsTrue(list.Check());
559
        Assert.IsTrue(IC.eq(list, 4, 91, 81, 71, 56, 18));
560
      }
561

    
562
      [Test]
563
      public void AddAll()
564
      {
565
        list.Add(4); list.Add(56); list.Add(18);
566
        try
567
        {
568
          list.View(0, 1).AddAll<int>(new BadEnumerable<int>(new BadEnumerableException(), 91, 81, 71));
569
          Assert.Fail("Should not get here");
570
        }
571
        catch (BadEnumerableException) { }
572
        Assert.IsTrue(list.Check());
573
        Assert.IsTrue(IC.eq(list, 4, 91, 81, 71, 56, 18));
574
      }
575

    
576
      [Test]
577
      public void RemoveAll()
578
      {
579
        list.Add(4); list.Add(56); list.Add(18);
580
        try
581
        {
582
          list.RemoveAll(new BadEnumerable<int>(new BadEnumerableException(), 9, 8, 7));
583
          Assert.Fail("Should not get here");
584
        }
585
        catch (BadEnumerableException) { }
586
        Assert.IsTrue(list.Check());
587
        Assert.IsTrue(IC.eq(list, 4, 56, 18));
588
      }
589

    
590
      [Test]
591
      public void RetainAll()
592
      {
593
        list.Add(4); list.Add(56); list.Add(18);
594
        try
595
        {
596
          list.RetainAll(new BadEnumerable<int>(new BadEnumerableException(), 9, 8, 7));
597
          Assert.Fail("Should not get here");
598
        }
599
        catch (BadEnumerableException) { }
600
        Assert.IsTrue(list.Check());
601
        Assert.IsTrue(IC.eq(list, 4, 56, 18));
602
      }
603

    
604

    
605
      [Test]
606
      public void ContainsAll()
607
      {
608
        list.Add(4); list.Add(56); list.Add(18);
609
        try
610
        {
611
          list.ContainsAll(new BadEnumerable<int>(new BadEnumerableException(), 4, 18));
612
          Assert.Fail("Should not get here");
613
        }
614
        catch (BadEnumerableException) { }
615
        Assert.IsTrue(list.Check());
616
        Assert.IsTrue(IC.eq(list, 4, 56, 18));
617
      }
618

    
619

    
620
      [TearDown]
621
      public void Dispose() { list = null; }
622
    }
623

    
624
    /// <summary>
625
    /// Tests to see if the collection classes are robust for delegate arguments that throw exceptions.
626
    /// </summary>
627
    [TestFixture]
628
    public class BadFun
629
    {
630
      private HashedArrayList<int> list;
631

    
632
      [SetUp]
633
      public void Init()
634
      {
635
        list = new HashedArrayList<int>();
636
      }
637

    
638
      [Test]
639
      public void NoTests() { }
640

    
641
      [TearDown]
642
      public void Dispose() { list = null; }
643
    }
644
  }
645

    
646

    
647
  namespace Enumerable
648
  {
649
    [TestFixture]
650
    public class Multiops
651
    {
652
      private HashedArrayList<int> list;
653

    
654
      private Fun<int, bool> always, never, even;
655

    
656

    
657
      [SetUp]
658
      public void Init()
659
      {
660
        list = new HashedArrayList<int>();
661
        always = delegate { return true; };
662
        never = delegate { return false; };
663
        even = delegate(int i) { return i % 2 == 0; };
664
      }
665

    
666

    
667
      [Test]
668
      public void All()
669
      {
670
        Assert.IsTrue(list.All(always));
671
        Assert.IsTrue(list.All(never));
672
        Assert.IsTrue(list.All(even));
673
        list.Add(8);
674
        Assert.IsTrue(list.All(always));
675
        Assert.IsFalse(list.All(never));
676
        Assert.IsTrue(list.All(even));
677
        list.Add(5);
678
        Assert.IsTrue(list.All(always));
679
        Assert.IsFalse(list.All(never));
680
        Assert.IsFalse(list.All(even));
681
      }
682

    
683

    
684
      [Test]
685
      public void Exists()
686
      {
687
        Assert.IsFalse(list.Exists(always));
688
        Assert.IsFalse(list.Exists(never));
689
        Assert.IsFalse(list.Exists(even));
690
        list.Add(5);
691
        Assert.IsTrue(list.Exists(always));
692
        Assert.IsFalse(list.Exists(never));
693
        Assert.IsFalse(list.Exists(even));
694
        list.Add(8);
695
        Assert.IsTrue(list.Exists(always));
696
        Assert.IsFalse(list.Exists(never));
697
        Assert.IsTrue(list.Exists(even));
698
      }
699

    
700

    
701
      [Test]
702
      public void Apply()
703
      {
704
        int sum = 0;
705
        Act<int> a = delegate(int i) { sum = i + 10 * sum; };
706

    
707
        list.Apply(a);
708
        Assert.AreEqual(0, sum);
709
        sum = 0;
710
        list.Add(5); list.Add(8); list.Add(7); list.Add(5);
711
        list.Apply(a);
712
        Assert.AreEqual(587, sum);
713
      }
714

    
715

    
716
      [TearDown]
717
      public void Dispose() { list = null; }
718
    }
719

    
720

    
721

    
722
    [TestFixture]
723
    public class GetEnumerator
724
    {
725
      private HashedArrayList<int> list;
726

    
727

    
728
      [SetUp]
729
      public void Init() { list = new HashedArrayList<int>(); }
730

    
731

    
732
      [Test]
733
      public void Empty()
734
      {
735
        SCG.IEnumerator<int> e = list.GetEnumerator();
736

    
737
        Assert.IsFalse(e.MoveNext());
738
      }
739

    
740

    
741
      [Test]
742
      public void Normal()
743
      {
744
        list.Add(5);
745
        list.Add(8);
746
        list.Add(5);
747
        list.Add(5);
748
        list.Add(10);
749
        list.Add(1);
750

    
751
        SCG.IEnumerator<int> e = list.GetEnumerator();
752

    
753
        Assert.IsTrue(e.MoveNext());
754
        Assert.AreEqual(5, e.Current);
755
        Assert.IsTrue(e.MoveNext());
756
        Assert.AreEqual(8, e.Current);
757
        Assert.IsTrue(e.MoveNext());
758
        Assert.AreEqual(10, e.Current);
759
        Assert.IsTrue(e.MoveNext());
760
        Assert.AreEqual(1, e.Current);
761
        Assert.IsFalse(e.MoveNext());
762
      }
763

    
764

    
765
      [Test]
766
      public void DoDispose()
767
      {
768
        list.Add(5);
769
        list.Add(8);
770
        list.Add(5);
771

    
772
        SCG.IEnumerator<int> e = list.GetEnumerator();
773

    
774
        e.MoveNext();
775
        e.MoveNext();
776
        e.Dispose();
777
      }
778

    
779

    
780
      [Test]
781
      [ExpectedException(typeof(CollectionModifiedException))]
782
      public void MoveNextAfterUpdate()
783
      {
784
        list.Add(5);
785
        list.Add(8);
786
        list.Add(5);
787

    
788
        SCG.IEnumerator<int> e = list.GetEnumerator();
789

    
790
        e.MoveNext();
791
        list.Add(99);
792
        e.MoveNext();
793
      }
794

    
795

    
796

    
797
      [TearDown]
798
      public void Dispose() { list = null; }
799
    }
800
  }
801

    
802
  namespace CollectionOrSink
803
  {
804
    [TestFixture]
805
    public class Formatting
806
    {
807
      ICollection<int> coll;
808
      IFormatProvider rad16;
809
      [SetUp]
810
      public void Init() { coll = Factory.New<int>(); rad16 = new RadixFormatProvider(16); }
811
      [TearDown]
812
      public void Dispose() { coll = null; rad16 = null; }
813
      [Test]
814
      public void Format()
815
      {
816
        Assert.AreEqual("[  ]", coll.ToString());
817
        coll.AddAll<int>(new int[] { -4, 28, 129, 65530 });
818
        Assert.AreEqual("[ 0:-4, 1:28, 2:129, 3:65530 ]", coll.ToString());
819
        Assert.AreEqual("[ 0:-4, 1:1C, 2:81, 3:FFFA ]", coll.ToString(null, rad16));
820
        Assert.AreEqual("[ 0:-4, 1:28... ]", coll.ToString("L14", null));
821
        Assert.AreEqual("[ 0:-4, 1:1C... ]", coll.ToString("L14", rad16));
822
      }
823
    }
824

    
825
    [TestFixture]
826
    public class CollectionOrSink
827
    {
828
      private HashedArrayList<int> list;
829

    
830

    
831
      [SetUp]
832
      public void Init() { list = new HashedArrayList<int>(); }
833

    
834
      [Test]
835
      public void Choose()
836
      {
837
        list.Add(7);
838
        Assert.AreEqual(7, list.Choose());
839
      }
840

    
841
      [Test]
842
      [ExpectedException(typeof(NoSuchItemException))]
843
      public void BadChoose()
844
      {
845
        list.Choose();
846
      }
847

    
848

    
849
      [Test]
850
      public void CountEtAl()
851
      {
852
        Assert.AreEqual(0, list.Count);
853
        Assert.IsTrue(list.IsEmpty);
854
        Assert.IsFalse(list.AllowsDuplicates);
855
        Assert.IsTrue(list.Add(5));
856
        Assert.AreEqual(1, list.Count);
857
        Assert.IsFalse(list.IsEmpty);
858
        Assert.IsFalse(list.Add(5));
859
        Assert.AreEqual(1, list.Count);
860
        Assert.IsFalse(list.IsEmpty);
861
        Assert.IsTrue(list.Add(8));
862
        Assert.AreEqual(2, list.Count);
863
      }
864

    
865

    
866
      [Test]
867
      public void AddAll()
868
      {
869
        list.Add(3); list.Add(4); list.Add(5);
870

    
871
        HashedArrayList<int> list2 = new HashedArrayList<int>();
872

    
873
        list2.AddAll(list);
874
        Assert.IsTrue(IC.eq(list2, 3, 4, 5));
875
        list.AddAll(list2);
876
        Assert.IsTrue(IC.eq(list2, 3, 4, 5));
877
        Assert.IsTrue(IC.eq(list, 3, 4, 5));
878
      }
879

    
880

    
881
      [TearDown]
882
      public void Dispose() { list = null; }
883
    }
884

    
885
    [TestFixture]
886
    public class FindPredicate
887
    {
888
      private HashedArrayList<int> list;
889
      Fun<int, bool> pred;
890

    
891
      [SetUp]
892
      public void Init()
893
      {
894
        list = new HashedArrayList<int>(TenEqualityComparer.Default);
895
        pred = delegate(int i) { return i % 5 == 0; };
896
      }
897

    
898
      [TearDown]
899
      public void Dispose() { list = null; }
900

    
901
      [Test]
902
      public void Find()
903
      {
904
        int i;
905
        Assert.IsFalse(list.Find(pred, out i));
906
        list.AddAll<int>(new int[] { 4, 22, 67, 37 });
907
        Assert.IsFalse(list.Find(pred, out i));
908
        list.AddAll<int>(new int[] { 45, 122, 675, 137 });
909
        Assert.IsTrue(list.Find(pred, out i));
910
        Assert.AreEqual(45, i);
911
      }
912

    
913
      [Test]
914
      public void FindLast()
915
      {
916
        int i;
917
        Assert.IsFalse(list.FindLast(pred, out i));
918
        list.AddAll<int>(new int[] { 4, 22, 67, 37 });
919
        Assert.IsFalse(list.FindLast(pred, out i));
920
        list.AddAll<int>(new int[] { 45, 122, 675, 137 });
921
        Assert.IsTrue(list.FindLast(pred, out i));
922
        Assert.AreEqual(675, i);
923
      }
924

    
925
      [Test]
926
      public void FindIndex()
927
      {
928
        Assert.IsFalse(0 <= list.FindIndex(pred));
929
        list.AddAll<int>(new int[] { 4, 22, 67, 37 });
930
        Assert.IsFalse(0 <= list.FindIndex(pred));
931
        list.AddAll<int>(new int[] { 45, 122, 675, 137 });
932
        Assert.AreEqual(4, list.FindIndex(pred));
933
      }
934

    
935
      [Test]
936
      public void FindLastIndex()
937
      {
938
        Assert.IsFalse(0 <= list.FindLastIndex(pred));
939
        list.AddAll<int>(new int[] { 4, 22, 67, 37 });
940
        Assert.IsFalse(0 <= list.FindLastIndex(pred));
941
        list.AddAll<int>(new int[] { 45, 122, 675, 137 });
942
        Assert.AreEqual(6, list.FindLastIndex(pred));
943
      }
944
    }
945

    
946
    [TestFixture]
947
    public class UniqueItems
948
    {
949
      private HashedArrayList<int> list;
950

    
951
      [SetUp]
952
      public void Init() { list = new HashedArrayList<int>(); }
953

    
954
      [TearDown]
955
      public void Dispose() { list = null; }
956

    
957
      [Test]
958
      public void Test()
959
      {
960
        Assert.IsTrue(IC.seteq(list.UniqueItems()));
961
        Assert.IsTrue(IC.seteq(list.ItemMultiplicities()));
962
        list.AddAll<int>(new int[] { 7, 9, 7 });
963
        Assert.IsTrue(IC.seteq(list.UniqueItems(), 7, 9));
964
        Assert.IsTrue(IC.seteq(list.ItemMultiplicities(), 7, 1, 9, 1));
965
      }
966
    }
967

    
968
    [TestFixture]
969
    public class ArrayTest
970
    {
971
      private HashedArrayList<int> list;
972

    
973
      int[] a;
974

    
975

    
976
      [SetUp]
977
      public void Init()
978
      {
979
        list = new HashedArrayList<int>();
980
        a = new int[10];
981
        for (int i = 0; i < 10; i++)
982
          a[i] = 1000 + i;
983
      }
984

    
985

    
986
      [TearDown]
987
      public void Dispose() { list = null; }
988

    
989

    
990
      private string aeq(int[] a, params int[] b)
991
      {
992
        if (a.Length != b.Length)
993
          return "Lengths differ: " + a.Length + " != " + b.Length;
994

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

    
999
        return "Alles klar";
1000
      }
1001

    
1002

    
1003
      [Test]
1004
      public void ToArray()
1005
      {
1006
        Assert.AreEqual("Alles klar", aeq(list.ToArray()));
1007
        list.Add(7);
1008
        list.Add(8);
1009
        Assert.AreEqual("Alles klar", aeq(list.ToArray(), 7, 8));
1010
      }
1011

    
1012

    
1013
      [Test]
1014
      public void CopyTo()
1015
      {
1016
        list.CopyTo(a, 1);
1017
        Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009));
1018
        list.Add(6);
1019
        list.CopyTo(a, 2);
1020
        Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 1003, 1004, 1005, 1006, 1007, 1008, 1009));
1021
        list.Add(4);
1022
        list.Add(5);
1023
        list.Add(9);
1024
        list.CopyTo(a, 4);
1025
        Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 1003, 6, 4, 5, 9, 1008, 1009));
1026
        list.Clear();
1027
        list.Add(7);
1028
        list.CopyTo(a, 9);
1029
        Assert.AreEqual("Alles klar", aeq(a, 1000, 1001, 6, 1003, 6, 4, 5, 9, 1008, 7));
1030
      }
1031

    
1032

    
1033
      [Test]
1034
      [ExpectedException(typeof(ArgumentOutOfRangeException))]
1035
      public void CopyToBad()
1036
      {
1037
        list.CopyTo(a, 11);
1038
      }
1039

    
1040

    
1041
      [Test]
1042
      [ExpectedException(typeof(ArgumentOutOfRangeException))]
1043
      public void CopyToBad2()
1044
      {
1045
        list.CopyTo(a, -1);
1046
      }
1047

    
1048

    
1049
      [Test]
1050
      [ExpectedException(typeof(ArgumentOutOfRangeException))]
1051
      public void CopyToTooFar()
1052
      {
1053
        list.Add(3);
1054
        list.Add(4);
1055
        list.CopyTo(a, 9);
1056
      }
1057
    }
1058

    
1059

    
1060

    
1061
    [TestFixture]
1062
    public class Sync
1063
    {
1064
      private HashedArrayList<int> list;
1065

    
1066

    
1067
      [SetUp]
1068
      public void Init()
1069
      {
1070
        list = new HashedArrayList<int>();
1071
      }
1072

    
1073

    
1074
      [TearDown]
1075
      public void Dispose() { list = null; }
1076

    
1077

    
1078
      [Test]
1079
      public void Get()
1080
      {
1081
        Assert.IsNotNull(((System.Collections.IList)list).SyncRoot);
1082
      }
1083
    }
1084
  }
1085

    
1086

    
1087

    
1088

    
1089
  namespace EditableCollection
1090
  {
1091
    [TestFixture]
1092
    public class Searching
1093
    {
1094
      private HashedArrayList<int> list;
1095

    
1096

    
1097
      [SetUp]
1098
      public void Init() { list = new HashedArrayList<int>(); }
1099

    
1100

    
1101
      [Test]
1102
      [ExpectedException(typeof(NullReferenceException))]
1103
      public void NullEqualityComparerinConstructor1()
1104
      {
1105
        new HashedArrayList<int>(null);
1106
      }
1107

    
1108
      [Test]
1109
      [ExpectedException(typeof(NullReferenceException))]
1110
      public void NullEqualityComparerinConstructor2()
1111
      {
1112
        new HashedArrayList<int>(5, null);
1113
      }
1114

    
1115
      [Test]
1116
      public void Contains()
1117
      {
1118
        Assert.IsFalse(list.Contains(5));
1119
        list.Add(5);
1120
        Assert.IsTrue(list.Contains(5));
1121
        Assert.IsFalse(list.Contains(7));
1122
        list.Add(8);
1123
        list.Add(10);
1124
        Assert.IsTrue(list.Contains(5));
1125
        Assert.IsFalse(list.Contains(7));
1126
        Assert.IsTrue(list.Contains(8));
1127
        Assert.IsTrue(list.Contains(10));
1128
        list.Remove(8);
1129
        Assert.IsTrue(list.Contains(5));
1130
        Assert.IsFalse(list.Contains(7));
1131
        Assert.IsFalse(list.Contains(8));
1132
        Assert.IsTrue(list.Contains(10));
1133
      }
1134

    
1135
      [Test]
1136
      public void BadAdd()
1137
      {
1138
        Assert.IsTrue(list.Add(5));
1139
        Assert.IsTrue(list.Add(8));
1140
        Assert.IsFalse(list.Add(5));
1141
      }
1142

    
1143

    
1144
      [Test]
1145
      public void ContainsCount()
1146
      {
1147
        Assert.AreEqual(0, list.ContainsCount(5));
1148
        list.Add(5);
1149
        Assert.AreEqual(1, list.ContainsCount(5));
1150
        Assert.AreEqual(0, list.ContainsCount(7));
1151
        list.Add(8);
1152
        Assert.AreEqual(1, list.ContainsCount(5));
1153
        Assert.AreEqual(0, list.ContainsCount(7));
1154
        Assert.AreEqual(1, list.ContainsCount(8));
1155
      }
1156

    
1157

    
1158
      [Test]
1159
      public void RemoveAllCopies()
1160
      {
1161
        list.Add(5); list.Add(7);
1162
        Assert.AreEqual(1, list.ContainsCount(5));
1163
        Assert.AreEqual(1, list.ContainsCount(7));
1164
        list.RemoveAllCopies(5);
1165
        Assert.AreEqual(0, list.ContainsCount(5));
1166
        Assert.AreEqual(1, list.ContainsCount(7));
1167
        list.Add(5); list.Add(8);
1168
        list.RemoveAllCopies(8);
1169
        Assert.IsTrue(IC.eq(list, 7, 5));
1170
      }
1171

    
1172

    
1173
      [Test]
1174
      public void FindAll()
1175
      {
1176
        Fun<int, bool> f = delegate(int i) { return i % 2 == 0; };
1177

    
1178
        Assert.IsTrue(list.FindAll(f).IsEmpty);
1179
        list.Add(5); list.Add(8); list.Add(10);
1180
        Assert.IsTrue(((HashedArrayList<int>)list.FindAll(f)).Check());
1181
        Assert.IsTrue(IC.eq(list.FindAll(f), 8, 10));
1182
      }
1183

    
1184

    
1185
      [Test]
1186
      public void ContainsAll()
1187
      {
1188
        HashedArrayList<int> list2 = new HashedArrayList<int>();
1189

    
1190
        Assert.IsTrue(list.ContainsAll(list2));
1191
        list2.Add(4);
1192
        Assert.IsFalse(list.ContainsAll(list2));
1193
        list.Add(4);
1194
        Assert.IsTrue(list.ContainsAll(list2));
1195
        list.Add(5);
1196
        Assert.IsTrue(list.ContainsAll(list2));
1197
      }
1198

    
1199

    
1200
      [Test]
1201
      public void RetainAll()
1202
      {
1203
        HashedArrayList<int> list2 = new HashedArrayList<int>();
1204

    
1205
        list.Add(4); list.Add(5); list.Add(6);
1206
        list2.Add(5); list2.Add(4); list2.Add(7);
1207
        list.RetainAll(list2);
1208
        Assert.IsTrue(list.Check());
1209
        Assert.IsTrue(IC.eq(list, 4, 5));
1210
        list.Add(5); list.Add(4); list.Add(6);
1211
        list2.Clear();
1212
        list2.Add(5); list2.Add(5); list2.Add(6);
1213
        list.RetainAll(list2);
1214
        Assert.IsTrue(list.Check());
1215
        Assert.IsTrue(IC.eq(list, 5, 6));
1216
        list2.Clear();
1217
        list2.Add(7); list2.Add(8); list2.Add(9);
1218
        list.RetainAll(list2);
1219
        Assert.IsTrue(list.Check());
1220
        Assert.IsTrue(IC.eq(list));
1221
      }
1222

    
1223

    
1224
      [Test]
1225
      public void RemoveAll()
1226
      {
1227
        HashedArrayList<int> list2 = new HashedArrayList<int>();
1228

    
1229
        list.Add(4); list.Add(5); list.Add(6);
1230
        list2.Add(5); list2.Add(4); list2.Add(7);
1231
        list.RemoveAll(list2);
1232
        Assert.IsTrue(list.Check());
1233
        Assert.IsTrue(IC.eq(list, 6));
1234
        list.Add(5); list.Add(4); list.Add(6);
1235
        list2.Clear();
1236
        list2.Add(6); list2.Add(5);
1237
        list.RemoveAll(list2);
1238
        Assert.IsTrue(list.Check());
1239
        Assert.IsTrue(IC.eq(list, 4));
1240
        list2.Clear();
1241
        list2.Add(7); list2.Add(8); list2.Add(9);
1242
        list.RemoveAll(list2);
1243
        Assert.IsTrue(list.Check());
1244
        Assert.IsTrue(IC.eq(list, 4));
1245
      }
1246

    
1247

    
1248
      [Test]
1249
      public void Remove()
1250
      {
1251
        list.Add(4); list.Add(5); list.Add(6);
1252
        Assert.IsFalse(list.Remove(2));
1253
        Assert.IsTrue(list.Check());
1254
        Assert.IsTrue(list.Remove(4));
1255
        Assert.IsTrue(list.Check());
1256
        Assert.IsTrue(IC.eq(list, 5, 6));
1257
        Assert.AreEqual(6, list.RemoveLast());
1258
        Assert.IsTrue(list.Check());
1259
        Assert.IsTrue(IC.eq(list, 5));
1260
        list.Add(7);
1261
        Assert.AreEqual(5, list.RemoveFirst());
1262
        Assert.IsTrue(list.Check());
1263
        Assert.IsTrue(IC.eq(list, 7));
1264
      }
1265

    
1266

    
1267
      [Test]
1268
      public void Clear()
1269
      {
1270
        list.Add(7); list.Add(6);
1271
        list.Clear();
1272
        Assert.IsTrue(list.IsEmpty);
1273
      }
1274

    
1275

    
1276
      [TearDown]
1277
      public void Dispose() { list = null; }
1278
    }
1279
  }
1280

    
1281

    
1282

    
1283

    
1284
  namespace IIndexed
1285
  {
1286
    [TestFixture]
1287
    public class Searching
1288
    {
1289
      private IIndexed<int> dit;
1290

    
1291

    
1292
      [SetUp]
1293
      public void Init()
1294
      {
1295
        dit = new HashedArrayList<int>();
1296
      }
1297

    
1298

    
1299
      [Test]
1300
      public void IndexOf()
1301
      {
1302
        Assert.AreEqual(~0, dit.IndexOf(6));
1303
        dit.Add(7);
1304
        Assert.AreEqual(~1, dit.IndexOf(6));
1305
        Assert.AreEqual(~1, dit.LastIndexOf(6));
1306
        Assert.AreEqual(0, dit.IndexOf(7));
1307
        dit.Add(5); dit.Add(7); dit.Add(8); dit.Add(7);
1308
        Assert.AreEqual(~3, dit.IndexOf(6));
1309
        Assert.AreEqual(0, dit.IndexOf(7));
1310
        Assert.AreEqual(0, dit.LastIndexOf(7));
1311
        Assert.AreEqual(2, dit.IndexOf(8));
1312
        Assert.AreEqual(1, dit.LastIndexOf(5));
1313
      }
1314

    
1315

    
1316
      [TearDown]
1317
      public void Dispose()
1318
      {
1319
        dit = null;
1320
      }
1321
    }
1322

    
1323

    
1324

    
1325
    [TestFixture]
1326
    public class Removing
1327
    {
1328
      private IIndexed<int> dit;
1329

    
1330

    
1331
      [SetUp]
1332
      public void Init()
1333
      {
1334
        dit = new HashedArrayList<int>();
1335
      }
1336

    
1337

    
1338
      [Test]
1339
      public void RemoveAt()
1340
      {
1341
        dit.Add(5); dit.Add(7); dit.Add(9); dit.Add(1); dit.Add(2);
1342
        Assert.AreEqual(7, dit.RemoveAt(1));
1343
        Assert.IsTrue(((HashedArrayList<int>)dit).Check());
1344
        Assert.IsTrue(IC.eq(dit, 5, 9, 1, 2));
1345
        Assert.AreEqual(5, dit.RemoveAt(0));
1346
        Assert.IsTrue(((HashedArrayList<int>)dit).Check());
1347
        Assert.IsTrue(IC.eq(dit, 9, 1, 2));
1348
        Assert.AreEqual(2, dit.RemoveAt(2));
1349
        Assert.IsTrue(((HashedArrayList<int>)dit).Check());
1350
        Assert.IsTrue(IC.eq(dit, 9, 1));
1351
      }
1352

    
1353

    
1354
      [Test]
1355
      [ExpectedException(typeof(IndexOutOfRangeException))]
1356
      public void RemoveAtBad0()
1357
      {
1358
        dit.RemoveAt(0);
1359
      }
1360

    
1361

    
1362
      [Test]
1363
      [ExpectedException(typeof(IndexOutOfRangeException))]
1364
      public void RemoveAtBadM1()
1365
      {
1366
        dit.RemoveAt(-1);
1367
      }
1368

    
1369

    
1370
      [Test]
1371
      [ExpectedException(typeof(IndexOutOfRangeException))]
1372
      public void RemoveAtBad1()
1373
      {
1374
        dit.Add(8);
1375
        dit.RemoveAt(1);
1376
      }
1377

    
1378

    
1379
      [Test]
1380
      public void RemoveInterval()
1381
      {
1382
        dit.RemoveInterval(0, 0);
1383
        dit.Add(10); dit.Add(20); dit.Add(30); dit.Add(40); dit.Add(50); dit.Add(60);
1384
        dit.RemoveInterval(3, 0);
1385
        Assert.IsTrue(((HashedArrayList<int>)dit).Check());
1386
        Assert.IsTrue(IC.eq(dit, 10, 20, 30, 40, 50, 60));
1387
        dit.RemoveInterval(3, 1);
1388
        Assert.IsTrue(((HashedArrayList<int>)dit).Check());
1389
        Assert.IsTrue(IC.eq(dit, 10, 20, 30, 50, 60));
1390
        dit.RemoveInterval(1, 3);
1391
        Assert.IsTrue(((HashedArrayList<int>)dit).Check());
1392
        Assert.IsTrue(IC.eq(dit, 10, 60));
1393
        dit.RemoveInterval(0, 2);
1394
        Assert.IsTrue(((HashedArrayList<int>)dit).Check());
1395
        Assert.IsTrue(IC.eq(dit));
1396
        dit.Add(10); dit.Add(20); dit.Add(30); dit.Add(40); dit.Add(50); dit.Add(60);
1397
        dit.RemoveInterval(0, 2);
1398
        Assert.IsTrue(((HashedArrayList<int>)dit).Check());
1399
        Assert.IsTrue(IC.eq(dit, 30, 40, 50, 60));
1400
        dit.RemoveInterval(2, 2);
1401
        Assert.IsTrue(((HashedArrayList<int>)dit).Check());
1402
        Assert.IsTrue(IC.eq(dit, 30, 40));
1403
      }
1404

    
1405

    
1406
      [TearDown]
1407
      public void Dispose()
1408
      {
1409
        dit = null;
1410
      }
1411
    }
1412
  }
1413

    
1414

    
1415

    
1416

    
1417
  namespace IList
1418
  {
1419
    [TestFixture]
1420
    public class Searching
1421
    {
1422
      private IList<int> lst;
1423

    
1424

    
1425
      [SetUp]
1426
      public void Init() { lst = new HashedArrayList<int>(); }
1427

    
1428

    
1429
      [TearDown]
1430
      public void Dispose() { lst = null; }
1431

    
1432

    
1433
      [Test]
1434
      [ExpectedException(typeof(NoSuchItemException))]
1435
      public void FirstBad()
1436
      {
1437
        int f = lst.First;
1438
      }
1439

    
1440

    
1441
      [Test]
1442
      [ExpectedException(typeof(NoSuchItemException))]
1443
      public void LastBad()
1444
      {
1445
        int f = lst.Last;
1446
      }
1447

    
1448

    
1449
      [Test]
1450
      public void FirstLast()
1451
      {
1452
        lst.Add(19);
1453
        Assert.AreEqual(19, lst.First);
1454
        Assert.AreEqual(19, lst.Last);
1455
        lst.Add(34); lst.InsertFirst(12);
1456
        Assert.AreEqual(12, lst.First);
1457
        Assert.AreEqual(34, lst.Last);
1458
      }
1459

    
1460

    
1461
      [Test]
1462
      public void This()
1463
      {
1464
        lst.Add(34);
1465
        Assert.AreEqual(34, lst[0]);
1466
        lst[0] = 56;
1467
        Assert.AreEqual(56, lst.First);
1468
        lst.Add(7); lst.Add(77); lst.Add(777); lst.Add(7777);
1469
        lst[0] = 45; lst[2] = 78; lst[4] = 101;
1470
        Assert.IsTrue(IC.eq(lst, 45, 7, 78, 777, 101));
1471
      }
1472

    
1473
      [Test]
1474
      public void ThisWithUpdates()
1475
      {
1476
        HashedArrayList<KeyValuePair<int, int>> pairlist = new HashedArrayList<KeyValuePair<int, int>>(new KeyValuePairEqualityComparer<int, int>());
1477
        pairlist.Add(new KeyValuePair<int, int>(10, 50));
1478
        pairlist.Add(new KeyValuePair<int, int>(11, 51));
1479
        pairlist.Add(new KeyValuePair<int, int>(12, 52));
1480
        pairlist.Add(new KeyValuePair<int, int>(13, 53));
1481
        pairlist[2] = new KeyValuePair<int, int>(12, 102);
1482
        Assert.IsTrue(pairlist.Check());
1483
        Assert.AreEqual(new KeyValuePair<int, int>(12, 102), pairlist[2]);
1484
        pairlist[2] = new KeyValuePair<int, int>(22, 202);
1485
        Assert.IsTrue(pairlist.Check());
1486
        Assert.AreEqual(new KeyValuePair<int, int>(22, 202), pairlist[2]);
1487
        pairlist[1] = new KeyValuePair<int, int>(12, 303);
1488
        Assert.IsTrue(pairlist.Check());
1489
        Assert.AreEqual(new KeyValuePair<int, int>(12, 303), pairlist[1]);
1490
        Assert.AreEqual(new KeyValuePair<int, int>(22, 202), pairlist[2]);
1491
      }
1492

    
1493
      [Test]
1494
      [ExpectedException(typeof(DuplicateNotAllowedException))]
1495
      public void ThisWithUpdatesBad()
1496
      {
1497
        HashedArrayList<KeyValuePair<int, int>> pairlist = new HashedArrayList<KeyValuePair<int, int>>(new KeyValuePairEqualityComparer<int, int>());
1498
        pairlist.Add(new KeyValuePair<int, int>(10, 50));
1499
        pairlist.Add(new KeyValuePair<int, int>(11, 51));
1500
        pairlist.Add(new KeyValuePair<int, int>(12, 52));
1501
        pairlist.Add(new KeyValuePair<int, int>(13, 53));
1502
        pairlist[2] = new KeyValuePair<int, int>(11, 102);
1503
      }
1504

    
1505

    
1506

    
1507
      [Test]
1508
      [ExpectedException(typeof(IndexOutOfRangeException))]
1509
      public void ThisBadEmptyGet()
1510
      {
1511
        int f = lst[0];
1512
      }
1513

    
1514

    
1515
      [Test]
1516
      [ExpectedException(typeof(IndexOutOfRangeException))]
1517
      public void ThisBadLowGet()
1518
      {
1519
        lst.Add(7);
1520

    
1521
        int f = lst[-1];
1522
      }
1523

    
1524

    
1525
      [Test]
1526
      [ExpectedException(typeof(IndexOutOfRangeException))]
1527
      public void ThisBadHiGet()
1528
      {
1529
        lst.Add(6);
1530

    
1531
        int f = lst[1];
1532
      }
1533

    
1534

    
1535
      [Test]
1536
      [ExpectedException(typeof(IndexOutOfRangeException))]
1537
      public void ThisBadEmptySet()
1538
      {
1539
        lst[0] = 4;
1540
      }
1541

    
1542

    
1543
      [Test]
1544
      [ExpectedException(typeof(IndexOutOfRangeException))]
1545
      public void ThisBadLowSet()
1546
      {
1547
        lst.Add(7);
1548
        lst[-1] = 9;
1549
      }
1550

    
1551

    
1552
      [Test]
1553
      [ExpectedException(typeof(IndexOutOfRangeException))]
1554
      public void ThisBadHiSet()
1555
      {
1556
        lst.Add(6);
1557
        lst[1] = 11;
1558
      }
1559
    }
1560

    
1561

    
1562

    
1563
    [TestFixture]
1564
    public class Inserting
1565
    {
1566
      private IList<int> lst;
1567

    
1568

    
1569
      [SetUp]
1570
      public void Init() { lst = new HashedArrayList<int>(); }
1571

    
1572

    
1573
      [TearDown]
1574
      public void Dispose() { lst = null; }
1575

    
1576

    
1577
      [Test]
1578
      public void Insert()
1579
      {
1580
        lst.Insert(0, 5);
1581
        Assert.IsTrue(IC.eq(lst, 5));
1582
        lst.Insert(0, 7);
1583
        Assert.IsTrue(IC.eq(lst, 7, 5));
1584
        lst.Insert(1, 4);
1585
        Assert.IsTrue(IC.eq(lst, 7, 4, 5));
1586
        lst.Insert(3, 2);
1587
        Assert.IsTrue(IC.eq(lst, 7, 4, 5, 2));
1588
      }
1589

    
1590
      [Test]
1591
      [ExpectedException(typeof(DuplicateNotAllowedException))]
1592
      public void InsertDuplicate()
1593
      {
1594
        lst.Insert(0, 5);
1595
        Assert.IsTrue(IC.eq(lst, 5));
1596
        lst.Insert(0, 7);
1597
        Assert.IsTrue(IC.eq(lst, 7, 5));
1598
        lst.Insert(1, 5);
1599
      }
1600

    
1601
      [Test]
1602
      public void InsertAllDuplicate1()
1603
      {
1604
        lst.Insert(0, 3);
1605
        Assert.IsTrue(IC.eq(lst, 3));
1606
        lst.Insert(0, 7);
1607
        Assert.IsTrue(IC.eq(lst, 7, 3));
1608
        try
1609
        {
1610
          lst.InsertAll<int>(1, new int[] { 1, 2, 3, 4 });
1611
        }
1612
        catch (DuplicateNotAllowedException)
1613
        {
1614
        }
1615
        Assert.IsTrue(lst.Check());
1616
        Assert.IsTrue(IC.eq(lst, 7, 1, 2, 3));
1617
      }
1618

    
1619
      [Test]
1620
      public void InsertAllDuplicate2()
1621
      {
1622
        lst.Insert(0, 3);
1623
        Assert.IsTrue(IC.eq(lst, 3));
1624
        lst.Insert(0, 7);
1625
        Assert.IsTrue(IC.eq(lst, 7, 3));
1626
        try
1627
        {
1628
          lst.InsertAll<int>(1, new int[] { 5, 6, 5, 8 });
1629
        }
1630
        catch (DuplicateNotAllowedException)
1631
        {
1632
        }
1633
        Assert.IsTrue(lst.Check());
1634
        Assert.IsTrue(IC.eq(lst, 7, 5, 6, 3));
1635
      }
1636

    
1637

    
1638
      [Test]
1639
      [ExpectedException(typeof(IndexOutOfRangeException))]
1640
      public void BadInsertLow()
1641
      {
1642
        lst.Add(7);
1643
        lst.Insert(-1, 9);
1644
      }
1645

    
1646

    
1647
      [Test]
1648
      [ExpectedException(typeof(IndexOutOfRangeException))]
1649
      public void BadInsertHi()
1650
      {
1651
        lst.Add(6);
1652
        lst.Insert(2, 11);
1653
      }
1654

    
1655

    
1656
      [Test]
1657
      public void FIFO()
1658
      {
1659
        for (int i = 0; i < 7; i++)
1660
          lst.Add(2 * i);
1661

    
1662
        Assert.IsFalse(lst.FIFO);
1663
        Assert.AreEqual(12, lst.Remove());
1664
        Assert.AreEqual(10, lst.Remove());
1665
        lst.FIFO = true;
1666
        Assert.AreEqual(0, lst.Remove());
1667
        Assert.AreEqual(2, lst.Remove());
1668
        lst.FIFO = false;
1669
        Assert.AreEqual(8, lst.Remove());
1670
        Assert.AreEqual(6, lst.Remove());
1671
      }
1672

    
1673

    
1674
      [Test]
1675
      public void InsertFirstLast()
1676
      {
1677
        lst.InsertFirst(4);
1678
        lst.InsertLast(5);
1679
        lst.InsertFirst(14);
1680
        lst.InsertLast(15);
1681
        lst.InsertFirst(24);
1682
        lst.InsertLast(25);
1683
        lst.InsertFirst(34);
1684
        lst.InsertLast(55);
1685
        Assert.IsTrue(IC.eq(lst, 34, 24, 14, 4, 5, 15, 25, 55));
1686
      }
1687

    
1688

    
1689
      [Test]
1690
      public void InsertFirst()
1691
      {
1692
        lst.Add(2);
1693
        lst.Add(3);
1694
        lst.Add(4);
1695
        lst.Add(5);
1696
        lst.ViewOf(2).InsertFirst(7);
1697
        Assert.IsTrue(lst.Check());
1698
        Assert.IsTrue(IC.eq(lst, 7, 2, 3, 4, 5));
1699
        lst.ViewOf(3).InsertFirst(8);
1700
        Assert.IsTrue(lst.Check());
1701
        Assert.IsTrue(IC.eq(lst, 7, 2, 8, 3, 4, 5));
1702
        lst.ViewOf(5).InsertFirst(9);
1703
        Assert.IsTrue(lst.Check());
1704
        Assert.IsTrue(IC.eq(lst, 7, 2, 8, 3, 4, 9, 5));
1705
      }
1706

    
1707

    
1708
      [Test]
1709
      public void BadFirst()
1710
      {
1711
        lst.Add(2);
1712
        lst.Add(3);
1713
        lst.Add(2);
1714
        lst.Add(5);
1715
        Assert.IsNull(lst.ViewOf(4));
1716
      }
1717

    
1718

    
1719
      [Test]
1720
      public void InsertAfter()
1721
      {
1722
        lst.Add(1);
1723
        lst.Add(2);
1724
        lst.Add(3);
1725
        lst.Add(4);
1726
        lst.Add(5);
1727
        lst.LastViewOf(2).InsertLast(7);
1728
        Assert.IsTrue(lst.Check());
1729
        Assert.IsTrue(IC.eq(lst, 1, 2, 7, 3, 4, 5));
1730
        lst.LastViewOf(1).InsertLast(8);
1731
        Assert.IsTrue(lst.Check());
1732
        Assert.IsTrue(IC.eq(lst, 1, 8, 2, 7, 3, 4, 5));
1733
        lst.LastViewOf(5).InsertLast(9);
1734
        Assert.IsTrue(lst.Check());
1735
        Assert.IsTrue(IC.eq(lst, 1, 8, 2, 7, 3, 4, 5, 9));
1736
      }
1737

    
1738

    
1739
      [Test]
1740
      public void BadInsertAfter()
1741
      {
1742
        lst.Add(2);
1743
        lst.Add(3);
1744
        lst.Add(6);
1745
        lst.Add(5);
1746
        Assert.IsNull(lst.ViewOf(4));
1747
      }
1748

    
1749

    
1750
      [Test]
1751
      public void InsertAll()
1752
      {
1753
        lst.Add(1);
1754
        lst.Add(2);
1755
        lst.Add(3);
1756
        lst.Add(4);
1757

    
1758
        IList<int> lst2 = new HashedArrayList<int>();
1759

    
1760
        lst2.Add(7); lst2.Add(8); lst2.Add(9);
1761
        lst.InsertAll(0, lst2);
1762
        Assert.IsTrue(lst.Check());
1763
        Assert.IsTrue(IC.eq(lst, 7, 8, 9, 1, 2, 3, 4));
1764
        lst.RemoveAll(lst2);
1765
        lst.InsertAll(4, lst2);
1766
        Assert.IsTrue(lst.Check());
1767
        Assert.IsTrue(IC.eq(lst, 1, 2, 3, 4, 7, 8, 9));
1768
        lst.RemoveAll(lst2);
1769
        lst.InsertAll(2, lst2);
1770
        Assert.IsTrue(lst.Check());
1771
        Assert.IsTrue(IC.eq(lst, 1, 2, 7, 8, 9, 3, 4));
1772
      }
1773

    
1774
      [Test]
1775
      [ExpectedException(typeof(DuplicateNotAllowedException))]
1776
      public void InsertAllBad()
1777
      {
1778
        lst.Add(1);
1779
        lst.Add(2);
1780
        lst.Add(3);
1781
        lst.Add(4);
1782

    
1783
        IList<int> lst2 = new HashedArrayList<int>();
1784

    
1785
        lst2.Add(5); lst2.Add(2); lst2.Add(9);
1786
        lst.InsertAll(0, lst2);
1787
      }
1788

    
1789

    
1790
      [Test]
1791
      public void Map()
1792
      {
1793
        Fun<int, string> m = delegate(int i) { return "<<" + i + ">>"; };
1794
        IList<string> r = lst.Map(m);
1795

    
1796
        Assert.IsTrue(((HashedArrayList<string>)r).Check());
1797
        Assert.IsTrue(r.IsEmpty);
1798
        lst.Add(1);
1799
        lst.Add(2);
1800
        lst.Add(3);
1801
        lst.Add(4);
1802
        r = lst.Map(m);
1803
        Assert.IsTrue(((HashedArrayList<string>)r).Check());
1804
        Assert.AreEqual(4, r.Count);
1805
        for (int i = 0; i < 4; i++)
1806
          Assert.AreEqual("<<" + (i + 1) + ">>", r[i]);
1807
      }
1808
      [Test]
1809
      [ExpectedException(typeof(CollectionModifiedException))]
1810
      public void BadMapper()
1811
      {
1812
        lst.Add(1);
1813
        lst.Add(2);
1814
        lst.Add(3);
1815
        Fun<int, bool> m = delegate(int i) { if (i == 2) lst.Add(7); return true; };
1816
        lst.Map(m);
1817
      }
1818

    
1819
      [Test]
1820
      [ExpectedException(typeof(CollectionModifiedException))]
1821
      public void ModifyingFindAll()
1822
      {
1823
        lst.Add(1);
1824
        lst.Add(2);
1825
        lst.Add(3);
1826
        Fun<int, bool> m = delegate(int i) { if (i == 2) lst.Add(7); return true; };
1827
        lst.FindAll(m);
1828
      }
1829
      [Test]
1830
      [ExpectedException(typeof(CollectionModifiedException))]
1831
      public void BadMapperView()
1832
      {
1833
        lst = lst.View(0, 0);
1834
        lst.Add(1);
1835
        lst.Add(2);
1836
        lst.Add(3);
1837
        Fun<int, bool> m = delegate(int i) { if (i == 2) lst.Add(7); return true; };
1838
        lst.Map(m);
1839
      }
1840

    
1841
      [Test]
1842
      [ExpectedException(typeof(CollectionModifiedException))]
1843
      public void ModifyingFindAllView()
1844
      {
1845
        lst = lst.View(0, 0);
1846
        lst.Add(1);
1847
        lst.Add(2);
1848
        lst.Add(3);
1849
        Fun<int, bool> m = delegate(int i) { if (i == 2) lst.Add(7); return true; };
1850
        lst.FindAll(m);
1851
      }
1852

    
1853

    
1854
      [Test]
1855
      [ExpectedException(typeof(NoSuchItemException))]
1856
      public void BadRemove() { lst.Remove(); }
1857

    
1858
      [Test]
1859
      [ExpectedException(typeof(NoSuchItemException))]
1860
      public void BadRemoveFirst() { lst.RemoveFirst(); }
1861

    
1862
      [Test]
1863
      [ExpectedException(typeof(NoSuchItemException))]
1864
      public void BadRemoveLast() { lst.RemoveLast(); }
1865

    
1866

    
1867
      [Test]
1868
      public void RemoveFirstLast()
1869
      {
1870
        lst.Add(1);
1871
        lst.Add(2);
1872
        lst.Add(3);
1873
        lst.Add(4);
1874
        Assert.AreEqual(1, lst.RemoveFirst());
1875
        Assert.AreEqual(4, lst.RemoveLast());
1876
        Assert.AreEqual(2, lst.RemoveFirst());
1877
        Assert.AreEqual(3, lst.RemoveLast());
1878
        Assert.IsTrue(lst.IsEmpty);
1879
      }
1880

    
1881

    
1882
      [Test]
1883
      public void Reverse()
1884
      {
1885
        for (int i = 0; i < 10; i++)
1886
          lst.Add(i);
1887

    
1888
        lst.Reverse();
1889
        Assert.IsTrue(lst.Check());
1890
        Assert.IsTrue(IC.eq(lst, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0));
1891
        lst.View(0, 3).Reverse();
1892
        Assert.IsTrue(lst.Check());
1893
        Assert.IsTrue(IC.eq(lst, 7, 8, 9, 6, 5, 4, 3, 2, 1, 0));
1894
        lst.View(7, 0).Reverse();
1895
        Assert.IsTrue(lst.Check());
1896
        Assert.IsTrue(IC.eq(lst, 7, 8, 9, 6, 5, 4, 3, 2, 1, 0));
1897
        lst.View(7, 3).Reverse();
1898
        Assert.IsTrue(lst.Check());
1899
        Assert.IsTrue(IC.eq(lst, 7, 8, 9, 6, 5, 4, 3, 0, 1, 2));
1900
        lst.View(5, 1).Reverse();
1901
        Assert.IsTrue(lst.Check());
1902
        Assert.IsTrue(IC.eq(lst, 7, 8, 9, 6, 5, 4, 3, 0, 1, 2));
1903
      }
1904

    
1905

    
1906
      [Test]
1907
      [ExpectedException(typeof(ArgumentOutOfRangeException))]
1908
      public void BadReverse()
1909
      {
1910
        for (int i = 0; i < 10; i++)
1911
          lst.Add(i);
1912

    
1913
        lst.View(8, 3).Reverse();
1914
      }
1915
    }
1916

    
1917

    
1918
    [TestFixture]
1919
    public class Combined
1920
    {
1921
      private IList<KeyValuePair<int, int>> lst;
1922

    
1923

    
1924
      [SetUp]
1925
      public void Init()
1926
      {
1927
        lst = new HashedArrayList<KeyValuePair<int, int>>(new KeyValuePairEqualityComparer<int, int>());
1928
        for (int i = 0; i < 10; i++)
1929
          lst.Add(new KeyValuePair<int, int>(i, i + 30));
1930
      }
1931

    
1932

    
1933
      [TearDown]
1934
      public void Dispose() { lst = null; }
1935

    
1936

    
1937
      [Test]
1938
      public void Find()
1939
      {
1940
        KeyValuePair<int, int> p = new KeyValuePair<int, int>(3, 78);
1941

    
1942
        Assert.IsTrue(lst.Find(ref p));
1943
        Assert.AreEqual(3, p.Key);
1944
        Assert.AreEqual(33, p.Value);
1945
        p = new KeyValuePair<int, int>(13, 78);
1946
        Assert.IsFalse(lst.Find(ref p));
1947
      }
1948

    
1949

    
1950
      [Test]
1951
      public void FindOrAdd()
1952
      {
1953
        KeyValuePair<int, int> p = new KeyValuePair<int, int>(3, 78);
1954

    
1955
        Assert.IsTrue(lst.FindOrAdd(ref p));
1956
        Assert.AreEqual(3, p.Key);
1957
        Assert.AreEqual(33, p.Value);
1958
        p = new KeyValuePair<int, int>(13, 79);
1959
        Assert.IsFalse(lst.FindOrAdd(ref p));
1960
        Assert.AreEqual(13, lst[10].Key);
1961
        Assert.AreEqual(79, lst[10].Value);
1962
      }
1963

    
1964

    
1965
      [Test]
1966
      public void Update()
1967
      {
1968
        KeyValuePair<int, int> p = new KeyValuePair<int, int>(3, 78);
1969

    
1970
        Assert.IsTrue(lst.Update(p));
1971
        Assert.AreEqual(3, lst[3].Key);
1972
        Assert.AreEqual(78, lst[3].Value);
1973
        p = new KeyValuePair<int, int>(13, 78);
1974
        Assert.IsFalse(lst.Update(p));
1975
      }
1976

    
1977

    
1978
      [Test]
1979
      public void UpdateOrAdd1()
1980
      {
1981
        KeyValuePair<int, int> p = new KeyValuePair<int, int>(3, 78);
1982

    
1983
        Assert.IsTrue(lst.UpdateOrAdd(p));
1984
        Assert.AreEqual(3, lst[3].Key);
1985
        Assert.AreEqual(78, lst[3].Value);
1986
        p = new KeyValuePair<int, int>(13, 79);
1987
        Assert.IsFalse(lst.UpdateOrAdd(p));
1988
        Assert.AreEqual(13, lst[10].Key);
1989
        Assert.AreEqual(79, lst[10].Value);
1990
      }
1991

    
1992
      [Test]
1993
      public void UpdateOrAdd2()
1994
      {
1995
          ICollection<String> coll = new HashedArrayList<String>();
1996
          // s1 and s2 are distinct objects but contain the same text:
1997
          String old, s1 = "abc", s2 = ("def" + s1).Substring(3);
1998
          Assert.IsFalse(coll.UpdateOrAdd(s1, out old));
1999
          Assert.AreEqual(null, old);
2000
          Assert.IsTrue(coll.UpdateOrAdd(s2, out old));
2001
          Assert.IsTrue(Object.ReferenceEquals(s1, old));
2002
          Assert.IsFalse(Object.ReferenceEquals(s2, old));
2003
      }
2004

    
2005
      [Test]
2006
      public void RemoveWithReturn()
2007
      {
2008
        KeyValuePair<int, int> p = new KeyValuePair<int, int>(3, 78);
2009

    
2010
        Assert.IsTrue(lst.Remove(p, out p));
2011
        Assert.AreEqual(3, p.Key);
2012
        Assert.AreEqual(33, p.Value);
2013
        Assert.AreEqual(4, lst[3].Key);
2014
        Assert.AreEqual(34, lst[3].Value);
2015
        p = new KeyValuePair<int, int>(13, 78);
2016
        Assert.IsFalse(lst.Remove(p, out p));
2017
      }
2018
    }
2019

    
2020

    
2021
    [TestFixture]
2022
    public class Sorting
2023
    {
2024
      private IList<int> lst;
2025

    
2026

    
2027
      [SetUp]
2028
      public void Init() { lst = new HashedArrayList<int>(); }
2029

    
2030

    
2031
      [TearDown]
2032
      public void Dispose() { lst = null; }
2033

    
2034

    
2035
      [Test]
2036
      public void Sort()
2037
      {
2038
        lst.Add(5); lst.Add(6); lst.Add(55); lst.Add(7); lst.Add(3);
2039
        Assert.IsFalse(lst.IsSorted(new IC()));
2040
        lst.Sort(new IC());
2041
        Assert.IsTrue(lst.IsSorted());
2042
        Assert.IsTrue(lst.IsSorted(new IC()));
2043
        Assert.IsTrue(IC.eq(lst, 3, 5, 6, 7, 55));
2044
      }
2045
    }
2046
  }
2047

    
2048

    
2049

    
2050

    
2051
  namespace Range
2052
  {
2053
    [TestFixture]
2054
    public class Range
2055
    {
2056
      private IList<int> lst;
2057

    
2058

    
2059
      [SetUp]
2060
      public void Init() { lst = new HashedArrayList<int>(); }
2061

    
2062

    
2063
      [TearDown]
2064
      public void Dispose() { lst = null; }
2065

    
2066

    
2067
      [Test]
2068
      public void GetRange()
2069
      {
2070
        //Assert.IsTrue(IC.eq(lst[0, 0)));
2071
        for (int i = 0; i < 10; i++) lst.Add(i);
2072

    
2073
        Assert.IsTrue(IC.eq(lst[0, 3], 0, 1, 2));
2074
        Assert.IsTrue(IC.eq(lst[3, 3], 3, 4, 5));
2075
        Assert.IsTrue(IC.eq(lst[6, 3], 6, 7, 8));
2076
        Assert.IsTrue(IC.eq(lst[6, 4], 6, 7, 8, 9));
2077
      }
2078

    
2079

    
2080
      [Test]
2081
      public void Backwards()
2082
      {
2083
        for (int i = 0; i < 10; i++) lst.Add(i);
2084

    
2085
        Assert.IsTrue(IC.eq(lst.Backwards(), 9, 8, 7, 6, 5, 4, 3, 2, 1, 0));
2086
        Assert.IsTrue(IC.eq(lst[0, 3].Backwards(), 2, 1, 0));
2087
        Assert.IsTrue(IC.eq(lst[3, 3].Backwards(), 5, 4, 3));
2088
        Assert.IsTrue(IC.eq(lst[6, 4].Backwards(), 9, 8, 7, 6));
2089
      }
2090

    
2091

    
2092
      [Test]
2093
      public void DirectionAndCount()
2094
      {
2095
        for (int i = 0; i < 10; i++) lst.Add(i);
2096

    
2097
        Assert.AreEqual(EnumerationDirection.Forwards, lst.Direction);
2098
        Assert.AreEqual(EnumerationDirection.Forwards, lst[3, 4].Direction);
2099
        Assert.AreEqual(EnumerationDirection.Backwards, lst[3, 4].Backwards().Direction);
2100
        Assert.AreEqual(EnumerationDirection.Backwards, lst.Backwards().Direction);
2101
        Assert.AreEqual(4, lst[3, 4].Count);
2102
        Assert.AreEqual(4, lst[3, 4].Backwards().Count);
2103
        Assert.AreEqual(10, lst.Backwards().Count);
2104
      }
2105

    
2106

    
2107
      [Test]
2108
      [ExpectedException(typeof(CollectionModifiedException))]
2109
      public void MoveNextAfterUpdate()
2110
      {
2111
        for (int i = 0; i < 10; i++) lst.Add(i);
2112

    
2113
        foreach (int i in lst)
2114
        {
2115
          lst.Add(45 + i);
2116
        }
2117
      }
2118
    }
2119
  }
2120

    
2121

    
2122

    
2123

    
2124
  namespace View
2125
  {
2126
    [TestFixture]
2127
    public class Simple
2128
    {
2129
      HashedArrayList<int> list;
2130
      HashedArrayList<int> view;
2131

    
2132

    
2133
      [SetUp]
2134
      public void Init()
2135
      {
2136
        list = new HashedArrayList<int>();
2137
        list.Add(0); list.Add(1); list.Add(2); list.Add(3);
2138
        view = (HashedArrayList<int>)list.View(1, 2);
2139
      }
2140

    
2141

    
2142
      [TearDown]
2143
      public void Dispose()
2144
      {
2145
        list = null;
2146
        view = null;
2147
      }
2148

    
2149

    
2150
      void check()
2151
      {
2152
        Assert.IsTrue(list.Check());
2153
        Assert.IsTrue(view.Check());
2154
      }
2155

    
2156
      [Test]
2157
      public void InsertPointer()
2158
      {
2159
        IList<int> view2 = list.View(2, 0);
2160
        list.Insert(view2, 7);
2161
        check();
2162
        list.Insert(list, 8);
2163
        check();
2164
        view.Insert(view2, 9);
2165
        check();
2166
        view.Insert(list.View(3, 2), 10);
2167
        check();
2168
        view.Insert(list.ViewOf(0), 11);
2169
        check();
2170
        Assert.IsTrue(IC.eq(list, 0, 11, 1, 9, 7, 2, 10, 3, 8));
2171
        Assert.IsTrue(IC.eq(view, 11, 1, 9, 7, 2, 10));
2172
      }
2173

    
2174
      [Test]
2175
      [ExpectedException(typeof(IndexOutOfRangeException))]
2176
      public void InsertPointerBad1()
2177
      {
2178
        view.Insert(list.View(0, 0), 7);
2179
      }
2180

    
2181
      [Test]
2182
      [ExpectedException(typeof(IndexOutOfRangeException))]
2183
      public void InsertPointerBad2()
2184
      {
2185
        view.Insert(list, 7);
2186
      }
2187

    
2188
      [Test]
2189
      [ExpectedException(typeof(IncompatibleViewException))]
2190
      public void InsertPointerBad3()
2191
      {
2192
        list.Insert(new ArrayList<int>(), 7);
2193
      }
2194

    
2195
      [Test]
2196
      [ExpectedException(typeof(IncompatibleViewException))]
2197
      public void InsertPointerBad4()
2198
      {
2199
        list.Insert(new ArrayList<int>().View(0, 0), 7);
2200
      }
2201

    
2202

    
2203
      [Test]
2204
      public void Span()
2205
      {
2206
        IList<int> span = list.View(1, 0).Span(list.View(2, 0));
2207
        Assert.IsTrue(span.Check());
2208
        Assert.AreEqual(1, span.Offset);
2209
        Assert.AreEqual(1, span.Count);
2210
        span = list.View(0, 2).Span(list.View(2, 2));
2211
        Assert.IsTrue(span.Check());
2212
        Assert.AreEqual(0, span.Offset);
2213
        Assert.AreEqual(4, span.Count);
2214
        span = list.View(3, 1).Span(list.View(1, 1));
2215
        Assert.IsNull(span);
2216
      }
2217

    
2218
      [Test]
2219
      public void ViewOf()
2220
      {
2221
        for (int i = 0; i < 4; i++)
2222
          list.Add(i);
2223
        IList<int> v = view.ViewOf(2);
2224
        Assert.IsTrue(v.Check());
2225
        Assert.IsTrue(IC.eq(v, 2));
2226
        Assert.AreEqual(2, v.Offset);
2227
        v = list.ViewOf(2);
2228
        Assert.IsTrue(v.Check());
2229
        Assert.IsTrue(IC.eq(v, 2));
2230
        Assert.AreEqual(2, v.Offset);
2231
        v = list.LastViewOf(2);
2232
        Assert.IsTrue(v.Check());
2233
        Assert.IsTrue(IC.eq(v, 2));
2234
        Assert.AreEqual(2, v.Offset);
2235
      }
2236

    
2237
      [Test]
2238
      public void BadViewOf()
2239
      {
2240
        Assert.IsNull(view.ViewOf(5));
2241
        Assert.IsNull(view.LastViewOf(5));
2242
        Assert.IsNull(view.ViewOf(3));
2243
        Assert.IsNull(view.LastViewOf(3));
2244
        Assert.IsNull(view.ViewOf(0));
2245
        Assert.IsNull(view.LastViewOf(0));
2246
      }
2247

    
2248

    
2249
      [Test]
2250
      public void ArrayStuff()
2251
      {
2252
        Assert.IsTrue(IC.eq(view.ToArray(), 1, 2));
2253
        int[] extarray = new int[5];
2254
        view.CopyTo(extarray, 2);
2255
        Assert.IsTrue(IC.eq(extarray, 0, 0, 1, 2, 0));
2256
      }
2257

    
2258

    
2259
      [Test]
2260
      public void Add()
2261
      {
2262
        check();
2263
        Assert.IsTrue(IC.eq(list, 0, 1, 2, 3));
2264
        Assert.IsTrue(IC.eq(view, 1, 2));
2265
        view.InsertFirst(10);
2266
        check();
2267
        Assert.IsTrue(IC.eq(list, 0, 10, 1, 2, 3));
2268
        Assert.IsTrue(IC.eq(view, 10, 1, 2));
2269
        view.Clear();
2270
        Assert.IsFalse(view.IsReadOnly);
2271
        Assert.IsFalse(view.AllowsDuplicates);
2272
        Assert.IsTrue(view.IsEmpty);
2273
        check();
2274
        Assert.IsTrue(IC.eq(list, 0, 3));
2275
        Assert.IsTrue(IC.eq(view));
2276
        view.Add(8);
2277
        Assert.IsFalse(view.IsEmpty);
2278
        Assert.IsFalse(view.AllowsDuplicates);
2279
        Assert.IsFalse(view.IsReadOnly);
2280
        check();
2281
        Assert.IsTrue(IC.eq(list, 0, 8, 3));
2282
        Assert.IsTrue(IC.eq(view, 8));
2283
        view.Add(12);
2284
        check();
2285
        Assert.IsTrue(IC.eq(list, 0, 8, 12, 3));
2286
        Assert.IsTrue(IC.eq(view, 8, 12));
2287
        view./*ViewOf(12).*/InsertLast(15);
2288
        check();
2289
        Assert.IsTrue(IC.eq(list, 0, 8, 12, 15, 3));
2290
        Assert.IsTrue(IC.eq(view, 8, 12, 15));
2291
        view.ViewOf(12).InsertFirst(18);
2292
        check();
2293
        Assert.IsTrue(IC.eq(list, 0, 8, 18, 12, 15, 3));
2294
        Assert.IsTrue(IC.eq(view, 8, 18, 12, 15));
2295

    
2296
        HashedArrayList<int> lst2 = new HashedArrayList<int>();
2297

    
2298
        lst2.Add(90); lst2.Add(92);
2299
        view.AddAll(lst2);
2300
        check();
2301
        Assert.IsTrue(IC.eq(list, 0, 8, 18, 12, 15, 90, 92, 3));
2302
        Assert.IsTrue(IC.eq(view, 8, 18, 12, 15, 90, 92));
2303
        view.InsertLast(66);
2304
        check();
2305
        Assert.IsTrue(IC.eq(list, 0, 8, 18, 12, 15, 90, 92, 66, 3));
2306
        Assert.IsTrue(IC.eq(view, 8, 18, 12, 15, 90, 92, 66));
2307
      }
2308

    
2309

    
2310
      [Test]
2311
      public void Bxxx()
2312
      {
2313
        Assert.IsTrue(IC.eq(view.Backwards(), 2, 1));
2314
        Assert.AreSame(list, view.Underlying);
2315
        Assert.IsNull(list.Underlying);
2316
        Assert.AreEqual(EnumerationDirection.Forwards, view.Direction);
2317
        Assert.AreEqual(EnumerationDirection.Backwards, view.Backwards().Direction);
2318
        Assert.AreEqual(0, list.Offset);
2319
        Assert.AreEqual(1, view.Offset);
2320
      }
2321

    
2322

    
2323
      [Test]
2324
      public void Contains()
2325
      {
2326
        Assert.IsTrue(view.Contains(1));
2327
        Assert.IsFalse(view.Contains(0));
2328

    
2329
        HashedArrayList<int> lst2 = new HashedArrayList<int>();
2330

    
2331
        lst2.Add(2);
2332
        Assert.IsTrue(view.ContainsAll(lst2));
2333
        lst2.Add(3);
2334
        Assert.IsFalse(view.ContainsAll(lst2));
2335
        Assert.AreEqual(Speed.Constant, view.ContainsSpeed);
2336
        Assert.AreEqual(2, view.Count);
2337
        view.Add(1);
2338
        Assert.AreEqual(1, view.ContainsCount(2));
2339
        Assert.AreEqual(1, view.ContainsCount(1));
2340
        Assert.AreEqual(2, view.Count);
2341
      }
2342

    
2343

    
2344
      [Test]
2345
      public void CreateView()
2346
      {
2347
        HashedArrayList<int> view2 = (HashedArrayList<int>)view.View(1, 0);
2348

    
2349
        Assert.AreSame(list, view2.Underlying);
2350
      }
2351

    
2352

    
2353
      [Test]
2354
      public void FIFO()
2355
      {
2356
        Assert.IsFalse(view.FIFO);
2357
        view.FIFO = true;
2358
        view.Add(23); view.Add(24); view.Add(25);
2359
        check();
2360
        Assert.IsTrue(IC.eq(view, 1, 2, 23, 24, 25));
2361
        Assert.AreEqual(1, view.Remove());
2362
        check();
2363
        Assert.IsTrue(IC.eq(view, 2, 23, 24, 25));
2364
        view.FIFO = false;
2365
        Assert.IsFalse(view.FIFO);
2366
        Assert.AreEqual(25, view.Remove());
2367
        check();
2368
        Assert.IsTrue(IC.eq(view, 2, 23, 24));
2369
      }
2370

    
2371

    
2372
      [Test]
2373
      public void MapEtc()
2374
      {
2375
        HashedArrayList<double> dbl = (HashedArrayList<double>)view.Map(new Fun<int, double>(delegate(int i) { return i / 10.0; }));
2376

    
2377
        Assert.IsTrue(dbl.Check());
2378
        Assert.AreEqual(0.1, dbl[0]);
2379
        Assert.AreEqual(0.2, dbl[1]);
2380
        for (int i = 0; i < 10; i++) view.Add(i);
2381

    
2382
        HashedArrayList<int> list2 = (HashedArrayList<int>)view.FindAll(new Fun<int, bool>(delegate(int i) { return i % 4 == 1; }));
2383

    
2384
        Assert.IsTrue(list2.Check());
2385
        Assert.IsTrue(IC.eq(list2, 1, 5, 9));
2386
      }
2387

    
2388

    
2389
      [Test]
2390
      public void FL()
2391
      {
2392
        Assert.AreEqual(1, view.First);
2393
        Assert.AreEqual(2, view.Last);
2394
      }
2395

    
2396

    
2397
      [Test]
2398
      public void Indexing()
2399
      {
2400
        list.Clear();
2401
        for (int i = 0; i < 20; i++) list.Add(i);
2402

    
2403
        view = (HashedArrayList<int>)list.View(5, 7);
2404
        for (int i = 0; i < 7; i++) Assert.AreEqual(i + 5, view[i]);
2405

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

    
2408
        for (int i = 0; i < 7; i++) Assert.AreEqual(i, view.LastIndexOf(i + 5));
2409
      }
2410

    
2411

    
2412
      [Test]
2413
      public void Insert()
2414
      {
2415
        view.Insert(0, 34);
2416
        view.Insert(1, 35);
2417
        view.Insert(4, 36);
2418
        Assert.IsTrue(view.Check());
2419
        Assert.IsTrue(IC.eq(view, 34, 35, 1, 2, 36));
2420

    
2421
        IList<int> list2 = new HashedArrayList<int>();
2422

    
2423
        list2.Add(40); list2.Add(41);
2424
        view.InsertAll(3, list2);
2425
        Assert.IsTrue(view.Check());
2426
        Assert.IsTrue(IC.eq(view, 34, 35, 1, 40, 41, 2, 36));
2427
      }
2428

    
2429

    
2430
      [Test]
2431
      public void Sort()
2432
      {
2433
        view.Add(45); view.Add(47); view.Add(46); view.Add(48);
2434
        Assert.IsFalse(view.IsSorted(new IC()));
2435
        view.Sort(new IC());
2436
        check();
2437
        Assert.IsTrue(IC.eq(list, 0, 1, 2, 45, 46, 47, 48, 3));
2438
        Assert.IsTrue(IC.eq(view, 1, 2, 45, 46, 47, 48));
2439
      }
2440

    
2441

    
2442
      [Test]
2443
      public void Remove()
2444
      {
2445
        view.Add(1); view.Add(5); view.Add(3); view.Add(1); view.Add(3); view.Add(0);
2446
        Assert.IsTrue(IC.eq(view, 1, 2, 5));
2447
        Assert.IsTrue(view.Remove(1));
2448
        check();
2449
        Assert.IsTrue(IC.eq(view, 2, 5));
2450
        Assert.IsFalse(view.Remove(1));
2451
        check();
2452
        Assert.IsTrue(IC.eq(view, 2, 5));
2453
        Assert.IsFalse(view.Remove(0));
2454
        check();
2455
        Assert.IsTrue(IC.eq(view, 2, 5));
2456
        view.RemoveAllCopies(3);
2457
        check();
2458
        Assert.IsTrue(IC.eq(view, 2, 5));
2459
        Assert.IsTrue(IC.eq(list, 0, 2, 5, 3));
2460
        view.Add(1); view.Add(5); view.Add(3); view.Add(1); view.Add(3); view.Add(0);
2461
        Assert.IsTrue(IC.eq(view, 2, 5, 1));
2462

    
2463
        HashedArrayList<int> l2 = new HashedArrayList<int>();
2464

    
2465
        l2.Add(1); l2.Add(2); l2.Add(2); l2.Add(3); l2.Add(1);
2466
        view.RemoveAll(l2);
2467
        check();
2468
        Assert.IsTrue(IC.eq(view, 5));
2469
        view.RetainAll(l2);
2470
        check();
2471
        Assert.IsTrue(IC.eq(view));
2472
        view.Add(2); view.Add(4); view.Add(5);
2473
        Assert.AreEqual(2, view.RemoveAt(0));
2474
        Assert.AreEqual(5, view.RemoveAt(1));
2475
        Assert.AreEqual(4, view.RemoveAt(0));
2476
        check();
2477
        Assert.IsTrue(IC.eq(view));
2478
        view.Add(8); view.Add(6); view.Add(78);
2479
        Assert.AreEqual(8, view.RemoveFirst());
2480
        Assert.AreEqual(78, view.RemoveLast());
2481
        view.Add(2); view.Add(5); view.Add(3); view.Add(1);
2482
        view.RemoveInterval(1, 2);
2483
        check();
2484
        Assert.IsTrue(IC.eq(view, 6, 1));
2485
      }
2486

    
2487

    
2488
      [Test]
2489
      public void Reverse()
2490
      {
2491
        view.Clear();
2492
        for (int i = 0; i < 10; i++) view.Add(10 + i);
2493

    
2494
        view.View(3, 4).Reverse();
2495
        check();
2496
        Assert.IsTrue(IC.eq(view, 10, 11, 12, 16, 15, 14, 13, 17, 18, 19));
2497
        view.Reverse();
2498
        Assert.IsTrue(IC.eq(view, 19, 18, 17, 13, 14, 15, 16, 12, 11, 10));
2499
        Assert.IsTrue(IC.eq(list, 0, 19, 18, 17, 13, 14, 15, 16, 12, 11, 10, 3));
2500
      }
2501

    
2502

    
2503
      [Test]
2504
      public void Slide()
2505
      {
2506
        view.Slide(1);
2507
        check();
2508
        Assert.IsTrue(IC.eq(view, 2, 3));
2509
        view.Slide(-2);
2510
        check();
2511
        Assert.IsTrue(IC.eq(view, 0, 1));
2512
        view.Slide(0, 3);
2513
        check();
2514
        Assert.IsTrue(IC.eq(view, 0, 1, 2));
2515
        view.Slide(2, 1);
2516
        check();
2517
        Assert.IsTrue(IC.eq(view, 2));
2518
        view.Slide(-1, 0);
2519
        check();
2520
        Assert.IsTrue(IC.eq(view));
2521
        view.Add(28);
2522
        Assert.IsTrue(IC.eq(list, 0, 28, 1, 2, 3));
2523
      }
2524
      [Test]
2525
      public void Iterate()
2526
      {
2527
        list.Clear();
2528
        view = null;
2529
        foreach (int i in new int[] { 2, 4, 8, 13, 6, 1, 10, 11 }) list.Add(i);
2530

    
2531
        view = (HashedArrayList<int>)list.View(list.Count - 2, 2);
2532
        int j = 666;
2533
        while (true)
2534
        {
2535
          //Console.WriteLine("View: {0}:  {1} --> {2}", view.Count, view.First, view.Last);
2536
          if ((view.Last - view.First) % 2 == 1)
2537
            view.Insert(1, j++);
2538
          check();
2539
          if (view.Offset == 0)
2540
            break;
2541
          else
2542
            view.Slide(-1, 2);
2543
        }
2544
        //foreach (int cell in list) Console.Write(" " + cell);
2545
        //Assert.IsTrue(list.Check());
2546
        Assert.IsTrue(IC.eq(list, 2, 4, 8, 668, 13, 6, 1, 667, 10, 666, 11));
2547
      }
2548

    
2549

    
2550
      [Test]
2551
      public void SyncRoot()
2552
      {
2553
        Assert.AreSame(((System.Collections.IList)view).SyncRoot, ((System.Collections.IList)list).SyncRoot);
2554
      }
2555
    }
2556

    
2557
    [TestFixture]
2558
    public class MulipleViews
2559
    {
2560
      IList<int> list;
2561
      IList<int>[][] views;
2562
      [SetUp]
2563
      public void Init()
2564
      {
2565
        list = new HashedArrayList<int>();
2566
        for (int i = 0; i < 6; i++)
2567
          list.Add(i);
2568
        views = new IList<int>[7][];
2569
        for (int i = 0; i < 7; i++)
2570
        {
2571
          views[i] = new IList<int>[7 - i];
2572
          for (int j = 0; j < 7 - i; j++)
2573
            views[i][j] = list.View(i, j);
2574
        }
2575
      }
2576
      [TearDown]
2577
      public void Dispose()
2578
      {
2579
        list = null;
2580
        views = null;
2581
      }
2582
      [Test]
2583
      public void Insert()
2584
      {
2585
        Assert.IsTrue(list.Check(), "list check before insert");
2586
        list.Insert(3, 777);
2587
        Assert.IsTrue(list.Check(), "list check after insert");
2588
        for (int i = 0; i < 7; i++)
2589
          for (int j = 0; j < 7 - i; j++)
2590
          {
2591
            Assert.AreEqual(i < 3 || (i == 3 && j == 0) ? i : i + 1, views[i][j].Offset, "view[" + i + "][" + j + "] offset");
2592
            Assert.AreEqual(i < 3 && i + j > 3 ? j + 1 : j, views[i][j].Count, "view[" + i + "][" + j + "] count");
2593
          }
2594
      }
2595
      [Test]
2596
      public void RemoveAt()
2597
      {
2598
        Assert.IsTrue(list.Check(), "list check before remove");
2599
        list.RemoveAt(3);
2600
        Assert.IsTrue(list.Check(), "list check after remove");
2601
        for (int i = 0; i < 7; i++)
2602
          for (int j = 0; j < 7 - i; j++)
2603
          {
2604
            Assert.AreEqual(i <= 3 ? i : i - 1, views[i][j].Offset, "view[" + i + "][" + j + "] offset");
2605
            Assert.AreEqual(i <= 3 && i + j > 3 ? j - 1 : j, views[i][j].Count, "view[" + i + "][" + j + "] count");
2606
          }
2607
      }
2608

    
2609
      [Test]
2610
      public void RemoveInterval()
2611
      {
2612
        Assert.IsTrue(list.Check(), "list check before remove");
2613
        list.RemoveInterval(3, 2);
2614
        Assert.IsTrue(list.Check(), "list check after remove");
2615
        for (int i = 0; i < 7; i++)
2616
          for (int j = 0; j < 7 - i; j++)
2617
          {
2618
            Assert.AreEqual(i <= 3 ? i : i <= 5 ? 3 : i - 2, views[i][j].Offset, "view[" + i + "][" + j + "] offset");
2619
            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");
2620
          }
2621
      }
2622

    
2623
      [Test]
2624
      public void InsertAtEnd()
2625
      {
2626
        Assert.IsTrue(list.Check(), "list check before insert");
2627
        list.InsertLast(777);
2628
        Assert.IsTrue(list.Check(), "list check after insert");
2629
        for (int i = 0; i < 7; i++)
2630
          for (int j = 0; j < 7 - i; j++)
2631
          {
2632
            Assert.AreEqual(i, views[i][j].Offset, "view[" + i + "][" + j + "] offset");
2633
            Assert.AreEqual(j, views[i][j].Count, "view[" + i + "][" + j + "] count");
2634
          }
2635
      }
2636
      [Test]
2637
      public void RemoveAtEnd()
2638
      {
2639
        Assert.IsTrue(list.Check(), "list check before remove");
2640
        list.RemoveAt(5);
2641
        Assert.IsTrue(list.Check(), "list check after remove");
2642
        for (int i = 0; i < 7; i++)
2643
          for (int j = 0; j < 7 - i; j++)
2644
          {
2645
            Assert.AreEqual(i <= 5 ? i : i - 1, views[i][j].Offset, "view[" + i + "][" + j + "] offset");
2646
            Assert.AreEqual(i <= 5 && i + j > 5 ? j - 1 : j, views[i][j].Count, "view[" + i + "][" + j + "] count");
2647
          }
2648
      }
2649
      [Test]
2650
      public void InsertAtStart()
2651
      {
2652
        Assert.IsTrue(list.Check(), "list check before insert");
2653
        list.Insert(0, 777);
2654
        Assert.IsTrue(list.Check(), "list check after insert");
2655
        for (int i = 0; i < 7; i++)
2656
          for (int j = 0; j < 7 - i; j++)
2657
          {
2658
            Assert.AreEqual(i == 0 && j == 0 ? 0 : i + 1, views[i][j].Offset, "view[" + i + "][" + j + "] offset");
2659
            Assert.AreEqual(j, views[i][j].Count, "view[" + i + "][" + j + "] count");
2660
          }
2661
      }
2662
      [Test]
2663
      public void RemoveAtStart()
2664
      {
2665
        Assert.IsTrue(list.Check(), "list check before remove");
2666
        list.RemoveAt(0);
2667
        Assert.IsTrue(list.Check(), "list check after remove");
2668
        for (int i = 0; i < 7; i++)
2669
          for (int j = 0; j < 7 - i; j++)
2670
          {
2671
            Assert.AreEqual(i == 0 ? i : i - 1, views[i][j].Offset, "view[" + i + "][" + j + "] offset");
2672
            Assert.AreEqual(i == 0 && j > 0 ? j - 1 : j, views[i][j].Count, "view[" + i + "][" + j + "] count");
2673
          }
2674
      }
2675
      [Test]
2676
      public void Clear()
2677
      {
2678
        Assert.IsTrue(list.Check(), "list check before clear");
2679
        //for (int i = 0; i < 7; i++)
2680
        //for (int j = 0; j < 7 - i; j++)
2681
        //Console.WriteLine("// view[{0}][{1}] : {2}", i, j, ((HashedArrayList<int>) views[i][j]).GetHashCode());
2682
        views[2][3].Clear();
2683
        Assert.IsTrue(list.Check(), "list check after clear");
2684
        for (int i = 0; i < 7; i++)
2685
          for (int j = 0; j < 7 - i; j++)
2686
          {
2687
            Assert.AreEqual(i < 2 ? i : i < 6 ? 2 : i - 3, views[i][j].Offset, "view[" + i + "][" + j + "] offset");
2688
            Assert.AreEqual(s(i, j), views[i][j].Count, "view[" + i + "][" + j + "] count");
2689
          }
2690
      }
2691

    
2692
      private int s(int i, int j)
2693
      {
2694
        if (j == 0) return 0;
2695
        int k = i + j - 1; //end
2696
        if (i > 4 || k <= 1) return j;
2697
        if (i >= 2) return k > 4 ? k - 4 : 0;
2698
        if (i <= 2) return k >= 4 ? j - 3 : 2 - i;
2699
        return -1;
2700
      }
2701
      [Test]
2702
      public void InsertAll()
2703
      {
2704
        IList<int> list2 = new HashedArrayList<int>();
2705
        for (int i = 0; i < 5; i++) { list2.Add(100 + i); }
2706
        Assert.IsTrue(list.Check(), "list check before insertAll");
2707
        list.InsertAll(3, list2);
2708
        Assert.IsTrue(list.Check(), "list check after insertAll");
2709
        for (int i = 0; i < 7; i++)
2710
          for (int j = 0; j < 7 - i; j++)
2711
          {
2712
            Assert.AreEqual(i < 3 || (i == 3 && j == 0) ? i : i + 5, views[i][j].Offset, "view[" + i + "][" + j + "] offset");
2713
            Assert.AreEqual(i < 3 && i + j > 3 ? j + 5 : j, views[i][j].Count, "view[" + i + "][" + j + "] count");
2714
          }
2715
      }
2716

    
2717
      [Test]
2718
      public void AddAll()
2719
      {
2720
        IList<int> list2 = new HashedArrayList<int>();
2721
        for (int i = 0; i < 5; i++) { list2.Add(100 + i); }
2722
        Assert.IsTrue(list.Check(), "list check before AddAll");
2723
        list.View(1, 2).AddAll(list2);
2724
        Assert.IsTrue(list.Check(), "list check after AddAll");
2725
        for (int i = 0; i < 7; i++)
2726
          for (int j = 0; j < 7 - i; j++)
2727
          {
2728
            Assert.AreEqual(i < 3 || (i == 3 && j == 0) ? i : i + 5, views[i][j].Offset, "view[" + i + "][" + j + "] offset");
2729
            Assert.AreEqual(i < 3 && i + j > 3 ? j + 5 : j, views[i][j].Count, "view[" + i + "][" + j + "] count");
2730
          }
2731
      }
2732

    
2733
      [Test]
2734
      public void Remove()
2735
      {
2736
        for (int i = 0; i < 7; i++)
2737
        {
2738
          for (int j = 0; j < 7 - i; j++)
2739
          {
2740
            list = new HashedArrayList<int>();
2741
            for (int k = 0; k < 6; k++) list.Add(k);
2742
            HashedArrayList<int> v = (HashedArrayList<int>)list.View(i, j);
2743
            list.Remove(3);
2744
            Assert.IsTrue(list.Check(), "list check after Remove, i=" + i + ", j=" + j);
2745
          }
2746
        }
2747
      }
2748
      [Test]
2749
      public void RemoveAll1()
2750
      {
2751
        IList<int> list2 = new HashedArrayList<int>();
2752
        list2.Add(1); list2.Add(3); list2.Add(4);
2753

    
2754
        for (int i = 0; i < 7; i++)
2755
        {
2756
          for (int j = 0; j < 7 - i; j++)
2757
          {
2758
            list = new HashedArrayList<int>();
2759
            for (int k = 0; k < 6; k++) list.Add(k);
2760
            HashedArrayList<int> v = (HashedArrayList<int>)list.View(i, j);
2761
            list.RemoveAll(list2);
2762
            Assert.IsTrue(list.Check(), "list check after RemoveAll, i=" + i + ", j=" + j);
2763
          }
2764
        }
2765
      }
2766
      [Test]
2767
      public void RemoveAll2()
2768
      {
2769
        IList<int> list2 = new HashedArrayList<int>();
2770
        list2.Add(1); list2.Add(3); list2.Add(4);
2771
        Assert.IsTrue(list.Check(), "list check before RemoveAll");
2772
        list.RemoveAll(list2);
2773

    
2774
        Assert.AreEqual(0, views[0][0].Offset, "view [0][0] offset");
2775
        Assert.AreEqual(0, views[0][1].Offset, "view [0][1] offset");
2776
        Assert.AreEqual(0, views[0][2].Offset, "view [0][2] offset");
2777
        Assert.AreEqual(0, views[0][3].Offset, "view [0][3] offset");
2778
        Assert.AreEqual(0, views[0][4].Offset, "view [0][4] offset");
2779
        Assert.AreEqual(0, views[0][5].Offset, "view [0][5] offset");
2780
        Assert.AreEqual(0, views[0][6].Offset, "view [0][6] offset");
2781
        Assert.AreEqual(1, views[1][0].Offset, "view [1][0] offset");
2782
        Assert.AreEqual(1, views[1][1].Offset, "view [1][1] offset");
2783
        Assert.AreEqual(1, views[1][2].Offset, "view [1][2] offset");
2784
        Assert.AreEqual(1, views[1][3].Offset, "view [1][3] offset");
2785
        Assert.AreEqual(1, views[1][4].Offset, "view [1][4] offset");
2786
        Assert.AreEqual(1, views[1][5].Offset, "view [1][5] offset");
2787
        Assert.AreEqual(1, views[2][0].Offset, "view [2][0] offset");
2788
        Assert.AreEqual(1, views[2][1].Offset, "view [2][1] offset");
2789
        Assert.AreEqual(1, views[2][2].Offset, "view [2][2] offset");
2790
        Assert.AreEqual(1, views[2][3].Offset, "view [2][3] offset");
2791
        Assert.AreEqual(1, views[2][4].Offset, "view [2][4] offset");
2792
        Assert.AreEqual(2, views[3][0].Offset, "view [3][0] offset");
2793
        Assert.AreEqual(2, views[3][1].Offset, "view [3][1] offset");
2794
        Assert.AreEqual(2, views[3][2].Offset, "view [3][2] offset");
2795
        Assert.AreEqual(2, views[3][3].Offset, "view [3][3] offset");
2796
        Assert.AreEqual(2, views[4][0].Offset, "view [4][0] offset");
2797
        Assert.AreEqual(2, views[4][1].Offset, "view [4][1] offset");
2798
        Assert.AreEqual(2, views[4][2].Offset, "view [4][2] offset");
2799
        Assert.AreEqual(2, views[5][0].Offset, "view [5][0] offset");
2800
        Assert.AreEqual(2, views[5][1].Offset, "view [5][1] offset");
2801
        Assert.AreEqual(3, views[6][0].Offset, "view [6][0] offset");
2802

    
2803
        Assert.AreEqual(0, views[0][0].Count, "view [0][0] count");
2804
        Assert.AreEqual(1, views[0][1].Count, "view [0][1] count");
2805
        Assert.AreEqual(1, views[0][2].Count, "view [0][2] count");
2806
        Assert.AreEqual(2, views[0][3].Count, "view [0][3] count");
2807
        Assert.AreEqual(2, views[0][4].Count, "view [0][4] count");
2808
        Assert.AreEqual(2, views[0][5].Count, "view [0][5] count");
2809
        Assert.AreEqual(3, views[0][6].Count, "view [0][6] count");
2810
        Assert.AreEqual(0, views[1][0].Count, "view [1][0] count");
2811
        Assert.AreEqual(0, views[1][1].Count, "view [1][1] count");
2812
        Assert.AreEqual(1, views[1][2].Count, "view [1][2] count");
2813
        Assert.AreEqual(1, views[1][3].Count, "view [1][3] count");
2814
        Assert.AreEqual(1, views[1][4].Count, "view [1][4] count");
2815
        Assert.AreEqual(2, views[1][5].Count, "view [1][5] count");
2816
        Assert.AreEqual(0, views[2][0].Count, "view [2][0] count");
2817
        Assert.AreEqual(1, views[2][1].Count, "view [2][1] count");
2818
        Assert.AreEqual(1, views[2][2].Count, "view [2][2] count");
2819
        Assert.AreEqual(1, views[2][3].Count, "view [2][3] count");
2820
        Assert.AreEqual(2, views[2][4].Count, "view [2][4] count");
2821
        Assert.AreEqual(0, views[3][0].Count, "view [3][0] count");
2822
        Assert.AreEqual(0, views[3][1].Count, "view [3][1] count");
2823
        Assert.AreEqual(0, views[3][2].Count, "view [3][2] count");
2824
        Assert.AreEqual(1, views[3][3].Count, "view [3][3] count");
2825
        Assert.AreEqual(0, views[4][0].Count, "view [4][0] count");
2826
        Assert.AreEqual(0, views[4][1].Count, "view [4][1] count");
2827
        Assert.AreEqual(1, views[4][2].Count, "view [4][2] count");
2828
        Assert.AreEqual(0, views[5][0].Count, "view [5][0] count");
2829
        Assert.AreEqual(1, views[5][1].Count, "view [5][1] count");
2830
        Assert.AreEqual(0, views[6][0].Count, "view [6][0] count");
2831

    
2832
        Assert.IsTrue(list.Check(), "list check after RemoveAll");
2833
      }
2834

    
2835
      [Test]
2836
      public void RetainAll()
2837
      {
2838
        IList<int> list2 = new HashedArrayList<int>();
2839
        list2.Add(2); list2.Add(4); list2.Add(5);
2840
        Assert.IsTrue(list.Check(), "list check before RetainAll");
2841
        list.RetainAll(list2);
2842
        Assert.AreEqual(0, views[0][0].Offset, "view [0][0] offset");
2843
        Assert.AreEqual(0, views[0][1].Offset, "view [0][1] offset");
2844
        Assert.AreEqual(0, views[0][2].Offset, "view [0][2] offset");
2845
        Assert.AreEqual(0, views[0][3].Offset, "view [0][3] offset");
2846
        Assert.AreEqual(0, views[0][4].Offset, "view [0][4] offset");
2847
        Assert.AreEqual(0, views[0][5].Offset, "view [0][5] offset");
2848
        Assert.AreEqual(0, views[0][6].Offset, "view [0][6] offset");
2849
        Assert.AreEqual(0, views[1][0].Offset, "view [1][0] offset");
2850
        Assert.AreEqual(0, views[1][1].Offset, "view [1][1] offset");
2851
        Assert.AreEqual(0, views[1][2].Offset, "view [1][2] offset");
2852
        Assert.AreEqual(0, views[1][3].Offset, "view [1][3] offset");
2853
        Assert.AreEqual(0, views[1][4].Offset, "view [1][4] offset");
2854
        Assert.AreEqual(0, views[1][5].Offset, "view [1][5] offset");
2855
        Assert.AreEqual(0, views[2][0].Offset, "view [2][0] offset");
2856
        Assert.AreEqual(0, views[2][1].Offset, "view [2][1] offset");
2857
        Assert.AreEqual(0, views[2][2].Offset, "view [2][2] offset");
2858
        Assert.AreEqual(0, views[2][3].Offset, "view [2][3] offset");
2859
        Assert.AreEqual(0, views[2][4].Offset, "view [2][4] offset");
2860
        Assert.AreEqual(1, views[3][0].Offset, "view [3][0] offset");
2861
        Assert.AreEqual(1, views[3][1].Offset, "view [3][1] offset");
2862
        Assert.AreEqual(1, views[3][2].Offset, "view [3][2] offset");
2863
        Assert.AreEqual(1, views[3][3].Offset, "view [3][3] offset");
2864
        Assert.AreEqual(1, views[4][0].Offset, "view [4][0] offset");
2865
        Assert.AreEqual(1, views[4][1].Offset, "view [4][1] offset");
2866
        Assert.AreEqual(1, views[4][2].Offset, "view [4][2] offset");
2867
        Assert.AreEqual(2, views[5][0].Offset, "view [5][0] offset");
2868
        Assert.AreEqual(2, views[5][1].Offset, "view [5][1] offset");
2869
        Assert.AreEqual(3, views[6][0].Offset, "view [6][0] offset");
2870

    
2871
        Assert.AreEqual(0, views[0][0].Count, "view [0][0] count");
2872
        Assert.AreEqual(0, views[0][1].Count, "view [0][1] count");
2873
        Assert.AreEqual(0, views[0][2].Count, "view [0][2] count");
2874
        Assert.AreEqual(1, views[0][3].Count, "view [0][3] count");
2875
        Assert.AreEqual(1, views[0][4].Count, "view [0][4] count");
2876
        Assert.AreEqual(2, views[0][5].Count, "view [0][5] count");
2877
        Assert.AreEqual(3, views[0][6].Count, "view [0][6] count");
2878
        Assert.AreEqual(0, views[1][0].Count, "view [1][0] count");
2879
        Assert.AreEqual(0, views[1][1].Count, "view [1][1] count");
2880
        Assert.AreEqual(1, views[1][2].Count, "view [1][2] count");
2881
        Assert.AreEqual(1, views[1][3].Count, "view [1][3] count");
2882
        Assert.AreEqual(2, views[1][4].Count, "view [1][4] count");
2883
        Assert.AreEqual(3, views[1][5].Count, "view [1][5] count");
2884
        Assert.AreEqual(0, views[2][0].Count, "view [2][0] count");
2885
        Assert.AreEqual(1, views[2][1].Count, "view [2][1] count");
2886
        Assert.AreEqual(1, views[2][2].Count, "view [2][2] count");
2887
        Assert.AreEqual(2, views[2][3].Count, "view [2][3] count");
2888
        Assert.AreEqual(3, views[2][4].Count, "view [2][4] count");
2889
        Assert.AreEqual(0, views[3][0].Count, "view [3][0] count");
2890
        Assert.AreEqual(0, views[3][1].Count, "view [3][1] count");
2891
        Assert.AreEqual(1, views[3][2].Count, "view [3][2] count");
2892
        Assert.AreEqual(2, views[3][3].Count, "view [3][3] count");
2893
        Assert.AreEqual(0, views[4][0].Count, "view [4][0] count");
2894
        Assert.AreEqual(1, views[4][1].Count, "view [4][1] count");
2895
        Assert.AreEqual(2, views[4][2].Count, "view [4][2] count");
2896
        Assert.AreEqual(0, views[5][0].Count, "view [5][0] count");
2897
        Assert.AreEqual(1, views[5][1].Count, "view [5][1] count");
2898
        Assert.AreEqual(0, views[6][0].Count, "view [6][0] count");
2899

    
2900
        Assert.IsTrue(list.Check(), "list check after RetainAll");
2901
      }
2902

    
2903
      [Test]
2904
      public void RemoveAllCopies()
2905
      {
2906
        IList<int> list2 = new HashedArrayList<int>();
2907
        list2.Add(0); list2.Add(2); list2.Add(82); list2.Add(92); list2.Add(5); list2.Add(2); list2.Add(1);
2908
        for (int i = 0; i < 7; i++)
2909
        {
2910
          for (int j = 0; j < 7 - i; j++)
2911
          {
2912
            list = new HashedArrayList<int>();
2913
            list.AddAll(list2);
2914
            HashedArrayList<int> v = (HashedArrayList<int>)list.View(i, j);
2915
            list.RemoveAllCopies(2);
2916
            Assert.IsTrue(list.Check(), "list check after RemoveAllCopies, i=" + i + ", j=" + j);
2917
          }
2918
        }
2919
      }
2920

    
2921
      private void checkDisposed(bool reverse, int start, int count)
2922
      {
2923
        int k = 0;
2924
        for (int i = 0; i < 7; i++)
2925
          for (int j = 0; j < 7 - i; j++)
2926
          {
2927
            if (i + j <= start || i >= start + count || (i <= start && i + j >= start + count) || (reverse && start <= i && start + count >= i + j))
2928
            {
2929
              try
2930
              {
2931
                k = views[i][j].Count;
2932
              }
2933
              catch (ViewDisposedException)
2934
              {
2935
                Assert.Fail("view[" + i + "][" + j + "] threw");
2936
              }
2937
              Assert.AreEqual(j, views[i][j].Count, "view[" + i + "][" + j + "] size");
2938
              if (reverse && ((j > 0 && start <= i && start + count >= i + j) || (j == 0 && start < i && start + count > i)))
2939
                Assert.AreEqual(start + (start + count - i - j), views[i][j].Offset, "view[" + i + "][" + j + "] offset (mirrored)");
2940
              else
2941
                Assert.AreEqual(i, views[i][j].Offset, "view[" + i + "][" + j + "] offset");
2942
            }
2943
            else
2944
            {
2945
              try
2946
              {
2947
                k = views[i][j].Count;
2948
                Assert.Fail("view[" + i + "][" + j + "] no throw");
2949
              }
2950
              catch (ViewDisposedException) { }
2951
            }
2952
          }
2953
      }
2954

    
2955
      [Test]
2956
      public void Reverse()
2957
      {
2958
        int start = 2, count = 3;
2959
        IList<int> list2 = list.View(start, count);
2960
        Assert.IsTrue(list.Check(), "list check before Reverse");
2961
        list2.Reverse();
2962
        Assert.IsTrue(list.Check(), "list check after Reverse");
2963
        checkDisposed(true, start, count);
2964
      }
2965

    
2966
      [Test]
2967
      public void Sort()
2968
      {
2969
        int start = 2, count = 3;
2970
        IList<int> list2 = list.View(start, count);
2971
        Assert.IsTrue(list.Check(), "list check before Sort");
2972
        list2.Sort();
2973
        Assert.IsTrue(list.Check(), "list check after Sort");
2974
        checkDisposed(false, start, count);
2975
      }
2976
      [Test]
2977
      public void Shuffle()
2978
      {
2979
        int start = 2, count = 3;
2980
        IList<int> list2 = list.View(start, count);
2981
        Assert.IsTrue(list.Check(), "list check before Shuffle");
2982
        list2.Shuffle();
2983
        Assert.IsTrue(list.Check(), "list check after Shuffle");
2984
        checkDisposed(false, start, count);
2985
      }
2986

    
2987

    
2988
    }
2989

    
2990
  }
2991

    
2992
  namespace HashingAndEquals
2993
  {
2994
    [TestFixture]
2995
    public class IIndexed
2996
    {
2997
      private ISequenced<int> dit, dat, dut;
2998

    
2999

    
3000
      [SetUp]
3001
      public void Init()
3002
      {
3003
        dit = new HashedArrayList<int>();
3004
        dat = new HashedArrayList<int>();
3005
        dut = new HashedArrayList<int>();
3006
      }
3007

    
3008

    
3009
      [Test]
3010
      public void EmptyEmpty()
3011
      {
3012
        Assert.IsTrue(dit.SequencedEquals(dat));
3013
      }
3014

    
3015

    
3016
      [Test]
3017
      public void EmptyNonEmpty()
3018
      {
3019
        dit.Add(3);
3020
        Assert.IsFalse(dit.SequencedEquals(dat));
3021
        Assert.IsFalse(dat.SequencedEquals(dit));
3022
      }
3023

    
3024
      [Test]
3025
      public void HashVal()
3026
      {
3027
        Assert.AreEqual(CHC.sequencedhashcode(), dit.GetSequencedHashCode());
3028
        dit.Add(3);
3029
        Assert.AreEqual(CHC.sequencedhashcode(3), dit.GetSequencedHashCode());
3030
        dit.Add(7);
3031
        Assert.AreEqual(CHC.sequencedhashcode(3, 7), dit.GetSequencedHashCode());
3032
        Assert.AreEqual(CHC.sequencedhashcode(), dut.GetSequencedHashCode());
3033
        dut.Add(7);
3034
        Assert.AreEqual(CHC.sequencedhashcode(7), dut.GetSequencedHashCode());
3035
        dut.Add(3);
3036
        Assert.AreEqual(CHC.sequencedhashcode(7, 3), dut.GetSequencedHashCode());
3037
      }
3038

    
3039

    
3040
      [Test]
3041
      public void EqualHashButDifferent()
3042
      {
3043
        dit.Add(0); dit.Add(31);
3044
        dat.Add(1); dat.Add(0);
3045
        Assert.AreEqual(dit.GetSequencedHashCode(), dat.GetSequencedHashCode());
3046
        Assert.IsFalse(dit.SequencedEquals(dat));
3047
      }
3048

    
3049

    
3050
      [Test]
3051
      public void Normal()
3052
      {
3053
        dit.Add(3);
3054
        dit.Add(7);
3055
        dat.Add(3);
3056
        Assert.IsFalse(dit.SequencedEquals(dat));
3057
        Assert.IsFalse(dat.SequencedEquals(dit));
3058
        dat.Add(7);
3059
        Assert.IsTrue(dit.SequencedEquals(dat));
3060
        Assert.IsTrue(dat.SequencedEquals(dit));
3061
      }
3062

    
3063

    
3064
      [Test]
3065
      public void WrongOrder()
3066
      {
3067
        dit.Add(3);
3068
        dut.Add(3);
3069
        Assert.IsTrue(dit.SequencedEquals(dut));
3070
        Assert.IsTrue(dut.SequencedEquals(dit));
3071
        dit.Add(7);
3072
        ((HashedArrayList<int>)dut).InsertFirst(7);
3073
        Assert.IsFalse(dit.SequencedEquals(dut));
3074
        Assert.IsFalse(dut.SequencedEquals(dit));
3075
      }
3076

    
3077

    
3078
      [Test]
3079
      public void Reflexive()
3080
      {
3081
        Assert.IsTrue(dit.SequencedEquals(dit));
3082
        dit.Add(3);
3083
        Assert.IsTrue(dit.SequencedEquals(dit));
3084
        dit.Add(7);
3085
        Assert.IsTrue(dit.SequencedEquals(dit));
3086
      }
3087

    
3088

    
3089
      [TearDown]
3090
      public void Dispose()
3091
      {
3092
        dit = null;
3093
        dat = null;
3094
        dut = null;
3095
      }
3096
    }
3097

    
3098

    
3099

    
3100
    [TestFixture]
3101
    public class IEditableCollection
3102
    {
3103
      private ICollection<int> dit, dat, dut;
3104

    
3105

    
3106
      [SetUp]
3107
      public void Init()
3108
      {
3109
        dit = new HashedArrayList<int>();
3110
        dat = new HashedArrayList<int>();
3111
        dut = new HashedArrayList<int>();
3112
      }
3113

    
3114

    
3115
      [Test]
3116
      public void EmptyEmpty()
3117
      {
3118
        Assert.IsTrue(dit.UnsequencedEquals(dat));
3119
      }
3120

    
3121

    
3122
      [Test]
3123
      public void EmptyNonEmpty()
3124
      {
3125
        dit.Add(3);
3126
        Assert.IsFalse(dit.UnsequencedEquals(dat));
3127
        Assert.IsFalse(dat.UnsequencedEquals(dit));
3128
      }
3129

    
3130

    
3131
      [Test]
3132
      public void HashVal()
3133
      {
3134
        Assert.AreEqual(CHC.unsequencedhashcode(), dit.GetUnsequencedHashCode());
3135
        dit.Add(3);
3136
        Assert.AreEqual(CHC.unsequencedhashcode(3), dit.GetUnsequencedHashCode());
3137
        dit.Add(7);
3138
        Assert.AreEqual(CHC.unsequencedhashcode(3, 7), dit.GetUnsequencedHashCode());
3139
        Assert.AreEqual(CHC.unsequencedhashcode(), dut.GetUnsequencedHashCode());
3140
        dut.Add(3);
3141
        Assert.AreEqual(CHC.unsequencedhashcode(3), dut.GetUnsequencedHashCode());
3142
        dut.Add(7);
3143
        Assert.AreEqual(CHC.unsequencedhashcode(7, 3), dut.GetUnsequencedHashCode());
3144
      }
3145

    
3146

    
3147
      [Test]
3148
      public void EqualHashButDifferent()
3149
      {
3150
        dit.Add(-1657792980); dit.Add(-1570288808);
3151
        dat.Add(1862883298); dat.Add(-272461342);
3152
        Assert.AreEqual(dit.GetUnsequencedHashCode(), dat.GetUnsequencedHashCode());
3153
        Assert.IsFalse(dit.UnsequencedEquals(dat));
3154
      }
3155

    
3156

    
3157
      [Test]
3158
      public void Normal()
3159
      {
3160
        dit.Add(3);
3161
        dit.Add(7);
3162
        dat.Add(3);
3163
        Assert.IsFalse(dit.UnsequencedEquals(dat));
3164
        Assert.IsFalse(dat.UnsequencedEquals(dit));
3165
        dat.Add(7);
3166
        Assert.IsTrue(dit.UnsequencedEquals(dat));
3167
        Assert.IsTrue(dat.UnsequencedEquals(dit));
3168
      }
3169

    
3170

    
3171
      [Test]
3172
      public void WrongOrder()
3173
      {
3174
        dit.Add(3);
3175
        dut.Add(3);
3176
        Assert.IsTrue(dit.UnsequencedEquals(dut));
3177
        Assert.IsTrue(dut.UnsequencedEquals(dit));
3178
        dit.Add(7);
3179
        dut.Add(7);
3180
        Assert.IsTrue(dit.UnsequencedEquals(dut));
3181
        Assert.IsTrue(dut.UnsequencedEquals(dit));
3182
      }
3183

    
3184

    
3185
      [Test]
3186
      public void Reflexive()
3187
      {
3188
        Assert.IsTrue(dit.UnsequencedEquals(dit));
3189
        dit.Add(3);
3190
        Assert.IsTrue(dit.UnsequencedEquals(dit));
3191
        dit.Add(7);
3192
        Assert.IsTrue(dit.UnsequencedEquals(dit));
3193
      }
3194

    
3195

    
3196
      [TearDown]
3197
      public void Dispose()
3198
      {
3199
        dit = null;
3200
        dat = null;
3201
        dut = null;
3202
      }
3203
    }
3204

    
3205

    
3206

    
3207
    [TestFixture]
3208
    public class MultiLevelUnorderedOfUnOrdered
3209
    {
3210
      private ICollection<int> dit, dat, dut;
3211

    
3212
      private ICollection<ICollection<int>> Dit, Dat, Dut;
3213

    
3214

    
3215
      [SetUp]
3216
      public void Init()
3217
      {
3218
        dit = new HashedArrayList<int>();
3219
        dat = new HashedArrayList<int>();
3220
        dut = new HashedArrayList<int>();
3221
        dit.Add(2); dit.Add(1);
3222
        dat.Add(1); dat.Add(2);
3223
        dut.Add(3);
3224
        Dit = new HashedArrayList<ICollection<int>>();
3225
        Dat = new HashedArrayList<ICollection<int>>();
3226
        Dut = new HashedArrayList<ICollection<int>>();
3227
      }
3228

    
3229

    
3230
      [Test]
3231
      public void Check()
3232
      {
3233
        Assert.IsTrue(dit.UnsequencedEquals(dat));
3234
        Assert.IsFalse(dit.UnsequencedEquals(dut));
3235
      }
3236

    
3237

    
3238
      [Test]
3239
      public void Multi()
3240
      {
3241
        Dit.Add(dit); Dit.Add(dut); Dit.Add(dit);
3242
        Dat.Add(dut); Dat.Add(dit); Dat.Add(dat);
3243
        Assert.IsTrue(Dit.UnsequencedEquals(Dat));
3244
        Assert.IsFalse(Dit.UnsequencedEquals(Dut));
3245
      }
3246

    
3247

    
3248
      [TearDown]
3249
      public void Dispose()
3250
      {
3251
        dit = dat = dut = null;
3252
        Dit = Dat = Dut = null;
3253
      }
3254
    }
3255

    
3256

    
3257

    
3258
    [TestFixture]
3259
    public class MultiLevelOrderedOfUnOrdered
3260
    {
3261
      private ICollection<int> dit, dat, dut;
3262

    
3263
      private ISequenced<ICollection<int>> Dit, Dat, Dut;
3264

    
3265

    
3266
      [SetUp]
3267
      public void Init()
3268
      {
3269
        dit = new HashedArrayList<int>();
3270
        dat = new HashedArrayList<int>();
3271
        dut = new HashedArrayList<int>();
3272
        dit.Add(2); dit.Add(1);
3273
        dat.Add(1); dat.Add(2);
3274
        dut.Add(3);
3275
        Dit = new HashedArrayList<ICollection<int>>();
3276
        Dat = new HashedArrayList<ICollection<int>>();
3277
        Dut = new HashedArrayList<ICollection<int>>();
3278
      }
3279

    
3280

    
3281
      [Test]
3282
      public void Check()
3283
      {
3284
        Assert.IsTrue(dit.UnsequencedEquals(dat));
3285
        Assert.IsFalse(dit.UnsequencedEquals(dut));
3286
      }
3287

    
3288

    
3289
      [Test]
3290
      public void Multi()
3291
      {
3292
        Dit.Add(dit); Dit.Add(dut); Dit.Add(dit);
3293
        Dat.Add(dut); Dat.Add(dit); Dat.Add(dat);
3294
        Dut.Add(dit); Dut.Add(dut); Dut.Add(dat);
3295
        Assert.IsFalse(Dit.SequencedEquals(Dat));
3296
        Assert.IsTrue(Dit.SequencedEquals(Dut));
3297
      }
3298

    
3299

    
3300
      [TearDown]
3301
      public void Dispose()
3302
      {
3303
        dit = dat = dut = null;
3304
        Dit = Dat = Dut = null;
3305
      }
3306
    }
3307

    
3308

    
3309

    
3310
    [TestFixture]
3311
    public class MultiLevelUnOrderedOfOrdered
3312
    {
3313
      private ISequenced<int> dit, dat, dut, dot;
3314

    
3315
      private ICollection<ISequenced<int>> Dit, Dat, Dut, Dot;
3316

    
3317

    
3318
      [SetUp]
3319
      public void Init()
3320
      {
3321
        dit = new HashedArrayList<int>();
3322
        dat = new HashedArrayList<int>();
3323
        dut = new HashedArrayList<int>();
3324
        dot = new HashedArrayList<int>();
3325
        dit.Add(2); dit.Add(1);
3326
        dat.Add(1); dat.Add(2);
3327
        dut.Add(3);
3328
        dot.Add(2); dot.Add(1);
3329
        Dit = new HashedArrayList<ISequenced<int>>();
3330
        Dat = new HashedArrayList<ISequenced<int>>();
3331
        Dut = new HashedArrayList<ISequenced<int>>();
3332
        Dot = new HashedArrayList<ISequenced<int>>();
3333
      }
3334

    
3335

    
3336
      [Test]
3337
      public void Check()
3338
      {
3339
        Assert.IsFalse(dit.SequencedEquals(dat));
3340
        Assert.IsTrue(dit.SequencedEquals(dot));
3341
        Assert.IsFalse(dit.SequencedEquals(dut));
3342
      }
3343

    
3344

    
3345
      [Test]
3346
      public void Multi()
3347
      {
3348
        Dit.Add(dit); Dit.Add(dut); Dit.Add(dit);
3349
        Dat.Add(dut); Dat.Add(dit); Dat.Add(dat);
3350
        Dut.Add(dot); Dut.Add(dut); Dut.Add(dit);
3351
        Dot.Add(dit); Dot.Add(dit); Dot.Add(dut);
3352
        Assert.IsTrue(Dit.UnsequencedEquals(Dut));
3353
        Assert.IsFalse(Dit.UnsequencedEquals(Dat));
3354
        Assert.IsTrue(Dit.UnsequencedEquals(Dot));
3355
      }
3356

    
3357

    
3358
      [TearDown]
3359
      public void Dispose()
3360
      {
3361
        dit = dat = dut = dot = null;
3362
        Dit = Dat = Dut = Dot = null;
3363
      }
3364
    }
3365

    
3366

    
3367

    
3368
    [TestFixture]
3369
    public class MultiLevelOrderedOfOrdered
3370
    {
3371
      private ISequenced<int> dit, dat, dut, dot;
3372

    
3373
      private ISequenced<ISequenced<int>> Dit, Dat, Dut, Dot;
3374

    
3375

    
3376
      [SetUp]
3377
      public void Init()
3378
      {
3379
        dit = new HashedArrayList<int>();
3380
        dat = new HashedArrayList<int>();
3381
        dut = new HashedArrayList<int>();
3382
        dot = new HashedArrayList<int>();
3383
        dit.Add(2); dit.Add(1); //{2,1}
3384
        dat.Add(1); dat.Add(2); //{1,2}
3385
        dut.Add(3);            //{3}
3386
        dot.Add(2); dot.Add(1); //{2,1}
3387
        Dit = new HashedArrayList<ISequenced<int>>();
3388
        Dat = new HashedArrayList<ISequenced<int>>();
3389
        Dut = new HashedArrayList<ISequenced<int>>();
3390
        Dot = new HashedArrayList<ISequenced<int>>();
3391
      }
3392

    
3393

    
3394
      [Test]
3395
      public void Check()
3396
      {
3397
        Assert.IsFalse(dit.SequencedEquals(dat));
3398
        Assert.IsTrue(dit.SequencedEquals(dot));
3399
        Assert.IsFalse(dit.SequencedEquals(dut));
3400
      }
3401

    
3402

    
3403
      [Test]
3404
      public void Multi()
3405
      {
3406
        Dit.Add(dit); Dit.Add(dut); Dit.Add(dit); // {{2,1},{3}}
3407
        Dat.Add(dut); Dat.Add(dit); Dat.Add(dat); // {{3},{2,1},{1,2}}
3408
        Dut.Add(dot); Dut.Add(dut); Dut.Add(dit); // {{2,1},{3}}
3409
        Dot.Add(dit); Dot.Add(dit); Dot.Add(dut); // {{2,1},{3}}
3410
        Assert.IsTrue(Dit.SequencedEquals(Dut));
3411
        Assert.IsFalse(Dit.SequencedEquals(Dat));
3412
        Assert.IsTrue(Dit.SequencedEquals(Dot));
3413
      }
3414

    
3415

    
3416
      [TearDown]
3417
      public void Dispose()
3418
      {
3419
        dit = dat = dut = dot = null;
3420
        Dit = Dat = Dut = Dot = null;
3421
      }
3422
    }
3423
  }
3424
}