Project

General

Profile

root / branches / compiler / cSharp / ooasCompiler / src / libs / c5 / nunit / WrappersTest.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.wrappers
29
{
30
  namespace events
31
  {
32
    [TestFixture]
33
    public class IList_
34
    {
35
      private ArrayList<int> list;
36
      ICollectionValue<int> guarded;
37
      CollectionEventList<int> seen;
38

    
39
      [SetUp]
40
      public void Init()
41
      {
42
        list = new ArrayList<int>(TenEqualityComparer.Default);
43
        guarded = new GuardedList<int>(list);
44
        seen = new CollectionEventList<int>(IntEqualityComparer.Default);
45
      }
46

    
47
      private void listen() { seen.Listen(guarded, EventTypeEnum.All); }
48

    
49
      [Test]
50
      public void Listenable()
51
      {
52
        Assert.AreEqual(EventTypeEnum.All, guarded.ListenableEvents);
53
        Assert.AreEqual(EventTypeEnum.None, guarded.ActiveEvents);
54
        listen();
55
        Assert.AreEqual(EventTypeEnum.All, guarded.ActiveEvents);
56
      }
57

    
58
      [Test]
59
      public void SetThis()
60
      {
61
        list.Add(4); list.Add(56); list.Add(8);
62
        listen();
63
        list[1] = 45;
64
        seen.Check(new CollectionEvent<int>[] {
65
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(56, 1), guarded),
66
          new CollectionEvent<int>(EventTypeEnum.RemovedAt, new ItemAtEventArgs<int>(56,1), guarded),
67
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(45, 1), guarded),
68
          new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(45,1), guarded),
69
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)
70
          });
71
      }
72

    
73
      [Test]
74
      public void Insert()
75
      {
76
        list.Add(4); list.Add(56); list.Add(8);
77
        listen();
78
        list.Insert(1, 45);
79
        seen.Check(new CollectionEvent<int>[] {
80
          new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(45,1), guarded),
81
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(45, 1), guarded),
82
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)
83
          });
84
      }
85

    
86
      [Test]
87
      public void InsertAll()
88
      {
89
        list.Add(4); list.Add(56); list.Add(8);
90
        listen();
91
        list.InsertAll<int>(1, new int[] { 666, 777, 888 });
92
        //seen.Print(Console.Error);
93
        seen.Check(new CollectionEvent<int>[] {
94
          new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(666,1), guarded),
95
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(666, 1), guarded),
96
          new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(777,2), guarded),
97
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(777, 1), guarded),
98
          new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(888,3), guarded),
99
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(888, 1), guarded),
100
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)
101
          });
102
        list.InsertAll<int>(1, new int[] {});
103
        seen.Check(new CollectionEvent<int>[] {});
104
      }
105

    
106
      [Test]
107
      public void InsertFirstLast()
108
      {
109
        list.Add(4); list.Add(56); list.Add(8);
110
        listen();
111
        list.InsertFirst(45);
112
        seen.Check(new CollectionEvent<int>[] {
113
          new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(45,0), guarded),
114
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(45, 1), guarded),
115
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)
116
          });
117
        list.InsertLast(88);
118
        seen.Check(new CollectionEvent<int>[] {
119
          new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(88,4), guarded),
120
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(88, 1), guarded),
121
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)
122
          });
123
      }
124

    
125
      [Test]
126
      public void Remove()
127
      {
128
        list.Add(4); list.Add(56); list.Add(8);
129
        listen();
130
        list.Remove();
131
        seen.Check(new CollectionEvent<int>[] {
132
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(8, 1), guarded),
133
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});
134
      }
135

    
136
      [Test]
137
      public void RemoveFirst()
138
      {
139
        list.Add(4); list.Add(56); list.Add(8);
140
        listen();
141
        list.RemoveFirst();
142
        seen.Check(new CollectionEvent<int>[] {
143
          new CollectionEvent<int>(EventTypeEnum.RemovedAt, new ItemAtEventArgs<int>(4,0), guarded),
144
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(4, 1), guarded),
145
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});
146
      }
147

    
148
      [Test]
149
      public void RemoveLast()
150
      {
151
        list.Add(4); list.Add(56); list.Add(8);
152
        listen();
153
        list.RemoveLast();
154
        seen.Check(new CollectionEvent<int>[] {
155
          new CollectionEvent<int>(EventTypeEnum.RemovedAt, new ItemAtEventArgs<int>(8,2), guarded),
156
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(8, 1), guarded),
157
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});
158
      }
159

    
160
      [Test]
161
      public void Reverse()
