//[Theory] //[MemberData(nameof(Max_NullableInt_TestData))] //public void Max_NullableInt(IEnumerable<int?> source, int? expected) //{ // Assert.Equal(expected, source.Max()); // Assert.Equal(expected, source.Max(x => x)); //} //[Fact] //public void Max_NullableInt_NullSource_ThrowsArgumentNullException() //{ // Assert.Throws<ArgumentNullException>("source", () => ((IEnumerable<int?>)null).Max()); // Assert.Throws<ArgumentNullException>("source", () => ((IEnumerable<int?>)null).Max(i => i)); //} public static IEnumerable <object[]> Max_NullableLong_TestData() { yield return(new object[] { Enumerable.Repeat((long?)42, 1), 42L }); yield return(new object[] { Enumerable.Range(1, 10).Select(i => (long?)i).ToArray(), 10L }); yield return(new object[] { new long?[] { null, -100, -15, -50, -10 }, -10L }); yield return(new object[] { new long?[] { null, -16, 0, 50, 100, 1000 }, 1000L }); yield return(new object[] { new long?[] { null, -16, 0, 50, 100, 1000 }.Concat(Enumerable.Repeat((long?)long.MaxValue, 1)), long.MaxValue }); yield return(new object[] { Enumerable.Repeat(default(long?), 100), null }); yield return(new object[] { Enumerable.Empty <long?>(), null }); yield return(new object[] { Enumerable.Repeat((long?)long.MaxValue, 1), long.MaxValue }); yield return(new object[] { Enumerable.Repeat(default(long?), 5), null }); yield return(new object[] { new long?[] { long.MaxValue, null, 9, 10, null, 7, 8 }, long.MaxValue }); yield return(new object[] { new long?[] { null, null, null, null, null, -long.MaxValue }, -long.MaxValue }); yield return(new object[] { new long?[] { -6, null, null, 0, -9, 0, -10, -30 }, 0L }); }
public void LastOrDefaultOnOrdered() { Assert.Equal(9, Enumerable.Range(0, 10).Shuffle().OrderBy(i => i).LastOrDefault()); Assert.Equal(0, Enumerable.Range(0, 10).Shuffle().OrderByDescending(i => i).LastOrDefault()); Assert.Equal(10, Enumerable.Range(0, 100).Shuffle().OrderBy(i => i.ToString().Length).ThenByDescending(i => i).LastOrDefault()); Assert.Equal(0, Enumerable.Empty <int>().OrderBy(i => i).LastOrDefault()); }
public void SumOfLong_SourceIsEmptyCollection_ZeroReturned() { IEnumerable <long> sourceLong = Enumerable.Empty <long>(); Assert.Equal(0L, sourceLong.Sum()); Assert.Equal(0L, sourceLong.Sum(x => x)); }
//[Fact] //public void Max_Decimal_NullSource_ThrowsArgumentNullException() //{ // Assert.Throws<ArgumentNullException>("source", () => ((IEnumerable<decimal>)null).Max()); // Assert.Throws<ArgumentNullException>("source", () => ((IEnumerable<decimal>)null).Max(i => i)); //} //[Fact] //public void Max_Decimal_EmptySource_ThrowsInvalidOperationException() //{ // Assert.Throws<InvalidOperationException>(() => Enumerable.Empty<decimal>().Max()); // Assert.Throws<InvalidOperationException>(() => Enumerable.Empty<decimal>().Max(x => x)); //} public static IEnumerable <object[]> Max_NullableInt_TestData() { yield return(new object[] { Enumerable.Repeat((int?)42, 1), 42 }); yield return(new object[] { Enumerable.Range(1, 10).Select(i => (int?)i).ToArray(), 10 }); yield return(new object[] { new int?[] { null, -100, -15, -50, -10 }, -10 }); yield return(new object[] { new int?[] { null, -16, 0, 50, 100, 1000 }, 1000 }); yield return(new object[] { new int?[] { null, -16, 0, 50, 100, 1000 }.Concat(Enumerable.Repeat((int?)int.MaxValue, 1)), int.MaxValue }); yield return(new object[] { Enumerable.Repeat(default(int?), 100), null }); yield return(new object[] { Enumerable.Empty <int?>(), null }); yield return(new object[] { Enumerable.Repeat((int?)-20, 1), -20 }); yield return(new object[] { new int?[] { -6, null, -9, -10, null, -17, -18 }, -6 }); yield return(new object[] { new int?[] { null, null, null, null, null, -5 }, -5 }); yield return(new object[] { new int?[] { 6, null, null, 100, 9, 100, 10, 100 }, 100 }); yield return(new object[] { Enumerable.Repeat(default(int?), 5), null }); }
private void TestEmptyCached <T>() { var enumerable1 = Enumerable.Empty <T>(); var enumerable2 = Enumerable.Empty <T>(); Assert.Same(enumerable1, enumerable2); // Enumerable.Empty is not cached if not the same. }
//[Fact] //public void Min_Decimal_NullSource_ThrowsArgumentNullException() //{ // Assert.Throws<ArgumentNullException>("source", () => ((IEnumerable<decimal>)null).Min()); // Assert.Throws<ArgumentNullException>("source", () => ((IEnumerable<decimal>)null).Min(x => x)); //} public static IEnumerable <object[]> Min_NullableInt_TestData() { yield return(new object[] { Enumerable.Range(1, 10).Select(i => (int?)i).ToArray(), 1 }); yield return(new object[] { new int?[] { null, -1, -10, 10, 200, 1000 }, -10 }); yield return(new object[] { new int?[] { null, 3000, 100, 200, 1000 }, 100 }); yield return(new object[] { new int?[] { null, 3000, 100, 200, 1000 }.Concat(Enumerable.Repeat((int?)int.MinValue, 1)), int.MinValue }); yield return(new object[] { Enumerable.Repeat(default(int?), 100), null }); yield return(new object[] { Enumerable.Repeat((int?)42, 1), 42 }); yield return(new object[] { Enumerable.Empty <int?>(), null }); yield return(new object[] { Enumerable.Repeat((int?)20, 1), 20 }); yield return(new object[] { Enumerable.Repeat(default(int?), 5), null }); yield return(new object[] { new int?[] { 6, null, 9, 10, null, 7, 8 }, 6 }); yield return(new object[] { new int?[] { null, null, null, null, null, -5 }, -5 }); yield return(new object[] { new int?[] { 6, null, null, 0, 9, 0, 10, 0 }, 0 }); }
//[Theory] //[MemberData(nameof(Max_NullableDouble_TestData))] //public void Max_NullableDouble(IEnumerable<double?> source, double? expected) //{ // Assert.Equal(expected, source.Max()); // Assert.Equal(expected, source.Max(x => x)); //} //[Fact] //public void Max_NullableDouble_NullSource_ThrowsArgumentNullException() //{ // Assert.Throws<ArgumentNullException>("source", () => ((IEnumerable<double?>)null).Max()); // Assert.Throws<ArgumentNullException>("source", () => ((IEnumerable<double?>)null).Max(i => i)); //} public static IEnumerable <object[]> Max_NullableDecimal_TestData() { yield return(new object[] { Enumerable.Repeat((decimal?)42, 1), 42m }); yield return(new object[] { Enumerable.Range(1, 10).Select(i => (decimal?)i).ToArray(), 10m }); yield return(new object[] { new decimal?[] { null, -100M, -15, -50, -10 }, -10m }); yield return(new object[] { new decimal?[] { null, -16M, 0, 50, 100, 1000 }, 1000m }); yield return(new object[] { new decimal?[] { null, -16M, 0, 50, 100, 1000 }.Concat(Enumerable.Repeat((decimal?)decimal.MaxValue, 1)), decimal.MaxValue }); yield return(new object[] { Enumerable.Repeat(default(decimal?), 100), null }); yield return(new object[] { Enumerable.Empty <decimal?>(), null }); yield return(new object[] { Enumerable.Repeat((decimal?)decimal.MaxValue, 1), decimal.MaxValue }); yield return(new object[] { Enumerable.Repeat(default(decimal?), 5), null }); yield return(new object[] { new decimal?[] { 14.50m, null, null, 10.98m, null, 7.5m, 8.6m }, 14.50m }); yield return(new object[] { new decimal?[] { null, null, null, null, null, 0m }, 0m }); yield return(new object[] { new decimal?[] { 6.4m, null, null, decimal.MaxValue, 9.4m, decimal.MaxValue, 10.9m, decimal.MaxValue }, decimal.MaxValue }); }
public void SumOfInt_SourceIsEmptyCollection_ZeroReturned() { IEnumerable <int> sourceInt = Enumerable.Empty <int>(); Assert.Equal(0, sourceInt.Sum()); Assert.Equal(0, sourceInt.Sum(x => x)); }
public void EmptyArraysSameObject() { Assert.Same(Enumerable.Empty <int>().ToArray(), Enumerable.Empty <int>().ToArray()); var array = new int[0]; Assert.NotSame(array, array.ToArray()); }
public void ForcedToEnumeratorDoesntEnumerate() { var iterator = NumberRangeGuaranteedNotCollectionType(0, 3).GroupJoin(Enumerable.Empty <int>(), i => i, i => i, (o, i) => i); // Don't insist on this behaviour, but check it's correct if it happens var en = iterator as IEnumerator <IEnumerable <int> >; Assert.False(en != null && en.MoveNext()); }
public void SkipOnEmptyIList() { // Enumerable.Empty does return an IList, but not guaranteed as such // by the spec. Assert.Equal(Enumerable.Empty <int>(), Enumerable.Empty <int>().ToList().Skip(0)); Assert.Equal(Enumerable.Empty <string>(), Enumerable.Empty <string>().ToList().Skip(-1)); Assert.Equal(Enumerable.Empty <double>(), Enumerable.Empty <double>().ToList().Skip(1)); }
public void Where_EmptyEnumerable_ReturnsNoElements() { IEnumerable <int> source = Enumerable.Empty <int>(); bool wasSelectorCalled = false; IEnumerable <int> result = source.Where(value => { wasSelectorCalled = true; return(true); }); Assert.Equal(0, result.Count()); Assert.False(wasSelectorCalled); }
public void ManyEmptyConcats() { IEnumerable <int> source = Enumerable.Empty <int>(); for (int i = 0; i < 256; i++) { source = source.Concat(Enumerable.Empty <int>()); } Assert.Equal(0, source.Count()); Assert.Equal(Enumerable.Empty <int>(), source); }
public void ManyNonEmptyConcats() { IEnumerable <int> source = Enumerable.Empty <int>(); for (int i = 0; i < 256; i++) { source = source.Concat(Enumerable.Repeat(i, 1)); } Assert.Equal(256, source.Count()); Assert.Equal(Enumerable.Range(0, 256), source); }
private void VerifyEquals(IEnumerable <int> expected, IEnumerable <int> actual) { Assert.Equal(expected, actual); Assert.Equal(expected, actual.ToArray()); Assert.Equal(expected, actual.ToList()); Assert.Equal(expected, actual.Select(i => i).ToArray()); Assert.Equal(expected, actual.Where(i => true).ToArray()); Assert.Equal(expected, actual.OrderBy(i => i)); Assert.Equal(expected, Enumerable.Empty <int>().Concat(actual)); Assert.Equal(expected.Count(), actual.Count()); }
public void IndexOverflow() { var selected = new FastInfiniteEnumerator <int>().SelectMany((e, i) => Enumerable.Empty <int>()); using (var en = selected.GetEnumerator()) Assert.Throws <OverflowException>(() => { while (en.MoveNext()) { } }); }
public static IEnumerable <object[]> TestData() { yield return(new object[] { new int[0], 0, new int[] { 0 } }); yield return(new object[] { new int[] { 3 }, 0, new int[] { 3 } }); yield return(new object[] { new int[] { 3, -1, 0, 10, 15 }, 0, new int[] { 3, -1, 0, 10, 15 } }); yield return(new object[] { new int[0], -10, new int[] { -10 } }); yield return(new object[] { new int[] { 3 }, 9, new int[] { 3 } }); yield return(new object[] { new int[] { 3, -1, 0, 10, 15 }, 9, new int[] { 3, -1, 0, 10, 15 } }); yield return(new object[] { Enumerable.Empty <int>(), 0, new int[] { 0 } }); }
public void Select_SourceIsEmptyEnumerable_ReturnedCollectionHasNoElements() { IEnumerable <int> source = Enumerable.Empty <int>(); bool wasSelectorCalled = false; IEnumerable <int> result = source.Select(i => { wasSelectorCalled = true; return(i + 1); }); bool hadItems = false; foreach (var item in result) { hadItems = true; } Assert.False(hadItems); Assert.False(wasSelectorCalled); }
//[Theory] //[MemberData(nameof(Max_NullableLong_TestData))] //public void Max_NullableLong(IEnumerable<long?> source, long? expected) //{ // Assert.Equal(expected, source.Max()); // Assert.Equal(expected, source.Max(x => x)); //} //[Fact] //public void Max_NullableLong_NullSource_ThrowsArgumentNullException() //{ // Assert.Throws<ArgumentNullException>("source", () => ((IEnumerable<long?>)null).Max()); // Assert.Throws<ArgumentNullException>("source", () => ((IEnumerable<long?>)null).Max(i => i)); //} public static IEnumerable <object[]> Max_NullableFloat_TestData() { yield return(new object[] { Enumerable.Repeat((float?)42, 1), 42f }); yield return(new object[] { Enumerable.Range(1, 10).Select(i => (float?)i).ToArray(), 10f }); yield return(new object[] { new float?[] { null, -100, -15, -50, -10 }, -10f }); yield return(new object[] { new float?[] { null, -16, 0, 50, 100, 1000 }, 1000f }); yield return(new object[] { new float?[] { null, -16, 0, 50, 100, 1000 }.Concat(Enumerable.Repeat((float?)float.MaxValue, 1)), float.MaxValue }); yield return(new object[] { Enumerable.Repeat(default(float?), 100), null }); yield return(new object[] { Enumerable.Empty <float?>(), null }); yield return(new object[] { Enumerable.Repeat((float?)float.MinValue, 1), float.MinValue }); yield return(new object[] { Enumerable.Repeat(default(float?), 5), null }); yield return(new object[] { new float?[] { 14.50f, null, float.NaN, 10.98f, null, 7.5f, 8.6f }, 14.50f }); yield return(new object[] { new float?[] { null, null, null, null, null, 0f }, 0f }); yield return(new object[] { new float?[] { -6.4f, null, null, -0.5f, -9.4f, -0.5f, -10.9f, -0.5f }, -0.5f }); yield return(new object[] { new float?[] { float.NaN, 6.8f, 9.4f, 10f, 0, null, -5.6f }, 10f }); yield return(new object[] { new float?[] { 6.8f, 9.4f, 10f, 0, null, -5.6f, float.NaN }, 10f }); yield return(new object[] { new float?[] { float.NaN, float.NegativeInfinity }, float.NegativeInfinity }); yield return(new object[] { new float?[] { float.NegativeInfinity, float.NaN }, float.NegativeInfinity }); yield return(new object[] { Enumerable.Repeat((float?)float.NaN, 3), float.NaN }); yield return(new object[] { new float?[] { float.NaN, null, null, null }, float.NaN }); yield return(new object[] { new float?[] { null, null, null, float.NaN }, float.NaN }); yield return(new object[] { new float?[] { null, float.NaN, null }, float.NaN }); }
//[Theory] //[MemberData(nameof(Max_NullableFloat_TestData))] //public void Max_NullableFloat(IEnumerable<float?> source, float? expected) //{ // Assert.Equal(expected, source.Max()); // Assert.Equal(expected, source.Max(x => x)); //} //[Fact] //public void Max_NullableFloat_NullSource_ThrowsArgumentNullException() //{ // Assert.Throws<ArgumentNullException>("source", () => ((IEnumerable<float?>)null).Max()); // Assert.Throws<ArgumentNullException>("source", () => ((IEnumerable<float?>)null).Max(i => i)); //} public static IEnumerable <object[]> Max_NullableDouble_TestData() { yield return(new object[] { Enumerable.Repeat((double?)42, 1), 42.0 }); yield return(new object[] { Enumerable.Range(1, 10).Select(i => (double?)i).ToArray(), 10.0 }); yield return(new object[] { new double?[] { null, -100, -15, -50, -10 }, -10.0 }); yield return(new object[] { new double?[] { null, -16, 0, 50, 100, 1000 }, 1000.0 }); yield return(new object[] { new double?[] { null, -16, 0, 50, 100, 1000 }.Concat(Enumerable.Repeat((double?)double.MaxValue, 1)), double.MaxValue }); yield return(new object[] { Enumerable.Repeat(default(double?), 100), null }); yield return(new object[] { Enumerable.Empty <double?>(), null }); yield return(new object[] { Enumerable.Repeat((double?)double.MinValue, 1), double.MinValue }); yield return(new object[] { Enumerable.Repeat(default(double?), 5), null }); yield return(new object[] { new double?[] { 14.50, null, double.NaN, 10.98, null, 7.5, 8.6 }, 14.50 }); yield return(new object[] { new double?[] { null, null, null, null, null, 0 }, 0.0 }); yield return(new object[] { new double?[] { -6.4, null, null, -0.5, -9.4, -0.5, -10.9, -0.5 }, -0.5 }); yield return(new object[] { new double?[] { double.NaN, 6.8, 9.4, 10.5, 0, null, -5.6 }, 10.5 }); yield return(new object[] { new double?[] { 6.8, 9.4, 10.8, 0, null, -5.6, double.NaN }, 10.8 }); yield return(new object[] { new double?[] { double.NaN, double.NegativeInfinity }, double.NegativeInfinity }); yield return(new object[] { new double?[] { double.NegativeInfinity, double.NaN }, double.NegativeInfinity }); yield return(new object[] { Enumerable.Repeat((double?)double.NaN, 3), double.NaN }); yield return(new object[] { new double?[] { double.NaN, null, null, null }, double.NaN }); yield return(new object[] { new double?[] { null, null, null, double.NaN }, double.NaN }); yield return(new object[] { new double?[] { null, double.NaN, null }, double.NaN }); }
//[Theory] //[MemberData(nameof(Max_DateTime_TestData))] //public void Max_DateTime(IEnumerable<DateTime> source, DateTime expected) //{ // Assert.Equal(expected, source.Max()); // Assert.Equal(expected, source.Max(x => x)); //} //[Fact] //public void Max_DateTime_NullSource_ThrowsArgumentNullException() //{ // Assert.Throws<ArgumentNullException>("source", () => ((IEnumerable<DateTime>)null).Max()); // Assert.Throws<ArgumentNullException>("source", () => ((IEnumerable<DateTime>)null).Max(i => i)); //} //[Fact] //public void Max_DateTime_EmptySource_ThrowsInvalidOperationException() //{ // Assert.Throws<InvalidOperationException>(() => Enumerable.Empty<DateTime>().Max()); // Assert.Throws<InvalidOperationException>(() => Enumerable.Empty<DateTime>().Max(i => i)); //} public static IEnumerable <object[]> Max_String_TestData() { yield return(new object[] { Enumerable.Range(1, 10).Select(i => i.ToString()).ToArray(), "9" }); yield return(new object[] { new string[] { "Alice", "Bob", "Charlie", "Eve", "Mallory", "Victor", "Trent" }, "Victor" }); yield return(new object[] { new string[] { null, "Charlie", null, "Victor", "Trent", null, "Eve", "Alice", "Mallory", "Bob" }, "Victor" }); yield return(new object[] { Enumerable.Empty <string>(), null }); yield return(new object[] { Enumerable.Repeat("Hello", 1), "Hello" }); yield return(new object[] { Enumerable.Repeat("hi", 5), "hi" }); yield return(new object[] { new string[] { "zzz", "aaa", "abcd", "bark", "temp", "cat" }, "zzz" }); yield return(new object[] { new string[] { null, null, null, null, "aAa" }, "aAa" }); yield return(new object[] { new string[] { "ooo", "ccc", "ccc", "ooo", "ooo", "nnn" }, "ooo" }); yield return(new object[] { Enumerable.Repeat(default(string), 5), null }); }
public void IndexCausingFirstToBeSelectedWithResultSelector() { StringWithIntArray[] source = { new StringWithIntArray { name = "Prakash", total = new int?[]{ 1, 2, 3, 4 } }, new StringWithIntArray { name = "Bob", total = new int?[]{ 5, 6 } }, new StringWithIntArray { name = "Chris", total = new int?[0] }, new StringWithIntArray { name = null, total = new int?[]{ 8, 9 } }, new StringWithIntArray { name = "Prakash", total = new int?[]{ -10, 100 } } }; string[] expected = { "1", "2", "3", "4" }; Assert.Equal(expected, source.SelectMany((e, i) => i == 0 ? e.total : Enumerable.Empty <int?>(), (e, f) => f.ToString())); }
// [Fact] public void NullIndexedCollectionSelector() { Func <StringWithIntArray, int, IEnumerable <int?> > collectionSelector = null; Assert.Throws <ArgumentNullException>("collectionSelector", () => Enumerable.Empty <StringWithIntArray>().SelectMany(collectionSelector, (e, f) => f.ToString())); }
public void IndexCausingLastToBeSelected() { StringWithIntArray[] source = { new StringWithIntArray { name = "Prakash", total = new int?[]{ 1, 2, 3, 4 } }, new StringWithIntArray { name = "Bob", total = new int?[]{ 5, 6 } }, new StringWithIntArray { name = "Chris", total = new int?[0] }, new StringWithIntArray { name = null, total = new int?[]{ 8, 9 } }, new StringWithIntArray { name = "Robert", total = new int?[]{ -10, 100 } } }; Assert.Equal(source.Last().total, source.SelectMany((e, i) => i == 4 ? e.total : Enumerable.Empty <int?>())); }
// [Fact] public void NullResultSelectorIndexedSelector() { Func <StringWithIntArray, int?, string> resultSelector = null; Assert.Throws <ArgumentNullException>("resultSelector", () => Enumerable.Empty <StringWithIntArray>().SelectMany((e, i) => e.total, resultSelector)); }
public void EmptySourceResultSelectorIndexedSelector() { Assert.Empty(Enumerable.Empty <StringWithIntArray>().SelectMany((e, i) => e.total, (e, f) => f.ToString())); }
public void LastOrDefaultEmptyOrderedEnumerable() { Assert.Equal(0, Enumerable.Empty <int>().OrderBy(i => i).LastOrDefault()); Assert.Equal(0, Enumerable.Empty <int>().OrderBy(i => i).LastOrDefault(x => true)); }
public void LastEmptyOrderedEnumerable() { Assert.Throws <InvalidOperationException>(() => Enumerable.Empty <int>().OrderBy(i => i).Last()); Assert.Throws <InvalidOperationException>(() => Enumerable.Empty <int>().OrderBy(i => i).Last(x => true)); }
public void SourceEmptyIndexed() { Assert.Empty(Enumerable.Empty <int>().TakeWhile((e, i) => true)); }
public void SourceEmpty() { Assert.Empty(Enumerable.Empty <int>().TakeWhile(e => true)); }