Project

General

Profile

root / branches / compiler / cSharp / ooasCompiler / src / libs / c5 / nunit / templates / Events.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
using System.Reflection;
27

    
28
namespace C5UnitTests.Templates.Events
29
{
30
  public abstract class CollectionValueTester<TCollection, TItem> : GenericCollectionTester<TCollection, EventTypeEnum>
31
    where TCollection : ICollectionValue<TItem>
32
  {
33
    protected TCollection collection;
34
    protected CollectionEventList<TItem> seen;
35
    protected EventTypeEnum listenTo;
36
    protected void listen() { seen.Listen(collection, listenTo); }
37

    
38
    public override void SetUp(TCollection list, EventTypeEnum testSpec)
39
    {
40
      this.collection = list;
41
      listenTo = testSpec;
42
      seen = new CollectionEventList<TItem>(EqualityComparer<TItem>.Default);
43
    }
44

    
45
    public SCG.IEnumerable<EventTypeEnum> SpecsBasic
46
    {
47
      get
48
      {
49
        CircularQueue<EventTypeEnum> specs = new CircularQueue<EventTypeEnum>();
50
        //foreach (EventTypeEnum listenTo in Enum.GetValues(typeof(EventTypeEnum)))
51
        //  if ((listenTo & ~EventTypeEnum.Basic) == 0)
52
        //    specs.Enqueue(listenTo);
53
        //specs.Enqueue(EventTypeEnum.Added | EventTypeEnum.Removed);
54
        for (int spec = 0; spec <= (int)EventTypeEnum.Basic; spec++)
55
          specs.Enqueue((EventTypeEnum)spec);
56
        return specs;
57
      }
58
    }
59
    public SCG.IEnumerable<EventTypeEnum> SpecsAll
60
    {
61
      get
62
      {
63
        CircularQueue<EventTypeEnum> specs = new CircularQueue<EventTypeEnum>();
64
        //foreach (EventTypeEnum listenTo in Enum.GetValues(typeof(EventTypeEnum)))
65
        //  specs.Enqueue(listenTo);
66
        //specs.Enqueue(EventTypeEnum.Added | EventTypeEnum.Removed);
67

    
68
        for (int spec = 0; spec <= (int)EventTypeEnum.All; spec++)
69
          specs.Enqueue((EventTypeEnum)spec);
70
        return specs;
71
      }
72
    }
73
  }
74
  public abstract class CollectionValueTester<U> : CollectionValueTester<U, int> where U : ICollectionValue<int>
75
  {
76
  }
77

    
78
  public class ExtensibleTester<U> : CollectionValueTester<U> where U : IExtensible<int>
79
  {
80
    public override SCG.IEnumerable<EventTypeEnum> GetSpecs()
81
    {
82
      return SpecsBasic;
83
    }
84
    [Test]
85
    public virtual void Listenable()
86
    {
87
      Assert.AreEqual(EventTypeEnum.Basic, collection.ListenableEvents);
88
      Assert.AreEqual(EventTypeEnum.None, collection.ActiveEvents);
89
      listen();
90
      Assert.AreEqual(listenTo, collection.ActiveEvents);
91
    }
92

    
93
    [Test]
94
    public void Add()
95
    {
96
      listen();
97
      seen.Check(new CollectionEvent<int>[0]);
98
      collection.Add(23);
99
      seen.Check(new CollectionEvent<int>[] {
100
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(23, 1), collection),
101
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection)});
102
    }
103

    
104
    [Test]
105
    public void AddAll()
106
    {
107
      for (int i = 0; i < 10; i++)
108
      {
109
        collection.Add(10 * i + 5);
110
      }
111
      listen();
112
      collection.AddAll<int>(new int[] { 45, 200, 56, 67 });
113
      seen.Check(collection.AllowsDuplicates ?
114
        collection.DuplicatesByCounting ?
115
          new CollectionEvent<int>[] {
116
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(45, 1), collection),
117
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(200, 1), collection),
118
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(55, 1), collection),
119
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(65, 1), collection),
120
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection)}
121
        :
