Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix LINQ Aggregate/CountBy tests for Native AOT #105357

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 18 additions & 30 deletions src/libraries/System.Linq/tests/AggregateByTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,65 +93,50 @@ public void AggregateBy_SourceThrowsOnCurrent()
Assert.Throws<InvalidOperationException>(() => enumerator.MoveNext());
}

[Theory]
[ActiveIssue("https://github.com/dotnet/runtime/issues/92387", typeof(PlatformDetection), nameof(PlatformDetection.IsNativeAot))]
[MemberData(nameof(AggregateBy_TestData))]
public static void AggregateBy_HasExpectedOutput<TSource, TKey, TAccumulate>(IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, TAccumulate> seedSelector, Func<TAccumulate, TSource, TAccumulate> func, IEqualityComparer<TKey>? comparer, IEnumerable<KeyValuePair<TKey, TAccumulate>> expected)
{
Assert.Equal(expected, source.AggregateBy(keySelector, seedSelector, func, comparer));
}

[Theory]
[ActiveIssue("https://github.com/dotnet/runtime/issues/92387", typeof(PlatformDetection), nameof(PlatformDetection.IsNativeAot))]
[MemberData(nameof(AggregateBy_TestData))]
public static void AggregateBy_RunOnce_HasExpectedOutput<TSource, TKey, TAccumulate>(IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, TAccumulate> seedSelector, Func<TAccumulate, TSource, TAccumulate> func, IEqualityComparer<TKey>? comparer, IEnumerable<KeyValuePair<TKey, TAccumulate>> expected)
{
Assert.Equal(expected, source.RunOnce().AggregateBy(keySelector, seedSelector, func, comparer));
}

public static IEnumerable<object[]> AggregateBy_TestData()
[Fact]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our usual approach when fixing failures like this is to list missing specializations in the corresponding default.rd.xml, which should hopefully no longer be necessary once generic theories are supported in Native AOT I believe @agocke has been working on this space.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find this cleaner and less susceptible to bugs introduced by refactoring or new additions causing later failures due to not updating the rd.xml.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, but the point having a theory is being able to test individual inputs independently without success of one input dependent on the success of the previous inputs.

Copy link
Member Author

@stephentoub stephentoub Jul 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a benefit of theories, yes, and we give up on that benefit frequently for things off the golden path, such as more inputs than the system can handle, places where theories aren't conducive to the kinds of work being done, or places like here where without significant overhaul to how they're implemented they're not compatible with aot. I'd much rather give up on that minor benefit than have this fail in complicated ways when we add one more input.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's possible the work Andy is doing won't even support generic theories. It's not exactly trivial to figure out the type information necessary to source generate the actual generic parameters to call the theory with at compile time.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it's possible for it to work in principle, although it might require constraining what generic theories are allowed or adopting a new DSL. In any case, we can adopt this approach for now to unblock testing.

