Merge pull request #948 from ermshiperete/bug-xamarin-2394
[mono.git] / mcs / class / corlib / Test / System.Collections.ObjectModel / ReadOnlyCollectionTest.cs
1 //
2 // MonoTests.System.Collections.Generic.Test.ReadOnlyCollectionTest
3 //
4 // Authors:
5 //      Gert Driesen (drieseng@users.sourceforge.net)
6 //
7 // Copyright (C) 2008 Gert Driesen
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 #if NET_2_0
30
31 using System;
32 using System.Collections;
33 using System.Collections.Generic;
34 using System.Collections.ObjectModel;
35 using System.Text;
36
37 using NUnit.Framework;
38
39 namespace MonoTests.System.Collections.ObjectModel
40 {
41         class SyncPretendingList<T> : List<T>, ICollection
42         {
43                 bool ICollection.IsSynchronized {
44                         get { return true; }
45                 }
46         }
47
48         [TestFixture]
49         public class ReadOnlyCollectionTest
50         {
51                 [Test]
52                 public void Constructor0 ()
53                 {
54                         Collection <int> c = new Collection <int> ();
55                         c.Add (10);
56                         c.Add (7);
57
58                         ReadOnlyCollection <int> r = new ReadOnlyCollection <int> (c);
59                         Assert.AreEqual (10, r [0], "#1");
60                         Assert.AreEqual (7, r [1], "#2");
61                 }
62                 
63                 [Test]
64                 public void IsSimpleWrapper ()
65                 {
66                         Collection <int> c = new Collection <int> ();
67                         c.Add (1);
68                         
69                         ReadOnlyCollection <int> r = new ReadOnlyCollection <int> (c);
70                         Assert.AreEqual (1, r.Count, "#1");                     
71
72                         c.Remove (1);
73                         Assert.AreEqual (0, r.Count, "#2");                     
74                 }
75                 
76                 [Test]
77                 public void IList_Properties ()
78                 {
79                         List <int> l = new List <int> ();
80                         ReadOnlyCollection <int> r = new ReadOnlyCollection <int> (l);
81
82                         Assert.IsTrue (((IList)r).IsReadOnly, "#1");
83                         Assert.IsTrue (((IList)r).IsFixedSize, "#2");
84                 }
85                 
86                 [Test]
87                 public void ICollection_Properties ()
88                 {
89                         List <int> l = new SyncPretendingList <int> ();
90                         ReadOnlyCollection <int> r = new ReadOnlyCollection <int> (l);
91
92                         Assert.IsFalse (((ICollection)r).IsSynchronized, "#1");
93                 }
94
95                 [Test]
96                 public void Constructor0_List_Null ()
97                 {
98                         try {
99                                 new ReadOnlyCollection <int> ((List <int>) null);
100                                 Assert.Fail ("#1");
101                         } catch (ArgumentNullException ex) {
102                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
103                                 Assert.IsNull (ex.InnerException, "#3");
104                                 Assert.IsNotNull (ex.Message, "#4");
105                                 Assert.IsNotNull (ex.ParamName, "#5");
106                                 Assert.AreEqual ("list", ex.ParamName, "#6");
107                         }
108                 }
109
110                 [Test]
111                 public void ICollection_CopyTo ()
112                 {
113                         Collection <int> c = new Collection <int> ();
114                         c.Add (10);
115                         c.Add (7);
116
117                         ReadOnlyCollection <int> r = new ReadOnlyCollection <int> (c);
118                         Array array = Array.CreateInstance (typeof (int), 2);
119                         ((ICollection) c).CopyTo (array, 0);
120                         Assert.AreEqual (10, array.GetValue (0), "#A1");
121                         Assert.AreEqual (7, array.GetValue (1), "#A2");
122
123                         array = Array.CreateInstance (typeof (object), 2);
124                         ((ICollection) c).CopyTo (array, 0);
125                         Assert.AreEqual (10, array.GetValue (0), "#B1");
126                         Assert.AreEqual (7, array.GetValue (1), "#B2");
127                 }
128         }
129 }
130
131 #endif