122
          new CollectionEvent<int>[] {
123
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(45, 1), collection),
124
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(200, 1), collection),
125
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(56, 1), collection),
126
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(67, 1), collection),
127
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection)}
128
          :
129
          new CollectionEvent<int>[] {
130
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(200, 1), collection),
131
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection)});
132
      collection.AddAll<int>(new int[] { });
133
      seen.Check(new CollectionEvent<int>[] { });
134
    }
135

    
136
  }
137

    
138
  public class CollectionTester<U> : ExtensibleTester<U> where U : ICollection<int>
139
  {
140
    [Test]
141
    public void Update()
142
    {
143
      collection.Add(4); collection.Add(54); collection.Add(56); collection.Add(8);
144
      listen();
145
      collection.Update(53);
146
      seen.Check(
147
        collection.AllowsDuplicates ?
148
        collection.DuplicatesByCounting ?
149
        new CollectionEvent<int>[] {
150
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(54, 2), collection),
151
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(53, 2), collection),
152
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection)
153
          }
154
        : new CollectionEvent<int>[] {
155
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(54, 1), collection),
156
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(53, 1), collection),
157
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection)
158
          }
159
        : new CollectionEvent<int>[] {
160
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(54, 1), collection),
161
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(53, 1), collection),
162
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection)
163
          });
164
      collection.Update(67);
165
      seen.Check(new CollectionEvent<int>[] { });
166
    }
167

    
168
    [Test]
169
    public void FindOrAdd()
170
    {
171
      collection.Add(4); collection.Add(56); collection.Add(8);
172
      listen();
173
      int val = 53;
174
      collection.FindOrAdd(ref val);
175
      seen.Check(new CollectionEvent<int>[] { });
176
      val = 67;
177
      collection.FindOrAdd(ref val);
178
      seen.Check(new CollectionEvent<int>[] { 
179
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(67, 1), collection),
180
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection)
181
        });
182
    }
183

    
184
    [Test]
185
    public void UpdateOrAdd()
186
    {
187
      collection.Add(4); collection.Add(56); collection.Add(8);
188
      listen();
189
      int val = 53;
190
      collection.UpdateOrAdd(val);
191
      seen.Check(new CollectionEvent<int>[] {
192
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(56, 1), collection),
193
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(53, 1), collection),
194
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection)
195
        });
196
      val = 67;
197
      collection.UpdateOrAdd(val);
198
      seen.Check(new CollectionEvent<int>[] { 
199
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(67, 1), collection),
200
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection)
201
        });
202
      collection.UpdateOrAdd(51, out val);
203
      seen.Check(new CollectionEvent<int>[] {
204
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(53, 1), collection),
205
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(51, 1), collection),
206
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection)
207
        });
208
      val = 67;
209
      collection.UpdateOrAdd(81, out val);
210
      seen.Check(new CollectionEvent<int>[] { 
211
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(81, 1), collection),
212
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection)
213
        });
214
    }
215

    
216
    [Test]
217
    public void RemoveItem()
218
    {
219
      collection.Add(4); collection.Add(56); collection.Add(18);
220
      listen();
221
      collection.Remove(53);
222
      seen.Check(new CollectionEvent<int>[] {
223
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(56, 1), collection),
224
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection)});
225
      collection.Remove(11);
226
      seen.Check(new CollectionEvent<int>[] {
227
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(18, 1), collection),
228
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection)});
229
    }
230

    
231
    [Test]
232
    public void RemoveAll()
233
    {
234
      for (int i = 0; i < 10; i++)
235
      {
236
        collection.Add(10 * i + 5);
237
      }
238
      listen();
239
      collection.RemoveAll<int>(new int[] { 32, 187, 45 });
240
      //TODO: the order depends on internals of the HashSet
241
      seen.Check(new CollectionEvent<int>[] {
242
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(35, 1), collection),
243
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(45, 1), collection),
244
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection)});
245
      collection.RemoveAll<int>(new int[] { 200, 300 });
246
      seen.Check(new CollectionEvent<int>[] { });
247
    }
248

    
249
    [Test]
250
    public void RetainAll()
