New test.
[mono.git] / mcs / class / Mono.C5 / Test / WrappersTest.cs
1 /*\r
2  Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft\r
3  Permission is hereby granted, free of charge, to any person obtaining a copy\r
4  of this software and associated documentation files (the "Software"), to deal\r
5  in the Software without restriction, including without limitation the rights\r
6  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
7  copies of the Software, and to permit persons to whom the Software is\r
8  furnished to do so, subject to the following conditions:\r
9  \r
10  The above copyright notice and this permission notice shall be included in\r
11  all copies or substantial portions of the Software.\r
12  \r
13  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
14  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
15  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
16  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
17  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
18  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\r
19  SOFTWARE.\r
20 */\r
21 \r
22 using System;\r
23 using C5;\r
24 using NUnit.Framework;\r
25 using SCG = System.Collections.Generic;\r
26 \r
27 \r
28 namespace C5UnitTests.wrappers\r
29 {\r
30   namespace Events\r
31   {\r
32     [TestFixture]\r
33     public class IList_\r
34     {\r
35       private ArrayList<int> list;\r
36       ICollectionValue<int> guarded;\r
37       CollectionEventList<int> seen;\r
38 \r
39       [SetUp]\r
40       public void Init()\r
41       {\r
42         list = new ArrayList<int>(TenEqualityComparer.Default);\r
43         guarded = new GuardedList<int>(list);\r
44         seen = new CollectionEventList<int>(IntEqualityComparer.Default);\r
45       }\r
46 \r
47       private void listen() { seen.Listen(guarded, EventTypeEnum.All); }\r
48 \r
49       [Test]\r
50       public void Listenable()\r
51       {\r
52         Assert.AreEqual(EventTypeEnum.All, guarded.ListenableEvents);\r
53         Assert.AreEqual(EventTypeEnum.None, guarded.ActiveEvents);\r
54         listen();\r
55         Assert.AreEqual(EventTypeEnum.All, guarded.ActiveEvents);\r
56       }\r
57 \r
58       [Test]\r
59       public void SetThis()\r
60       {\r
61         list.Add(4); list.Add(56); list.Add(8);\r
62         listen();\r
63         list[1] = 45;\r
64         seen.Check(new CollectionEvent<int>[] {\r
65           new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(56, 1), guarded),\r
66           new CollectionEvent<int>(EventTypeEnum.RemovedAt, new ItemAtEventArgs<int>(56,1), guarded),\r
67           new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(45, 1), guarded),\r
68           new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(45,1), guarded),\r
69           new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)\r
70           });\r
71       }\r
72 \r
73       [Test]\r
74       public void Insert()\r
75       {\r
76         list.Add(4); list.Add(56); list.Add(8);\r
77         listen();\r
78         list.Insert(1, 45);\r
79         seen.Check(new CollectionEvent<int>[] {\r
80           new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(45,1), guarded),\r
81           new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(45, 1), guarded),\r
82           new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)\r
83           });\r
84       }\r
85 \r
86       [Test]\r
87       public void InsertAll()\r
88       {\r
89         list.Add(4); list.Add(56); list.Add(8);\r
90         listen();\r
91         list.InsertAll<int>(1, new int[] { 666, 777, 888 });\r
92         //seen.Print(Console.Error);\r
93         seen.Check(new CollectionEvent<int>[] {\r
94           new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(666,1), guarded),\r
95           new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(666, 1), guarded),\r
96           new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(777,2), guarded),\r
97           new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(777, 1), guarded),\r
98           new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(888,3), guarded),\r
99           new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(888, 1), guarded),\r
100           new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)\r
101           });\r
102         list.InsertAll<int>(1, new int[] {});\r
103         seen.Check(new CollectionEvent<int>[] {});\r
104       }\r
105 \r
106       [Test]\r
107       public void InsertFirstLast()\r
108       {\r
109         list.Add(4); list.Add(56); list.Add(8);\r
110         listen();\r
111         list.InsertFirst(45);\r
112         seen.Check(new CollectionEvent<int>[] {\r
113           new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(45,0), guarded),\r
114           new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(45, 1), guarded),\r
115           new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)\r
116           });\r
117         list.InsertLast(88);\r
118         seen.Check(new CollectionEvent<int>[] {\r
119           new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(88,4), guarded),\r
120           new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(88, 1), guarded),\r
121           new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)\r
122           });\r
123       }\r
124 \r
125       [Test]\r
126       public void Remove()\r
127       {\r
128         list.Add(4); list.Add(56); list.Add(8);\r
129         listen();\r
130         list.Remove();\r
131         seen.Check(new CollectionEvent<int>[] {\r
132           new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(8, 1), guarded),\r
133           new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});\r
134       }\r
135 \r
136       [Test]\r
137       public void RemoveFirst()\r
138       {\r
139         list.Add(4); list.Add(56); list.Add(8);\r
140         listen();\r
141         list.RemoveFirst();\r
142         seen.Check(new CollectionEvent<int>[] {\r
143           new CollectionEvent<int>(EventTypeEnum.RemovedAt, new ItemAtEventArgs<int>(4,0), guarded),\r
144           new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(4, 1), guarded),\r
145           new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});\r
146       }\r
147 \r
148       [Test]\r
149       public void RemoveLast()\r
150       {\r
151         list.Add(4); list.Add(56); list.Add(8);\r
152         listen();\r
153         list.RemoveLast();\r
154         seen.Check(new CollectionEvent<int>[] {\r
155           new CollectionEvent<int>(EventTypeEnum.RemovedAt, new ItemAtEventArgs<int>(8,2), guarded),\r
156           new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(8, 1), guarded),\r
157           new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});\r
158       }\r
159 \r
160       [Test]\r
161       public void Reverse()\r
162       {\r
163         list.Add(4); list.Add(56); list.Add(8);\r
164         listen();\r
165         list.Reverse();\r
166         seen.Check(new CollectionEvent<int>[] {\r
167           new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)\r
168         });\r
169         list.View(1, 0).Reverse();\r
170         seen.Check(new CollectionEvent<int>[] {});\r
171       }\r
172 \r
173 \r
174       [Test]\r
175       public void Sort()\r
176       {\r
177         list.Add(4); list.Add(56); list.Add(8);\r
178         listen();\r
179         list.Sort();\r
180         seen.Check(new CollectionEvent<int>[] {\r
181           new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)\r
182         });\r
183         list.View(1, 0).Sort();\r
184         seen.Check(new CollectionEvent<int>[] {});\r
185       }\r
186 \r
187       [Test]\r
188       public void Shuffle()\r
189       {\r
190         list.Add(4); list.Add(56); list.Add(8);\r
191         listen();\r
192         list.Shuffle();\r
193         seen.Check(new CollectionEvent<int>[] {\r
194           new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)\r
195         });\r
196         list.View(1, 0).Shuffle();\r
197         seen.Check(new CollectionEvent<int>[] {});\r
198       }\r
199 \r
200       [Test]\r
201       public void RemoveAt()\r
202       {\r
203         list.Add(4); list.Add(56); list.Add(8);\r
204         listen();\r
205         list.RemoveAt(1);\r
206         seen.Check(new CollectionEvent<int>[] {\r
207           new CollectionEvent<int>(EventTypeEnum.RemovedAt, new ItemAtEventArgs<int>(56,1), guarded),\r
208           new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(56, 1), guarded),\r
209           new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});\r
210       }\r
211 \r
212       [Test]\r
213       public void RemoveInterval()\r
214       {\r
215         list.Add(4); list.Add(56); list.Add(8);\r
216         listen();\r
217         list.RemoveInterval(1, 2);\r
218         seen.Check(new CollectionEvent<int>[] {\r
219            new CollectionEvent<int>(EventTypeEnum.Cleared, new ClearedRangeEventArgs(false,2,1), guarded),\r
220          new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)\r
221         });\r
222         list.RemoveInterval(1, 0);\r
223         seen.Check(new CollectionEvent<int>[] {});\r
224       }\r
225 \r
226       [Test]\r
227       public void Update()\r
228       {\r
229         list.Add(4); list.Add(56); list.Add(8);\r
230         listen();\r
231         list.Update(53);\r
232         seen.Check(new CollectionEvent<int>[] {\r
233           new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(56, 1), guarded),\r
234           new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(53, 1), guarded),\r
235           new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)\r
236           });\r
237         list.Update(67);\r
238         seen.Check(new CollectionEvent<int>[] {});\r
239       }\r
240 \r
241       [Test]\r
242       public void FindOrAdd()\r
243       {\r
244         list.Add(4); list.Add(56); list.Add(8);\r
245         listen();\r
246         int val = 53;\r
247         list.FindOrAdd(ref val);\r
248         seen.Check(new CollectionEvent<int>[] {});\r
249         val = 67;\r
250         list.FindOrAdd(ref val);\r
251         seen.Check(new CollectionEvent<int>[] { \r
252           new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(67, 1), guarded),\r
253           new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)\r
254         });\r
255       }\r
256 \r
257       [Test]\r
258       public void UpdateOrAdd()\r
259       {\r
260         list.Add(4); list.Add(56); list.Add(8);\r
261         listen();\r
262         int val = 53;\r
263         list.UpdateOrAdd(val);\r
264         seen.Check(new CollectionEvent<int>[] {\r
265           new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(56, 1), guarded),\r
266           new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(53, 1), guarded),\r
267           new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)\r
268         });\r
269         val = 67;\r
270         list.UpdateOrAdd(val);\r
271         seen.Check(new CollectionEvent<int>[] { \r
272           new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(67, 1), guarded),\r
273           new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)\r
274         });\r
275         list.UpdateOrAdd(51, out val);\r
276         seen.Check(new CollectionEvent<int>[] {\r
277           new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(53, 1), guarded),\r
278           new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(51, 1), guarded),\r
279           new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)\r
280         });\r
281         val = 67;\r
282         list.UpdateOrAdd(81, out val);\r
283         seen.Check(new CollectionEvent<int>[] { \r
284           new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(81, 1), guarded),\r
285           new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)\r
286         });\r
287       }\r
288 \r
289       [Test]\r
290       public void RemoveItem()\r
291       {\r
292         list.Add(4); list.Add(56); list.Add(18);\r
293         listen();\r
294         list.Remove(53);\r
295         seen.Check(new CollectionEvent<int>[] {\r
296           new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(56, 1), guarded),\r
297           new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});\r
298         list.Remove(11);\r
299         seen.Check(new CollectionEvent<int>[] {\r
300           new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(18, 1), guarded),\r
301           new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});\r
302       }\r
303 \r
304       [Test]\r
305       public void RemoveAll()\r
306       {\r
307         for (int i = 0; i < 10; i++)\r
308         {\r
309           list.Add(10 * i + 5);\r
310         }\r
311         listen();\r
312         list.RemoveAll<int>(new int[] { 32, 187, 45 });\r
313         //TODO: the order depends on internals of the HashSet\r
314         seen.Check(new CollectionEvent<int>[] {\r
315           new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(35, 1), guarded),\r
316           new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(45, 1), guarded),\r
317           new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});\r
318         list.RemoveAll<int>(new int[] { 200, 300 });\r
319         seen.Check(new CollectionEvent<int>[] {});\r
320       }\r
321 \r
322       [Test]\r
323       public void Clear()\r
324       {\r
325         list.Add(4); list.Add(56); list.Add(8);\r
326         listen();\r
327         list.View(1, 1).Clear();\r
328         seen.Check(new CollectionEvent<int>[] {\r
329           new CollectionEvent<int>(EventTypeEnum.Cleared, new ClearedRangeEventArgs(false,1,1), guarded),\r
330           new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)\r
331         });\r
332         list.Clear();\r
333         seen.Check(new CollectionEvent<int>[] {\r
334           new CollectionEvent<int>(EventTypeEnum.Cleared, new ClearedRangeEventArgs(true,2,0), guarded),\r
335           new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)\r
336         });\r
337         list.Clear();\r
338         seen.Check(new CollectionEvent<int>[] {});\r
339       }\r
340 \r
341       [Test]\r
342       public void ListDispose()\r
343       {\r
344         list.Add(4); list.Add(56); list.Add(8);\r
345         listen();\r
346         list.View(1, 1).Dispose();\r
347         seen.Check(new CollectionEvent<int>[] {});\r
348         list.Dispose();\r
349         seen.Check(new CollectionEvent<int>[] {\r
350           new CollectionEvent<int>(EventTypeEnum.Cleared, new ClearedRangeEventArgs(true,3,0), guarded),\r
351           new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)\r
352         });\r
353         list.Dispose();\r
354         seen.Check(new CollectionEvent<int>[] {});\r
355       }\r
356 \r
357 \r
358       [Test]\r
359       public void RetainAll()\r
360       {\r
361         for (int i = 0; i < 10; i++)\r
362         {\r
363           list.Add(10 * i + 5);\r
364         }\r
365         listen();\r
366         list.RetainAll<int>(new int[] { 32, 187, 45, 62, 82, 95, 2 });\r
367         seen.Check(new CollectionEvent<int>[] {\r
368           new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(15, 1), guarded),\r
369           new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(25, 1), guarded),\r
370           new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(55, 1), guarded),\r
371           new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(75, 1), guarded),\r
372           new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});\r
373         list.RetainAll<int>(new int[] { 32, 187, 45, 62, 82, 95, 2 });\r
374         seen.Check(new CollectionEvent<int>[] {});\r
375       }\r
376 \r
377       [Test]\r
378       public void RemoveAllCopies()\r
379       {\r
380         for (int i = 0; i < 10; i++)\r
381         {\r
382           list.Add(3 * i + 5);\r
383         }\r
384         listen();\r
385         list.RemoveAllCopies(14);\r
386         seen.Check(new CollectionEvent<int>[] {\r
387           new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(11, 1), guarded),\r
388           new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(14, 1), guarded),\r
389           new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(17, 1), guarded),\r
390           new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});\r
391         list.RemoveAllCopies(14);\r
392         seen.Check(new CollectionEvent<int>[] {});\r
393       }\r
394 \r
395       [Test]\r
396       public void Add()\r
397       {\r
398         listen();\r
399         seen.Check(new CollectionEvent<int>[0]);\r
400         list.Add(23);\r
401         seen.Check(new CollectionEvent<int>[] {\r
402           new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(23, 1), guarded),\r
403           new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});\r
404       }\r
405 \r
406       [Test]\r
407       public void AddAll()\r
408       {\r
409         for (int i = 0; i < 10; i++)\r
410         {\r
411           list.Add(10 * i + 5);\r
412         }\r
413         listen();\r
414         list.AddAll<int>(new int[] { 45, 56, 67 });\r
415         seen.Check(new CollectionEvent<int>[] {\r
416           new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(45, 1), guarded),\r
417           new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(56, 1), guarded),\r
418           new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(67, 1), guarded),\r
419           new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});\r
420         list.AddAll<int>(new int[] { });\r
421         seen.Check(new CollectionEvent<int>[] {});\r
422       }\r
423 \r
424       [TearDown]\r
425       public void Dispose() { list = null; seen = null; }\r
426 \r
427       [Test]\r
428       [ExpectedException(typeof(UnlistenableEventException))]\r
429       public void ViewChanged()\r
430       {\r
431         IList<int> w = list.View(0, 0);\r
432         w.CollectionChanged += new CollectionChangedHandler<int>(w_CollectionChanged);\r
433       }\r
434 \r
435       [Test]\r
436       [ExpectedException(typeof(UnlistenableEventException))]\r
437       public void ViewCleared()\r
438       {\r
439         IList<int> w = list.View(0, 0);\r
440         w.CollectionCleared += new CollectionClearedHandler<int>(w_CollectionCleared);\r
441       }\r
442 \r
443       [Test]\r
444       [ExpectedException(typeof(UnlistenableEventException))]\r
445       public void ViewAdded()\r
446       {\r
447         IList<int> w = list.View(0, 0);\r
448         w.ItemsAdded += new ItemsAddedHandler<int>(w_ItemAdded);\r
449       }\r
450 \r
451       [Test]\r
452       [ExpectedException(typeof(UnlistenableEventException))]\r
453       public void ViewInserted()\r
454       {\r
455         IList<int> w = list.View(0, 0);\r
456         w.ItemInserted += new ItemInsertedHandler<int>(w_ItemInserted);\r
457       }\r
458 \r
459       [Test]\r
460       [ExpectedException(typeof(UnlistenableEventException))]\r
461       public void ViewRemoved()\r
462       {\r
463         IList<int> w = list.View(0, 0);\r
464         w.ItemsRemoved += new ItemsRemovedHandler<int>(w_ItemRemoved);\r
465       }\r
466 \r
467       [Test]\r
468       [ExpectedException(typeof(UnlistenableEventException))]\r
469       public void ViewRemovedAt()\r
470       {\r
471         IList<int> w = list.View(0, 0);\r
472         w.ItemRemovedAt += new ItemRemovedAtHandler<int>(w_ItemRemovedAt);\r
473       }\r
474 \r
475       void w_CollectionChanged(object sender)\r
476       {\r
477         throw new NotImplementedException();\r
478       }\r
479 \r
480       void w_CollectionCleared(object sender, ClearedEventArgs eventArgs)\r
481       {\r
482         throw new NotImplementedException();\r
483       }\r
484 \r
485       void w_ItemAdded(object sender, ItemCountEventArgs<int> eventArgs)\r
486       {\r
487         throw new NotImplementedException();\r
488       }\r
489 \r
490       void w_ItemInserted(object sender, ItemAtEventArgs<int> eventArgs)\r
491       {\r
492         throw new NotImplementedException();\r
493       }\r
494 \r
495       void w_ItemRemoved(object sender, ItemCountEventArgs<int> eventArgs)\r
496       {\r
497         throw new NotImplementedException();\r
498       }\r
499 \r
500       void w_ItemRemovedAt(object sender, ItemAtEventArgs<int> eventArgs)\r
501       {\r
502         throw new NotImplementedException();\r
503       }\r
504     }\r
505 \r
506     [TestFixture]\r
507     public class StackQueue\r
508     {\r
509       private ArrayList<int> list;\r
510       ICollectionValue<int> guarded;\r
511       CollectionEventList<int> seen;\r
512 \r
513       [SetUp]\r
514       public void Init()\r
515       {\r
516         list = new ArrayList<int>(TenEqualityComparer.Default);\r
517         guarded = new GuardedList<int>(list);\r
518         seen = new CollectionEventList<int>(IntEqualityComparer.Default);\r
519       }\r
520 \r
521       private void listen() { seen.Listen(guarded, EventTypeEnum.All); }\r
522 \r
523 \r
524       [Test]\r
525       public void EnqueueDequeue()\r
526       {\r
527         listen();\r
528         list.Enqueue(67);\r
529         seen.Check(new CollectionEvent<int>[] {\r
530           new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(67,0), guarded),\r
531           new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(67, 1), guarded),\r
532           new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});\r
533         list.Enqueue(2);\r
534         seen.Check(new CollectionEvent<int>[] {\r
535           new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(2,1), guarded),\r
536           new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(2, 1), guarded),\r
537           new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});\r
538         list.Dequeue();\r
539         seen.Check(new CollectionEvent<int>[] {\r
540           new CollectionEvent<int>(EventTypeEnum.RemovedAt, new ItemAtEventArgs<int>(67,0), guarded),\r
541           new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(67, 1), guarded),\r
542           new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});\r
543         list.Dequeue();\r
544         seen.Check(new CollectionEvent<int>[] {\r
545           new CollectionEvent<int>(EventTypeEnum.RemovedAt, new ItemAtEventArgs<int>(2,0), guarded),\r
546           new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(2, 1), guarded),\r
547           new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});\r
548       }\r
549 \r
550       [Test]\r
551       public void PushPop()\r
552       {\r
553         listen();\r
554         seen.Check(new CollectionEvent<int>[0]);\r
555         list.Push(23);\r
556         seen.Check(new CollectionEvent<int>[] {\r
557           new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(23,0), guarded),\r
558           new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(23, 1), guarded),\r
559           new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});\r
560         list.Push(-12);\r
561         seen.Check(new CollectionEvent<int>[] {\r
562           new CollectionEvent<int>(EventTypeEnum.Inserted, new ItemAtEventArgs<int>(-12,1), guarded),\r
563           new CollectionEvent<int>(EventTypeEnum.Added, new ItemCountEventArgs<int>(-12, 1), guarded),\r
564           new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});\r
565         list.Pop();\r
566         seen.Check(new CollectionEvent<int>[] {\r
567           new CollectionEvent<int>(EventTypeEnum.RemovedAt, new ItemAtEventArgs<int>(-12,1), guarded),\r
568           new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(-12, 1), guarded),\r
569           new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});\r
570         list.Pop();\r
571         seen.Check(new CollectionEvent<int>[] {\r
572           new CollectionEvent<int>(EventTypeEnum.RemovedAt, new ItemAtEventArgs<int>(23,0), guarded),\r
573           new CollectionEvent<int>(EventTypeEnum.Removed, new ItemCountEventArgs<int>(23, 1), guarded),\r
574           new CollectionEvent<int>(EventTypeEnum.Changed, new EventArgs(), guarded)});\r
575       }\r
576 \r
577       [TearDown]\r
578       public void Dispose() { list = null; seen = null; }\r
579     }\r
580 \r
581 \r
582   }\r
583 \r
584   namespace wrappedarray\r
585   {\r
586     [TestFixture]\r
587     public class Basic\r
588     {\r
589 \r
590       [SetUp]\r
591       public void Init()\r
592       {\r
593       }\r
594 \r
595       [TearDown]\r
596       public void Dispose()\r
597       {\r
598       }\r
599 \r
600       [Test]\r
601       public void NoExc()\r
602       {\r
603         WrappedArray<int> wrapped = new WrappedArray<int>(new int[] { 4, 6, 5 });\r
604         Assert.AreEqual(6, wrapped[1]);\r
605         Assert.IsTrue(IC.eq(wrapped[1, 2], 6, 5));\r
606         //\r
607         Fun<int, bool> is4 = delegate(int i) { return i == 4; };\r
608         Assert.AreEqual(EventTypeEnum.None, wrapped.ActiveEvents);\r
609         Assert.AreEqual(false, wrapped.All(is4));\r
610         Assert.AreEqual(true, wrapped.AllowsDuplicates);\r
611         wrapped.Apply(delegate(int i) { });\r
612         Assert.AreEqual("{ 5, 6, 4 }", wrapped.Backwards().ToString());\r
613         Assert.AreEqual(true, wrapped.Check());\r
614         wrapped.Choose();\r
615         Assert.AreEqual(true, wrapped.Contains(4));\r
616         Assert.AreEqual(true, wrapped.ContainsAll(new ArrayList<int>()));\r
617         Assert.AreEqual(1, wrapped.ContainsCount(4));\r
618         Assert.AreEqual(Speed.Linear, wrapped.ContainsSpeed);\r
619         int[] extarray = new int[5];\r
620         wrapped.CopyTo(extarray, 1);\r
621         Assert.IsTrue(IC.eq(extarray, 0, 4, 6, 5, 0));\r
622         Assert.AreEqual(3, wrapped.Count);\r
623         Assert.AreEqual(Speed.Constant, wrapped.CountSpeed);\r
624         Assert.AreEqual(EnumerationDirection.Forwards, wrapped.Direction);\r
625         Assert.AreEqual(false, wrapped.DuplicatesByCounting);\r
626         Assert.AreEqual(IntEqualityComparer.Default, wrapped.EqualityComparer);\r
627         Assert.AreEqual(true, wrapped.Exists(is4));\r
628         Assert.IsTrue(IC.eq(wrapped.Filter(is4), 4));\r
629         int j = 5;\r
630         Assert.AreEqual(true, wrapped.Find(ref j));\r
631         Assert.AreEqual(true, wrapped.Find(is4, out j));\r
632         Assert.AreEqual("[ 0:4 ]", wrapped.FindAll(is4).ToString());\r
633         Assert.AreEqual(0, wrapped.FindIndex(is4));\r
634         Assert.AreEqual(true, wrapped.FindLast(is4, out j));\r
635         Assert.AreEqual(0, wrapped.FindLastIndex(is4));\r
636         Assert.AreEqual(4, wrapped.First);\r
637         wrapped.GetEnumerator();\r
638         Assert.AreEqual(CHC.sequencedhashcode(4, 6, 5), wrapped.GetSequencedHashCode());\r
639         Assert.AreEqual(CHC.unsequencedhashcode(4, 6, 5), wrapped.GetUnsequencedHashCode());\r
640         Assert.AreEqual(Speed.Constant, wrapped.IndexingSpeed);\r
641         Assert.AreEqual(2, wrapped.IndexOf(5));\r
642         Assert.AreEqual(false, wrapped.IsEmpty);\r
643         Assert.AreEqual(true, wrapped.IsReadOnly);\r
644         Assert.AreEqual(false, wrapped.IsSorted());\r
645         Assert.AreEqual(true, wrapped.IsValid);\r
646         Assert.AreEqual(5, wrapped.Last);\r
647         Assert.AreEqual(2, wrapped.LastIndexOf(5));\r
648         Assert.AreEqual(EventTypeEnum.None, wrapped.ListenableEvents);\r
649         Fun<int, string> i2s = delegate(int i) { return string.Format("T{0}", i); };\r
650         Assert.AreEqual("[ 0:T4, 1:T6, 2:T5 ]", wrapped.Map<string>(i2s).ToString());\r
651         Assert.AreEqual(0, wrapped.Offset);\r
652         wrapped.Reverse();\r
653         Assert.AreEqual("[ 0:5, 1:6, 2:4 ]", wrapped.ToString());\r
654         IList<int> other = new ArrayList<int>(); other.AddAll<int>(new int[] { 4, 5, 6 });\r
655         Assert.IsFalse(wrapped.SequencedEquals(other));\r
656         j = 30;\r
657         Assert.AreEqual(true, wrapped.Show(new System.Text.StringBuilder(), ref j, null));\r
658         wrapped.Sort();\r
659         Assert.AreEqual("[ 0:4, 1:5, 2:6 ]", wrapped.ToString());\r
660         Assert.IsNotNull(wrapped.SyncRoot);\r
661         Assert.IsTrue(IC.eq(wrapped.ToArray(), 4, 5, 6));\r
662         Assert.AreEqual("[ ... ]", wrapped.ToString("L4", null));\r
663         Assert.AreEqual(null, wrapped.Underlying);\r
664         Assert.IsTrue(IC.seteq(wrapped.UniqueItems(), 4, 5, 6));\r
665         Assert.IsTrue(wrapped.UnsequencedEquals(other));\r
666         wrapped.Shuffle();\r
667         Assert.IsTrue(IC.seteq(wrapped.UniqueItems(), 4, 5, 6));\r
668       }\r
669 \r
670       [Test]\r
671       public void WithExc()\r
672       {\r
673         WrappedArray<int> wrapped = new WrappedArray<int>(new int[] { 3, 4, 6, 5, 7 });\r
674         //\r
675         try { wrapped.Add(1); Assert.Fail("No throw"); }\r
676         catch (FixedSizeCollectionException) { }\r
677         try { wrapped.AddAll<int>(null); Assert.Fail("No throw"); }\r
678         catch (FixedSizeCollectionException) { }\r
679         try { wrapped.Clear(); Assert.Fail("No throw"); }\r
680         catch (FixedSizeCollectionException) { }\r
681         try { wrapped.Dispose(); Assert.Fail("No throw"); }\r
682         catch (FixedSizeCollectionException) { }\r
683         int j = 1;\r
684         try { wrapped.FindOrAdd(ref j); Assert.Fail("No throw"); }\r
685         catch (FixedSizeCollectionException) { }\r
686         try { wrapped.Insert(1, 1); Assert.Fail("No throw"); }\r
687         catch (FixedSizeCollectionException) { }\r
688         try { wrapped.Insert(wrapped.View(0, 0), 1); Assert.Fail("No throw"); }\r
689         catch (FixedSizeCollectionException) { }\r
690         try { wrapped.InsertAll<int>(1, null); Assert.Fail("No throw"); }\r
691         catch (FixedSizeCollectionException) { }\r
692         try { wrapped.InsertFirst(1); Assert.Fail("No throw"); }\r
693         catch (FixedSizeCollectionException) { }\r
694         try { wrapped.InsertLast(1); Assert.Fail("No throw"); }\r
695         catch (FixedSizeCollectionException) { }\r
696         try { wrapped.Remove(); Assert.Fail("No throw"); }\r
697         catch (FixedSizeCollectionException) { }\r
698         try { wrapped.Remove(1); Assert.Fail("No throw"); }\r
699         catch (FixedSizeCollectionException) { }\r
700         try { wrapped.RemoveAll<int>(null); Assert.Fail("No throw"); }\r
701         catch (FixedSizeCollectionException) { }\r
702         try { wrapped.RemoveAllCopies(1); Assert.Fail("No throw"); }\r
703         catch (FixedSizeCollectionException) { }\r
704         try { wrapped.RemoveAt(1); Assert.Fail("No throw"); }\r
705         catch (FixedSizeCollectionException) { }\r
706         try { wrapped.RemoveFirst(); Assert.Fail("No throw"); }\r
707         catch (FixedSizeCollectionException) { }\r
708         try { wrapped.RemoveInterval(0, 0); Assert.Fail("No throw"); }\r
709         catch (FixedSizeCollectionException) { }\r
710         try { wrapped.RemoveLast(); Assert.Fail("No throw"); }\r
711         catch (FixedSizeCollectionException) { }\r
712         try { wrapped.RetainAll<int>(null); Assert.Fail("No throw"); }\r
713         catch (FixedSizeCollectionException) { }\r
714         try { wrapped.Update(1, out j); Assert.Fail("No throw"); }\r
715         catch (FixedSizeCollectionException) { }\r
716         try { wrapped.UpdateOrAdd(1); Assert.Fail("No throw"); }\r
717         catch (FixedSizeCollectionException) { }\r
718       }\r
719 \r
720       [Test]\r
721       public void View()\r
722       {\r
723         WrappedArray<int> outerwrapped = new WrappedArray<int>(new int[] { 3, 4, 6, 5, 7 });\r
724         WrappedArray<int> wrapped = (WrappedArray<int>)outerwrapped.View(1, 3);\r
725         //\r
726         Assert.AreEqual(6, wrapped[1]);\r
727         Assert.IsTrue(IC.eq(wrapped[1, 2], 6, 5));\r
728         //\r
729         Fun<int, bool> is4 = delegate(int i) { return i == 4; };\r
730         Assert.AreEqual(EventTypeEnum.None, wrapped.ActiveEvents);\r
731         Assert.AreEqual(false, wrapped.All(is4));\r
732         Assert.AreEqual(true, wrapped.AllowsDuplicates);\r
733         wrapped.Apply(delegate(int i) { });\r
734         Assert.AreEqual("{ 5, 6, 4 }", wrapped.Backwards().ToString());\r
735         Assert.AreEqual(true, wrapped.Check());\r
736         wrapped.Choose();\r
737         Assert.AreEqual(true, wrapped.Contains(4));\r
738         Assert.AreEqual(true, wrapped.ContainsAll(new ArrayList<int>()));\r
739         Assert.AreEqual(1, wrapped.ContainsCount(4));\r
740         Assert.AreEqual(Speed.Linear, wrapped.ContainsSpeed);\r
741         int[] extarray = new int[5];\r
742         wrapped.CopyTo(extarray, 1);\r
743         Assert.IsTrue(IC.eq(extarray, 0, 4, 6, 5, 0));\r
744         Assert.AreEqual(3, wrapped.Count);\r
745         Assert.AreEqual(Speed.Constant, wrapped.CountSpeed);\r
746         Assert.AreEqual(EnumerationDirection.Forwards, wrapped.Direction);\r
747         Assert.AreEqual(false, wrapped.DuplicatesByCounting);\r
748         Assert.AreEqual(IntEqualityComparer.Default, wrapped.EqualityComparer);\r
749         Assert.AreEqual(true, wrapped.Exists(is4));\r
750         Assert.IsTrue(IC.eq(wrapped.Filter(is4), 4));\r
751         int j = 5;\r
752         Assert.AreEqual(true, wrapped.Find(ref j));\r
753         Assert.AreEqual(true, wrapped.Find(is4, out j));\r
754         Assert.AreEqual("[ 0:4 ]", wrapped.FindAll(is4).ToString());\r
755         Assert.AreEqual(0, wrapped.FindIndex(is4));\r
756         Assert.AreEqual(true, wrapped.FindLast(is4, out j));\r
757         Assert.AreEqual(0, wrapped.FindLastIndex(is4));\r
758         Assert.AreEqual(4, wrapped.First);\r
759         wrapped.GetEnumerator();\r
760         Assert.AreEqual(CHC.sequencedhashcode(4, 6, 5), wrapped.GetSequencedHashCode());\r
761         Assert.AreEqual(CHC.unsequencedhashcode(4, 6, 5), wrapped.GetUnsequencedHashCode());\r
762         Assert.AreEqual(Speed.Constant, wrapped.IndexingSpeed);\r
763         Assert.AreEqual(2, wrapped.IndexOf(5));\r
764         Assert.AreEqual(false, wrapped.IsEmpty);\r
765         Assert.AreEqual(true, wrapped.IsReadOnly);\r
766         Assert.AreEqual(false, wrapped.IsSorted());\r
767         Assert.AreEqual(true, wrapped.IsValid);\r
768         Assert.AreEqual(5, wrapped.Last);\r
769         Assert.AreEqual(2, wrapped.LastIndexOf(5));\r
770         Assert.AreEqual(EventTypeEnum.None, wrapped.ListenableEvents);\r
771         Fun<int, string> i2s = delegate(int i) { return string.Format("T{0}", i); };\r
772         Assert.AreEqual("[ 0:T4, 1:T6, 2:T5 ]", wrapped.Map<string>(i2s).ToString());\r
773         Assert.AreEqual(1, wrapped.Offset);\r
774         wrapped.Reverse();\r
775         Assert.AreEqual("[ 0:5, 1:6, 2:4 ]", wrapped.ToString());\r
776         IList<int> other = new ArrayList<int>(); other.AddAll<int>(new int[] { 4, 5, 6 });\r
777         Assert.IsFalse(wrapped.SequencedEquals(other));\r
778         j = 30;\r
779         Assert.AreEqual(true, wrapped.Show(new System.Text.StringBuilder(), ref j, null));\r
780         wrapped.Sort();\r
781         Assert.AreEqual("[ 0:4, 1:5, 2:6 ]", wrapped.ToString());\r
782         Assert.IsNotNull(wrapped.SyncRoot);\r
783         Assert.IsTrue(IC.eq(wrapped.ToArray(), 4, 5, 6));\r
784         Assert.AreEqual("[ ... ]", wrapped.ToString("L4", null));\r
785         Assert.AreEqual(outerwrapped, wrapped.Underlying);\r
786         Assert.IsTrue(IC.seteq(wrapped.UniqueItems(), 4, 5, 6));\r
787         Assert.IsTrue(wrapped.UnsequencedEquals(other));\r
788         //\r
789         Assert.IsTrue(wrapped.TrySlide(1));\r
790         Assert.IsTrue(IC.eq(wrapped, 5, 6, 7));\r
791         Assert.IsTrue(wrapped.TrySlide(-1, 2));\r
792         Assert.IsTrue(IC.eq(wrapped, 4, 5));\r
793         Assert.IsFalse(wrapped.TrySlide(-2));\r
794         Assert.IsTrue(IC.eq(wrapped.Span(outerwrapped.ViewOf(7)), 4, 5, 6, 7));\r
795         //\r
796         wrapped.Shuffle();\r
797         Assert.IsTrue(IC.seteq(wrapped.UniqueItems(), 4, 5));\r
798         Assert.IsTrue(wrapped.IsValid);\r
799         wrapped.Dispose();\r
800         Assert.IsFalse(wrapped.IsValid);\r
801       }\r
802 \r
803       [Test]\r
804       public void ViewWithExc()\r
805       {\r
806         WrappedArray<int> outerwrapped = new WrappedArray<int>(new int[] { 3, 4, 6, 5, 7 });\r
807         WrappedArray<int> wrapped = (WrappedArray<int>)outerwrapped.View(1, 3);\r
808         //\r
809         try { wrapped.Add(1); Assert.Fail("No throw"); }\r
810         catch (FixedSizeCollectionException) { }\r
811         try { wrapped.AddAll<int>(null); Assert.Fail("No throw"); }\r
812         catch (FixedSizeCollectionException) { }\r
813         try { wrapped.Clear(); Assert.Fail("No throw"); }\r
814         catch (FixedSizeCollectionException) { }\r
815         //Should not throw\r
816         //try { wrapped.Dispose(); Assert.Fail("No throw"); }\r
817         //catch (FixedSizeCollectionException) { }\r
818         int j = 1;\r
819         try { wrapped.FindOrAdd(ref j); Assert.Fail("No throw"); }\r
820         catch (FixedSizeCollectionException) { }\r
821         try { wrapped.Insert(1, 1); Assert.Fail("No throw"); }\r
822         catch (FixedSizeCollectionException) { }\r
823         try { wrapped.Insert(wrapped.View(0, 0), 1); Assert.Fail("No throw"); }\r
824         catch (FixedSizeCollectionException) { }\r
825         try { wrapped.InsertAll<int>(1, null); Assert.Fail("No throw"); }\r
826         catch (FixedSizeCollectionException) { }\r
827         try { wrapped.InsertFirst(1); Assert.Fail("No throw"); }\r
828         catch (FixedSizeCollectionException) { }\r
829         try { wrapped.InsertLast(1); Assert.Fail("No throw"); }\r
830         catch (FixedSizeCollectionException) { }\r
831         try { wrapped.Remove(); Assert.Fail("No throw"); }\r
832         catch (FixedSizeCollectionException) { }\r
833         try { wrapped.Remove(1); Assert.Fail("No throw"); }\r
834         catch (FixedSizeCollectionException) { }\r
835         try { wrapped.RemoveAll<int>(null); Assert.Fail("No throw"); }\r
836         catch (FixedSizeCollectionException) { }\r
837         try { wrapped.RemoveAllCopies(1); Assert.Fail("No throw"); }\r
838         catch (FixedSizeCollectionException) { }\r
839         try { wrapped.RemoveAt(1); Assert.Fail("No throw"); }\r
840         catch (FixedSizeCollectionException) { }\r
841         try { wrapped.RemoveFirst(); Assert.Fail("No throw"); }\r
842         catch (FixedSizeCollectionException) { }\r
843         try { wrapped.RemoveInterval(0, 0); Assert.Fail("No throw"); }\r
844         catch (FixedSizeCollectionException) { }\r
845         try { wrapped.RemoveLast(); Assert.Fail("No throw"); }\r
846         catch (FixedSizeCollectionException) { }\r
847         try { wrapped.RetainAll<int>(null); Assert.Fail("No throw"); }\r
848         catch (FixedSizeCollectionException) { }\r
849         try { wrapped.Update(1, out j); Assert.Fail("No throw"); }\r
850         catch (FixedSizeCollectionException) { }\r
851         try { wrapped.UpdateOrAdd(1); Assert.Fail("No throw"); }\r
852         catch (FixedSizeCollectionException) { }\r
853       }\r
854     }\r
855   }\r
856 }\r
857 \r