fixed tests
[mono.git] / mcs / class / System / Test / System.Collections.Generic / SortedListTest.cs
1 // 
2 // System.Collections.SortedListTest.cs
3 // 
4 // Author:
5 //   Zoltan Varga (vargaz@gmail.com)
6 // 
7
8 //
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 #if NET_2_0
32
33 using System;
34 using System.Collections;
35 using System.Collections.Generic;
36 using System.IO;
37 using System.Text;
38 using System.Runtime.Serialization;
39 using System.Runtime.Serialization.Formatters.Binary;
40 using NUnit.Framework;
41
42 namespace MonoTests.System.Collections.Generic
43 {
44         [TestFixture]
45         public class SortedListTest
46         {
47                 SortedList<int, string> list;
48                 SortedList<string, int> list2;
49
50                 [SetUp]
51                 public void SetUp () {
52                         list = new SortedList <int, string> ();
53
54                         list [0] = "A";
55                         list [5] = "C";
56                         list [2] = "B";
57
58                         list2 = new SortedList<string, int> ();
59                 }
60
61                 [Test]
62                 public void Item () {
63                         Assert.AreEqual ("A", list [0]);
64                         Assert.AreEqual ("B", list [2]);
65                         Assert.AreEqual ("C", list [5]);
66
67                         list [2] = "D";
68
69                         Assert.AreEqual ("D", list [2]);
70                 }
71
72                 [Test]
73                 [ExpectedException (typeof (ArgumentNullException))]
74                 public void ItemNullKey () {
75                         int i = list2 [null];
76                 }
77
78                 [Test]
79                 [ExpectedException (typeof (KeyNotFoundException))]
80                 public void ItemMissingKey () {
81                         string s = list [99];
82                 }
83 #if !TARGET_JVM
84                 [Test]
85                 public void Keys () {
86                         IList<int> keys = list.Keys;
87
88                         Assert.AreEqual (3, keys.Count);
89                         Assert.AreEqual (0, keys [0]);
90                         Assert.AreEqual (2, keys [1]);
91                         Assert.AreEqual (5, keys [2]);
92
93                         int[] arr = new int [4];
94                         keys.CopyTo (arr, 1);
95                         Assert.AreEqual (0, arr [1]);
96                         Assert.AreEqual (2, arr [2]);
97                         Assert.AreEqual (5, arr [3]);
98
99                         Assert.AreEqual (true, keys.Contains (2));
100                         Assert.AreEqual (false, keys.Contains (100));
101
102                         Assert.AreEqual (2, keys.IndexOf (5));
103                         Assert.AreEqual (-1, keys.IndexOf (100));
104
105                         int index = 0;
106                         arr [0] = 0;
107                         arr [1] = 0;
108                         arr [2] = 0;
109                         foreach (int i in keys)
110                                 arr [index ++] = i;
111                         Assert.AreEqual (0, arr [0]);
112                         Assert.AreEqual (2, arr [1]);
113                         Assert.AreEqual (5, arr [2]);
114                 }
115 #endif
116                 [Test]
117                 public void KeysNonGeneric () {
118                         ICollection keys = ((IDictionary)list).Keys;
119
120                         Assert.AreEqual (3, keys.Count);
121
122                         int[] arr = new int [4];
123                         keys.CopyTo (arr, 1);
124                         Assert.AreEqual (0, arr [1]);
125                         Assert.AreEqual (2, arr [2]);
126                         Assert.AreEqual (5, arr [3]);
127
128                         int index = 0;
129                         arr [0] = 0;
130                         arr [1] = 0;
131                         arr [2] = 0;
132                         foreach (int i in keys)
133                                 arr [index ++] = i;
134                         Assert.AreEqual (0, arr [0]);
135                         Assert.AreEqual (2, arr [1]);
136                         Assert.AreEqual (5, arr [2]);
137                 }
138
139                 [Test]
140                 public void Values () {
141                         IList<string> values = list.Values;
142
143                         Assert.AreEqual (3, values.Count);
144                         Assert.AreEqual ("A", values [0]);
145                         Assert.AreEqual ("B", values [1]);
146                         Assert.AreEqual ("C", values [2]);
147
148                         string[] arr = new string [4];
149                         values.CopyTo (arr, 1);
150                         Assert.AreEqual ("A", arr [1]);
151                         Assert.AreEqual ("B", arr [2]);
152                         Assert.AreEqual ("C", arr [3]);
153
154                         Assert.AreEqual (true, values.Contains ("B"));
155                         Assert.AreEqual (false, values.Contains ("X"));
156
157                         Assert.AreEqual (2, values.IndexOf ("C"));
158                         Assert.AreEqual (-1, values.IndexOf ("X"));
159
160                         int index = 0;
161                         arr [0] = null;
162                         arr [1] = null;
163                         arr [2] = null;
164                         foreach (string s in values)
165                                 arr [index ++] = s;
166                         Assert.AreEqual ("A", arr [0]);
167                         Assert.AreEqual ("B", arr [1]);
168                         Assert.AreEqual ("C", arr [2]);
169                 }
170
171                 [Test]
172                 public void ValuesNonGeneric () {
173                         ICollection values = ((IDictionary)list).Values;
174
175                         Assert.AreEqual (3, values.Count);
176
177                         string[] arr = new string [4];
178                         values.CopyTo (arr, 1);
179                         Assert.AreEqual ("A", arr [1]);
180                         Assert.AreEqual ("B", arr [2]);
181                         Assert.AreEqual ("C", arr [3]);
182
183                         int index = 0;
184                         arr [0] = null;
185                         arr [1] = null;
186                         arr [2] = null;
187                         foreach (string s in values)
188                                 arr [index ++] = s;
189                         Assert.AreEqual ("A", arr [0]);
190                         Assert.AreEqual ("B", arr [1]);
191                         Assert.AreEqual ("C", arr [2]);
192                 }
193
194                 [Test]
195                 public void KeysIDictionaryGeneric () {
196                         ICollection<int> keys = ((IDictionary<int,string>)list).Keys;
197
198                         Assert.AreEqual (3, keys.Count);
199                 }
200
201                 [Test]
202                 public void ValuesIDictionaryGeneric () {
203                         ICollection<string> values = ((IDictionary<int,string>)list).Values;
204
205                         Assert.AreEqual (3, values.Count);
206                 }
207
208                 public void Add () {
209                         list.Add (10, "D");
210
211                         Assert.AreEqual ("D", list [10]);
212                 }
213
214                 [Test]
215                 [ExpectedException (typeof (ArgumentNullException))]
216                 public void AddNullKey () {
217                         list2.Add (null, 10);
218                 }
219
220                 [Test]
221                 [ExpectedException (typeof (ArgumentException))]
222                 public void AddKeyAlreadyExists () {
223                         list.Add (10, "B");
224                         list.Add (10, "C");
225                 }
226
227                 [Test]
228                 public void ContainsKey () {
229                         Assert.AreEqual (true, list.ContainsKey (5));
230                         Assert.AreEqual (false, list.ContainsKey (10));
231                 }
232
233                 [Test]
234                 public void Remove () {
235                         Assert.AreEqual (true, list.Remove (5));
236                         Assert.AreEqual (false, list.Remove (5));
237                         Assert.AreEqual (false, list.Remove (10));
238                 }
239
240                 [Test]
241                 [ExpectedException (typeof (ArgumentNullException))]
242                 public void RemoveNullKey () {
243                         list2.Remove (null);
244                 }
245
246                 [Test]
247                 public void GetEnumerator () {
248                         int[] keys = new int [3];
249                         string[] values = new string [3];
250                         int i = 0;
251                         foreach (KeyValuePair <int, string> kvp in list) {
252                                 keys [i] = kvp.Key;
253                                 values [i] = kvp.Value;
254                                 i ++;
255                         }
256
257                         Assert.AreEqual (0, keys [0]);
258                         Assert.AreEqual (2, keys [1]);
259                         Assert.AreEqual (5, keys [2]);
260                         Assert.AreEqual ("A", values [0]);
261                         Assert.AreEqual ("B", values [1]);
262                         Assert.AreEqual ("C", values [2]);
263                 }
264         }
265 }
266
267 #endif