251
    {
252
      for (int i = 0; i < 10; i++)
253
      {
254
        collection.Add(10 * i + 5);
255
      }
256
      listen();
257
      collection.RetainAll<int>(new int[] { 32, 187, 45, 62, 75, 82, 95, 2 });
258
      seen.Check(new CollectionEvent<int>[] {
259
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(15, 1), collection),
260
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(25, 1), collection),
261
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(55, 1), collection),
262
          //new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(75, 1), collection),
263
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection)});
264
      collection.RetainAll<int>(new int[] { 32, 187, 45, 62, 75, 82, 95, 2 });
265
      seen.Check(new CollectionEvent<int>[] { });
266
    }
267

    
268
    [Test]
269
    public void RemoveAllCopies()
270
    {
271
      for (int i = 0; i < 10; i++)
272
      {
273
        collection.Add(3 * i + 5);
274
      }
275
      listen();
276
      collection.RemoveAllCopies(14);
277
      seen.Check(
278
        collection.AllowsDuplicates ?
279
          collection.DuplicatesByCounting ?
280
            new CollectionEvent<int>[] {
281
              new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(11, 3), collection),
282
              new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection)}
283
          :
284
          new CollectionEvent<int>[] {
285
            new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(11, 1), collection),
286
            new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(14, 1), collection),
287
            new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(17, 1), collection),
288
            new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection)}
289
        :
290
        new CollectionEvent<int>[] {
291
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(11, 1), collection),
292
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection)});
293
      collection.RemoveAllCopies(14);
294
      seen.Check(new CollectionEvent<int>[] { });
295
    }
296

    
297
    [Test]
298
    public virtual void Clear()
299
    {
300
      collection.Add(4); collection.Add(56); collection.Add(8);
301
      listen();
302
      collection.Clear();
303
      seen.Check(new CollectionEvent<int>[] {
304
          new CollectionEvent<int>(EventTypeEnum.Cleared, new ClearedEventArgs(true, collection.AllowsDuplicates ? 3 : 2), collection),
305
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection)
306
        });
307
      collection.Clear();
308
      seen.Check(new CollectionEvent<int>[] { });
309
    }
310

    
311
  }
312

    
313
  public class IndexedTester<U> : CollectionTester<U> where U : IIndexed<int>
314
  {
315
    [Test]
316
    public void RemoveAt()
317
    {
318
      collection.Add(4); collection.Add(16); collection.Add(28);
319
      listen();
320
      collection.RemoveAt(1);
321
      seen.Check(new CollectionEvent<int>[] {
322
          new CollectionEvent<int>(EventTypeEnum.RemovedAt, new ItemAtEventArgs<int>(16,1), collection),
323
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(16, 1), collection),
324
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection)});
325
    }
326

    
327
    [Test]
328
    public void RemoveInterval()
329
    {
330
      collection.Add(4); collection.Add(56); collection.Add(18);
331
      listen();
332
      collection.RemoveInterval(1, 2);
333
      seen.Check(new CollectionEvent<int>[] {
334
        collection is IList<int> ?
335
           new CollectionEvent<int>(EventTypeEnum.Cleared, new ClearedRangeEventArgs(false,2,1), collection):
336
           new CollectionEvent<int>(EventTypeEnum.Cleared, new ClearedEventArgs(false,2), collection),
337
         new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection)
338
        });
339
      collection.RemoveInterval(1, 0);
340
      seen.Check(new CollectionEvent<int>[] { });
341
    }
342
  }
343

    
344
  public class SortedIndexedTester<U> : IndexedTester<U> where U : IIndexedSorted<int>
345
  {
346
    [Test]
347
    public void DeleteMinMax()
348
    {
349
      collection.Add(34);
350
      collection.Add(56);
351
      collection.Add(34);
352
      collection.Add(12);
353
      listen();
354
      collection.DeleteMax();
355
      seen.Check(new CollectionEvent<int>[] {
356
        new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(56, 1), collection),
357
        new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection), 
358
      });
359
      collection.DeleteMin();
360
      seen.Check(new CollectionEvent<int>[] {
361
        new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(12, 1), collection), 
362
        new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection), 
