2 // MonoTests.System.Collections.Generic.Test.ReadOnlyCollectionTest
5 // Gert Driesen (drieseng@users.sourceforge.net)
7 // Copyright (C) 2008 Gert Driesen
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
32 using System.Collections;
33 using System.Collections.Generic;
34 using System.Collections.ObjectModel;
37 using NUnit.Framework;
39 namespace MonoTests.System.Collections.ObjectModel
41 class SyncPretendingList<T> : List<T>, ICollection
43 bool ICollection.IsSynchronized {
49 public class ReadOnlyCollectionTest
52 public void Constructor0 ()
54 Collection <int> c = new Collection <int> ();
58 ReadOnlyCollection <int> r = new ReadOnlyCollection <int> (c);
59 Assert.AreEqual (10, r [0], "#1");
60 Assert.AreEqual (7, r [1], "#2");
64 public void IsSimpleWrapper ()
66 Collection <int> c = new Collection <int> ();
69 ReadOnlyCollection <int> r = new ReadOnlyCollection <int> (c);
70 Assert.AreEqual (1, r.Count, "#1");
73 Assert.AreEqual (0, r.Count, "#2");
77 public void IList_Properties ()
79 List <int> l = new List <int> ();
80 ReadOnlyCollection <int> r = new ReadOnlyCollection <int> (l);
82 Assert.IsTrue (((IList)r).IsReadOnly, "#1");
83 Assert.IsTrue (((IList)r).IsFixedSize, "#2");
87 public void ICollection_Properties ()
89 List <int> l = new SyncPretendingList <int> ();
90 ReadOnlyCollection <int> r = new ReadOnlyCollection <int> (l);
92 Assert.IsFalse (((ICollection)r).IsSynchronized, "#1");
96 public void Constructor0_List_Null ()
99 new ReadOnlyCollection <int> ((List <int>) null);
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");
111 public void ICollection_CopyTo ()
113 Collection <int> c = new Collection <int> ();
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");
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");