merge -r 60814:60815
[mono.git] / mcs / class / System / Test / System.Collections.Specialized / StringCollectionTest.cs
1 // System.Collections.Specialized.StringCollection.cs
2 //
3 // Authors:
4 //   John Barnette (jbarn@httcb.net)
5 //   Martin Willemoes Hansen (mwh@sysrq.dk)
6 //   Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 //  (C) Copyright 2001 John Barnette
9 //  (C) Copyright 2003 Martin Willemoes Hansen
10 //  Copyright (C) 2005 Novell, Inc (http://www.novell.com)
11 //
12
13 using NUnit.Framework;
14 using System.Collections;
15 using System.Collections.Specialized;
16
17 namespace MonoTests.System.Collections.Specialized {
18
19         [TestFixture]
20         public class StringCollectionTest {
21
22                 private StringCollection sc;
23                 string[] strings = {
24                         "foo",
25                         "bar",
26                         "baz",
27                         "john",
28                         "paul",
29                         "george",
30                         "ringo"
31                 };
32                 
33                 [SetUp]
34                 public void GetReady() 
35                 {
36                         sc = new StringCollection();
37                         sc.AddRange(strings);
38                 }
39
40                 // Simple Tests
41                 [Test]
42                 public void SimpleCount() 
43                 {
44                         Assertion.Assert(sc.Count == 7);
45                 }
46                 
47                 [Test]
48                 public void SimpleIsReadOnly() 
49                 {
50                         Assertion.Assert(!sc.IsReadOnly);
51                 }
52                 
53                 [Test]
54                 public void SimpleIsSynchronized() 
55                 {
56                         Assertion.Assert(!sc.IsSynchronized);
57                 }
58                 
59                 [Test]
60                 public void SimpleItemGet() 
61                 {
62                         for(int i = 0; i < strings.Length; i++) {
63                                 Assertion.Assert(strings[i].Equals(sc[i]));
64                         }
65                 }
66                 
67                 [Test]
68                 public void SimpleItemSet() 
69                 {
70                         sc[0] = "bob";
71                         Assertion.Assert(sc[0].Equals("bob"));
72                 }
73                 
74                 [Test]
75 #if NET_2_0
76                 [Category ("NotDotNet")] // SyncRoot != this on 2.0
77 #endif
78                 public void SimpleSyncRoot() 
79                 {
80                         Assertion.Assert(sc.Equals(sc.SyncRoot));
81                 }
82                 
83                 [Test]
84                 public void SimpleAdd() 
85                 {
86                         int index = sc.Add("chuck");
87                         Assertion.Assert(index == strings.Length);
88                         Assertion.Assert(sc[strings.Length].Equals("chuck"));
89                 }
90                 
91                 [Test]
92                 public void SimpleAddRange() 
93                 {
94                         string[] newStrings = {
95                                 "peter",
96                                 "paul",
97                                 "mary"
98                         };
99                         
100                         int index = sc.Count;
101                         sc.AddRange(newStrings);
102                         
103                         Assertion.Assert(sc.Count == index + newStrings.Length);
104                         
105                         for (int i = 0; i+index <= sc.Count-1; i++) {
106                                 Assertion.Assert(newStrings[i].Equals(sc[i+index]));
107                         }
108                 }
109                 
110                 [Test]
111                 public void SimpleClear() 
112                 {
113                         sc.Clear();
114                         Assertion.Assert(sc.Count == 0);
115                 }
116                 
117                 [Test]
118                 public void SimpleContains() 
119                 {
120                         Assertion.Assert(sc.Contains(strings[0]));
121                         Assertion.Assert(!sc.Contains("NOT CONTAINED"));
122                 }
123                 
124                 [Test]
125                 public void SimpleCopyTo() 
126                 {
127                         string[] copyArray = new string[sc.Count];
128                         sc.CopyTo(copyArray, 0);
129                         for (int i = 0; i < copyArray.Length; i++) {
130                                 Assertion.Assert(copyArray[i] == sc[i]);
131                         }
132                 }
133                 
134                 [Test]
135                 public void SimpleGetEnumerator() 
136                 {
137                         int index = 0;
138                         foreach(string s in sc) {
139                                 Assertion.Assert(s.Equals(strings[index]));
140                                 index++;
141                         }
142                 }
143                 
144                 [Test]
145                 public void SimpleIndexOf() 
146                 {
147                         Assertion.Assert(sc.IndexOf(strings[0]) == 0);
148                 }
149                 
150                 [Test]
151                 public void SimpleInsert() 
152                 {
153                         int index = 3;
154                         int oldCount = sc.Count;
155                         string before  = sc[index - 1];
156                         string current = sc[index];
157                         string after   = sc[index + 1];
158                         string newStr  = "paco";
159                         
160                         sc.Insert(index, newStr);
161                         
162                         Assertion.Assert(sc.Count == oldCount + 1);
163                         Assertion.Assert(sc[index].Equals(newStr));
164                         Assertion.Assert(sc[index-1].Equals(before));
165                         Assertion.Assert(sc[index+1].Equals(current));
166                         Assertion.Assert(sc[index+2].Equals(after));
167                 }
168                 
169                 [Test]
170                 public void SimpleRemove() 
171                 {
172                         int oldCount = sc.Count;
173                         sc.Remove(strings[0]);
174                         Assertion.Assert(oldCount == sc.Count + 1);
175                         Assertion.Assert(!sc.Contains(strings[0]));
176                 }
177                 
178                 [Test]
179                 public void SimpleRemoveAt() 
180                 {
181                         int index = 3;
182                         int oldCount = sc.Count;
183                         string after = sc[index+1];
184                         
185                         sc.RemoveAt(index);
186                         Assertion.Assert(oldCount == sc.Count + 1);
187                         Assertion.Assert(sc[index].Equals(after));
188                 }
189
190                 [Test]
191                 public void IList ()
192                 {
193                         IList list = (IList) new StringCollection ();
194                         Assert.AreEqual (0, list.Count, "Count-0");
195                         Assert.IsFalse (list.IsFixedSize, "IsFixedSize");
196                         Assert.IsFalse (list.IsFixedSize, "IsReadOnly");
197
198                         list.Add ("a");
199                         Assert.AreEqual (1, list.Count, "Count-1");
200                         Assert.IsTrue (list.Contains ("a"), "Contains(b)");
201                         Assert.IsFalse (list.Contains ("b"), "Contains(b)");
202
203                         Assert.AreEqual (0, list.IndexOf ("a"), "IndexOf(a)");
204                         Assert.AreEqual (-1, list.IndexOf ("b"), "IndexOf(b)");
205
206                         list.Insert (0, "b");
207                         Assert.AreEqual (2, list.Count, "Count-2");
208                         list.Remove ("b");
209                         Assert.AreEqual (1, list.Count, "Count-3");
210
211                         list.Add ("b");
212                         list.RemoveAt (0);
213                         Assert.AreEqual (1, list.Count, "Count-4");
214
215                         list.Clear ();
216                         Assert.AreEqual (0, list.Count, "Count-5");
217                 }
218
219                 [Test]
220                 public void ICollection ()
221                 {
222                         ICollection coll = (ICollection) new StringCollection ();
223                         Assert.AreEqual (0, coll.Count, "Count");
224                         Assert.IsNotNull (coll.GetEnumerator (), "GetEnumerator");
225                         coll.CopyTo (new string[0], 0);
226                         Assert.IsFalse (coll.IsSynchronized, "IsSynchronized");
227                         Assert.IsNotNull (coll.SyncRoot, "SyncRoot");
228                 }
229
230                 [Test]
231                 public void IEnumerable ()
232                 {
233                         IEnumerable e = (IEnumerable) new StringCollection ();
234                         Assert.IsNotNull (e.GetEnumerator (), "GetEnumerator");
235                 }
236         }
237 }