public void AggregateBy_HasExpectedOutput()
{
yield return WrapArgs(
Validate(
source: Enumerable.Empty<int>(),
keySelector: x => x,
seedSelector: x => 0,
func: (x, y) => x + y,
comparer: null,
expected: Enumerable.Empty<KeyValuePair<int,int>>());

yield return WrapArgs(
Validate(
source: Enumerable.Range(0, 10),
keySelector: x => x,
seedSelector: x => 0,
func: (x, y) => x + y,
comparer: null,
expected: Enumerable.Range(0, 10).Select(x => new KeyValuePair<int, int>(x, x)));

yield return WrapArgs(
Validate(
source: Enumerable.Range(5, 10),
keySelector: x => true,
seedSelector: x => 0,
func: (x, y) => x + y,
comparer: null,
expected: Enumerable.Repeat(true, 1).Select(x => new KeyValuePair<bool, int>(x, 95)));

yield return WrapArgs(
Validate(
source: Enumerable.Range(0, 20),
keySelector: x => x % 5,
seedSelector: x => 0,
func: (x, y) => x + y,
comparer: null,
expected: Enumerable.Range(0, 5).Select(x => new KeyValuePair<int, int>(x, 30 + 4 * x)));

yield return WrapArgs(
Validate(
source: Enumerable.Repeat(5, 20),
keySelector: x => x,
seedSelector: x => 0,
func: (x, y) => x + y,
comparer: null,
expected: Enumerable.Repeat(5, 1).Select(x => new KeyValuePair<int, int>(x, 100)));

yield return WrapArgs(
Validate(
source: new string[] { "Bob", "bob", "tim", "Bob", "Tim" },
keySelector: x => x,
seedSelector: x => string.Empty,
Expand All @@ -165,7 +150,7 @@ public static IEnumerable<object[]> AggregateBy_TestData()
new("Tim", "Tim"),
]);

yield return WrapArgs(
Validate(
source: new string[] { "Bob", "bob", "tim", "Bob", "Tim" },
keySelector: x => x,
seedSelector: x => string.Empty,
Expand All @@ -177,7 +162,7 @@ public static IEnumerable<object[]> AggregateBy_TestData()
new("tim", "timTim")
]);

yield return WrapArgs(
Validate(
source: new (string Name, int Age)[] { ("Tom", 20), ("Dick", 30), ("Harry", 40) },
keySelector: x => x.Age,
seedSelector: x => $"I am {x} and my name is ",
Expand All @@ -190,7 +175,7 @@ public static IEnumerable<object[]> AggregateBy_TestData()
new(40, "I am 40 and my name is Harry")
]);

yield return WrapArgs(
Validate(
source: new (string Name, int Age)[] { ("Tom", 20), ("Dick", 20), ("Harry", 40) },
keySelector: x => x.Age,
seedSelector: x => $"I am {x} and my name is",
Expand All @@ -202,15 +187,15 @@ public static IEnumerable<object[]> AggregateBy_TestData()
new(40, "I am 40 and my name is maybe Harry")
]);

yield return WrapArgs(
Validate(
source: new (string Name, int Age)[] { ("Bob", 20), ("bob", 20), ("Harry", 20) },
keySelector: x => x.Name,
seedSelector: x => 0,
func: (x, y) => x + y.Age,
comparer: null,
expected: new string[] { "Bob", "bob", "Harry" }.Select(x => new KeyValuePair<string, int>(x, 20)));

yield return WrapArgs(
Validate(
source: new (string Name, int Age)[] { ("Bob", 20), ("bob", 30), ("Harry", 40) },
keySelector: x => x.Name,
seedSelector: x => 0,
Expand All @@ -222,8 +207,11 @@ public static IEnumerable<object[]> AggregateBy_TestData()
new("Harry", 40)
]);

object[] WrapArgs<TSource, TKey, TAccumulate>(IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, TAccumulate> seedSelector, Func<TAccumulate, TSource, TAccumulate> func, IEqualityComparer<TKey>? comparer, IEnumerable<KeyValuePair<TKey, TAccumulate>> expected)
=> new object[] { source, keySelector, seedSelector, func, comparer, expected };
static void Validate<TSource, TKey, TAccumulate>(IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, TAccumulate> seedSelector, Func<TAccumulate, TSource, TAccumulate> func, IEqualityComparer<TKey>? comparer, IEnumerable<KeyValuePair<TKey, TAccumulate>> expected)
{
Assert.Equal(expected, source.AggregateBy(keySelector, seedSelector, func, comparer));
Assert.Equal(expected, source.RunOnce().AggregateBy(keySelector, seedSelector, func, comparer));
}
}

[Fact]
Expand Down
48 changes: 18 additions & 30 deletions src/libraries/System.Linq/tests/CountByTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,55 +57,40 @@ public void CountBy_SourceThrowsOnCurrent()
Assert.Throws<InvalidOperationException>(() => enumerator.MoveNext());
}

[Theory]
[ActiveIssue("https://github.com/dotnet/runtime/issues/92387", typeof(PlatformDetection), nameof(PlatformDetection.IsNativeAot))]
[MemberData(nameof(CountBy_TestData))]
public static void CountBy_HasExpectedOutput<TSource, TKey>(IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer, IEnumerable<KeyValuePair<TKey, int>> expected)
{
Assert.Equal(expected, source.CountBy(keySelector, comparer));
}

[Theory]
[ActiveIssue("https://github.com/dotnet/runtime/issues/92387", typeof(PlatformDetection), nameof(PlatformDetection.IsNativeAot))]
[MemberData(nameof(CountBy_TestData))]
public static void CountBy_RunOnce_HasExpectedOutput<TSource, TKey>(IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer, IEnumerable<KeyValuePair<TKey, int>> expected)
{
Assert.Equal(expected, source.RunOnce().CountBy(keySelector, comparer));
}

public static IEnumerable<object[]> CountBy_TestData()
[Fact]
public void CountBy_HasExpectedOutput()
{
yield return WrapArgs(
Validate(
source: Enumerable.Empty<int>(),
keySelector: x => x,
comparer: null,
expected: Enumerable.Empty<KeyValuePair<int,int>>());

yield return WrapArgs(
Validate(
source: Enumerable.Range(0, 10),
keySelector: x => x,
comparer: null,
expected: Enumerable.Range(0, 10).Select(x => new KeyValuePair<int, int>(x, 1)));

yield return WrapArgs(
Validate(
source: Enumerable.Range(5, 10),
keySelector: x => true,
comparer: null,
expected: Enumerable.Repeat(true, 1).Select(x => new KeyValuePair<bool, int>(x, 10)));

yield return WrapArgs(
Validate(
source: Enumerable.Range(0, 20),
keySelector: x => x % 5,
comparer: null,
expected: Enumerable.Range(0, 5).Select(x => new KeyValuePair<int, int>(x, 4)));

yield return WrapArgs(
Validate(
source: Enumerable.Repeat(5, 20),
keySelector: x => x,
comparer: null,
expected: Enumerable.Repeat(5, 1).Select(x => new KeyValuePair<int, int>(x, 20)));

yield return WrapArgs(
Validate(
source: new string[] { "Bob", "bob", "tim", "Bob", "Tim" },
keySelector: x => x,
null,
Expand All @@ -117,7 +102,7 @@ public static IEnumerable<object[]> CountBy_TestData()
new("Tim", 1)
]);

yield return WrapArgs(
Validate(
source: new string[] { "Bob", "bob", "tim", "Bob", "Tim" },
keySelector: x => x,
StringComparer.OrdinalIgnoreCase,
Expand All @@ -127,13 +112,13 @@ public static IEnumerable<object[]> CountBy_TestData()
new("tim", 2)
]);

yield return WrapArgs(
Validate(
source: new (string Name, int Age)[] { ("Tom", 20), ("Dick", 30), ("Harry", 40) },
keySelector: x => x.Age,
comparer: null,
expected: new int[] { 20, 30, 40 }.Select(x => new KeyValuePair<int, int>(x, 1)));

yield return WrapArgs(
Validate(
source: new (string Name, int Age)[] { ("Tom", 20), ("Dick", 20), ("Harry", 40) },
keySelector: x => x.Age,
comparer: null,
Expand All @@ -143,13 +128,13 @@ public static IEnumerable<object[]> CountBy_TestData()
new(40, 1)
]);

yield return WrapArgs(
Validate(
source: new (string Name, int Age)[] { ("Bob", 20), ("bob", 30), ("Harry", 40) },
keySelector: x => x.Name,
comparer: null,
expected: new string[] { "Bob", "bob", "Harry" }.Select(x => new KeyValuePair<string, int>(x, 1)));

yield return WrapArgs(
Validate(
source: new (string Name, int Age)[] { ("Bob", 20), ("bob", 30), ("Harry", 40) },
keySelector: x => x.Name,
comparer: StringComparer.OrdinalIgnoreCase,
Expand All @@ -159,8 +144,11 @@ public static IEnumerable<object[]> CountBy_TestData()
new("Harry", 1)
]);

object[] WrapArgs<TSource, TKey>(IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer, IEnumerable<KeyValuePair<TKey, int>> expected)
=> new object[] { source, keySelector, comparer, expected };
static void Validate<TSource, TKey>(IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer, IEnumerable<KeyValuePair<TKey, int>> expected)
{
Assert.Equal(expected, source.CountBy(keySelector, comparer));
Assert.Equal(expected, source.RunOnce().CountBy(keySelector, comparer));
}
}
}
}
Loading