Skip to content

Commit

Permalink
Augment System.Linq sink tests to validate more collection types (#10…
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub authored Jul 22, 2024
1 parent 7cac50f commit 810d646
Show file tree
Hide file tree
Showing 15 changed files with 482 additions and 359 deletions.
81 changes: 65 additions & 16 deletions src/libraries/System.Linq/tests/AggregateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ public void EmptySource()
{
int[] source = { };

Assert.Throws<InvalidOperationException>(() => source.RunOnce().Aggregate((x, y) => x + y));
Assert.All(CreateSources(source), source =>
{
Assert.Throws<InvalidOperationException>(() => source.Aggregate((x, y) => x + y));
Assert.Throws<InvalidOperationException>(() => source.RunOnce().Aggregate((x, y) => x + y));
});
}

[Fact]
Expand All @@ -43,7 +47,10 @@ public void SingleElement()
int[] source = { 5 };
int expected = 5;

Assert.Equal(expected, source.Aggregate((x, y) => x + y));
Assert.All(CreateSources(source), source =>
{
Assert.Equal(expected, source.Aggregate((x, y) => x + y));
});
}

[Fact]
Expand All @@ -52,7 +59,10 @@ public void SingleElementRunOnce()
int[] source = { 5 };
int expected = 5;

Assert.Equal(expected, source.RunOnce().Aggregate((x, y) => x + y));
Assert.All(CreateSources(source), source =>
{
Assert.Equal(expected, source.RunOnce().Aggregate((x, y) => x + y));
});
}

[Fact]
Expand All @@ -61,7 +71,10 @@ public void TwoElements()
int[] source = { 5, 6 };
int expected = 11;

Assert.Equal(expected, source.Aggregate((x, y) => x + y));
Assert.All(CreateSources(source), source =>
{
Assert.Equal(expected, source.Aggregate((x, y) => x + y));
});
}

[Fact]
Expand All @@ -70,7 +83,10 @@ public void MultipleElements()
int[] source = { 5, 6, 0, -4 };
int expected = 7;

Assert.Equal(expected, source.Aggregate((x, y) => x + y));
Assert.All(CreateSources(source), source =>
{
Assert.Equal(expected, source.Aggregate((x, y) => x + y));
});
}

[Fact]
Expand All @@ -79,7 +95,10 @@ public void MultipleElementsRunOnce()
int[] source = { 5, 6, 0, -4 };
int expected = 7;

Assert.Equal(expected, source.RunOnce().Aggregate((x, y) => x + y));
Assert.All(CreateSources(source), source =>
{
Assert.Equal(expected, source.RunOnce().Aggregate((x, y) => x + y));
});
}

[Fact]
Expand All @@ -89,7 +108,10 @@ public void EmptySourceAndSeed()
long seed = 2;
long expected = 2;

Assert.Equal(expected, source.Aggregate(seed, (x, y) => x * y));
Assert.All(CreateSources(source), source =>
{
Assert.Equal(expected, source.Aggregate(seed, (x, y) => x * y));
});
}

[Fact]
Expand All @@ -99,7 +121,10 @@ public void SingleElementAndSeed()
long seed = 2;
long expected = 10;

Assert.Equal(expected, source.Aggregate(seed, (x, y) => x * y));
Assert.All(CreateSources(source), source =>
{
Assert.Equal(expected, source.Aggregate(seed, (x, y) => x * y));
});
}

[Fact]
Expand All @@ -109,7 +134,10 @@ public void TwoElementsAndSeed()
long seed = 2;
long expected = 60;

Assert.Equal(expected, source.Aggregate(seed, (x, y) => x * y));
Assert.All(CreateSources(source), source =>
{
Assert.Equal(expected, source.Aggregate(seed, (x, y) => x * y));
});
}

[Fact]
Expand All @@ -119,7 +147,10 @@ public void MultipleElementsAndSeed()
long seed = 2;
long expected = -480;

Assert.Equal(expected, source.Aggregate(seed, (x, y) => x * y));
Assert.All(CreateSources(source), source =>
{
Assert.Equal(expected, source.Aggregate(seed, (x, y) => x * y));
});
}

[Fact]
Expand All @@ -129,7 +160,10 @@ public void MultipleElementsAndSeedRunOnce()
long seed = 2;
long expected = -480;

Assert.Equal(expected, source.RunOnce().Aggregate(seed, (x, y) => x * y));
Assert.All(CreateSources(source), source =>
{
Assert.Equal(expected, source.RunOnce().Aggregate(seed, (x, y) => x * y));
});
}

[Fact]
Expand All @@ -139,7 +173,10 @@ public void NoElementsSeedResultSeletor()
long seed = 2;
double expected = 7;

Assert.Equal(expected, source.Aggregate(seed, (x, y) => x * y, x => x + 5.0));
Assert.All(CreateSources(source), source =>
{
Assert.Equal(expected, source.Aggregate(seed, (x, y) => x * y, x => x + 5.0));
});
}

