5 // Jb Evain (jbevain@novell.com)
7 // (C) 2007 Novell, Inc. (http://www.novell.com)
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.
31 using System.Collections;
32 using System.Collections.Generic;
34 using System.Linq.Expressions;
36 using NUnit.Framework;
38 namespace MonoTests.System.Linq {
41 public class EnumerableTest {
44 public void TestSimpleExcept ()
46 int [] first = {0, 1, 2, 3, 4, 5};
47 int [] second = {2, 4, 6};
48 int [] result = {0, 1, 3, 5};
50 AssertAreSame (result, first.Except (second));
54 public void TestSimpleIntersect ()
56 int [] first = {0, 1, 2, 3, 4, 5};
57 int [] second = {2, 4, 6};
58 int [] result = {2, 4};
60 AssertAreSame (result, first.Intersect (second));
64 public void TestSimpleUnion ()
66 int [] first = {0, 1, 2, 3, 4, 5};
67 int [] second = {2, 4, 6};
68 int [] result = {0, 1, 2, 3, 4, 5, 6};
70 AssertAreSame (result, first.Union (second));
77 public void TestCast ()
83 Foo [] foos = new Foo [] {a, b, c};
84 Bar [] result = new Bar [] {a, b, c};
86 AssertAreSame (result, foos.Cast<Bar> ());
90 public void TestLast ()
92 int [] data = {1, 2, 3};
94 Assert.AreEqual (3, data.Last ());
98 public void TestLastOrDefault ()
102 Assert.AreEqual (default (int), data.LastOrDefault ());
106 public void TestFirst ()
108 int [] data = {1, 2, 3};
110 Assert.AreEqual (1, data.First ());
114 public void TestFirstOrDefault ()
118 Assert.AreEqual (default (int), data.FirstOrDefault ());
122 public void TestSequenceEqual ()
124 int [] first = {0, 1, 2, 3, 4, 5};
125 int [] second = {0, 1, 2};
126 int [] third = {0, 1, 2, 3, 4, 5};
128 Assert.IsFalse (first.SequenceEqual (second));
129 Assert.IsTrue (first.SequenceEqual (third));
133 public void TestSkip ()
135 int [] data = {0, 1, 2, 3, 4, 5};
136 int [] result = {3, 4, 5};
138 AssertAreSame (result, data.Skip (3));
142 public void TestSkipWhile ()
144 int [] data = {0, 1, 2, 3, 4, 5};
145 int [] result = {3, 4, 5};
147 AssertAreSame (result, data.SkipWhile (i => i < 3));
151 public void TestTake ()
153 int [] data = {0, 1, 2, 3, 4, 5};
154 int [] result = {0, 1, 2};
156 AssertAreSame (result, data.Take (3));
160 public void TestTakeWhile ()
162 int [] data = {0, 1, 2, 3, 4, 5};
163 int [] result = {0, 1, 2};
165 AssertAreSame (result, data.TakeWhile (i => i < 3));
169 public void TestSelect ()
171 int [] data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
172 int [] result = {1, 3, 5, 7, 9};
174 AssertAreSame (result, data.Where (i => i % 2 != 0));
178 public void TestReverse ()
180 int [] data = {0, 1, 2, 3, 4};
181 int [] result = {4, 3, 2, 1, 0};
183 AssertAreSame (result, data.Reverse ());
184 AssertAreSame (result, Enumerable.Range (0, 5).Reverse ());
188 public void TestSum ()
190 int [] data = {1, 2, 3, 4};
192 Assert.AreEqual (10, data.Sum ());
196 public void SumOnEmpty ()
200 Assert.AreEqual (0, data.Sum ());
204 public void TestMax ()
206 int [] data = {1, 3, 5, 2};
208 Assert.AreEqual (5, data.Max ());
212 public void TestMaxNullableInt32 ()
214 int? [] data = { null, null, null };
216 Assert.IsNull (data.Max (x => -x));
218 data = new int? [] { null, 1, 2 };
220 Assert.AreEqual (-1, data.Max (x => -x));
224 public void TestMin ()
226 int [] data = {3, 5, 2, 6, 1, 7};
228 Assert.AreEqual (1, data.Min ());
232 public void TestMinNullableInt32 ()
234 int? [] data = { null, null, null };
236 Assert.IsNull (data.Min(x => -x));
238 data = new int? [] { null, 1, 2 };
240 Assert.AreEqual (-2, data.Min (x => -x));
244 public void TestToList ()
246 int [] data = {3, 5, 2};
248 var list = data.ToList ();
250 AssertAreSame (data, list);
252 Assert.AreEqual (typeof (List<int>), list.GetType ());
256 public void TestToArray ()
258 ICollection<int> coll = new List<int> ();
263 int [] result = {0, 1, 2};
265 var array = coll.ToArray ();
267 AssertAreSame (result, array);
269 Assert.AreEqual (typeof (int []), array.GetType ());
273 public void TestIntersect ()
275 int [] left = { 1, 1 }, right = { 1, 1 };
276 int [] result = { 1 };
278 AssertAreSame (result, left.Intersect (right));
282 public void TestAverageOnInt32 ()
284 Assert.AreEqual (23.25, (new int [] { 24, 7, 28, 34 }).Average ());
288 public void TestAverageOnInt64 ()
290 Assert.AreEqual (23.25, (new long [] { 24, 7, 28, 34 }).Average ());
294 public void TestAverageOnLongNullable ()
296 List<long?> list = new List<long?> ();
299 Assert.AreEqual (2.5d, list.Average ());
303 public void TestRange ()
305 AssertAreSame (new [] {1, 2, 3, 4}, Enumerable.Range (1, 4));
307 AssertAreSame (new [] {0, 1, 2, 3}, Enumerable.Range (0, 4));
311 public void TestTakeTakesProperNumberOfItems ()
313 var stream = new MemoryStream (new byte [] { 1, 2, 3, 4, 0 });
315 Assert.AreEqual (0, stream.Position);
317 foreach (byte b in AsEnumerable (stream).Take (2))
320 Assert.AreEqual (2, stream.Position);
323 static IEnumerable<byte> AsEnumerable (Stream stream)
326 while ((b = (byte) stream.ReadByte ()) >= 0)
331 public void TestOrderBy ()
333 int [] array = { 14, 53, 3, 9, 11, 14, 5, 32, 2 };
334 var q = from i in array
347 if (string.IsNullOrEmpty (name))
348 return Age.ToString ();
350 return name + " (" + Age + ")";
356 get { return age + 1; }
359 public Baz (string name, int age)
365 public override int GetHashCode ()
367 return this.Age ^ this.Name.GetHashCode ();
370 public override bool Equals (object obj)
376 return b.Age == this.Age && b.Name == this.Name;
379 public override string ToString ()
385 static IEnumerable<Baz> CreateBazCollection ()
397 public void TestOrderByAgeAscendingTheByNameDescending ()
399 var q = from b in CreateBazCollection ()
400 orderby b.Age ascending, b.Name descending
403 var expected = new [] {
411 AssertAreSame (expected, q);
415 public int ID { get; set; }
416 public string Name { get; set; }
418 public override string ToString ()
420 return ID + " " + Name;
424 IEnumerable<Data> CreateData ()
427 new Data { ID = 10, Name = "bcd" },
428 new Data { ID = 20, Name = "Abcd" },
429 new Data { ID = 20, Name = "Ab" },
430 new Data { ID = 10, Name = "Zyx" },
435 public void TestOrderByIdDescendingThenByNameAscending ()
437 var q = from d in CreateData ()
438 orderby d.ID descending, d.Name ascending
441 var list = new List<Data> (q);
443 Assert.AreEqual ("Ab", list [0].Name);
444 Assert.AreEqual ("Abcd", list [1].Name);
445 Assert.AreEqual ("bcd", list [2].Name);
446 Assert.AreEqual ("Zyx", list [3].Name);
449 static void AssertIsOrdered (IEnumerable<int> e)
451 int f = int.MinValue;
452 foreach(int i in e) {
453 Assert.IsTrue (f <= i);
458 static void AssertAreSame<T> (IEnumerable<T> expected, IEnumerable<T> actual)
460 if (expected == null) {
461 Assert.IsNull (actual);
465 Assert.IsNotNull (actual);
467 IEnumerator<T> ee = expected.GetEnumerator ();
468 IEnumerator<T> ea = actual.GetEnumerator ();
470 while (ee.MoveNext ()) {
471 Assert.IsTrue (ea.MoveNext (), "'" + ee.Current + "' expected.");
472 Assert.AreEqual (ee.Current, ea.Current);
476 Assert.Fail ("Unexpected element: " + ea.Current);