162
      {
163
        list.Add(4); list.Add(56); list.Add(8);
164
        listen();
165
        list.Reverse();
166
        seen.Check(new CollectionEvent<int>[] {
167
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)
168
        });
169
        list.View(1, 0).Reverse();
170
        seen.Check(new CollectionEvent<int>[] {});
171
      }
172

    
173

    
174
      [Test]
175
      public void Sort()
176
      {
177
        list.Add(4); list.Add(56); list.Add(8);
178
        listen();
179
        list.Sort();
180
        seen.Check(new CollectionEvent<int>[] {
181
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)
182
        });
183
        list.View(1, 0).Sort();
184
        seen.Check(new CollectionEvent<int>[] {});
185
      }
186

    
187
      [Test]
188
      public void Shuffle()
189
      {
190
        list.Add(4); list.Add(56); list.Add(8);
191
        listen();
192
        list.Shuffle();
193
        seen.Check(new CollectionEvent<int>[] {
194
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)
195
        });
196
        list.View(1, 0).Shuffle();
197
        seen.Check(new CollectionEvent<int>[] {});
198
      }
199

    
200
      [Test]
201
      public void RemoveAt()
202
      {
203
        list.Add(4); list.Add(56); list.Add(8);
204
        listen();
205
        list.RemoveAt(1);
206
        seen.Check(new CollectionEvent<int>[] {
207
          new CollectionEvent<int>(EventTypeEnum.RemovedAt, new ItemAtEventArgs<int>(56,1), guarded),
208
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(56, 1), guarded),
209
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});
210
      }
211

    
212
      [Test]
213
      public void RemoveInterval()
214
      {
215
        list.Add(4); list.Add(56); list.Add(8);
216
        listen();
217
        list.RemoveInterval(1, 2);
218
        seen.Check(new CollectionEvent<int>[] {
219
           new CollectionEvent<int>(EventTypeEnum.Cleared, new ClearedRangeEventArgs(false,2,1), guarded),
220
         new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)
221
        });
222
        list.RemoveInterval(1, 0);
223
        seen.Check(new CollectionEvent<int>[] {});
224
      }
225

    
226
      [Test]
227
      public void Update()
228
      {
229
        list.Add(4); list.Add(56); list.Add(8);
230
        listen();
231
        list.Update(53);
232
        seen.Check(new CollectionEvent<int>[] {
233
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(56, 1), guarded),
234
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(53, 1), guarded),
235
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)
236
          });
237
        list.Update(67);
238
        seen.Check(new CollectionEvent<int>[] {});
239
      }
240

    
241
      [Test]
242
      public void FindOrAdd()
243
      {
244
        list.Add(4); list.Add(56); list.Add(8);
245
        listen();
246
        int val = 53;
247
        list.FindOrAdd(ref val);
248
        seen.Check(new CollectionEvent<int>[] {});
249
        val = 67;
250
        list.FindOrAdd(ref val);
251
        seen.Check(new CollectionEvent<int>[] { 
252
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(67, 1), guarded),
253
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)
254
        });
255
      }
256

    
257
      [Test]
258
      public void UpdateOrAdd()
259
      {
260
        list.Add(4); list.Add(56); list.Add(8);
261
        listen();
262
        int val = 53;
263
        list.UpdateOrAdd(val);
264
        seen.Check(new CollectionEvent<int>[] {
265
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(56, 1), guarded),
266
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(53, 1), guarded),
267
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)
268
        });
269
        val = 67;
270
        list.UpdateOrAdd(val);
271
        seen.Check(new CollectionEvent<int>[] { 
272
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(67, 1), guarded),
273
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)
274
        });
275
        list.UpdateOrAdd(51, out val);
276
        seen.Check(new CollectionEvent<int>[] {
277
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(53, 1), guarded),
278
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(51, 1), guarded),
279
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)
280
        });
281
        val = 67;
282
        list.UpdateOrAdd(81, out val);
283
        seen.Check(new CollectionEvent<int>[] { 
284
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(81, 1), guarded),
285
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)
286
        });
287
      }
288

    
289
      [Test]
290
      public void RemoveItem()
291
      {
292
        list.Add(4); list.Add(56); list.Add(18);
293
        listen();
294
        list.Remove(53);
295
        seen.Check(new CollectionEvent<int>[] {
296
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(56, 1), guarded),
297
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});
298
        list.Remove(11);
299
        seen.Check(new CollectionEvent<int>[] {
300
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(18, 1), guarded),
301
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});
302
      }
303

    
304
      [Test]
305
      public void RemoveAll()