[Fact]
Expand All @@ -149,7 +186,10 @@ public void SingleElementSeedResultSelector()
long seed = 2;
long expected = 15;

Assert.Equal(expected, source.Aggregate(seed, (x, y) => x * y, x => x + 5.0));
Assert.All(CreateSources(source), source =>
{
Assert.Equal(expected, source.Aggregate(seed, (x, y) => x * y, x => x + 5.0));
});
}

[Fact]
Expand All @@ -159,7 +199,10 @@ public void TwoElementsSeedResultSelector()
long seed = 2;
long expected = 65;

Assert.Equal(expected, source.Aggregate(seed, (x, y) => x * y, x => x + 5.0));
Assert.All(CreateSources(source), source =>
{
Assert.Equal(expected, source.Aggregate(seed, (x, y) => x * y, x => x + 5.0));
});
}

[Fact]
Expand All @@ -169,7 +212,10 @@ public void MultipleElementsSeedResultSelector()
long seed = 2;
long expected = -475;

Assert.Equal(expected, source.Aggregate(seed, (x, y) => x * y, x => x + 5.0));
Assert.All(CreateSources(source), source =>
{
Assert.Equal(expected, source.Aggregate(seed, (x, y) => x * y, x => x + 5.0));
});
}

[Fact]
Expand All @@ -179,7 +225,10 @@ public void MultipleElementsSeedResultSelectorRunOnce()
long seed = 2;
long expected = -475;

Assert.Equal(expected, source.RunOnce().Aggregate(seed, (x, y) => x * y, x => x + 5.0));
Assert.All(CreateSources(source), source =>
{
Assert.Equal(expected, source.RunOnce().Aggregate(seed, (x, y) => x * y, x => x + 5.0));
});
}

[Fact]
Expand Down
10 changes: 8 additions & 2 deletions src/libraries/System.Linq/tests/AllTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,19 @@ public static IEnumerable<object[]> All_TestData()
[MemberData(nameof(All_TestData))]
public void All(IEnumerable<int> source, Func<int, bool> predicate, bool expected)
{
Assert.Equal(expected, source.All(predicate));
Assert.All(CreateSources(source), source =>
{
Assert.Equal(expected, source.All(predicate));
});
}

[Theory, MemberData(nameof(All_TestData))]
public void AllRunOnce(IEnumerable<int> source, Func<int, bool> predicate, bool expected)
{
Assert.Equal(expected, source.RunOnce().All(predicate));
Assert.All(CreateSources(source), source =>
{
Assert.Equal(expected, source.RunOnce().All(predicate));
});
}

[Fact]
Expand Down
52 changes: 25 additions & 27 deletions src/libraries/System.Linq/tests/AnyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,7 @@ public static IEnumerable<object[]> TestData()
foreach (int count in new[] { 0, 1, 2 })
{
bool expected = count > 0;

var arr = new int[count];
var collectionTypes = new IEnumerable<int>[]
{
arr,
new List<int>(arr),
new TestCollection<int>(arr),
new TestNonGenericCollection<int>(arr),
NumberRangeGuaranteedNotCollectionType(0, count),
};

foreach (IEnumerable<int> source in collectionTypes)
foreach (IEnumerable<int> source in CreateSources(new int[count]))
{
yield return new object[] { source, expected };
yield return new object[] { source.Select(i => i), expected };
Expand Down Expand Up @@ -102,34 +91,43 @@ public static IEnumerable<object[]> TestDataWithPredicate()
[MemberData(nameof(TestDataWithPredicate))]
public void Any_Predicate(IEnumerable<int> source, Func<int, bool> predicate, bool expected)
{
if (predicate is null)
Assert.All(CreateSources(source), source =>
{
Assert.Equal(expected, source.Any());
}
else
{
Assert.Equal(expected, source.Any(predicate));
}
if (predicate is null)
{
Assert.Equal(expected, source.Any());
}
else
{
Assert.Equal(expected, source.Any(predicate));
}
});
}

[Theory, MemberData(nameof(TestDataWithPredicate))]
public void AnyRunOnce(IEnumerable<int> source, Func<int, bool> predicate, bool expected)
{
if (predicate is null)
Assert.All(CreateSources(source), source =>
{
Assert.Equal(expected, source.RunOnce().Any());
}
else
{
Assert.Equal(expected, source.RunOnce().Any(predicate));
}
if (predicate is null)
{
Assert.Equal(expected, source.RunOnce().Any());
}
else
{
Assert.Equal(expected, source.RunOnce().Any(predicate));
}
});
}

[Fact]
public void NullObjectsInArray_Included()
{
int?[] source = { null, null, null, null };
Assert.True(source.Any());
Assert.All(CreateSources(source), source =>
{
Assert.True(source.Any());
});
}

[Fact]
Expand Down
Loading

0 comments on commit 810d646

Please sign in to comment.