363
      });
364
    }
365

    
366
    [Test]
367
    public void AddSorted()
368
    {
369
      listen();
370
      collection.AddSorted(collection.AllowsDuplicates ? new int[] { 31, 62, 63, 93 } : new int[] { 31, 62, 93 });
371
      seen.Check(collection.AllowsDuplicates ?
372
        collection.DuplicatesByCounting ?
373
          new CollectionEvent<int>[] {
374
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(31, 1), collection),
375
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(62, 1), collection),
376
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(62, 1), collection),
377
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(93, 1), collection),
378
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection)}
379
        :
380
          new CollectionEvent<int>[] {
381
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(31, 1), collection),
382
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(62, 1), collection),
383
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(63, 1), collection),
384
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(93, 1), collection),
385
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection)}
386
          :
387
          new CollectionEvent<int>[] {
388
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(31, 1), collection),
389
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(62, 1), collection),
390
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(93, 1), collection),
391
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection)});
392
      collection.AddSorted(new int[] { });
393
      seen.Check(new CollectionEvent<int>[] { });
394
    }
395

    
396
    [Test]
397
    public void RemoveRange()
398
    {
399
      for (int i = 0; i < 20; i++)
400
        collection.Add(i * 10 + 5);
401
      listen();
402
      collection.RemoveRangeFrom(173);
403
      //TODO: fix order to remove in:
404
      seen.Check(
405
          new CollectionEvent<int>[] {
406
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(195, 1), collection),
407
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(185, 1), collection),
408
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(175, 1), collection),
409
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection)});
410
      collection.RemoveRangeFromTo(83, 113);
411
      seen.Check(
412
          new CollectionEvent<int>[] {
413
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(105, 1), collection),
414
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(95, 1), collection),
415
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(85, 1), collection),
416
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection)});
417
      collection.RemoveRangeTo(33);
418
      seen.Check(
419
          new CollectionEvent<int>[] {
420
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(5, 1), collection),
421
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(15, 1), collection),
422
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(25, 1), collection),
423
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection)});
424
      collection.RemoveRangeFrom(173);
425
      seen.Check(new CollectionEvent<int>[] { });
426
      collection.RemoveRangeFromTo(83, 113);
427
      seen.Check(new CollectionEvent<int>[] { });
428
      collection.RemoveRangeTo(33);
429
      seen.Check(new CollectionEvent<int>[] { });
430
    }
431
  }
432

    
433
  public class ListTester<U> : IndexedTester<U> where U : IList<int>
434
  {
435
    public override SCG.IEnumerable<EventTypeEnum> GetSpecs()
436
    {
437
      return SpecsAll;
438
    }
439

    
440
    [Test]
441
    public override void Listenable()
442
    {
443
      Assert.AreEqual(EventTypeEnum.All, collection.ListenableEvents);
444
      Assert.AreEqual(EventTypeEnum.None, collection.ActiveEvents);
445
      listen();
446
      Assert.AreEqual(listenTo, collection.ActiveEvents);
447
    }
448
    [Test]
449
    public void SetThis()
450
    {
451
      collection.Add(4); collection.Add(56); collection.Add(8);
452
      listen();
453
      collection[1] = 45;
454
      seen.Check(new CollectionEvent<int>[] {
455
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(56, 1), collection),
456
          new CollectionEvent<int>(EventTypeEnum.RemovedAt, new ItemAtEventArgs<int>(56,1), collection),
457
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(45, 1), collection),
458
          new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(45,1), collection),
459
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection)
460
          });
461
    }
462

    
463
    [Test]
464
    public void Insert()
465
    {
466
      collection.Add(4); collection.Add(56); collection.Add(8);
467
      listen();
468
      collection.Insert(1, 45);
469
      seen.Check(new CollectionEvent<int>[] {
470
          new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(45,1), collection),
471
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(45, 1), collection),
472
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection)
473
          });
474
    }
475

    
476
    [Test]
477
    public void InsertAll()
