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