306
      {
307
        for (int i = 0; i < 10; i++)
308
        {
309
          list.Add(10 * i + 5);
310
        }
311
        listen();
312
        list.RemoveAll<int>(new int[] { 32, 187, 45 });
313
        //TODO: the order depends on internals of the HashSet
314
        seen.Check(new CollectionEvent<int>[] {
315
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(35, 1), guarded),
316
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(45, 1), guarded),
317
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});
318
        list.RemoveAll<int>(new int[] { 200, 300 });
319
        seen.Check(new CollectionEvent<int>[] {});
320
      }
321

    
322
      [Test]
323
      public void Clear()
324
      {
325
        list.Add(4); list.Add(56); list.Add(8);
326
        listen();
327
        list.View(1, 1).Clear();
328
        seen.Check(new CollectionEvent<int>[] {
329
          new CollectionEvent<int>(EventTypeEnum.Cleared, new ClearedRangeEventArgs(false,1,1), guarded),
330
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)
331
        });
332
        list.Clear();
333
        seen.Check(new CollectionEvent<int>[] {
334
          new CollectionEvent<int>(EventTypeEnum.Cleared, new ClearedRangeEventArgs(true,2,0), guarded),
335
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)
336
        });
337
        list.Clear();
338
        seen.Check(new CollectionEvent<int>[] {});
339
      }
340

    
341
      [Test]
342
      public void ListDispose()
343
      {
344
        list.Add(4); list.Add(56); list.Add(8);
345
        listen();
346
        list.View(1, 1).Dispose();
347
        seen.Check(new CollectionEvent<int>[] {});
348
        list.Dispose();
349
        seen.Check(new CollectionEvent<int>[] {
350
          new CollectionEvent<int>(EventTypeEnum.Cleared, new ClearedRangeEventArgs(true,3,0), guarded),
351
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)
352
        });
353
        list.Dispose();
354
        seen.Check(new CollectionEvent<int>[] {});
355
      }
356

    
357

    
358
      [Test]
359
      public void RetainAll()
360
      {
361
        for (int i = 0; i < 10; i++)
362
        {
363
          list.Add(10 * i + 5);
364
        }
365
        listen();
366
        list.RetainAll<int>(new int[] { 32, 187, 45, 62, 82, 95, 2 });
367
        seen.Check(new CollectionEvent<int>[] {
368
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(15, 1), guarded),
369
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(25, 1), guarded),
370
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(55, 1), guarded),
371
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(75, 1), guarded),
372
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});
373
        list.RetainAll<int>(new int[] { 32, 187, 45, 62, 82, 95, 2 });
374
        seen.Check(new CollectionEvent<int>[] {});
375
      }
376

    
377
      [Test]
378
      public void RemoveAllCopies()
379
      {
380
        for (int i = 0; i < 10; i++)
381
        {
382
          list.Add(3 * i + 5);
383
        }
384
        listen();
385
        list.RemoveAllCopies(14);
386
        seen.Check(new CollectionEvent<int>[] {
387
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(11, 1), guarded),
388
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(14, 1), guarded),
389
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(17, 1), guarded),
390
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});
391
        list.RemoveAllCopies(14);
392
        seen.Check(new CollectionEvent<int>[] {});
393
      }
394

    
395
      [Test]
396
      public void Add()
397
      {
398
        listen();
399
        seen.Check(new CollectionEvent<int>[0]);
400
        list.Add(23);
401
        seen.Check(new CollectionEvent<int>[] {
402
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(23, 1), guarded),
403
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});
404
      }
405

    
406
      [Test]
407
      public void AddAll()
408
      {
409
        for (int i = 0; i < 10; i++)
410
        {
411
          list.Add(10 * i + 5);
412
        }
413
        listen();
414
        list.AddAll<int>(new int[] { 45, 56, 67 });
415
        seen.Check(new CollectionEvent<int>[] {
416
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(45, 1), guarded),
417
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(56, 1), guarded),
418
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(67, 1), guarded),
419
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});
420
        list.AddAll<int>(new int[] { });
421
        seen.Check(new CollectionEvent<int>[] {});
422
      }
423

    
424
      [TearDown]
425
      public void Dispose() { list = null; seen = null; }
426

    
427
      [Test]
428
      [ExpectedException(typeof(UnlistenableEventException))]
429
      public void ViewChanged()
430
      {
431
        IList<int> w = list.View(0, 0);
432
        w.CollectionChanged += new CollectionChangedHandler<int>(w_CollectionChanged);
433
      }
434

    
435
      [Test]
436
      [ExpectedException(typeof(UnlistenableEventException))]
437
      public void ViewCleared()