478
    {
479
      collection.Add(4); collection.Add(56); collection.Add(8);
480
      listen();
481
      collection.InsertAll<int>(1, new int[] { 666, 777, 888 });
482
      //seen.Print(Console.Error);
483
      seen.Check(new CollectionEvent<int>[] {
484
          new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(666,1), collection),
485
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(666, 1), collection),
486
          new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(777,2), collection),
487
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(777, 1), collection),
488
          new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(888,3), collection),
489
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(888, 1), collection),
490
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection)
491
          });
492
      collection.InsertAll<int>(1, new int[] { });
493
      seen.Check(new CollectionEvent<int>[] { });
494
    }
495

    
496
    [Test]
497
    public void InsertFirstLast()
498
    {
499
      collection.Add(4); collection.Add(56); collection.Add(18);
500
      listen();
501
      collection.InsertFirst(45);
502
      seen.Check(new CollectionEvent<int>[] {
503
          new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(45,0), collection),
504
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(45, 1), collection),
505
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection)
506
          });
507
      collection.InsertLast(88);
508
      seen.Check(new CollectionEvent<int>[] {
509
          new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(88,4), collection),
510
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(88, 1), collection),
511
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection)
512
          });
513
    }
514

    
515
    [Test]
516
    public void Remove()
517
    {
518
      collection.FIFO = false;
519
      collection.Add(4); collection.Add(56); collection.Add(18);
520
      listen();
521
      collection.Remove();
522
      seen.Check(new CollectionEvent<int>[] {
523
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(18, 1), collection),
524
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection)});
525
      collection.FIFO = true;
526
      collection.Remove();
527
      seen.Check(new CollectionEvent<int>[] {
528
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(4, 1), collection),
529
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection)});
530
    }
531

    
532
    [Test]
533
    public void RemoveFirst()
534
    {
535
      collection.Add(4); collection.Add(56); collection.Add(18);
536
      listen();
537
      collection.RemoveFirst();
538
      seen.Check(new CollectionEvent<int>[] {
539
          new CollectionEvent<int>(EventTypeEnum.RemovedAt, new ItemAtEventArgs<int>(4,0), collection),
540
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(4, 1), collection),
541
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection)});
542
    }
543

    
544
    [Test]
545
    public void RemoveLast()
546
    {
547
      collection.Add(4); collection.Add(56); collection.Add(18);
548
      listen();
549
      collection.RemoveLast();
550
      seen.Check(new CollectionEvent<int>[] {
551
          new CollectionEvent<int>(EventTypeEnum.RemovedAt, new ItemAtEventArgs<int>(18,2), collection),
552
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(18, 1), collection),
553
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection)});
554
    }
555

    
556
    [Test]
557
    public void Reverse()
558
    {
559
      collection.Add(4); collection.Add(56); collection.Add(8);
560
      listen();
561
      collection.Reverse();
562
      seen.Check(new CollectionEvent<int>[] {
563
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection)
564
        });
565
      collection.View(1, 0).Reverse();
566
      seen.Check(new CollectionEvent<int>[] { });
567
    }
568

    
569

    
570
    [Test]
571
    public void Sort()
572
    {
573
      collection.Add(4); collection.Add(56); collection.Add(8);
574
      listen();
575
      collection.Sort();
576
      seen.Check(new CollectionEvent<int>[] {
577
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection)
578
        });
579
      collection.View(1, 0).Sort();
580
      seen.Check(new CollectionEvent<int>[] { });
581
    }
582

    
583
    [Test]
584
    public void Shuffle()
585
    {
586
      collection.Add(4); collection.Add(56); collection.Add(8);
587
      listen();
588
      collection.Shuffle();
589
      seen.Check(new CollectionEvent<int>[] {
590
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection)
591
        });
592
      collection.View(1, 0).Shuffle();
593
      seen.Check(new CollectionEvent<int>[] { });
594
    }
595

    
596
    [Test]
597
    public override void Clear()
598
    {
599
      collection.Add(4); collection.Add(56); collection.Add(18);
600
      listen();
601
      collection.View(1, 1).Clear();
602
      seen.Check(new CollectionEvent<int>[] {
603
          new CollectionEvent<int>(EventTypeEnum.Cleared, new ClearedRangeEventArgs(false,1,1), collection),
604
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection)
605
        });
