2010-04-02 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System / Test / System.Collections.Generic / SortedSetTest.cs
blob5249d1833ff52cf3e7caffb82248152a313bc9a3
1 //
2 // SortedSetTest.cs
3 //
4 // Author:
5 // Jb Evain <jbevain@novell.com>
6 //
7 // Copyright (C) 2010 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.
29 #if NET_4_0
31 using System;
32 using System.Collections;
33 using System.Collections.Generic;
34 using System.Linq;
35 using System.Runtime.Serialization;
37 using NUnit.Framework;
39 namespace MonoTests.System.Collections.Generic
41 [TestFixture]
42 public class SortedSetTest
44 [Test]
45 public void CtorNullComparer ()
47 var set = new SortedSet<int> ((IComparer<int>) null);
48 Assert.AreEqual (Comparer<int>.Default, set.Comparer);
51 [Test]
52 [ExpectedException (typeof (ArgumentNullException))]
53 public void CtorNullCollection ()
55 new SortedSet<int> (null as IEnumerable<int>);
58 [Test]
59 public void CtorDefault ()
61 var set = new SortedSet<int> ();
62 Assert.IsNotNull (set.Comparer);
65 [Test]
66 public void Add ()
68 var set = new SortedSet<int> ();
69 Assert.AreEqual (0, set.Count);
70 Assert.IsTrue (set.Add (2));
71 Assert.IsTrue (set.Add (4));
72 Assert.IsTrue (set.Add (3));
73 Assert.AreEqual (3, set.Count);
74 Assert.IsFalse (set.Add (2));
77 [Test]
78 public void Remove ()
80 var set = new SortedSet<int> ();
81 Assert.IsTrue (set.Add (2));
82 Assert.IsTrue (set.Add (4));
83 Assert.AreEqual (2, set.Count);
84 Assert.IsTrue (set.Remove (4));
85 Assert.IsTrue (set.Remove (2));
86 Assert.AreEqual (0, set.Count);
87 Assert.IsFalse (set.Remove (4));
88 Assert.IsFalse (set.Remove (2));
91 [Test]
92 public void Clear ()
94 var set = new SortedSet<int> { 2, 3, 4, 5 };
95 Assert.AreEqual (4, set.Count);
96 set.Clear ();
97 Assert.AreEqual (0, set.Count);
100 [Test]
101 public void Contains ()
103 var set = new SortedSet<int> { 2, 3, 4, 5 };
104 Assert.IsTrue (set.Contains (4));
105 Assert.IsFalse (set.Contains (7));
108 [Test]
109 public void GetEnumerator ()
111 var set = new SortedSet<int> { 5, 3, 1, 2, 6, 4 };
112 Assert.IsTrue (set.SequenceEqual (new [] { 1, 2, 3, 4, 5, 6 }));
115 [Test]
116 public void Reverse ()
118 var set = new SortedSet<int> { 5, 3, 1, 2, 6, 4 };
119 var reversed = set.Reverse ();
120 Assert.IsTrue (reversed.SequenceEqual (new [] { 6, 5, 4, 3, 2, 1 }));
123 [Test]
124 public void RemoveWhere ()
126 var set = new SortedSet<int> { 1, 2, 3, 4, 5, 6 };
127 Assert.AreEqual (3, set.RemoveWhere (i => i % 2 == 0));
128 Assert.AreEqual (3, set.Count);
129 Assert.IsTrue (set.SequenceEqual (new [] { 1, 3, 5 }));
133 [Test]
134 public void Max ()
136 var set = new SortedSet<int> { 1, 3, 12, 9 };
137 Assert.AreEqual (12, set.Max);
140 [Test]
141 public void Min ()
143 var set = new SortedSet<int> { 2, 3, 1, 9 };
144 Assert.AreEqual (1, set.Min);
147 [Test]
148 [ExpectedException (typeof (ArgumentException))]
149 public void GetViewBetweenLowerBiggerThanUpper ()
151 var set = new SortedSet<int> { 1, 2, 3, 4, 5, 6 };
152 set.GetViewBetween (4, 2);
155 [Test]
156 public void GetView ()
158 var set = new SortedSet<int> { 1, 3, 5, 7, 9 };
159 var view = set.GetViewBetween (3, 7);
161 Assert.IsTrue (view.SequenceEqual (new [] { 3, 5, 7 }));
164 [Test]
165 public void ViewAdd ()
167 var set = new SortedSet<int> { 1, 3, 5, 7 };
168 var view = set.GetViewBetween (3, 5);
170 Assert.IsTrue (view.Add (4));
171 Assert.IsTrue (view.Contains (4));
172 Assert.IsTrue (set.Contains (4));
174 Assert.IsFalse (view.Add (5));
177 [Test]
178 [ExpectedException (typeof (ArgumentOutOfRangeException))]
179 public void ViewAddOutOfRange ()
181 var set = new SortedSet<int> { 1, 3, 5, 7 };
182 var view = set.GetViewBetween (3, 5);
184 view.Add (7);
187 [Test]
188 public void ViewContains ()
190 var set = new SortedSet<int> { 1, 3, 5, 7, 9 };
191 var view = set.GetViewBetween (3, 7);
193 Assert.IsFalse (view.Contains (4));
194 Assert.IsTrue (view.Contains (3));
195 Assert.IsTrue (view.Contains (5));
198 [Test]
199 public void ViewRemove ()
201 var set = new SortedSet<int> { 1, 3, 5, 7, 9 };
202 var view = set.GetViewBetween (3, 7);
204 Assert.IsTrue (view.Remove (3));
205 Assert.IsFalse (view.Contains (3));
206 Assert.IsFalse (set.Contains (3));
207 Assert.IsFalse (view.Remove (9));
208 Assert.IsTrue (set.Contains (9));
211 [Test]
212 public void ViewClear ()
214 var set = new SortedSet<int> { 1, 3, 5, 7, 9 };
215 var view = set.GetViewBetween (3, 7);
217 view.Clear ();
219 Assert.IsTrue (set.SequenceEqual (new [] { 1, 9 }));
222 [Test]
223 [ExpectedException (typeof (ArgumentOutOfRangeException))]
224 public void ViewGetViewLowerOutOfRange ()
226 var set = new SortedSet<int> { 1, 3, 5, 7, 9 };
227 var view = set.GetViewBetween (3, 7);
228 view.GetViewBetween (2, 5);
231 [Test]
232 [ExpectedException (typeof (ArgumentOutOfRangeException))]
233 public void ViewGetViewUpperOutOfRange ()
235 var set = new SortedSet<int> { 1, 3, 5, 7, 9 };
236 var view = set.GetViewBetween (3, 7);
237 view.GetViewBetween (5, 9);
240 [Test]
241 public void ViewGetView ()
243 var set = new SortedSet<int> { 1, 3, 5, 7, 9 };
244 var view = set.GetViewBetween (3, 7);
245 view = view.GetViewBetween (4, 6);
247 Assert.IsTrue (view.SequenceEqual (new [] { 5 }));
250 [Test]
251 public void ViewMin ()
253 var set = new SortedSet<int> { 1, 3, 5, 7, 9 };
254 var view = set.GetViewBetween (4, 8);
256 Assert.AreEqual (5, view.Min);
259 [Test]
260 public void ViewMax ()
262 var set = new SortedSet<int> { 1, 3, 5, 7, 9 };
263 var view = set.GetViewBetween (4, 8);
265 Assert.AreEqual (7, view.Max);
270 #endif