438
      {
439
        IList<int> w = list.View(0, 0);
440
        w.CollectionCleared += new CollectionClearedHandler<int>(w_CollectionCleared);
441
      }
442

    
443
      [Test]
444
      [ExpectedException(typeof(UnlistenableEventException))]
445
      public void ViewAdded()
446
      {
447
        IList<int> w = list.View(0, 0);
448
        w.ItemsAdded += new ItemsAddedHandler<int>(w_ItemAdded);
449
      }
450

    
451
      [Test]
452
      [ExpectedException(typeof(UnlistenableEventException))]
453
      public void ViewInserted()
454
      {
455
        IList<int> w = list.View(0, 0);
456
        w.ItemInserted += new ItemInsertedHandler<int>(w_ItemInserted);
457
      }
458

    
459
      [Test]
460
      [ExpectedException(typeof(UnlistenableEventException))]
461
      public void ViewRemoved()
462
      {
463
        IList<int> w = list.View(0, 0);
464
        w.ItemsRemoved += new ItemsRemovedHandler<int>(w_ItemRemoved);
465
      }
466

    
467
      [Test]
468
      [ExpectedException(typeof(UnlistenableEventException))]
469
      public void ViewRemovedAt()
470
      {
471
        IList<int> w = list.View(0, 0);
472
        w.ItemRemovedAt += new ItemRemovedAtHandler<int>(w_ItemRemovedAt);
473
      }
474

    
475
      void w_CollectionChanged(object sender)
476
      {
477
        throw new NotImplementedException();
478
      }
479

    
480
      void w_CollectionCleared(object sender, ClearedEventArgs eventArgs)
481
      {
482
        throw new NotImplementedException();
483
      }
484

    
485
      void w_ItemAdded(object sender, ItemCountEventArgs<int> eventArgs)
486
      {
487
        throw new NotImplementedException();
488
      }
489

    
490
      void w_ItemInserted(object sender, ItemAtEventArgs<int> eventArgs)
491
      {
492
        throw new NotImplementedException();
493
      }
494

    
495
      void w_ItemRemoved(object sender, ItemCountEventArgs<int> eventArgs)
496
      {
497
        throw new NotImplementedException();
498
      }
499

    
500
      void w_ItemRemovedAt(object sender, ItemAtEventArgs<int> eventArgs)
501
      {
502
        throw new NotImplementedException();
503
      }
504
    }
505

    
506
    [TestFixture]
507
    public class StackQueue
508
    {
509
      private ArrayList<int> list;
510
      ICollectionValue<int> guarded;
511
      CollectionEventList<int> seen;
512

    
513
      [SetUp]
514
      public void Init()
515
      {
516
        list = new ArrayList<int>(TenEqualityComparer.Default);
517
        guarded = new GuardedList<int>(list);
518
        seen = new CollectionEventList<int>(IntEqualityComparer.Default);
519
      }
520

    
521
      private void listen() { seen.Listen(guarded, EventTypeEnum.All); }
522

    
523

    
524
      [Test]
525
      public void EnqueueDequeue()
526
      {
527
        listen();
528
        list.Enqueue(67);
529
        seen.Check(new CollectionEvent<int>[] {
530
          new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(67,0), guarded),
531
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(67, 1), guarded),
532
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});
533
        list.Enqueue(2);
534
        seen.Check(new CollectionEvent<int>[] {
535
          new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(2,1), guarded),
536
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(2, 1), guarded),
537
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});
538
        list.Dequeue();
539
        seen.Check(new CollectionEvent<int>[] {
540
          new CollectionEvent<int>(EventTypeEnum.RemovedAt, new ItemAtEventArgs<int>(67,0), guarded),
541
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(67, 1), guarded),
542
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});
543
        list.Dequeue();
544
        seen.Check(new CollectionEvent<int>[] {
545
          new CollectionEvent<int>(EventTypeEnum.RemovedAt, new ItemAtEventArgs<int>(2,0), guarded),
546
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(2, 1), guarded),
547
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});
548
      }
549

    
550
      [Test]
551
      public void PushPop()
552
      {
553
        listen();
554
        seen.Check(new CollectionEvent<int>[0]);
555
        list.Push(23);
556
        seen.Check(new CollectionEvent<int>[] {
557
          new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(23,0), guarded),
558
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(23, 1), guarded),
559
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});
560
        list.Push(-12);
561
        seen.Check(new CollectionEvent<int>[] {
562
          new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(-12,1), guarded),
563
          new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(-12, 1), guarded),
564
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});
565
        list.Pop();
566
        seen.Check(new CollectionEvent<int>[] {
567
          new CollectionEvent<int>(EventTypeEnum.RemovedAt, new ItemAtEventArgs<int>(-12,1), guarded),
568
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(-12, 1), guarded),
569
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});
570
        list.Pop();