606
      collection.Clear();
607
      seen.Check(new CollectionEvent<int>[] {
608
          new CollectionEvent<int>(EventTypeEnum.Cleared, new ClearedRangeEventArgs(true,2,0), collection),
609
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection)
610
        });
611
      collection.Clear();
612
      seen.Check(new CollectionEvent<int>[] { });
613
    }
614

    
615
    [Test]
616
    public void ListDispose()
617
    {
618
      collection.Add(4); collection.Add(56); collection.Add(18);
619
      listen();
620
      collection.View(1, 1).Dispose();
621
      seen.Check(new CollectionEvent<int>[] { });
622
      collection.Dispose();
623
      seen.Check(new CollectionEvent<int>[] {
624
          new CollectionEvent<int>(EventTypeEnum.Cleared, new ClearedRangeEventArgs(true,3,0), collection),
625
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection)
626
        });
627
      collection.Dispose();
628
      seen.Check(new CollectionEvent<int>[] { });
629
    }
630

    
631
    /* 
632
 
633
     * /
634
        //[TearDown]
635
        //public void Dispose() { list = null; seen = null; }
636
        /*
637
        [Test]
638
        [ExpectedException(typeof(UnlistenableEventException))]
639
        public void ViewChanged()
640
        {
641
          IList<int> w = collection.View(0, 0);
642
          w.CollectionChanged += new CollectionChangedHandler<int>(w_CollectionChanged);
643
        }
644

    
645
        [Test]
646
        [ExpectedException(typeof(UnlistenableEventException))]
647
        public void ViewCleared()
648
        {
649
          IList<int> w = collection.View(0, 0);
650
          w.CollectionCleared += new CollectionClearedHandler<int>(w_CollectionCleared);
651
        }
652

    
653
        [Test]
654
        [ExpectedException(typeof(UnlistenableEventException))]
655
        public void ViewAdded()
656
        {
657
          IList<int> w = collection.View(0, 0);
658
          w.ItemsAdded += new ItemsAddedHandler<int>(w_ItemAdded);
659
        }
660

    
661
        [Test]
662
        [ExpectedException(typeof(UnlistenableEventException))]
663
        public void ViewInserted()
664
        {
665
          IList<int> w = collection.View(0, 0);
666
          w.ItemInserted += new ItemInsertedHandler<int>(w_ItemInserted);
667
        }
668

    
669
        [Test]
670
        [ExpectedException(typeof(UnlistenableEventException))]
671
        public void ViewRemoved()
672
        {
673
          IList<int> w = collection.View(0, 0);
674
          w.ItemsRemoved += new ItemsRemovedHandler<int>(w_ItemRemoved);
675
        }
676

    
677
        [Test]
678
        [ExpectedException(typeof(UnlistenableEventException))]
679
        public void ViewRemovedAt()
680
        {
681
          IList<int> w = collection.View(0, 0);
682
          w.ItemRemovedAt += new ItemRemovedAtHandler<int>(w_ItemRemovedAt);
683
        }
684

    
685
        void w_CollectionChanged(object sender)
686
        {
687
          throw new NotImplementedException();
688
        }
689

    
690
        void w_CollectionCleared(object sender, ClearedEventArgs eventArgs)
691
        {
692
          throw new NotImplementedException();
693
        }
694

    
695
        void w_ItemAdded(object sender, ItemCountEventArgs<int> eventArgs)
696
        {
697
          throw new NotImplementedException();
698
        }
699

    
700
        void w_ItemInserted(object sender, ItemAtEventArgs<int> eventArgs)
701
        {
702
          throw new NotImplementedException();
703
        }
704

    
705
        void w_ItemRemoved(object sender, ItemCountEventArgs<int> eventArgs)
706
        {
707
          throw new NotImplementedException();
708
        }
709

    
710
        void w_ItemRemovedAt(object sender, ItemAtEventArgs<int> eventArgs)
711
        {
712
          throw new NotImplementedException();
713
        }*/
714
  }
