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 TestSum ()
222 int [] data = {1, 2, 3, 4};
224 Assert.AreEqual (10, data.Sum ());
228 public void SumOnEmpty ()
232 Assert.AreEqual (0, data.Sum ());
236 public void TestMax ()
238 int [] data = {1, 3, 5, 2};
240 Assert.AreEqual (5, data.Max ());
244 public void TestMaxNullableInt32 ()
246 int? [] data = { null, null, null };
248 Assert.IsNull (data.Max (x => -x));
250 data = new int? [] { null, 1, 2 };
252 Assert.AreEqual (-1, data.Max (x => -x));
256 public void TestMin ()
258 int [] data = {3, 5, 2, 6, 1, 7};
260 Assert.AreEqual (1, data.Min ());
264 public void TestMinNullableInt32 ()
266 int? [] data = { null, null, null };
268 Assert.IsNull (data.Min(x => -x));
270 data = new int? [] { null, 1, 2 };
272 Assert.AreEqual (-2, data.Min (x => -x));
276 public void TestMinStringEmpty ()
278 Assert.IsNull ((new string [0]).Min ());
282 public void TestMaxStringEmpty ()
284 Assert.IsNull ((new string [0]).Max ());
288 public void TestToList ()
290 int [] data = {3, 5, 2};
292 var list = data.ToList ();
294 AssertAreSame (data, list);
296 Assert.AreEqual (typeof (List<int>), list.GetType ());
300 public void TestToArray ()
302 ICollection<int> coll = new List<int> ();
307 int [] result = {0, 1, 2};
309 var array = coll.ToArray ();
311 AssertAreSame (result, array);
313 Assert.AreEqual (typeof (int []), array.GetType ());
317 public void TestIntersect ()
319 int [] left = { 1, 1 }, right = { 1, 1 };
320 int [] result = { 1 };
322 AssertAreSame (result, left.Intersect (right));
326 public void TestAverageOnInt32 ()
328 Assert.AreEqual (23.25, (new int [] { 24, 7, 28, 34 }).Average ());
332 public void TestAverageOnInt64 ()
334 Assert.AreEqual (23.25, (new long [] { 24, 7, 28, 34 }).Average ());
338 public void TestAverageOnLongNullable ()
340 List<long?> list = new List<long?> ();
343 Assert.AreEqual (2.5d, list.Average ());
347 public void TestRange ()
349 AssertAreSame (new [] {1, 2, 3, 4}, Enumerable.Range (1, 4));
351 AssertAreSame (new [] {0, 1, 2, 3}, Enumerable.Range (0, 4));
355 public void TestTakeTakesProperNumberOfItems ()
357 var stream = new MemoryStream (new byte [] { 1, 2, 3, 4, 0 });
359 Assert.AreEqual (0, stream.Position);
361 foreach (byte b in AsEnumerable (stream).Take (2))
364 Assert.AreEqual (2, stream.Position);
367 static IEnumerable<byte> AsEnumerable (Stream stream)
370 while ((b = (byte) stream.ReadByte ()) >= 0)
375 public void TestOrderBy ()
377 int [] array = { 14, 53, 3, 9, 11, 14, 5, 32, 2 };
378 var q = from i in array
391 if (string.IsNullOrEmpty (name))
392 return Age.ToString ();
394 return name + " (" + Age + ")";
400 get { return age + 1; }
403 public Baz (string name, int age)
409 public override int GetHashCode ()
411 return this.Age ^ this.Name.GetHashCode ();
414 public override bool Equals (object obj)
420 return b.Age == this.Age && b.Name == this.Name;
423 public override string ToString ()
429 static IEnumerable<Baz> CreateBazCollection ()
441 public void TestOrderByAgeAscendingTheByNameDescending ()
443 var q = from b in CreateBazCollection ()
444 orderby b.Age ascending, b.Name descending
447 var expected = new [] {
455 AssertAreSame (expected, q);
459 public int ID { get; set; }
460 public string Name { get; set; }
462 public override string ToString ()
464 return ID + " " + Name;
468 IEnumerable<Data> CreateData ()
471 new Data { ID = 10, Name = "bcd" },
472 new Data { ID = 20, Name = "Abcd" },
473 new Data { ID = 20, Name = "Ab" },
474 new Data { ID = 10, Name = "Zyx" },
479 public void TestOrderByIdDescendingThenByNameAscending ()
481 var q = from d in CreateData ()
482 orderby d.ID descending, d.Name ascending
485 var list = new List<Data> (q);
487 Assert.AreEqual ("Ab", list [0].Name);
488 Assert.AreEqual ("Abcd", list [1].Name);
489 Assert.AreEqual ("bcd", list [2].Name);
490 Assert.AreEqual ("Zyx", list [3].Name);
493 static void AssertIsOrdered (IEnumerable<int> e)
495 int f = int.MinValue;
496 foreach(int i in e) {
497 Assert.IsTrue (f <= i);
502 static void AssertAreSame<T> (IEnumerable<T> expected, IEnumerable<T> actual)
504 if (expected == null) {
505 Assert.IsNull (actual);
509 Assert.IsNotNull (actual);
511 IEnumerator<T> ee = expected.GetEnumerator ();
512 IEnumerator<T> ea = actual.GetEnumerator ();
514 while (ee.MoveNext ()) {
515 Assert.IsTrue (ea.MoveNext (), "'" + ee.Current + "' expected.");
516 Assert.AreEqual (ee.Current, ea.Current);
520 Assert.Fail ("Unexpected element: " + ea.Current);