From ce3531e0c5731e545d3052a3e1dcceda43e01095 Mon Sep 17 00:00:00 2001 From: Jon Hanna Date: Sun, 10 Jan 2016 05:09:12 +0000 Subject: [PATCH] Have IList and non-IList versions of all Enumerable.Skip tests. #4551 introduced optimised versions of Skip for IList sources. Have all tests for Skip test both this and the previous path. --- src/System.Linq/tests/SkipTests.cs | 132 ++++++++++++++++++++++++++--- 1 file changed, 119 insertions(+), 13 deletions(-) diff --git a/src/System.Linq/tests/SkipTests.cs b/src/System.Linq/tests/SkipTests.cs index 2a83df1de643..a49570be9015 100644 --- a/src/System.Linq/tests/SkipTests.cs +++ b/src/System.Linq/tests/SkipTests.cs @@ -9,28 +9,58 @@ namespace System.Linq.Tests { public class SkipTests : EnumerableTests { + private static IEnumerable GuaranteeNotIList(IEnumerable source) + { + foreach (T element in source) + yield return element; + } + [Fact] public void SkipSome() { - Assert.Equal(Enumerable.Range(10, 10), Enumerable.Range(0, 20).Skip(10)); + Assert.Equal(Enumerable.Range(10, 10), NumberRangeGuaranteedNotCollectionType(0, 20).Skip(10)); + } + + [Fact] + public void SkipSomeIList() + { + Assert.Equal(Enumerable.Range(10, 10), NumberRangeGuaranteedNotCollectionType(0, 20).ToList().Skip(10)); } [Fact] public void SkipNone() { - Assert.Equal(Enumerable.Range(0, 20), Enumerable.Range(0, 20).Skip(0)); + Assert.Equal(Enumerable.Range(0, 20), NumberRangeGuaranteedNotCollectionType(0, 20).Skip(0)); + } + + [Fact] + public void SkipNoneIList() + { + Assert.Equal(Enumerable.Range(0, 20), NumberRangeGuaranteedNotCollectionType(0, 20).ToList().Skip(0)); } [Fact] public void SkipExcessive() { - Assert.Equal(Enumerable.Empty(), Enumerable.Range(0, 20).Skip(42)); + Assert.Equal(Enumerable.Empty(), NumberRangeGuaranteedNotCollectionType(0, 20).Skip(42)); + } + + [Fact] + public void SkipExcessiveIList() + { + Assert.Equal(Enumerable.Empty(), NumberRangeGuaranteedNotCollectionType(0, 20).ToList().Skip(42)); } [Fact] public void SkipAllExactly() { - Assert.False(Enumerable.Range(0, 20).Skip(20).Any()); + Assert.False(NumberRangeGuaranteedNotCollectionType(0, 20).Skip(20).Any()); + } + + [Fact] + public void SkipAllExactlyIList() + { + Assert.False(NumberRangeGuaranteedNotCollectionType(0, 20).Skip(20).ToList().Any()); } [Fact] @@ -38,27 +68,60 @@ public void SkipThrowsOnNull() { Assert.Throws("source", () => ((IEnumerable)null).Skip(3)); } - + + [Fact] + public void SkipThrowsOnNullIList() + { + Assert.Throws("source", () => ((List)null).Skip(3)); + Assert.Throws("source", () => ((IList)null).Skip(3)); + } + [Fact] public void SkipOnEmpty() { - Assert.Equal(Enumerable.Empty(), Enumerable.Empty().Skip(0)); - Assert.Equal(Enumerable.Empty(), Enumerable.Empty().Skip(-1)); - Assert.Equal(Enumerable.Empty(), Enumerable.Empty().Skip(1)); + Assert.Equal(Enumerable.Empty(), GuaranteeNotIList(Enumerable.Empty()).Skip(0)); + Assert.Equal(Enumerable.Empty(), GuaranteeNotIList(Enumerable.Empty()).Skip(-1)); + Assert.Equal(Enumerable.Empty(), GuaranteeNotIList(Enumerable.Empty()).Skip(1)); + } + + [Fact] + public void SkipOnEmptyIList() + { + // Enumerable.Empty does return an IList, but not guaranteed as such + // by the spec. + Assert.Equal(Enumerable.Empty(), Enumerable.Empty().ToList().Skip(0)); + Assert.Equal(Enumerable.Empty(), Enumerable.Empty().ToList().Skip(-1)); + Assert.Equal(Enumerable.Empty(), Enumerable.Empty().ToList().Skip(1)); } [Fact] public void SkipNegative() { - Assert.Equal(Enumerable.Range(0, 20), Enumerable.Range(0, 20).Skip(-42)); + Assert.Equal(Enumerable.Range(0, 20), NumberRangeGuaranteedNotCollectionType(0, 20).Skip(-42)); + } + + [Fact] + public void SkipNegativeIList() + { + Assert.Equal(Enumerable.Range(0, 20), NumberRangeGuaranteedNotCollectionType(0, 20).ToList().Skip(-42)); } [Fact] public void SameResultsRepeatCallsIntQuery() { - var q = from x in new[] { 9999, 0, 888, -1, 66, -777, 1, 2, -12345 } + var q = GuaranteeNotIList(from x in new[] { 9999, 0, 888, -1, 66, -777, 1, 2, -12345 } where x > Int32.MinValue - select x; + select x); + + Assert.Equal(q.Skip(0), q.Skip(0)); + } + + [Fact] + public void SameResultsRepeatCallsIntQueryIList() + { + var q = (from x in new[] { 9999, 0, 888, -1, 66, -777, 1, 2, -12345 } + where x > Int32.MinValue + select x).ToList(); Assert.Equal(q.Skip(0), q.Skip(0)); } @@ -66,9 +129,19 @@ where x > Int32.MinValue [Fact] public void SameResultsRepeatCallsStringQuery() { - var q = from x in new[] { "!@#$%^", "C", "AAA", "", "Calling Twice", "SoS", String.Empty } + var q = GuaranteeNotIList(from x in new[] { "!@#$%^", "C", "AAA", "", "Calling Twice", "SoS", String.Empty } + where !String.IsNullOrEmpty(x) + select x); + + Assert.Equal(q.Skip(0), q.Skip(0)); + } + + [Fact] + public void SameResultsRepeatCallsStringQueryIList() + { + var q = (from x in new[] { "!@#$%^", "C", "AAA", "", "Calling Twice", "SoS", String.Empty } where !String.IsNullOrEmpty(x) - select x; + select x).ToList(); Assert.Equal(q.Skip(0), q.Skip(0)); } @@ -82,6 +155,14 @@ public void SkipOne() Assert.Equal(expected, source.Skip(1)); } + [Fact] + public void SkipOneNotIList() + { + int?[] source = { 3, 100, 4, null, 10 }; + int?[] expected = { 100, 4, null, 10 }; + + Assert.Equal(expected, GuaranteeNotIList(source).Skip(1)); + } [Fact] public void SkipAllButOne() @@ -92,6 +173,15 @@ public void SkipAllButOne() Assert.Equal(expected, source.Skip(source.Length - 1)); } + [Fact] + public void SkipAllButOneNotIList() + { + int?[] source = { 3, 100, null, 4, 10 }; + int?[] expected = { 10 }; + + Assert.Equal(expected, GuaranteeNotIList(source.Skip(source.Length - 1))); + } + [Fact] public void SkipOneMoreThanAll() { @@ -99,6 +189,13 @@ public void SkipOneMoreThanAll() Assert.Empty(source.Skip(source.Length + 1)); } + [Fact] + public void SkipOneMoreThanAllNotIList() + { + int[] source = { 3, 100, 4, 10 }; + Assert.Empty(GuaranteeNotIList(source).Skip(source.Length + 1)); + } + [Fact] public void ForcedToEnumeratorDoesntEnumerate() { @@ -107,5 +204,14 @@ public void ForcedToEnumeratorDoesntEnumerate() var en = iterator as IEnumerator; Assert.False(en != null && en.MoveNext()); } + + [Fact] + public void ForcedToEnumeratorDoesntEnumerateIList() + { + var iterator = (new[] { 0, 1, 2 }).Skip(2); + // Don't insist on this behaviour, but check its correct if it happens + var en = iterator as IEnumerator; + Assert.False(en != null && en.MoveNext()); + } } }