// // EnumerableMoreTest.cs // // Author: // Andreas Noever // // (C) 2007 Andreas Noever // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the // "Software"), to deal in the Software without restriction, including // without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to // permit persons to whom the Software is furnished to do so, subject to // the following conditions: // // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // using System; using System.Collections; using System.Collections.Generic; using System.Linq; using NUnit.Framework; namespace MonoTests.System.Linq { [TestFixture] public class EnumerableMoreTest { class BigEnumerable : IEnumerable { public readonly ulong Count; public BigEnumerable (ulong Count) { this.Count = Count; } #region IEnumerable Members public IEnumerator GetEnumerator () { return new BigEnumerator (this); } #endregion #region IEnumerable Members IEnumerator IEnumerable.GetEnumerator () { throw new NotImplementedException (); } #endregion } class BigEnumerator : IEnumerator { BigEnumerable Parent; private ulong current; public BigEnumerator (BigEnumerable parent) { Parent = parent; } public int Current { get { return 3; } } public void Dispose () { } object IEnumerator.Current { get { throw new NotImplementedException (); } } public bool MoveNext () { if (current == Parent.Count) return false; current++; return true; } public void Reset () { throw new NotImplementedException (); } } public static void AssertException (Action action) where T : Exception { try { action (); } catch (T) { return; } Assert.Fail ("Expected: " + typeof (T).Name); } static void AssertAreSame (K expectedKey, IEnumerable expectedValues, IGrouping actual) { if (expectedValues == null) { Assert.IsNull (actual); return; } Assert.IsNotNull (actual); Assert.AreEqual (expectedKey, actual.Key); var ee = expectedValues.GetEnumerator (); var ea = actual.GetEnumerator (); while (ee.MoveNext ()) { Assert.IsTrue (ea.MoveNext (), "'" + ee.Current + "' expected."); Assert.AreEqual (ee.Current, ea.Current); } if (ea.MoveNext ()) Assert.Fail ("Unexpected element: " + ee.Current); } static void AssertAreSame (IDictionary> expected, IEnumerable> actual) { if (expected == null) { Assert.IsNull (actual); return; } Assert.IsNotNull (actual); var ee = expected.GetEnumerator (); var ea = actual.GetEnumerator (); while (ee.MoveNext ()) { Assert.IsTrue (ea.MoveNext (), "'" + ee.Current.Key + "' expected."); AssertAreSame (ee.Current.Key, ee.Current.Value, ea.Current); } if (ea.MoveNext ()) Assert.Fail ("Unexpected element: " + ee.Current.Key); } static void AssertAreSame (IDictionary> expected, ILookup actual) { if (expected == null) { Assert.IsNull (actual); return; } Assert.IsNotNull (actual); var ee = expected.GetEnumerator (); var ea = actual.GetEnumerator (); while (ee.MoveNext ()) { Assert.IsTrue (ea.MoveNext (), "'" + ee.Current.Key + "' expected."); AssertAreSame (ee.Current.Key, ee.Current.Value, ea.Current); } if (ea.MoveNext ()) Assert.Fail ("Unexpected element: " + ee.Current.Key); } static void AssertAreSame (IDictionary expected, IDictionary actual) { if (expected == null) { Assert.IsNull (actual); return; } Assert.IsNotNull (actual); var ee = expected.GetEnumerator (); var ea = actual.GetEnumerator (); while (ee.MoveNext ()) { Assert.IsTrue (ea.MoveNext (), "'" + ee.Current.Key + ", " + ee.Current.Value + "' expected."); Assert.AreEqual (ee.Current.Key, ea.Current.Key); Assert.AreEqual (ee.Current.Value, ea.Current.Value); } if (ea.MoveNext ()) Assert.Fail ("Unexpected element: " + ee.Current.Key + ", " + ee.Current.Value); } static void AssertAreSame (IEnumerable expected, IEnumerable actual) { if (expected == null) { Assert.IsNull (actual); return; } Assert.IsNotNull (actual); IEnumerator ee = expected.GetEnumerator (); IEnumerator ea = actual.GetEnumerator (); while (ee.MoveNext ()) { Assert.IsTrue (ea.MoveNext (), "'" + ee.Current + "' expected."); Assert.AreEqual (ee.Current, ea.Current); } if (ea.MoveNext ()) Assert.Fail ("Unexpected element: " + ea.Current); } [Test] public void FirstArgumentNullTest () { string [] data = { "2", "1", "5", "3", "4" }; // First () AssertException (delegate () { ((IEnumerable) null).First (); }); // First (Func) AssertException (delegate () { ((IEnumerable) null).First ((x => true)); }); AssertException (delegate () { data.First ((Func) null); }); } [Test] public void FirstTest () { int [] data = { 2, 1, 5, 3, 4 }; int [] empty = { }; // First () Assert.AreEqual (2, data.First ()); AssertException (delegate () { empty.First (); }); // First (Func) Assert.AreEqual (5, data.First (x => x == 5)); AssertException (delegate () { empty.First (x => x == 5); }); AssertException (delegate () { data.First (x => x == 6); }); } [Test] public void FirstOrDefaultArgumentNullTest () { string [] data = { "2", "1", "5", "3", "4" }; // FirstOrDefault () AssertException (delegate () { ((IEnumerable) null).FirstOrDefault (); }); // FirstOrDefault (Func) AssertException (delegate () { ((IEnumerable) null).FirstOrDefault ((x => true)); }); AssertException (delegate () { data.FirstOrDefault ((Func) null); }); } [Test] public void FirstOrDefaultTest () { int [] data = { 2, 1, 5, 3, 4 }; int [] empty = { }; // FirstOrDefault () Assert.AreEqual (2, data.FirstOrDefault ()); Assert.AreEqual (0, empty.FirstOrDefault ()); // FirstOrDefault (Func) Assert.AreEqual (5, data.FirstOrDefault (x => x == 5)); Assert.AreEqual (0, empty.FirstOrDefault (x => x == 5)); Assert.AreEqual (0, data.FirstOrDefault (x => x == 6)); } [Test] public void LastArgumentNullTest () { string [] data = { "2", "1", "5", "3", "4" }; // Last () AssertException (delegate () { ((IEnumerable) null).Last (); }); // Last (Func) AssertException (delegate () { ((IEnumerable) null).Last (x => true); }); AssertException (delegate () { data.Last ((Func) null); }); } [Test] public void LastTest () { int [] data = { 2, 1, 1, 3, 4, 5 }; int [] empty = { }; // Last () Assert.AreEqual (5, data.Last ()); AssertException (delegate () { empty.Last (); }); // Last (Func) Assert.AreEqual (4, data.Last (x => x < 5)); AssertException (delegate () { empty.Last (x => x == 5); }); AssertException (delegate () { data.Last (x => x == 6); }); } [Test] public void LastOrDefaultArgumentNullTest () { string [] data = { "2", "1", "5", "3", "4" }; // LastOrDefault () AssertException (delegate () { ((IEnumerable) null).LastOrDefault (); }); // LastOrDefault (Func) AssertException (delegate () { ((IEnumerable) null).LastOrDefault (x => true); }); AssertException (delegate () { data.LastOrDefault ((Func) null); }); } [Test] public void LastOrDefaultTest () { int [] data = { 2, 1, 5, 3, 4 }; int [] empty = { }; // LastOrDefault () Assert.AreEqual (4, data.LastOrDefault ()); Assert.AreEqual (0, empty.LastOrDefault ()); // LastOrDefault (Func) Assert.AreEqual (3, data.LastOrDefault (x => x < 4)); Assert.AreEqual (0, empty.LastOrDefault (x => x == 5)); Assert.AreEqual (0, data.LastOrDefault (x => x == 6)); } [Test] public void SingleArgumentNullTest () { string [] data = { "2", "1", "5", "3", "4" }; // Single () AssertException (delegate () { ((IEnumerable) null).Single (); }); // Single (Func) AssertException (delegate () { ((IEnumerable) null).Single ((x => true)); }); AssertException (delegate () { data.Single ((Func) null); }); } [Test] public void SingleTest () { int [] data = { 2 }; int [] data2 = { 2, 3, 5 }; int [] empty = { }; // Single () Assert.AreEqual (2, data.Single ()); AssertException (delegate () { data2.Single (); }); AssertException (delegate () { empty.Single (); }); // Single (Func) Assert.AreEqual (5, data2.Single (x => x == 5)); AssertException (delegate () { data2.Single (x => false); }); AssertException (delegate () { data2.Single (x => true); }); AssertException (delegate () { empty.Single (x => true); }); } [Test] public void SingleOrDefaultArgumentNullTest () { string [] data = { "2", "1", "5", "3", "4" }; // SingleOrDefault () AssertException (delegate () { ((IEnumerable) null).SingleOrDefault (); }); // SingleOrDefault (Func) AssertException (delegate () { ((IEnumerable) null).SingleOrDefault (x => true); }); AssertException (delegate () { data.SingleOrDefault ((Func) null); }); } [Test] public void SingleOrDefaultTest () { int [] data = { 2 }; int [] data2 = { 2, 3, 5 }; int [] empty = { }; // SingleOrDefault () Assert.AreEqual (2, data.SingleOrDefault ()); Assert.AreEqual (0, empty.SingleOrDefault ()); AssertException (delegate () { data2.SingleOrDefault (); }); // SingleOrDefault (Func) Assert.AreEqual (3, data2.SingleOrDefault (x => x == 3)); Assert.AreEqual (0, data2.SingleOrDefault (x => false)); AssertException (delegate () { data2.SingleOrDefault (x => true); }); } [Test] public void ElementAtArgumentNullTest () { //string [] data = { "2", "1", "5", "3", "4" }; // ElementAt (int) AssertException (delegate () { ((IEnumerable) null).ElementAt (0); }); } [Test] public void ElementAtTest () { int [] data = { 2, 3, 4, 5 }; // ElementAt (int) Assert.AreEqual (2, data.ElementAt (0)); Assert.AreEqual (4, data.ElementAt (2)); AssertException (delegate () { data.ElementAt (-1); }); AssertException (delegate () { data.ElementAt (4); }); AssertException (delegate () { data.ElementAt (6); }); } [Test] public void ElementAtOrDefaultArgumentNullTest () { //string [] data = { "2", "1", "5", "3", "4" }; // ElementAtOrDefault (int) AssertException (delegate () { ((IEnumerable) null).ElementAtOrDefault (0); }); } [Test] public void ElementAtOrDefaultTest () { int [] data = { 2, 3, 4, 5 }; int [] empty = { }; // ElementAtOrDefault (int) Assert.AreEqual (2, data.ElementAtOrDefault (0)); Assert.AreEqual (4, data.ElementAtOrDefault (2)); Assert.AreEqual (0, data.ElementAtOrDefault (-1)); Assert.AreEqual (0, data.ElementAtOrDefault (4)); Assert.AreEqual (0, empty.ElementAtOrDefault (4)); } [Test] public void EmptyTest () { IEnumerable empty = Enumerable.Empty (); Assert.IsFalse (empty.GetEnumerator ().MoveNext ()); } [Test] public void AnyArgumentNullTest () { string [] data = { "2", "1", "5", "3", "4" }; // Any () AssertException (delegate () { ((IEnumerable) null).Any (); }); // Any (Func) AssertException (delegate () { ((IEnumerable) null).Any (x => true); }); AssertException (delegate () { data.Any ((Func) null); }); } [Test] public void AnyTest () { int [] data = { 5, 2, 3, 1, 6 }; int [] empty = { }; // Any () Assert.IsTrue (data.Any ()); Assert.IsFalse (empty.Any ()); // Any (Func) Assert.IsTrue (data.Any (x => x == 5)); Assert.IsFalse (data.Any (x => x == 9)); Assert.IsFalse (empty.Any (x => true)); } [Test] public void AllArgumentNullTest () { string [] data = { "2", "1", "5", "3", "4" }; // All (Func) AssertException (delegate () { ((IEnumerable) null).All (x => true); }); AssertException (delegate () { data.All ((Func) null); }); } [Test] public void AllTest () { int [] data = { 5, 2, 3, 1, 6 }; int [] empty = { }; // All (Func) Assert.IsTrue (data.All (x => true)); Assert.IsFalse (data.All (x => x != 1)); Assert.IsTrue (empty.All (x => false)); } [Test] public void CountArgumentNullTest () { string [] data = { "2", "1", "5", "3", "4" }; // Count () AssertException (delegate () { ((IEnumerable) null).Count (); }); // Count (Func) AssertException (delegate () { ((IEnumerable) null).Count (x => true); }); AssertException (delegate () { data.Count ((Func) null); }); } [Test] public void CountTest () { int [] data = { 5, 2, 3, 1, 6 }; // Count () Assert.AreEqual (5, data.Count ()); // Count (Func) Assert.AreEqual (3, data.Count (x => x < 5)); } //[Test] public void CountOverflowTest () { //BigEnumerable data = new BigEnumerable ((ulong) int.MaxValue + 1); // Count () //AssertException (delegate () { data.Count (); }); // Count (Func) //AssertException (delegate () { data.Count (x => 3 == x); }); // Documentation error: http://msdn2.microsoft.com/en-us/library/bb535181.aspx // An exception is only rasied if count > int.MaxValue. Not if source contains more than int.MaxValue elements. // AssertException (delegate () { data.Count (x => 5 == x); }); } [Test] public void LongCountArgumentNullTest () { string [] data = { "2", "1", "5", "3", "4" }; // LongCount () AssertException (delegate () { ((IEnumerable) null).LongCount (); }); // LongCount (Func) AssertException (delegate () { ((IEnumerable) null).LongCount (x => true); }); AssertException (delegate () { data.LongCount ((Func) null); }); } [Test] public void LongCountTest () { int [] data = { 5, 2, 3, 1, 6 }; //TODO: Overflow test... // LongCount () Assert.AreEqual (5, data.LongCount ()); Assert.AreEqual (5, Enumerable.Range (0, 5).LongCount ()); // LongCount (Func) Assert.AreEqual (3, data.LongCount (x => x < 5)); } [Test] public void ContainsArgumentNullTest () { //string [] data = { "2", "1", "5", "3", "4" }; // Contains (TSource) AssertException (delegate () { ((IEnumerable) null).Contains ("2"); }); // Contains (TSource, IEqualityComparer) AssertException (delegate () { ((IEnumerable) null).Contains ("2", (IEqualityComparer) EqualityComparer.Default); }); } static void IsFalse(bool b, int[] data) { if (b) { Console.WriteLine (data.Contains (0)); object o = null; o.ToString (); Assert.IsFalse (true); } //Console.WriteLine ("HIT!"); } [Test] public void ContainsTest () { int [] data = { 5, 2, 3, 1, 6 }; ICollection icoll = data; // Contains (TSource) Assert.IsTrue (data.Contains (2)); for (int i = 0; i < 50; ++i) Console.WriteLine (icoll.Contains (0));//Console.WriteLine (data.Contains (0)); IsFalse (data.Contains (0), data); // Contains (TSource, IEqualityComparer) Assert.IsTrue (data.Contains (2, EqualityComparer.Default)); Assert.IsFalse (data.Contains (0, EqualityComparer.Default)); } [Test] public void AggregateArgumentNullTest () { string [] data = { "2", "1", "5", "3", "4" }; // Aggregate (Func) AssertException (delegate () { ((IEnumerable) null).Aggregate ((x, y) => "test"); }); AssertException (delegate () { data.Aggregate ((Func) null); }); // Aggregate (TAccumulate, Func) AssertException (delegate () { ((IEnumerable) null).Aggregate ("initial", (x, y) => "test"); }); AssertException (delegate () { data.Aggregate ("initial", (Func) null); }); // Aggregate (TAccumulate, Func, Func) AssertException (delegate () { ((IEnumerable) null).Aggregate ("initial", (x, y) => "test", x => "test"); }); AssertException (delegate () { data.Aggregate ("initial", (Func) null, x => "test"); }); AssertException (delegate () { data.Aggregate ("initial", (x, y) => "test", (Func) null); }); } [Test] public void AggregateTest () { string [] data = { "2", "1", "5", "3", "4" }; string [] empty = { }; // Aggregate (Func) Assert.AreEqual ("21534", data.Aggregate ((x, y) => x + y)); AssertException (delegate () { empty.Aggregate ((x, y) => x + y); }); //only this overload throws // Aggregate (TAccumulate, Func) Assert.AreEqual ("initial21534", (data.Aggregate ("initial", (x, y) => x + y))); // Aggregate (TAccumulate, Func, Func) Assert.AreEqual ("INITIAL21534", data.Aggregate ("initial", (x, y) => x + y, (x => x.ToUpper ()))); } [Test] public void SumArgumentNullTest () { string [] data = { "2", "1", "5", "3", "4" }; // Sum (Func) AssertException (delegate () { ((IEnumerable) null).Sum ((Func) (x => 0)); }); AssertException (delegate () { data.Sum ((Func) null); }); // Sum (Func>) AssertException (delegate () { ((IEnumerable) null).Sum ((Func>) (x => (int?) 0)); }); AssertException (delegate () { data.Sum ((Func>) null); }); // Sum (Func) AssertException (delegate () { ((IEnumerable) null).Sum ((Func) (x => 0L)); }); AssertException (delegate () { data.Sum ((Func) null); }); // Sum (Func>) AssertException (delegate () { ((IEnumerable) null).Sum ((Func>) (x => (int?) 0L)); }); AssertException (delegate () { data.Sum ((Func>) null); }); // Sum (Func) AssertException (delegate () { ((IEnumerable) null).Sum ((Func) (x => 0f)); }); AssertException (delegate () { data.Sum ((Func) null); }); // Sum (Func>) AssertException (delegate () { ((IEnumerable) null).Sum ((Func>) (x => (int?) 0f)); }); AssertException (delegate () { data.Sum ((Func>) null); }); // Sum (Func) AssertException (delegate () { ((IEnumerable) null).Sum ((Func) (x => 0d)); }); AssertException (delegate () { data.Sum ((Func) null); }); // Sum (Func>) AssertException (delegate () { ((IEnumerable) null).Sum ((Func>) (x => (int?) 0d)); }); AssertException (delegate () { data.Sum ((Func>) null); }); // Sum (Func) AssertException (delegate () { ((IEnumerable) null).Sum ((Func) (x => 0m)); }); AssertException (delegate () { data.Sum ((Func) null); }); // Sum (Func>) AssertException (delegate () { ((IEnumerable) null).Sum ((Func>) (x => (int?) 0m)); }); AssertException (delegate () { data.Sum ((Func>) null); }); // Sum (IEnumerable) AssertException (delegate () { ((IEnumerable) null).Sum (); }); // Sum (IEnumerable) AssertException (delegate () { ((IEnumerable>) null).Sum (); }); // Sum (IEnumerable) AssertException (delegate () { ((IEnumerable) null).Sum (); }); // Sum (IEnumerable) AssertException (delegate () { ((IEnumerable>) null).Sum (); }); // Sum (IEnumerable) AssertException (delegate () { ((IEnumerable) null).Sum (); }); // Sum (IEnumerable) AssertException (delegate () { ((IEnumerable>) null).Sum (); }); // Sum (IEnumerable) AssertException (delegate () { ((IEnumerable) null).Sum (); }); // Sum (IEnumerable) AssertException (delegate () { ((IEnumerable>) null).Sum (); }); // Sum (IEnumerable) AssertException (delegate () { ((IEnumerable) null).Sum (); }); // Sum (IEnumerable) AssertException (delegate () { ((IEnumerable>) null).Sum (); }); } [Test] public void SumTest () { string [] data = { "2", "3", "5", "5" }; //TODO: OverflowException // Sum (Func) Assert.AreEqual (15, ((IEnumerable) data).Sum ((Func) (x => int.Parse (x)))); Assert.AreEqual (0, Enumerable.Empty ().Sum ((Func) (x => int.Parse (x)))); // Sum (Func>) Assert.AreEqual (15, ((IEnumerable) data).Sum ((Func>) (x => (int?) int.Parse (x)))); Assert.AreEqual (0, Enumerable.Empty ().Sum ((Func>) (x => (int?) int.Parse (x)))); // Sum (Func) Assert.AreEqual (15, ((IEnumerable) data).Sum ((Func) (x => int.Parse (x)))); Assert.AreEqual (0, Enumerable.Empty ().Sum ((Func) (x => int.Parse (x)))); // Sum (Func>) Assert.AreEqual (15, ((IEnumerable) data).Sum ((Func>) (x => (int?) int.Parse (x)))); Assert.AreEqual (0, Enumerable.Empty ().Sum ((Func>) (x => (int?) int.Parse (x)))); // Sum (Func) Assert.AreEqual (15, ((IEnumerable) data).Sum ((Func) (x => int.Parse (x)))); Assert.AreEqual (0, Enumerable.Empty ().Sum ((Func) (x => int.Parse (x)))); // Sum (Func>) Assert.AreEqual (15, ((IEnumerable) data).Sum ((Func>) (x => (int?) int.Parse (x)))); Assert.AreEqual (0, Enumerable.Empty ().Sum ((Func>) (x => (int?) int.Parse (x)))); // Sum (Func) Assert.AreEqual (15, ((IEnumerable) data).Sum ((Func) (x => int.Parse (x)))); Assert.AreEqual (0, Enumerable.Empty ().Sum ((Func) (x => int.Parse (x)))); // Sum (Func>) Assert.AreEqual (15, ((IEnumerable) data).Sum ((Func>) (x => (int?) int.Parse (x)))); Assert.AreEqual (0, Enumerable.Empty ().Sum ((Func>) (x => (int?) int.Parse (x)))); // Sum (Func) Assert.AreEqual (15, ((IEnumerable) data).Sum ((Func) (x => int.Parse (x)))); Assert.AreEqual (0, Enumerable.Empty ().Sum ((Func) (x => int.Parse (x)))); // Sum (Func>) Assert.AreEqual (15, ((IEnumerable) data).Sum ((Func>) (x => (int?) int.Parse (x)))); Assert.AreEqual (0, Enumerable.Empty ().Sum ((Func>) (x => (int?) int.Parse (x)))); // Sum<> () Assert.AreEqual (6, ((IEnumerable) new int [] { 1, 2, 3 }).Sum ()); Assert.AreEqual (0, Enumerable.Empty ().Sum ()); // Sum<> () Assert.AreEqual (6, ((IEnumerable>) new int? [] { 1, 2, 3 }).Sum ()); Assert.AreEqual (0, Enumerable.Empty ().Sum ()); // Sum<> () Assert.AreEqual (6, ((IEnumerable) new long [] { 1, 2, 3 }).Sum ()); Assert.AreEqual (0, Enumerable.Empty ().Sum ()); // Sum<> () Assert.AreEqual (6, ((IEnumerable>) new long? [] { 1, 2, 3 }).Sum ()); Assert.AreEqual (0, Enumerable.Empty ().Sum ()); // Sum<> () Assert.AreEqual (6, ((IEnumerable) new float [] { 1, 2, 3 }).Sum ()); Assert.AreEqual (0, Enumerable.Empty ().Sum ()); // Sum<> () Assert.AreEqual (6, ((IEnumerable>) new float? [] { 1, 2, 3 }).Sum ()); Assert.AreEqual (0, Enumerable.Empty ().Sum ()); // Sum<> () Assert.AreEqual (6, ((IEnumerable) new double [] { 1, 2, 3 }).Sum ()); Assert.AreEqual (0, Enumerable.Empty ().Sum ()); // Sum<> () Assert.AreEqual (6, ((IEnumerable>) new double? [] { 1, 2, 3 }).Sum ()); Assert.AreEqual (0, Enumerable.Empty ().Sum ()); // Sum<> () Assert.AreEqual (6, ((IEnumerable) new decimal [] { 1, 2, 3 }).Sum ()); Assert.AreEqual (0, Enumerable.Empty ().Sum ()); // Sum<> () Assert.AreEqual (6, ((IEnumerable>) new decimal? [] { 1, 2, 3 }).Sum ()); Assert.AreEqual (0, Enumerable.Empty ().Sum ()); } [Test] public void MinArgumentNullTest () { string [] data = { "2", "1", "5", "3", "4" }; // Min () AssertException (delegate () { ((IEnumerable) null).Min (); }); // Min (Func) AssertException (delegate () { ((IEnumerable) null).Min ((Func) (x => 0)); }); AssertException (delegate () { data.Min ((Func) null); }); // Min (Func>) AssertException (delegate () { ((IEnumerable) null).Min ((Func>) (x => (int?) 0)); }); AssertException (delegate () { data.Min ((Func>) null); }); // Min (Func) AssertException (delegate () { ((IEnumerable) null).Min ((Func) (x => 0L)); }); AssertException (delegate () { data.Min ((Func) null); }); // Min (Func>) AssertException (delegate () { ((IEnumerable) null).Min ((Func>) (x => (int?) 0L)); }); AssertException (delegate () { data.Min ((Func>) null); }); // Min (Func) AssertException (delegate () { ((IEnumerable) null).Min ((Func) (x => 0f)); }); AssertException (delegate () { data.Min ((Func) null); }); // Min (Func>) AssertException (delegate () { ((IEnumerable) null).Min ((Func>) (x => (int?) 0f)); }); AssertException (delegate () { data.Min ((Func>) null); }); // Min (Func) AssertException (delegate () { ((IEnumerable) null).Min ((Func) (x => 0d)); }); AssertException (delegate () { data.Min ((Func) null); }); // Min (Func>) AssertException (delegate () { ((IEnumerable) null).Min ((Func>) (x => (int?) 0d)); }); AssertException (delegate () { data.Min ((Func>) null); }); // Min (Func) AssertException (delegate () { ((IEnumerable) null).Min ((Func) (x => 0m)); }); AssertException (delegate () { data.Min ((Func) null); }); // Min (Func>) AssertException (delegate () { ((IEnumerable) null).Min ((Func>) (x => (int?) 0m)); }); AssertException (delegate () { data.Min ((Func>) null); }); // Min (Func) AssertException (delegate () { ((IEnumerable) null).Min ((Func) (x => "test")); }); AssertException (delegate () { data.Min ((Func) null); }); // Min<> () AssertException (delegate () { ((IEnumerable) null).Min (); }); // Min<> () AssertException (delegate () { ((IEnumerable>) null).Min (); }); // Min<> () AssertException (delegate () { ((IEnumerable) null).Min (); }); // Min<> () AssertException (delegate () { ((IEnumerable>) null).Min (); }); // Min<> () AssertException (delegate () { ((IEnumerable) null).Min (); }); // Min<> () AssertException (delegate () { ((IEnumerable>) null).Min (); }); // Min<> () AssertException (delegate () { ((IEnumerable) null).Min (); }); // Min<> () AssertException (delegate () { ((IEnumerable>) null).Min (); }); // Min<> () AssertException (delegate () { ((IEnumerable) null).Min (); }); // Min<> () AssertException (delegate () { ((IEnumerable>) null).Min (); }); } [Test] public void MinTest () { string [] data = { "2", "1", "5", "3", "4" }; // Min () Assert.AreEqual ("1", ((IEnumerable) data).Min ()); // Min (Func) Assert.AreEqual (1, ((IEnumerable) data).Min ((Func) (x => int.Parse (x)))); // Min (Func>) Assert.AreEqual (1, ((IEnumerable) data).Min ((Func>) (x => (int?) int.Parse (x)))); // Min (Func) Assert.AreEqual (1, ((IEnumerable) data).Min ((Func) (x => int.Parse (x)))); // Min (Func>) Assert.AreEqual (1, ((IEnumerable) data).Min ((Func>) (x => (int?) int.Parse (x)))); // Min (Func) Assert.AreEqual (1, ((IEnumerable) data).Min ((Func) (x => int.Parse (x)))); // Min (Func>) Assert.AreEqual (1, ((IEnumerable) data).Min ((Func>) (x => (int?) int.Parse (x)))); // Min (Func) Assert.AreEqual (1, ((IEnumerable) data).Min ((Func) (x => int.Parse (x)))); // Min (Func>) Assert.AreEqual (1, ((IEnumerable) data).Min ((Func>) (x => (int?) int.Parse (x)))); // Min (Func) Assert.AreEqual (1, ((IEnumerable) data).Min ((Func) (x => int.Parse (x)))); // Min (Func>) Assert.AreEqual (1, ((IEnumerable) data).Min ((Func>) (x => (int?) int.Parse (x)))); // Min (Func) Assert.AreEqual ("1", ((IEnumerable) data).Min ((Func) (x => x))); // Min<> () Assert.AreEqual (2, ((IEnumerable) new int [] { 2, 3, 4 }).Min ()); // Min<> () Assert.AreEqual (2, ((IEnumerable>) new int? [] { 2, 3, 4 }).Min ()); // Min<> () Assert.AreEqual (2, ((IEnumerable) new long [] { 2, 3, 4 }).Min ()); // Min<> () Assert.AreEqual (2, ((IEnumerable>) new long? [] { 2, 3, 4 }).Min ()); // Min<> () Assert.AreEqual (2, ((IEnumerable) new float [] { 2, 3, 4 }).Min ()); // Min<> () Assert.AreEqual (2, ((IEnumerable>) new float? [] { 2, 3, 4 }).Min ()); // Min<> () Assert.AreEqual (2, ((IEnumerable) new double [] { 2, 3, 4 }).Min ()); // Min<> () Assert.AreEqual (2, ((IEnumerable>) new double? [] { 2, 3, 4 }).Min ()); // Min<> () Assert.AreEqual (2, ((IEnumerable) new decimal [] { 2, 3, 4 }).Min ()); // Min<> () Assert.AreEqual (2, ((IEnumerable>) new decimal? [] { 2, 3, 4 }).Min ()); } [Test] public void MaxArgumentNullTest () { string [] data = { "2", "1", "5", "3", "4" }; // Max () AssertException (delegate () { ((IEnumerable) null).Max (); }); // Max (Func) AssertException (delegate () { ((IEnumerable) null).Max ((Func) (x => 0)); }); AssertException (delegate () { data.Max ((Func) null); }); // Max (Func>) AssertException (delegate () { ((IEnumerable) null).Max ((Func>) (x => (int?) 0)); }); AssertException (delegate () { data.Max ((Func>) null); }); // Max (Func) AssertException (delegate () { ((IEnumerable) null).Max ((Func) (x => 0L)); }); AssertException (delegate () { data.Max ((Func) null); }); // Max (Func>) AssertException (delegate () { ((IEnumerable) null).Max ((Func>) (x => (int?) 0L)); }); AssertException (delegate () { data.Max ((Func>) null); }); // Max (Func) AssertException (delegate () { ((IEnumerable) null).Max ((Func) (x => 0f)); }); AssertException (delegate () { data.Max ((Func) null); }); // Max (Func>) AssertException (delegate () { ((IEnumerable) null).Max ((Func>) (x => (int?) 0f)); }); AssertException (delegate () { data.Max ((Func>) null); }); // Max (Func) AssertException (delegate () { ((IEnumerable) null).Max ((Func) (x => 0d)); }); AssertException (delegate () { data.Max ((Func) null); }); // Max (Func>) AssertException (delegate () { ((IEnumerable) null).Max ((Func>) (x => (int?) 0d)); }); AssertException (delegate () { data.Max ((Func>) null); }); // Max (Func) AssertException (delegate () { ((IEnumerable) null).Max ((Func) (x => 0m)); }); AssertException (delegate () { data.Max ((Func) null); }); // Max (Func>) AssertException (delegate () { ((IEnumerable) null).Max ((Func>) (x => (int?) 0m)); }); AssertException (delegate () { data.Max ((Func>) null); }); // Max (Func) AssertException (delegate () { ((IEnumerable) null).Max ((Func) (x => "test")); }); AssertException (delegate () { data.Max ((Func) null); }); // Max<> () AssertException (delegate () { ((IEnumerable) null).Max (); }); // Max<> () AssertException (delegate () { ((IEnumerable>) null).Max (); }); // Max<> () AssertException (delegate () { ((IEnumerable) null).Max (); }); // Max<> () AssertException (delegate () { ((IEnumerable>) null).Max (); }); // Max<> () AssertException (delegate () { ((IEnumerable) null).Max (); }); // Max<> () AssertException (delegate () { ((IEnumerable>) null).Max (); }); // Max<> () AssertException (delegate () { ((IEnumerable) null).Max (); }); // Max<> () AssertException (delegate () { ((IEnumerable>) null).Max (); }); // Max<> () AssertException (delegate () { ((IEnumerable) null).Max (); }); // Max<> () AssertException (delegate () { ((IEnumerable>) null).Max (); }); } [Test] public void MaxTest () { string [] data = { "2", "1", "5", "3", "4" }; // Max () Assert.AreEqual ("5", ((IEnumerable) data).Max ()); // Max (Func) Assert.AreEqual (5, ((IEnumerable) data).Max ((Func) (x => int.Parse (x)))); // Max (Func>) Assert.AreEqual (5, ((IEnumerable) data).Max ((Func>) (x => (int?) int.Parse (x)))); // Max (Func) Assert.AreEqual (5, ((IEnumerable) data).Max ((Func) (x => int.Parse (x)))); // Max (Func>) Assert.AreEqual (5, ((IEnumerable) data).Max ((Func>) (x => (int?) int.Parse (x)))); // Max (Func) Assert.AreEqual (5, ((IEnumerable) data).Max ((Func) (x => int.Parse (x)))); // Max (Func>) Assert.AreEqual (5, ((IEnumerable) data).Max ((Func>) (x => (int?) int.Parse (x)))); // Max (Func) Assert.AreEqual (5, ((IEnumerable) data).Max ((Func) (x => int.Parse (x)))); // Max (Func>) Assert.AreEqual (5, ((IEnumerable) data).Max ((Func>) (x => (int?) int.Parse (x)))); // Max (Func) Assert.AreEqual (5, ((IEnumerable) data).Max ((Func) (x => int.Parse (x)))); // Max (Func>) Assert.AreEqual (5, ((IEnumerable) data).Max ((Func>) (x => (int?) int.Parse (x)))); // Max (Func) Assert.AreEqual ("5", ((IEnumerable) data).Max ((Func) (x => x))); // Max<> () Assert.AreEqual (4, ((IEnumerable) new int [] { 2, 3, 4 }).Max ()); // Max<> () Assert.AreEqual (4, ((IEnumerable>) new int? [] { 2, 3, 4 }).Max ()); // Max<> () Assert.AreEqual (4, ((IEnumerable) new long [] { 2, 3, 4 }).Max ()); // Max<> () Assert.AreEqual (4, ((IEnumerable>) new long? [] { 2, 3, 4 }).Max ()); // Max<> () Assert.AreEqual (4, ((IEnumerable) new float [] { 2, 3, 4 }).Max ()); // Max<> () Assert.AreEqual (4, ((IEnumerable>) new float? [] { 2, 3, 4 }).Max ()); // Max<> () Assert.AreEqual (4, ((IEnumerable) new double [] { 2, 3, 4 }).Max ()); // Max<> () Assert.AreEqual (4, ((IEnumerable>) new double? [] { 2, 3, 4 }).Max ()); // Max<> () Assert.AreEqual (4, ((IEnumerable) new decimal [] { 2, 3, 4 }).Max ()); // Max<> () Assert.AreEqual (4, ((IEnumerable>) new decimal? [] { 2, 3, 4 }).Max ()); } [Test] public void AverageArgumentNullTest () { string [] data = { "2", "1", "5", "3", "4" }; // Average (Func) AssertException (delegate () { ((IEnumerable) null).Average ((Func) (x => 0)); }); AssertException (delegate () { data.Average ((Func) null); }); // Average (Func>) AssertException (delegate () { ((IEnumerable) null).Average ((Func>) (x => (int?) 0)); }); AssertException (delegate () { data.Average ((Func>) null); }); // Average (Func) AssertException (delegate () { ((IEnumerable) null).Average ((Func) (x => 0L)); }); AssertException (delegate () { data.Average ((Func) null); }); // Average (Func>) AssertException (delegate () { ((IEnumerable) null).Average ((Func>) (x => (int?) 0L)); }); AssertException (delegate () { data.Average ((Func>) null); }); // Average (Func) AssertException (delegate () { ((IEnumerable) null).Average ((Func) (x => 0f)); }); AssertException (delegate () { data.Average ((Func) null); }); // Average (Func>) AssertException (delegate () { ((IEnumerable) null).Average ((Func>) (x => (int?) 0f)); }); AssertException (delegate () { data.Average ((Func>) null); }); // Average (Func) AssertException (delegate () { ((IEnumerable) null).Average ((Func) (x => 0d)); }); AssertException (delegate () { data.Average ((Func) null); }); // Average (Func>) AssertException (delegate () { ((IEnumerable) null).Average ((Func>) (x => (int?) 0d)); }); AssertException (delegate () { data.Average ((Func>) null); }); // Average (Func) AssertException (delegate () { ((IEnumerable) null).Average ((Func) (x => 0m)); }); AssertException (delegate () { data.Average ((Func) null); }); // Average (Func>) AssertException (delegate () { ((IEnumerable) null).Average ((Func>) (x => (int?) 0m)); }); AssertException (delegate () { data.Average ((Func>) null); }); // Average<> () AssertException (delegate () { ((IEnumerable) null).Average (); }); // Average<> () AssertException (delegate () { ((IEnumerable>) null).Average (); }); // Average<> () AssertException (delegate () { ((IEnumerable) null).Average (); }); // Average<> () AssertException (delegate () { ((IEnumerable>) null).Average (); }); // Average<> () AssertException (delegate () { ((IEnumerable) null).Average (); }); // Average<> () AssertException (delegate () { ((IEnumerable>) null).Average (); }); // Average<> () AssertException (delegate () { ((IEnumerable) null).Average (); }); // Average<> () AssertException (delegate () { ((IEnumerable>) null).Average (); }); // Average<> () AssertException (delegate () { ((IEnumerable) null).Average (); }); // Average<> () AssertException (delegate () { ((IEnumerable>) null).Average (); }); } [Test] public void AverageTest () { string [] data = { "2", "1", "5", "3", "4" }; string [] empty = { }; // Average (Func) Assert.AreEqual (3, ((IEnumerable) data).Average ((Func) (x => int.Parse (x)))); AssertException (delegate () { empty.Average ((Func) (x => int.Parse (x))); }); // Average (Func>) Assert.AreEqual (3, ((IEnumerable) data).Average ((Func>) (x => (int?) int.Parse (x)))); // Average (Func) Assert.AreEqual (3, ((IEnumerable) data).Average ((Func) (x => int.Parse (x)))); AssertException (delegate () { empty.Average ((Func) (x => int.Parse (x))); }); // Average (Func>) Assert.AreEqual (3, ((IEnumerable) data).Average ((Func) (x => (int?) int.Parse (x)))); // Average (Func) Assert.AreEqual (3, ((IEnumerable) data).Average ((Func) (x => int.Parse (x)))); AssertException (delegate () { empty.Average ((Func) (x => int.Parse (x))); }); // Average (Func>) Assert.AreEqual (3, ((IEnumerable) data).Average ((Func) (x => (int?) int.Parse (x)))); // Average (Func) Assert.AreEqual (3, ((IEnumerable) data).Average ((Func) (x => int.Parse (x)))); AssertException (delegate () { empty.Average ((Func) (x => int.Parse (x))); }); // Average (Func>) Assert.AreEqual (3, ((IEnumerable) data).Average ((Func) (x => (int?) int.Parse (x)))); // Average (Func) Assert.AreEqual (3, ((IEnumerable) data).Average ((Func) (x => int.Parse (x)))); AssertException (delegate () { empty.Average ((Func) (x => int.Parse (x))); }); // Average (Func>) Assert.AreEqual (3, ((IEnumerable) data).Average ((Func) (x => (int?) int.Parse (x)))); // Average<> () Assert.AreEqual (3, ((IEnumerable) new int [] { 2, 3, 4 }).Average ()); AssertException (delegate () { new int [0].Average (); }); // Average<> () Assert.AreEqual (3, ((IEnumerable>) new int? [] { 2, 3, 4 }).Average ()); // Average<> () Assert.AreEqual (3, ((IEnumerable) new long [] { 2, 3, 4 }).Average ()); AssertException (delegate () { new long [0].Average (); }); // Average<> () Assert.AreEqual (3, ((IEnumerable>) new long? [] { 2, 3, 4 }).Average ()); // Average<> () Assert.AreEqual (3, ((IEnumerable) new float [] { 2, 3, 4 }).Average ()); AssertException (delegate () { new float [0].Average (); }); // Average<> () Assert.AreEqual (3, ((IEnumerable>) new float? [] { 2, 3, 4 }).Average ()); // Average<> () Assert.AreEqual (3, ((IEnumerable) new double [] { 2, 3, 4 }).Average ()); AssertException (delegate () { new double [0].Average (); }); // Average<> () Assert.AreEqual (3, ((IEnumerable>) new double? [] { 2, 3, 4 }).Average ()); // Average<> () Assert.AreEqual (3, ((IEnumerable) new decimal [] { 2, 3, 4 }).Average ()); AssertException (delegate () { new decimal [0].Average (); }); // Average<> () Assert.AreEqual (3, ((IEnumerable>) new decimal? [] { 2, 3, 4 }).Average ()); } [Test] public void WhereArgumentNullTest () { string [] data = { "2", "1", "5", "3", "4" }; // Where (Func) AssertException (delegate () { ((IEnumerable) null).Where (x => true); }); AssertException (delegate () { data.Where ((Func) null); }); // Where (Func) AssertException (delegate () { ((IEnumerable) null).Where ((x, y) => true); }); AssertException (delegate () { data.Where ((Func) null); }); } [Test] public void WhereTest () { int [] data = { 2, 1, 5, 3, 4 }; int [] expected1 = { 2, 1 }; int [] expected2 = { 2 }; // Where (Func) AssertAreSame (expected1, data.Where (x => x < 3)); // Where (Func) AssertAreSame (expected2, data.Where ((x, y) => x < 3 && y != 1)); } [Test] public void SelectArgumentNullTest () { string [] data = { "2", "1", "5", "3", "4" }; // Select (Func) AssertException (delegate () { ((IEnumerable) null).Select (x => "test"); }); AssertException (delegate () { data.Select ((Func) null); }); // Select (Func) AssertException (delegate () { ((IEnumerable) null).Select ((x, y) => "test"); }); AssertException (delegate () { data.Select ((Func) null); }); } [Test] public void SelectTest () { string [] data = { "2", "1", "5", "3", "4" }; string [] expected1 = { "2x", "1x", "5x", "3x", "4x" }; string [] expected2 = { "2x0", "1x1", "5x2", "3x3", "4x4" }; // Select (Func) AssertAreSame (expected1, data.Select (x => x + "x")); // Select (Func) AssertAreSame (expected2, data.Select ((x, y) => x + "x" + y)); } [Test] public void SelectManyArgumentNullTest () { string [] data = { "2", "1", "5", "3", "4" }; // SelectMany (Func>) AssertException (delegate () { ((IEnumerable) null).SelectMany (x => data); }); AssertException (delegate () { data.SelectMany ((Func>) null); }); // SelectMany (Func>) AssertException (delegate () { ((IEnumerable) null).SelectMany ((x, y) => data); }); AssertException (delegate () { data.SelectMany ((Func>) null); }); // SelectMany (Func>, Func) AssertException (delegate () { ((IEnumerable) null).SelectMany ((x, y) => data, (x, y) => "test"); }); AssertException (delegate () { data.SelectMany ((Func>) null, (x, y) => "test"); }); AssertException (delegate () { data.SelectMany ((x, y) => data, (Func) null); }); // SelectMany (Func>, Func) AssertException (delegate () { ((IEnumerable) null).SelectMany (x => data, (x, y) => "test"); }); AssertException (delegate () { data.SelectMany ((Func>) null, (x, y) => "test"); }); AssertException (delegate () { data.SelectMany (x => data, (Func) null); }); } [Test] public void SelectManyTest () { string [] data = { "0", "1" }; string [] expected = { "0", "00", "1", "11" }; // SelectMany (Func>) AssertAreSame (expected, ((IEnumerable) data).SelectMany (x => new string [] { x, x + x })); // SelectMany (Func>) AssertAreSame (expected, ((IEnumerable) data).SelectMany ((x, y) => new string [] { x, x + y })); // SelectMany (Func>, Func) AssertAreSame (expected, ((IEnumerable) data).SelectMany ((x, y) => new string [] { x, x + y }, (x, y) => y)); // SelectMany (Func>, Func) AssertAreSame (expected, ((IEnumerable) data).SelectMany (x => new string [] { x, x + x }, (x, y) => y)); } [Test] public void TakeArgumentNullTest () { //string [] data = { "2", "1", "5", "3", "4" }; // Take (int) AssertException (delegate () { ((IEnumerable) null).Take (0); }); } [Test] public void TakeTest () { int [] data = { 2, 1, 5, 3, 1 }; int [] expected = { 2, 1 }; int [] empty = { }; // Take (int) AssertAreSame (expected, data.Take (2)); AssertAreSame (empty, data.Take (-2)); } [Test] public void TakeWhileArgumentNullTest () { string [] data = { "2", "1", "5", "3", "4" }; // TakeWhile (Func) AssertException (delegate () { ((IEnumerable) null).TakeWhile (x => true); }); AssertException (delegate () { data.TakeWhile ((Func) null); }); // TakeWhile (Func) AssertException (delegate () { ((IEnumerable) null).TakeWhile ((x, y) => true); }); AssertException (delegate () { data.TakeWhile ((Func) null); }); } [Test] public void TakeWhileTest () { int [] data = { 2, 1, 5, 3, 1 }; int [] expected = { 2, 1 }; // TakeWhile (Func) AssertAreSame (expected, data.TakeWhile (x => x != 5)); // TakeWhile (Func) AssertAreSame (expected, data.TakeWhile ((x, y) => y != 2)); } [Test] public void SkipArgumentNullTest () { //string [] data = { "2", "1", "5", "3", "4" }; // Skip (int) AssertException (delegate () { ((IEnumerable) null).Skip (0); }); } [Test] public void SkipTest () { int [] data = { 2, 1, 5, 3, 1 }; int [] expected = { 5, 3, 1 }; // Skip (TSource) AssertAreSame (expected, data.Skip (2)); AssertAreSame (data, data.Skip (-2)); } [Test] public void SkipWhileArgumentNullTest () { string [] data = { "2", "1", "5", "3", "4" }; // SkipWhile (Func) AssertException (delegate () { ((IEnumerable) null).SkipWhile (x => true); }); AssertException (delegate () { data.SkipWhile ((Func) null); }); // SkipWhile (Func) AssertException (delegate () { ((IEnumerable) null).SkipWhile ((x, y) => true); }); AssertException (delegate () { data.SkipWhile ((Func) null); }); } [Test] public void SkipWhileTest () { int [] data = { 2, 1, 5, 3, 1 }; int [] expected = { 5, 3, 1 }; // SkipWhile (Func) AssertAreSame (expected, data.SkipWhile (x => x != 5)); // SkipWhile (Func) AssertAreSame (expected, data.SkipWhile ((x, y) => y != 2)); } [Test] public void JoinArgumentNullTest () { string [] data = { "2", "1", "5", "3", "4" }; // Join (IEnumerable, Func, Func, Func) AssertException (delegate () { ((IEnumerable) null).Join (data, x => "test", x => "test", (x, y) => "test"); }); AssertException (delegate () { data.Join ((IEnumerable) null, x => "test", x => "test", (x, y) => "test"); }); AssertException (delegate () { data.Join (data, (Func) null, x => "test", (x, y) => "test"); }); AssertException (delegate () { data.Join (data, x => "test", (Func) null, (x, y) => "test"); }); AssertException (delegate () { data.Join (data, x => "test", x => "test", (Func) null); }); // Join (IEnumerable, Func, Func, Func, IEqualityComparer) AssertException (delegate () { ((IEnumerable) null).Join (data, x => "test", x => "test", (x, y) => "test", EqualityComparer.Default); }); AssertException (delegate () { data.Join ((IEnumerable) null, x => "test", x => "test", (x, y) => "test", EqualityComparer.Default); }); AssertException (delegate () { data.Join (data, (Func) null, x => "test", (x, y) => "test", EqualityComparer.Default); }); AssertException (delegate () { data.Join (data, x => "test", (Func) null, (x, y) => "test", EqualityComparer.Default); }); AssertException (delegate () { data.Join (data, x => "test", x => "test", (Func) null, EqualityComparer.Default); }); } [Test] public void JoinTest () { string [] dataOuter1 = { "2", "1", "5", "3", "4" }; string [] dataInner1 = { "7", "3", "5", "8", "9" }; string [] expected1 = { "55", "33" }; string [] dataOuter2 = { "2", "1", "3", "4" }; string [] dataInner2 = { "7", "5", "8", "9" }; string [] expected2 = { }; // Join (IEnumerable, Func, Func, Func) AssertAreSame (expected1, dataOuter1.Join (dataInner1, x => x, x => x, (x, y) => x + y)); AssertAreSame (expected2, dataOuter2.Join (dataInner2, x => x, x => x, (x, y) => x + y)); // Join (IEnumerable, Func, Func, Func, IEqualityComparer) AssertAreSame (expected1, dataOuter1.Join (dataInner1, x => x, x => x, (x, y) => x + y, EqualityComparer.Default)); AssertAreSame (expected2, dataOuter2.Join (dataInner2, x => x, x => x, (x, y) => x + y, EqualityComparer.Default)); } [Test] public void JoinTestNullKeys () { var l1 = new [] { new { Name = "name1", Nullable = (int?) null }, new { Name = "name2", Nullable = (int?) null } }; var count = l1.Join (l1, i => i.Nullable, i => i.Nullable, (x, y) => x.Name).Count (); Assert.AreEqual (0, count); } [Test] public void GroupJoinArgumentNullTest () { string [] data = { "2", "1", "5", "3", "4" }; // GroupJoin (IEnumerable, Func, Func, Func, TResult>) AssertException (delegate () { ((IEnumerable) null).GroupJoin (data, x => "test", x => "test", (x, y) => "test"); }); AssertException (delegate () { data.GroupJoin ((IEnumerable) null, x => "test", x => "test", (x, y) => "test"); }); AssertException (delegate () { data.GroupJoin (data, (Func) null, x => "test", (x, y) => "test"); }); AssertException (delegate () { data.GroupJoin (data, x => "test", (Func) null, (x, y) => "test"); }); AssertException (delegate () { data.GroupJoin (data, x => "test", x => "test", (Func, string>) null); }); // GroupJoin (IEnumerable, Func, Func, Func, TResult, IEqualityComparer>) AssertException (delegate () { ((IEnumerable) null).GroupJoin (data, x => "test", x => "test", (x, y) => "test", EqualityComparer.Default); }); AssertException (delegate () { data.GroupJoin ((IEnumerable) null, x => "test", x => "test", (x, y) => "test", EqualityComparer.Default); }); AssertException (delegate () { data.GroupJoin (data, (Func) null, x => "test", (x, y) => "test", EqualityComparer.Default); }); AssertException (delegate () { data.GroupJoin (data, x => "test", (Func) null, (x, y) => "test", EqualityComparer.Default); }); AssertException (delegate () { data.GroupJoin (data, x => "test", x => "test", (Func, string>) null, EqualityComparer.Default); }); } [Test] public void GroupJoinTest () { string [] dataOuter1 = { "2", "1", "5", "3", "4" }; string [] dataInner1 = { "7", "3", "5", "3", "9" }; string [] expected1 = { "2", "1", "55", "333", "4" }; string [] dataOuter2 = { "2", "1", "5", "8", "4" }; string [] dataInner2 = { "7", "3", "6", "3", "9" }; string [] expected2 = { "2", "1", "5", "8", "4" }; // GroupJoin (IEnumerable, Func, Func, Func, TResult>) AssertAreSame (expected1, (dataOuter1.GroupJoin (dataInner1, x => x, x => x, (x, y) => { foreach (var s in y) x += s; return x; }))); AssertAreSame (expected2, (dataOuter2.GroupJoin (dataInner2, x => x, x => x, (x, y) => { foreach (var s in y) x += s; return x; }))); // GroupJoin (IEnumerable, Func, Func, Func, TResult, IEqualityComparer>) AssertAreSame (expected1, dataOuter1.GroupJoin (dataInner1, x => x, x => x, (x, y) => { foreach (var s in y) x += s; return x; }, EqualityComparer.Default)); AssertAreSame (expected2, dataOuter2.GroupJoin (dataInner2, x => x, x => x, (x, y) => { foreach (var s in y) x += s; return x; }, EqualityComparer.Default)); } [Test] public void GroupJoinWithNullKeys () { string[] l1 = { null }; string[] l2 = { null, null }; var res = l1.GroupJoin (l2, x => x, y => y, (a, b) => new { Key = a, Count = b.Count () }).ToArray (); Assert.AreEqual (1, res.Length); Assert.AreEqual (0, res [0].Count); } [Test] public void OrderByArgumentNullTest () { string [] data = { "2", "1", "5", "3", "4" }; // OrderBy (Func) AssertException (delegate () { ((IEnumerable) null).OrderBy (x => "test"); }); AssertException (delegate () { data.OrderBy ((Func) null); }); // OrderBy (Func, IComparer) AssertException (delegate () { ((IEnumerable) null).OrderBy (x => "test", Comparer.Default); }); AssertException (delegate () { data.OrderBy ((Func) null, Comparer.Default); }); } [Test] public void OrderByTest () { int [] data = { 2, 1, 5, 3, 4 }; int [] expected = { 1, 2, 3, 4, 5 }; // OrderBy (Func) AssertAreSame (expected, data.OrderBy (x => x)); // OrderBy (Func, IComparer) AssertAreSame (expected, data.OrderBy (x => x, Comparer.Default)); } [Test] public void OrderByDescendingArgumentNullTest () { string [] data = { "2", "1", "5", "3", "4" }; // OrderByDescending (Func) AssertException (delegate () { ((IEnumerable) null).OrderByDescending (x => "test"); }); AssertException (delegate () { data.OrderByDescending ((Func) null); }); // OrderByDescending (Func, IComparer) AssertException (delegate () { ((IEnumerable) null).OrderByDescending (x => "test", Comparer.Default); }); AssertException (delegate () { data.OrderByDescending ((Func) null, Comparer.Default); }); } [Test] public void OrderByDescendingTest () { int [] data = { 2, 1, 5, 3, 4 }; int [] expected = { 5, 4, 3, 2, 1 }; // OrderByDescending (Func) AssertAreSame (expected, data.OrderByDescending (x => x)); // OrderByDescending (Func, IComparer) AssertAreSame (expected, data.OrderByDescending (x => x, Comparer.Default)); } [Test] public void ThenByArgumentNullTest () { string [] data = { "2", "1", "5", "3", "4" }; // ThenBy (Func) AssertException (delegate () { ((IOrderedEnumerable) null).ThenBy (x => "test"); }); AssertException (delegate () { data.OrderBy (x => x).ThenBy ((Func) null); }); // ThenBy (Func, IComparer) AssertException (delegate () { ((IOrderedEnumerable) null).ThenBy (x => "test", Comparer.Default); }); AssertException (delegate () { data.OrderBy (x => x).ThenBy ((Func) null, Comparer.Default); }); } [Test] public void ThenByTest () { int [] data = { 2, 1, 5, 3, 4 }; int [] expected = { 1, 2, 3, 4, 5 }; // ThenBy (Func) AssertAreSame (expected, data.OrderBy (x => x).ThenBy (x => x)); // ThenBy (Func, IComparer) AssertAreSame (expected, data.OrderBy (x => x).ThenBy (x => x, Comparer.Default)); } [Test] public void ThenByDescendingArgumentNullTest () { string [] data = { "2", "1", "5", "3", "4" }; // ThenByDescending (Func) AssertException (delegate () { ((IOrderedEnumerable) null).ThenByDescending (x => "test"); }); AssertException (delegate () { data.OrderBy (x => x).ThenByDescending ((Func) null); }); // ThenByDescending (Func, IComparer) AssertException (delegate () { ((IOrderedEnumerable) null).ThenByDescending (x => "test", Comparer.Default); }); AssertException (delegate () { data.OrderBy (x => x).ThenByDescending ((Func) null, Comparer.Default); }); } [Test] public void ThenByDescendingTest () { int [] data = { 2, 1, 5, 3, 4 }; int [] expected = { 5, 4, 3, 2, 1 }; // ThenByDescending (Func) AssertAreSame (expected, data.OrderBy (x => 0).ThenByDescending (x => x)); // ThenByDescending (Func, IComparer) AssertAreSame (expected, data.OrderBy (x => 0).ThenByDescending (x => x, Comparer.Default)); } [Test] public void GroupByArgumentNullTest () { string [] data = { "2", "1", "5", "3", "4" }; // GroupBy (Func) AssertException (delegate () { ((IEnumerable) null).GroupBy ((Func) (x => "test")); }); AssertException (delegate () { data.GroupBy ((Func) null); }); // GroupBy (Func, IEqualityComparer) AssertException (delegate () { ((IEnumerable) null).GroupBy ((Func) (x => "test"), (IEqualityComparer) EqualityComparer.Default); }); AssertException (delegate () { data.GroupBy ((Func) null, (IEqualityComparer) EqualityComparer.Default); }); // GroupBy (Func, Func) AssertException (delegate () { ((IEnumerable) null).GroupBy ((Func) (x => "test"), (Func) (x => "test")); }); AssertException (delegate () { data.GroupBy ((Func) null, (Func) (x => "test")); }); AssertException (delegate () { data.GroupBy ((Func) (x => "test"), (Func) null); }); // GroupBy (Func, Func, IEqualityComparer) AssertException (delegate () { ((IEnumerable) null).GroupBy ((Func) (x => "test"), (Func) (x => "test"), (IEqualityComparer) EqualityComparer.Default); }); AssertException (delegate () { data.GroupBy ((Func) null, (Func) (x => "test"), (IEqualityComparer) EqualityComparer.Default); }); AssertException (delegate () { data.GroupBy ((Func) (x => "test"), (Func) null, (IEqualityComparer) EqualityComparer.Default); }); // GroupBy (Func, Func, string>) AssertException (delegate () { ((IEnumerable) null).GroupBy ((Func) (x => "test"), (Func, string>) ((x, y) => "test")); }); AssertException (delegate () { data.GroupBy ((Func) null, (Func, string>) ((x, y) => "test")); }); AssertException (delegate () { data.GroupBy ((Func) (x => "test"), (Func, string>) null); }); // GroupBy (Func, Func, Func, string>) AssertException (delegate () { ((IEnumerable) null).GroupBy ((Func) (x => "test"), (Func) (x => "test"), (Func, string>) ((x, y) => "test")); }); AssertException (delegate () { data.GroupBy ((Func) null, (Func) (x => "test"), (Func, string>) ((x, y) => "test")); }); AssertException (delegate () { data.GroupBy ((Func) (x => "test"), (Func) null, (Func, string>) ((x, y) => "test")); }); AssertException (delegate () { data.GroupBy ((Func) (x => "test"), (Func) (x => "test"), (Func, string>) null); }); // GroupBy (Func, Func, string>, IEqualityComparer) AssertException (delegate () { ((IEnumerable) null).GroupBy ((Func) (x => "test"), (Func, string>) ((x, y) => "test"), (IEqualityComparer) EqualityComparer.Default); }); AssertException (delegate () { data.GroupBy ((Func) null, (Func, string>) ((x, y) => "test"), (IEqualityComparer) EqualityComparer.Default); }); AssertException (delegate () { data.GroupBy ((Func) (x => "test"), (Func, string>) null, (IEqualityComparer) EqualityComparer.Default); }); // GroupBy (Func, Func, Func, string>, IEqualityComparer) AssertException (delegate () { ((IEnumerable) null).GroupBy ((Func) (x => "test"), (Func) (x => "test"), (Func, string>) ((x, y) => "test"), (IEqualityComparer) EqualityComparer.Default); }); AssertException (delegate () { data.GroupBy ((Func) null, (Func) (x => "test"), (Func, string>) ((x, y) => "test"), (IEqualityComparer) EqualityComparer.Default); }); AssertException (delegate () { data.GroupBy ((Func) (x => "test"), (Func) null, (Func, string>) ((x, y) => "test"), (IEqualityComparer) EqualityComparer.Default); }); AssertException (delegate () { data.GroupBy ((Func) (x => "test"), (Func) (x => "test"), (Func, string>) null, (IEqualityComparer) EqualityComparer.Default); }); } [Test] public void GroupByTest () { string [] data = { "2", "1", "5", "3", "4", "3" }; Dictionary> expected = new Dictionary> (); expected.Add ("2", new List () { "2" }); expected.Add ("1", new List () { "1" }); expected.Add ("5", new List () { "5" }); expected.Add ("3", new List () { "3", "3" }); expected.Add ("4", new List () { "4" }); Dictionary> expected2 = new Dictionary> (); expected2.Add ("2", new List () { "22" }); expected2.Add ("1", new List () { "11" }); expected2.Add ("5", new List () { "55" }); expected2.Add ("3", new List () { "33", "33" }); expected2.Add ("4", new List () { "44" }); string [] expected3 = new string [] { "22", "11", "55", "333", "44" }; // GroupBy (Func) AssertAreSame (expected, data.GroupBy (x => x)); // GroupBy (Func, IEqualityComparer) AssertAreSame (expected, data.GroupBy (x => x, EqualityComparer.Default)); // GroupBy (Func, Func) AssertAreSame (expected2, data.GroupBy (x => x, x => x + x)); // GroupBy (Func, Func, IEqualityComparer) AssertAreSame (expected2, data.GroupBy (x => x, x => x + x, EqualityComparer.Default)); // GroupBy (Func, Func, int>) AssertAreSame (expected3, data.GroupBy (x => x, (x, y) => { foreach (var s in y) x += s; return x; })); // GroupBy (Func, Func, Func, int>) AssertAreSame (expected3, data.GroupBy (x => x, x => x, (x, y) => { foreach (var s in y) x += s; return x; })); // GroupBy (Func, Func, int>, IEqualityComparer) AssertAreSame (expected3, data.GroupBy (x => x, (x, y) => { foreach (var s in y) x += s; return x; }, EqualityComparer.Default)); // GroupBy (Func, Func, Func, int>, IEqualityComparer) AssertAreSame (expected3, data.GroupBy (x => x, x => x, (x, y) => { foreach (var s in y) x += s; return x; }, EqualityComparer.Default)); } class Data { public int Number; public string String; public Data (int number, string str) { Number = number; String = str; } } [Test] public void GroupByLastNullGroup () { var values = new List (); values.Add (new Data (0, "a")); values.Add (new Data (1, "a")); values.Add (new Data (2, "b")); values.Add (new Data (3, "b")); values.Add (new Data (4, null)); var groups = values.GroupBy (d => d.String); Assert.AreEqual (3, groups.Count ()); } [Test] public void ConcatArgumentNullTest () { string [] data = { "2", "1", "5", "3", "4" }; // Concat (IEnumerable) AssertException (delegate () { ((IEnumerable) null).Concat (data); }); AssertException (delegate () { data.Concat ((IEnumerable) null); }); } [Test] public void ConcatTest () { int [] data1 = { 2, 1, 5, 3, 4 }; int [] data2 = { 1, 2, 3, 4, 5 }; int [] expected = { 2, 1, 5, 3, 4, 1, 2, 3, 4, 5 }; // Concat (IEnumerable) AssertAreSame (expected, data1.Concat (data2)); } [Test] public void DistinctArgumentNullTest () { //string [] data = { "2", "1", "5", "3", "4" }; // Distinct () AssertException (delegate () { ((IEnumerable) null).Distinct (); }); // Distinct (IEqualityComparer) AssertException (delegate () { ((IEnumerable) null).Distinct (EqualityComparer.Default); }); } [Test] public void DistinctTest () { int [] data = { 2, 1, 5, 3, 4, 2, 5, 3, 1, 8 }; int [] expected = { 2, 1, 5, 3, 4, 8 }; // Distinct () AssertAreSame (expected, data.Distinct ()); // Distinct (IEqualityComparer) AssertAreSame (expected, data.Distinct (EqualityComparer.Default)); } [Test] public void UnionArgumentNullTest () { string [] data = { "2", "1", "5", "3", "4" }; // Union (IEnumerable) AssertException (delegate () { ((IEnumerable) null).Union (data); }); AssertException (delegate () { data.Union ((IEnumerable) null); }); // Union (IEnumerable, IEqualityComparer) AssertException (delegate () { ((IEnumerable) null).Union (data, EqualityComparer.Default); }); AssertException (delegate () { data.Union ((IEnumerable) null, EqualityComparer.Default); }); } [Test] public void UnionTest () { int [] data1 = { 2, 1, 5, 7, 3, 4 }; int [] data2 = { 1, 2, 3, 8, 4, 5 }; int [] expected = { 2, 1, 5, 7, 3, 4, 8 }; // Union (IEnumerable) AssertAreSame (expected, data1.Union (data2)); // Union (IEnumerable, IEqualityComparer) AssertAreSame (expected, data1.Union (data2, EqualityComparer.Default)); } [Test] public void IntersectArgumentNullTest () { string [] data = { "2", "1", "5", "3", "4" }; // Intersect (IEnumerable) AssertException (delegate () { ((IEnumerable) null).Intersect (data); }); AssertException (delegate () { data.Intersect ((IEnumerable) null); }); // Intersect (IEnumerable, IEqualityComparer) AssertException (delegate () { ((IEnumerable) null).Intersect (data, EqualityComparer.Default); }); AssertException (delegate () { data.Intersect ((IEnumerable) null, EqualityComparer.Default); }); } [Test] public void IntersectTest () { int [] data1 = { 2, 1, 5, 7, 3, 4 }; int [] data2 = { 1, 2, 3, 8, 4, 5 }; int [] expected = { 2, 1, 5, 3, 4 }; // Intersect (IEnumerable) AssertAreSame (expected, data1.Intersect (data2)); // Intersect (IEnumerable, IEqualityComparer) AssertAreSame (expected, data1.Intersect (data2, EqualityComparer.Default)); } [Test] public void ExceptArgumentNullTest () { string [] data = { "2", "1", "5", "3", "4" }; // Except (IEnumerable) AssertException (delegate () { ((IEnumerable) null).Except (data); }); AssertException (delegate () { data.Except ((IEnumerable) null); }); // Except (IEnumerable, IEqualityComparer) AssertException (delegate () { ((IEnumerable) null).Except (data, EqualityComparer.Default); }); AssertException (delegate () { data.Except ((IEnumerable) null, EqualityComparer.Default); }); } [Test] public void ExceptTest () { int [] data1 = { 2, 1, 5, 7, 3, 4 }; int [] data2 = { 1, 2, 3, 8, 4, 5 }; int [] expected = { 7 }; // Except (IEnumerable) AssertAreSame (expected, data1.Except (data2)); // Except (IEnumerable, IEqualityComparer) AssertAreSame (expected, data1.Except (data2, EqualityComparer.Default)); } [Test] public void ReverseArgumentNullTest () { //string [] data = { "2", "1", "5", "3", "4" }; // Reverse () AssertException (delegate () { ((IEnumerable) null).Reverse (); }); } [Test] public void ReverseTest () { int [] data = { 2, 1, 5, 7, 3, 4 }; int [] expected = { 4, 3, 7, 5, 1, 2 }; // Reverse () AssertAreSame (expected, data.Reverse ()); } [Test] public void SequenceEqualArgumentNullTest () { string [] data = { "2", "1", "5", "3", "4" }; // SequenceEqual (IEnumerable) AssertException (delegate () { ((IEnumerable) null).SequenceEqual (data); }); AssertException (delegate () { data.SequenceEqual ((IEnumerable) null); }); // SequenceEqual (IEnumerable, IEqualityComparer) AssertException (delegate () { ((IEnumerable) null).SequenceEqual (data, EqualityComparer.Default); }); AssertException (delegate () { data.SequenceEqual ((IEnumerable) null, EqualityComparer.Default); }); } [Test] public void SequenceEqualTest () { int [] data1 = { 2, 1, 5, 7, 3, 4 }; int [] data2 = { 2, 1, 5, 7, 3, 4 }; int [] data3 = { 2, 1, 5, 7, 3, 4, 5 }; int [] data4 = { 2, 1, 5, 7, 3 }; int [] data5 = { 2, 1, 5, 8, 3, 4 }; // SequenceEqual (IEnumerable) Assert.IsTrue (data1.SequenceEqual (data2)); Assert.IsFalse (data1.SequenceEqual (data3)); Assert.IsFalse (data1.SequenceEqual (data4)); Assert.IsFalse (data1.SequenceEqual (data5)); // SequenceEqual (IEnumerable, IEqualityComparer) Assert.IsTrue (data1.SequenceEqual (data2, EqualityComparer.Default)); Assert.IsFalse (data1.SequenceEqual (data3, EqualityComparer.Default)); Assert.IsFalse (data1.SequenceEqual (data4, EqualityComparer.Default)); Assert.IsFalse (data1.SequenceEqual (data5, EqualityComparer.Default)); } [Test] public void AsEnumerableArgumentNullTest () { //string [] data = { "2", "1", "5", "3", "4" }; } [Test] public void AsEnumerableTest () { int [] data = { 2, 1, 5, 7, 3, 4 }; // AsEnumerable () Assert.AreSame (data, data.AsEnumerable ()); } [Test] public void ToArrayArgumentNullTest () { //string [] data = { "2", "1", "5", "3", "4" }; // ToArray () AssertException (delegate () { ((IEnumerable) null).ToArray (); }); } [Test] public void ToArrayTest () { int [] data = { 2, 3, 4, 5 }; int [] expected = { 2, 3, 4, 5 }; // ToArray () AssertAreSame (expected, data.ToArray ()); } [Test] public void ToListArgumentNullTest () { //string [] data = { "2", "1", "5", "3", "4" }; // ToList () AssertException (delegate () { ((IEnumerable) null).ToList (); }); } [Test] public void ToListTest () { int [] data = { 2, 4, 5, 1 }; int [] expected = { 2, 4, 5, 1 }; // ToList () AssertAreSame (expected, data.ToList ()); } [Test] public void ToDictionaryArgumentNullTest () { string [] data = { "2", "1", "5", "3", "4" }; // ToDictionary (Func) AssertException (delegate () { ((IEnumerable) null).ToDictionary (x => "test"); }); AssertException (delegate () { data.ToDictionary ((Func) null); }); // ToDictionary (Func, IEqualityComparer) AssertException (delegate () { ((IEnumerable) null).ToDictionary (x => "test", EqualityComparer.Default); }); AssertException (delegate () { data.ToDictionary ((Func) null, EqualityComparer.Default); }); // ToDictionary (Func, Func) AssertException (delegate () { ((IEnumerable) null).ToDictionary (x => "test", x => "test"); }); AssertException (delegate () { data.ToDictionary ((Func) null, x => "test"); }); AssertException (delegate () { data.ToDictionary (x => "test", (Func) null); }); // ToDictionary (Func, Func, IEqualityComparer) AssertException (delegate () { ((IEnumerable) null).ToDictionary (x => "test", x => "test", EqualityComparer.Default); }); AssertException (delegate () { data.ToDictionary ((Func) null, x => "test", EqualityComparer.Default); }); AssertException (delegate () { data.ToDictionary (x => "test", (Func) null, EqualityComparer.Default); }); } [Test] public void ToDictionaryTest () { string [] data = { "2", "1", "5", "3", "4" }; Dictionary expected = new Dictionary (); expected.Add ("k2", "2"); expected.Add ("k1", "1"); expected.Add ("k5", "5"); expected.Add ("k3", "3"); expected.Add ("k4", "4"); // ToDictionary (Func) AssertAreSame (expected, ((IEnumerable) data).ToDictionary (x => "k" + x)); AssertException (delegate () { data.ToDictionary (x => "key"); }); // ToDictionary (Func, IEqualityComparer) AssertAreSame (expected, ((IEnumerable) data).ToDictionary (x => "k" + x, EqualityComparer.Default)); AssertException (delegate () { data.ToDictionary (x => "key", EqualityComparer.Default); }); // ToDictionary (Func, Func) AssertAreSame (expected, ((IEnumerable) data).ToDictionary (x => "k" + x, x => x)); AssertException (delegate () { data.ToDictionary (x => "key", x => x); }); // ToDictionary (Func, Func, IEqualityComparer) AssertAreSame (expected, ((IEnumerable) data).ToDictionary (x => "k" + x, x => x, EqualityComparer.Default)); AssertException (delegate () { data.ToDictionary (x => "key", x => x, EqualityComparer.Default); }); } [Test] public void ToLookupArgumentNullTest () { string [] data = { "2", "1", "5", "3", "4" }; // ToLookup (Func) AssertException (delegate () { ((IEnumerable) null).ToLookup ((Func) (x => "test")); }); AssertException (delegate () { data.ToLookup ((Func) null); }); // ToLookup (Func, IEqualityComparer) AssertException (delegate () { ((IEnumerable) null).ToLookup ((Func) (x => "test"), (IEqualityComparer) EqualityComparer.Default); }); AssertException (delegate () { data.ToLookup ((Func) null, (IEqualityComparer) EqualityComparer.Default); }); // ToLookup (Func, Func) AssertException (delegate () { ((IEnumerable) null).ToLookup ((Func) (x => "test"), (Func) (x => "test")); }); AssertException (delegate () { data.ToLookup ((Func) null, (Func) (x => "test")); }); AssertException (delegate () { data.ToLookup ((Func) (x => "test"), (Func) null); }); // ToLookup (Func, Func, IEqualityComparer) AssertException (delegate () { ((IEnumerable) null).ToLookup ((Func) (x => "test"), (Func) (x => "test"), (IEqualityComparer) EqualityComparer.Default); }); AssertException (delegate () { data.ToLookup ((Func) null, (Func) (x => "test"), (IEqualityComparer) EqualityComparer.Default); }); AssertException (delegate () { data.ToLookup ((Func) (x => "test"), (Func) null, (IEqualityComparer) EqualityComparer.Default); }); } [Test] public void ToLookupTest () { string [] data = { "23", "12", "55", "42", "41" }; Dictionary> expected = new Dictionary> (); expected.Add ("2", new List () { "23" }); expected.Add ("1", new List () { "12" }); expected.Add ("5", new List () { "55" }); expected.Add ("4", new List () { "42", "41" }); Assert.AreEqual (expected.Count, ((IEnumerable)data).ToLookup ((x => x [0].ToString ())).Count); // ToLookup (Func) AssertAreSame (expected, ((IEnumerable) data).ToLookup ((x => x [0].ToString ()))); // ToLookup (Func, IEqualityComparer) AssertAreSame (expected, ((IEnumerable) data).ToLookup (x => x [0].ToString (), EqualityComparer.Default)); // ToLookup (Func, Func) AssertAreSame (expected, ((IEnumerable) data).ToLookup (x => x [0].ToString (), x => x)); // ToLookup (Func, Func, IEqualityComparer) AssertAreSame (expected, ((IEnumerable) data).ToLookup (x => x [0].ToString (), x => x, EqualityComparer.Default)); } [Test] public void ToLookupNullKeyTest () { string[] strs = new string[] { "one", null, "two", null, "three" }; int i = 0; var l = strs.ToLookup (s => (s == null) ? null : "numbers", s => (s == null) ? (++i).ToString() : s); Assert.AreEqual (2, l.Count); Assert.AreEqual (2, l [null].Count()); Assert.IsTrue (l [null].Contains ("1")); Assert.IsTrue (l [null].Contains ("2")); Assert.AreEqual (3, l ["numbers"].Count()); Assert.IsTrue (l ["numbers"].Contains ("one")); Assert.IsTrue (l ["numbers"].Contains ("two")); Assert.IsTrue (l ["numbers"].Contains ("three")); } [Test] public void DefaultIfEmptyArgumentNullTest () { //string [] data = { "2", "1", "5", "3", "4" }; // DefaultIfEmpty () AssertException (delegate () { ((IEnumerable) null).DefaultIfEmpty (); }); // DefaultIfEmpty (string) AssertException (delegate () { ((IEnumerable) null).DefaultIfEmpty ((string) "default"); }); } [Test] public void DefaultIfEmptyTest () { string [] data = { "2", "1", "5", "3", "4" }; string [] empty = { }; string [] default1 = { null }; string [] default2 = { "default" }; // DefaultIfEmpty () AssertAreSame (data, data.DefaultIfEmpty ()); AssertAreSame (default1, empty.DefaultIfEmpty ()); // DefaultIfEmpty (string) AssertAreSame (data, data.DefaultIfEmpty ("default")); AssertAreSame (default2, empty.DefaultIfEmpty ("default")); } [Test] public void OfTypeArgumentNullTest () { //string [] data = { "2", "1", "5", "3", "4" }; // OfType () AssertException (delegate () { ((IEnumerable) null).OfType (); }); } [Test] public void OfTypeTest () { object [] data = { "2", 2, "1", "5", "3", "4" }; string [] expected = { "2", "1", "5", "3", "4" }; // OfType () AssertAreSame (expected, data.OfType ()); } [Test] public void CastArgumentNullTest () { //string [] data = { "2", "1", "5", "3", "4" }; // Cast () AssertException (delegate () { ((IEnumerable) null).Cast (); }); } [Test] public void CastTest () { object [] data = { 1, 2, 3 }; int [] expected = { 1, 2, 3 }; // Cast () AssertAreSame (expected, data.Cast ()); AssertException (delegate () { data.Cast ().GetEnumerator ().MoveNext (); }); data.Cast (); } [Test] public void RangeArgumentNullTest () { //string [] data = { "2", "1", "5", "3", "4" }; } [Test] public void RangeTest () { int [] expected = { 2, 3, 4, 5 }; // Range<> (int) AssertAreSame (expected, Enumerable.Range (2, 4)); AssertException (delegate () { Enumerable.Range (2, -3); }); AssertException (delegate () { Enumerable.Range (int.MaxValue - 5, 7); }); Enumerable.Range (int.MaxValue - 5, 6); } [Test] public void ExceptMultipleItems () { var data = new [] { 1, 2, 2, 2, 3, 4, 5, 5, 6, 7, 8, 8, 9, 10 }; var expected = new [] { 2, 4, 6, 8, 10 }; AssertAreSame (expected, data.Except (new [] { 1, 3, 5, 7, 9 })); } } }