571
        seen.Check(new CollectionEvent<int>[] {
572
          new CollectionEvent<int>(EventTypeEnum.RemovedAt, new ItemAtEventArgs<int>(23,0), guarded),
573
          new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(23, 1), guarded),
574
          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});
575
      }
576

    
577
      [TearDown]
578
      public void Dispose() { list = null; seen = null; }
579
    }
580

    
581

    
582
  }
583

    
584
  namespace wrappedarray
585
  {
586
    [TestFixture]
587
    public class Basic
588
    {
589

    
590
      [SetUp]
591
      public void Init()
592
      {
593
      }
594

    
595
      [TearDown]
596
      public void Dispose()
597
      {
598
      }
599

    
600
      [Test]
601
      public void NoExc()
602
      {
603
        WrappedArray<int> wrapped = new WrappedArray<int>(new int[] { 4, 6, 5 });
604
        Assert.AreEqual(6, wrapped[1]);
605
        Assert.IsTrue(IC.eq(wrapped[1, 2], 6, 5));
606
        //
607
        Fun<int, bool> is4 = delegate(int i) { return i == 4; };
608
        Assert.AreEqual(EventTypeEnum.None, wrapped.ActiveEvents);
609
        Assert.AreEqual(false, wrapped.All(is4));
610
        Assert.AreEqual(true, wrapped.AllowsDuplicates);
611
        wrapped.Apply(delegate(int i) { });
612
        Assert.AreEqual("{ 5, 6, 4 }", wrapped.Backwards().ToString());
613
        Assert.AreEqual(true, wrapped.Check());
614
        wrapped.Choose();
615
        Assert.AreEqual(true, wrapped.Contains(4));
616
        Assert.AreEqual(true, wrapped.ContainsAll(new ArrayList<int>()));
617
        Assert.AreEqual(1, wrapped.ContainsCount(4));
618
        Assert.AreEqual(Speed.Linear, wrapped.ContainsSpeed);
619
        int[] extarray = new int[5];
620
        wrapped.CopyTo(extarray, 1);
621
        Assert.IsTrue(IC.eq(extarray, 0, 4, 6, 5, 0));
622
        Assert.AreEqual(3, wrapped.Count);
623
        Assert.AreEqual(Speed.Constant, wrapped.CountSpeed);
624
        Assert.AreEqual(EnumerationDirection.Forwards, wrapped.Direction);
625
        Assert.AreEqual(false, wrapped.DuplicatesByCounting);
626
        Assert.AreEqual(IntEqualityComparer.Default, wrapped.EqualityComparer);
627
        Assert.AreEqual(true, wrapped.Exists(is4));
628
        Assert.IsTrue(IC.eq(wrapped.Filter(is4), 4));
629
        int j = 5;
630
        Assert.AreEqual(true, wrapped.Find(ref j));
631
        Assert.AreEqual(true, wrapped.Find(is4, out j));
632
        Assert.AreEqual("[ 0:4 ]", wrapped.FindAll(is4).ToString());
633
        Assert.AreEqual(0, wrapped.FindIndex(is4));
634
        Assert.AreEqual(true, wrapped.FindLast(is4, out j));
635
        Assert.AreEqual(0, wrapped.FindLastIndex(is4));
636
        Assert.AreEqual(4, wrapped.First);
637
        wrapped.GetEnumerator();
638
        Assert.AreEqual(CHC.sequencedhashcode(4, 6, 5), wrapped.GetSequencedHashCode());
639
        Assert.AreEqual(CHC.unsequencedhashcode(4, 6, 5), wrapped.GetUnsequencedHashCode());
640
        Assert.AreEqual(Speed.Constant, wrapped.IndexingSpeed);
641
        Assert.AreEqual(2, wrapped.IndexOf(5));
642
        Assert.AreEqual(false, wrapped.IsEmpty);
643
        Assert.AreEqual(true, wrapped.IsReadOnly);
644
        Assert.AreEqual(false, wrapped.IsSorted());
645
        Assert.AreEqual(true, wrapped.IsValid);
646
        Assert.AreEqual(5, wrapped.Last);
647
        Assert.AreEqual(2, wrapped.LastIndexOf(5));
648
        Assert.AreEqual(EventTypeEnum.None, wrapped.ListenableEvents);
649
        Fun<int, string> i2s = delegate(int i) { return string.Format("T{0}", i); };
650
        Assert.AreEqual("[ 0:T4, 1:T6, 2:T5 ]", wrapped.Map<string>(i2s).ToString());
651
        Assert.AreEqual(0, wrapped.Offset);
652
        wrapped.Reverse();
653
        Assert.AreEqual("[ 0:5, 1:6, 2:4 ]", wrapped.ToString());
654
        IList<int> other = new ArrayList<int>(); other.AddAll<int>(new int[] { 4, 5, 6 });
655
        Assert.IsFalse(wrapped.SequencedEquals(other));
656
        j = 30;
657
        Assert.AreEqual(true, wrapped.Show(new System.Text.StringBuilder(), ref j, null));
658
        wrapped.Sort();
659
        Assert.AreEqual("[ 0:4, 1:5, 2:6 ]", wrapped.ToString());
660
        Assert.IsTrue(IC.eq(wrapped.ToArray(), 4, 5, 6));
661
        Assert.AreEqual("[ ... ]", wrapped.ToString("L4", null));
662
        Assert.AreEqual(null, wrapped.Underlying);
663
        Assert.IsTrue(IC.seteq(wrapped.UniqueItems(), 4, 5, 6));
664
        Assert.IsTrue(wrapped.UnsequencedEquals(other));
665
        wrapped.Shuffle();
666
        Assert.IsTrue(IC.seteq(wrapped.UniqueItems(), 4, 5, 6));
667
      }
668

    
669
      [Test]
670
      public void WithExc()
671
      {
672
        WrappedArray<int> wrapped = new WrappedArray<int>(new int[] { 3, 4, 6, 5, 7 });
673
        //
674
        try { wrapped.Add(1); Assert.Fail("No throw"); }
675
        catch (FixedSizeCollectionException) { }
676
        try { wrapped.AddAll<int>(null); Assert.Fail("No throw"); }
677
        catch (FixedSizeCollectionException) { }
678
        try { wrapped.Clear(); Assert.Fail("No throw"); }
679
        catch (FixedSizeCollectionException) { }
680
        try { wrapped.Dispose(); Assert.Fail("No throw"); }
681
        catch (FixedSizeCollectionException) { }
682
        int j = 1;
683
        try { wrapped.FindOrAdd(ref j); Assert.Fail("No throw"); }
684
        catch (FixedSizeCollectionException) { }
685
        try { wrapped.Insert(1, 1); Assert.Fail("No throw"); }
686
        catch (FixedSizeCollectionException) { }
687
        try { wrapped.Insert(wrapped.View(0, 0), 1); Assert.Fail("No throw"); }
688
        catch (FixedSizeCollectionException) { }
689
        try { wrapped.InsertAll<int>(1, null); Assert.Fail("No throw"); }
690
        catch (FixedSizeCollectionException) { }
691
        try { wrapped.InsertFirst(1); Assert.Fail("No throw"); }
692
        catch (FixedSizeCollectionException) { }
693
        try { wrapped.InsertLast(1); Assert.Fail("No throw"); }
694
        catch (FixedSizeCollectionException) { }
695
        try { wrapped.Remove(); Assert.Fail("No throw"); }
696
        catch (FixedSizeCollectionException) { }
697
        try { wrapped.Remove(1); Assert.Fail("No throw"); }
698
        catch (FixedSizeCollectionException) { }
699
        try { wrapped.RemoveAll<int>(null); Assert.Fail("No throw"); }
700
        catch (FixedSizeCollectionException) { }
701
        try { wrapped.RemoveAllCopies(1); Assert.Fail("No throw"); }
702
        catch (FixedSizeCollectionException) { }
703
        try { wrapped.RemoveAt(1); Assert.Fail("No throw"); }
704
        catch (FixedSizeCollectionException) { }
705
        try { wrapped.RemoveFirst(); Assert.Fail("No throw"); }
706
        catch (FixedSizeCollectionException) { }
707
        try { wrapped.RemoveInterval(0, 0); Assert.Fail("No throw"); }
708
        catch (FixedSizeCollectionException) { }
709
        try { wrapped.RemoveLast(); Assert.Fail("No throw"); }
710
        catch (FixedSizeCollectionException) { }
711
        try { wrapped.RetainAll<int>(null); Assert.Fail("No throw"); }
712
        catch (FixedSizeCollectionException) { }
713
        try { wrapped.Update(1, out j); Assert.Fail("No throw"); }
714
        catch (FixedSizeCollectionException) { }
715
        try { wrapped.UpdateOrAdd(1); Assert.Fail("No throw"); }
716
        catch (FixedSizeCollectionException) { }
717
      }
718

    
719
      [Test]
720
      public void View()
721
      {
722
        int[] inner = new int[] { 3, 4, 6, 5, 7 };
723
        WrappedArray<int> outerwrapped = new WrappedArray<int>(inner);
724
        WrappedArray<int> wrapped = (WrappedArray<int>)outerwrapped.View(1, 3);
725
        //
726
        Assert.AreEqual(6, wrapped[1]);
727
        Assert.IsTrue(IC.eq(wrapped[1, 2], 6, 5));
728
        //
729
        Fun<int, bool> is4 = delegate(int i) { return i == 4; };
730
        Assert.AreEqual(EventTypeEnum.None, wrapped.ActiveEvents);
731
        Assert.AreEqual(false, wrapped.All(is4));
732
        Assert.AreEqual(true, wrapped.AllowsDuplicates);
733
        wrapped.Apply(delegate(int i) { });
734
        Assert.AreEqual("{ 5, 6, 4 }", wrapped.Backwards().ToString());
735
        Assert.AreEqual(true, wrapped.Check());
736
        wrapped.Choose();
737
        Assert.AreEqual(true, wrapped.Contains(4));
738
        Assert.AreEqual(true, wrapped.ContainsAll(new ArrayList<int>()));
739
        Assert.AreEqual(1, wrapped.ContainsCount(4));
740
        Assert.AreEqual(Speed.Linear, wrapped.ContainsSpeed);
741
        int[] extarray = new int[5];
742
        wrapped.CopyTo(extarray, 1);
743
        Assert.IsTrue(IC.eq(extarray, 0, 4, 6, 5, 0));
744
        Assert.AreEqual(3, wrapped.Count);
745
        Assert.AreEqual(Speed.Constant, wrapped.CountSpeed);
746
        Assert.AreEqual(EnumerationDirection.Forwards, wrapped.Direction);
747
        Assert.AreEqual(false, wrapped.DuplicatesByCounting);
748
        Assert.AreEqual(IntEqualityComparer.Default, wrapped.EqualityComparer);
749
        Assert.AreEqual(true, wrapped.Exists(is4));
750
        Assert.IsTrue(IC.eq(wrapped.Filter(is4), 4));
751
        int j = 5;
752
        Assert.AreEqual(true, wrapped.Find(ref j));
753
        Assert.AreEqual(true, wrapped.Find(is4, out j));
754
        Assert.AreEqual("[ 0:4 ]", wrapped.FindAll(is4).ToString());
755
        Assert.AreEqual(0, wrapped.FindIndex(is4));
756
        Assert.AreEqual(true, wrapped.FindLast(is4, out j));
757
        Assert.AreEqual(0, wrapped.FindLastIndex(is4));
758
        Assert.AreEqual(4, wrapped.First);
759
        wrapped.GetEnumerator();
760
        Assert.AreEqual(CHC.sequencedhashcode(4, 6, 5), wrapped.GetSequencedHashCode());
761
        Assert.AreEqual(CHC.unsequencedhashcode(4, 6, 5), wrapped.GetUnsequencedHashCode());
762
        Assert.AreEqual(Speed.Constant, wrapped.IndexingSpeed);
763
        Assert.AreEqual(2, wrapped.IndexOf(5));
764
        Assert.AreEqual(false, wrapped.IsEmpty);
765
        Assert.AreEqual(true, wrapped.IsReadOnly);
766
        Assert.AreEqual(false, wrapped.IsSorted());
767
        Assert.AreEqual(true, wrapped.IsValid);
768
        Assert.AreEqual(5, wrapped.Last);
769
        Assert.AreEqual(2, wrapped.LastIndexOf(5));
770
        Assert.AreEqual(EventTypeEnum.None, wrapped.ListenableEvents);
771
        Fun<int, string> i2s = delegate(int i) { return string.Format("T{0}", i); };
772
        Assert.AreEqual("[ 0:T4, 1:T6, 2:T5 ]", wrapped.Map<string>(i2s).ToString());
773
        Assert.AreEqual(1, wrapped.Offset);
774
        wrapped.Reverse();
775
        Assert.AreEqual("[ 0:5, 1:6, 2:4 ]", wrapped.ToString());
776
        IList<int> other = new ArrayList<int>(); other.AddAll<int>(new int[] { 4, 5, 6 });
777
        Assert.IsFalse(wrapped.SequencedEquals(other));
778
        j = 30;
779
        Assert.AreEqual(true, wrapped.Show(new System.Text.StringBuilder(), ref j, null));
780
        wrapped.Sort();
781
        Assert.AreEqual("[ 0:4, 1:5, 2:6 ]", wrapped.ToString());
782
        Assert.IsTrue(IC.eq(wrapped.ToArray(), 4, 5, 6));
783
        Assert.AreEqual("[ ... ]", wrapped.ToString("L4", null));
784
        Assert.AreEqual(outerwrapped, wrapped.Underlying);
785
        Assert.IsTrue(IC.seteq(wrapped.UniqueItems(), 4, 5, 6));
786
        Assert.IsTrue(wrapped.UnsequencedEquals(other));
787
        //
788
        Assert.IsTrue(wrapped.TrySlide(1));
789
        Assert.IsTrue(IC.eq(wrapped, 5, 6, 7));
790
        Assert.IsTrue(wrapped.TrySlide(-1, 2));
791
        Assert.IsTrue(IC.eq(wrapped, 4, 5));
792
        Assert.IsFalse(wrapped.TrySlide(-2));
793
        Assert.IsTrue(IC.eq(wrapped.Span(outerwrapped.ViewOf(7)), 4, 5, 6, 7));
794
        //
795
        wrapped.Shuffle();
796
        Assert.IsTrue(IC.seteq(wrapped.UniqueItems(), 4, 5));
797
        Assert.IsTrue(wrapped.IsValid);
798
        wrapped.Dispose();
799
        Assert.IsFalse(wrapped.IsValid);
800
      }
801

    
802
      [Test]
803
      public void ViewWithExc()
804
      {
805
        int[] inner = new int[] { 3, 4, 6, 5, 7 };
806
        WrappedArray<int> outerwrapped = new WrappedArray<int>(inner);
807
        WrappedArray<int> wrapped = (WrappedArray<int>)outerwrapped.View(1, 3);
808
        //
809
        try { wrapped.Add(1); Assert.Fail("No throw"); }
810
        catch (FixedSizeCollectionException) { }
811
        try { wrapped.AddAll<int>(null); Assert.Fail("No throw"); }
812
        catch (FixedSizeCollectionException) { }
813
        try { wrapped.Clear(); Assert.Fail("No throw"); }
814
        catch (FixedSizeCollectionException) { }
815
        //Should not throw
816
        //try { wrapped.Dispose(); Assert.Fail("No throw"); }
817
        //catch (FixedSizeCollectionException) { }
818
        int j = 1;
819
        try { wrapped.FindOrAdd(ref j); Assert.Fail("No throw"); }
820
        catch (FixedSizeCollectionException) { }
821
        try { wrapped.Insert(1, 1); Assert.Fail("No throw"); }
822
        catch (FixedSizeCollectionException) { }
823
        try { wrapped.Insert(wrapped.View(0, 0), 1); Assert.Fail("No throw"); }
824
        catch (FixedSizeCollectionException) { }
825
        try { wrapped.InsertAll<int>(1, null); Assert.Fail("No throw"); }
826
        catch (FixedSizeCollectionException) { }
827
        try { wrapped.InsertFirst(1); Assert.Fail("No throw"); }
828
        catch (FixedSizeCollectionException) { }
829
        try { wrapped.InsertLast(1); Assert.Fail("No throw"); }
830
        catch (FixedSizeCollectionException) { }
831
        try { wrapped.Remove(); Assert.Fail("No throw"); }
832
        catch (FixedSizeCollectionException) { }
833
        try { wrapped.Remove(1); Assert.Fail("No throw"); }
834
        catch (FixedSizeCollectionException) { }
835
        try { wrapped.RemoveAll<int>(null); Assert.Fail("No throw"); }
836
        catch (FixedSizeCollectionException) { }
837
        try { wrapped.RemoveAllCopies(1); Assert.Fail("No throw"); }
838
        catch (FixedSizeCollectionException) { }
839
        try { wrapped.RemoveAt(1); Assert.Fail("No throw"); }
840
        catch (FixedSizeCollectionException) { }
841
        try { wrapped.RemoveFirst(); Assert.Fail("No throw"); }
842
        catch (FixedSizeCollectionException) { }
843
        try { wrapped.RemoveInterval(0, 0); Assert.Fail("No throw"); }
844
        catch (FixedSizeCollectionException) { }
845
        try { wrapped.RemoveLast(); Assert.Fail("No throw"); }
846
        catch (FixedSizeCollectionException) { }
847
        try { wrapped.RetainAll<int>(null); Assert.Fail("No throw"); }
848
        catch (FixedSizeCollectionException) { }
849
        try { wrapped.Update(1, out j); Assert.Fail("No throw"); }
850
        catch (FixedSizeCollectionException) { }
851
        try { wrapped.UpdateOrAdd(1); Assert.Fail("No throw"); }
852
        catch (FixedSizeCollectionException) { }
853
      }
854
    }
855
  }
856
}
857