715

    
716
  public class StackTester<U> : CollectionValueTester<U> where U : IStack<int>
717
  {
718
    public override SCG.IEnumerable<EventTypeEnum> GetSpecs()
719
    {
720
      return SpecsBasic;
721
    }
722

    
723
    [Test]
724
    public void PushPop()
725
    {
726
      listen();
727
      seen.Check(new CollectionEvent<int>[0]);
728
      collection.Push(23);
729
      seen.Check(new CollectionEvent<int>[] {
730
              new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(23,0), collection),
731
              new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(23, 1), collection),
732
              new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection)});
733
      collection.Push(-12);
734
      seen.Check(new CollectionEvent<int>[] {
735
              new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(-12,1), collection),
736
              new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(-12, 1), collection),
737
              new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection)});
738
      collection.Pop();
739
      seen.Check(new CollectionEvent<int>[] {
740
              new CollectionEvent<int>(EventTypeEnum.RemovedAt, new ItemAtEventArgs<int>(-12,1), collection),
741
              new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(-12, 1), collection),
742
              new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection)});
743
      collection.Pop();
744
      seen.Check(new CollectionEvent<int>[] {
745
              new CollectionEvent<int>(EventTypeEnum.RemovedAt, new ItemAtEventArgs<int>(23,0), collection),
746
              new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(23, 1), collection),
747
              new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection)});
748
    }
749
  }
750

    
751
  public class QueueTester<U> : CollectionValueTester<U> where U : IQueue<int>
752
  {
753
    public override SCG.IEnumerable<EventTypeEnum> GetSpecs()
754
    {
755
      return SpecsBasic;
756
    }
757

    
758
    [Test]
759
    public void EnqueueDequeue()
760
    {
761
      listen();
762
      collection.Enqueue(67);
763
      seen.Check(new CollectionEvent<int>[] {
764
              new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(67,0), collection),
765
              new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(67, 1), collection),
766
              new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection)});
767
      collection.Enqueue(2);
768
      seen.Check(new CollectionEvent<int>[] {
769
              new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(2,1), collection),
770
              new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(2, 1), collection),
771
              new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection)});
772
      collection.Dequeue();
773
      seen.Check(new CollectionEvent<int>[] {
774
              new CollectionEvent<int>(EventTypeEnum.RemovedAt, new ItemAtEventArgs<int>(67,0), collection),
775
              new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(67, 1), collection),
776
              new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection)});
777
      collection.Dequeue();
778
      seen.Check(new CollectionEvent<int>[] {
779
              new CollectionEvent<int>(EventTypeEnum.RemovedAt, new ItemAtEventArgs<int>(2,0), collection),
780
              new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(2, 1), collection),
781
              new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection)});
782
    }
783

    
784
  }
785

    
786
  public class PriorityQueueTester<U> : ExtensibleTester<U> where U : IPriorityQueue<int>
787
  {
788
    public override System.Collections.Generic.IEnumerable<EventTypeEnum> GetSpecs()
789
    {
790
      return SpecsBasic;
791
    }
792

    
793
    [Test]
794
    public void Direct()
795
    {
796
      listen();
797
      collection.Add(34);
798
      seen.Check(new CollectionEvent<int>[] {
799
        new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(34, 1), collection), 
800
        new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection), 
801
      });
802
      collection.Add(56);
803
      seen.Check(new CollectionEvent<int>[] {
804
        new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(56, 1), collection), 
805
        new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection), 
806
      });
807
      collection.AddAll<int>(new int[] { });
808
      seen.Check(new CollectionEvent<int>[] {
809
      });
810
      collection.Add(34);
811
      seen.Check(new CollectionEvent<int>[] {
812
        new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(34, 1), collection), 
813
        new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection), 
814
      });
815
      collection.Add(12);
816
      seen.Check(new CollectionEvent<int>[] {
817
        new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(12, 1), collection), 
818
        new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection), 
819
      });
820
      collection.DeleteMax();
821
      seen.Check(new CollectionEvent<int>[] {
822
        new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(56, 1), collection),
823
        new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection), 
