Merge pull request #948 from ermshiperete/bug-xamarin-2394
[mono.git] / mcs / class / corlib / Test / System.Collections.ObjectModel / CollectionTest.cs
1 //
2 // MonoTests.System.Collections.Generic.Test.CollectionTest
3 //
4 // Authors:
5 //      David Waite (mass@akuma.org)
6 //
7 // Copyright (C) 2005 David Waite
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 using NUnit.Framework;
37
38 namespace MonoTests.System.Collections.ObjectModel
39 {
40         [TestFixture]
41         public class CollectionTest
42         {
43                 [Test]
44                 public void UsableSyncLockTest ()
45                 {
46                         List <int> list = new List <int> ();
47                         Collection <int> c = new Collection <int> (list);
48
49                         object listLock = ((ICollection) list).SyncRoot;
50                         object cLock = ((ICollection) c).SyncRoot;
51
52                         Assert.AreSame (listLock, cLock);
53                 }
54
55                 [Test]
56                 public void UnusableSyncLockTest ()
57                 {
58                         UnimplementedList <int> list = new UnimplementedList <int> ();
59                         Collection <int> c = new Collection <int> (list);
60
61                         object cLock = ((ICollection) c).SyncRoot;
62
63                         Assert.IsNotNull (cLock);
64                         Assert.IsTrue (cLock.GetType ().Equals (typeof (object)));
65                 }
66
67                 [Test]
68                 public void ICollection_CopyTo ()
69                 {
70                         Collection <int> c = new Collection <int> ();
71                         c.Add (10);
72                         c.Add (7);
73
74                         Array array = Array.CreateInstance (typeof (int), 2);
75                         ((ICollection) c).CopyTo (array, 0);
76                         Assert.AreEqual (10, array.GetValue (0), "#A1");
77                         Assert.AreEqual (7, array.GetValue (1), "#A2");
78
79                         array = Array.CreateInstance (typeof (int), 5);
80                         ((ICollection) c).CopyTo (array, 2);
81                         Assert.AreEqual (0, array.GetValue (0), "#B1");
82                         Assert.AreEqual (0, array.GetValue (1), "#B2");
83                         Assert.AreEqual (10, array.GetValue (2), "#B3");
84                         Assert.AreEqual (7, array.GetValue (3), "#B4");
85                         Assert.AreEqual (0, array.GetValue (4), "#B5");
86
87                         array = Array.CreateInstance (typeof (object), 5);
88                         ((ICollection) c).CopyTo (array, 2);
89                         Assert.IsNull (array.GetValue (0), "#C1");
90                         Assert.IsNull (array.GetValue (1), "#C2");
91                         Assert.AreEqual (10, array.GetValue (2), "#C3");
92                         Assert.AreEqual (7, array.GetValue (3), "#C4");
93                         Assert.IsNull (array.GetValue (4), "#C2");
94                 }
95
96                 class UnimplementedList <T> : IList <T>
97                 {
98
99                         #region IList <T> Members
100
101                         int IList <T>.IndexOf (T item)
102                         {
103                                 throw new Exception ("The method or operation is not implemented.");
104                         }
105
106                         void IList <T>.Insert (int index, T item)
107                         {
108                                 throw new Exception ("The method or operation is not implemented.");
109                         }
110
111                         void IList <T>.RemoveAt (int index)
112                         {
113                                 throw new Exception ("The method or operation is not implemented.");
114                         }
115
116                         T IList <T>.this [int index]
117                         {
118                                 get
119                                 {
120                                         throw new Exception ("The method or operation is not implemented.");
121                                 }
122                                 set
123                                 {
124                                         throw new Exception ("The method or operation is not implemented.");
125                                 }
126                         }
127
128                         #endregion
129
130                         #region ICollection <T> Members
131
132                         void ICollection <T>.Add (T item)
133                         {
134                                 throw new Exception ("The method or operation is not implemented.");
135                         }
136
137                         void ICollection <T>.Clear ()
138                         {
139                                 throw new Exception ("The method or operation is not implemented.");
140                         }
141
142                         bool ICollection <T>.Contains (T item)
143                         {
144                                 throw new Exception ("The method or operation is not implemented.");
145                         }
146
147                         void ICollection <T>.CopyTo (T [] array, int arrayIndex)
148                         {
149                                 throw new Exception ("The method or operation is not implemented.");
150                         }
151
152                         int ICollection <T>.Count
153                         {
154                                 get { throw new Exception ("The method or operation is not implemented."); }
155                         }
156
157                         bool ICollection <T>.IsReadOnly
158                         {
159                                 get { throw new Exception ("The method or operation is not implemented."); }
160                         }
161
162                         bool ICollection <T>.Remove (T item)
163                         {
164                                 throw new Exception ("The method or operation is not implemented.");
165                         }
166
167                         #endregion
168
169                         #region IEnumerable <T> Members
170
171                         IEnumerator <T> IEnumerable <T>.GetEnumerator ()
172                         {
173                                 throw new Exception ("The method or operation is not implemented.");
174                         }
175
176                         #endregion
177
178                         #region IEnumerable Members
179
180                         IEnumerator IEnumerable.GetEnumerator ()
181                         {
182                                 throw new Exception ("The method or operation is not implemented.");
183                         }
184
185                         #endregion
186 }
187         }
188 }
189
190 #endif