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> ());
89 class Bingo : IEnumerable<int>, IEnumerable<string> {
91 IEnumerator<int> IEnumerable<int>.GetEnumerator ()
97 IEnumerator<string> IEnumerable<string>.GetEnumerator ()
103 public IEnumerator GetEnumerator ()
105 return (this as IEnumerable<int>).GetEnumerator ();
110 public void TestCastToImplementedType ()
112 var ints = new int [] { 42, 12 };
113 var strs = new string [] { "foo", "bar" };
115 var bingo = new Bingo ();
117 AssertAreSame (ints, bingo.Cast<int> ());
118 AssertAreSame (strs, bingo.Cast<string> ());
122 public void TestLast ()
124 int [] data = {1, 2, 3};
126 Assert.AreEqual (3, data.Last ());
130 public void TestLastOrDefault ()
134 Assert.AreEqual (default (int), data.LastOrDefault ());
138 public void TestFirst ()
140 int [] data = {1, 2, 3};
142 Assert.AreEqual (1, data.First ());
146 public void TestFirstOrDefault ()
150 Assert.AreEqual (default (int), data.FirstOrDefault ());
154 public void TestSequenceEqual ()
156 int [] first = {0, 1, 2, 3, 4, 5};
157 int [] second = {0, 1, 2};
158 int [] third = {0, 1, 2, 3, 4, 5};
160 Assert.IsFalse (first.SequenceEqual (second));
161 Assert.IsTrue (first.SequenceEqual (third));
165 public void TestSkip ()
167 int [] data = {0, 1, 2, 3, 4, 5};
168 int [] result = {3, 4, 5};
170 AssertAreSame (result, data.Skip (3));
174 public void TestSkipWhile ()
176 int [] data = {0, 1, 2, 3, 4, 5};
177 int [] result = {3, 4, 5};
179 AssertAreSame (result, data.SkipWhile (i => i < 3));
183 public void TestTake ()
185 int [] data = {0, 1, 2, 3, 4, 5};
186 int [] result = {0, 1, 2};
188 AssertAreSame (result, data.Take (3));
192 public void TestTakeWhile ()
194 int [] data = {0, 1, 2, 3, 4, 5};
195 int [] result = {0, 1, 2};
197 AssertAreSame (result, data.TakeWhile (i => i < 3));
201 public void TestSelect ()
203 int [] data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
204 int [] result = {1, 3, 5, 7, 9};
206 AssertAreSame (result, data.Where (i => i % 2 != 0));
210 public void TestReverse ()
212 int [] data = {0, 1, 2, 3, 4};
213 int [] result = {4, 3, 2, 1, 0};
215 AssertAreSame (result, data.Reverse ());
216 AssertAreSame (result, Enumerable.Range (0, 5).Reverse ());
220 public void ReverseArrays ()
222 int[] source = { 1, 2, 3 };
224 var query = source.Reverse ();
225 using (var enumerator = query.GetEnumerator ()) {
226 enumerator.MoveNext ();
227 Assert.AreEqual (3, enumerator.Current);
230 enumerator.MoveNext ();
231 Assert.AreEqual (2, enumerator.Current);
233 enumerator.MoveNext ();
234 Assert.AreEqual (1, enumerator.Current);
239 public void TestSum ()
241 int [] data = {1, 2, 3, 4};
243 Assert.AreEqual (10, data.Sum ());
247 public void SumOnEmpty ()
251 Assert.AreEqual (0, data.Sum ());
255 public void TestMax ()
257 int [] data = {1, 3, 5, 2};
259 Assert.AreEqual (5, data.Max ());
263 public void TestMaxNullableInt32 ()
265 int? [] data = { null, null, null };
267 Assert.IsNull (data.Max (x => -x));
269 data = new int? [] { null, 1, 2 };
271 Assert.AreEqual (-1, data.Max (x => -x));
275 public void TestMin ()
277 int [] data = {3, 5, 2, 6, 1, 7};
279 Assert.AreEqual (1, data.Min ());
283 public void TestMinNullableInt32 ()
285 int? [] data = { null, null, null };
287 Assert.IsNull (data.Min(x => -x));
289 data = new int? [] { null, 1, 2 };
291 Assert.AreEqual (-2, data.Min (x => -x));
295 public void TestMinStringEmpty ()
297 Assert.IsNull ((new string [0]).Min ());
301 public void TestMaxStringEmpty ()
303 Assert.IsNull ((new string [0]).Max ());
307 public void TestToList ()
309 int [] data = {3, 5, 2};
311 var list = data.ToList ();
313 AssertAreSame (data, list);
315 Assert.AreEqual (typeof (List<int>), list.GetType ());
319 public void TestToArray ()
321 ICollection<int> coll = new List<int> ();
326 int [] result = {0, 1, 2};
328 var array = coll.ToArray ();
330 AssertAreSame (result, array);
332 Assert.AreEqual (typeof (int []), array.GetType ());
336 public void TestIntersect ()
338 int [] left = { 1, 1 }, right = { 1, 1 };
339 int [] result = { 1 };
341 AssertAreSame (result, left.Intersect (right));
345 public void TestAverageOnInt32 ()
347 Assert.AreEqual (23.25, (new int [] { 24, 7, 28, 34 }).Average ());
351 public void TestAverageOnInt64 ()
353 Assert.AreEqual (23.25, (new long [] { 24, 7, 28, 34 }).Average ());
357 public void TestAverageInt32 ()
359 // This does not overflow, computation is done with longs
360 var x = new int [] { Int32.MaxValue, Int32.MaxValue };
361 Assert.AreEqual ((double) Int32.MaxValue, x.Average ());
365 public void TestAverageOverflowOnInt64 ()
367 var x = new long [] { Int64.MaxValue, Int64.MaxValue };
372 public void TestAverageOnLongNullable ()
374 List<long?> list = new List<long?> ();
377 Assert.AreEqual (2.5d, list.Average ());
381 public void TestRange ()
383 AssertAreSame (new [] {1, 2, 3, 4}, Enumerable.Range (1, 4));
384 AssertAreSame (new [] {0, 1, 2, 3}, Enumerable.Range (0, 4));
388 public void SingleValueOfMaxInt32 ()
390 AssertAreSame (new [] { int.MaxValue }, Enumerable.Range(int.MaxValue, 1));
394 public void EmptyRangeStartingAtMinInt32 ()
396 AssertAreSame (new int [0], Enumerable.Range(int.MinValue, 0));
399 static void AssertThrows<T> (Action action) where T : Exception
411 public void TestTakeTakesProperNumberOfItems ()
413 var stream = new MemoryStream (new byte [] { 1, 2, 3, 4, 0 });
415 Assert.AreEqual (0, stream.Position);
417 foreach (byte b in AsEnumerable (stream).Take (2))
420 Assert.AreEqual (2, stream.Position);
423 static IEnumerable<byte> AsEnumerable (Stream stream)
426 while ((b = (byte) stream.ReadByte ()) >= 0)
431 public void TestOrderBy ()
433 int [] array = { 14, 53, 3, 9, 11, 14, 5, 32, 2 };
434 var q = from i in array
447 if (string.IsNullOrEmpty (name))
448 return Age.ToString ();
450 return name + " (" + Age + ")";
456 get { return age + 1; }
459 public Baz (string name, int age)
465 public override int GetHashCode ()
467 return this.Age ^ this.Name.GetHashCode ();
470 public override bool Equals (object obj)
476 return b.Age == this.Age && b.Name == this.Name;
479 public override string ToString ()
485 static IEnumerable<Baz> CreateBazCollection ()
497 public void TestOrderByAgeAscendingTheByNameDescending ()
499 var q = from b in CreateBazCollection ()
500 orderby b.Age ascending, b.Name descending
503 var expected = new [] {
511 AssertAreSame (expected, q);
515 public int ID { get; set; }
516 public string Name { get; set; }
518 public override string ToString ()
520 return ID + " " + Name;
524 IEnumerable<Data> CreateData ()
527 new Data { ID = 10, Name = "bcd" },
528 new Data { ID = 20, Name = "Abcd" },
529 new Data { ID = 20, Name = "Ab" },
530 new Data { ID = 10, Name = "Zyx" },
535 public void TestOrderByIdDescendingThenByNameAscending ()
537 var q = from d in CreateData ()
538 orderby d.ID descending, d.Name ascending
541 var list = new List<Data> (q);
543 Assert.AreEqual ("Ab", list [0].Name);
544 Assert.AreEqual ("Abcd", list [1].Name);
545 Assert.AreEqual ("bcd", list [2].Name);
546 Assert.AreEqual ("Zyx", list [3].Name);
550 public void TestOrderByDescendingStability ()
553 new { Key = true, Value = 1 },
554 new { Key = false, Value = 2},
555 new { Key = true, Value = 3},
556 new { Key = false, Value = 4},
557 new { Key = true, Value = 5},
558 new { Key = false, Value = 6},
559 new { Key = true, Value = 7},
560 new { Key = false, Value = 8},
561 new { Key = true, Value = 9},
562 new { Key = false, Value = 10},
565 var expected = new [] {
566 new { Key = true, Value = 1 },
567 new { Key = true, Value = 3},
568 new { Key = true, Value = 5},
569 new { Key = true, Value = 7},
570 new { Key = true, Value = 9},
571 new { Key = false, Value = 2},
572 new { Key = false, Value = 4},
573 new { Key = false, Value = 6},
574 new { Key = false, Value = 8},
575 new { Key = false, Value = 10},
578 AssertAreSame (expected, data.OrderByDescending (x => x.Key));
581 static void AssertIsOrdered (IEnumerable<int> e)
583 int f = int.MinValue;
584 foreach(int i in e) {
585 Assert.IsTrue (f <= i);
590 static void AssertAreSame<T> (IEnumerable<T> expected, IEnumerable<T> actual)
592 if (expected == null) {
593 Assert.IsNull (actual);
597 Assert.IsNotNull (actual);
599 IEnumerator<T> ee = expected.GetEnumerator ();
600 IEnumerator<T> ea = actual.GetEnumerator ();
602 while (ee.MoveNext ()) {
603 Assert.IsTrue (ea.MoveNext (), "'" + ee.Current + "' expected.");
604 Assert.AreEqual (ee.Current, ea.Current);
608 Assert.Fail ("Unexpected element: " + ea.Current);