824
      });
825
      collection.DeleteMin();
826
      seen.Check(new CollectionEvent<int>[] {
827
        new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(12, 1), collection), 
828
        new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection), 
829
      });
830
      collection.AddAll<int>(new int[] { 4, 5, 6, 2 });
831
      seen.Check(new CollectionEvent<int>[] {
832
        new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(4, 1), collection), 
833
        new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(5, 1), collection), 
834
        new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(6, 1), collection), 
835
        new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(2, 1), collection), 
836
        new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection)
837
      });
838
    }
839

    
840
    [Test]
841
    public void WithHandles()
842
    {
843
      listen();
844
      IPriorityQueueHandle<int> handle = null, handle2;
845
      collection.Add(34);
846
      seen.Check(new CollectionEvent<int>[] {
847
        new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(34, 1), collection), 
848
        new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection), 
849
      });
850
      collection.Add(56);
851
      seen.Check(new CollectionEvent<int>[] {
852
        new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(56, 1), collection), 
853
        new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection), 
854
      });
855
      collection.Add(ref handle, 34);
856
      seen.Check(new CollectionEvent<int>[] {
857
        new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(34, 1), collection), 
858
        new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection), 
859
      });
860
      collection.Add(12);
861
      seen.Check(new CollectionEvent<int>[] {
862
        new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(12, 1), collection), 
863
        new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection), 
864
      });
865
      collection.DeleteMax(out handle2);
866
      seen.Check(new CollectionEvent<int>[] {
867
        new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(56, 1), collection),
868
        new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection), 
869
      });
870
      collection.DeleteMin(out handle2);
871
      seen.Check(new CollectionEvent<int>[] {
872
        new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(12, 1), collection), 
873
        new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection), 
874
      });
875

    
876
      collection.Replace(handle, 117);
877
      seen.Check(new CollectionEvent<int>[] {
878
        new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(34, 1), collection), 
879
        new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(117, 1), collection), 
880
        new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection), 
881
      });
882

    
883
      collection.Delete(handle);
884
      seen.Check(new CollectionEvent<int>[] {
885
        new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(117, 1), collection), 
886
        new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), collection), 
887
      });
888
    }
889
  }
890

    
891
  public class DictionaryTester<U> : CollectionValueTester<U, KeyValuePair<int, int>> where U : IDictionary<int, int>
892
  {
893
    public override SCG.IEnumerable<EventTypeEnum> GetSpecs()
894
    {
895
      return SpecsBasic;
896
    }
897

    
898
    [Test]
899
    public virtual void Listenable()
900
    {
901
      Assert.AreEqual(EventTypeEnum.Basic, collection.ListenableEvents);
902
      Assert.AreEqual(EventTypeEnum.None, collection.ActiveEvents);
903
      listen();
904
      Assert.AreEqual(listenTo, collection.ActiveEvents);
905
    }
906

    
907
    [Test]
908
    public void AddAndREmove()
909
    {
910
      listen();
911
      seen.Check(new CollectionEvent<KeyValuePair<int, int>>[0]);
912
      collection.Add(23, 45);
913
      seen.Check(new CollectionEvent<KeyValuePair<int,int>>[] {
914
          new CollectionEvent<KeyValuePair<int,int>>(EventTypeEnum.Added, new ItemCountEventArgs<KeyValuePair<int,int>>(new KeyValuePair<int,int>(23,45), 1), collection),
915
          new CollectionEvent<KeyValuePair<int,int>>(EventTypeEnum.Changed, new EventArgs(), collection)});
916
      collection.Remove(25);
917
      seen.Check(new CollectionEvent<KeyValuePair<int, int>>[] {
918
          new CollectionEvent<KeyValuePair<int,int>>(EventTypeEnum.Removed, new ItemCountEventArgs<KeyValuePair<int,int>>(new KeyValuePair<int,int>(23,45), 1), collection),
919
          new CollectionEvent<KeyValuePair<int,int>>(EventTypeEnum.Changed, new EventArgs(), collection)});
920
    }
921

    
922

    
923

    
924
  }
925

    
926
}
927

    
928