Mark tests as not working under TARGET_JVM
[mono.git] / mcs / class / System / Test / System.Collections.Generic / SortedDictionaryTest.cs
1 //
2 // SortedDictionaryTest.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
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.Runtime.Serialization;
35
36 using NUnit.Framework;
37
38 namespace MonoTests.System.Collections.Generic
39 {
40         [TestFixture]
41         public class SortedDictionaryTest
42         {
43                 [Test]
44                 public void CtorNullComparer ()
45                 {
46                         SortedDictionary<int,string> sd =
47                                 new SortedDictionary<int,string> ((IComparer<int>) null);
48                         Assert.AreEqual (Comparer<int>.Default, sd.Comparer);
49                 }
50
51                 [Test]
52                 [ExpectedException (typeof (ArgumentNullException))]
53                 public void CtorNullDictionary ()
54                 {
55                         new SortedDictionary<int,string> (default (IDictionary<int,string>));
56                 }
57
58                 [Test]
59                 [ExpectedException (typeof (ArgumentNullException))]
60                 public void CtorComparerDictionaryNullComparer ()
61                 {
62                         new SortedDictionary<int,string> (default (IDictionary<int,string>), null);
63                 }
64
65                 [Test]
66                 [ExpectedException (typeof (ArgumentNullException))]
67                 public void CtorComparerDictionaryNullDictionary ()
68                 {
69                         new SortedDictionary<int,string> (null, default (IComparer<int>));
70                 }
71
72                 [Test]
73                 public void CtorDefault ()
74                 {
75                         SortedDictionary<int,string> d =
76                                 new SortedDictionary<int,string> ();
77                         Assert.IsNotNull (d.Comparer);
78                 }
79
80                 [Test]
81                 public void CtorDictionary ()
82                 {
83                         Dictionary<int,string> src = new Dictionary<int,string> ();
84                         src.Add (0, "Foo");
85                         src.Add (4, "Bar");
86                         src.Add (2, "Baz");
87                         SortedDictionary<int,string> d =
88                                 new SortedDictionary<int,string> (src);
89                         Assert.AreEqual (3, d.Count, "#1");
90                         Assert.AreEqual ("Bar", d [4], "#2");
91                         IDictionaryEnumerator e = d.GetEnumerator ();
92                         Assert.IsTrue (e.MoveNext (), "#3");
93                         Assert.AreEqual ("Foo", e.Value, "#4");
94                         Assert.IsTrue (e.MoveNext (), "#5");
95                         Assert.AreEqual ("Baz", e.Value, "#6");
96                         Assert.IsTrue (e.MoveNext (), "#7");
97                         Assert.AreEqual ("Bar", e.Value, "#8");
98
99                         src.Add (3, "Hoge"); // it does not affect.
100                         Assert.AreEqual (3, d.Count, "#9");
101                 }
102
103                 [Test]
104                 [ExpectedException (typeof (ArgumentException))]
105                 public void AddDuplicate ()
106                 {
107                         SortedDictionary<int,string> d =
108                                 new SortedDictionary<int,string> ();
109                         d.Add (0, "A");
110                         d.Add (0, "A");
111                 }
112
113                 [Test]
114                 public void AddNullValue ()
115                 {
116                         SortedDictionary<int,string> d =
117                                 new SortedDictionary<int,string> ();
118                         d.Add (0, null);
119                         d.Add (1, "A");
120                         d.Add (2, null);
121                         Assert.IsNull (d [0], "#0");
122                         Assert.AreEqual ("A", d [1], "#1");
123                         Assert.IsNull (d [2], "#2");
124                 }
125
126                 [Test]
127                 [ExpectedException (typeof (ArgumentNullException))]
128                 public void AddNullKey ()
129                 {
130                         SortedDictionary<string,string> d =
131                                 new SortedDictionary<string,string> ();
132                         d.Add (null, null);
133                 }
134
135                 [Test]
136                 [ExpectedException (typeof (ArgumentNullException))]
137                 [Category ("NotWorking")] // see bug #78019
138                 public void AddNullKeyNullable ()
139                 {
140                         SortedDictionary<Nullable<int>,string> d =
141                                 new SortedDictionary<Nullable<int>,string> ();
142                         d.Add (null, "TEST");
143                 }
144
145                 [Test]
146                 [ExpectedException (typeof (KeyNotFoundException))]
147                 public void GetItemNonexistent ()
148                 {
149                         SortedDictionary<int,int> d =
150                                 new SortedDictionary<int,int> ();
151                         Assert.AreEqual (0, d [0]); // does not exist.
152                 }
153
154                 [Test]
155                 public void SetItemNonexistent ()
156                 {
157                         SortedDictionary<int,int> d =
158                                 new SortedDictionary<int,int> ();
159                         d [0] = 1;
160                         Assert.AreEqual (1, d.Count);
161                 }
162
163                 [Test]
164                 public void SetItemExistent ()
165                 {
166                         SortedDictionary<int,int> d =
167                                 new SortedDictionary<int,int> ();
168                         d.Add (0, 0);
169                         Assert.AreEqual (1, d.Count, "#1");
170                         d [0] = 1;
171                         Assert.AreEqual (1, d.Count, "#2");
172                         Assert.AreEqual (1, d [0], "#3");
173                 }
174
175                 [Test]
176                 public void GetEnumerator1 ()
177                 {
178                         SortedDictionary<int,string> d =
179                                 new SortedDictionary<int,string> ();
180                         d.Add (1, "A");
181                         d.Add (3, "B");
182                         d.Add (2, "C");
183                         SortedDictionary<int,string>.Enumerator e = d.GetEnumerator ();
184                         Assert.IsTrue (e.MoveNext (), "#1");
185                         Assert.AreEqual ("A", e.Current.Value, "#2");
186                         Assert.IsTrue (e.MoveNext (), "#3");
187                         Assert.AreEqual ("C", e.Current.Value, "#4");
188                         Assert.IsTrue (e.MoveNext (), "#5");
189                         Assert.AreEqual ("B", e.Current.Value, "#6");
190                         Assert.IsFalse (e.MoveNext (), "#7");
191                 }
192
193                 [Test]
194                 [ExpectedException (typeof (InvalidOperationException))]
195                 public void GetEnumerator2 ()
196                 {
197                         SortedDictionary<int,string> d =
198                                 new SortedDictionary<int,string> ();
199                         d.Add (1, "A");
200                         d.Add (3, "B");
201                         d.Add (2, "C");
202                         IEnumerator e = d.GetEnumerator ();
203                         d.Add (4, "D");
204                         e.MoveNext ();
205                 }
206
207                 [Test]
208                 public void CustomComparer ()
209                 {
210                         SortedDictionary<int,string> d =
211                                 new SortedDictionary<int,string> (
212                                         ReverseComparer<int>.Instance);
213
214                         d.Add (1, "A");
215                         d.Add (3, "B");
216                         d.Add (2, "C");
217                         SortedDictionary<int,string>.Enumerator e = d.GetEnumerator ();
218                         Assert.IsTrue (e.MoveNext (), "#1");
219                         Assert.AreEqual ("B", e.Current.Value, "#2");
220                         Assert.IsTrue (e.MoveNext (), "#3");
221                         Assert.AreEqual ("C", e.Current.Value, "#4");
222                         Assert.IsTrue (e.MoveNext (), "#5");
223                         Assert.AreEqual ("A", e.Current.Value, "#6");
224                         Assert.IsFalse (e.MoveNext (), "#7");
225                 }
226
227                 [Test]
228                 public void Remove ()
229                 {
230                         SortedDictionary<int,string> d =
231                                 new SortedDictionary<int,string> ();
232                         Assert.IsFalse (d.Remove (0), "#1");
233                         d.Add (0, "Foo");
234                         Assert.IsTrue (d.Remove (0), "#2");
235                         Assert.IsFalse (d.Remove (0), "#3");
236                 }
237
238                 [Test]
239                 public void TryGetValue ()
240                 {
241                         SortedDictionary<int,string> d =
242                                 new SortedDictionary<int,string> ();
243                         string s;
244                         Assert.IsFalse (d.TryGetValue (0, out s), "#1");
245                         Assert.IsNull (s, "#2");
246                         d.Add (0, "Test");
247                         Assert.IsTrue (d.TryGetValue (0, out s), "#3");
248                         Assert.AreEqual ("Test", s, "#4");
249                         Assert.IsFalse (d.TryGetValue (1, out s), "#5");
250                         Assert.IsNull (s, "#6");
251                 }
252
253                 [Test]
254                 [ExpectedException (typeof (ArgumentNullException))]
255                 public void IDictionaryAddKeyNull ()
256                 {
257                         IDictionary d = new SortedDictionary<string,string> ();
258                         d.Add (null, "A");
259                 }
260
261                 [Test]
262                 [ExpectedException (typeof (ArgumentNullException))]
263                 public void IDictionaryAddKeyNullValueType ()
264                 {
265                         IDictionary d = new SortedDictionary<int,string> ();
266                         d.Add (null, "A");
267                 }
268
269                 [Test]
270                 public void IDictionaryAddValueNull ()
271                 {
272                         IDictionary d = new SortedDictionary<string,string> ();
273                         // If we simply check "if (value is TValue)" it won't pass.
274                         d.Add ("A", null);
275                 }
276
277                 [Test]
278                 [ExpectedException (typeof (ArgumentException))]
279                 public void IDictionaryAddValueNullValueType ()
280                 {
281                         IDictionary d = new SortedDictionary<string,int> ();
282                         // If we simply allow null it won't result in ArgumentException.
283                         d.Add ("A", null);
284                 }
285
286                 [Test]
287                 [ExpectedException (typeof (NotSupportedException))]
288                 public void KeysICollectionAdd ()
289                 {
290                         SortedDictionary<int,string> d = new SortedDictionary<int,string> ();
291                         d.Add (1, "A");
292                         ICollection<int> col = d.Keys;
293                         col.Add (2);
294                 }
295
296                 [Test]
297                 [ExpectedException (typeof (NotSupportedException))]
298                 public void KeysICollectionClear ()
299                 {
300                         SortedDictionary<int,string> d = new SortedDictionary<int,string> ();
301                         d.Add (1, "A");
302                         ICollection<int> col = d.Keys;
303                         col.Clear ();
304                 }
305
306                 [Test]
307                 [ExpectedException (typeof (NotSupportedException))]
308                 public void KeysICollectionRemove ()
309                 {
310                         SortedDictionary<int,string> d = new SortedDictionary<int,string> ();
311                         d.Add (1, "A");
312                         ICollection<int> col = d.Keys;
313                         col.Remove (1);
314                 }
315
316                 [Test]
317                 [ExpectedException (typeof (NotSupportedException))]
318                 public void ValuesICollectionAdd ()
319                 {
320                         SortedDictionary<int,string> d = new SortedDictionary<int,string> ();
321                         d.Add (1, "A");
322                         ICollection<string> col = d.Values;
323                         col.Add ("B");
324                 }
325
326                 [Test]
327                 [ExpectedException (typeof (NotSupportedException))]
328                 public void ValuesICollectionClear ()
329                 {
330                         SortedDictionary<int,string> d = new SortedDictionary<int,string> ();
331                         d.Add (1, "A");
332                         ICollection<string> col = d.Values;
333                         col.Clear ();
334                 }
335
336                 [Test]
337                 [ExpectedException (typeof (NotSupportedException))]
338                 public void ValuesICollectionRemove ()
339                 {
340                         SortedDictionary<int,string> d = new SortedDictionary<int,string> ();
341                         d.Add (1, "A");
342                         ICollection<string> col = d.Values;
343                         col.Remove ("A");
344                 }
345
346                 [Test]
347                 public void KeysGetEnumerator1 ()
348                 {
349                         SortedDictionary<int,string> d =
350                                 new SortedDictionary<int,string> ();
351                         d.Add (1, "A");
352                         d.Add (3, "B");
353                         d.Add (2, "C");
354                         IEnumerator e = d.Keys.GetEnumerator ();
355                         Assert.IsTrue (e.MoveNext (), "#1");
356                         Assert.AreEqual (1, e.Current, "#2");
357                         Assert.IsTrue (e.MoveNext (), "#3");
358                         Assert.AreEqual (2, e.Current, "#4");
359                         Assert.IsTrue (e.MoveNext (), "#5");
360                         Assert.AreEqual (3, e.Current, "#6");
361                         Assert.IsFalse (e.MoveNext (), "#7");
362                 }
363
364                 [Test]
365                 [ExpectedException (typeof (InvalidOperationException))]
366                 public void KeysGetEnumerator2 ()
367                 {
368                         SortedDictionary<int,string> d =
369                                 new SortedDictionary<int,string> ();
370                         d.Add (1, "A");
371                         d.Add (3, "B");
372                         d.Add (2, "C");
373                         IEnumerator e = d.Keys.GetEnumerator ();
374                         d.Add (4, "D");
375                         e.MoveNext ();
376                 }
377
378                 [Test]
379                 public void ValuesGetEnumerator1 ()
380                 {
381                         SortedDictionary<int,string> d =
382                                 new SortedDictionary<int,string> ();
383                         d.Add (1, "A");
384                         d.Add (3, "B");
385                         d.Add (2, "C");
386                         IEnumerator e = d.Values.GetEnumerator ();
387                         Assert.IsTrue (e.MoveNext (), "#1");
388                         Assert.AreEqual ("A", e.Current, "#2");
389                         Assert.IsTrue (e.MoveNext (), "#3");
390                         Assert.AreEqual ("C", e.Current, "#4");
391                         Assert.IsTrue (e.MoveNext (), "#5");
392                         Assert.AreEqual ("B", e.Current, "#6");
393                         Assert.IsFalse (e.MoveNext (), "#7");
394                 }
395
396                 [Test]
397                 [ExpectedException (typeof (InvalidOperationException))]
398                 public void ValuesGetEnumerator2 ()
399                 {
400                         SortedDictionary<int,string> d =
401                                 new SortedDictionary<int,string> ();
402                         d.Add (1, "A");
403                         d.Add (3, "B");
404                         d.Add (2, "C");
405                         IEnumerator e = d.Values.GetEnumerator ();
406                         d.Add (4, "D");
407                         e.MoveNext ();
408                 }
409         }
410
411         class ReverseComparer<T> : IComparer<T>
412         {
413                 static ReverseComparer<T> instance = new ReverseComparer<T> ();
414                 public static ReverseComparer<T> Instance {
415                         get { return instance; }
416                 }
417
418                 ReverseComparer ()
419                 {
420                 }
421
422                 public int Compare (T t1, T t2)
423                 {
424                         return Comparer<T>.Default.Compare (t2, t1);
425                 }
426         }
427 }
428
429 #endif