diff --git a/tests/System.Linq.Tests/Stubs/System.Linq/EnumerableTests.cs b/tests/System.Linq.Tests/Stubs/System.Linq/EnumerableTests.cs index cbcdb8be..2f3b8688 100644 --- a/tests/System.Linq.Tests/Stubs/System.Linq/EnumerableTests.cs +++ b/tests/System.Linq.Tests/Stubs/System.Linq/EnumerableTests.cs @@ -247,6 +247,7 @@ protected static IEnumerable FlipIsCollection(IEnumerable source) { return source is ICollection ? ForceNotCollection(source) : new List(source); } + protected static T[] Repeat(Func factory, int count) { T[] results = new T[count]; @@ -320,27 +321,86 @@ protected static IEnumerable> CreateSources(IEnumerable sou } } - // TODO: latest `dotnet/runtime` version supports more test patterns. - protected static List, IEnumerable>> IdentityTransforms() + protected static IEnumerable, IEnumerable>> IdentityTransforms() { - // All of these transforms should take an enumerable and produce - // another enumerable with the same contents. - return new List, IEnumerable>> + // Various collection types all representing the same source. + List, IEnumerable>> sources = + [ + e => e, // original + e => e.ToArray(), // T[] + e => e.ToList(), // List + e => new ReadOnlyCollection(e.ToArray()), // IList that's not List/T[] + e => new TestCollection(e.ToArray()), // ICollection that's not IList + e => new TestReadOnlyCollection(e.ToArray()), // IReadOnlyCollection that's not ICollection + e => ForceNotCollection(e), // IEnumerable with no other interfaces + ]; + if (typeof(T) == typeof(char)) { - e => e, - e => e.ToArray(), - e => e.ToList(), - e => e.ToList().Take(int.MaxValue), + sources.Add(e => (IEnumerable)(object)string.Concat((IEnumerable)(object)e)); // string + } + + // Various transforms that all yield the same elements as the source. + List, IEnumerable>> transforms = + [ + // Concat + e => e.Concat(ForceNotCollection([])), + e => ForceNotCollection([]).Concat(e), + + // Following transforms cause test failure on System.Linq tests with .NET 9 +#if NET10_0_OR_GREATER + // Append + e => + { + T[] values = e.ToArray(); + return values.Length == 0 ? [] : values[0..^1].Append(values[^1]); + }, + + // Prepend + e => + { + T[] values = e.ToArray(); + return values.Length == 0 ? [] : values[1..].Prepend(values[0]); + }, + + // Reverse + e => e.Reverse().Reverse(), +#endif + + // Select e => e.Select(i => i), - e => e.Select(i => i).Take(int.MaxValue), - e => e.Select(i => i).Where(i => true), + + // SelectMany + e => e.SelectMany(i => [i]), + + // Take + e => e.Take(int.MaxValue), + e => e.TakeLast(int.MaxValue), + e => e.TakeWhile(i => true), + + // Skip + e => e.SkipWhile(i => false), + + // Where e => e.Where(i => true), - e => e.Concat(Array.Empty()), - e => e.Concat(ForceNotCollection(Array.Empty())), - e => ForceNotCollection(e), - e => ForceNotCollection(e).Skip(0), - e => new ReadOnlyCollection(e.ToArray()), - }; + ]; + + foreach (Func, IEnumerable> source in sources) + { + // Yield the source itself. + yield return source; + + foreach (Func, IEnumerable> transform in transforms) + { + // Yield a single transform on the source + yield return e => transform(source(e)); + + foreach (Func, IEnumerable> transform2 in transforms) + { + // Yield a second transform on the first transform on the source. + yield return e => transform2(transform(source(e))); + } + } + } } protected sealed class DelegateIterator : IEnumerable, IEnumerator diff --git a/tests/System.Linq.Tests/Stubs/TestData/MinMaxTestData.cs b/tests/System.Linq.Tests/Stubs/TestData/MinMaxTestData.cs index 6e6691bd..42d5d338 100644 --- a/tests/System.Linq.Tests/Stubs/TestData/MinMaxTestData.cs +++ b/tests/System.Linq.Tests/Stubs/TestData/MinMaxTestData.cs @@ -3,10 +3,14 @@ using System.Linq.Tests; -// Original code: https://github.com/dotnet/runtime/blob/v9.0.3/src/libraries/Common/tests/System/Linq/SkipTakeData.cs +// Original code: +// https://github.com/dotnet/runtime/blob/v10.0.0-preview.2.25163.2/src/libraries/System.Linq/tests/MinTests.cs +// https://github.com/dotnet/runtime/blob/v10.0.0-preview.2.25163.2/src/libraries/System.Linq/tests/MaxTests.cs namespace System.Linq; +// This class is used to share test data between System.Linq/ZLinq tests. +// Note: It need to use `Shuffler.Shuffle` to show indivisual test cases on Test Explorer. public class MinMaxTestData { public static IEnumerable Min_AllTypes_TestData() @@ -14,47 +18,24 @@ public static IEnumerable Min_AllTypes_TestData() for (int length = 2; length < 65; length++) { yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (byte)i)), (byte)length }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (byte)i).ToArray()), (byte)length }; // Unit Tests does +T.One so we should generate data up to one value below sbyte.MaxValue, otherwise the type overflows if ((length + length) < sbyte.MaxValue) { yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (sbyte)i)), (sbyte)length }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (sbyte)i).ToArray()), (sbyte)length }; } yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (ushort)i)), (ushort)length }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (ushort)i).ToArray()), (ushort)length }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (short)i)), (short)length }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (short)i).ToArray()), (short)length }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (uint)i)), (uint)length }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (uint)i).ToArray()), (uint)length }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (int)i)), (int)length }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (int)i).ToArray()), (int)length }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (ulong)i)), (ulong)length }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (ulong)i).ToArray()), (ulong)length }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (long)i)), (long)length }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (long)i).ToArray()), (long)length }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (float)i)), (float)length }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (float)i).ToArray()), (float)length }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (double)i)), (double)length }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (double)i).ToArray()), (double)length }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (decimal)i)), (decimal)length }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (decimal)i).ToArray()), (decimal)length }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (nuint)i)), (nuint)length }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (nuint)i).ToArray()), (nuint)length }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (nint)i)), (nint)length }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (nint)i).ToArray()), (nint)length }; } } @@ -63,56 +44,27 @@ public static IEnumerable Max_AllTypes_TestData() for (int length = 2; length < 65; length++) { yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (byte)i)), (byte)(length + length - 1) }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (byte)i).ToArray()), (byte)(length + length - 1) }; // Unit Tests does +T.One so we should generate data up to one value below sbyte.MaxValue if ((length + length) < sbyte.MaxValue) { yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (sbyte)i)), (sbyte)(length + length - 1) }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (sbyte)i).ToArray()), (sbyte)(length + length - 1) }; } yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (ushort)i)), (ushort)(length + length - 1) }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (ushort)i).ToArray()), (ushort)(length + length - 1) }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (short)i)), (short)(length + length - 1) }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (short)i).ToArray()), (short)(length + length - 1) }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (char)i)), (char)(length + length - 1) }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (char)i).ToArray()), (char)(length + length - 1) }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (uint)i)), (uint)(length + length - 1) }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (uint)i).ToArray()), (uint)(length + length - 1) }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (int)i)), (int)(length + length - 1) }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (int)i).ToArray()), (int)(length + length - 1) }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (ulong)i)), (ulong)(length + length - 1) }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (ulong)i).ToArray()), (ulong)(length + length - 1) }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (long)i)), (long)(length + length - 1) }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (long)i).ToArray()), (long)(length + length - 1) }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (float)i)), (float)(length + length - 1) }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (float)i).ToArray()), (float)(length + length - 1) }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (double)i)), (double)(length + length - 1) }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (double)i).ToArray()), (double)(length + length - 1) }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (decimal)i)), (decimal)(length + length - 1) }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (decimal)i).ToArray()), (decimal)(length + length - 1) }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (nuint)i)), (nuint)(length + length - 1) }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (nuint)i).ToArray()), (nuint)(length + length - 1) }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (nint)i)), (nint)(length + length - 1) }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (nint)i).ToArray()), (nint)(length + length - 1) }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (Int128)i)), (Int128)(length + length - 1) }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (Int128)i).ToArray()), (Int128)(length + length - 1) }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (UInt128)i)), (UInt128)(length + length - 1) }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (UInt128)i).ToArray()), (UInt128)(length + length - 1) }; } } } diff --git a/tests/System.Linq.Tests/Stubs/TestData/SkipTakeData.cs b/tests/System.Linq.Tests/Stubs/TestData/SkipTakeData.cs index 67f992cf..0c78c3ca 100644 --- a/tests/System.Linq.Tests/Stubs/TestData/SkipTakeData.cs +++ b/tests/System.Linq.Tests/Stubs/TestData/SkipTakeData.cs @@ -3,34 +3,27 @@ // Original code: https://github.com/dotnet/runtime/blob/v9.0.3/src/libraries/Common/tests/System/Linq/SkipTakeData.cs -namespace System.Linq; +namespace System.Linq.Tests; public class SkipTakeData { - public static IEnumerable EnumerableData() + public static TheoryData EnumerableData() { - IEnumerable sourceCounts = new[] { 0, 1, 2, 3, 5, 8, 13, 55, 100, 250 }; + IEnumerable sourceCounts = [0, 1, 2, 3, 5, 8, 13, 55, 100, 250]; - IEnumerable counts = new[] { 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 100, 250, 500, int.MaxValue }; + IEnumerable counts = [1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 100, 250, 500, int.MaxValue]; counts = counts.Concat(counts.Select(c => -c)).Append(0).Append(int.MinValue); - return from sourceCount in sourceCounts - let source = Enumerable.Range(0, sourceCount) - from count in counts - select new object[] { source, count }; - } + var items = from sourceCount in sourceCounts + let source = Enumerable.Range(0, sourceCount) + from count in counts + select new { source, count }; - public static IEnumerable EvaluationBehaviorData() - { - return Enumerable.Range(-1, 15).Select(count => new object[] { count }); + return new(items.Select(x => (x.source.ToArray(), x.count))); } - public static IEnumerable QueryableData() + public static TheoryData EvaluationBehaviorData() { - return EnumerableData().Select(array => - { - var enumerable = (IEnumerable)array[0]; - return new[] { enumerable.AsQueryable(), array[1] }; - }); + return new(Enumerable.Range(-1, 15).Select(count => count).ToArray()); } } diff --git a/tests/System.Linq.Tests/Stubs/TestUtilities/PlatformDetection.cs b/tests/System.Linq.Tests/Stubs/TestUtilities/PlatformDetection.cs index e20fac2c..a863f8e8 100644 --- a/tests/System.Linq.Tests/Stubs/TestUtilities/PlatformDetection.cs +++ b/tests/System.Linq.Tests/Stubs/TestUtilities/PlatformDetection.cs @@ -43,6 +43,8 @@ private static bool ComputeIsLinqSizeOptimized() #endif } #else + public static bool IsLinqSpeedOptimized => IsSpeedOptimized; + public static bool IsSpeedOptimized => !IsSizeOptimized; public static bool IsSizeOptimized => IsBrowser || IsWasi || IsAndroid || IsAppleMobile; #endif diff --git a/tests/System.Linq.Tests/Stubs/xUnit/ConditionalUtils.cs b/tests/System.Linq.Tests/Stubs/xUnit/ConditionalUtils.cs index 6e72d858..ac347d16 100644 --- a/tests/System.Linq.Tests/Stubs/xUnit/ConditionalUtils.cs +++ b/tests/System.Linq.Tests/Stubs/xUnit/ConditionalUtils.cs @@ -23,11 +23,8 @@ public static bool IsEnable(Type type, string key) case "IsDebuggerTypeProxyAttributeSupported" when PlatformDetection.IsDebuggerTypeProxyAttributeSupported: case "IsNotBuiltWithAggressiveTrimming" when PlatformDetection.IsNotBuiltWithAggressiveTrimming: -#if NET10_0_OR_GREATER case "IsLinqSpeedOptimized" when PlatformDetection.IsLinqSpeedOptimized: -#else case "IsSpeedOptimized" when PlatformDetection.IsSpeedOptimized: -#endif return true; default: return false; diff --git a/tests/System.Linq.Tests/Tests/System.Linq/AggregateByTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/AggregateByTests.cs index b974ec6f..bd610467 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/AggregateByTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/AggregateByTests.cs @@ -11,10 +11,10 @@ public class AggregateByTests : EnumerableTests [Fact] public void Empty() { - Assert.All(IdentityTransforms(), transform => + Assert.All(CreateSources([]), source => { - Assert.Equal(Enumerable.Empty>(), transform(Enumerable.Empty()).AggregateBy(i => i, i => i, (a, i) => a + i)); - Assert.Equal(Enumerable.Empty>(), transform(Enumerable.Empty()).AggregateBy(i => i, 0, (a, i) => a + i)); + Assert.Equal([], source.AggregateBy(i => i, i => i, (a, i) => a + i)); + Assert.Equal([], source.AggregateBy(i => i, 0, (a, i) => a + i)); }); } @@ -68,7 +68,7 @@ public void AggregateBy_SourceThrowsOnGetEnumerator() { IEnumerable source = new ThrowsOnGetEnumerator(); - var enumerator = source.AggregateBy(x => x, 0, (x, y) => x + y).GetEnumerator(); + using var enumerator = source.AggregateBy(x => x, 0, (x, y) => x + y).GetEnumerator(); Assert.Throws(() => enumerator.MoveNext()); } @@ -78,7 +78,7 @@ public void AggregateBy_SourceThrowsOnMoveNext() { IEnumerable source = new ThrowsOnMoveNext(); - var enumerator = source.AggregateBy(x => x, 0, (x, y) => x + y).GetEnumerator(); + using var enumerator = source.AggregateBy(x => x, 0, (x, y) => x + y).GetEnumerator(); Assert.Throws(() => enumerator.MoveNext()); } @@ -88,7 +88,7 @@ public void AggregateBy_SourceThrowsOnCurrent() { IEnumerable source = new ThrowsOnCurrentEnumerator(); - var enumerator = source.AggregateBy(x => x, 0, (x, y) => x + y).GetEnumerator(); + using var enumerator = source.AggregateBy(x => x, 0, (x, y) => x + y).GetEnumerator(); Assert.Throws(() => enumerator.MoveNext()); } @@ -102,7 +102,7 @@ public void AggregateBy_HasExpectedOutput() seedSelector: x => 0, func: (x, y) => x + y, comparer: null, - expected: Enumerable.Empty>()); + expected: []); Validate( source: Enumerable.Range(0, 10), @@ -137,7 +137,7 @@ public void AggregateBy_HasExpectedOutput() expected: Enumerable.Repeat(5, 1).Select(x => new KeyValuePair(x, 100))); Validate( - source: new string[] { "Bob", "bob", "tim", "Bob", "Tim" }, + source: ["Bob", "bob", "tim", "Bob", "Tim"], keySelector: x => x, seedSelector: x => string.Empty, func: (x, y) => x + y, @@ -151,7 +151,7 @@ public void AggregateBy_HasExpectedOutput() ]); Validate( - source: new string[] { "Bob", "bob", "tim", "Bob", "Tim" }, + source: ["Bob", "bob", "tim", "Bob", "Tim"], keySelector: x => x, seedSelector: x => string.Empty, func: (x, y) => x + y, @@ -224,10 +224,10 @@ static IEnumerable>> GroupBy(IEn (group, element) => { group.Add(element); return group; }); IEnumerable>> oddsEvens = GroupBy( - new int[] { 1, 2, 3, 4 }, + [1, 2, 3, 4], i => i % 2 == 0); - var e = oddsEvens.GetEnumerator(); + using var e = oddsEvens.GetEnumerator(); Assert.True(e.MoveNext()); KeyValuePair> oddsItem = e.Current; @@ -258,10 +258,10 @@ static IEnumerable> LongCountBy(IEnumera (count, _) => ++count); IEnumerable> oddsEvens = LongCountBy( - new int[] { 1, 2, 3, 4 }, + [1, 2, 3, 4], i => i % 2 == 0); - var e = oddsEvens.GetEnumerator(); + using var e = oddsEvens.GetEnumerator(); Assert.True(e.MoveNext()); KeyValuePair oddsItem = e.Current; diff --git a/tests/System.Linq.Tests/Tests/System.Linq/AggregateTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/AggregateTests.cs index 2f48fc8e..7fdf8e19 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/AggregateTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/AggregateTests.cs @@ -32,7 +32,7 @@ public void SameResultsRepeatCallsStringQuery() [Fact] public void EmptySource() { - int[] source = { }; + int[] source = []; Assert.All(CreateSources(source), source => { @@ -44,7 +44,7 @@ public void EmptySource() [Fact] public void SingleElement() { - int[] source = { 5 }; + int[] source = [5]; int expected = 5; Assert.All(CreateSources(source), source => @@ -56,7 +56,7 @@ public void SingleElement() [Fact] public void SingleElementRunOnce() { - int[] source = { 5 }; + int[] source = [5]; int expected = 5; Assert.All(CreateSources(source), source => @@ -68,7 +68,7 @@ public void SingleElementRunOnce() [Fact] public void TwoElements() { - int[] source = { 5, 6 }; + int[] source = [5, 6]; int expected = 11; Assert.All(CreateSources(source), source => @@ -80,7 +80,7 @@ public void TwoElements() [Fact] public void MultipleElements() { - int[] source = { 5, 6, 0, -4 }; + int[] source = [5, 6, 0, -4]; int expected = 7; Assert.All(CreateSources(source), source => @@ -92,7 +92,7 @@ public void MultipleElements() [Fact] public void MultipleElementsRunOnce() { - int[] source = { 5, 6, 0, -4 }; + int[] source = [5, 6, 0, -4]; int expected = 7; Assert.All(CreateSources(source), source => @@ -104,7 +104,7 @@ public void MultipleElementsRunOnce() [Fact] public void EmptySourceAndSeed() { - int[] source = { }; + int[] source = []; long seed = 2; long expected = 2; @@ -117,7 +117,7 @@ public void EmptySourceAndSeed() [Fact] public void SingleElementAndSeed() { - int[] source = { 5 }; + int[] source = [5]; long seed = 2; long expected = 10; @@ -130,7 +130,7 @@ public void SingleElementAndSeed() [Fact] public void TwoElementsAndSeed() { - int[] source = { 5, 6 }; + int[] source = [5, 6]; long seed = 2; long expected = 60; @@ -143,7 +143,7 @@ public void TwoElementsAndSeed() [Fact] public void MultipleElementsAndSeed() { - int[] source = { 5, 6, 2, -4 }; + int[] source = [5, 6, 2, -4]; long seed = 2; long expected = -480; @@ -156,7 +156,7 @@ public void MultipleElementsAndSeed() [Fact] public void MultipleElementsAndSeedRunOnce() { - int[] source = { 5, 6, 2, -4 }; + int[] source = [5, 6, 2, -4]; long seed = 2; long expected = -480; @@ -169,7 +169,7 @@ public void MultipleElementsAndSeedRunOnce() [Fact] public void NoElementsSeedResultSeletor() { - int[] source = { }; + int[] source = []; long seed = 2; double expected = 7; @@ -182,7 +182,7 @@ public void NoElementsSeedResultSeletor() [Fact] public void SingleElementSeedResultSelector() { - int[] source = { 5 }; + int[] source = [5]; long seed = 2; long expected = 15; @@ -195,7 +195,7 @@ public void SingleElementSeedResultSelector() [Fact] public void TwoElementsSeedResultSelector() { - int[] source = { 5, 6 }; + int[] source = [5, 6]; long seed = 2; long expected = 65; @@ -208,7 +208,7 @@ public void TwoElementsSeedResultSelector() [Fact] public void MultipleElementsSeedResultSelector() { - int[] source = { 5, 6, 2, -4 }; + int[] source = [5, 6, 2, -4]; long seed = 2; long expected = -475; @@ -221,7 +221,7 @@ public void MultipleElementsSeedResultSelector() [Fact] public void MultipleElementsSeedResultSelectorRunOnce() { - int[] source = { 5, 6, 2, -4 }; + int[] source = [5, 6, 2, -4]; long seed = 2; long expected = -475; diff --git a/tests/System.Linq.Tests/Tests/System.Linq/AllTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/AllTests.cs index e599726c..7b454361 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/AllTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/AllTests.cs @@ -32,20 +32,20 @@ public void SameResultsRepeatCallsStringQuery() public static IEnumerable All_TestData() { Func isEvenFunc = IsEven; - yield return new object[] { new int[0], isEvenFunc, true }; - yield return new object[] { new int[] { 3 }, isEvenFunc, false }; - yield return new object[] { new int[] { 4 }, isEvenFunc, true }; - yield return new object[] { new int[] { 3 }, isEvenFunc, false }; - yield return new object[] { new int[] { 4, 8, 3, 5, 10, 20, 12 }, isEvenFunc, false }; - yield return new object[] { new int[] { 4, 2, 10, 12, 8, 6, 3 }, isEvenFunc, false }; - yield return new object[] { new int[] { 4, 2, 10, 12, 8, 6, 14 }, isEvenFunc, true }; + yield return [new int[0], isEvenFunc, true]; + yield return [new int[] { 3 }, isEvenFunc, false]; + yield return [new int[] { 4 }, isEvenFunc, true]; + yield return [new int[] { 3 }, isEvenFunc, false]; + yield return [new int[] { 4, 8, 3, 5, 10, 20, 12 }, isEvenFunc, false]; + yield return [new int[] { 4, 2, 10, 12, 8, 6, 3 }, isEvenFunc, false]; + yield return [new int[] { 4, 2, 10, 12, 8, 6, 14 }, isEvenFunc, true]; int[] range = Enumerable.Range(1, 10).ToArray(); - yield return new object[] { range, (Func)(i => i > 0), true }; + yield return [range, (Func)(i => i > 0), true]; for (int j = 1; j <= 10; j++) { int k = j; // Local copy for iterator - yield return new object[] { range, (Func)(i => i > k), false }; + yield return [range, (Func)(i => i > k), false]; } } diff --git a/tests/System.Linq.Tests/Tests/System.Linq/AnyTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/AnyTests.cs index 93633de9..b76c7adc 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/AnyTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/AnyTests.cs @@ -36,11 +36,11 @@ public static IEnumerable TestData() bool expected = count > 0; foreach (IEnumerable source in CreateSources(new int[count])) { - yield return new object[] { source, expected }; - yield return new object[] { source.Select(i => i), expected }; - yield return new object[] { source.Where(i => true), expected }; + yield return [source, expected]; + yield return [source.Select(i => i), expected]; + yield return [source.Where(i => true), expected]; - yield return new object[] { source.Where(i => false), false }; + yield return [source.Where(i => false), false]; } } } @@ -54,9 +54,9 @@ public void Any(IEnumerable source, bool expected) public static IEnumerable TestDataForGroupBy() { - yield return new object[] { Array.Empty().GroupBy(num => num), false }; - yield return new object[] { new int[2] { 1, 2 }.GroupBy(num => num), true }; - yield return new object[] { new int[5] { 1, 2, 1, 3, 2 }.GroupBy(n => n, (k, v) => v), true }; + yield return [Array.Empty().GroupBy(num => num), false]; + yield return [new int[2] { 1, 2 }.GroupBy(num => num), true]; + yield return [new int[5] { 1, 2, 1, 3, 2 }.GroupBy(n => n, (k, v) => v), true]; } [Theory, MemberData(nameof(TestDataForGroupBy))] @@ -68,22 +68,22 @@ public void Any_GroupBy(IEnumerable values, bool expectedValue) public static IEnumerable TestDataWithPredicate() { - yield return new object[] { new int[0], null, false }; - yield return new object[] { new int[] { 3 }, null, true }; + yield return [new int[0], null, false]; + yield return [new int[] { 3 }, null, true]; Func isEvenFunc = IsEven; - yield return new object[] { new int[0], isEvenFunc, false }; - yield return new object[] { new int[] { 4 }, isEvenFunc, true }; - yield return new object[] { new int[] { 5 }, isEvenFunc, false }; - yield return new object[] { new int[] { 5, 9, 3, 7, 4 }, isEvenFunc, true }; - yield return new object[] { new int[] { 5, 8, 9, 3, 7, 11 }, isEvenFunc, true }; + yield return [new int[0], isEvenFunc, false]; + yield return [new int[] { 4 }, isEvenFunc, true]; + yield return [new int[] { 5 }, isEvenFunc, false]; + yield return [new int[] { 5, 9, 3, 7, 4 }, isEvenFunc, true]; + yield return [new int[] { 5, 8, 9, 3, 7, 11 }, isEvenFunc, true]; int[] range = Enumerable.Range(1, 10).ToArray(); - yield return new object[] { range, (Func)(i => i > 10), false }; + yield return [range, (Func)(i => i > 10), false]; for (int j = 0; j <= 9; j++) { int k = j; // Local copy for iterator - yield return new object[] { range, (Func)(i => i > k), true }; + yield return [range, (Func)(i => i > k), true]; } } @@ -123,7 +123,7 @@ public void AnyRunOnce(IEnumerable source, Func predicate, bool [Fact] public void NullObjectsInArray_Included() { - int?[] source = { null, null, null, null }; + int?[] source = [null, null, null, null]; Assert.All(CreateSources(source), source => { Assert.True(source.Any()); diff --git a/tests/System.Linq.Tests/Tests/System.Linq/AppendPrependTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/AppendPrependTests.cs index 4763bf74..3a7a2e5e 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/AppendPrependTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/AppendPrependTests.cs @@ -16,7 +16,7 @@ public void SameResultsRepeatCallsIntQueryAppend() select x1; Assert.Equal(q1.Append(42), q1.Append(42)); - Assert.Equal(q1.Append(42), q1.Concat(new int?[] { 42 })); + Assert.Equal(q1.Append(42), q1.Concat([42])); } [Fact] @@ -36,7 +36,7 @@ public void SameResultsRepeatCallsStringQueryAppend() select x1; Assert.Equal(q1.Append("hi"), q1.Append("hi")); - Assert.Equal(q1.Append("hi"), q1.Concat(new string[] { "hi" })); + Assert.Equal(q1.Append("hi"), q1.Concat(["hi"])); } [Fact] @@ -61,7 +61,7 @@ public void RepeatIteration() [Fact] public void EmptyAppend() { - int[] first = { }; + int[] first = []; Assert.All(CreateSources(first), first => { Assert.Single(first.Append(42), 42); @@ -71,7 +71,7 @@ public void EmptyAppend() [Fact] public void EmptyPrepend() { - string[] first = { }; + string[] first = []; Assert.All(CreateSources(first), first => { Assert.Single(first.Prepend("aa"), "aa"); @@ -150,12 +150,12 @@ public void AppendCombinations() var app1ba = app0b.Append(9); var app1bb = app0b.Append(10); - Assert.Equal(new[] { 0, 1, 2, 3, 4, 5 }, app0a); - Assert.Equal(new[] { 0, 1, 2, 3, 4, 6 }, app0b); - Assert.Equal(new[] { 0, 1, 2, 3, 4, 5, 7 }, app1aa); - Assert.Equal(new[] { 0, 1, 2, 3, 4, 5, 8 }, app1ab); - Assert.Equal(new[] { 0, 1, 2, 3, 4, 6, 9 }, app1ba); - Assert.Equal(new[] { 0, 1, 2, 3, 4, 6, 10 }, app1bb); + Assert.Equal([0, 1, 2, 3, 4, 5], app0a); + Assert.Equal([0, 1, 2, 3, 4, 6], app0b); + Assert.Equal([0, 1, 2, 3, 4, 5, 7], app1aa); + Assert.Equal([0, 1, 2, 3, 4, 5, 8], app1ab); + Assert.Equal([0, 1, 2, 3, 4, 6, 9], app1ba); + Assert.Equal([0, 1, 2, 3, 4, 6, 10], app1bb); } [Fact] @@ -169,12 +169,12 @@ public void PrependCombinations() var pre1ba = pre0b.Prepend(9); var pre1bb = pre0b.Prepend(10); - Assert.Equal(new[] { 5, 0, 1, 2, 3 }, pre0a); - Assert.Equal(new[] { 6, 0, 1, 2, 3 }, pre0b); - Assert.Equal(new[] { 7, 5, 0, 1, 2, 3 }, pre1aa); - Assert.Equal(new[] { 8, 5, 0, 1, 2, 3 }, pre1ab); - Assert.Equal(new[] { 9, 6, 0, 1, 2, 3 }, pre1ba); - Assert.Equal(new[] { 10, 6, 0, 1, 2, 3 }, pre1bb); + Assert.Equal([5, 0, 1, 2, 3], pre0a); + Assert.Equal([6, 0, 1, 2, 3], pre0b); + Assert.Equal([7, 5, 0, 1, 2, 3], pre1aa); + Assert.Equal([8, 5, 0, 1, 2, 3], pre1ab); + Assert.Equal([9, 6, 0, 1, 2, 3], pre1ba); + Assert.Equal([10, 6, 0, 1, 2, 3], pre1bb); } [Fact] diff --git a/tests/System.Linq.Tests/Tests/System.Linq/AverageTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/AverageTests.cs index 541a5134..d613af72 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/AverageTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/AverageTests.cs @@ -29,14 +29,14 @@ public void SameResultsRepeatCallsNullableLongQuery() public static IEnumerable NullableFloat_TestData() { - yield return new object[] { new float?[0], null }; - yield return new object[] { new float?[] { float.MinValue }, float.MinValue }; - yield return new object[] { new float?[] { 0f, 0f, 0f, 0f, 0f }, 0f }; + yield return [new float?[0], null]; + yield return [new float?[] { float.MinValue }, float.MinValue]; + yield return [new float?[] { 0f, 0f, 0f, 0f, 0f }, 0f]; - yield return new object[] { new float?[] { 5.5f, 0, null, null, null, 15.5f, 40.5f, null, null, -23.5f }, 7.6f }; + yield return [new float?[] { 5.5f, 0, null, null, null, 15.5f, 40.5f, null, null, -23.5f }, 7.6f]; - yield return new object[] { new float?[] { null, null, null, null, 45f }, 45f }; - yield return new object[] { new float?[] { null, null, null, null, null }, null }; + yield return [new float?[] { null, null, null, null, 45f }, 45f]; + yield return [new float?[] { null, null, null, null, null }, null]; } [Theory] @@ -89,8 +89,8 @@ public void Int_EmptySource_ThrowsInvalidOperationException() { Array.Empty(), new List(), - Enumerable.Empty(), - new TestEnumerable(Array.Empty()) + [], + new TestEnumerable([]) }) { Assert.Throws(() => source.Average()); @@ -114,9 +114,9 @@ public void Int_NullSelector_ThrowsArgumentNullException() public static IEnumerable Int_TestData() { - yield return new object[] { new int[] { 5 }, 5 }; - yield return new object[] { new int[] { 0, 0, 0, 0, 0 }, 0 }; - yield return new object[] { new int[] { 5, -10, 15, 40, 28 }, 15.6 }; + yield return [new int[] { 5 }, 5]; + yield return [new int[] { 0, 0, 0, 0, 0 }, 0]; + yield return [new int[] { 5, -10, 15, 40, 28 }, 15.6]; for (int i = 1; i <= 33; i++) { @@ -124,8 +124,7 @@ public static IEnumerable Int_TestData() for (int c = 1; c <= i; c++) sum += c; double expected = (double)sum / i; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(1, i)), expected }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(1, i).ToArray()), expected }; + yield return [Enumerable.Range(1, i).Shuffle(), expected]; } } @@ -166,12 +165,12 @@ public void Int_WithSelector() public static IEnumerable NullableInt_TestData() { - yield return new object[] { new int?[0], null }; - yield return new object[] { new int?[] { -5 }, -5.0 }; - yield return new object[] { new int?[] { 0, 0, 0, 0, 0 }, 0.0 }; - yield return new object[] { new int?[] { 5, -10, null, null, null, 15, 40, 28, null, null }, 15.6 }; - yield return new object[] { new int?[] { null, null, null, null, 50 }, 50.0 }; - yield return new object[] { new int?[] { null, null, null, null, null }, null }; + yield return [new int?[0], null]; + yield return [new int?[] { -5 }, -5.0]; + yield return [new int?[] { 0, 0, 0, 0, 0 }, 0.0]; + yield return [new int?[] { 5, -10, null, null, null, 15, 40, 28, null, null }, 15.6]; + yield return [new int?[] { null, null, null, null, 50 }, 50.0]; + yield return [new int?[] { null, null, null, null, null }, null]; } [Theory] @@ -220,8 +219,8 @@ public void Long_EmptySource_ThrowsInvalidOperationException() { Array.Empty(), new List(), - Enumerable.Empty(), - new TestEnumerable(Array.Empty()) + [], + new TestEnumerable([]) }) { Assert.Throws(() => source.Average()); @@ -245,9 +244,9 @@ public void Long_NullSelector_ThrowsArgumentNullException() public static IEnumerable Long_TestData() { - yield return new object[] { new long[] { long.MaxValue }, long.MaxValue }; - yield return new object[] { new long[] { 0, 0, 0, 0, 0 }, 0 }; - yield return new object[] { new long[] { 5, -10, 15, 40, 28 }, 15.6 }; + yield return [new long[] { long.MaxValue }, long.MaxValue]; + yield return [new long[] { 0, 0, 0, 0, 0 }, 0]; + yield return [new long[] { 5, -10, 15, 40, 28 }, 15.6]; for (int i = 1; i <= 33; i++) { @@ -255,8 +254,7 @@ public static IEnumerable Long_TestData() for (int c = 1; c <= i; c++) sum += c; double expected = (double)sum / i; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(1, i).Select(i => (long)i)), expected }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(1, i).Select(i => (long)i).ToArray()), expected }; + yield return [Enumerable.Range(1, i).Select(i => (long)i).Shuffle(), expected]; } } @@ -288,19 +286,19 @@ public void Long_FromSelector() [Fact] public void Long_SumTooLarge_ThrowsOverflowException() { - long[] source = new long[] { long.MaxValue, long.MaxValue }; + long[] source = [long.MaxValue, long.MaxValue]; Assert.Throws(() => source.Average()); } public static IEnumerable NullableLong_TestData() { - yield return new object[] { new long?[0], null }; - yield return new object[] { new long?[] { long.MaxValue }, (double)long.MaxValue }; - yield return new object[] { new long?[] { 0, 0, 0, 0, 0 }, 0.0 }; - yield return new object[] { new long?[] { 5, -10, null, null, null, 15, 40, 28, null, null }, 15.6 }; - yield return new object[] { new long?[] { null, null, null, null, 50 }, 50.0 }; - yield return new object[] { new long?[] { null, null, null, null, null }, null }; + yield return [new long?[0], null]; + yield return [new long?[] { long.MaxValue }, (double)long.MaxValue]; + yield return [new long?[] { 0, 0, 0, 0, 0 }, 0.0]; + yield return [new long?[] { 5, -10, null, null, null, 15, 40, 28, null, null }, 15.6]; + yield return [new long?[] { null, null, null, null, 50 }, 50.0]; + yield return [new long?[] { null, null, null, null, null }, null]; } [Theory] @@ -349,8 +347,8 @@ public void Double_EmptySource_ThrowsInvalidOperationException() { Array.Empty(), new List(), - Enumerable.Empty(), - new TestEnumerable(Array.Empty()) + [], + new TestEnumerable([]) }) { Assert.Throws(() => source.Average()); @@ -374,10 +372,10 @@ public void Double_NullSelector_ThrowsArgumentNullException() public static IEnumerable Double_TestData() { - yield return new object[] { new double[] { double.MaxValue }, double.MaxValue }; - yield return new object[] { new double[] { 0.0, 0.0, 0.0, 0.0, 0.0 }, 0 }; - yield return new object[] { new double[] { 5.5, -10, 15.5, 40.5, 28.5 }, 16 }; - yield return new object[] { new double[] { 5.58, double.NaN, 30, 4.55, 19.38 }, double.NaN }; + yield return [new double[] { double.MaxValue }, double.MaxValue]; + yield return [new double[] { 0.0, 0.0, 0.0, 0.0, 0.0 }, 0]; + yield return [new double[] { 5.5, -10, 15.5, 40.5, 28.5 }, 16]; + yield return [new double[] { 5.58, double.NaN, 30, 4.55, 19.38 }, double.NaN]; for (int i = 1; i <= 33; i++) { @@ -385,8 +383,7 @@ public static IEnumerable Double_TestData() for (int c = 1; c <= i; c++) sum += c; double expected = (double)sum / i; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(1, i).Select(i => (double)i)), expected }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(1, i).Select(i => (double)i).ToArray()), expected }; + yield return [Enumerable.Range(1, i).Select(i => (double)i).Shuffle(), expected]; } } @@ -417,13 +414,13 @@ public void Double_WithSelector() public static IEnumerable NullableDouble_TestData() { - yield return new object[] { new double?[0], null }; - yield return new object[] { new double?[] { double.MinValue }, double.MinValue }; - yield return new object[] { new double?[] { 0, 0, 0, 0, 0 }, 0.0 }; - yield return new object[] { new double?[] { 5.5, 0, null, null, null, 15.5, 40.5, null, null, -23.5 }, 7.6 }; - yield return new object[] { new double?[] { null, null, null, null, 45 }, 45.0 }; - yield return new object[] { new double?[] { -23.5, 0, double.NaN, 54.3, 0.56 }, double.NaN }; - yield return new object[] { new double?[] { null, null, null, null, null }, null }; + yield return [new double?[0], null]; + yield return [new double?[] { double.MinValue }, double.MinValue]; + yield return [new double?[] { 0, 0, 0, 0, 0 }, 0.0]; + yield return [new double?[] { 5.5, 0, null, null, null, 15.5, 40.5, null, null, -23.5 }, 7.6]; + yield return [new double?[] { null, null, null, null, 45 }, 45.0]; + yield return [new double?[] { -23.5, 0, double.NaN, 54.3, 0.56 }, double.NaN]; + yield return [new double?[] { null, null, null, null, null }, null]; } [Theory] @@ -472,8 +469,8 @@ public void Decimal_EmptySource_ThrowsInvalidOperationException() { Array.Empty(), new List(), - Enumerable.Empty(), - new TestEnumerable(Array.Empty()) + [], + new TestEnumerable([]) }) { Assert.Throws(() => source.Average()); @@ -497,9 +494,9 @@ public void Decimal_NullSelector_ThrowsArgumentNullException() public static IEnumerable Decimal_TestData() { - yield return new object[] { new decimal[] { decimal.MaxValue }, decimal.MaxValue }; - yield return new object[] { new decimal[] { 0.0m, 0.0m, 0.0m, 0.0m, 0.0m }, 0 }; - yield return new object[] { new decimal[] { 5.5m, -10m, 15.5m, 40.5m, 28.5m }, 16 }; + yield return [new decimal[] { decimal.MaxValue }, decimal.MaxValue]; + yield return [new decimal[] { 0.0m, 0.0m, 0.0m, 0.0m, 0.0m }, 0]; + yield return [new decimal[] { 5.5m, -10m, 15.5m, 40.5m, 28.5m }, 16]; for (int i = 1; i <= 33; i++) { @@ -507,8 +504,7 @@ public static IEnumerable Decimal_TestData() for (int c = 1; c <= i; c++) sum += c; decimal expected = (decimal)sum / i; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(1, i).Select(i => (decimal)i)), expected }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(1, i).Select(i => (decimal)i).ToArray()), expected }; + yield return [Enumerable.Range(1, i).Select(i => (decimal)i).Shuffle(), expected]; } } @@ -539,12 +535,12 @@ public void Decimal_WithSelector() public static IEnumerable NullableDecimal_TestData() { - yield return new object[] { new decimal?[0], null }; - yield return new object[] { new decimal?[] { decimal.MinValue }, decimal.MinValue }; - yield return new object[] { new decimal?[] { 0m, 0m, 0m, 0m, 0m }, 0m }; - yield return new object[] { new decimal?[] { 5.5m, 0, null, null, null, 15.5m, 40.5m, null, null, -23.5m }, 7.6m }; - yield return new object[] { new decimal?[] { null, null, null, null, 45m }, 45m }; - yield return new object[] { new decimal?[] { null, null, null, null, null }, null }; + yield return [new decimal?[0], null]; + yield return [new decimal?[] { decimal.MinValue }, decimal.MinValue]; + yield return [new decimal?[] { 0m, 0m, 0m, 0m, 0m }, 0m]; + yield return [new decimal?[] { 5.5m, 0, null, null, null, 15.5m, 40.5m, null, null, -23.5m }, 7.6m]; + yield return [new decimal?[] { null, null, null, null, 45m }, 45m]; + yield return [new decimal?[] { null, null, null, null, null }, null]; } [Theory] @@ -589,7 +585,7 @@ public void NullableDecimal_WithSelector() [Fact] public void NullableDecimal_SumTooLarge_ThrowsOverflowException() { - decimal?[] source = new decimal?[] { decimal.MaxValue, decimal.MaxValue }; + decimal?[] source = [decimal.MaxValue, decimal.MaxValue]; Assert.Throws(() => source.Average()); } @@ -601,8 +597,8 @@ public void Float_EmptySource_ThrowsInvalidOperationException() { Array.Empty(), new List(), - Enumerable.Empty(), - new TestEnumerable(Array.Empty()) + [], + new TestEnumerable([]) }) { Assert.Throws(() => source.Average()); @@ -626,9 +622,9 @@ public void Float_NullSelector_ThrowsArgumentNullException() public static IEnumerable Float_TestData() { - yield return new object[] { new float[] { float.MaxValue }, float.MaxValue }; - yield return new object[] { new float[] { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f }, 0f }; - yield return new object[] { new float[] { 5.5f, -10f, 15.5f, 40.5f, 28.5f }, 16f }; + yield return [new float[] { float.MaxValue }, float.MaxValue]; + yield return [new float[] { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f }, 0f]; + yield return [new float[] { 5.5f, -10f, 15.5f, 40.5f, 28.5f }, 16f]; for (int i = 1; i <= 33; i++) { @@ -636,8 +632,7 @@ public static IEnumerable Float_TestData() for (int c = 1; c <= i; c++) sum += c; float expected = (float)sum / i; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(1, i).Select(i => (float)i)), expected }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(1, i).Select(i => (float)i).ToArray()), expected }; + yield return [Enumerable.Range(1, i).Select(i => (float)i).Shuffle(), expected]; } } diff --git a/tests/System.Linq.Tests/Tests/System.Linq/CastTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/CastTests.cs index 125c2b57..90a3c04d 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/CastTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/CastTests.cs @@ -33,7 +33,7 @@ public void CastByteToUShortThrows() [Fact] public void EmptySource() { - object[] source = { }; + object[] source = []; Assert.Empty(source.Cast()); } @@ -41,8 +41,8 @@ public void EmptySource() public void NullableIntFromAppropriateObjects() { int? i = 10; - object[] source = { -4, 1, 2, 3, 9, i }; - int?[] expected = { -4, 1, 2, 3, 9, i }; + object[] source = [-4, 1, 2, 3, 9, i]; + int?[] expected = [-4, 1, 2, 3, 9, i]; Assert.Equal(expected, source.Cast()); } @@ -51,8 +51,8 @@ public void NullableIntFromAppropriateObjects() public void NullableIntFromAppropriateObjectsRunOnce() { int? i = 10; - object[] source = { -4, 1, 2, 3, 9, i }; - int?[] expected = { -4, 1, 2, 3, 9, i }; + object[] source = [-4, 1, 2, 3, 9, i]; + int?[] expected = [-4, 1, 2, 3, 9, i]; Assert.Equal(expected, source.RunOnce().Cast()); } @@ -61,7 +61,7 @@ public void NullableIntFromAppropriateObjectsRunOnce() public void LongFromNullableIntInObjectsThrows() { int? i = 10; - object[] source = { -4, 1, 2, 3, 9, i }; + object[] source = [-4, 1, 2, 3, 9, i]; IEnumerable cast = source.Cast(); Assert.Throws(() => cast.ToList()); @@ -71,7 +71,7 @@ public void LongFromNullableIntInObjectsThrows() public void LongFromNullableIntInObjectsIncludingNullThrows() { int? i = 10; - object[] source = { -4, 1, 2, 3, 9, null, i }; + object[] source = [-4, 1, 2, 3, 9, null, i]; IEnumerable cast = source.Cast(); Assert.Throws(() => cast.ToList()); @@ -81,8 +81,8 @@ public void LongFromNullableIntInObjectsIncludingNullThrows() public void NullableIntFromAppropriateObjectsIncludingNull() { int? i = 10; - object[] source = { -4, 1, 2, 3, 9, null, i }; - int?[] expected = { -4, 1, 2, 3, 9, null, i }; + object[] source = [-4, 1, 2, 3, 9, null, i]; + int?[] expected = [-4, 1, 2, 3, 9, null, i]; Assert.Equal(expected, source.Cast()); } @@ -90,8 +90,8 @@ public void NullableIntFromAppropriateObjectsIncludingNull() [Fact] public void ThrowOnUncastableItem() { - object[] source = { -4, 1, 2, 3, 9, "45" }; - int[] expectedBeginning = { -4, 1, 2, 3, 9 }; + object[] source = [-4, 1, 2, 3, 9, "45"]; + int[] expectedBeginning = [-4, 1, 2, 3, 9]; IEnumerable cast = source.Cast(); Assert.Throws(() => cast.ToList()); @@ -102,7 +102,7 @@ public void ThrowOnUncastableItem() [Fact] public void ThrowCastingIntToDouble() { - int[] source = new int[] { -4, 1, 2, 9 }; + int[] source = [-4, 1, 2, 9]; IEnumerable cast = source.Cast(); Assert.Throws(() => cast.ToList()); @@ -111,7 +111,7 @@ public void ThrowCastingIntToDouble() private static void TestCastThrow(object o) { byte? i = 10; - object[] source = { -1, 0, o, i }; + object[] source = [-1, 0, o, i]; IEnumerable cast = source.Cast(); @@ -128,8 +128,8 @@ public void ThrowOnHeterogenousSource() [Fact] public void CastToString() { - object[] source = { "Test1", "4.5", null, "Test2" }; - string[] expected = { "Test1", "4.5", null, "Test2" }; + object[] source = ["Test1", "4.5", null, "Test2"]; + string[] expected = ["Test1", "4.5", null, "Test2"]; Assert.Equal(expected, source.Cast()); } @@ -137,8 +137,8 @@ public void CastToString() [Fact] public void CastToStringRunOnce() { - object[] source = { "Test1", "4.5", null, "Test2" }; - string[] expected = { "Test1", "4.5", null, "Test2" }; + object[] source = ["Test1", "4.5", null, "Test2"]; + string[] expected = ["Test1", "4.5", null, "Test2"]; Assert.Equal(expected, source.RunOnce().Cast()); } @@ -152,7 +152,7 @@ public void ArrayConversionThrows() [Fact] public void FirstElementInvalidForCast() { - object[] source = { "Test", 3, 5, 10 }; + object[] source = ["Test", 3, 5, 10]; IEnumerable cast = source.Cast(); Assert.Throws(() => cast.ToList()); @@ -161,7 +161,7 @@ public void FirstElementInvalidForCast() [Fact] public void LastElementInvalidForCast() { - object[] source = { -5, 9, 0, 5, 9, "Test" }; + object[] source = [-5, 9, 0, 5, 9, "Test"]; IEnumerable cast = source.Cast(); Assert.Throws(() => cast.ToList()); @@ -170,8 +170,8 @@ public void LastElementInvalidForCast() [Fact] public void NullableIntFromNullsAndInts() { - object[] source = { 3, null, 5, -4, 0, null, 9 }; - int?[] expected = { 3, null, 5, -4, 0, null, 9 }; + object[] source = [3, null, 5, -4, 0, null, 9]; + int?[] expected = [3, null, 5, -4, 0, null, 9]; Assert.Equal(expected, source.Cast()); } @@ -179,7 +179,7 @@ public void NullableIntFromNullsAndInts() [Fact] public void ThrowCastingIntToLong() { - int[] source = new int[] { -4, 1, 2, 3, 9 }; + int[] source = [-4, 1, 2, 3, 9]; IEnumerable cast = source.Cast(); Assert.Throws(() => cast.ToList()); @@ -188,7 +188,7 @@ public void ThrowCastingIntToLong() [Fact] public void ThrowCastingIntToNullableLong() { - int[] source = new int[] { -4, 1, 2, 3, 9 }; + int[] source = [-4, 1, 2, 3, 9]; IEnumerable cast = source.Cast(); Assert.Throws(() => cast.ToList()); @@ -197,7 +197,7 @@ public void ThrowCastingIntToNullableLong() [Fact] public void ThrowCastingNullableIntToLong() { - int?[] source = new int?[] { -4, 1, 2, 3, 9 }; + int?[] source = [-4, 1, 2, 3, 9]; IEnumerable cast = source.Cast(); Assert.Throws(() => cast.ToList()); @@ -206,7 +206,7 @@ public void ThrowCastingNullableIntToLong() [Fact] public void ThrowCastingNullableIntToNullableLong() { - int?[] source = new int?[] { -4, 1, 2, 3, 9, null }; + int?[] source = [-4, 1, 2, 3, 9, null]; IEnumerable cast = source.Cast(); Assert.Throws(() => cast.ToList()); @@ -215,7 +215,7 @@ public void ThrowCastingNullableIntToNullableLong() [Fact] public void CastingNullToNonnullableIsNullReferenceException() { - int?[] source = new int?[] { -4, 1, null, 3 }; + int?[] source = [-4, 1, null, 3]; IEnumerable cast = source.Cast(); Assert.Throws(() => cast.ToList()); } @@ -264,71 +264,71 @@ public void CastOnMultidimensionalArraySucceeds() [Fact] public void CastCountReturnsExpectedLength() { - object[] objects = new object[] { "hello", "world" }; + object[] objects = ["hello", "world"]; Assert.Equal(2, objects.Cast().Count()); } [Fact] public void CastFirstReturnsFirstElement() { - object[] objects = new object[] { "hello", "world" }; + object[] objects = ["hello", "world"]; Assert.Equal("hello", objects.Cast().First()); } [Fact] public void CastFirstOnEmptySequenceThrows() { - object[] objects = Array.Empty(); + object[] objects = []; Assert.Throws(() => objects.Cast().First()); } [Fact] public void CastLastReturnsLastElement() { - object[] objects = new object[] { "hello", "world" }; + object[] objects = ["hello", "world"]; Assert.Equal("world", objects.Cast().Last()); } [Fact] public void CastElementAtReturnsExpectedElement() { - object[] objects = new object[] { "hello", "world" }; + object[] objects = ["hello", "world"]; Assert.Equal("world", objects.Cast().ElementAt(1)); } [Fact] public void CastElementAtOutOfRangeThrows() { - object[] objects = new object[] { "hello", "world" }; + object[] objects = ["hello", "world"]; Assert.Throws(() => objects.Cast().ElementAt(2)); } [Fact] public void CastLastOnEmptySequenceThrows() { - object[] objects = Array.Empty(); + object[] objects = []; Assert.Throws(() => objects.Cast().Last()); } [Fact] public void CastSelectProcessesEachElement() { - object[] objects = new object[] { "hello", "world!" }; - Assert.Equal(new[] { 5, 6 }, objects.Cast().Select(s => s.Length)); + object[] objects = ["hello", "world!"]; + Assert.Equal([5, 6], objects.Cast().Select(s => s.Length)); } [Fact] public void CastSkipSkipsElements() { - object[] objects = new object[] { "hello", "there", "world" }; - Assert.Equal(new[] { "world" }, objects.Cast().Skip(2)); + object[] objects = ["hello", "there", "world"]; + Assert.Equal(["world"], objects.Cast().Skip(2)); } [Fact] public void CastTakeTakesElements() { - object[] objects = new object[] { "hello", "there", "world" }; - Assert.Equal(new[] { "hello", "there" }, objects.Cast().Take(2)); + object[] objects = ["hello", "there", "world"]; + Assert.Equal(["hello", "there"], objects.Cast().Take(2)); } } } diff --git a/tests/System.Linq.Tests/Tests/System.Linq/ChunkTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/ChunkTests.cs index 97f86ed0..84a60d63 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/ChunkTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/ChunkTests.cs @@ -10,7 +10,7 @@ public class ChunkTests : EnumerableTests [Fact] public void Empty() { - Assert.Equal(Enumerable.Empty(), Enumerable.Empty().Chunk(4)); + Assert.Equal([], Enumerable.Empty().Chunk(4)); } [Fact] @@ -25,7 +25,7 @@ public void ThrowsOnNullSource() [InlineData(-1)] public void ThrowsWhenSizeIsNonPositive(int size) { - int[] source = { 1 }; + int[] source = [1]; AssertExtensions.Throws("size", () => source.Chunk(size)); } @@ -42,10 +42,8 @@ public void ChunkSourceLazily() [InlineData(new[] { 9999, 0, 888, -1, 66, -777, 1, 2, -12345 })] public void ChunkSourceRepeatCalls(int[] array) { - Assert.All(IdentityTransforms(), t => + Assert.All(CreateSources(array), source => { - IEnumerable source = t(array); - Assert.Equal(source.Chunk(3), source.Chunk(3)); }); } @@ -54,10 +52,8 @@ public void ChunkSourceRepeatCalls(int[] array) [InlineData(new[] { 9999, 0, 888, -1, 66, -777, 1, 2, -12345 })] public void ChunkSourceEvenly(int[] array) { - Assert.All(IdentityTransforms(), t => + Assert.All(CreateSources(array), source => { - IEnumerable source = t(array); - using IEnumerator chunks = source.Chunk(3).GetEnumerator(); chunks.MoveNext(); Assert.Equal(new[] { 9999, 0, 888 }, chunks.Current); @@ -73,10 +69,8 @@ public void ChunkSourceEvenly(int[] array) [InlineData(new[] { 9999, 0, 888, -1, 66, -777, 1, 2 })] public void ChunkSourceUnevenly(int[] array) { - Assert.All(IdentityTransforms(), t => + Assert.All(CreateSources(array), source => { - IEnumerable source = t(array); - using IEnumerator chunks = source.Chunk(3).GetEnumerator(); chunks.MoveNext(); Assert.Equal(new[] { 9999, 0, 888 }, chunks.Current); @@ -92,10 +86,8 @@ public void ChunkSourceUnevenly(int[] array) [InlineData(new[] { 9999, 0 })] public void ChunkSourceSmallerThanMaxSize(int[] array) { - Assert.All(IdentityTransforms(), t => + Assert.All(CreateSources(array), source => { - IEnumerable source = t(array); - using IEnumerator chunks = source.Chunk(3).GetEnumerator(); chunks.MoveNext(); Assert.Equal(new[] { 9999, 0 }, chunks.Current); @@ -107,10 +99,8 @@ public void ChunkSourceSmallerThanMaxSize(int[] array) [InlineData(new int[0])] public void EmptySourceYieldsNoChunks(int[] array) { - Assert.All(IdentityTransforms(), t => + Assert.All(CreateSources(array), source => { - IEnumerable source = t(array); - using IEnumerator chunks = source.Chunk(3).GetEnumerator(); Assert.False(chunks.MoveNext()); }); @@ -126,7 +116,7 @@ public void RemovingFromSourceBeforeIterating() IEnumerable chunks = list.Chunk(3); list.Remove(66); - Assert.Equal(new[] { new[] { 9999, 0, 888 }, new[] { -1, -777, 1 }, new[] { 2, -12345 } }, chunks); + Assert.Equal([[9999, 0, 888], [-1, -777, 1], [2, -12345]], chunks); } [Fact] @@ -139,7 +129,7 @@ public void AddingToSourceBeforeIterating() IEnumerable chunks = list.Chunk(3); list.Add(10); - Assert.Equal(new[] { new[] { 9999, 0, 888 }, new[] { -1, 66, -777 }, new[] { 1, 2, -12345 }, new[] { 10 } }, chunks); + Assert.Equal([[9999, 0, 888], [-1, 66, -777], [1, 2, -12345], [10]], chunks); } // reproduces https://github.com/dotnet/runtime/issues/67132 @@ -148,7 +138,7 @@ public void DoesNotPrematurelyAllocateHugeArray() { int[][] chunks = Enumerable.Range(0, 10).Chunk(int.MaxValue).ToArray(); - Assert.Equal(new[] { Enumerable.Range(0, 10).ToArray() }, chunks); + Assert.Equal([Enumerable.Range(0, 10).ToArray()], chunks); } } } diff --git a/tests/System.Linq.Tests/Tests/System.Linq/ConcatTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/ConcatTests.cs index c4c3bbe5..90c9a107 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/ConcatTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/ConcatTests.cs @@ -31,8 +31,8 @@ private static void SameResultsWithQueryAndRepeatCallsWorker(IEnumerable f first = from item in first select item; second = from item in second select item; - VerifyEqualsWorker(first.Concat(second), first.Concat(second)); - VerifyEqualsWorker(second.Concat(first), second.Concat(first)); + Assert.Equal(first.Concat(second), first.Concat(second)); + Assert.Equal(second.Concat(first), second.Concat(first)); } [Theory] @@ -41,8 +41,8 @@ private static void SameResultsWithQueryAndRepeatCallsWorker(IEnumerable f [InlineData(new int[] { 2, 3, 5, 9 }, new int[] { 8, 10 }, new int[] { 2, 3, 5, 9, 8, 10 })] // Neither side is empty public void PossiblyEmptyInputs(IEnumerable first, IEnumerable second, IEnumerable expected) { - VerifyEqualsWorker(expected, first.Concat(second)); - VerifyEqualsWorker(expected.Skip(first.Count()).Concat(expected.Take(first.Count())), second.Concat(first)); // Swap the inputs around + Assert.Equal(expected, first.Concat(second)); + Assert.Equal(expected.Skip(first.Count()).Concat(expected.Take(first.Count())), second.Concat(first)); // Swap the inputs around } [Fact] @@ -80,7 +80,7 @@ public void SecondNull() public void VerifyEquals(IEnumerable expected, IEnumerable actual) { // workaround: xUnit type inference doesn't work if the input type is not T (like IEnumerable) - VerifyEqualsWorker(expected, actual); + Assert.Equal(expected, actual); } [Theory] @@ -133,23 +133,6 @@ public void First_Last_ElementAt(IEnumerable _, IEnumerable actual) } } - private static void VerifyEqualsWorker(IEnumerable expected, IEnumerable actual) - { - // Returns a list of functions that, when applied to enumerable, should return - // another one that has equivalent contents. - var identityTransforms = IdentityTransforms(); - - // We run the transforms N^2 times, by testing all transforms - // of expected against all transforms of actual. - foreach (var outTransform in identityTransforms) - { - foreach (var inTransform in identityTransforms) - { - Assert.Equal(outTransform(expected), inTransform(actual)); - } - } - } - public static IEnumerable ArraySourcesData() => GenerateSourcesData(outerTransform: e => e.ToArray()); public static IEnumerable SelectArraySourcesData() => GenerateSourcesData(outerTransform: e => e.Select(i => i).ToArray()); @@ -162,8 +145,8 @@ private static void VerifyEqualsWorker(IEnumerable expected, IEnumerable ConcatOfConcatsData() { - yield return new object[] - { + yield return + [ Enumerable.Range(0, 20), Enumerable.Concat( Enumerable.Concat( @@ -172,7 +155,7 @@ public static IEnumerable ConcatOfConcatsData() Enumerable.Concat( Enumerable.Range(10, 3), Enumerable.Range(13, 7))) - }; + ]; } public static IEnumerable ConcatWithSelfData() @@ -180,7 +163,7 @@ public static IEnumerable ConcatWithSelfData() IEnumerable source = Enumerable.Repeat(1, 4).Concat(Enumerable.Repeat(1, 5)); source = source.Concat(source); - yield return new object[] { Enumerable.Repeat(1, 18), source }; + yield return [Enumerable.Repeat(1, 18), source]; } public static IEnumerable ChainedCollectionConcatData() => GenerateSourcesData(innerTransform: e => e.ToList()); @@ -225,7 +208,7 @@ public static IEnumerable AppendedPrependedConcatAlternationsData() } } - yield return new object[] { expected.ToArray(), actual.ToArray() }; + yield return [expected.ToArray(), actual.ToArray()]; actual = foundation; expected.Clear(); @@ -237,26 +220,26 @@ public static IEnumerable ConcatWithEmptyEnumerableData() { List baseList = [0, 1, 2, 3, 4]; - yield return new object[] - { + yield return + [ Enumerable.Range(0, 5), Enumerable.Concat(Enumerable.Concat(new List(), new List()), baseList) - }; - yield return new object[] - { + ]; + yield return + [ Enumerable.Range(0, 5), Enumerable.Concat(new List(), baseList) - }; - yield return new object[] - { + ]; + yield return + [ Enumerable.Range(0, 5), Enumerable.Concat(Enumerable.Concat(baseList, new List()), new List()) - }; - yield return new object[] - { + ]; + yield return + [ Enumerable.Range(0, 5), Enumerable.Concat(baseList, new List()) - }; + ]; } private static IEnumerable GenerateSourcesData( @@ -275,7 +258,7 @@ private static IEnumerable GenerateSourcesData( actual = outerTransform(actual.Concat(innerTransform(Enumerable.Range(j * 3, 3)))); } - yield return new object[] { expected, actual }; + yield return [expected, actual]; } } @@ -285,14 +268,14 @@ public void ManyConcats(IEnumerable> sources) { foreach (var transform in IdentityTransforms()) { - IEnumerable concatee = Enumerable.Empty(); + IEnumerable concatee = []; foreach (var source in sources) { concatee = concatee.Concat(transform(source)); } Assert.Equal(sources.Sum(s => s.Count()), concatee.Count()); - VerifyEqualsWorker(sources.SelectMany(s => s), concatee); + Assert.Equal(sources.SelectMany(s => s), concatee); } } @@ -302,7 +285,7 @@ public void ManyConcatsRunOnce(IEnumerable> sources) { foreach (var transform in IdentityTransforms()) { - IEnumerable concatee = Enumerable.Empty(); + IEnumerable concatee = []; foreach (var source in sources) { concatee = concatee.RunOnce().Concat(transform(source)); @@ -314,13 +297,13 @@ public void ManyConcatsRunOnce(IEnumerable> sources) public static IEnumerable ManyConcatsData() { - yield return new object[] { Enumerable.Repeat(Enumerable.Empty(), 256) }; - yield return new object[] { Enumerable.Repeat(Enumerable.Repeat(6, 1), 256) }; + yield return [Enumerable.Repeat(Enumerable.Empty(), 256)]; + yield return [Enumerable.Repeat(Enumerable.Repeat(6, 1), 256)]; // Make sure Concat doesn't accidentally swap around the sources, e.g. [3, 4], [1, 2] should not become [1..4] - yield return new object[] { Enumerable.Range(0, 500).Select(i => Enumerable.Repeat(i, 1)).Reverse() }; + yield return [Enumerable.Range(0, 500).Select(i => Enumerable.Repeat(i, 1)).Reverse()]; } - [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsSpeedOptimized))] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsLinqSpeedOptimized))] public void CountOfConcatIteratorShouldThrowExceptionOnIntegerOverflow() { var supposedlyLargeCollection = new DelegateBasedCollection { CountWorker = () => int.MaxValue }; @@ -373,7 +356,7 @@ public void CountOfConcatCollectionChainShouldBeResilientToStackOverflow() // ToArray needs the count as well, and the process of copying all of the collections // to the array should also not be recursive. Assert.Equal(new int[] { }, concatChain.ToArray()); - Assert.Equal(new List { }, concatChain.ToList()); // ToList also gets the count beforehand + Assert.Equal([], concatChain.ToList()); // ToList also gets the count beforehand } [Fact] @@ -425,7 +408,7 @@ public void GettingFirstEnumerableShouldBeResilientToStackOverflow() // Start with a lazy seed. // The seed holds 1 item, so during the first MoveNext we won't have to // backtrack through the linked list 30000 times. This is for test perf. - IEnumerable concatChain = ForceNotCollection(new int[] { 0xf00 }); + IEnumerable concatChain = ForceNotCollection([0xf00]); for (int i = 0; i < NumberOfConcats; i++) { @@ -461,7 +444,7 @@ public void GetEnumerableOfConcatCollectionChainFollowedByEnumerableNodeShouldBe // This time, start with an ICollection seed. We want the subsequent Concats in // the loop to produce collection iterators. - IEnumerable concatChain = new int[] { 0xf00 }; + IEnumerable concatChain = [0xf00]; for (int i = 0; i < NumberOfConcats - 1; i++) { @@ -502,98 +485,91 @@ public void CollectionInterleavedWithLazyEnumerables_ToArray(IEnumerable[] public static IEnumerable GetToArrayDataSources() { // Marker at the end - yield return new object[] - { + yield return + [ new IEnumerable[] { - new TestEnumerable(new int[] { 0 }), - new TestEnumerable(new int[] { 1 }), - new TestEnumerable(new int[] { 2 }), - new int[] { 3 }, + new TestEnumerable([0]), + new TestEnumerable([1]), + new TestEnumerable([2]), [3], } - }; + ]; // Marker at beginning - yield return new object[] - { + yield return + [ new IEnumerable[] { - new int[] { 0 }, - new TestEnumerable(new int[] { 1 }), - new TestEnumerable(new int[] { 2 }), - new TestEnumerable(new int[] { 3 }), + [0], + new TestEnumerable([1]), + new TestEnumerable([2]), + new TestEnumerable([3]), } - }; + ]; // Marker in middle - yield return new object[] - { + yield return + [ new IEnumerable[] { - new TestEnumerable(new int[] { 0 }), - new int[] { 1 }, - new TestEnumerable(new int[] { 2 }), + new TestEnumerable([0]), [1], + new TestEnumerable([2]), } - }; + ]; // Non-marker in middle - yield return new object[] - { + yield return + [ new IEnumerable[] { - new int[] { 0 }, - new TestEnumerable(new int[] { 1 }), - new int[] { 2 }, + [0], + new TestEnumerable([1]), [2], } - }; + ]; // Big arrays (marker in middle) - yield return new object[] - { + yield return + [ new IEnumerable[] { new TestEnumerable(Enumerable.Range(0, 100).ToArray()), Enumerable.Range(100, 100).ToArray(), new TestEnumerable(Enumerable.Range(200, 100).ToArray()), } - }; + ]; // Big arrays (non-marker in middle) - yield return new object[] - { + yield return + [ new IEnumerable[] { Enumerable.Range(0, 100).ToArray(), new TestEnumerable(Enumerable.Range(100, 100).ToArray()), Enumerable.Range(200, 100).ToArray(), } - }; + ]; // Interleaved (first marker) - yield return new object[] - { + yield return + [ new IEnumerable[] { - new int[] { 0 }, - new TestEnumerable(new int[] { 1 }), - new int[] { 2 }, - new TestEnumerable(new int[] { 3 }), - new int[] { 4 }, + [0], + new TestEnumerable([1]), [2], + new TestEnumerable([3]), [4], } - }; + ]; // Interleaved (first non-marker) - yield return new object[] - { + yield return + [ new IEnumerable[] { - new TestEnumerable(new int[] { 0 }), - new int[] { 1 }, - new TestEnumerable(new int[] { 2 }), - new int[] { 3 }, - new TestEnumerable(new int[] { 4 }), + new TestEnumerable([0]), [1], + new TestEnumerable([2]), [3], + new TestEnumerable([4]), } - }; + ]; } } } diff --git a/tests/System.Linq.Tests/Tests/System.Linq/ConsistencyTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/ConsistencyTests.cs index b4298162..89ea2305 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/ConsistencyTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/ConsistencyTests.cs @@ -23,10 +23,10 @@ public static void MatchSequencePattern() MethodInfo queryableNotInEnumerable = GetMissingExtensionMethod( typeof(Queryable), typeof(Enumerable), - new[] { - nameof(Queryable.AsQueryable) - } - ); + [ + nameof(Queryable.AsQueryable) + ] + ); Assert.True(queryableNotInEnumerable is null, string.Format("Queryable method {0} not defined by Enumerable", queryableNotInEnumerable)); } @@ -35,8 +35,8 @@ public static void MatchSequencePattern() // make the same change at src/System.Linq.Queryable/tests/Queryable.cs. private static IEnumerable GetExcludedMethods() { - IEnumerable result = new[] - { + IEnumerable result = + [ nameof(Enumerable.ToLookup), nameof(Enumerable.ToDictionary), nameof(Enumerable.ToArray), @@ -44,10 +44,10 @@ private static IEnumerable GetExcludedMethods() nameof(Enumerable.ToList), nameof(Enumerable.ToHashSet), nameof(Enumerable.TryGetNonEnumeratedCount), - nameof(Enumerable.Reverse), // Required for .NET 10 + nameof(Enumerable.Reverse), "Fold", - "LeftJoin", - }; + "LeftJoin" + ]; return result; } diff --git a/tests/System.Linq.Tests/Tests/System.Linq/ContainsTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/ContainsTests.cs index 198dfe22..139260b6 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/ContainsTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/ContainsTests.cs @@ -4,6 +4,8 @@ using System.Collections.Generic; using Xunit; +#pragma warning disable xUnit2017 + namespace System.Linq.Tests { public class ContainsTests : EnumerableTests @@ -32,17 +34,17 @@ public static IEnumerable Int_TestData() { foreach (Func, IEnumerable> transform in IdentityTransforms()) { - yield return new object[] { transform(new int[0]), 6, false }; - yield return new object[] { transform(new int[] { 8, 10, 3, 0, -8 }), 6, false }; - yield return new object[] { transform(new int[] { 8, 10, 3, 0, -8 }), 8, true }; - yield return new object[] { transform(new int[] { 8, 10, 3, 0, -8 }), -8, true }; - yield return new object[] { transform(new int[] { 8, 0, 10, 3, 0, -8, 0 }), 0, true }; - - yield return new object[] { transform(Enumerable.Range(0, 0)), 0, false }; - yield return new object[] { transform(Enumerable.Range(4, 5)), 3, false }; - yield return new object[] { transform(Enumerable.Range(3, 5)), 3, true }; - yield return new object[] { transform(Enumerable.Range(3, 5)), 7, true }; - yield return new object[] { transform(Enumerable.Range(10, 3)), 10, true }; + yield return [transform(new int[0]), 6, false]; + yield return [transform([8, 10, 3, 0, -8]), 6, false]; + yield return [transform([8, 10, 3, 0, -8]), 8, true]; + yield return [transform([8, 10, 3, 0, -8]), -8, true]; + yield return [transform([8, 0, 10, 3, 0, -8, 0]), 0, true]; + + yield return [transform(Enumerable.Range(0, 0)), 0, false]; + yield return [transform(Enumerable.Range(4, 5)), 3, false]; + yield return [transform(Enumerable.Range(3, 5)), 3, true]; + yield return [transform(Enumerable.Range(3, 5)), 7, true]; + yield return [transform(Enumerable.Range(10, 3)), 10, true]; } } @@ -63,11 +65,11 @@ public void IntRunOnce(IEnumerable source, int value, bool expected) public static IEnumerable String_TestData() { - yield return new object[] { new string[] { null }, StringComparer.Ordinal, null, true }; - yield return new object[] { new string[] { "Bob", "Robert", "Tim" }, null, "trboeR", false }; - yield return new object[] { new string[] { "Bob", "Robert", "Tim" }, null, "Tim", true }; - yield return new object[] { new string[] { "Bob", "Robert", "Tim" }, new AnagramEqualityComparer(), "trboeR", true }; - yield return new object[] { new string[] { "Bob", "Robert", "Tim" }, new AnagramEqualityComparer(), "nevar", false }; + yield return [new string[] { null }, StringComparer.Ordinal, null, true]; + yield return [new string[] { "Bob", "Robert", "Tim" }, null, "trboeR", false]; + yield return [new string[] { "Bob", "Robert", "Tim" }, null, "Tim", true]; + yield return [new string[] { "Bob", "Robert", "Tim" }, new AnagramEqualityComparer(), "trboeR", true]; + yield return [new string[] { "Bob", "Robert", "Tim" }, new AnagramEqualityComparer(), "nevar", false]; } [Theory] @@ -93,11 +95,11 @@ public void StringRunOnce(IEnumerable source, IEqualityComparer public static IEnumerable NullableInt_TestData() { - yield return new object[] { new int?[] { 8, 0, 10, 3, 0, -8, 0 }, null, false }; - yield return new object[] { new int?[] { 8, 0, 10, null, 3, 0, -8, 0 }, null, true }; + yield return [new int?[] { 8, 0, 10, 3, 0, -8, 0 }, null, false]; + yield return [new int?[] { 8, 0, 10, null, 3, 0, -8, 0 }, null, true]; - yield return new object[] { NullableNumberRangeGuaranteedNotCollectionType(3, 4), null, false }; - yield return new object[] { RepeatedNullableNumberGuaranteedNotCollectionType(null, 5), null, true }; + yield return [NullableNumberRangeGuaranteedNotCollectionType(3, 4), null, false]; + yield return [RepeatedNullableNumberGuaranteedNotCollectionType(null, 5), null, true]; } [Theory] @@ -146,5 +148,145 @@ public void NoComparerDoesDeferToCollection() IEnumerable source = new HashSet(StringComparer.OrdinalIgnoreCase) { "ABC" }; Assert.Contains("abc", source); } + + [Fact] + public void FollowingVariousOperators() + { + IEnumerable source = Enumerable.Range(1, 3); + foreach (var transform in IdentityTransforms()) + { + IEnumerable transformedSource = transform(source); + IEnumerable transformedEmpty = transform([]); + + Assert.Contains(1, transformedSource); + Assert.Contains(2, transformedSource); + Assert.Contains(3, transformedSource); + Assert.DoesNotContain(0, transformedSource); + Assert.DoesNotContain(4, transformedSource); + + // Append/Prepend + var ap = transformedSource.Append(4).Prepend(5).Append(6).Prepend(7); + Assert.Contains(3, ap); + Assert.Contains(4, ap); + Assert.Contains(5, ap); + Assert.Contains(6, ap); + Assert.Contains(7, ap); + Assert.DoesNotContain(8, ap); + + // Concat + Assert.Contains(2, transform([4, 5, 6]).Concat(transformedSource)); + Assert.DoesNotContain(7, transform([4, 5, 6]).Concat(transformedSource)); + Assert.Contains(2, transform([4, 5, 6]).Concat(transform([7, 8, 9])).Concat(transformedSource)); + Assert.DoesNotContain(10, transform([4, 5, 6]).Concat(transform([7, 8, 9])).Concat(transformedSource)); + + // DefaultIfEmpty + Assert.Contains(1, transformedSource.DefaultIfEmpty(4)); + Assert.DoesNotContain(0, transformedEmpty.DefaultIfEmpty(4)); + Assert.Contains(4, transformedEmpty.DefaultIfEmpty(4)); + Assert.DoesNotContain(4, transformedSource.DefaultIfEmpty(4)); + Assert.DoesNotContain(4, transformedSource.DefaultIfEmpty(0)); + Assert.DoesNotContain(0, transformedSource.DefaultIfEmpty()); + Assert.Contains(0, transformedEmpty.DefaultIfEmpty()); + Assert.DoesNotContain(4, transformedSource.DefaultIfEmpty()); + + // Distinct + Assert.Contains(2, transform(source.Concat(source)).Distinct()); + Assert.DoesNotContain(4, transform(source.Concat(source)).Distinct()); + Assert.Contains(1, transform(source.Concat(source)).Distinct()); + Assert.Contains(1, transform(source.Concat(source)).Distinct(EqualityComparer.Create((x, y) => true, x => 0))); + Assert.DoesNotContain(2, transform(source.Concat(source)).Distinct(EqualityComparer.Create((x, y) => true, x => 0))); + Assert.DoesNotContain(0, transform(source.Concat(source)).Distinct(EqualityComparer.Create((x, y) => true, x => 0))); + + // OrderBy + Assert.Contains(2, transformedSource.OrderBy(x => x)); + Assert.Contains(2, transformedSource.OrderBy(x => x).ThenBy(x => x)); + Assert.DoesNotContain(4, transformedSource.OrderBy(x => x)); + Assert.DoesNotContain(4, transformedSource.OrderBy(x => x).ThenBy(x => x)); + + // OrderByDescending + Assert.Contains(2, transformedSource.OrderByDescending(x => x)); + Assert.Contains(2, transformedSource.OrderByDescending(x => x).ThenByDescending(x => x)); + Assert.DoesNotContain(4, transformedSource.OrderByDescending(x => x)); + Assert.DoesNotContain(4, transformedSource.OrderByDescending(x => x).ThenByDescending(x => x)); + + // Where/Select + Assert.Contains(2, transformedSource.Where(x => x > 1)); + Assert.DoesNotContain(2, transformedSource.Where(x => x > 3)); + Assert.Contains(6, transformedSource.Select(x => x * 2)); + Assert.DoesNotContain(3, transformedSource.Select(x => x * 2)); + Assert.Contains(4, transformedSource.Where(x => x % 2 == 0).Select(x => x * 2)); + Assert.DoesNotContain(6, transformedSource.Where(x => x % 2 == 0).Select(x => x * 2)); + + // SelectMany + Assert.Contains(2, transformedSource.SelectMany(x => new[] { x })); + Assert.Contains(2, transformedSource.SelectMany(x => new List { x, x * 2 })); + Assert.DoesNotContain(4, transformedSource.SelectMany(x => new[] { x })); + Assert.Contains(4, transformedSource.SelectMany(x => new List { x, x * 2 })); + Assert.DoesNotContain(5, transformedSource.SelectMany(x => new List { x, x * 2 })); + + // Shuffle + Assert.Contains(2, transformedSource.Shuffle()); + Assert.DoesNotContain(4, transformedSource.Shuffle()); + Assert.DoesNotContain(4, transformedSource.Shuffle().Take(1)); + Assert.Contains(2, transformedSource.Shuffle().Take(3)); + Assert.False(transformedSource.Shuffle().Take(1).Contains(4)); + for (int trial = 0; trial < 100 && !transformedSource.Shuffle().Take(1).Contains(3); trial++) + { + if (trial == 99) + { + Assert.Fail("Shuffle().Take() didn't contain value after 100 tries. The chances of that are infinitesimal with a correct implementation."); + } + } + + // Skip/Take + Assert.True(transformedSource.Skip(2).Contains(3)); + Assert.True(transformedSource.Skip(2).Take(1).Contains(3)); + Assert.True(transformedSource.Take(1).Contains(1)); + Assert.False(transformedSource.Take(1).Contains(2)); + Assert.False(transformedSource.Take(1).Contains(2)); + Assert.False(transformedSource.Take(2).Contains(3)); + Assert.False(transformedSource.Skip(1).Take(1).Contains(1)); + Assert.True(transformedSource.Skip(1).Take(1).Contains(2)); + Assert.False(transformedSource.Skip(1).Take(1).Contains(3)); + + // Union + Assert.True(transformedSource.Union(transform([4])).Contains(4)); + Assert.True(transformedSource.Union(transform([4]), EqualityComparer.Create((x, y) => true, x => 0)).Contains(1)); + Assert.False(transformedSource.Union(transform([4]), EqualityComparer.Create((x, y) => true, x => 0)).Contains(4)); + Assert.False(transformedSource.Union(transform([3])).Contains(4)); + } + + // DefaultIfEmpty + Assert.True(Enumerable.Empty().DefaultIfEmpty(1).Contains(1)); + Assert.False(Enumerable.Empty().DefaultIfEmpty(1).Contains(0)); + + // Distinct + Assert.True(new string[] { "a", "A" }.Distinct().Contains("a")); + Assert.True(new string[] { "a", "A" }.Distinct().Contains("A")); + Assert.True(new string[] { "a", "A" }.Distinct(StringComparer.OrdinalIgnoreCase).Contains("a")); + Assert.False(new string[] { "a", "A" }.Distinct(StringComparer.OrdinalIgnoreCase).Contains("A")); + + // Repeat + Assert.True(Enumerable.Repeat(1, 5).Contains(1)); + Assert.False(Enumerable.Repeat(1, 5).Contains(2)); + + // Cast + Assert.True(new int[] { 1, 2, 3 }.Cast().Contains(2)); + Assert.True(new object[] { 1, 2, 3 }.Cast().Contains(2)); + Assert.False(new object[] { 1, 2, 3 }.Cast().Contains(4)); + + // OfType + Assert.True(new object[] { 1, "2", 3 }.OfType().Contains(3)); + Assert.False(new object[] { 1, "2", 3 }.OfType().Contains(4)); + Assert.False(new object[] { 1, "2", 3 }.OfType().Contains(2)); + Assert.True(new object[] { 1, "2", 3 }.OfType().Contains("2")); + Assert.False(new object[] { 1, "2", 3 }.OfType().Contains("4")); + + // Union + Assert.True(new string[] { "a" }.Union(new string[] { "A" }).Contains("a")); + Assert.True(new string[] { "a" }.Union(new string[] { "A" }).Contains("A")); + Assert.True(new string[] { "a" }.Union(new string[] { "A" }, StringComparer.OrdinalIgnoreCase).Contains("a")); + Assert.False(new string[] { "a" }.Union(new string[] { "A" }, StringComparer.OrdinalIgnoreCase).Contains("A")); + } } } diff --git a/tests/System.Linq.Tests/Tests/System.Linq/CountByTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/CountByTests.cs index c144d839..1de6ae1d 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/CountByTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/CountByTests.cs @@ -20,7 +20,7 @@ public void CountBy_SourceNull_ThrowsArgumentNullException() [Fact] public void CountBy_KeySelectorNull_ThrowsArgumentNullException() { - string[] source = { "Bob", "Tim", "Robert", "Chris" }; + string[] source = ["Bob", "Tim", "Robert", "Chris"]; Func keySelector = null; AssertExtensions.Throws("keySelector", () => source.CountBy(keySelector)); @@ -64,7 +64,7 @@ public void CountBy_HasExpectedOutput() source: Enumerable.Empty(), keySelector: x => x, comparer: null, - expected: Enumerable.Empty>()); + expected: []); Validate( source: Enumerable.Range(0, 10), @@ -91,7 +91,7 @@ public void CountBy_HasExpectedOutput() expected: Enumerable.Repeat(5, 1).Select(x => new KeyValuePair(x, 20))); Validate( - source: new string[] { "Bob", "bob", "tim", "Bob", "Tim" }, + source: ["Bob", "bob", "tim", "Bob", "Tim"], keySelector: x => x, null, expected: @@ -103,7 +103,7 @@ public void CountBy_HasExpectedOutput() ]); Validate( - source: new string[] { "Bob", "bob", "tim", "Bob", "Tim" }, + source: ["Bob", "bob", "tim", "Bob", "Tim"], keySelector: x => x, StringComparer.OrdinalIgnoreCase, expected: diff --git a/tests/System.Linq.Tests/Tests/System.Linq/CountTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/CountTests.cs index 923a96a3..e6a35f22 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/CountTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/CountTests.cs @@ -30,18 +30,18 @@ public void SameResultsRepeatCallsStringQuery() public static IEnumerable Int_TestData() { - yield return new object[] { new int[0], null, 0 }; + yield return [new int[0], null, 0]; Func isEvenFunc = IsEven; - yield return new object[] { new int[0], isEvenFunc, 0 }; - yield return new object[] { new int[] { 4 }, isEvenFunc, 1 }; - yield return new object[] { new int[] { 5 }, isEvenFunc, 0 }; - yield return new object[] { new int[] { 2, 5, 7, 9, 29, 10 }, isEvenFunc, 2 }; - yield return new object[] { new int[] { 2, 20, 22, 100, 50, 10 }, isEvenFunc, 6 }; + yield return [new int[0], isEvenFunc, 0]; + yield return [new int[] { 4 }, isEvenFunc, 1]; + yield return [new int[] { 5 }, isEvenFunc, 0]; + yield return [new int[] { 2, 5, 7, 9, 29, 10 }, isEvenFunc, 2]; + yield return [new int[] { 2, 20, 22, 100, 50, 10 }, isEvenFunc, 6]; - yield return new object[] { RepeatedNumberGuaranteedNotCollectionType(0, 0), null, 0 }; - yield return new object[] { RepeatedNumberGuaranteedNotCollectionType(5, 1), null, 1 }; - yield return new object[] { RepeatedNumberGuaranteedNotCollectionType(5, 10), null, 10 }; + yield return [RepeatedNumberGuaranteedNotCollectionType(0, 0), null, 0]; + yield return [RepeatedNumberGuaranteedNotCollectionType(5, 1), null, 1]; + yield return [RepeatedNumberGuaranteedNotCollectionType(5, 10), null, 10]; } [Theory] @@ -80,7 +80,7 @@ public void IntRunOnce(IEnumerable source, Func predicate, int e [Fact] public void NullableIntArray_IncludesNullObjects() { - int?[] data = { -10, 4, 9, null, 11 }; + int?[] data = [-10, 4, 9, null, 11]; Assert.Equal(5, data.Count()); } @@ -99,9 +99,9 @@ public void RunOnce(int count, IEnumerable enumerable) private static IEnumerable EnumerateCollectionTypesAndCounts(int count, IEnumerable enumerable) { - foreach (var transform in IdentityTransforms()) + foreach (IEnumerable source in CreateSources(enumerable)) { - yield return new object[] { count, transform(enumerable) }; + yield return [count, source]; } } @@ -172,16 +172,17 @@ IEnumerable Source() public static IEnumerable NonEnumeratedCount_SupportedEnumerables() { - yield return WrapArgs(4, new int[] { 1, 2, 3, 4 }); - yield return WrapArgs(4, new List(new int[] { 1, 2, 3, 4 })); - yield return WrapArgs(4, new Stack(new int[] { 1, 2, 3, 4 })); + yield return WrapArgs(4, [1, 2, 3, 4]); + yield return WrapArgs(4, new List([1, 2, 3, 4])); + yield return WrapArgs(4, new Stack([1, 2, 3, 4])); yield return WrapArgs(0, Enumerable.Empty()); - if (PlatformDetection.IsSpeedOptimized) + yield return WrapArgs(100, Enumerable.Range(1, 100)); + yield return WrapArgs(80, Enumerable.Repeat(1, 80)); + + if (PlatformDetection.IsLinqSpeedOptimized) { - yield return WrapArgs(100, Enumerable.Range(1, 100)); - yield return WrapArgs(80, Enumerable.Repeat(1, 80)); yield return WrapArgs(50, Enumerable.Range(1, 50).Select(x => x + 1)); yield return WrapArgs(4, new int[] { 1, 2, 3, 4 }.Select(x => x + 1)); yield return WrapArgs(50, Enumerable.Range(1, 50).Select(x => x + 1).Select(x => x - 1)); @@ -190,20 +191,18 @@ public static IEnumerable NonEnumeratedCount_SupportedEnumerables() yield return WrapArgs(20, Enumerable.Range(1, 10).Concat(Enumerable.Range(11, 10))); } - static object[] WrapArgs(int expectedCount, IEnumerable source) => new object[] { expectedCount, source }; + static object[] WrapArgs(int expectedCount, IEnumerable source) => [expectedCount, source]; } public static IEnumerable NonEnumeratedCount_UnsupportedEnumerables() { yield return WrapArgs(Enumerable.Range(1, 100).Where(x => x % 2 == 0)); yield return WrapArgs(Enumerable.Range(1, 100).GroupBy(x => x % 2 == 0)); - yield return WrapArgs(new Stack(new int[] { 1, 2, 3, 4 }).Select(x => x + 1)); + yield return WrapArgs(new Stack([1, 2, 3, 4]).Select(x => x + 1)); yield return WrapArgs(Enumerable.Range(1, 100).Distinct()); - if (!PlatformDetection.IsSpeedOptimized) + if (!PlatformDetection.IsLinqSpeedOptimized) { - yield return WrapArgs(Enumerable.Range(1, 100)); - yield return WrapArgs(Enumerable.Repeat(1, 80)); yield return WrapArgs(Enumerable.Range(1, 50).Select(x => x + 1)); yield return WrapArgs(new int[] { 1, 2, 3, 4 }.Select(x => x + 1)); yield return WrapArgs(Enumerable.Range(1, 50).Select(x => x + 1).Select(x => x - 1)); @@ -212,7 +211,7 @@ public static IEnumerable NonEnumeratedCount_UnsupportedEnumerables() yield return WrapArgs(Enumerable.Range(1, 10).Concat(Enumerable.Range(11, 10))); } - static object[] WrapArgs(IEnumerable source) => new object[] { source }; + static object[] WrapArgs(IEnumerable source) => [source]; } } } diff --git a/tests/System.Linq.Tests/Tests/System.Linq/DefaultIfEmptyTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/DefaultIfEmptyTests.cs index 3dadd6d6..8f153939 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/DefaultIfEmptyTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/DefaultIfEmptyTests.cs @@ -30,14 +30,14 @@ public void SameResultsRepeatCallsEmptyQuery() public static IEnumerable 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(), 0, new int[] { 0 } }; + yield return [new int[0], 0, new int[] { 0 }]; + yield return [new int[] { 3 }, 0, new int[] { 3 }]; + yield return [new int[] { 3, -1, 0, 10, 15 }, 0, new int[] { 3, -1, 0, 10, 15 }]; + + yield return [new int[0], -10, new int[] { -10 }]; + yield return [new int[] { 3 }, 9, new int[] { 3 }]; + yield return [new int[] { 3, -1, 0, 10, 15 }, 9, new int[] { 3, -1, 0, 10, 15 }]; + yield return [Enumerable.Empty(), 0, new int[] { 0 }]; } [Theory] @@ -76,16 +76,16 @@ public static void DefaultIfEmptyRunOnce(IEnumerable source, int defaultVal [Fact] public void NullableArray_Empty_WithoutDefaultValue() { - int?[] source = new int?[0]; - Assert.Equal(new int?[] { null }, source.DefaultIfEmpty()); + int?[] source = []; + Assert.Equal([null], source.DefaultIfEmpty()); } [Fact] public void NullableArray_Empty_WithDefaultValue() { - int?[] source = new int?[0]; + int?[] source = []; int? defaultValue = 9; - Assert.Equal(new int?[] { defaultValue }, source.DefaultIfEmpty(defaultValue)); + Assert.Equal([defaultValue], source.DefaultIfEmpty(defaultValue)); } [Fact] @@ -118,7 +118,7 @@ public void First_Last_ElementAt() Assert.Throws(() => nonEmpty.ElementAt(-1)); Assert.Throws(() => nonEmpty.ElementAt(4)); - IEnumerable empty = Enumerable.Empty(); + IEnumerable empty = []; Assert.Equal(42, empty.DefaultIfEmpty(42).First()); Assert.Equal(42, empty.DefaultIfEmpty(42).Last()); Assert.Equal(42, empty.DefaultIfEmpty(42).ElementAt(0)); diff --git a/tests/System.Linq.Tests/Tests/System.Linq/DistinctTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/DistinctTests.cs index 24ba295f..f0e29e65 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/DistinctTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/DistinctTests.cs @@ -33,22 +33,22 @@ where string.IsNullOrEmpty(x) [Fact] public void EmptySource() { - int[] source = { }; + int[] source = []; Assert.Empty(source.Distinct()); } [Fact] public void EmptySourceRunOnce() { - int[] source = { }; + int[] source = []; Assert.Empty(source.RunOnce().Distinct()); } [Fact] public void SingleNullElementExplicitlyUseDefaultComparer() { - string[] source = { null }; - string[] expected = { null }; + string[] source = [null]; + string[] expected = [null]; Assert.Equal(expected, source.Distinct(EqualityComparer.Default)); } @@ -56,8 +56,8 @@ public void SingleNullElementExplicitlyUseDefaultComparer() [Fact] public void EmptyStringDistinctFromNull() { - string[] source = { null, null, string.Empty }; - string[] expected = { null, string.Empty }; + string[] source = [null, null, string.Empty]; + string[] expected = [null, string.Empty]; Assert.Equal(expected, source.Distinct(EqualityComparer.Default)); } @@ -65,8 +65,8 @@ public void EmptyStringDistinctFromNull() [Fact] public void CollapsDuplicateNulls() { - string[] source = { null, null }; - string[] expected = { null }; + string[] source = [null, null]; + string[] expected = [null]; Assert.Equal(expected, source.Distinct(EqualityComparer.Default)); } @@ -74,8 +74,8 @@ public void CollapsDuplicateNulls() [Fact] public void SourceAllDuplicates() { - int[] source = { 5, 5, 5, 5, 5, 5 }; - int[] expected = { 5 }; + int[] source = [5, 5, 5, 5, 5, 5]; + int[] expected = [5]; Assert.Equal(expected, source.Distinct()); } @@ -83,7 +83,7 @@ public void SourceAllDuplicates() [Fact] public void AllUnique() { - int[] source = { 2, -5, 0, 6, 10, 9 }; + int[] source = [2, -5, 0, 6, 10, 9]; Assert.Equal(source, source.Distinct()); } @@ -91,8 +91,8 @@ public void AllUnique() [Fact] public void SomeDuplicatesIncludingNulls() { - int?[] source = { 1, 1, 1, 2, 2, 2, null, null }; - int?[] expected = { 1, 2, null }; + int?[] source = [1, 1, 1, 2, 2, 2, null, null]; + int?[] expected = [1, 2, null]; Assert.Equal(expected, source.Distinct()); } @@ -100,8 +100,8 @@ public void SomeDuplicatesIncludingNulls() [Fact] public void SomeDuplicatesIncludingNullsRunOnce() { - int?[] source = { 1, 1, 1, 2, 2, 2, null, null }; - int?[] expected = { 1, 2, null }; + int?[] source = [1, 1, 1, 2, 2, 2, null, null]; + int?[] expected = [1, 2, null]; Assert.Equal(expected, source.RunOnce().Distinct()); } @@ -109,8 +109,8 @@ public void SomeDuplicatesIncludingNullsRunOnce() [Fact] public void LastSameAsFirst() { - int[] source = { 1, 2, 3, 4, 5, 1 }; - int[] expected = { 1, 2, 3, 4, 5 }; + int[] source = [1, 2, 3, 4, 5, 1]; + int[] expected = [1, 2, 3, 4, 5]; Assert.Equal(expected, source.Distinct()); } @@ -119,8 +119,8 @@ public void LastSameAsFirst() [Fact] public void RepeatsNonConsecutive() { - int[] source = { 1, 1, 2, 2, 4, 3, 1, 3, 2 }; - int[] expected = { 1, 2, 4, 3 }; + int[] source = [1, 1, 2, 2, 4, 3, 1, 3, 2]; + int[] expected = [1, 2, 4, 3]; Assert.Equal(expected, source.Distinct()); } @@ -128,8 +128,8 @@ public void RepeatsNonConsecutive() [Fact] public void RepeatsNonConsecutiveRunOnce() { - int[] source = { 1, 1, 2, 2, 4, 3, 1, 3, 2 }; - int[] expected = { 1, 2, 4, 3 }; + int[] source = [1, 1, 2, 2, 4, 3, 1, 3, 2]; + int[] expected = [1, 2, 4, 3]; Assert.Equal(expected, source.RunOnce().Distinct()); } @@ -137,8 +137,8 @@ public void RepeatsNonConsecutiveRunOnce() [Fact] public void NullComparer() { - string[] source = { "Bob", "Tim", "bBo", "miT", "Robert", "iTm" }; - string[] expected = { "Bob", "Tim", "bBo", "miT", "Robert", "iTm" }; + string[] source = ["Bob", "Tim", "bBo", "miT", "Robert", "iTm"]; + string[] expected = ["Bob", "Tim", "bBo", "miT", "Robert", "iTm"]; Assert.Equal(expected, source.Distinct()); } @@ -162,8 +162,8 @@ public void NullSourceCustomComparer() [Fact] public void CustomEqualityComparer() { - string[] source = { "Bob", "Tim", "bBo", "miT", "Robert", "iTm" }; - string[] expected = { "Bob", "Tim", "Robert" }; + string[] source = ["Bob", "Tim", "bBo", "miT", "Robert", "iTm"]; + string[] expected = ["Bob", "Tim", "Robert"]; Assert.Equal(expected, source.Distinct(new AnagramEqualityComparer()), new AnagramEqualityComparer()); } @@ -171,8 +171,8 @@ public void CustomEqualityComparer() [Fact] public void CustomEqualityComparerRunOnce() { - string[] source = { "Bob", "Tim", "bBo", "miT", "Robert", "iTm" }; - string[] expected = { "Bob", "Tim", "Robert" }; + string[] source = ["Bob", "Tim", "bBo", "miT", "Robert", "iTm"]; + string[] expected = ["Bob", "Tim", "Robert"]; Assert.Equal(expected, source.RunOnce().Distinct(new AnagramEqualityComparer()), new AnagramEqualityComparer()); } @@ -197,13 +197,15 @@ public void FindDistinctAndValidate(IEnumerable original) public static IEnumerable SequencesWithDuplicates() { // Validate an array of different numeric data types. - yield return new object[] { new int[] { 1, 1, 1, 2, 3, 5, 5, 6, 6, 10 } }; - yield return new object[] { new long[] { 1, 1, 1, 2, 3, 5, 5, 6, 6, 10 } }; - yield return new object[] { new float[] { 1, 1, 1, 2, 3, 5, 5, 6, 6, 10 } }; - yield return new object[] { new double[] { 1, 1, 1, 2, 3, 5, 5, 6, 6, 10 } }; - yield return new object[] { new decimal[] { 1, 1, 1, 2, 3, 5, 5, 6, 6, 10 } }; + yield return [new int[] { 1, 1, 1, 2, 3, 5, 5, 6, 6, 10 }]; + yield return [new long[] { 1, 1, 1, 2, 3, 5, 5, 6, 6, 10 }]; + yield return [new float[] { 1, 1, 1, 2, 3, 5, 5, 6, 6, 10 }]; + yield return [new double[] { 1, 1, 1, 2, 3, 5, 5, 6, 6, 10 }]; + yield return [new decimal[] { 1, 1, 1, 2, 3, 5, 5, 6, 6, 10 }]; // Try strings - yield return new object[] { new [] + yield return + [ + new [] { "add", "add", @@ -221,7 +223,7 @@ public static IEnumerable SequencesWithDuplicates() "namespace", "namespace", } - }; + ]; } [Fact] @@ -236,8 +238,8 @@ public void ForcedToEnumeratorDoesntEnumerate() [Fact] public void ToArray() { - int?[] source = { 1, 1, 1, 2, 2, 2, null, null }; - int?[] expected = { 1, 2, null }; + int?[] source = [1, 1, 1, 2, 2, 2, null, null]; + int?[] expected = [1, 2, null]; Assert.Equal(expected, source.Distinct().ToArray()); } @@ -245,8 +247,8 @@ public void ToArray() [Fact] public void ToList() { - int?[] source = { 1, 1, 1, 2, 2, 2, null, null }; - int?[] expected = { 1, 2, null }; + int?[] source = [1, 1, 1, 2, 2, 2, null, null]; + int?[] expected = [1, 2, null]; Assert.Equal(expected, source.Distinct().ToList()); } @@ -254,14 +256,14 @@ public void ToList() [Fact] public void Count() { - int?[] source = { 1, 1, 1, 2, 2, 2, null, null }; + int?[] source = [1, 1, 1, 2, 2, 2, null, null]; Assert.Equal(3, source.Distinct().Count()); } [Fact] public void RepeatEnumerating() { - int?[] source = { 1, 1, 1, 2, 2, 2, null, null }; + int?[] source = [1, 1, 1, 2, 2, 2, null, null]; var result = source.Distinct(); @@ -280,7 +282,7 @@ public void DistinctBy_SourceNull_ThrowsArgumentNullException() [Fact] public void DistinctBy_KeySelectorNull_ThrowsArgumentNullException() { - string[] source = { "Bob", "Tim", "Robert", "Chris" }; + string[] source = ["Bob", "Tim", "Robert", "Chris"]; Func keySelector = null; AssertExtensions.Throws("keySelector", () => source.DistinctBy(keySelector)); @@ -307,7 +309,7 @@ public static IEnumerable DistinctBy_TestData() source: Array.Empty(), keySelector: x => x, comparer: null, - expected: Enumerable.Empty()); + expected: []); yield return WrapArgs( source: Enumerable.Range(0, 10), @@ -319,7 +321,7 @@ public static IEnumerable DistinctBy_TestData() source: Enumerable.Range(5, 10), keySelector: x => true, comparer: null, - expected: new int[] { 5 }); + expected: [5]); yield return WrapArgs( source: Enumerable.Range(0, 20), @@ -334,16 +336,16 @@ public static IEnumerable DistinctBy_TestData() expected: Enumerable.Repeat(5, 1)); yield return WrapArgs( - source: new string[] { "Bob", "bob", "tim", "Bob", "Tim" }, + source: ["Bob", "bob", "tim", "Bob", "Tim"], keySelector: x => x, null, - expected: new string[] { "Bob", "bob", "tim", "Tim" }); + expected: ["Bob", "bob", "tim", "Tim"]); yield return WrapArgs( - source: new string[] { "Bob", "bob", "tim", "Bob", "Tim" }, + source: ["Bob", "bob", "tim", "Bob", "Tim"], keySelector: x => x, StringComparer.OrdinalIgnoreCase, - expected: new string[] { "Bob", "tim" }); + expected: ["Bob", "tim"]); yield return WrapArgs( source: new (string Name, int Age)[] { ("Tom", 20), ("Dick", 30), ("Harry", 40) }, @@ -370,7 +372,7 @@ public static IEnumerable DistinctBy_TestData() expected: new (string Name, int Age)[] { ("Bob", 20), ("Harry", 40) }); object[] WrapArgs(IEnumerable source, Func keySelector, IEqualityComparer? comparer, IEnumerable expected) - => new object[] { source, keySelector, comparer, expected }; + => [source, keySelector, comparer, expected]; } } } diff --git a/tests/System.Linq.Tests/Tests/System.Linq/ElementAtOrDefaultTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/ElementAtOrDefaultTests.cs index a462be3e..0672673b 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/ElementAtOrDefaultTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/ElementAtOrDefaultTests.cs @@ -32,18 +32,18 @@ public void SameResultsRepeatCallsStringQuery() public static IEnumerable TestData() { - yield return new object[] { NumberRangeGuaranteedNotCollectionType(9, 1), 0, 1, 9 }; - yield return new object[] { NumberRangeGuaranteedNotCollectionType(9, 10), 9, 1, 18 }; - yield return new object[] { NumberRangeGuaranteedNotCollectionType(-4, 10), 3, 7, -1 }; - - yield return new object[] { new int[] { 1, 2, 3, 4 }, 4, 0, 0 }; - yield return new object[] { new int[0], 0, 0, 0 }; - yield return new object[] { new int[] { -4 }, 0, 1, -4 }; - yield return new object[] { new int[] { 9, 8, 0, -5, 10 }, 4, 1, 10 }; - - yield return new object[] { NumberRangeGuaranteedNotCollectionType(-4, 5), -1, 6, 0 }; - yield return new object[] { NumberRangeGuaranteedNotCollectionType(5, 5), 5, 0, 0 }; - yield return new object[] { NumberRangeGuaranteedNotCollectionType(0, 0), 0, 0, 0 }; + yield return [NumberRangeGuaranteedNotCollectionType(9, 1), 0, 1, 9]; + yield return [NumberRangeGuaranteedNotCollectionType(9, 10), 9, 1, 18]; + yield return [NumberRangeGuaranteedNotCollectionType(-4, 10), 3, 7, -1]; + + yield return [new int[] { 1, 2, 3, 4 }, 4, 0, 0]; + yield return [new int[0], 0, 0, 0]; + yield return [new int[] { -4 }, 0, 1, -4]; + yield return [new int[] { 9, 8, 0, -5, 10 }, 4, 1, 10]; + + yield return [NumberRangeGuaranteedNotCollectionType(-4, 5), -1, 6, 0]; + yield return [NumberRangeGuaranteedNotCollectionType(5, 5), 5, 0, 0]; + yield return [NumberRangeGuaranteedNotCollectionType(0, 0), 0, 0, 0]; } [Theory] @@ -77,7 +77,7 @@ public void ElementAtOrDefaultRunOnce(IEnumerable source, int index, int in [Fact] public void NullableArray_InvalidIndex_ReturnsNull() { - int?[] source = { 9, 8 }; + int?[] source = [9, 8]; Assert.Null(source.ElementAtOrDefault(-1)); Assert.Null(source.ElementAtOrDefault(3)); Assert.Null(source.ElementAtOrDefault(int.MaxValue)); @@ -92,7 +92,7 @@ public void NullableArray_InvalidIndex_ReturnsNull() [Fact] public void NullableArray_ValidIndex_ReturnsCorrectObject() { - int?[] source = { 9, 8, null, -5, 10 }; + int?[] source = [9, 8, null, -5, 10]; Assert.Null(source.ElementAtOrDefault(2)); Assert.Equal(-5, source.ElementAtOrDefault(3)); @@ -120,7 +120,7 @@ public void MutableSource() Assert.Equal(2, source.ElementAtOrDefault(new Index(2))); Assert.Equal(2, source.ElementAtOrDefault(^3)); - source.InsertRange(3, new[] { -1, -2 }); + source.InsertRange(3, [-1, -2]); source.RemoveAt(0); Assert.Equal(-1, source.ElementAtOrDefault(2)); Assert.Equal(-1, source.ElementAtOrDefault(new Index(2))); @@ -137,7 +137,7 @@ public void MutableSourceNotList() Assert.Equal(2, query1[2].ElementAtOrDefault(^3)); var query2 = Repeat(_ => ForceNotCollection(source).Select(i => i), 3); - source.InsertRange(3, new[] { -1, -2 }); + source.InsertRange(3, [-1, -2]); source.RemoveAt(0); Assert.Equal(-1, query2[0].ElementAtOrDefault(2)); Assert.Equal(-1, query2[1].ElementAtOrDefault(new Index(2))); @@ -186,7 +186,7 @@ public void EnumerateElements() [Fact] public void NonEmptySource_Consistency() { - int?[] source = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + int?[] source = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; Assert.Equal(5, source.ElementAtOrDefault(5)); Assert.Equal(5, source.ElementAtOrDefault(new Index(5))); @@ -217,7 +217,7 @@ public void NonEmptySource_Consistency() [Fact] public void NonEmptySource_Consistency_NotList() { - int?[] source = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + int?[] source = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; Assert.Equal(5, ForceNotCollection(source).ElementAtOrDefault(5)); Assert.Equal(5, ForceNotCollection(source).ElementAtOrDefault(new Index(5))); @@ -268,7 +268,7 @@ public void NonEmptySource_Consistency_NotList() [Fact] public void NonEmptySource_Consistency_ListPartition() { - int?[] source = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + int?[] source = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; Assert.Equal(5, ListPartitionOrEmpty(source).ElementAtOrDefault(5)); Assert.Equal(5, ListPartitionOrEmpty(source).ElementAtOrDefault(new Index(5))); @@ -299,7 +299,7 @@ public void NonEmptySource_Consistency_ListPartition() [Fact] public void NonEmptySource_Consistency_EnumerablePartition() { - int?[] source = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + int?[] source = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; Assert.Equal(5, EnumerablePartitionOrEmpty(source).ElementAtOrDefault(5)); Assert.Equal(5, EnumerablePartitionOrEmpty(source).ElementAtOrDefault(new Index(5))); @@ -330,7 +330,7 @@ public void NonEmptySource_Consistency_EnumerablePartition() [Fact] public void EmptySource_Consistency() { - int?[] source = { }; + int?[] source = []; Assert.Null(source.ElementAtOrDefault(1)); Assert.Null(source.ElementAtOrDefault(-1)); @@ -351,7 +351,7 @@ public void EmptySource_Consistency() [Fact] public void EmptySource_Consistency_NotList() { - int?[] source = { }; + int?[] source = []; Assert.Null(ForceNotCollection(source).ElementAtOrDefault(1)); Assert.Null(ForceNotCollection(source).ElementAtOrDefault(-1)); @@ -372,7 +372,7 @@ public void EmptySource_Consistency_NotList() [Fact] public void EmptySource_Consistency_ListPartition() { - int?[] source = { }; + int?[] source = []; Assert.Null(ListPartitionOrEmpty(source).ElementAtOrDefault(1)); Assert.Null(ListPartitionOrEmpty(source).ElementAtOrDefault(-1)); @@ -393,7 +393,7 @@ public void EmptySource_Consistency_ListPartition() [Fact] public void EmptySource_Consistency_EnumerablePartition() { - int?[] source = { }; + int?[] source = []; Assert.Null(EnumerablePartitionOrEmpty(source).ElementAtOrDefault(1)); Assert.Null(EnumerablePartitionOrEmpty(source).ElementAtOrDefault(-1)); diff --git a/tests/System.Linq.Tests/Tests/System.Linq/ElementAtTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/ElementAtTests.cs index 74a7a1c5..aa8ee00b 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/ElementAtTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/ElementAtTests.cs @@ -33,12 +33,12 @@ public void SameResultsRepeatCallsStringQuery() public static IEnumerable TestData() { - yield return new object[] { NumberRangeGuaranteedNotCollectionType(9, 1), 0, 1, 9 }; - yield return new object[] { NumberRangeGuaranteedNotCollectionType(9, 10), 9, 1, 18 }; - yield return new object[] { NumberRangeGuaranteedNotCollectionType(-4, 10), 3, 7, -1 }; + yield return [NumberRangeGuaranteedNotCollectionType(9, 1), 0, 1, 9]; + yield return [NumberRangeGuaranteedNotCollectionType(9, 10), 9, 1, 18]; + yield return [NumberRangeGuaranteedNotCollectionType(-4, 10), 3, 7, -1]; - yield return new object[] { new int[] { -4 }, 0, 1, -4 }; - yield return new object[] { new int[] { 9, 8, 0, -5, 10 }, 4, 1, 10 }; + yield return [new int[] { -4 }, 0, 1, -4]; + yield return [new int[] { 9, 8, 0, -5, 10 }, 4, 1, 10]; } [Theory] @@ -99,7 +99,7 @@ public void InvalidIndex_ThrowsArgumentOutOfRangeException() [Fact] public void NullableArray_ValidIndex_ReturnsCorrectObject() { - int?[] source = { 9, 8, null, -5, 10 }; + int?[] source = [9, 8, null, -5, 10]; Assert.Null(source.ElementAt(2)); Assert.Equal(-5, source.ElementAt(3)); @@ -127,7 +127,7 @@ public void MutableSource() Assert.Equal(2, source.ElementAt(new Index(2))); Assert.Equal(2, source.ElementAt(^3)); - source.InsertRange(3, new[] { -1, -2 }); + source.InsertRange(3, [-1, -2]); source.RemoveAt(0); Assert.Equal(-1, source.ElementAt(2)); Assert.Equal(-1, source.ElementAt(new Index(2))); @@ -144,7 +144,7 @@ public void MutableSourceNotList() Assert.Equal(2, query1[2].ElementAt(^3)); var query2 = Repeat(_ => ForceNotCollection(source).Select(i => i), 3); - source.InsertRange(3, new[] { -1, -2 }); + source.InsertRange(3, [-1, -2]); source.RemoveAt(0); Assert.Equal(-1, query2[0].ElementAt(2)); Assert.Equal(-1, query2[1].ElementAt(new Index(2))); @@ -193,7 +193,7 @@ public void EnumerateElements() [Fact] public void NonEmptySource_Consistency() { - int[] source = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + int[] source = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; Assert.Equal(5, source.ElementAt(5)); Assert.Equal(5, source.ElementAt(new Index(5))); @@ -224,7 +224,7 @@ public void NonEmptySource_Consistency() [Fact] public void NonEmptySource_Consistency_ThrowsIListIndexerException() { - int[] source = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + int[] source = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; Assert.Throws("index", () => source.ElementAt(-1)); Assert.Throws("index", () => source.ElementAt(^11)); // ImmutableArray implements IList. ElementAt calls ImmutableArray's indexer, which throws IndexOutOfRangeException instead of ArgumentOutOfRangeException. @@ -242,7 +242,7 @@ public void NonEmptySource_Consistency_ThrowsIListIndexerException() [Fact] public void NonEmptySource_Consistency_NotList() { - int[] source = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + int[] source = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; Assert.Equal(5, ForceNotCollection(source).ElementAt(5)); Assert.Equal(5, ForceNotCollection(source).ElementAt(new Index(5))); @@ -275,7 +275,7 @@ public void NonEmptySource_Consistency_NotList() [Fact] public void NonEmptySource_Consistency_ListPartition() { - int[] source = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + int[] source = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; Assert.Equal(5, ListPartitionOrEmpty(source).ElementAt(5)); Assert.Equal(5, ListPartitionOrEmpty(source).ElementAt(new Index(5))); @@ -306,7 +306,7 @@ public void NonEmptySource_Consistency_ListPartition() [Fact] public void NonEmptySource_Consistency_EnumerablePartition() { - int[] source = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + int[] source = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; Assert.Equal(5, EnumerablePartitionOrEmpty(source).ElementAt(5)); Assert.Equal(5, EnumerablePartitionOrEmpty(source).ElementAt(new Index(5))); @@ -337,7 +337,7 @@ public void NonEmptySource_Consistency_EnumerablePartition() [Fact] public void NonEmptySource_Consistency_Collection() { - int[] source = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + int[] source = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; Assert.Equal(5, new TestCollection(source).ElementAt(5)); Assert.Equal(5, new TestCollection(source).ElementAt(new Index(5))); @@ -368,7 +368,7 @@ public void NonEmptySource_Consistency_Collection() [Fact] public void NonEmptySource_Consistency_NonGenericCollection() { - int[] source = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + int[] source = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; Assert.Equal(5, new TestNonGenericCollection(source.ToArray()).ElementAt(5)); Assert.Equal(5, new TestNonGenericCollection(source.ToArray()).ElementAt(new Index(5))); @@ -399,7 +399,7 @@ public void NonEmptySource_Consistency_NonGenericCollection() [Fact] public void EmptySource_Consistency() { - int[] source = { }; + int[] source = []; Assert.Throws("index", () => source.ElementAt(1)); Assert.Throws("index", () => source.ElementAt(-1)); @@ -420,7 +420,7 @@ public void EmptySource_Consistency() [Fact] public void EmptySource_Consistency_ThrowsIListIndexerException() { - int[] source = { }; + int[] source = []; Assert.Throws("index", () => source.ElementAt(-1)); Assert.Throws("index", () => source.ElementAt(^1)); // ImmutableArray implements IList. ElementAt calls ImmutableArray's indexer, which throws IndexOutOfRangeException instead of ArgumentOutOfRangeException. @@ -441,7 +441,7 @@ public void EmptySource_Consistency_ThrowsIListIndexerException() [Fact] public void EmptySource_Consistency_NotList() { - int[] source = { }; + int[] source = []; Assert.Throws("index", () => ForceNotCollection(source).ElementAt(1)); Assert.Throws("index", () => ForceNotCollection(source).ElementAt(-1)); @@ -462,7 +462,7 @@ public void EmptySource_Consistency_NotList() [Fact] public void EmptySource_Consistency_ListPartition() { - int[] source = { }; + int[] source = []; Assert.Throws("index", () => ListPartitionOrEmpty(source).ElementAt(1)); Assert.Throws("index", () => ListPartitionOrEmpty(source).ElementAt(-1)); @@ -483,7 +483,7 @@ public void EmptySource_Consistency_ListPartition() [Fact] public void EmptySource_Consistency_EnumerablePartition() { - int[] source = { }; + int[] source = []; Assert.Throws("index", () => EnumerablePartitionOrEmpty(source).ElementAt(1)); Assert.Throws("index", () => EnumerablePartitionOrEmpty(source).ElementAt(-1)); @@ -504,7 +504,7 @@ public void EmptySource_Consistency_EnumerablePartition() [Fact] public void EmptySource_Consistency_Collection() { - int[] source = { }; + int[] source = []; Assert.Throws("index", () => new TestCollection(source).ElementAt(1)); Assert.Throws("index", () => new TestCollection(source).ElementAt(-1)); @@ -525,7 +525,7 @@ public void EmptySource_Consistency_Collection() [Fact] public void EmptySource_Consistency_NonGenericCollection() { - int[] source = { }; + int[] source = []; Assert.Throws("index", () => new TestNonGenericCollection(source.ToArray()).ElementAt(1)); Assert.Throws("index", () => new TestNonGenericCollection(source.ToArray()).ElementAt(-1)); diff --git a/tests/System.Linq.Tests/Tests/System.Linq/EmptyEnumerable.cs b/tests/System.Linq.Tests/Tests/System.Linq/EmptyEnumerable.cs new file mode 100644 index 00000000..afce451c --- /dev/null +++ b/tests/System.Linq.Tests/Tests/System.Linq/EmptyEnumerable.cs @@ -0,0 +1,73 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Collections.Generic; +using Xunit; + +namespace System.Linq.Tests +{ + public class EmptyEnumerableTest : EnumerableTests + { + private void TestEmptyCached() + { + var enumerable1 = Enumerable.Empty(); + var enumerable2 = Enumerable.Empty(); + + Assert.Same(enumerable1, enumerable2); + } + + [Fact] + public void EmptyEnumerableCachedTest() + { + TestEmptyCached(); + TestEmptyCached(); + TestEmptyCached(); + TestEmptyCached(); + } + + private void TestEmptyEmpty() + { + Assert.Equal(new T[0], Enumerable.Empty()); + Assert.Empty(Enumerable.Empty()); + Assert.Same(Enumerable.Empty().GetEnumerator(), Enumerable.Empty().GetEnumerator()); + } + + [Fact] + public void EmptyEnumerableIsIndeedEmpty() + { + TestEmptyEmpty(); + TestEmptyEmpty(); + TestEmptyEmpty(); + TestEmptyEmpty(); + } + + [Fact] + public void IListImplementationIsValid() + { + IList list = Assert.IsAssignableFrom>(Enumerable.Empty()); + IReadOnlyList roList = Assert.IsAssignableFrom>(Enumerable.Empty()); + + Assert.Throws(() => list.Add(42)); + Assert.Throws(() => list.Insert(0, 42)); + Assert.Throws(() => list.Clear()); + Assert.Throws(() => list.Remove(42)); + Assert.Throws(() => list.RemoveAt(0)); + AssertExtensions.Throws("index", () => list[0] = 42); + AssertExtensions.Throws("index", () => list[0]); + AssertExtensions.Throws("index", () => roList[0]); + + Assert.True(list.IsReadOnly); + Assert.Empty(list); + Assert.Empty(roList); + + Assert.False(list.Contains(42)); + Assert.Equal(-1, list.IndexOf(42)); + + list.CopyTo([], 0); + AssertExtensions.Throws("destinationArray", () => list.CopyTo([], 1)); + int[] array = [42]; + list.CopyTo(array, 0); + Assert.Equal(42, array[0]); + } + } +} diff --git a/tests/System.Linq.Tests/Tests/System.Linq/ExceptTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/ExceptTests.cs index ed700b69..195b8ae4 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/ExceptTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/ExceptTests.cs @@ -32,10 +32,10 @@ public void SameResultsRepeatCallsStringQuery() public static IEnumerable Int_TestData() { - yield return new object[] { new int[0], new int[0], null, new int[0] }; - yield return new object[] { new int[0], new int[] { -6, -8, -6, 2, 0, 0, 5, 6 }, null, new int[0] }; + yield return [new int[0], new int[0], null, new int[0]]; + yield return [new int[0], new int[] { -6, -8, -6, 2, 0, 0, 5, 6 }, null, new int[0]]; - yield return new object[] { new int[] { 1, 1, 1, 1, 1 }, new int[] { 2, 3, 4 }, null, new int[] { 1 } }; + yield return [new int[] { 1, 1, 1, 1, 1 }, new int[] { 2, 3, 4 }, null, new int[] { 1 }]; } [Theory] @@ -52,12 +52,11 @@ public void Int(IEnumerable first, IEnumerable second, IEqualityCompar public static IEnumerable String_TestData() { IEqualityComparer defaultComparer = EqualityComparer.Default; - yield return new object[] { new string[1], new string[0], defaultComparer, new string[1] }; - yield return new object[] { new string[] { null, null, string.Empty }, new string[1], defaultComparer, new string[] { string.Empty } }; - yield return new object[] { new string[2], new string[0], defaultComparer, new string[1] }; - - yield return new object[] { new string[] { "Bob", "Tim", "Robert", "Chris" }, new string[] { "bBo", "shriC" }, null, new string[] { "Bob", "Tim", "Robert", "Chris" } }; - yield return new object[] { new string[] { "Bob", "Tim", "Robert", "Chris" }, new string[] { "bBo", "shriC" }, new AnagramEqualityComparer(), new string[] { "Tim", "Robert" } }; + yield return [new string[1], new string[0], defaultComparer, new string[1]]; + yield return [new string[] { null, null, string.Empty }, new string[1], defaultComparer, new string[] { string.Empty }]; + yield return [new string[2], new string[0], defaultComparer, new string[1]]; + yield return [new string[] { "Bob", "Tim", "Robert", "Chris" }, new string[] { "bBo", "shriC" }, null, new string[] { "Bob", "Tim", "Robert", "Chris" }]; + yield return [new string[] { "Bob", "Tim", "Robert", "Chris" }, new string[] { "bBo", "shriC" }, new AnagramEqualityComparer(), new string[] { "Tim", "Robert" }]; } [Theory] @@ -73,10 +72,9 @@ public void String(IEnumerable first, IEnumerable second, IEqual public static IEnumerable NullableInt_TestData() { - yield return new object[] { new int?[] { -6, -8, -6, 2, 0, 0, 5, 6, null, null }, new int?[0], new int?[] { -6, -8, 2, 0, 5, 6, null } }; - - yield return new object[] { new int?[] { 1, 2, 2, 3, 4, 5 }, new int?[] { 5, 3, 2, 6, 6, 3, 1, null, null }, new int?[] { 4 } }; - yield return new object[] { new int?[] { 2, 3, null, 2, null, 4, 5 }, new int?[] { 1, 9, null, 4 }, new int?[] { 2, 3, 5 } }; + yield return [new int?[] { -6, -8, -6, 2, 0, 0, 5, 6, null, null }, new int?[0], new int?[] { -6, -8, 2, 0, 5, 6, null }]; + yield return [new int?[] { 1, 2, 2, 3, 4, 5 }, new int?[] { 5, 3, 2, 6, 6, 3, 1, null, null }, new int?[] { 4 }]; + yield return [new int?[] { 2, 3, null, 2, null, 4, 5 }, new int?[] { 1, 9, null, 4 }, new int?[] { 2, 3, 5 }]; } [Theory] @@ -97,7 +95,7 @@ public void NullableIntRunOnce(IEnumerable first, IEnumerable second public void FirstNull_ThrowsArgumentNullException() { string[] first = null; - string[] second = { "bBo", "shriC" }; + string[] second = ["bBo", "shriC"]; AssertExtensions.Throws("first", () => first.Except(second)); AssertExtensions.Throws("first", () => first.Except(second, new AnagramEqualityComparer())); @@ -106,7 +104,7 @@ public void FirstNull_ThrowsArgumentNullException() [Fact] public void SecondNull_ThrowsArgumentNullException() { - string[] first = { "Bob", "Tim", "Robert", "Chris" }; + string[] first = ["Bob", "Tim", "Robert", "Chris"]; string[] second = null; AssertExtensions.Throws("second", () => first.Except(second)); @@ -126,24 +124,24 @@ public void ForcedToEnumeratorDoesntEnumerate() public void HashSetWithBuiltInComparer_HashSetContainsNotUsed() { IEnumerable input1 = new HashSet(StringComparer.OrdinalIgnoreCase) { "a" }; - IEnumerable input2 = new[] { "A" }; + IEnumerable input2 = ["A"]; - Assert.Equal(new[] { "a" }, input1.Except(input2)); - Assert.Equal(new[] { "a" }, input1.Except(input2, null)); - Assert.Equal(new[] { "a" }, input1.Except(input2, EqualityComparer.Default)); - Assert.Equal(Enumerable.Empty(), input1.Except(input2, StringComparer.OrdinalIgnoreCase)); + Assert.Equal(["a"], input1.Except(input2)); + Assert.Equal(["a"], input1.Except(input2, null)); + Assert.Equal(["a"], input1.Except(input2, EqualityComparer.Default)); + Assert.Equal([], input1.Except(input2, StringComparer.OrdinalIgnoreCase)); - Assert.Equal(new[] { "A" }, input2.Except(input1)); - Assert.Equal(new[] { "A" }, input2.Except(input1, null)); - Assert.Equal(new[] { "A" }, input2.Except(input1, EqualityComparer.Default)); - Assert.Equal(Enumerable.Empty(), input2.Except(input1, StringComparer.OrdinalIgnoreCase)); + Assert.Equal(["A"], input2.Except(input1)); + Assert.Equal(["A"], input2.Except(input1, null)); + Assert.Equal(["A"], input2.Except(input1, EqualityComparer.Default)); + Assert.Equal([], input2.Except(input1, StringComparer.OrdinalIgnoreCase)); } [Fact] public void ExceptBy_FirstNull_ThrowsArgumentNullException() { string[] first = null; - string[] second = { "bBo", "shriC" }; + string[] second = ["bBo", "shriC"]; AssertExtensions.Throws("first", () => first.ExceptBy(second, x => x)); AssertExtensions.Throws("first", () => first.ExceptBy(second, x => x, new AnagramEqualityComparer())); @@ -152,7 +150,7 @@ public void ExceptBy_FirstNull_ThrowsArgumentNullException() [Fact] public void ExceptBy_SecondNull_ThrowsArgumentNullException() { - string[] first = { "Bob", "Tim", "Robert", "Chris" }; + string[] first = ["Bob", "Tim", "Robert", "Chris"]; string[] second = null; AssertExtensions.Throws("second", () => first.ExceptBy(second, x => x)); @@ -162,8 +160,8 @@ public void ExceptBy_SecondNull_ThrowsArgumentNullException() [Fact] public void ExceptBy_KeySelectorNull_ThrowsArgumentNullException() { - string[] first = { "Bob", "Tim", "Robert", "Chris" }; - string[] second = { "bBo", "shriC" }; + string[] first = ["Bob", "Tim", "Robert", "Chris"]; + string[] second = ["bBo", "shriC"]; Func keySelector = null; AssertExtensions.Throws("keySelector", () => first.ExceptBy(second, keySelector)); @@ -195,7 +193,7 @@ public static IEnumerable ExceptBy_TestData() yield return WrapArgs( first: Enumerable.Repeat(5, 20), - second: Enumerable.Empty(), + second: [], keySelector: x => x, comparer: null, expected: Enumerable.Repeat(5, 1)); @@ -205,45 +203,45 @@ public static IEnumerable ExceptBy_TestData() second: Enumerable.Repeat(5, 3), keySelector: x => x, comparer: null, - expected: Enumerable.Empty()); + expected: []); yield return WrapArgs( - first: new string[] { "Bob", "Tim", "Robert", "Chris" }, - second: new string[] { "bBo", "shriC" }, + first: ["Bob", "Tim", "Robert", "Chris"], + second: ["bBo", "shriC"], keySelector: x => x, null, - expected: new string[] { "Bob", "Tim", "Robert", "Chris" }); + expected: ["Bob", "Tim", "Robert", "Chris"]); yield return WrapArgs( - first: new string[] { "Bob", "Tim", "Robert", "Chris" }, - second: new string[] { "bBo", "shriC" }, + first: ["Bob", "Tim", "Robert", "Chris"], + second: ["bBo", "shriC"], keySelector: x => x, new AnagramEqualityComparer(), - expected: new string[] { "Tim", "Robert" }); + expected: ["Tim", "Robert"]); yield return WrapArgs( first: new (string Name, int Age)[] { ("Tom", 20), ("Dick", 30), ("Harry", 40) }, - second: new int[] { 15, 20, 40 }, + second: [15, 20, 40], keySelector: x => x.Age, comparer: null, expected: new (string Name, int Age)[] { ("Dick", 30) }); yield return WrapArgs( first: new (string Name, int Age)[] { ("Tom", 20), ("Dick", 30), ("Harry", 40) }, - second: new string[] { "moT" }, + second: ["moT"], keySelector: x => x.Name, comparer: null, expected: new (string Name, int Age)[] { ("Tom", 20), ("Dick", 30), ("Harry", 40) }); yield return WrapArgs( first: new (string Name, int Age)[] { ("Tom", 20), ("Dick", 30), ("Harry", 40) }, - second: new string[] { "moT" }, + second: ["moT"], keySelector: x => x.Name, comparer: new AnagramEqualityComparer(), expected: new (string Name, int Age)[] { ("Dick", 30), ("Harry", 40) }); object[] WrapArgs(IEnumerable first, IEnumerable second, Func keySelector, IEqualityComparer? comparer, IEnumerable expected) - => new object[] { first, second, keySelector, comparer, expected }; + => [first, second, keySelector, comparer, expected]; } } } diff --git a/tests/System.Linq.Tests/Tests/System.Linq/FirstOrDefaultTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/FirstOrDefaultTests.cs index b0d6525f..2c48df88 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/FirstOrDefaultTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/FirstOrDefaultTests.cs @@ -31,7 +31,7 @@ public void SameResultsRepeatCallsStringQuery() private static void TestEmptyIList() { - T[] source = { }; + T[] source = []; T expected = default(T); Assert.IsAssignableFrom>(source); @@ -41,7 +41,7 @@ private static void TestEmptyIList() private static void TestEmptyIListDefault(T defaultValue) { - T[] source = { }; + T[] source = []; Assert.IsAssignableFrom>(source); @@ -68,7 +68,7 @@ public void EmptyIListDefault() [Fact] public void IListTOneElement() { - int[] source = { 5 }; + int[] source = [5]; int expected = 5; Assert.IsAssignableFrom>(source); @@ -79,7 +79,7 @@ public void IListTOneElement() [Fact] public void IListOneElementDefault() { - int[] source = { 5 }; + int[] source = [5]; int expected = 5; Assert.IsAssignableFrom>(source); @@ -90,7 +90,7 @@ public void IListOneElementDefault() [Fact] public void IListTManyElementsFirstIsDefault() { - int?[] source = { null, -10, 2, 4, 3, 0, 2 }; + int?[] source = [null, -10, 2, 4, 3, 0, 2]; int? expected = null; Assert.IsAssignableFrom>(source); @@ -101,7 +101,7 @@ public void IListTManyElementsFirstIsDefault() [Fact] public void IListTManyElementsFirstIsNotDefault() { - int?[] source = { 19, null, -10, 2, 4, 3, 0, 2 }; + int?[] source = [19, null, -10, 2, 4, 3, 0, 2]; int? expected = 19; Assert.IsAssignableFrom>(source); @@ -172,7 +172,7 @@ public void ManyElementsNotIListT() [Fact] public void EmptySource() { - int?[] source = { }; + int?[] source = []; Assert.All(CreateSources(source), source => { @@ -184,7 +184,7 @@ public void EmptySource() [Fact] public void OneElementTruePredicate() { - int[] source = { 4 }; + int[] source = [4]; Func predicate = IsEven; int expected = 4; @@ -197,7 +197,7 @@ public void OneElementTruePredicate() [Fact] public void OneElementTruePredicateDefault() { - int[] source = { 4 }; + int[] source = [4]; Func predicate = IsEven; int expected = 4; @@ -210,7 +210,7 @@ public void OneElementTruePredicateDefault() [Fact] public void ManyElementsPredicateFalseForAll() { - int[] source = { 9, 5, 1, 3, 17, 21 }; + int[] source = [9, 5, 1, 3, 17, 21]; Func predicate = IsEven; int expected = default(int); @@ -223,7 +223,7 @@ public void ManyElementsPredicateFalseForAll() [Fact] public void ManyElementsPredicateFalseForAllDefault() { - int[] source = { 9, 5, 1, 3, 17, 21 }; + int[] source = [9, 5, 1, 3, 17, 21]; Func predicate = IsEven; int expected = 5; @@ -236,7 +236,7 @@ public void ManyElementsPredicateFalseForAllDefault() [Fact] public void PredicateTrueOnlyForLast() { - int[] source = { 9, 5, 1, 3, 17, 21, 50 }; + int[] source = [9, 5, 1, 3, 17, 21, 50]; Func predicate = IsEven; int expected = 50; @@ -249,7 +249,7 @@ public void PredicateTrueOnlyForLast() [Fact] public void PredicateTrueOnlyForLastDefault() { - int[] source = { 9, 5, 1, 3, 17, 21, 50 }; + int[] source = [9, 5, 1, 3, 17, 21, 50]; Func predicate = IsEven; int expected = 50; @@ -262,7 +262,7 @@ public void PredicateTrueOnlyForLastDefault() [Fact] public void PredicateTrueForSome() { - int[] source = { 3, 7, 10, 7, 9, 2, 11, 17, 13, 8 }; + int[] source = [3, 7, 10, 7, 9, 2, 11, 17, 13, 8]; Func predicate = IsEven; int expected = 10; @@ -275,7 +275,7 @@ public void PredicateTrueForSome() [Fact] public void PredicateTrueForSomeDefault() { - int[] source = { 3, 7, 10, 7, 9, 2, 11, 17, 13, 8 }; + int[] source = [3, 7, 10, 7, 9, 2, 11, 17, 13, 8]; Func predicate = IsEven; int expected = 10; @@ -288,7 +288,7 @@ public void PredicateTrueForSomeDefault() [Fact] public void PredicateTrueForSomeRunOnce() { - int[] source = { 3, 7, 10, 7, 9, 2, 11, 17, 13, 8 }; + int[] source = [3, 7, 10, 7, 9, 2, 11, 17, 13, 8]; Func predicate = IsEven; int expected = 10; diff --git a/tests/System.Linq.Tests/Tests/System.Linq/FirstTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/FirstTests.cs index 6b5b1bfe..d9217a41 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/FirstTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/FirstTests.cs @@ -31,7 +31,7 @@ public void SameResultsRepeatCallsStringQuery() private static void TestEmptyIList() { - T[] source = { }; + T[] source = []; Assert.NotNull(source as IList); @@ -50,7 +50,7 @@ public void EmptyIListT() [Fact] public void IListTOneElement() { - int[] source = { 5 }; + int[] source = [5]; int expected = 5; Assert.NotNull(source as IList); @@ -61,7 +61,7 @@ public void IListTOneElement() [Fact] public void IListTManyElementsFirstIsDefault() { - int?[] source = { null, -10, 2, 4, 3, 0, 2 }; + int?[] source = [null, -10, 2, 4, 3, 0, 2]; int? expected = null; Assert.IsAssignableFrom>(source); @@ -72,7 +72,7 @@ public void IListTManyElementsFirstIsDefault() [Fact] public void IListTManyElementsFirstIsNotDefault() { - int?[] source = { 19, null, -10, 2, 4, 3, 0, 2 }; + int?[] source = [19, null, -10, 2, 4, 3, 0, 2]; int? expected = 19; Assert.IsAssignableFrom>(source); @@ -128,7 +128,7 @@ public void ManyElementsNotIListT() [Fact] public void EmptySource() { - int[] source = { }; + int[] source = []; Assert.All(CreateSources(source), source => { Assert.Throws(() => source.First(x => true)); @@ -139,7 +139,7 @@ public void EmptySource() [Fact] public void OneElementTruePredicate() { - int[] source = { 4 }; + int[] source = [4]; Func predicate = IsEven; int expected = 4; @@ -152,7 +152,7 @@ public void OneElementTruePredicate() [Fact] public void ManyElementsPredicateFalseForAll() { - int[] source = { 9, 5, 1, 3, 17, 21 }; + int[] source = [9, 5, 1, 3, 17, 21]; Func predicate = IsEven; Assert.All(CreateSources(source), source => @@ -164,7 +164,7 @@ public void ManyElementsPredicateFalseForAll() [Fact] public void PredicateTrueOnlyForLast() { - int[] source = { 9, 5, 1, 3, 17, 21, 50 }; + int[] source = [9, 5, 1, 3, 17, 21, 50]; Func predicate = IsEven; int expected = 50; @@ -177,7 +177,7 @@ public void PredicateTrueOnlyForLast() [Fact] public void PredicateTrueForSome() { - int[] source = { 3, 7, 10, 7, 9, 2, 11, 17, 13, 8 }; + int[] source = [3, 7, 10, 7, 9, 2, 11, 17, 13, 8]; Func predicate = IsEven; int expected = 10; @@ -187,7 +187,7 @@ public void PredicateTrueForSome() [Fact] public void PredicateTrueForSomeRunOnce() { - int[] source = { 3, 7, 10, 7, 9, 2, 11, 17, 13, 8 }; + int[] source = [3, 7, 10, 7, 9, 2, 11, 17, 13, 8]; Func predicate = IsEven; int expected = 10; diff --git a/tests/System.Linq.Tests/Tests/System.Linq/GroupByTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/GroupByTests.cs index 0a0ecd91..342f911c 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/GroupByTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/GroupByTests.cs @@ -27,7 +27,7 @@ private static void AssertGroupingCorrect(IEnumerable keys Assert.NotNull(keys); Dictionary> dict = new Dictionary>(keyComparer); - List groupingForNullKeys = new List(); + List groupingForNullKeys = []; using (IEnumerator elEn = elements.GetEnumerator()) using (IEnumerator keyEn = keys.GetEnumerator()) { @@ -45,7 +45,7 @@ private static void AssertGroupingCorrect(IEnumerable keys { List list; if (!dict.TryGetValue(key, out list)) - dict.Add(key, list = new List()); + dict.Add(key, list = []); list.Add(elEn.Current); } } @@ -195,8 +195,8 @@ public void Grouping_IList_IndexOf() [Fact] public void SingleNullKeySingleNullElement() { - string[] key = { null }; - string[] element = { null }; + string[] key = [null]; + string[] element = [null]; AssertGroupingCorrect(key, element, new string[] { null }.GroupBy(e => e, e => e, EqualityComparer.Default), EqualityComparer.Default); } @@ -204,18 +204,18 @@ public void SingleNullKeySingleNullElement() [Fact] public void EmptySource() { - string[] key = { }; - int[] element = { }; - Record[] source = { }; + string[] key = []; + int[] element = []; + Record[] source = []; Assert.Empty(new Record[] { }.GroupBy(e => e.Name, e => e.Score, new AnagramEqualityComparer())); } [Fact] public void EmptySourceRunOnce() { - string[] key = { }; - int[] element = { }; - Record[] source = { }; + string[] key = []; + int[] element = []; + Record[] source = []; Assert.Empty(new Record[] { }.RunOnce().GroupBy(e => e.Name, e => e.Score, new AnagramEqualityComparer())); } @@ -251,15 +251,15 @@ public void SourceIsNullResultSelectorUsedNoComparerOrElementSelector() [Fact] public void KeySelectorNull() { - Record[] source = new[] - { + Record[] source = + [ new Record { Name = "Tim", Score = 55 }, new Record { Name = "Chris", Score = 49 }, new Record { Name = "Robert", Score = -100 }, new Record { Name = "Chris", Score = 24 }, new Record { Name = "Prakash", Score = 9 }, new Record { Name = "Tim", Score = 25 } - }; + ]; AssertExtensions.Throws("keySelector", () => source.GroupBy(null, e => e.Score, new AnagramEqualityComparer())); AssertExtensions.Throws("keySelector", () => source.GroupBy(null, new AnagramEqualityComparer())); @@ -268,15 +268,15 @@ public void KeySelectorNull() [Fact] public void KeySelectorNullResultSelectorUsed() { - Record[] source = new[] - { + Record[] source = + [ new Record { Name = "Tim", Score = 55 }, new Record { Name = "Chris", Score = 49 }, new Record { Name = "Robert", Score = -100 }, new Record { Name = "Chris", Score = 24 }, new Record { Name = "Prakash", Score = 9 }, new Record { Name = "Tim", Score = 25 } - }; + ]; AssertExtensions.Throws("keySelector", () => source.GroupBy(null, e => e.Score, (k, es) => es.Sum(), new AnagramEqualityComparer())); } @@ -284,15 +284,15 @@ public void KeySelectorNullResultSelectorUsed() [Fact] public void KeySelectorNullResultSelectorUsedNoComparer() { - Record[] source = new[] - { + Record[] source = + [ new Record { Name = "Tim", Score = 55 }, new Record { Name = "Chris", Score = 49 }, new Record { Name = "Robert", Score = -100 }, new Record { Name = "Chris", Score = 24 }, new Record { Name = "Prakash", Score = 9 }, new Record { Name = "Tim", Score = 25 } - }; + ]; Func keySelector = null; @@ -302,8 +302,8 @@ public void KeySelectorNullResultSelectorUsedNoComparer() [Fact] public void KeySelectorNullResultSelectorUsedNoElementSelector() { - string[] key = { "Tim", "Tim", "Tim", "Tim" }; - int[] element = { 60, -10, 40, 100 }; + string[] key = ["Tim", "Tim", "Tim", "Tim"]; + int[] element = [60, -10, 40, 100]; var source = key.Zip(element, (k, e) => new Record { Name = k, Score = e }); AssertExtensions.Throws("keySelector", () => source.GroupBy(null, (k, es) => es.Sum(e => e.Score), new AnagramEqualityComparer())); @@ -312,15 +312,15 @@ public void KeySelectorNullResultSelectorUsedNoElementSelector() [Fact] public void ElementSelectorNull() { - Record[] source = new[] - { + Record[] source = + [ new Record { Name = "Tim", Score = 55 }, new Record { Name = "Chris", Score = 49 }, new Record { Name = "Robert", Score = -100 }, new Record { Name = "Chris", Score = 24 }, new Record { Name = "Prakash", Score = 9 }, new Record { Name = "Tim", Score = 25 } - }; + ]; Func elementSelector = null; @@ -330,15 +330,15 @@ public void ElementSelectorNull() [Fact] public void ElementSelectorNullResultSelectorUsedNoComparer() { - Record[] source = new[] - { + Record[] source = + [ new Record { Name = "Tim", Score = 55 }, new Record { Name = "Chris", Score = 49 }, new Record { Name = "Robert", Score = -100 }, new Record { Name = "Chris", Score = 24 }, new Record { Name = "Prakash", Score = 9 }, new Record { Name = "Tim", Score = 25 } - }; + ]; Func elementSelector = null; @@ -348,14 +348,15 @@ public void ElementSelectorNullResultSelectorUsedNoComparer() [Fact] public void ResultSelectorNull() { - Record[] source = { + Record[] source = + [ new Record { Name = "Tim", Score = 55 }, new Record { Name = "Chris", Score = 49 }, new Record { Name = "Robert", Score = -100 }, new Record { Name = "Chris", Score = 24 }, new Record { Name = "Prakash", Score = 9 }, new Record { Name = "Tim", Score = 25 } - }; + ]; Func, long> resultSelector = null; @@ -365,14 +366,15 @@ public void ResultSelectorNull() [Fact] public void ResultSelectorNullNoComparer() { - Record[] source = { + Record[] source = + [ new Record { Name = "Tim", Score = 55 }, new Record { Name = "Chris", Score = 49 }, new Record { Name = "Robert", Score = -100 }, new Record { Name = "Chris", Score = 24 }, new Record { Name = "Prakash", Score = 9 }, new Record { Name = "Tim", Score = 25 } - }; + ]; Func, long> resultSelector = null; @@ -382,14 +384,15 @@ public void ResultSelectorNullNoComparer() [Fact] public void ResultSelectorNullNoElementSelector() { - Record[] source = { + Record[] source = + [ new Record { Name = "Tim", Score = 55 }, new Record { Name = "Chris", Score = 49 }, new Record { Name = "Robert", Score = -100 }, new Record { Name = "Chris", Score = 24 }, new Record { Name = "Prakash", Score = 9 }, new Record { Name = "Tim", Score = 25 } - }; + ]; Func, long> resultSelector = null; @@ -399,8 +402,8 @@ public void ResultSelectorNullNoElementSelector() [Fact] public void ResultSelectorNullNoElementSelectorCustomComparer() { - string[] key = { "Tim", "Tim", "Tim", "Tim" }; - int[] element = { 60, -10, 40, 100 }; + string[] key = ["Tim", "Tim", "Tim", "Tim"]; + int[] element = [60, -10, 40, 100]; var source = key.Zip(element, (k, e) => new Record { Name = k, Score = e }); Func, long> resultSelector = null; @@ -411,26 +414,27 @@ public void ResultSelectorNullNoElementSelectorCustomComparer() [Fact] public void EmptySourceWithResultSelector() { - string[] key = { }; - int[] element = { }; - Record[] source = { }; + string[] key = []; + int[] element = []; + Record[] source = []; Assert.Empty(new Record[] { }.GroupBy(e => e.Name, e => e.Score, (k, es) => (long)(k ?? " ").Length * es.Sum(), new AnagramEqualityComparer())); } [Fact] public void DuplicateKeysCustomComparer() { - string[] key = { "Tim", "Tim", "Chris", "Chris", "Robert", "Prakash" }; - int[] element = { 55, 25, 49, 24, -100, 9 }; - Record[] source = { + string[] key = ["Tim", "Tim", "Chris", "Chris", "Robert", "Prakash"]; + int[] element = [55, 25, 49, 24, -100, 9]; + Record[] source = + [ new Record { Name = "Tim", Score = 55 }, new Record { Name = "Chris", Score = 49 }, new Record { Name = "Robert", Score = -100 }, new Record { Name = "Chris", Score = 24 }, new Record { Name = "Prakash", Score = 9 }, new Record { Name = "miT", Score = 25 } - }; - long[] expected = { 240, 365, -600, 63 }; + ]; + long[] expected = [240, 365, -600, 63]; Assert.Equal(expected, source.GroupBy(e => e.Name, e => e.Score, (k, es) => (long)(k ?? " ").Length * es.Sum(), new AnagramEqualityComparer())); } @@ -438,17 +442,18 @@ public void DuplicateKeysCustomComparer() [Fact] public void DuplicateKeysCustomComparerRunOnce() { - string[] key = { "Tim", "Tim", "Chris", "Chris", "Robert", "Prakash" }; - int[] element = { 55, 25, 49, 24, -100, 9 }; - Record[] source = { + string[] key = ["Tim", "Tim", "Chris", "Chris", "Robert", "Prakash"]; + int[] element = [55, 25, 49, 24, -100, 9]; + Record[] source = + [ new Record { Name = "Tim", Score = 55 }, new Record { Name = "Chris", Score = 49 }, new Record { Name = "Robert", Score = -100 }, new Record { Name = "Chris", Score = 24 }, new Record { Name = "Prakash", Score = 9 }, new Record { Name = "miT", Score = 25 } - }; - long[] expected = { 240, 365, -600, 63 }; + ]; + long[] expected = [240, 365, -600, 63]; Assert.Equal(expected, source.RunOnce().GroupBy(e => e.Name, e => e.Score, (k, es) => (long)(k ?? " ").Length * es.Sum(), new AnagramEqualityComparer())); } @@ -456,17 +461,18 @@ public void DuplicateKeysCustomComparerRunOnce() [Fact] public void NullComparer() { - string[] key = { "Tim", null, null, "Robert", "Chris", "miT" }; - int[] element = { 55, 49, 9, -100, 24, 25 }; - Record[] source = { + string[] key = ["Tim", null, null, "Robert", "Chris", "miT"]; + int[] element = [55, 49, 9, -100, 24, 25]; + Record[] source = + [ new Record { Name = "Tim", Score = 55 }, new Record { Name = null, Score = 49 }, new Record { Name = "Robert", Score = -100 }, new Record { Name = "Chris", Score = 24 }, new Record { Name = null, Score = 9 }, new Record { Name = "miT", Score = 25 } - }; - long[] expected = { 165, 58, -600, 120, 75 }; + ]; + long[] expected = [165, 58, -600, 120, 75]; Assert.Equal(expected, source.GroupBy(e => e.Name, e => e.Score, (k, es) => (long)(k ?? " ").Length * es.Sum(), null)); } @@ -474,17 +480,18 @@ public void NullComparer() [Fact] public void NullComparerRunOnce() { - string[] key = { "Tim", null, null, "Robert", "Chris", "miT" }; - int[] element = { 55, 49, 9, -100, 24, 25 }; - Record[] source = { + string[] key = ["Tim", null, null, "Robert", "Chris", "miT"]; + int[] element = [55, 49, 9, -100, 24, 25]; + Record[] source = + [ new Record { Name = "Tim", Score = 55 }, new Record { Name = null, Score = 49 }, new Record { Name = "Robert", Score = -100 }, new Record { Name = "Chris", Score = 24 }, new Record { Name = null, Score = 9 }, new Record { Name = "miT", Score = 25 } - }; - long[] expected = { 165, 58, -600, 120, 75 }; + ]; + long[] expected = [165, 58, -600, 120, 75]; Assert.Equal(expected, source.RunOnce().GroupBy(e => e.Name, e => e.Score, (k, es) => (long)(k ?? " ").Length * es.Sum(), null)); } @@ -492,8 +499,8 @@ public void NullComparerRunOnce() [Fact] public void SingleNonNullElement() { - string[] key = { "Tim" }; - Record[] source = { new Record { Name = key[0], Score = 60 } }; + string[] key = ["Tim"]; + Record[] source = [new Record { Name = key[0], Score = 60 }]; AssertGroupingCorrect(key, source, source.GroupBy(e => e.Name)); } @@ -501,8 +508,8 @@ public void SingleNonNullElement() [Fact] public void AllElementsSameKey() { - string[] key = { "Tim", "Tim", "Tim", "Tim" }; - int[] scores = { 60, -10, 40, 100 }; + string[] key = ["Tim", "Tim", "Tim", "Tim"]; + int[] scores = [60, -10, 40, 100]; var source = key.Zip(scores, (k, e) => new Record { Name = k, Score = e }); AssertGroupingCorrect(key, source, source.GroupBy(e => e.Name, new AnagramEqualityComparer()), new AnagramEqualityComparer()); @@ -511,8 +518,8 @@ public void AllElementsSameKey() [Fact] public void AllElementsDifferentKeyElementSelectorUsed() { - string[] key = { "Tim", "Chris", "Robert", "Prakash" }; - int[] element = { 60, -10, 40, 100 }; + string[] key = ["Tim", "Chris", "Robert", "Prakash"]; + int[] element = [60, -10, 40, 100]; var source = key.Zip(element, (k, e) => new Record { Name = k, Score = e }); AssertGroupingCorrect(key, element, source.GroupBy(e => e.Name, e => e.Score)); @@ -521,8 +528,8 @@ public void AllElementsDifferentKeyElementSelectorUsed() [Fact] public void SomeDuplicateKeys() { - string[] key = { "Tim", "Tim", "Chris", "Chris", "Robert", "Prakash" }; - int[] element = { 55, 25, 49, 24, -100, 9 }; + string[] key = ["Tim", "Tim", "Chris", "Chris", "Robert", "Prakash"]; + int[] element = [55, 25, 49, 24, -100, 9]; var source = key.Zip(element, (k, e) => new Record { Name = k, Score = e }); AssertGroupingCorrect(key, element, source.GroupBy(e => e.Name, e => e.Score)); @@ -531,8 +538,8 @@ public void SomeDuplicateKeys() [Fact] public void SomeDuplicateKeysIncludingNulls() { - string[] key = { null, null, "Chris", "Chris", "Prakash", "Prakash" }; - int[] element = { 55, 25, 49, 24, 9, 9 }; + string[] key = [null, null, "Chris", "Chris", "Prakash", "Prakash"]; + int[] element = [55, 25, 49, 24, 9, 9]; var source = key.Zip(element, (k, e) => new Record { Name = k, Score = e }); AssertGroupingCorrect(key, element, source.GroupBy(e => e.Name, e => e.Score)); @@ -541,9 +548,9 @@ public void SomeDuplicateKeysIncludingNulls() [Fact] public void SingleElementResultSelectorUsed() { - string[] key = { "Tim" }; - int[] element = { 60 }; - long[] expected = { 180 }; + string[] key = ["Tim"]; + int[] element = [60]; + long[] expected = [180]; var source = key.Zip(element, (k, e) => new Record { Name = k, Score = e }); Assert.Equal(expected, source.GroupBy(e => e.Name, (k, es) => (long)(k ?? " ").Length * es.Sum(e => e.Score))); @@ -568,10 +575,10 @@ public void GroupedResultCorrectSize() [Fact] public void AllElementsDifferentKeyElementSelectorUsedResultSelector() { - string[] key = { "Tim", "Chris", "Robert", "Prakash" }; - int[] element = { 60, -10, 40, 100 }; + string[] key = ["Tim", "Chris", "Robert", "Prakash"]; + int[] element = [60, -10, 40, 100]; var source = key.Zip(element, (k, e) => new Record { Name = k, Score = e }); - long[] expected = { 180, -50, 240, 700 }; + long[] expected = [180, -50, 240, 700]; Assert.Equal(expected, source.GroupBy(e => e.Name, e => e.Score, (k, es) => (long)(k ?? " ").Length * es.Sum())); } @@ -579,14 +586,15 @@ public void AllElementsDifferentKeyElementSelectorUsedResultSelector() [Fact] public void AllElementsSameKeyResultSelectorUsed() { - int[] element = { 60, -10, 40, 100 }; - long[] expected = { 570 }; - Record[] source = { + int[] element = [60, -10, 40, 100]; + long[] expected = [570]; + Record[] source = + [ new Record { Name = "Tim", Score = element[0] }, new Record { Name = "Tim", Score = element[1] }, new Record { Name = "miT", Score = element[2] }, new Record { Name = "miT", Score = element[3] } - }; + ]; Assert.Equal(expected, source.GroupBy(e => e.Name, (k, es) => k.Length * es.Sum(e => (long)e.Score), new AnagramEqualityComparer())); } @@ -594,15 +602,16 @@ public void AllElementsSameKeyResultSelectorUsed() [Fact] public void NullComparerResultSelectorUsed() { - int[] element = { 60, -10, 40, 100 }; - Record[] source = { + int[] element = [60, -10, 40, 100]; + Record[] source = + [ new Record { Name = "Tim", Score = element[0] }, new Record { Name = "Tim", Score = element[1] }, new Record { Name = "miT", Score = element[2] }, - new Record { Name = "miT", Score = element[3] }, - }; + new Record { Name = "miT", Score = element[3] } + ]; - long[] expected = { 150, 420 }; + long[] expected = [150, 420]; Assert.Equal(expected, source.GroupBy(e => e.Name, (k, es) => k.Length * es.Sum(e => (long)e.Score), null)); } @@ -610,15 +619,15 @@ public void NullComparerResultSelectorUsed() [Fact] public void GroupingToArray() { - Record[] source = new Record[] - { + Record[] source = + [ new Record{ Name = "Tim", Score = 55 }, new Record{ Name = "Chris", Score = 49 }, new Record{ Name = "Robert", Score = -100 }, new Record{ Name = "Chris", Score = 24 }, new Record{ Name = "Prakash", Score = 9 }, new Record{ Name = "Tim", Score = 25 } - }; + ]; IGrouping[] groupedArray = source.GroupBy(r => r.Name).ToArray(); Assert.Equal(4, groupedArray.Length); @@ -628,15 +637,15 @@ public void GroupingToArray() [Fact] public void GroupingWithElementSelectorToArray() { - Record[] source = new Record[] - { + Record[] source = + [ new Record{ Name = "Tim", Score = 55 }, new Record{ Name = "Chris", Score = 49 }, new Record{ Name = "Robert", Score = -100 }, new Record{ Name = "Chris", Score = 24 }, new Record{ Name = "Prakash", Score = 9 }, new Record{ Name = "Tim", Score = 25 } - }; + ]; IGrouping[] groupedArray = source.GroupBy(r => r.Name, e => e.Score).ToArray(); Assert.Equal(4, groupedArray.Length); @@ -646,15 +655,15 @@ public void GroupingWithElementSelectorToArray() [Fact] public void GroupingWithResultsToArray() { - Record[] source = new Record[] - { + Record[] source = + [ new Record{ Name = "Tim", Score = 55 }, new Record{ Name = "Chris", Score = 49 }, new Record{ Name = "Robert", Score = -100 }, new Record{ Name = "Chris", Score = 24 }, new Record{ Name = "Prakash", Score = 9 }, new Record{ Name = "Tim", Score = 25 } - }; + ]; IEnumerable[] groupedArray = source.GroupBy(r => r.Name, (r, e) => e).ToArray(); Assert.Equal(4, groupedArray.Length); @@ -664,15 +673,15 @@ public void GroupingWithResultsToArray() [Fact] public void GroupingWithElementSelectorAndResultsToArray() { - Record[] source = new Record[] - { + Record[] source = + [ new Record{ Name = "Tim", Score = 55 }, new Record{ Name = "Chris", Score = 49 }, new Record{ Name = "Robert", Score = -100 }, new Record{ Name = "Chris", Score = 24 }, new Record{ Name = "Prakash", Score = 9 }, new Record{ Name = "Tim", Score = 25 } - }; + ]; IEnumerable[] groupedArray = source.GroupBy(r => r.Name, e => e, (r, e) => e).ToArray(); Assert.Equal(4, groupedArray.Length); @@ -682,15 +691,15 @@ public void GroupingWithElementSelectorAndResultsToArray() [Fact] public void GroupingToList() { - Record[] source = new Record[] - { + Record[] source = + [ new Record { Name = "Tim", Score = 55 }, new Record { Name = "Chris", Score = 49 }, new Record { Name = "Robert", Score = -100 }, new Record { Name = "Chris", Score = 24 }, new Record { Name = "Prakash", Score = 9 }, new Record { Name = "Tim", Score = 25 } - }; + ]; List> groupedList = source.GroupBy(r => r.Name).ToList(); Assert.Equal(4, groupedList.Count); @@ -700,15 +709,15 @@ public void GroupingToList() [Fact] public void GroupingWithElementSelectorToList() { - Record[] source = new Record[] - { + Record[] source = + [ new Record { Name = "Tim", Score = 55 }, new Record { Name = "Chris", Score = 49 }, new Record { Name = "Robert", Score = -100 }, new Record { Name = "Chris", Score = 24 }, new Record { Name = "Prakash", Score = 9 }, new Record { Name = "Tim", Score = 25 } - }; + ]; List> groupedList = source.GroupBy(r => r.Name, e => e.Score).ToList(); Assert.Equal(4, groupedList.Count); @@ -718,15 +727,15 @@ public void GroupingWithElementSelectorToList() [Fact] public void GroupingWithResultsToList() { - Record[] source = new Record[] - { + Record[] source = + [ new Record { Name = "Tim", Score = 55 }, new Record { Name = "Chris", Score = 49 }, new Record { Name = "Robert", Score = -100 }, new Record { Name = "Chris", Score = 24 }, new Record { Name = "Prakash", Score = 9 }, new Record { Name = "Tim", Score = 25 } - }; + ]; List> groupedList = source.GroupBy(r => r.Name, (r, e) => e).ToList(); Assert.Equal(4, groupedList.Count); @@ -736,15 +745,15 @@ public void GroupingWithResultsToList() [Fact] public void GroupingWithElementSelectorAndResultsToList() { - Record[] source = new Record[] - { + Record[] source = + [ new Record { Name = "Tim", Score = 55 }, new Record { Name = "Chris", Score = 49 }, new Record { Name = "Robert", Score = -100 }, new Record { Name = "Chris", Score = 24 }, new Record { Name = "Prakash", Score = 9 }, new Record { Name = "Tim", Score = 25 } - }; + ]; List> groupedList = source.GroupBy(r => r.Name, e => e, (r, e) => e).ToList(); Assert.Equal(4, groupedList.Count); @@ -754,15 +763,15 @@ public void GroupingWithElementSelectorAndResultsToList() [Fact] public void GroupingCount() { - Record[] source = new Record[] - { + Record[] source = + [ new Record { Name = "Tim", Score = 55 }, new Record { Name = "Chris", Score = 49 }, new Record { Name = "Robert", Score = -100 }, new Record { Name = "Chris", Score = 24 }, new Record { Name = "Prakash", Score = 9 }, new Record { Name = "Tim", Score = 25 } - }; + ]; Assert.Equal(4, source.GroupBy(r => r.Name).Count()); } @@ -770,15 +779,15 @@ public void GroupingCount() [Fact] public void GroupingWithElementSelectorCount() { - Record[] source = new Record[] - { + Record[] source = + [ new Record { Name = "Tim", Score = 55 }, new Record { Name = "Chris", Score = 49 }, new Record { Name = "Robert", Score = -100 }, new Record { Name = "Chris", Score = 24 }, new Record { Name = "Prakash", Score = 9 }, new Record { Name = "Tim", Score = 25 } - }; + ]; Assert.Equal(4, source.GroupBy(r => r.Name, e => e.Score).Count()); } @@ -786,15 +795,15 @@ public void GroupingWithElementSelectorCount() [Fact] public void GroupingWithResultsCount() { - Record[] source = new Record[] - { + Record[] source = + [ new Record { Name = "Tim", Score = 55 }, new Record { Name = "Chris", Score = 49 }, new Record { Name = "Robert", Score = -100 }, new Record { Name = "Chris", Score = 24 }, new Record { Name = "Prakash", Score = 9 }, new Record { Name = "Tim", Score = 25 } - }; + ]; Assert.Equal(4, source.GroupBy(r => r.Name, (r, e) => e).Count()); } @@ -802,15 +811,15 @@ public void GroupingWithResultsCount() [Fact] public void GroupingWithElementSelectorAndResultsCount() { - Record[] source = new Record[] - { + Record[] source = + [ new Record { Name = "Tim", Score = 55 }, new Record { Name = "Chris", Score = 49 }, new Record { Name = "Robert", Score = -100 }, new Record { Name = "Chris", Score = 24 }, new Record { Name = "Prakash", Score = 9 }, new Record { Name = "Tim", Score = 25 } - }; + ]; Assert.Equal(4, source.GroupBy(r => r.Name, e => e, (r, e) => e).Count()); } @@ -856,7 +865,7 @@ public static void GroupingKeyIsPublic() { // Grouping.Key needs to be public (not explicitly implemented) for the sake of WPF. - object[] objs = { "Foo", 1.0M, "Bar", new { X = "X" }, 2.00M }; + object[] objs = ["Foo", 1.0M, "Bar", new { X = "X" }, 2.00M]; object group = objs.GroupBy(x => x.GetType()).First(); Type grouptype = group.GetType(); diff --git a/tests/System.Linq.Tests/Tests/System.Linq/GroupJoinTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/GroupJoinTests.cs index 82fa5577..5f643318 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/GroupJoinTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/GroupJoinTests.cs @@ -98,35 +98,35 @@ public static JoinRec createJoinRec(CustomerRec cr, IEnumerable arIE [Fact] public void OuterEmptyInnerNonEmpty() { - CustomerRec[] outer = { }; - OrderRec[] inner = new[] - { + CustomerRec[] outer = []; + OrderRec[] inner = + [ new OrderRec{ orderID = 45321, custID = 98022, total = 50 }, new OrderRec{ orderID = 97865, custID = 32103, total = 25 } - }; + ]; Assert.Empty(outer.GroupJoin(inner, e => e.custID, e => e.custID, createJoinRec)); } [Fact] public void CustomComparer() { - CustomerRec[] outer = new[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Tim", custID = 1234 }, new CustomerRec{ name = "Bob", custID = 9865 }, new CustomerRec{ name = "Robert", custID = 9895 } - }; - AnagramRec[] inner = new[] - { + ]; + AnagramRec[] inner = + [ new AnagramRec{ name = "Robert", orderID = 93483, total = 19 }, new AnagramRec{ name = "miT", orderID = 93489, total = 45 } - }; - JoinRec[] expected = new[] - { - new JoinRec{ name = "Tim", orderID = new int?[]{ 93489 }, total = new int?[]{ 45 } }, - new JoinRec{ name = "Bob", orderID = new int?[]{ }, total = new int?[]{ } }, - new JoinRec{ name = "Robert", orderID = new int?[]{ 93483 }, total = new int?[]{ 19 } } - }; + ]; + JoinRec[] expected = + [ + new JoinRec{ name = "Tim", orderID = [93489], total = [45] }, + new JoinRec{ name = "Bob", orderID = [], total = [] }, + new JoinRec{ name = "Robert", orderID = [93483], total = [19] } + ]; Assert.Equal(expected, outer.GroupJoin(inner, e => e.name, e => e.name, createJoinRec, new AnagramEqualityComparer())); } @@ -135,11 +135,11 @@ public void CustomComparer() public void OuterNull() { CustomerRec[] outer = null; - AnagramRec[] inner = new AnagramRec[] - { + AnagramRec[] inner = + [ new AnagramRec{ name = "Robert", orderID = 93483, total = 19 }, new AnagramRec{ name = "miT", orderID = 93489, total = 45 } - }; + ]; AssertExtensions.Throws("outer", () => outer.GroupJoin(inner, e => e.name, e => e.name, createJoinRec, new AnagramEqualityComparer())); } @@ -147,12 +147,12 @@ public void OuterNull() [Fact] public void InnerNull() { - CustomerRec[] outer = new[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Tim", custID = 1234 }, new CustomerRec{ name = "Bob", custID = 9865 }, new CustomerRec{ name = "Robert", custID = 9895 } - }; + ]; AnagramRec[] inner = null; AssertExtensions.Throws("inner", () => outer.GroupJoin(inner, e => e.name, e => e.name, createJoinRec, new AnagramEqualityComparer())); @@ -161,17 +161,17 @@ public void InnerNull() [Fact] public void OuterKeySelectorNull() { - CustomerRec[] outer = new CustomerRec[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Tim", custID = 1234 }, new CustomerRec{ name = "Bob", custID = 9865 }, new CustomerRec{ name = "Robert", custID = 9895 } - }; - AnagramRec[] inner = new AnagramRec[] - { + ]; + AnagramRec[] inner = + [ new AnagramRec{ name = "Robert", orderID = 93483, total = 19 }, new AnagramRec{ name = "miT", orderID = 93489, total = 45 } - }; + ]; AssertExtensions.Throws("outerKeySelector", () => outer.GroupJoin(inner, null, e => e.name, createJoinRec, new AnagramEqualityComparer())); } @@ -179,17 +179,17 @@ public void OuterKeySelectorNull() [Fact] public void InnerKeySelectorNull() { - CustomerRec[] outer = new CustomerRec[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Tim", custID = 1234 }, new CustomerRec{ name = "Bob", custID = 9865 }, new CustomerRec{ name = "Robert", custID = 9895 } - }; - AnagramRec[] inner = new AnagramRec[] - { + ]; + AnagramRec[] inner = + [ new AnagramRec{ name = "Robert", orderID = 93483, total = 19 }, new AnagramRec{ name = "miT", orderID = 93489, total = 45 } - }; + ]; AssertExtensions.Throws("innerKeySelector", () => outer.GroupJoin(inner, e => e.name, null, createJoinRec, new AnagramEqualityComparer())); } @@ -197,17 +197,17 @@ public void InnerKeySelectorNull() [Fact] public void ResultSelectorNull() { - CustomerRec[] outer = new CustomerRec[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Tim", custID = 1234 }, new CustomerRec{ name = "Bob", custID = 9865 }, new CustomerRec{ name = "Robert", custID = 9895 } - }; - AnagramRec[] inner = new AnagramRec[] - { + ]; + AnagramRec[] inner = + [ new AnagramRec{ name = "Robert", orderID = 93483, total = 19 }, new AnagramRec{ name = "miT", orderID = 93489, total = 45 } - }; + ]; AssertExtensions.Throws("resultSelector", () => outer.GroupJoin(inner, e => e.name, e => e.name, (Func, JoinRec>)null, new AnagramEqualityComparer())); } @@ -216,11 +216,11 @@ public void ResultSelectorNull() public void OuterNullNoComparer() { CustomerRec[] outer = null; - AnagramRec[] inner = new AnagramRec[] - { + AnagramRec[] inner = + [ new AnagramRec{ name = "Robert", orderID = 93483, total = 19 }, new AnagramRec{ name = "miT", orderID = 93489, total = 45 } - }; + ]; AssertExtensions.Throws("outer", () => outer.GroupJoin(inner, e => e.name, e => e.name, createJoinRec)); } @@ -228,12 +228,12 @@ public void OuterNullNoComparer() [Fact] public void InnerNullNoComparer() { - CustomerRec[] outer = new[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Tim", custID = 1234 }, new CustomerRec{ name = "Bob", custID = 9865 }, new CustomerRec{ name = "Robert", custID = 9895 } - }; + ]; AnagramRec[] inner = null; AssertExtensions.Throws("inner", () => outer.GroupJoin(inner, e => e.name, e => e.name, createJoinRec)); @@ -242,17 +242,17 @@ public void InnerNullNoComparer() [Fact] public void OuterKeySelectorNullNoComparer() { - CustomerRec[] outer = new CustomerRec[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Tim", custID = 1234 }, new CustomerRec{ name = "Bob", custID = 9865 }, new CustomerRec{ name = "Robert", custID = 9895 } - }; - AnagramRec[] inner = new AnagramRec[] - { + ]; + AnagramRec[] inner = + [ new AnagramRec{ name = "Robert", orderID = 93483, total = 19 }, new AnagramRec{ name = "miT", orderID = 93489, total = 45 } - }; + ]; AssertExtensions.Throws("outerKeySelector", () => outer.GroupJoin(inner, null, e => e.name, createJoinRec)); } @@ -260,17 +260,17 @@ public void OuterKeySelectorNullNoComparer() [Fact] public void InnerKeySelectorNullNoComparer() { - CustomerRec[] outer = new CustomerRec[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Tim", custID = 1234 }, new CustomerRec{ name = "Bob", custID = 9865 }, new CustomerRec{ name = "Robert", custID = 9895 } - }; - AnagramRec[] inner = new AnagramRec[] - { + ]; + AnagramRec[] inner = + [ new AnagramRec{ name = "Robert", orderID = 93483, total = 19 }, new AnagramRec{ name = "miT", orderID = 93489, total = 45 } - }; + ]; AssertExtensions.Throws("innerKeySelector", () => outer.GroupJoin(inner, e => e.name, null, createJoinRec)); } @@ -278,17 +278,17 @@ public void InnerKeySelectorNullNoComparer() [Fact] public void ResultSelectorNullNoComparer() { - CustomerRec[] outer = new CustomerRec[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Tim", custID = 1234 }, new CustomerRec{ name = "Bob", custID = 9865 }, new CustomerRec{ name = "Robert", custID = 9895 } - }; - AnagramRec[] inner = new AnagramRec[] - { + ]; + AnagramRec[] inner = + [ new AnagramRec{ name = "Robert", orderID = 93483, total = 19 }, new AnagramRec{ name = "miT", orderID = 93489, total = 45 } - }; + ]; AssertExtensions.Throws("resultSelector", () => outer.GroupJoin(inner, e => e.name, e => e.name, (Func, JoinRec>)null)); } @@ -296,9 +296,9 @@ public void ResultSelectorNullNoComparer() [Fact] public void OuterInnerBothSingleNullElement() { - string[] outer = new string[] { null }; - string[] inner = new string[] { null }; - string[] expected = new string[] { null }; + string[] outer = [null]; + string[] inner = [null]; + string[] expected = [null]; Assert.Equal(expected, outer.GroupJoin(inner, e => e, e => e, (x, y) => x, EqualityComparer.Default)); } @@ -306,17 +306,17 @@ public void OuterInnerBothSingleNullElement() [Fact] public void OuterNonEmptyInnerEmpty() { - CustomerRec[] outer = new[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Tim", custID = 43434 }, new CustomerRec{ name = "Bob", custID = 34093 } - }; - OrderRec[] inner = { }; - JoinRec[] expected = new[] - { - new JoinRec{ name = "Tim", orderID = new int?[]{ }, total = new int?[]{ } }, - new JoinRec{ name = "Bob", orderID = new int?[]{ }, total = new int?[]{ } } - }; + ]; + OrderRec[] inner = []; + JoinRec[] expected = + [ + new JoinRec{ name = "Tim", orderID = [], total = [] }, + new JoinRec{ name = "Bob", orderID = [], total = [] } + ]; Assert.Equal(expected, outer.GroupJoin(inner, e => e.custID, e => e.custID, createJoinRec)); } @@ -324,9 +324,9 @@ public void OuterNonEmptyInnerEmpty() [Fact] public void SingleElementEachAndMatches() { - CustomerRec[] outer = new[] { new CustomerRec { name = "Tim", custID = 43434 } }; - OrderRec[] inner = new[] { new OrderRec { orderID = 97865, custID = 43434, total = 25 } }; - JoinRec[] expected = new[] { new JoinRec { name = "Tim", orderID = new int?[] { 97865 }, total = new int?[] { 25 } } }; + CustomerRec[] outer = [new CustomerRec { name = "Tim", custID = 43434 }]; + OrderRec[] inner = [new OrderRec { orderID = 97865, custID = 43434, total = 25 }]; + JoinRec[] expected = [new JoinRec { name = "Tim", orderID = [97865], total = [25] }]; Assert.Equal(expected, outer.GroupJoin(inner, e => e.custID, e => e.custID, createJoinRec)); } @@ -334,9 +334,9 @@ public void SingleElementEachAndMatches() [Fact] public void SingleElementEachAndDoesntMatch() { - CustomerRec[] outer = new[] { new CustomerRec { name = "Tim", custID = 43434 } }; - OrderRec[] inner = new[] { new OrderRec { orderID = 97865, custID = 49434, total = 25 } }; - JoinRec[] expected = new JoinRec[] { new JoinRec { name = "Tim", orderID = new int?[] { }, total = new int?[] { } } }; + CustomerRec[] outer = [new CustomerRec { name = "Tim", custID = 43434 }]; + OrderRec[] inner = [new OrderRec { orderID = 97865, custID = 49434, total = 25 }]; + JoinRec[] expected = [new JoinRec { name = "Tim", orderID = [], total = [] }]; Assert.Equal(expected, outer.GroupJoin(inner, e => e.custID, e => e.custID, createJoinRec)); } @@ -344,21 +344,21 @@ public void SingleElementEachAndDoesntMatch() [Fact] public void SelectorsReturnNull() { - CustomerRec[] outer = new[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Tim", custID = null }, new CustomerRec{ name = "Bob", custID = null } - }; - OrderRec[] inner = new[] - { + ]; + OrderRec[] inner = + [ new OrderRec{ orderID = 97865, custID = null, total = 25 }, new OrderRec{ orderID = 34390, custID = null, total = 19 } - }; - JoinRec[] expected = new[] - { - new JoinRec{ name = "Tim", orderID = new int?[]{ }, total = new int?[]{ } }, - new JoinRec{ name = "Bob", orderID = new int?[]{ }, total = new int?[]{ } } - }; + ]; + JoinRec[] expected = + [ + new JoinRec{ name = "Tim", orderID = [], total = [] }, + new JoinRec{ name = "Bob", orderID = [], total = [] } + ]; Assert.Equal(expected, outer.GroupJoin(inner, e => e.custID, e => e.custID, createJoinRec)); } @@ -366,22 +366,22 @@ public void SelectorsReturnNull() [Fact] public void InnerSameKeyMoreThanOneElementAndMatches() { - CustomerRec[] outer = new[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Tim", custID = 1234 }, new CustomerRec{ name = "Bob", custID = 9865 } - }; - OrderRec[] inner = new[] - { + ]; + OrderRec[] inner = + [ new OrderRec{ orderID = 97865, custID = 1234, total = 25 }, new OrderRec{ orderID = 34390, custID = 1234, total = 19 }, new OrderRec{ orderID = 34390, custID = 9865, total = 19 } - }; - JoinRec[] expected = new[] - { - new JoinRec { name = "Tim", orderID = new int?[]{ 97865, 34390 }, total = new int?[] { 25, 19 } }, - new JoinRec { name = "Bob", orderID = new int?[]{ 34390 }, total = new int?[]{ 19 } } - }; + ]; + JoinRec[] expected = + [ + new JoinRec { name = "Tim", orderID = [97865, 34390], total = [25, 19] }, + new JoinRec { name = "Bob", orderID = [34390], total = [19] } + ]; Assert.Equal(expected, outer.GroupJoin(inner, e => e.custID, e => e.custID, createJoinRec)); } @@ -389,22 +389,22 @@ public void InnerSameKeyMoreThanOneElementAndMatches() [Fact] public void InnerSameKeyMoreThanOneElementAndMatchesRunOnce() { - CustomerRec[] outer = new[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Tim", custID = 1234 }, new CustomerRec{ name = "Bob", custID = 9865 } - }; - OrderRec[] inner = new[] - { + ]; + OrderRec[] inner = + [ new OrderRec{ orderID = 97865, custID = 1234, total = 25 }, new OrderRec{ orderID = 34390, custID = 1234, total = 19 }, new OrderRec{ orderID = 34390, custID = 9865, total = 19 } - }; - JoinRec[] expected = new[] - { - new JoinRec { name = "Tim", orderID = new int?[]{ 97865, 34390 }, total = new int?[] { 25, 19 } }, - new JoinRec { name = "Bob", orderID = new int?[]{ 34390 }, total = new int?[]{ 19 } } - }; + ]; + JoinRec[] expected = + [ + new JoinRec { name = "Tim", orderID = [97865, 34390], total = [25, 19] }, + new JoinRec { name = "Bob", orderID = [34390], total = [19] } + ]; Assert.Equal(expected, outer.RunOnce().GroupJoin(inner.RunOnce(), e => e.custID, e => e.custID, createJoinRec)); } @@ -412,23 +412,23 @@ public void InnerSameKeyMoreThanOneElementAndMatchesRunOnce() [Fact] public void OuterSameKeyMoreThanOneElementAndMatches() { - CustomerRec[] outer = new[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Tim", custID = 1234 }, new CustomerRec{ name = "Bob", custID = 9865 }, new CustomerRec{ name = "Robert", custID = 9865 } - }; - OrderRec[] inner = new[] - { + ]; + OrderRec[] inner = + [ new OrderRec{ orderID = 97865, custID = 1234, total = 25 }, new OrderRec{ orderID = 34390, custID = 9865, total = 19 } - }; - JoinRec[] expected = new[] - { - new JoinRec { name = "Tim", orderID = new int?[]{ 97865 }, total = new int?[]{ 25 } }, - new JoinRec { name = "Bob", orderID = new int?[]{ 34390 }, total = new int?[]{ 19 } }, - new JoinRec { name = "Robert", orderID = new int?[]{ 34390 }, total = new int?[]{ 19 } } - }; + ]; + JoinRec[] expected = + [ + new JoinRec { name = "Tim", orderID = [97865], total = [25] }, + new JoinRec { name = "Bob", orderID = [34390], total = [19] }, + new JoinRec { name = "Robert", orderID = [34390], total = [19] } + ]; Assert.Equal(expected, outer.GroupJoin(inner, e => e.custID, e => e.custID, createJoinRec)); } @@ -436,23 +436,23 @@ public void OuterSameKeyMoreThanOneElementAndMatches() [Fact] public void NoMatches() { - CustomerRec[] outer = new[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Tim", custID = 1234 }, new CustomerRec{ name = "Bob", custID = 9865 }, new CustomerRec{ name = "Robert", custID = 9895 } - }; - OrderRec[] inner = new[] - { + ]; + OrderRec[] inner = + [ new OrderRec{ orderID = 97865, custID = 2334, total = 25 }, new OrderRec{ orderID = 34390, custID = 9065, total = 19 } - }; - JoinRec[] expected = new[] - { - new JoinRec{ name = "Tim", orderID = new int?[]{ }, total = new int?[]{ } }, - new JoinRec{ name = "Bob", orderID = new int?[]{ }, total = new int?[]{ } }, - new JoinRec{ name = "Robert", orderID = new int?[]{ }, total = new int?[]{ } } - }; + ]; + JoinRec[] expected = + [ + new JoinRec{ name = "Tim", orderID = [], total = [] }, + new JoinRec{ name = "Bob", orderID = [], total = [] }, + new JoinRec{ name = "Robert", orderID = [], total = [] } + ]; Assert.Equal(expected, outer.GroupJoin(inner, e => e.custID, e => e.custID, createJoinRec)); } @@ -460,23 +460,23 @@ public void NoMatches() [Fact] public void NullComparer() { - CustomerRec[] outer = new[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Tim", custID = 1234 }, new CustomerRec{ name = "Bob", custID = 9865 }, new CustomerRec{ name = "Robert", custID = 9895 } - }; - AnagramRec[] inner = new[] - { + ]; + AnagramRec[] inner = + [ new AnagramRec{ name = "Robert", orderID = 93483, total = 19 }, new AnagramRec{ name = "miT", orderID = 93489, total = 45 } - }; - JoinRec[] expected = new[] - { - new JoinRec{ name = "Tim", orderID = new int?[]{ }, total = new int?[]{ } }, - new JoinRec{ name = "Bob", orderID = new int?[]{ }, total = new int?[]{ } }, - new JoinRec{ name = "Robert", orderID = new int?[]{ 93483 }, total = new int?[]{ 19 } } - }; + ]; + JoinRec[] expected = + [ + new JoinRec{ name = "Tim", orderID = [], total = [] }, + new JoinRec{ name = "Bob", orderID = [], total = [] }, + new JoinRec{ name = "Robert", orderID = [93483], total = [19] } + ]; Assert.Equal(expected, outer.GroupJoin(inner, e => e.name, e => e.name, createJoinRec, null)); } @@ -484,23 +484,23 @@ public void NullComparer() [Fact] public void NullComparerRunOnce() { - CustomerRec[] outer = new[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Tim", custID = 1234 }, new CustomerRec{ name = "Bob", custID = 9865 }, new CustomerRec{ name = "Robert", custID = 9895 } - }; - AnagramRec[] inner = new[] - { + ]; + AnagramRec[] inner = + [ new AnagramRec{ name = "Robert", orderID = 93483, total = 19 }, new AnagramRec{ name = "miT", orderID = 93489, total = 45 } - }; - JoinRec[] expected = new[] - { - new JoinRec{ name = "Tim", orderID = new int?[]{ }, total = new int?[]{ } }, - new JoinRec{ name = "Bob", orderID = new int?[]{ }, total = new int?[]{ } }, - new JoinRec{ name = "Robert", orderID = new int?[]{ 93483 }, total = new int?[]{ 19 } } - }; + ]; + JoinRec[] expected = + [ + new JoinRec{ name = "Tim", orderID = [], total = [] }, + new JoinRec{ name = "Bob", orderID = [], total = [] }, + new JoinRec{ name = "Robert", orderID = [93483], total = [19] } + ]; Assert.Equal(expected, outer.RunOnce().GroupJoin(inner.RunOnce(), e => e.name, e => e.name, createJoinRec, null)); } diff --git a/tests/System.Linq.Tests/Tests/System.Linq/IntersectTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/IntersectTests.cs index 57a5ff12..5318352d 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/IntersectTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/IntersectTests.cs @@ -32,10 +32,10 @@ public void SameResultsRepeatCallsStringQuery() public static IEnumerable Int_TestData() { - yield return new object[] { new int[0], new int[0], new int[0] }; - yield return new object[] { new int[] { -5, 3, -2, 6, 9 }, new int[] { 0, 5, 2, 10, 20 }, new int[0] }; - yield return new object[] { new int[] { 1, 2, 2, 3, 4, 3, 5 }, new int[] { 1, 4, 4, 2, 2, 2 }, new int[] { 1, 2, 4 } }; - yield return new object[] { new int[] { 1, 1, 1, 1, 1, 1 }, new int[] { 1, 1, 1, 1, 1 }, new int[] { 1 } }; + yield return [new int[0], new int[0], new int[0]]; + yield return [new int[] { -5, 3, -2, 6, 9 }, new int[] { 0, 5, 2, 10, 20 }, new int[0]]; + yield return [new int[] { 1, 2, 2, 3, 4, 3, 5 }, new int[] { 1, 4, 4, 2, 2, 2 }, new int[] { 1, 2, 4 }]; + yield return [new int[] { 1, 1, 1, 1, 1, 1 }, new int[] { 1, 1, 1, 1, 1 }, new int[] { 1 }]; } [Theory] @@ -49,12 +49,12 @@ public void Int(IEnumerable first, IEnumerable second, int[] expected) public static IEnumerable String_TestData() { IEqualityComparer defaultComparer = EqualityComparer.Default; - yield return new object[] { new string[1], new string[0], defaultComparer, new string[0] }; - yield return new object[] { new string[] { null, null, string.Empty }, new string[2], defaultComparer, new string[] { null } }; - yield return new object[] { new string[2], new string[0], defaultComparer, new string[0] }; + yield return [new string[1], new string[0], defaultComparer, new string[0]]; + yield return [new string[] { null, null, string.Empty }, new string[2], defaultComparer, new string[] { null }]; + yield return [new string[2], new string[0], defaultComparer, new string[0]]; - yield return new object[] { new string[] { "Tim", "Bob", "Mike", "Robert" }, new string[] { "ekiM", "bBo" }, null, new string[0] }; - yield return new object[] { new string[] { "Tim", "Bob", "Mike", "Robert" }, new string[] { "ekiM", "bBo" }, new AnagramEqualityComparer(), new string[] { "Bob", "Mike" } }; + yield return [new string[] { "Tim", "Bob", "Mike", "Robert" }, new string[] { "ekiM", "bBo" }, null, new string[0]]; + yield return [new string[] { "Tim", "Bob", "Mike", "Robert" }, new string[] { "ekiM", "bBo" }, new AnagramEqualityComparer(), new string[] { "Bob", "Mike" }]; } [Theory] @@ -70,9 +70,9 @@ public void String(IEnumerable first, IEnumerable second, IEqual public static IEnumerable NullableInt_TestData() { - yield return new object[] { new int?[0], new int?[] { -5, 0, null, 1, 2, 9, 2 }, new int?[0] }; - yield return new object[] { new int?[] { -5, 0, 1, 2, null, 9, 2 }, new int?[0], new int?[0] }; - yield return new object[] { new int?[] { 1, 2, null, 3, 4, 5, 6 }, new int?[] { 6, 7, 7, 7, null, 8, 1 }, new int?[] { 1, null, 6 } }; + yield return [new int?[0], new int?[] { -5, 0, null, 1, 2, 9, 2 }, new int?[0]]; + yield return [new int?[] { -5, 0, 1, 2, null, 9, 2 }, new int?[0], new int?[0]]; + yield return [new int?[] { 1, 2, null, 3, 4, 5, 6 }, new int?[] { 6, 7, 7, 7, null, 8, 1 }, new int?[] { 1, null, 6 }]; } [Theory] @@ -94,7 +94,7 @@ public void NullableIntRunOnce(IEnumerable first, IEnumerable second public void FirstNull_ThrowsArgumentNullException() { string[] first = null; - string[] second = { "ekiM", "bBo" }; + string[] second = ["ekiM", "bBo"]; AssertExtensions.Throws("first", () => first.Intersect(second)); AssertExtensions.Throws("first", () => first.Intersect(second, new AnagramEqualityComparer())); @@ -103,7 +103,7 @@ public void FirstNull_ThrowsArgumentNullException() [Fact] public void SecondNull_ThrowsArgumentNullException() { - string[] first = { "Tim", "Bob", "Mike", "Robert" }; + string[] first = ["Tim", "Bob", "Mike", "Robert"]; string[] second = null; AssertExtensions.Throws("second", () => first.Intersect(second)); @@ -123,24 +123,24 @@ public void ForcedToEnumeratorDoesntEnumerate() public void HashSetWithBuiltInComparer_HashSetContainsNotUsed() { IEnumerable input1 = new HashSet(StringComparer.OrdinalIgnoreCase) { "a" }; - IEnumerable input2 = new[] { "A" }; + IEnumerable input2 = ["A"]; - Assert.Equal(Enumerable.Empty(), input1.Intersect(input2)); - Assert.Equal(Enumerable.Empty(), input1.Intersect(input2, null)); - Assert.Equal(Enumerable.Empty(), input1.Intersect(input2, EqualityComparer.Default)); - Assert.Equal(new[] { "a" }, input1.Intersect(input2, StringComparer.OrdinalIgnoreCase)); + Assert.Equal([], input1.Intersect(input2)); + Assert.Equal([], input1.Intersect(input2, null)); + Assert.Equal([], input1.Intersect(input2, EqualityComparer.Default)); + Assert.Equal(["a"], input1.Intersect(input2, StringComparer.OrdinalIgnoreCase)); - Assert.Equal(Enumerable.Empty(), input2.Intersect(input1)); - Assert.Equal(Enumerable.Empty(), input2.Intersect(input1, null)); - Assert.Equal(Enumerable.Empty(), input2.Intersect(input1, EqualityComparer.Default)); - Assert.Equal(new[] { "A" }, input2.Intersect(input1, StringComparer.OrdinalIgnoreCase)); + Assert.Equal([], input2.Intersect(input1)); + Assert.Equal([], input2.Intersect(input1, null)); + Assert.Equal([], input2.Intersect(input1, EqualityComparer.Default)); + Assert.Equal(["A"], input2.Intersect(input1, StringComparer.OrdinalIgnoreCase)); } [Fact] public void IntersectBy_FirstNull_ThrowsArgumentNullException() { string[] first = null; - string[] second = { "bBo", "shriC" }; + string[] second = ["bBo", "shriC"]; AssertExtensions.Throws("first", () => first.IntersectBy(second, x => x)); AssertExtensions.Throws("first", () => first.IntersectBy(second, x => x, new AnagramEqualityComparer())); @@ -149,7 +149,7 @@ public void IntersectBy_FirstNull_ThrowsArgumentNullException() [Fact] public void IntersectBy_SecondNull_ThrowsArgumentNullException() { - string[] first = { "Bob", "Tim", "Robert", "Chris" }; + string[] first = ["Bob", "Tim", "Robert", "Chris"]; string[] second = null; AssertExtensions.Throws("second", () => first.IntersectBy(second, x => x)); @@ -159,8 +159,8 @@ public void IntersectBy_SecondNull_ThrowsArgumentNullException() [Fact] public void IntersectBy_KeySelectorNull_ThrowsArgumentNullException() { - string[] first = { "Bob", "Tim", "Robert", "Chris" }; - string[] second = { "bBo", "shriC" }; + string[] first = ["Bob", "Tim", "Robert", "Chris"]; + string[] second = ["bBo", "shriC"]; Func keySelector = null; AssertExtensions.Throws("keySelector", () => first.IntersectBy(second, keySelector)); @@ -195,14 +195,14 @@ public static IEnumerable IntersectBy_TestData() second: Enumerable.Range(10, 10), keySelector: x => x, comparer: null, - expected: Enumerable.Empty()); + expected: []); yield return WrapArgs( first: Enumerable.Repeat(5, 20), - second: Enumerable.Empty(), + second: [], keySelector: x => x, comparer: null, - expected: Enumerable.Empty()); + expected: []); yield return WrapArgs( first: Enumerable.Repeat(5, 20), @@ -212,42 +212,42 @@ public static IEnumerable IntersectBy_TestData() expected: Enumerable.Repeat(5, 1)); yield return WrapArgs( - first: new string[] { "Bob", "Tim", "Robert", "Chris" }, - second: new string[] { "bBo", "shriC" }, + first: ["Bob", "Tim", "Robert", "Chris"], + second: ["bBo", "shriC"], keySelector: x => x, null, expected: Array.Empty()); yield return WrapArgs( - first: new string[] { "Bob", "Tim", "Robert", "Chris" }, - second: new string[] { "bBo", "shriC" }, + first: ["Bob", "Tim", "Robert", "Chris"], + second: ["bBo", "shriC"], keySelector: x => x, new AnagramEqualityComparer(), - expected: new string[] { "Bob", "Chris" }); + expected: ["Bob", "Chris"]); yield return WrapArgs( first: new (string Name, int Age)[] { ("Tom", 20), ("Dick", 30), ("Harry", 40) }, - second: new int[] { 15, 20, 40 }, + second: [15, 20, 40], keySelector: x => x.Age, comparer: null, expected: new (string Name, int Age)[] { ("Tom", 20), ("Harry", 40) }); yield return WrapArgs( first: new (string Name, int Age)[] { ("Tom", 20), ("Dick", 30), ("Harry", 40) }, - second: new string[] { "moT" }, + second: ["moT"], keySelector: x => x.Name, comparer: null, expected: Array.Empty<(string Name, int Age)>()); yield return WrapArgs( first: new (string Name, int Age)[] { ("Tom", 20), ("Dick", 30), ("Harry", 40) }, - second: new string[] { "moT" }, + second: ["moT"], keySelector: x => x.Name, comparer: new AnagramEqualityComparer(), expected: new (string Name, int Age)[] { ("Tom", 20) }); object[] WrapArgs(IEnumerable first, IEnumerable second, Func keySelector, IEqualityComparer? comparer, IEnumerable expected) - => new object[] { first, second, keySelector, comparer, expected }; + => [first, second, keySelector, comparer, expected]; } } } diff --git a/tests/System.Linq.Tests/Tests/System.Linq/JoinTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/JoinTests.cs index 6d484a63..0aa4a906 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/JoinTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/JoinTests.cs @@ -1,7 +1,6 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System; using System.Collections.Generic; using Xunit; @@ -49,35 +48,35 @@ public static JoinRec createJoinRec(CustomerRec cr, AnagramRec or) [Fact] public void OuterEmptyInnerNonEmpty() { - CustomerRec[] outer = { }; - OrderRec[] inner = new[] - { + CustomerRec[] outer = []; + OrderRec[] inner = + [ new OrderRec{ orderID = 45321, custID = 98022, total = 50 }, new OrderRec{ orderID = 97865, custID = 32103, total = 25 } - }; + ]; Assert.Empty(outer.Join(inner, e => e.custID, e => e.custID, createJoinRec)); } [Fact] public void FirstOuterMatchesLastInnerLastOuterMatchesFirstInnerSameNumberElements() { - CustomerRec[] outer = new[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Prakash", custID = 98022 }, new CustomerRec{ name = "Tim", custID = 99021 }, new CustomerRec{ name = "Robert", custID = 99022 } - }; - OrderRec[] inner = new[] - { + ]; + OrderRec[] inner = + [ new OrderRec{ orderID = 45321, custID = 99022, total = 50 }, new OrderRec{ orderID = 43421, custID = 29022, total = 20 }, new OrderRec{ orderID = 95421, custID = 98022, total = 9 } - }; - JoinRec[] expected = new[] - { + ]; + JoinRec[] expected = + [ new JoinRec{ name = "Prakash", orderID = 95421, total = 9 }, new JoinRec{ name = "Robert", orderID = 45321, total = 50 } - }; + ]; Assert.Equal(expected, outer.Join(inner, e => e.custID, e => e.custID, createJoinRec)); } @@ -85,18 +84,18 @@ public void FirstOuterMatchesLastInnerLastOuterMatchesFirstInnerSameNumberElemen [Fact] public void NullComparer() { - CustomerRec[] outer = new[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Prakash", custID = 98022 }, new CustomerRec{ name = "Tim", custID = 99021 }, new CustomerRec{ name = "Robert", custID = 99022 } - }; - AnagramRec[] inner = new[] - { + ]; + AnagramRec[] inner = + [ new AnagramRec{ name = "miT", orderID = 43455, total = 10 }, new AnagramRec{ name = "Prakash", orderID = 323232, total = 9 } - }; - JoinRec[] expected = new[] { new JoinRec { name = "Prakash", orderID = 323232, total = 9 } }; + ]; + JoinRec[] expected = [new JoinRec { name = "Prakash", orderID = 323232, total = 9 }]; Assert.Equal(expected, outer.Join(inner, e => e.name, e => e.name, createJoinRec, null)); } @@ -104,22 +103,22 @@ public void NullComparer() [Fact] public void CustomComparer() { - CustomerRec[] outer = new[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Prakash", custID = 98022 }, new CustomerRec{ name = "Tim", custID = 99021 }, new CustomerRec{ name = "Robert", custID = 99022 } - }; - AnagramRec[] inner = new[] - { + ]; + AnagramRec[] inner = + [ new AnagramRec{ name = "miT", orderID = 43455, total = 10 }, new AnagramRec{ name = "Prakash", orderID = 323232, total = 9 } - }; - JoinRec[] expected = new[] - { + ]; + JoinRec[] expected = + [ new JoinRec{ name = "Prakash", orderID = 323232, total = 9 }, new JoinRec{ name = "Tim", orderID = 43455, total = 10 } - }; + ]; Assert.Equal(expected, outer.Join(inner, e => e.name, e => e.name, createJoinRec, new AnagramEqualityComparer())); } @@ -128,11 +127,11 @@ public void CustomComparer() public void OuterNull() { CustomerRec[] outer = null; - AnagramRec[] inner = new[] - { + AnagramRec[] inner = + [ new AnagramRec{ name = "miT", orderID = 43455, total = 10 }, new AnagramRec{ name = "Prakash", orderID = 323232, total = 9 } - }; + ]; AssertExtensions.Throws("outer", () => outer.Join(inner, e => e.name, e => e.name, createJoinRec, new AnagramEqualityComparer())); } @@ -140,12 +139,12 @@ public void OuterNull() [Fact] public void InnerNull() { - CustomerRec[] outer = new[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Prakash", custID = 98022 }, new CustomerRec{ name = "Tim", custID = 99021 }, new CustomerRec{ name = "Robert", custID = 99022 } - }; + ]; AnagramRec[] inner = null; AssertExtensions.Throws("inner", () => outer.Join(inner, e => e.name, e => e.name, createJoinRec, new AnagramEqualityComparer())); @@ -154,17 +153,17 @@ public void InnerNull() [Fact] public void OuterKeySelectorNull() { - CustomerRec[] outer = new[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Prakash", custID = 98022 }, new CustomerRec{ name = "Tim", custID = 99021 }, new CustomerRec{ name = "Robert", custID = 99022 } - }; - AnagramRec[] inner = new[] - { + ]; + AnagramRec[] inner = + [ new AnagramRec{ name = "miT", orderID = 43455, total = 10 }, new AnagramRec{ name = "Prakash", orderID = 323232, total = 9 } - }; + ]; AssertExtensions.Throws("outerKeySelector", () => outer.Join(inner, null, e => e.name, createJoinRec, new AnagramEqualityComparer())); } @@ -172,17 +171,17 @@ public void OuterKeySelectorNull() [Fact] public void InnerKeySelectorNull() { - CustomerRec[] outer = new[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Prakash", custID = 98022 }, new CustomerRec{ name = "Tim", custID = 99021 }, new CustomerRec{ name = "Robert", custID = 99022 } - }; - AnagramRec[] inner = new[] - { + ]; + AnagramRec[] inner = + [ new AnagramRec{ name = "miT", orderID = 43455, total = 10 }, new AnagramRec{ name = "Prakash", orderID = 323232, total = 9 } - }; + ]; AssertExtensions.Throws("innerKeySelector", () => outer.Join(inner, e => e.name, null, createJoinRec, new AnagramEqualityComparer())); } @@ -190,17 +189,17 @@ public void InnerKeySelectorNull() [Fact] public void ResultSelectorNull() { - CustomerRec[] outer = new[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Prakash", custID = 98022 }, new CustomerRec{ name = "Tim", custID = 99021 }, new CustomerRec{ name = "Robert", custID = 99022 } - }; - AnagramRec[] inner = new[] - { + ]; + AnagramRec[] inner = + [ new AnagramRec{ name = "miT", orderID = 43455, total = 10 }, new AnagramRec{ name = "Prakash", orderID = 323232, total = 9 } - }; + ]; AssertExtensions.Throws("resultSelector", () => outer.Join(inner, e => e.name, e => e.name, (Func)null, new AnagramEqualityComparer())); } @@ -209,11 +208,11 @@ public void ResultSelectorNull() public void OuterNullNoComparer() { CustomerRec[] outer = null; - AnagramRec[] inner = new[] - { + AnagramRec[] inner = + [ new AnagramRec{ name = "miT", orderID = 43455, total = 10 }, new AnagramRec{ name = "Prakash", orderID = 323232, total = 9 } - }; + ]; AssertExtensions.Throws("outer", () => outer.Join(inner, e => e.name, e => e.name, createJoinRec)); } @@ -221,12 +220,12 @@ public void OuterNullNoComparer() [Fact] public void InnerNullNoComparer() { - CustomerRec[] outer = new[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Prakash", custID = 98022 }, new CustomerRec{ name = "Tim", custID = 99021 }, new CustomerRec{ name = "Robert", custID = 99022 } - }; + ]; AnagramRec[] inner = null; AssertExtensions.Throws("inner", () => outer.Join(inner, e => e.name, e => e.name, createJoinRec)); @@ -235,17 +234,17 @@ public void InnerNullNoComparer() [Fact] public void OuterKeySelectorNullNoComparer() { - CustomerRec[] outer = new[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Prakash", custID = 98022 }, new CustomerRec{ name = "Tim", custID = 99021 }, new CustomerRec{ name = "Robert", custID = 99022 } - }; - AnagramRec[] inner = new[] - { + ]; + AnagramRec[] inner = + [ new AnagramRec{ name = "miT", orderID = 43455, total = 10 }, new AnagramRec{ name = "Prakash", orderID = 323232, total = 9 } - }; + ]; AssertExtensions.Throws("outerKeySelector", () => outer.Join(inner, null, e => e.name, createJoinRec)); } @@ -253,17 +252,17 @@ public void OuterKeySelectorNullNoComparer() [Fact] public void InnerKeySelectorNullNoComparer() { - CustomerRec[] outer = new[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Prakash", custID = 98022 }, new CustomerRec{ name = "Tim", custID = 99021 }, new CustomerRec{ name = "Robert", custID = 99022 } - }; - AnagramRec[] inner = new[] - { + ]; + AnagramRec[] inner = + [ new AnagramRec{ name = "miT", orderID = 43455, total = 10 }, new AnagramRec{ name = "Prakash", orderID = 323232, total = 9 } - }; + ]; AssertExtensions.Throws("innerKeySelector", () => outer.Join(inner, e => e.name, null, createJoinRec)); } @@ -271,17 +270,17 @@ public void InnerKeySelectorNullNoComparer() [Fact] public void ResultSelectorNullNoComparer() { - CustomerRec[] outer = new[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Prakash", custID = 98022 }, new CustomerRec{ name = "Tim", custID = 99021 }, new CustomerRec{ name = "Robert", custID = 99022 } - }; - AnagramRec[] inner = new[] - { + ]; + AnagramRec[] inner = + [ new AnagramRec{ name = "miT", orderID = 43455, total = 10 }, new AnagramRec{ name = "Prakash", orderID = 323232, total = 9 } - }; + ]; AssertExtensions.Throws("resultSelector", () => outer.Join(inner, e => e.name, e => e.name, (Func)null)); } @@ -289,9 +288,9 @@ public void ResultSelectorNullNoComparer() [Fact] public void SkipsNullElements() { - string[] outer = new[] { null, string.Empty }; - string[] inner = new[] { null, string.Empty }; - string[] expected = new[] { string.Empty }; + string[] outer = [null, string.Empty]; + string[] inner = [null, string.Empty]; + string[] expected = [string.Empty]; Assert.Equal(expected, outer.Join(inner, e => e, e => e, (x, y) => y, EqualityComparer.Default)); } @@ -299,21 +298,21 @@ public void SkipsNullElements() [Fact] public void OuterNonEmptyInnerEmpty() { - CustomerRec[] outer = new[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Tim", custID = 43434 }, new CustomerRec{ name = "Bob", custID = 34093 } - }; - OrderRec[] inner = { }; + ]; + OrderRec[] inner = []; Assert.Empty(outer.Join(inner, e => e.custID, e => e.custID, createJoinRec)); } [Fact] public void SingleElementEachAndMatches() { - CustomerRec[] outer = new[] { new CustomerRec { name = "Prakash", custID = 98022 } }; - OrderRec[] inner = new[] { new OrderRec { orderID = 45321, custID = 98022, total = 50 } }; - JoinRec[] expected = new[] { new JoinRec { name = "Prakash", orderID = 45321, total = 50 } }; + CustomerRec[] outer = [new CustomerRec { name = "Prakash", custID = 98022 }]; + OrderRec[] inner = [new OrderRec { orderID = 45321, custID = 98022, total = 50 }]; + JoinRec[] expected = [new JoinRec { name = "Prakash", orderID = 45321, total = 50 }]; Assert.Equal(expected, outer.Join(inner, e => e.custID, e => e.custID, createJoinRec)); } @@ -321,44 +320,45 @@ public void SingleElementEachAndMatches() [Fact] public void SingleElementEachAndDoesntMatch() { - CustomerRec[] outer = new[] { new CustomerRec { name = "Prakash", custID = 98922 } }; - OrderRec[] inner = new[] { new OrderRec { orderID = 45321, custID = 98022, total = 50 } }; + CustomerRec[] outer = [new CustomerRec { name = "Prakash", custID = 98922 }]; + OrderRec[] inner = [new OrderRec { orderID = 45321, custID = 98022, total = 50 }]; Assert.Empty(outer.Join(inner, e => e.custID, e => e.custID, createJoinRec)); } [Fact] public void SelectorsReturnNull() { - int?[] inner = { null, null, null }; - int?[] outer = { null, null }; + int?[] outer = [null, null]; + int?[] inner = [null, null, null]; + Assert.Empty(outer.Join(inner, e => e, e => e, (x, y) => x)); } [Fact] public void InnerSameKeyMoreThanOneElementAndMatches() { - CustomerRec[] outer = new[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Prakash", custID = 98022 }, new CustomerRec{ name = "Tim", custID = 99021 }, new CustomerRec{ name = "Robert", custID = 99022 } - }; - OrderRec[] inner = new[] - { + ]; + OrderRec[] inner = + [ new OrderRec{ orderID = 45321, custID = 98022, total = 50 }, new OrderRec{ orderID = 45421, custID = 98022, total = 10 }, new OrderRec{ orderID = 43421, custID = 99022, total = 20 }, new OrderRec{ orderID = 85421, custID = 98022, total = 18 }, new OrderRec{ orderID = 95421, custID = 99021, total = 9 } - }; - JoinRec[] expected = new[] - { + ]; + JoinRec[] expected = + [ new JoinRec{ name = "Prakash", orderID = 45321, total = 50 }, new JoinRec{ name = "Prakash", orderID = 45421, total = 10 }, new JoinRec{ name = "Prakash", orderID = 85421, total = 18 }, new JoinRec{ name = "Tim", orderID = 95421, total = 9 }, new JoinRec{ name = "Robert", orderID = 43421, total = 20 } - }; + ]; Assert.Equal(expected, outer.Join(inner, e => e.custID, e => e.custID, createJoinRec)); } @@ -366,26 +366,26 @@ public void InnerSameKeyMoreThanOneElementAndMatches() [Fact] public void OuterSameKeyMoreThanOneElementAndMatches() { - CustomerRec[] outer = new[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Prakash", custID = 98022 }, new CustomerRec{ name = "Bob", custID = 99022 }, new CustomerRec{ name = "Tim", custID = 99021 }, new CustomerRec{ name = "Robert", custID = 99022 } - }; - OrderRec[] inner = new[] - { + ]; + OrderRec[] inner = + [ new OrderRec{ orderID = 45321, custID = 98022, total = 50 }, new OrderRec{ orderID = 43421, custID = 99022, total = 20 }, new OrderRec{ orderID = 95421, custID = 99021, total = 9 } - }; - JoinRec[] expected = new[] - { + ]; + JoinRec[] expected = + [ new JoinRec{ name = "Prakash", orderID = 45321, total = 50 }, new JoinRec{ name = "Bob", orderID = 43421, total = 20 }, new JoinRec{ name = "Tim", orderID = 95421, total = 9 }, new JoinRec{ name = "Robert", orderID = 43421, total = 20 } - }; + ]; Assert.Equal(expected, outer.Join(inner, e => e.custID, e => e.custID, createJoinRec)); } @@ -393,19 +393,19 @@ public void OuterSameKeyMoreThanOneElementAndMatches() [Fact] public void NoMatches() { - CustomerRec[] outer = new[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Prakash", custID = 98022 }, new CustomerRec{ name = "Bob", custID = 99022 }, new CustomerRec{ name = "Tim", custID = 99021 }, new CustomerRec{ name = "Robert", custID = 99022 } - }; - OrderRec[] inner = new[] - { + ]; + OrderRec[] inner = + [ new OrderRec{ orderID = 45321, custID = 18022, total = 50 }, new OrderRec{ orderID = 43421, custID = 29022, total = 20 }, new OrderRec{ orderID = 95421, custID = 39021, total = 9 } - }; + ]; Assert.Empty(outer.Join(inner, e => e.custID, e => e.custID, createJoinRec)); } diff --git a/tests/System.Linq.Tests/Tests/System.Linq/LastOrDefaultTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/LastOrDefaultTests.cs index 6ca8a806..92135ae7 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/LastOrDefaultTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/LastOrDefaultTests.cs @@ -31,7 +31,7 @@ public void SameResultsRepeatCallsStringQuery() private static void TestEmptyIList() { - T[] source = { }; + T[] source = []; T expected = default(T); Assert.IsAssignableFrom>(source); @@ -41,7 +41,7 @@ private static void TestEmptyIList() private static void TestEmptyIListDefault(T defaultValue) { - T[] source = { }; + T[] source = []; Assert.IsAssignableFrom>(source); @@ -68,7 +68,7 @@ public void EmptyIList() [Fact] public void IListTOneElement() { - int[] source = { 5 }; + int[] source = [5]; int expected = 5; Assert.IsAssignableFrom>(source); @@ -79,7 +79,7 @@ public void IListTOneElement() [Fact] public void IListTOneElementDefault() { - int[] source = { 5 }; + int[] source = [5]; int expected = 5; Assert.IsAssignableFrom>(source); @@ -91,7 +91,7 @@ public void IListTOneElementDefault() [Fact] public void IListTManyElementsLastIsDefault() { - int?[] source = { -10, 2, 4, 3, 0, 2, null }; + int?[] source = [-10, 2, 4, 3, 0, 2, null]; int? expected = null; Assert.IsAssignableFrom>(source); @@ -102,7 +102,7 @@ public void IListTManyElementsLastIsDefault() [Fact] public void IListTManyElementsLastIsNotDefault() { - int?[] source = { -10, 2, 4, 3, 0, 2, null, 19 }; + int?[] source = [-10, 2, 4, 3, 0, 2, null, 19]; int? expected = 19; Assert.IsAssignableFrom>(source); @@ -113,7 +113,7 @@ public void IListTManyElementsLastIsNotDefault() [Fact] public void IListTManyElementsLastHasDefault() { - int?[] source = { -10, 2, 4, 3, 0, 2, null }; + int?[] source = [-10, 2, 4, 3, 0, 2, null]; int? expected = null; Assert.IsAssignableFrom>(source); @@ -124,7 +124,7 @@ public void IListTManyElementsLastHasDefault() [Fact] public void IListTManyElementsLastIsHasDefault() { - int?[] source = { -10, 2, 4, 3, 0, 2, null, 19 }; + int?[] source = [-10, 2, 4, 3, 0, 2, null, 19]; int? expected = 19; Assert.IsAssignableFrom>(source); @@ -181,7 +181,7 @@ public void ManyElementsNotIListT() [Fact] public void EmptySourcePredicate() { - int?[] source = { }; + int?[] source = []; Assert.All(CreateSources(source), source => { @@ -193,7 +193,7 @@ public void EmptySourcePredicate() [Fact] public void OneElementTruePredicate() { - int[] source = { 4 }; + int[] source = [4]; Func predicate = IsEven; int expected = 4; @@ -206,7 +206,7 @@ public void OneElementTruePredicate() [Fact] public void OneElementTruePredicateDefault() { - int[] source = { 4 }; + int[] source = [4]; Func predicate = IsEven; int expected = 4; @@ -219,7 +219,7 @@ public void OneElementTruePredicateDefault() [Fact] public void ManyElementsPredicateFalseForAll() { - int[] source = { 9, 5, 1, 3, 17, 21 }; + int[] source = [9, 5, 1, 3, 17, 21]; Func predicate = IsEven; int expected = default(int); @@ -232,7 +232,7 @@ public void ManyElementsPredicateFalseForAll() [Fact] public void ManyElementsPredicateFalseForAllDefault() { - int[] source = { 9, 5, 1, 3, 17, 21 }; + int[] source = [9, 5, 1, 3, 17, 21]; Func predicate = IsEven; int expected = 5; @@ -245,7 +245,7 @@ public void ManyElementsPredicateFalseForAllDefault() [Fact] public void PredicateTrueOnlyForLast() { - int[] source = { 9, 5, 1, 3, 17, 21, 50 }; + int[] source = [9, 5, 1, 3, 17, 21, 50]; Func predicate = IsEven; int expected = 50; @@ -258,7 +258,7 @@ public void PredicateTrueOnlyForLast() [Fact] public void PredicateTrueForSome() { - int[] source = { 3, 7, 10, 7, 9, 2, 11, 18, 13, 9 }; + int[] source = [3, 7, 10, 7, 9, 2, 11, 18, 13, 9]; Func predicate = IsEven; int expected = 18; @@ -271,7 +271,7 @@ public void PredicateTrueForSome() [Fact] public void PredicateTrueForSomeRunOnce() { - int[] source = { 3, 7, 10, 7, 9, 2, 11, 18, 13, 9 }; + int[] source = [3, 7, 10, 7, 9, 2, 11, 18, 13, 9]; Func predicate = IsEven; int expected = 18; diff --git a/tests/System.Linq.Tests/Tests/System.Linq/LastTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/LastTests.cs index 305c3187..33c69b87 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/LastTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/LastTests.cs @@ -31,7 +31,7 @@ public void SameResultsRepeatCallsStringQuery() private static void TestEmptyIList() { - T[] source = { }; + T[] source = []; Assert.NotNull(source as IList); @@ -50,7 +50,7 @@ public void EmptyIListT() [Fact] public void IListTOneElement() { - int[] source = { 5 }; + int[] source = [5]; int expected = 5; Assert.NotNull(source as IList); @@ -61,7 +61,7 @@ public void IListTOneElement() [Fact] public void IListTManyElementsLastIsDefault() { - int?[] source = { -10, 2, 4, 3, 0, 2, null }; + int?[] source = [-10, 2, 4, 3, 0, 2, null]; int? expected = null; Assert.IsAssignableFrom>(source); @@ -72,7 +72,7 @@ public void IListTManyElementsLastIsDefault() [Fact] public void IListTManyElementsLastIsNotDefault() { - int?[] source = { -10, 2, 4, 3, 0, 2, null, 19 }; + int?[] source = [-10, 2, 4, 3, 0, 2, null, 19]; int? expected = 19; Assert.IsAssignableFrom>(source); @@ -128,7 +128,7 @@ public void ManyElementsNotIListT() [Fact] public void EmptySourcePredicate() { - int[] source = { }; + int[] source = []; Assert.All(CreateSources(source), source => { @@ -140,7 +140,7 @@ public void EmptySourcePredicate() [Fact] public void OneElementTruePredicate() { - int[] source = { 4 }; + int[] source = [4]; Func predicate = IsEven; int expected = 4; @@ -153,7 +153,7 @@ public void OneElementTruePredicate() [Fact] public void ManyElementsPredicateFalseForAll() { - int[] source = { 9, 5, 1, 3, 17, 21 }; + int[] source = [9, 5, 1, 3, 17, 21]; Func predicate = IsEven; Assert.All(CreateSources(source), source => @@ -165,7 +165,7 @@ public void ManyElementsPredicateFalseForAll() [Fact] public void PredicateTrueOnlyForLast() { - int[] source = { 9, 5, 1, 3, 17, 21, 50 }; + int[] source = [9, 5, 1, 3, 17, 21, 50]; Func predicate = IsEven; int expected = 50; @@ -178,7 +178,7 @@ public void PredicateTrueOnlyForLast() [Fact] public void PredicateTrueForSome() { - int[] source = { 3, 7, 10, 7, 9, 2, 11, 18, 13, 9 }; + int[] source = [3, 7, 10, 7, 9, 2, 11, 18, 13, 9]; Func predicate = IsEven; int expected = 18; @@ -191,7 +191,7 @@ public void PredicateTrueForSome() [Fact] public void PredicateTrueForSomeRunOnce() { - int[] source = { 3, 7, 10, 7, 9, 2, 11, 18, 13, 9 }; + int[] source = [3, 7, 10, 7, 9, 2, 11, 18, 13, 9]; Func predicate = IsEven; int expected = 18; diff --git a/tests/System.Linq.Tests/Tests/System.Linq/LeftJoinTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/LeftJoinTests.cs index 288e06cc..c83dc714 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/LeftJoinTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/LeftJoinTests.cs @@ -5,10 +5,8 @@ using Xunit; #if NET10_0_OR_GREATER - namespace System.Linq.Tests { - public class LeftJoinTests : EnumerableTests { public struct CustomerRec diff --git a/tests/System.Linq.Tests/Tests/System.Linq/LongCountTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/LongCountTests.cs index e935476d..5d6c821c 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/LongCountTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/LongCountTests.cs @@ -30,15 +30,15 @@ public void SameResultsRepeatCallsStringQuery() public static IEnumerable LongCount_TestData() { - yield return new object[] { new int[0], null, 0L }; - yield return new object[] { new int[] { 3 }, null, 1L }; + yield return [new int[0], null, 0L]; + yield return [new int[] { 3 }, null, 1L]; Func isEvenFunc = IsEven; - yield return new object[] { new int[0], isEvenFunc, 0L }; - yield return new object[] { new int[] { 4 }, isEvenFunc, 1L }; - yield return new object[] { new int[] { 5 }, isEvenFunc, 0L }; - yield return new object[] { new int[] { 2, 5, 7, 9, 29, 10 }, isEvenFunc, 2L }; - yield return new object[] { new int[] { 2, 20, 22, 100, 50, 10 }, isEvenFunc, 6L }; + yield return [new int[0], isEvenFunc, 0L]; + yield return [new int[] { 4 }, isEvenFunc, 1L]; + yield return [new int[] { 5 }, isEvenFunc, 0L]; + yield return [new int[] { 2, 5, 7, 9, 29, 10 }, isEvenFunc, 2L]; + yield return [new int[] { 2, 20, 22, 100, 50, 10 }, isEvenFunc, 6L]; } [Theory] @@ -72,7 +72,7 @@ public static void LongCountRunOnce(IEnumerable source, Func pre [Fact] public void NullableArray_IncludesNullValues() { - int?[] data = { -10, 4, 9, null, 11 }; + int?[] data = [-10, 4, 9, null, 11]; Assert.Equal(5, data.LongCount()); } diff --git a/tests/System.Linq.Tests/Tests/System.Linq/MaxTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/MaxTests.cs index 3c8da53d..c78637df 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/MaxTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/MaxTests.cs @@ -10,62 +10,7 @@ namespace System.Linq.Tests public class MaxTests : EnumerableTests { public static IEnumerable Max_AllTypes_TestData() - { - for (int length = 2; length < 65; length++) - { - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (byte)i)), (byte)(length + length - 1) }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (byte)i).ToArray()), (byte)(length + length - 1) }; - - // Unit Tests does +T.One so we should generate data up to one value below sbyte.MaxValue - if ((length + length) < sbyte.MaxValue) - { - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (sbyte)i)), (sbyte)(length + length - 1) }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (sbyte)i).ToArray()), (sbyte)(length + length - 1) }; - } - - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (ushort)i)), (ushort)(length + length - 1) }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (ushort)i).ToArray()), (ushort)(length + length - 1) }; - - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (short)i)), (short)(length + length - 1) }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (short)i).ToArray()), (short)(length + length - 1) }; - - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (char)i)), (char)(length + length - 1) }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (char)i).ToArray()), (char)(length + length - 1) }; - - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (uint)i)), (uint)(length + length - 1) }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (uint)i).ToArray()), (uint)(length + length - 1) }; - - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (int)i)), (int)(length + length - 1) }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (int)i).ToArray()), (int)(length + length - 1) }; - - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (ulong)i)), (ulong)(length + length - 1) }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (ulong)i).ToArray()), (ulong)(length + length - 1) }; - - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (long)i)), (long)(length + length - 1) }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (long)i).ToArray()), (long)(length + length - 1) }; - - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (float)i)), (float)(length + length - 1) }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (float)i).ToArray()), (float)(length + length - 1) }; - - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (double)i)), (double)(length + length - 1) }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (double)i).ToArray()), (double)(length + length - 1) }; - - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (decimal)i)), (decimal)(length + length - 1) }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (decimal)i).ToArray()), (decimal)(length + length - 1) }; - - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (nuint)i)), (nuint)(length + length - 1) }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (nuint)i).ToArray()), (nuint)(length + length - 1) }; - - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (nint)i)), (nint)(length + length - 1) }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (nint)i).ToArray()), (nint)(length + length - 1) }; - - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (Int128)i)), (Int128)(length + length - 1) }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (Int128)i).ToArray()), (Int128)(length + length - 1) }; - - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (UInt128)i)), (UInt128)(length + length - 1) }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (UInt128)i).ToArray()), (UInt128)(length + length - 1) }; - } - } + => System.Linq.MinMaxTestData.Max_AllTypes_TestData(); [Theory] [MemberData(nameof(Max_AllTypes_TestData))] @@ -123,21 +68,21 @@ public static IEnumerable Max_Int_TestData() { foreach ((int[] array, long expected) in new[] { - (new[] { 42 }, 42), + ([42], 42), (Enumerable.Range(1, 10).ToArray(), 10), - (new int[] { -100, -15, -50, -10 }, -10), - (new int[] { -16, 0, 50, 100, 1000 }, 1000), + ([-100, -15, -50, -10], -10), + ([-16, 0, 50, 100, 1000], 1000), (new int[] { -16, 0, 50, 100, 1000 }.Concat(Enumerable.Repeat(int.MaxValue, 1)).ToArray(), int.MaxValue), - (new[] { 20 }, 20), + ([20], 20), (Enumerable.Repeat(-2, 5).ToArray(), -2), - (new int[] { 16, 9, 10, 7, 8 }, 16), - (new int[] { 6, 9, 10, 0, 50 }, 50), - (new int[] { -6, 0, -9, 0, -10, 0 }, 0), + ([16, 9, 10, 7, 8], 16), + ([6, 9, 10, 0, 50], 50), + ([-6, 0, -9, 0, -10, 0], 0), }) { - yield return new object[] { new TestEnumerable(array), expected }; - yield return new object[] { array, expected }; + yield return [new TestEnumerable(array), expected]; + yield return [array, expected]; } } @@ -153,21 +98,21 @@ public static IEnumerable Max_Long_TestData() { foreach ((long[] array, long expected) in new[] { - (new[] { 42L }, 42L), + ([42L], 42L), (Enumerable.Range(1, 10).Select(i => (long)i).ToArray(), 10L), - (new long[] { -100, -15, -50, -10 }, -10L), - (new long[] { -16, 0, 50, 100, 1000 }, 1000L), + ([-100, -15, -50, -10], -10L), + ([-16, 0, 50, 100, 1000], 1000L), (new long[] { -16, 0, 50, 100, 1000 }.Concat(Enumerable.Repeat(long.MaxValue, 1)).ToArray(), long.MaxValue), - (new[] { int.MaxValue + 10L }, int.MaxValue + 10L), + ([int.MaxValue + 10L], int.MaxValue + 10L), (Enumerable.Repeat(500L, 5).ToArray(), 500L), - (new long[] { 250, 49, 130, 47, 28 }, 250L), - (new long[] { 6, 9, 10, 0, int.MaxValue + 50L }, int.MaxValue + 50L), - (new long[] { 6, 50, 9, 50, 10, 50 }, 50L), + ([250, 49, 130, 47, 28], 250L), + ([6, 9, 10, 0, int.MaxValue + 50L], int.MaxValue + 50L), + ([6, 50, 9, 50, 10, 50], 50L), }) { - yield return new object[] { new TestEnumerable(array), expected }; - yield return new object[] { array, expected }; + yield return [new TestEnumerable(array), expected]; + yield return [array, expected]; } } @@ -199,23 +144,23 @@ public static IEnumerable Max_Float_TestData() { foreach ((float[] array, float expected) in new[] { - (new[] { 42f }, 42f), + ([42f], 42f), (Enumerable.Range(1, 10).Select(i => (float)i).ToArray(), 10f), - (new float[] { -100, -15, -50, -10 }, -10f), - (new float[] { -16, 0, 50, 100, 1000 }, 1000f), + ([-100, -15, -50, -10], -10f), + ([-16, 0, 50, 100, 1000], 1000f), (new float[] { -16, 0, 50, 100, 1000 }.Concat(Enumerable.Repeat(float.MaxValue, 1)).ToArray(), float.MaxValue), - (new[] { 5.5f }, 5.5f), - (new float[] { 112.5f, 4.9f, 30f, 4.7f, 28f }, 112.5f), - (new float[] { 6.8f, 9.4f, -10f, 0f, float.NaN, 53.6f }, 53.6f), - (new float[] { -5.5f, float.PositiveInfinity, 9.9f, float.PositiveInfinity }, float.PositiveInfinity), + ([5.5f], 5.5f), + ([112.5f, 4.9f, 30f, 4.7f, 28f], 112.5f), + ([6.8f, 9.4f, -10f, 0f, float.NaN, 53.6f], 53.6f), + ([-5.5f, float.PositiveInfinity, 9.9f, float.PositiveInfinity], float.PositiveInfinity), (Enumerable.Repeat(float.NaN, 5).ToArray(), float.NaN), - (new float[] { float.NaN, 6.8f, 9.4f, 10f, 0, -5.6f }, 10f), - (new float[] { 6.8f, 9.4f, 10f, 0, -5.6f, float.NaN }, 10f), - (new float[] { float.NaN, float.NegativeInfinity }, float.NegativeInfinity), - (new float[] { float.NegativeInfinity, float.NaN }, float.NegativeInfinity), - + ([float.NaN, 6.8f, 9.4f, 10f, 0, -5.6f], 10f), + ([6.8f, 9.4f, 10f, 0, -5.6f, float.NaN], 10f), + ([float.NaN, float.NegativeInfinity], float.NegativeInfinity), + ([float.NegativeInfinity, float.NaN], float.NegativeInfinity), + // Normally NaN < anything and anything < NaN returns false // However, this leads to some irksome outcomes in Min and Max. // If we use those semantics then Min(NaN, 5.0) is NaN, but @@ -223,12 +168,12 @@ public static IEnumerable Max_Float_TestData() // ordering where NaN is smaller than every value, including // negative infinity. (Enumerable.Range(1, 10).Select(i => (float)i).Concat(Enumerable.Repeat(float.NaN, 1)).ToArray(), 10f), - (new float[] { -1f, -10, float.NaN, 10, 200, 1000 }, 1000f), - (new float[] { float.MinValue, 3000f, 100, 200, float.NaN, 1000 }, 3000f), + ([-1f, -10, float.NaN, 10, 200, 1000], 1000f), + ([float.MinValue, 3000f, 100, 200, float.NaN, 1000], 3000f), }) { - yield return new object[] { new TestEnumerable(array), expected }; - yield return new object[] { array, expected }; + yield return [new TestEnumerable(array), expected]; + yield return [array, expected]; } } @@ -267,14 +212,14 @@ public void Max_Float_SeveralNaNWithSelector() [Fact] public void Max_NullableFloat_SeveralNaNOrNullWithSelector() { - float?[] source = new float?[] { float.NaN, null, float.NaN, null }; + float?[] source = [float.NaN, null, float.NaN, null]; Assert.True(float.IsNaN(source.Max(i => i).Value)); } [Fact] public void Max_Float_NaNAtStartWithSelector() { - float[] source = { float.NaN, 6.8f, 9.4f, 10f, 0, -5.6f }; + float[] source = [float.NaN, 6.8f, 9.4f, 10f, 0, -5.6f]; Assert.Equal(10f, source.Max(i => i)); } @@ -282,21 +227,21 @@ public static IEnumerable Max_Double_TestData() { foreach ((double[] array, double expected) in new[] { - (new[] { 42.0 }, 42.0), + ([42.0], 42.0), (Enumerable.Range(1, 10).Select(i => (double)i).ToArray(), 10.0), - (new double[] { -100, -15, -50, -10 }, -10.0), - (new double[] { -16, 0, 50, 100, 1000 }, 1000.0), + ([-100, -15, -50, -10], -10.0), + ([-16, 0, 50, 100, 1000], 1000.0), (new double[] { -16, 0, 50, 100, 1000 }.Concat(Enumerable.Repeat(double.MaxValue, 1)).ToArray(), double.MaxValue), (Enumerable.Repeat(5.5, 1).ToArray(), 5.5), (Enumerable.Repeat(double.NaN, 5).ToArray(), double.NaN), - (new double[] { 112.5, 4.9, 30, 4.7, 28 }, 112.5), - (new double[] { 6.8, 9.4, -10, 0, double.NaN, 53.6 }, 53.6), - (new double[] { -5.5, double.PositiveInfinity, 9.9, double.PositiveInfinity }, double.PositiveInfinity), - (new double[] { double.NaN, 6.8, 9.4, 10.5, 0, -5.6 }, 10.5), - (new double[] { 6.8, 9.4, 10.5, 0, -5.6, double.NaN }, 10.5), - (new double[] { double.NaN, double.NegativeInfinity }, double.NegativeInfinity), - (new double[] { double.NegativeInfinity, double.NaN }, double.NegativeInfinity), + ([112.5, 4.9, 30, 4.7, 28], 112.5), + ([6.8, 9.4, -10, 0, double.NaN, 53.6], 53.6), + ([-5.5, double.PositiveInfinity, 9.9, double.PositiveInfinity], double.PositiveInfinity), + ([double.NaN, 6.8, 9.4, 10.5, 0, -5.6], 10.5), + ([6.8, 9.4, 10.5, 0, -5.6, double.NaN], 10.5), + ([double.NaN, double.NegativeInfinity], double.NegativeInfinity), + ([double.NegativeInfinity, double.NaN], double.NegativeInfinity), // Normally NaN < anything and anything < NaN returns false // However, this leads to some irksome outcomes in Min and Max. @@ -305,12 +250,12 @@ public static IEnumerable Max_Double_TestData() // ordering where NaN is smaller than every value, including // negative infinity. (Enumerable.Range(1, 10).Select(i => (double)i).Concat(Enumerable.Repeat(double.NaN, 1)).ToArray(), 10.0), - (new double[] { -1F, -10, double.NaN, 10, 200, 1000 }, 1000.0), - (new double[] { double.MinValue, 3000F, 100, 200, double.NaN, 1000 }, 3000.0), + ([-1F, -10, double.NaN, 10, 200, 1000], 1000.0), + ([double.MinValue, 3000F, 100, 200, double.NaN, 1000], 3000.0), }) { - yield return new object[] { new TestEnumerable(array), expected }; - yield return new object[] { array, expected }; + yield return [new TestEnumerable(array), expected]; + yield return [array, expected]; } } @@ -349,14 +294,14 @@ public void Max_Double_AllNaNWithSelector() [Fact] public void Max_Double_SeveralNaNOrNullWithSelector() { - double?[] source = new double?[] { double.NaN, null, double.NaN, null }; + double?[] source = [double.NaN, null, double.NaN, null]; Assert.True(double.IsNaN(source.Max(i => i).Value)); } [Fact] public void Max_Double_NaNThenNegativeInfinityWithSelector() { - double[] source = { double.NaN, double.NegativeInfinity }; + double[] source = [double.NaN, double.NegativeInfinity]; Assert.True(double.IsNegativeInfinity(source.Max(i => i))); } @@ -364,21 +309,21 @@ public static IEnumerable Max_Decimal_TestData() { foreach ((decimal[] array, decimal expected) in new[] { - (new[] { 42m }, 42m), + ([42m], 42m), (Enumerable.Range(1, 10).Select(i => (decimal)i).ToArray(), 10m), - (new decimal[] { -100, -15, -50, -10 }, -10m), - (new decimal[] { -16, 0, 50, 100, 1000 }, 1000m), + ([-100, -15, -50, -10], -10m), + ([-16, 0, 50, 100, 1000], 1000m), (new decimal[] { -16, 0, 50, 100, 1000 }.Concat(Enumerable.Repeat(decimal.MaxValue, 1)).ToArray(), decimal.MaxValue), - (new decimal[] { 5.5m }, 5.5m), + ([5.5m], 5.5m), (Enumerable.Repeat(-3.4m, 5).ToArray(), -3.4m), - (new decimal[] { 122.5m, 4.9m, 10m, 4.7m, 28m }, 122.5m), - (new decimal[] { 6.8m, 9.4m, 10m, 0m, 0m, decimal.MaxValue }, decimal.MaxValue), - (new decimal[] { -5.5m, 0m, 9.9m, -5.5m, 9.9m }, 9.9m), + ([122.5m, 4.9m, 10m, 4.7m, 28m], 122.5m), + ([6.8m, 9.4m, 10m, 0m, 0m, decimal.MaxValue], decimal.MaxValue), + ([-5.5m, 0m, 9.9m, -5.5m, 9.9m], 9.9m), }) { - yield return new object[] { new TestEnumerable(array), expected }; - yield return new object[] { array, expected }; + yield return [new TestEnumerable(array), expected]; + yield return [array, expected]; } } @@ -410,19 +355,19 @@ public void Max_Decimal_EmptySource_ThrowsInvalidOperationException() public static IEnumerable 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 [Enumerable.Repeat((int?)42, 1), 42]; + yield return [Enumerable.Range(1, 10).Select(i => (int?)i).ToArray(), 10]; + yield return [new int?[] { null, -100, -15, -50, -10 }, -10]; + yield return [new int?[] { null, -16, 0, 50, 100, 1000 }, 1000]; + yield return [new int?[] { null, -16, 0, 50, 100, 1000 }.Concat(Enumerable.Repeat((int?)int.MaxValue, 1)), int.MaxValue]; + yield return [Enumerable.Repeat(default(int?), 100), null]; - yield return new object[] { Enumerable.Empty(), 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 }; + yield return [Enumerable.Empty(), null]; + yield return [Enumerable.Repeat((int?)-20, 1), -20]; + yield return [new int?[] { -6, null, -9, -10, null, -17, -18 }, -6]; + yield return [new int?[] { null, null, null, null, null, -5 }, -5]; + yield return [new int?[] { 6, null, null, 100, 9, 100, 10, 100 }, 100]; + yield return [Enumerable.Repeat(default(int?), 5), null]; } [Theory] @@ -449,19 +394,19 @@ public void Max_NullableInt_NullSource_ThrowsArgumentNullException() public static IEnumerable 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 [Enumerable.Repeat((long?)42, 1), 42L]; + yield return [Enumerable.Range(1, 10).Select(i => (long?)i).ToArray(), 10L]; + yield return [new long?[] { null, -100, -15, -50, -10 }, -10L]; + yield return [new long?[] { null, -16, 0, 50, 100, 1000 }, 1000L]; + yield return [new long?[] { null, -16, 0, 50, 100, 1000 }.Concat(Enumerable.Repeat((long?)long.MaxValue, 1)), long.MaxValue]; + yield return [Enumerable.Repeat(default(long?), 100), null]; - yield return new object[] { Enumerable.Empty(), 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 }; + yield return [Enumerable.Empty(), null]; + yield return [Enumerable.Repeat((long?)long.MaxValue, 1), long.MaxValue]; + yield return [Enumerable.Repeat(default(long?), 5), null]; + yield return [new long?[] { long.MaxValue, null, 9, 10, null, 7, 8 }, long.MaxValue]; + yield return [new long?[] { null, null, null, null, null, -long.MaxValue }, -long.MaxValue]; + yield return [new long?[] { -6, null, null, 0, -9, 0, -10, -30 }, 0L]; } [Theory] @@ -481,28 +426,28 @@ public void Max_NullableLong_NullSource_ThrowsArgumentNullException() public static IEnumerable 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(), 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 }; + yield return [Enumerable.Repeat((float?)42, 1), 42f]; + yield return [Enumerable.Range(1, 10).Select(i => (float?)i).ToArray(), 10f]; + yield return [new float?[] { null, -100, -15, -50, -10 }, -10f]; + yield return [new float?[] { null, -16, 0, 50, 100, 1000 }, 1000f]; + yield return [new float?[] { null, -16, 0, 50, 100, 1000 }.Concat(Enumerable.Repeat((float?)float.MaxValue, 1)), float.MaxValue]; + yield return [Enumerable.Repeat(default(float?), 100), null]; + + yield return [Enumerable.Empty(), null]; + yield return [Enumerable.Repeat((float?)float.MinValue, 1), float.MinValue]; + yield return [Enumerable.Repeat(default(float?), 5), null]; + yield return [new float?[] { 14.50f, null, float.NaN, 10.98f, null, 7.5f, 8.6f }, 14.50f]; + yield return [new float?[] { null, null, null, null, null, 0f }, 0f]; + yield return [new float?[] { -6.4f, null, null, -0.5f, -9.4f, -0.5f, -10.9f, -0.5f }, -0.5f]; + + yield return [new float?[] { float.NaN, 6.8f, 9.4f, 10f, 0, null, -5.6f }, 10f]; + yield return [new float?[] { 6.8f, 9.4f, 10f, 0, null, -5.6f, float.NaN }, 10f]; + yield return [new float?[] { float.NaN, float.NegativeInfinity }, float.NegativeInfinity]; + yield return [new float?[] { float.NegativeInfinity, float.NaN }, float.NegativeInfinity]; + yield return [Enumerable.Repeat((float?)float.NaN, 3), float.NaN]; + yield return [new float?[] { float.NaN, null, null, null }, float.NaN]; + yield return [new float?[] { null, null, null, float.NaN }, float.NaN]; + yield return [new float?[] { null, float.NaN, null }, float.NaN]; } [Theory] @@ -522,28 +467,28 @@ public void Max_NullableFloat_NullSource_ThrowsArgumentNullException() public static IEnumerable 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(), 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 }; + yield return [Enumerable.Repeat((double?)42, 1), 42.0]; + yield return [Enumerable.Range(1, 10).Select(i => (double?)i).ToArray(), 10.0]; + yield return [new double?[] { null, -100, -15, -50, -10 }, -10.0]; + yield return [new double?[] { null, -16, 0, 50, 100, 1000 }, 1000.0]; + yield return [new double?[] { null, -16, 0, 50, 100, 1000 }.Concat(Enumerable.Repeat((double?)double.MaxValue, 1)), double.MaxValue]; + yield return [Enumerable.Repeat(default(double?), 100), null]; + + yield return [Enumerable.Empty(), null]; + yield return [Enumerable.Repeat((double?)double.MinValue, 1), double.MinValue]; + yield return [Enumerable.Repeat(default(double?), 5), null]; + yield return [new double?[] { 14.50, null, double.NaN, 10.98, null, 7.5, 8.6 }, 14.50]; + yield return [new double?[] { null, null, null, null, null, 0 }, 0.0]; + yield return [new double?[] { -6.4, null, null, -0.5, -9.4, -0.5, -10.9, -0.5 }, -0.5]; + + yield return [new double?[] { double.NaN, 6.8, 9.4, 10.5, 0, null, -5.6 }, 10.5]; + yield return [new double?[] { 6.8, 9.4, 10.8, 0, null, -5.6, double.NaN }, 10.8]; + yield return [new double?[] { double.NaN, double.NegativeInfinity }, double.NegativeInfinity]; + yield return [new double?[] { double.NegativeInfinity, double.NaN }, double.NegativeInfinity]; + yield return [Enumerable.Repeat((double?)double.NaN, 3), double.NaN]; + yield return [new double?[] { double.NaN, null, null, null }, double.NaN]; + yield return [new double?[] { null, null, null, double.NaN }, double.NaN]; + yield return [new double?[] { null, double.NaN, null }, double.NaN]; } [Theory] @@ -563,19 +508,19 @@ public void Max_NullableDouble_NullSource_ThrowsArgumentNullException() public static IEnumerable 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 [Enumerable.Repeat((decimal?)42, 1), 42m]; + yield return [Enumerable.Range(1, 10).Select(i => (decimal?)i).ToArray(), 10m]; + yield return [new decimal?[] { null, -100M, -15, -50, -10 }, -10m]; + yield return [new decimal?[] { null, -16M, 0, 50, 100, 1000 }, 1000m]; + yield return [new decimal?[] { null, -16M, 0, 50, 100, 1000 }.Concat(Enumerable.Repeat((decimal?)decimal.MaxValue, 1)), decimal.MaxValue]; + yield return [Enumerable.Repeat(default(decimal?), 100), null]; - yield return new object[] { Enumerable.Empty(), 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 }; + yield return [Enumerable.Empty(), null]; + yield return [Enumerable.Repeat((decimal?)decimal.MaxValue, 1), decimal.MaxValue]; + yield return [Enumerable.Repeat(default(decimal?), 5), null]; + yield return [new decimal?[] { 14.50m, null, null, 10.98m, null, 7.5m, 8.6m }, 14.50m]; + yield return [new decimal?[] { null, null, null, null, null, 0m }, 0m]; + yield return [new decimal?[] { 6.4m, null, null, decimal.MaxValue, 9.4m, decimal.MaxValue, 10.9m, decimal.MaxValue }, decimal.MaxValue]; } [Theory] @@ -595,18 +540,20 @@ public void Max_NullableDecimal_NullSource_ThrowsArgumentNullException() public static IEnumerable Max_DateTime_TestData() { - yield return new object[] { Enumerable.Range(1, 10).Select(i => new DateTime(2000, 1, i)).ToArray(), new DateTime(2000, 1, 10) }; - yield return new object[] { new DateTime[] { new DateTime(2000, 12, 1), new DateTime(2000, 12, 31), new DateTime(2000, 1, 12) }, new DateTime(2000, 12, 31) }; + yield return [Enumerable.Range(1, 10).Select(i => new DateTime(2000, 1, i)).ToArray(), new DateTime(2000, 1, 10) + ]; + yield return [new DateTime[] { new DateTime(2000, 12, 1), new DateTime(2000, 12, 31), new DateTime(2000, 1, 12) }, new DateTime(2000, 12, 31) + ]; - DateTime[] threeThousand = new DateTime[] - { + DateTime[] threeThousand = + [ new DateTime(3000, 1, 1), new DateTime(100, 1, 1), new DateTime(200, 1, 1), new DateTime(1000, 1, 1) - }; - yield return new object[] { threeThousand, new DateTime(3000, 1, 1) }; - yield return new object[] { threeThousand.Concat(Enumerable.Repeat(DateTime.MaxValue, 1)), DateTime.MaxValue }; + ]; + yield return [threeThousand, new DateTime(3000, 1, 1)]; + yield return [threeThousand.Concat(Enumerable.Repeat(DateTime.MaxValue, 1)), DateTime.MaxValue]; } [Theory] @@ -635,17 +582,17 @@ public void Max_DateTime_EmptySource_ThrowsInvalidOperationException() public static IEnumerable 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 [Enumerable.Range(1, 10).Select(i => i.ToString()).ToArray(), "9"]; + yield return [new string[] { "Alice", "Bob", "Charlie", "Eve", "Mallory", "Victor", "Trent" }, "Victor"]; + yield return [new string[] { null, "Charlie", null, "Victor", "Trent", null, "Eve", "Alice", "Mallory", "Bob" }, "Victor"]; - yield return new object[] { Enumerable.Empty(), 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 }; + yield return [Enumerable.Empty(), null]; + yield return [Enumerable.Repeat("Hello", 1), "Hello"]; + yield return [Enumerable.Repeat("hi", 5), "hi"]; + yield return [new string[] { "zzz", "aaa", "abcd", "bark", "temp", "cat" }, "zzz"]; + yield return [new string[] { null, null, null, null, "aAa" }, "aAa"]; + yield return [new string[] { "ooo", "ccc", "ccc", "ooo", "ooo", "nnn" }, "ooo"]; + yield return [Enumerable.Repeat(default(string), 5), null]; } [Theory] @@ -940,7 +887,7 @@ public static IEnumerable Max_Generic_TestData() expected: null); yield return WrapArgs( - source: Enumerable.Empty(), + source: [], comparer: Comparer.Create((_, _) => 0), expected: null); @@ -960,17 +907,17 @@ public static IEnumerable Max_Generic_TestData() expected: 0); yield return WrapArgs( - source: new string[] { "Aardvark", "Zyzzyva", "Zebra", "Antelope" }, + source: ["Aardvark", "Zyzzyva", "Zebra", "Antelope"], comparer: null, expected: "Zyzzyva"); yield return WrapArgs( - source: new string[] { "Aardvark", "Zyzzyva", "Zebra", "Antelope" }, + source: ["Aardvark", "Zyzzyva", "Zebra", "Antelope"], comparer: Comparer.Create((x, y) => -x.CompareTo(y)), expected: "Aardvark"); object[] WrapArgs(IEnumerable source, IComparer? comparer, TSource? expected) - => new object[] { source, comparer, expected }; + => [source, comparer, expected]; } [Fact] @@ -986,7 +933,7 @@ public static void MaxBy_Generic_NullSource_ThrowsArgumentNullException() [Fact] public static void MaxBy_Generic_NullKeySelector_ThrowsArgumentNullException() { - IEnumerable source = Enumerable.Empty(); + IEnumerable source = []; Func keySelector = null; AssertExtensions.Throws("keySelector", () => source.MaxBy(keySelector)); @@ -1089,13 +1036,13 @@ public static IEnumerable MaxBy_Generic_TestData() expected: 0); yield return WrapArgs( - source: new string[] { "Aardvark", "Zyzzyva", "Zebra", "Antelope" }, + source: ["Aardvark", "Zyzzyva", "Zebra", "Antelope"], keySelector: x => x, comparer: null, expected: "Zyzzyva"); yield return WrapArgs( - source: new string[] { "Aardvark", "Zyzzyva", "Zebra", "Antelope" }, + source: ["Aardvark", "Zyzzyva", "Zebra", "Antelope"], keySelector: x => x, comparer: Comparer.Create((x, y) => -x.CompareTo(y)), expected: "Aardvark"); @@ -1131,7 +1078,7 @@ public static IEnumerable MaxBy_Generic_TestData() expected: (Name: "Harry", Age: 20)); object[] WrapArgs(IEnumerable source, Func keySelector, IComparer? comparer, TSource? expected) - => new object[] { source, keySelector, comparer, expected }; + => [source, keySelector, comparer, expected]; } } } diff --git a/tests/System.Linq.Tests/Tests/System.Linq/MinTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/MinTests.cs index 91779492..b1e0515d 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/MinTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/MinTests.cs @@ -10,53 +10,7 @@ namespace System.Linq.Tests public class MinTests : EnumerableTests { public static IEnumerable Min_AllTypes_TestData() - { - for (int length = 2; length < 65; length++) - { - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (byte)i)), (byte)length }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (byte)i).ToArray()), (byte)length }; - - // Unit Tests does +T.One so we should generate data up to one value below sbyte.MaxValue, otherwise the type overflows - if ((length + length) < sbyte.MaxValue) - { - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (sbyte)i)), (sbyte)length }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (sbyte)i).ToArray()), (sbyte)length }; - } - - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (ushort)i)), (ushort)length }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (ushort)i).ToArray()), (ushort)length }; - - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (short)i)), (short)length }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (short)i).ToArray()), (short)length }; - - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (uint)i)), (uint)length }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (uint)i).ToArray()), (uint)length }; - - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (int)i)), (int)length }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (int)i).ToArray()), (int)length }; - - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (ulong)i)), (ulong)length }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (ulong)i).ToArray()), (ulong)length }; - - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (long)i)), (long)length }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (long)i).ToArray()), (long)length }; - - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (float)i)), (float)length }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (float)i).ToArray()), (float)length }; - - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (double)i)), (double)length }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (double)i).ToArray()), (double)length }; - - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (decimal)i)), (decimal)length }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (decimal)i).ToArray()), (decimal)length }; - - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (nuint)i)), (nuint)length }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (nuint)i).ToArray()), (nuint)length }; - - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (nint)i)), (nint)length }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (nint)i).ToArray()), (nint)length }; - } - } + => System.Linq.MinMaxTestData.Min_AllTypes_TestData(); [Theory] [MemberData(nameof(Min_AllTypes_TestData))] @@ -98,22 +52,22 @@ public static IEnumerable Min_Int_TestData() { foreach ((int[] array, long expected) in new[] { - (new[] { 42 }, 42), + ([42], 42), (Enumerable.Range(1, 10).ToArray(), 1), - (new int[] { -1, -10, 10, 200, 1000 }, -10), - (new int[] { 3000, 100, 200, 1000 }, 100), + ([-1, -10, 10, 200, 1000], -10), + ([3000, 100, 200, 1000], 100), (new int[] { 3000, 100, 200, 1000 }.Concat(Enumerable.Repeat(int.MinValue, 1)).ToArray(), int.MinValue), - (new[] { 20 }, 20), + ([20], 20), (Enumerable.Repeat(-2, 5).ToArray(), -2), (Enumerable.Range(1, 10).ToArray(), 1), - (new int[] { 6, 9, 10, 7, 8 }, 6), - (new int[] { 6, 9, 10, 0, -5 }, -5), - (new int[] { 6, 0, 9, 0, 10, 0 }, 0), + ([6, 9, 10, 7, 8], 6), + ([6, 9, 10, 0, -5], -5), + ([6, 0, 9, 0, 10, 0], 0), }) { - yield return new object[] { new TestEnumerable(array), expected }; - yield return new object[] { array, expected }; + yield return [new TestEnumerable(array), expected]; + yield return [array, expected]; } } @@ -147,21 +101,21 @@ public static IEnumerable Min_Long_TestData() { foreach ((long[] array, long expected) in new[] { - (new[] { 42L }, 42L), + ([42L], 42L), (Enumerable.Range(1, 10).Select(i => (long)i).ToArray(), 1L), - (new long[] { -1, -10, 10, 200, 1000 }, -10L), - (new long[] { 3000, 100, 200, 1000 }, 100L), + ([-1, -10, 10, 200, 1000], -10L), + ([3000, 100, 200, 1000], 100L), (new long[] { 3000, 100, 200, 1000 }.Concat(Enumerable.Repeat(long.MinValue, 1)).ToArray(), long.MinValue), - (new[] { int.MaxValue + 10L }, int.MaxValue + 10L), + ([int.MaxValue + 10L], int.MaxValue + 10L), (Enumerable.Repeat(500L, 5).ToArray(), 500L), - (new long[] { -250, 49, 130, 47, 28 }, -250L), - (new long[] { 6, 9, 10, 0, -int.MaxValue - 50L }, -int.MaxValue - 50L), - (new long[] { 6, -5, 9, -5, 10, -5 }, -5), + ([-250, 49, 130, 47, 28], -250L), + ([6, 9, 10, 0, -int.MaxValue - 50L], -int.MaxValue - 50L), + ([6, -5, 9, -5, 10, -5], -5), }) { - yield return new object[] { new TestEnumerable(array), expected }; - yield return new object[] { array, expected }; + yield return [new TestEnumerable(array), expected]; + yield return [array, expected]; } } @@ -195,22 +149,22 @@ public static IEnumerable Min_Float_TestData() { foreach ((float[] array, float expected) in new[] { - (new[] { 42f }, 42f), + ([42f], 42f), (Enumerable.Range(1, 10).Select(i => (float)i).ToArray(), 1f), - (new float[] { -1, -10, 10, 200, 1000 }, -10f), - (new float[] { 3000, 100, 200, 1000 }, 100), + ([-1, -10, 10, 200, 1000], -10f), + ([3000, 100, 200, 1000], 100), (new float[] { 3000, 100, 200, 1000 }.Concat(Enumerable.Repeat(float.MinValue, 1)).ToArray(), float.MinValue), - (new[] { 5.5f }, 5.5f), + ([5.5f], 5.5f), (Enumerable.Repeat(float.NaN, 5).ToArray(), float.NaN), - (new float[] { -2.5f, 4.9f, 130f, 4.7f, 28f }, -2.5f), - (new float[] { 6.8f, 9.4f, 10f, 0, -5.6f }, -5.6f), - (new float[] { -5.5f, float.NegativeInfinity, 9.9f, float.NegativeInfinity }, float.NegativeInfinity), + ([-2.5f, 4.9f, 130f, 4.7f, 28f], -2.5f), + ([6.8f, 9.4f, 10f, 0, -5.6f], -5.6f), + ([-5.5f, float.NegativeInfinity, 9.9f, float.NegativeInfinity], float.NegativeInfinity), - (new float[] { float.NaN, 6.8f, 9.4f, 10f, 0, -5.6f }, float.NaN), - (new float[] { 6.8f, 9.4f, 10f, 0, -5.6f, float.NaN }, float.NaN), - (new float[] { float.NaN, float.NegativeInfinity }, float.NaN), - (new float[] { float.NegativeInfinity, float.NaN }, float.NaN), + ([float.NaN, 6.8f, 9.4f, 10f, 0, -5.6f], float.NaN), + ([6.8f, 9.4f, 10f, 0, -5.6f, float.NaN], float.NaN), + ([float.NaN, float.NegativeInfinity], float.NaN), + ([float.NegativeInfinity, float.NaN], float.NaN), // Normally NaN < anything is false, as is anything < NaN // However, this leads to some irksome outcomes in Min and Max. @@ -219,20 +173,20 @@ public static IEnumerable Min_Float_TestData() // ordering where NaN is smaller than every value, including // negative infinity. (Enumerable.Range(1, 10).Select(i => (float)i).Concat(Enumerable.Repeat(float.NaN, 1)).ToArray(), float.NaN), - (new float[] { -1F, -10, float.NaN, 10, 200, 1000 }, float.NaN), - (new float[] { float.MinValue, 3000F, 100, 200, float.NaN, 1000 }, float.NaN), + ([-1F, -10, float.NaN, 10, 200, 1000], float.NaN), + ([float.MinValue, 3000F, 100, 200, float.NaN, 1000], float.NaN), }) { - yield return new object[] { new TestEnumerable(array), expected }; - yield return new object[] { array, expected }; + yield return [new TestEnumerable(array), expected]; + yield return [array, expected]; } // In .NET Core, Enumerable.Min shortcircuits if it finds any float.NaN in the array, // as nothing can be less than float.NaN. See https://github.com/dotnet/corefx/pull/2426. // Without this optimization, we would iterate through int.MaxValue elements, which takes // a long time. - yield return new object[] { Enumerable.Repeat(float.NaN, int.MaxValue), float.NaN }; - yield return new object[] { Enumerable.Repeat(float.NaN, 3).ToArray(), float.NaN }; + yield return [Enumerable.Repeat(float.NaN, int.MaxValue), float.NaN]; + yield return [Enumerable.Repeat(float.NaN, 3).ToArray(), float.NaN]; } [Theory] @@ -265,21 +219,21 @@ public static IEnumerable Min_Double_TestData() { foreach ((double[] array, double expected) in new[] { - (new[] { 42.0 }, 42.0), + ([42.0], 42.0), (Enumerable.Range(1, 10).Select(i => (double)i).ToArray(), 1.0 ), - (new double[] { -1, -10, 10, 200, 1000 }, -10.0), - (new double[] { 3000, 100, 200, 1000 }, 100.0), + ([-1, -10, 10, 200, 1000], -10.0), + ([3000, 100, 200, 1000], 100.0), (new double[] { 3000, 100, 200, 1000 }.Concat(Enumerable.Repeat(double.MinValue, 1)).ToArray(), double.MinValue), - (new[] { 5.5 }, 5.5), - (new double[] { -2.5, 4.9, 130, 4.7, 28 }, -2.5), - (new double[] { 6.8, 9.4, 10, 0, -5.6 }, -5.6), - (new double[] { -5.5, double.NegativeInfinity, 9.9, double.NegativeInfinity }, double.NegativeInfinity), + ([5.5], 5.5), + ([-2.5, 4.9, 130, 4.7, 28], -2.5), + ([6.8, 9.4, 10, 0, -5.6], -5.6), + ([-5.5, double.NegativeInfinity, 9.9, double.NegativeInfinity], double.NegativeInfinity), - (new double[] { double.NaN, 6.8, 9.4, 10, 0, -5.6 }, double.NaN), - (new double[] { 6.8, 9.4, 10, 0, -5.6, double.NaN }, double.NaN), - (new double[] { double.NaN, double.NegativeInfinity }, double.NaN), - (new double[] { double.NegativeInfinity, double.NaN }, double.NaN), + ([double.NaN, 6.8, 9.4, 10, 0, -5.6], double.NaN), + ([6.8, 9.4, 10, 0, -5.6, double.NaN], double.NaN), + ([double.NaN, double.NegativeInfinity], double.NaN), + ([double.NegativeInfinity, double.NaN], double.NaN), // Normally NaN < anything is false, as is anything < NaN // However, this leads to some irksome outcomes in Min and Max. @@ -288,19 +242,19 @@ public static IEnumerable Min_Double_TestData() // ordering where NaN is smaller than every value, including // negative infinity. (Enumerable.Range(1, 10).Select(i => (double)i).Concat(Enumerable.Repeat(double.NaN, 1)).ToArray(), double.NaN), - (new double[] { -1, -10, double.NaN, 10, 200, 1000 }, double.NaN), - (new double[] { double.MinValue, 3000F, 100, 200, double.NaN, 1000 }, double.NaN), + ([-1, -10, double.NaN, 10, 200, 1000], double.NaN), + ([double.MinValue, 3000F, 100, 200, double.NaN, 1000], double.NaN), }) { - yield return new object[] { new TestEnumerable(array), expected }; - yield return new object[] { array, expected }; + yield return [new TestEnumerable(array), expected]; + yield return [array, expected]; } // In .NET Core, Enumerable.Min shortcircuits if it finds any double.NaN in the array, // as nothing can be less than double.NaN. See https://github.com/dotnet/corefx/pull/2426. // Without this optimization, we would iterate through int.MaxValue elements, which takes // a long time. - yield return new object[] { Enumerable.Repeat(double.NaN, int.MaxValue), double.NaN }; + yield return [Enumerable.Repeat(double.NaN, int.MaxValue), double.NaN]; } [Theory] @@ -333,21 +287,21 @@ public static IEnumerable Min_Decimal_TestData() { foreach ((decimal[] array, decimal expected) in new[] { - (new[] { 42m }, 42m), + ([42m], 42m), (Enumerable.Range(1, 10).Select(i => (decimal)i).ToArray(), 1m), - (new decimal[] { -1, -10, 10, 200, 1000 }, -10m), - (new decimal[] { 3000, 100, 200, 1000 }, 100m), + ([-1, -10, 10, 200, 1000], -10m), + ([3000, 100, 200, 1000], 100m), (new decimal[] { 3000, 100, 200, 1000 }.Concat(Enumerable.Repeat(decimal.MinValue, 1)).ToArray(), decimal.MinValue), - (new[] { 5.5m }, 5.5m), + ([5.5m], 5.5m), (Enumerable.Repeat(-3.4m, 5).ToArray(), -3.4m), - (new decimal[] { -2.5m, 4.9m, 130m, 4.7m, 28m }, -2.5m), - (new decimal[] { 6.8m, 9.4m, 10m, 0m, 0m, decimal.MinValue }, decimal.MinValue), - (new decimal[] { -5.5m, 0m, 9.9m, -5.5m, 5m }, -5.5m), + ([-2.5m, 4.9m, 130m, 4.7m, 28m], -2.5m), + ([6.8m, 9.4m, 10m, 0m, 0m, decimal.MinValue], decimal.MinValue), + ([-5.5m, 0m, 9.9m, -5.5m, 5m], -5.5m), }) { - yield return new object[] { new TestEnumerable(array), expected }; - yield return new object[] { array, expected }; + yield return [new TestEnumerable(array), expected]; + yield return [array, expected]; } } @@ -379,19 +333,19 @@ public void Min_Decimal_NullSource_ThrowsArgumentNullException() public static IEnumerable 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 [Enumerable.Range(1, 10).Select(i => (int?)i).ToArray(), 1]; + yield return [new int?[] { null, -1, -10, 10, 200, 1000 }, -10]; + yield return [new int?[] { null, 3000, 100, 200, 1000 }, 100]; + yield return [new int?[] { null, 3000, 100, 200, 1000 }.Concat(Enumerable.Repeat((int?)int.MinValue, 1)), int.MinValue]; + yield return [Enumerable.Repeat(default(int?), 100), null]; + yield return [Enumerable.Repeat((int?)42, 1), 42]; - yield return new object[] { Enumerable.Empty(), 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 }; + yield return [Enumerable.Empty(), null]; + yield return [Enumerable.Repeat((int?)20, 1), 20]; + yield return [Enumerable.Repeat(default(int?), 5), null]; + yield return [new int?[] { 6, null, 9, 10, null, 7, 8 }, 6]; + yield return [new int?[] { null, null, null, null, null, -5 }, -5]; + yield return [new int?[] { 6, null, null, 0, 9, 0, 10, 0 }, 0]; } [Theory] @@ -416,19 +370,19 @@ public void Min_NullableInt_NullSource_ThrowsArgumentNullException() public static IEnumerable Min_NullableLong_TestData() { - yield return new object[] { Enumerable.Range(1, 10).Select(i => (long?)i).ToArray(), 1L }; - yield return new object[] { new long?[] { null, -1, -10, 10, 200, 1000 }, -10L }; - yield return new object[] { new long?[] { null, 3000, 100, 200, 1000 }, 100L }; - yield return new object[] { new long?[] { null, 3000, 100, 200, 1000 }.Concat(Enumerable.Repeat((long?)long.MinValue, 1)), long.MinValue }; - yield return new object[] { Enumerable.Repeat(default(long?), 100), null }; - yield return new object[] { Enumerable.Repeat((long?)42, 1), 42L }; + yield return [Enumerable.Range(1, 10).Select(i => (long?)i).ToArray(), 1L]; + yield return [new long?[] { null, -1, -10, 10, 200, 1000 }, -10L]; + yield return [new long?[] { null, 3000, 100, 200, 1000 }, 100L]; + yield return [new long?[] { null, 3000, 100, 200, 1000 }.Concat(Enumerable.Repeat((long?)long.MinValue, 1)), long.MinValue]; + yield return [Enumerable.Repeat(default(long?), 100), null]; + yield return [Enumerable.Repeat((long?)42, 1), 42L]; - yield return new object[] { Enumerable.Empty(), 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.MinValue, null, 9, 10, null, 7, 8 }, long.MinValue }; - 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, 0 }, 0L }; + yield return [Enumerable.Empty(), null]; + yield return [Enumerable.Repeat((long?)long.MaxValue, 1), long.MaxValue]; + yield return [Enumerable.Repeat(default(long?), 5), null]; + yield return [new long?[] { long.MinValue, null, 9, 10, null, 7, 8 }, long.MinValue]; + yield return [new long?[] { null, null, null, null, null, -long.MaxValue }, -long.MaxValue]; + yield return [new long?[] { 6, null, null, 0, 9, 0, 10, 0 }, 0L]; } [Theory] @@ -447,34 +401,34 @@ public void Min_NullableLong_NullSource_ThrowsArgumentNullException() public static IEnumerable Min_NullableFloat_TestData() { - yield return new object[] { Enumerable.Range(1, 10).Select(i => (float?)i).ToArray(), 1f }; - yield return new object[] { new float?[] { null, -1, -10, 10, 200, 1000 }, -10f }; - yield return new object[] { new float?[] { null, 3000, 100, 200, 1000 }, 100f }; - yield return new object[] { new float?[] { null, 3000, 100, 200, 1000 }.Concat(Enumerable.Repeat((float?)float.MinValue, 1)), float.MinValue }; - yield return new object[] { Enumerable.Repeat(default(float?), 100), null }; - yield return new object[] { Enumerable.Repeat((float?)42, 1), 42f }; - - yield return new object[] { Enumerable.Empty(), null }; - yield return new object[] { Enumerable.Repeat((float?)float.MinValue, 1), float.MinValue }; - yield return new object[] { Enumerable.Repeat(default(float?), 100), null }; - yield return new object[] { new float?[] { -4.50f, null, 10.98f, null, 7.5f, 8.6f }, -4.5f }; - 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 }, float.NaN }; - yield return new object[] { new float?[] { 6.8f, 9.4f, 10f, 0, null, -5.6f, float.NaN }, float.NaN }; - yield return new object[] { new float?[] { float.NaN, float.NegativeInfinity }, float.NaN }; - yield return new object[] { new float?[] { float.NegativeInfinity, float.NaN }, 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 }; + yield return [Enumerable.Range(1, 10).Select(i => (float?)i).ToArray(), 1f]; + yield return [new float?[] { null, -1, -10, 10, 200, 1000 }, -10f]; + yield return [new float?[] { null, 3000, 100, 200, 1000 }, 100f]; + yield return [new float?[] { null, 3000, 100, 200, 1000 }.Concat(Enumerable.Repeat((float?)float.MinValue, 1)), float.MinValue]; + yield return [Enumerable.Repeat(default(float?), 100), null]; + yield return [Enumerable.Repeat((float?)42, 1), 42f]; + + yield return [Enumerable.Empty(), null]; + yield return [Enumerable.Repeat((float?)float.MinValue, 1), float.MinValue]; + yield return [Enumerable.Repeat(default(float?), 100), null]; + yield return [new float?[] { -4.50f, null, 10.98f, null, 7.5f, 8.6f }, -4.5f]; + yield return [new float?[] { null, null, null, null, null, 0f }, 0f]; + yield return [new float?[] { 6.4f, null, null, -0.5f, 9.4f, -0.5f, 10.9f, -0.5f }, -0.5f]; + + yield return [new float?[] { float.NaN, 6.8f, 9.4f, 10f, 0, null, -5.6f }, float.NaN]; + yield return [new float?[] { 6.8f, 9.4f, 10f, 0, null, -5.6f, float.NaN }, float.NaN]; + yield return [new float?[] { float.NaN, float.NegativeInfinity }, float.NaN]; + yield return [new float?[] { float.NegativeInfinity, float.NaN }, float.NaN]; + yield return [new float?[] { float.NaN, null, null, null }, float.NaN]; + yield return [new float?[] { null, null, null, float.NaN }, float.NaN]; + yield return [new float?[] { null, float.NaN, null }, float.NaN]; // In .NET Core, Enumerable.Min shortcircuits if it finds any float.NaN in the array, // as nothing can be less than float.NaN. See https://github.com/dotnet/corefx/pull/2426. // Without this optimization, we would iterate through int.MaxValue elements, which takes // a long time. - yield return new object[] { Enumerable.Repeat((float?)float.NaN, int.MaxValue), float.NaN }; - yield return new object[] { Enumerable.Repeat((float?)float.NaN, 3), float.NaN }; + yield return [Enumerable.Repeat((float?)float.NaN, int.MaxValue), float.NaN]; + yield return [Enumerable.Repeat((float?)float.NaN, 3), float.NaN]; } [Theory] @@ -494,34 +448,34 @@ public void Min_NullableFloat_NullSource_ThrowsArgumentNullException() public static IEnumerable Min_NullableDouble_TestData() { - yield return new object[] { Enumerable.Range(1, 10).Select(i => (double?)i).ToArray(), 1.0 }; - yield return new object[] { new double?[] { null, -1, -10, 10, 200, 1000 }, -10.0 }; - yield return new object[] { new double?[] { null, 3000, 100, 200, 1000 }, 100.0 }; - yield return new object[] { new double?[] { null, 3000, 100, 200, 1000 }.Concat(Enumerable.Repeat((double?)double.MinValue, 1)), double.MinValue }; - yield return new object[] { Enumerable.Repeat(default(double?), 100), null }; - yield return new object[] { Enumerable.Repeat((double?)42, 1), 42.0 }; - - yield return new object[] { Enumerable.Empty(), 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?[] { -4.50, null, 10.98, null, 7.5, 8.6 }, -4.5 }; - 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.0, 0.0, null, -5.6 }, double.NaN }; - yield return new object[] { new double?[] { 6.8, 9.4, 10, 0.0, null, -5.6f, double.NaN }, double.NaN }; - yield return new object[] { new double?[] { double.NaN, double.NegativeInfinity }, double.NaN }; - yield return new object[] { new double?[] { double.NegativeInfinity, double.NaN }, 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 }; + yield return [Enumerable.Range(1, 10).Select(i => (double?)i).ToArray(), 1.0]; + yield return [new double?[] { null, -1, -10, 10, 200, 1000 }, -10.0]; + yield return [new double?[] { null, 3000, 100, 200, 1000 }, 100.0]; + yield return [new double?[] { null, 3000, 100, 200, 1000 }.Concat(Enumerable.Repeat((double?)double.MinValue, 1)), double.MinValue]; + yield return [Enumerable.Repeat(default(double?), 100), null]; + yield return [Enumerable.Repeat((double?)42, 1), 42.0]; + + yield return [Enumerable.Empty(), null]; + yield return [Enumerable.Repeat((double?)double.MinValue, 1), double.MinValue]; + yield return [Enumerable.Repeat(default(double?), 5), null]; + yield return [new double?[] { -4.50, null, 10.98, null, 7.5, 8.6 }, -4.5]; + yield return [new double?[] { null, null, null, null, null, 0 }, 0.0]; + yield return [new double?[] { 6.4, null, null, -0.5, 9.4, -0.5, 10.9, -0.5 }, -0.5]; + + yield return [new double?[] { double.NaN, 6.8, 9.4, 10.0, 0.0, null, -5.6 }, double.NaN]; + yield return [new double?[] { 6.8, 9.4, 10, 0.0, null, -5.6f, double.NaN }, double.NaN]; + yield return [new double?[] { double.NaN, double.NegativeInfinity }, double.NaN]; + yield return [new double?[] { double.NegativeInfinity, double.NaN }, double.NaN]; + yield return [new double?[] { double.NaN, null, null, null }, double.NaN]; + yield return [new double?[] { null, null, null, double.NaN }, double.NaN]; + yield return [new double?[] { null, double.NaN, null }, double.NaN]; // In .NET Core, Enumerable.Min shortcircuits if it finds any double.NaN in the array, // as nothing can be less than double.NaN. See https://github.com/dotnet/corefx/pull/2426. // Without this optimization, we would iterate through int.MaxValue elements, which takes // a long time. - yield return new object[] { Enumerable.Repeat((double?)double.NaN, int.MaxValue), double.NaN }; - yield return new object[] { Enumerable.Repeat((double?)double.NaN, 3), double.NaN }; + yield return [Enumerable.Repeat((double?)double.NaN, int.MaxValue), double.NaN]; + yield return [Enumerable.Repeat((double?)double.NaN, 3), double.NaN]; } [Theory] @@ -540,19 +494,19 @@ public void Min_NullableDouble_NullSource_ThrowsArgumentNullException() public static IEnumerable Min_NullableDecimal_TestData() { - yield return new object[] { Enumerable.Range(1, 10).Select(i => (decimal?)i).ToArray(), 1m }; - yield return new object[] { new decimal?[] { null, -1, -10, 10, 200, 1000 }, -10m }; - yield return new object[] { new decimal?[] { null, 3000, 100, 200, 1000 }, 100m }; - yield return new object[] { new decimal?[] { null, 3000, 100, 200, 1000 }.Concat(Enumerable.Repeat((decimal?)decimal.MinValue, 1)), decimal.MinValue }; - yield return new object[] { Enumerable.Repeat(default(decimal?), 100), null }; - yield return new object[] { Enumerable.Repeat((decimal?)42, 1), 42m }; + yield return [Enumerable.Range(1, 10).Select(i => (decimal?)i).ToArray(), 1m]; + yield return [new decimal?[] { null, -1, -10, 10, 200, 1000 }, -10m]; + yield return [new decimal?[] { null, 3000, 100, 200, 1000 }, 100m]; + yield return [new decimal?[] { null, 3000, 100, 200, 1000 }.Concat(Enumerable.Repeat((decimal?)decimal.MinValue, 1)), decimal.MinValue]; + yield return [Enumerable.Repeat(default(decimal?), 100), null]; + yield return [Enumerable.Repeat((decimal?)42, 1), 42m]; - yield return new object[] { Enumerable.Empty(), 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?[] { -4.50m, null, null, 10.98m, null, 7.5m, 8.6m }, -4.5m }; - yield return new object[] { new decimal?[] { null, null, null, null, null, 0m }, 0m }; - yield return new object[] { new decimal?[] { 6.4m, null, null, decimal.MinValue, 9.4m, decimal.MinValue, 10.9m, decimal.MinValue }, decimal.MinValue }; + yield return [Enumerable.Empty(), null]; + yield return [Enumerable.Repeat((decimal?)decimal.MaxValue, 1), decimal.MaxValue]; + yield return [Enumerable.Repeat(default(decimal?), 5), null]; + yield return [new decimal?[] { -4.50m, null, null, 10.98m, null, 7.5m, 8.6m }, -4.5m]; + yield return [new decimal?[] { null, null, null, null, null, 0m }, 0m]; + yield return [new decimal?[] { 6.4m, null, null, decimal.MinValue, 9.4m, decimal.MinValue, 10.9m, decimal.MinValue }, decimal.MinValue]; } [Theory] @@ -572,18 +526,20 @@ public void Min_NullableDecimal_NullSource_ThrowsArgumentNullException() public static IEnumerable Min_DateTime_TestData() { - yield return new object[] { Enumerable.Range(1, 10).Select(i => new DateTime(2000, 1, i)).ToArray(), new DateTime(2000, 1, 1) }; - yield return new object[] { new DateTime[] { new DateTime(2000, 12, 1), new DateTime(2000, 1, 1), new DateTime(2000, 1, 12) }, new DateTime(2000, 1, 1) }; + yield return [Enumerable.Range(1, 10).Select(i => new DateTime(2000, 1, i)).ToArray(), new DateTime(2000, 1, 1) + ]; + yield return [new DateTime[] { new DateTime(2000, 12, 1), new DateTime(2000, 1, 1), new DateTime(2000, 1, 12) }, new DateTime(2000, 1, 1) + ]; - DateTime[] hundred = new DateTime[] - { + DateTime[] hundred = + [ new DateTime(3000, 1, 1), new DateTime(100, 1, 1), new DateTime(200, 1, 1), new DateTime(1000, 1, 1) - }; - yield return new object[] { hundred, new DateTime(100, 1, 1) }; - yield return new object[] { hundred.Concat(Enumerable.Repeat(DateTime.MinValue, 1)), DateTime.MinValue }; + ]; + yield return [hundred, new DateTime(100, 1, 1)]; + yield return [hundred.Concat(Enumerable.Repeat(DateTime.MinValue, 1)), DateTime.MinValue]; } [Theory] @@ -614,17 +570,17 @@ public void Min_DateTime_EmptySource_ThrowsInvalidOperationException() public static IEnumerable Min_String_TestData() { - yield return new object[] { Enumerable.Range(1, 10).Select(i => i.ToString()).ToArray(), "1" }; - yield return new object[] { new string[] { "Alice", "Bob", "Charlie", "Eve", "Mallory", "Trent", "Victor" }, "Alice" }; - yield return new object[] { new string[] { null, "Charlie", null, "Victor", "Trent", null, "Eve", "Alice", "Mallory", "Bob" }, "Alice" }; + yield return [Enumerable.Range(1, 10).Select(i => i.ToString()).ToArray(), "1"]; + yield return [new string[] { "Alice", "Bob", "Charlie", "Eve", "Mallory", "Trent", "Victor" }, "Alice"]; + yield return [new string[] { null, "Charlie", null, "Victor", "Trent", null, "Eve", "Alice", "Mallory", "Bob" }, "Alice"]; - yield return new object[] { Enumerable.Empty(), 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[] { "aaa", "abcd", "bark", "temp", "cat" }, "aaa" }; - yield return new object[] { new string[] { null, null, null, null, "aAa" }, "aAa" }; - yield return new object[] { new string[] { "ooo", "www", "www", "ooo", "ooo", "ppp" }, "ooo" }; - yield return new object[] { Enumerable.Repeat(default(string), 5), null }; + yield return [Enumerable.Empty(), null]; + yield return [Enumerable.Repeat("Hello", 1), "Hello"]; + yield return [Enumerable.Repeat("hi", 5), "hi"]; + yield return [new string[] { "aaa", "abcd", "bark", "temp", "cat" }, "aaa"]; + yield return [new string[] { null, null, null, null, "aAa" }, "aAa"]; + yield return [new string[] { "ooo", "www", "www", "ooo", "ooo", "ppp" }, "ooo"]; + yield return [Enumerable.Repeat(default(string), 5), null]; } [Theory] @@ -914,7 +870,7 @@ public static IEnumerable Min_Generic_TestData() expected: null); yield return WrapArgs( - source: Enumerable.Empty(), + source: [], comparer: Comparer.Create((_, _) => 0), expected: null); @@ -934,17 +890,17 @@ public static IEnumerable Min_Generic_TestData() expected: 0); yield return WrapArgs( - source: new string[] { "Aardvark", "Zyzzyva", "Zebra", "Antelope" }, + source: ["Aardvark", "Zyzzyva", "Zebra", "Antelope"], comparer: null, expected: "Aardvark"); yield return WrapArgs( - source: new string[] { "Aardvark", "Zyzzyva", "Zebra", "Antelope" }, + source: ["Aardvark", "Zyzzyva", "Zebra", "Antelope"], comparer: Comparer.Create((x, y) => -x.CompareTo(y)), expected: "Zyzzyva"); object[] WrapArgs(IEnumerable source, IComparer? comparer, TSource? expected) - => new object[] { source, comparer, expected }; + => [source, comparer, expected]; } [Fact] @@ -960,7 +916,7 @@ public static void MinBy_Generic_NullSource_ThrowsArgumentNullException() [Fact] public static void MinBy_Generic_NullKeySelector_ThrowsArgumentNullException() { - IEnumerable source = Enumerable.Empty(); + IEnumerable source = []; Func keySelector = null; AssertExtensions.Throws("keySelector", () => source.MinBy(keySelector)); @@ -1063,13 +1019,13 @@ public static IEnumerable MinBy_Generic_TestData() expected: 0); yield return WrapArgs( - source: new string[] { "Aardvark", "Zyzzyva", "Zebra", "Antelope" }, + source: ["Aardvark", "Zyzzyva", "Zebra", "Antelope"], keySelector: x => x, comparer: null, expected: "Aardvark"); yield return WrapArgs( - source: new string[] { "Aardvark", "Zyzzyva", "Zebra", "Antelope" }, + source: ["Aardvark", "Zyzzyva", "Zebra", "Antelope"], keySelector: x => x, comparer: Comparer.Create((x, y) => -x.CompareTo(y)), expected: "Zyzzyva"); @@ -1105,7 +1061,7 @@ public static IEnumerable MinBy_Generic_TestData() expected: (Name: "Harry", Age: 20)); object[] WrapArgs(IEnumerable source, Func keySelector, IComparer? comparer, TSource? expected) - => new object[] { source, keySelector, comparer, expected }; + => [source, keySelector, comparer, expected]; } } } diff --git a/tests/System.Linq.Tests/Tests/System.Linq/OfTypeTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/OfTypeTests.cs index 34ccc8cd..d52e99cc 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/OfTypeTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/OfTypeTests.cs @@ -31,14 +31,14 @@ where string.IsNullOrEmpty(x) [Fact] public void EmptySource() { - object[] source = { }; + object[] source = []; Assert.Empty(source.OfType()); } [Fact] public void LongSequenceFromIntSource() { - int[] source = { 99, 45, 81 }; + int[] source = [99, 45, 81]; Assert.Empty(source.OfType()); } @@ -46,15 +46,15 @@ public void LongSequenceFromIntSource() [Fact] public void HeterogenousSourceNoAppropriateElements() { - object[] source = { "Hello", 3.5, "Test" }; + object[] source = ["Hello", 3.5, "Test"]; Assert.Empty(source.OfType()); } [Fact] public void HeterogenousSourceOnlyFirstOfType() { - object[] source = { 10, "Hello", 3.5, "Test" }; - int[] expected = { 10 }; + object[] source = [10, "Hello", 3.5, "Test"]; + int[] expected = [10]; Assert.Equal(expected, source.OfType()); } @@ -62,8 +62,8 @@ public void HeterogenousSourceOnlyFirstOfType() [Fact] public void AllElementsOfNullableTypeNullsSkipped() { - object[] source = { 10, -4, null, null, 4, 9 }; - int?[] expected = { 10, -4, 4, 9 }; + object[] source = [10, -4, null, null, 4, 9]; + int?[] expected = [10, -4, 4, 9]; Assert.Equal(expected, source.OfType()); } @@ -71,8 +71,8 @@ public void AllElementsOfNullableTypeNullsSkipped() [Fact] public void HeterogenousSourceSomeOfType() { - object[] source = { 3.5m, -4, "Test", "Check", 4, 8.0, 10.5, 9 }; - int[] expected = { -4, 4, 9 }; + object[] source = [3.5m, -4, "Test", "Check", 4, 8.0, 10.5, 9]; + int[] expected = [-4, 4, 9]; Assert.Equal(expected, source.OfType()); } @@ -80,8 +80,8 @@ public void HeterogenousSourceSomeOfType() [Fact] public void RunOnce() { - object[] source = { 3.5m, -4, "Test", "Check", 4, 8.0, 10.5, 9 }; - int[] expected = { -4, 4, 9 }; + object[] source = [3.5m, -4, "Test", "Check", 4, 8.0, 10.5, 9]; + int[] expected = [-4, 4, 9]; Assert.Equal(expected, source.RunOnce().OfType()); } @@ -89,8 +89,8 @@ public void RunOnce() [Fact] public void IntFromNullableInt() { - int[] source = { -4, 4, 9 }; - int?[] expected = { -4, 4, 9 }; + int[] source = [-4, 4, 9]; + int?[] expected = [-4, 4, 9]; Assert.Equal(expected, source.OfType()); } @@ -98,8 +98,8 @@ public void IntFromNullableInt() [Fact] public void IntFromNullableIntWithNulls() { - int?[] source = { null, -4, 4, null, 9 }; - int[] expected = { -4, 4, 9 }; + int?[] source = [null, -4, 4, null, 9]; + int[] expected = [-4, 4, 9]; Assert.Equal(expected, source.OfType()); } @@ -107,14 +107,14 @@ public void IntFromNullableIntWithNulls() [Fact] public void NullableDecimalFromString() { - string[] source = { "Test1", "Test2", "Test9" }; + string[] source = ["Test1", "Test2", "Test9"]; Assert.Empty(source.OfType()); } [Fact] public void LongFromDouble() { - long[] source = { 99L, 45L, 81L }; + long[] source = [99L, 45L, 81L]; Assert.Empty(source.OfType()); } @@ -161,7 +161,7 @@ public void ReferenceType_ReturnsNewEnumerable() [Fact] public void ToArray() { - IEnumerable source = new object[] { 1, 2, 3, 4, 5 }; + IEnumerable source = [1, 2, 3, 4, 5]; Assert.Equal(new int[] { 1, 2, 3, 4, 5 }, source.OfType().ToArray()); Assert.Empty(source.OfType().ToArray()); } @@ -169,7 +169,7 @@ public void ToArray() [Fact] public void ToList() { - IEnumerable source = new object[] { 1, 2, 3, 4, 5 }; + IEnumerable source = [1, 2, 3, 4, 5]; Assert.Equal(new int[] { 1, 2, 3, 4, 5 }, source.OfType().ToList()); Assert.Empty(source.OfType().ToList()); } @@ -192,7 +192,7 @@ public void Count() [Fact] public void First_Last_ElementAt() { - IEnumerable source = new object[] { 1, 2, 3, 4, 5 }; + IEnumerable source = [1, 2, 3, 4, 5]; Assert.Equal(1, source.OfType().First()); Assert.Equal(0, source.OfType().FirstOrDefault()); @@ -207,10 +207,10 @@ public void First_Last_ElementAt() [Fact] public void OfTypeSelect() { - IEnumerable objects = new object[] { "1", null, "22", null, 3, 4, "55555" }; - Assert.Equal(new int[] { 1, 2, 5 }, objects.OfType().Select(s => s.Length)); + IEnumerable objects = ["1", null, "22", null, 3, 4, "55555"]; + Assert.Equal([1, 2, 5], objects.OfType().Select(s => s.Length)); - Assert.Equal(new int[] { 1, 2, 3, 4, 5 }, new int[] { 1, 2, 3, 4, 5 }.OfType().Select(o => (int)o)); + Assert.Equal([1, 2, 3, 4, 5], new int[] { 1, 2, 3, 4, 5 }.OfType().Select(o => (int)o)); } [Fact] diff --git a/tests/System.Linq.Tests/Tests/System.Linq/OrderByDescendingTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/OrderByDescendingTests.cs index 94d5c58f..1ad8be7e 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/OrderByDescendingTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/OrderByDescendingTests.cs @@ -33,15 +33,15 @@ public void SameResultsRepeatCallsStringQuery() [Fact] public void SourceEmpty() { - int[] source = { }; + int[] source = []; Assert.Empty(source.OrderByDescending(e => e)); } [Fact] public void KeySelectorReturnsNull() { - int?[] source = { null, null, null }; - int?[] expected = { null, null, null }; + int?[] source = [null, null, null]; + int?[] expected = [null, null, null]; Assert.Equal(expected, source.OrderByDescending(e => e)); } @@ -49,8 +49,8 @@ public void KeySelectorReturnsNull() [Fact] public void ElementsAllSameKey() { - int?[] source = { 9, 9, 9, 9, 9, 9 }; - int?[] expected = { 9, 9, 9, 9, 9, 9 }; + int?[] source = [9, 9, 9, 9, 9, 9]; + int?[] expected = [9, 9, 9, 9, 9, 9]; Assert.Equal(expected, source.OrderByDescending(e => e)); } @@ -80,8 +80,8 @@ public void KeySelectorCalled() [Fact] public void FirstAndLastAreDuplicatesCustomComparer() { - string[] source = { "Prakash", "Alpha", "DAN", "dan", "Prakash" }; - string[] expected = { "Prakash", "Prakash", "DAN", "dan", "Alpha" }; + string[] source = ["Prakash", "Alpha", "DAN", "dan", "Prakash"]; + string[] expected = ["Prakash", "Prakash", "DAN", "dan", "Alpha"]; Assert.Equal(expected, source.OrderByDescending(e => e, StringComparer.OrdinalIgnoreCase)); } @@ -89,8 +89,8 @@ public void FirstAndLastAreDuplicatesCustomComparer() [Fact] public void RunOnce() { - string[] source = { "Prakash", "Alpha", "DAN", "dan", "Prakash" }; - string[] expected = { "Prakash", "Prakash", "DAN", "dan", "Alpha" }; + string[] source = ["Prakash", "Alpha", "DAN", "dan", "Prakash"]; + string[] expected = ["Prakash", "Prakash", "DAN", "dan", "Alpha"]; Assert.Equal(expected, source.RunOnce().OrderByDescending(e => e, StringComparer.OrdinalIgnoreCase)); } @@ -98,8 +98,8 @@ public void RunOnce() [Fact] public void FirstAndLastAreDuplicatesNullPassedAsComparer() { - int[] source = { 5, 1, 3, 2, 5 }; - int[] expected = { 5, 5, 3, 2, 1 }; + int[] source = [5, 1, 3, 2, 5]; + int[] expected = [5, 5, 3, 2, 1]; Assert.Equal(expected, source.OrderByDescending(e => e, null)); } @@ -107,8 +107,8 @@ public void FirstAndLastAreDuplicatesNullPassedAsComparer() [Fact] public void SourceReverseOfResultNullPassedAsComparer() { - int[] source = { -75, -50, 0, 5, 9, 30, 100 }; - int[] expected = { 100, 30, 9, 5, 0, -50, -75 }; + int[] source = [-75, -50, 0, 5, 9, 30, 100]; + int[] expected = [100, 30, 9, 5, 0, -50, -75]; Assert.Equal(expected, source.OrderByDescending(e => e, null)); } @@ -155,7 +155,7 @@ public int Compare(int x, int y) [Fact] public void OrderByExtremeComparer() { - int[] outOfOrder = new[] { 7, 1, 0, 9, 3, 5, 4, 2, 8, 6 }; + int[] outOfOrder = [7, 1, 0, 9, 3, 5, 4, 2, 8, 6]; // The .NET Framework has a bug where the input is incorrectly ordered if the comparer // returns int.MaxValue or int.MinValue. See https://github.com/dotnet/corefx/pull/2240. diff --git a/tests/System.Linq.Tests/Tests/System.Linq/OrderByTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/OrderByTests.cs index 580db649..cd1ed733 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/OrderByTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/OrderByTests.cs @@ -51,7 +51,7 @@ public void SameResultsRepeatCallsStringQuery() [Fact] public void SourceEmpty() { - int[] source = { }; + int[] source = []; Assert.Empty(source.OrderBy(e => e)); } @@ -66,8 +66,8 @@ public void OrderedCount() [Fact] public void SurviveBadComparerAlwaysReturnsNegative() { - int[] source = { 1 }; - int[] expected = { 1 }; + int[] source = [1]; + int[] expected = [1]; Assert.Equal(expected, source.OrderBy(e => e, new BadComparer2())); } @@ -75,8 +75,8 @@ public void SurviveBadComparerAlwaysReturnsNegative() [Fact] public void KeySelectorReturnsNull() { - int?[] source = { null, null, null }; - int?[] expected = { null, null, null }; + int?[] source = [null, null, null]; + int?[] expected = [null, null, null]; Assert.Equal(expected, source.OrderBy(e => e)); } @@ -84,8 +84,8 @@ public void KeySelectorReturnsNull() [Fact] public void ElementsAllSameKey() { - int?[] source = { 9, 9, 9, 9, 9, 9 }; - int?[] expected = { 9, 9, 9, 9, 9, 9 }; + int?[] source = [9, 9, 9, 9, 9, 9]; + int?[] expected = [9, 9, 9, 9, 9, 9]; Assert.Equal(expected, source.OrderBy(e => e)); } @@ -112,8 +112,8 @@ public void KeySelectorCalled() [Fact] public void FirstAndLastAreDuplicatesCustomComparer() { - string[] source = { "Prakash", "Alpha", "dan", "DAN", "Prakash" }; - string[] expected = { "Alpha", "dan", "DAN", "Prakash", "Prakash" }; + string[] source = ["Prakash", "Alpha", "dan", "DAN", "Prakash"]; + string[] expected = ["Alpha", "dan", "DAN", "Prakash", "Prakash"]; Assert.Equal(expected, source.OrderBy(e => e, StringComparer.OrdinalIgnoreCase)); } @@ -121,8 +121,8 @@ public void FirstAndLastAreDuplicatesCustomComparer() [Fact] public void RunOnce() { - string[] source = { "Prakash", "Alpha", "dan", "DAN", "Prakash" }; - string[] expected = { "Alpha", "dan", "DAN", "Prakash", "Prakash" }; + string[] source = ["Prakash", "Alpha", "dan", "DAN", "Prakash"]; + string[] expected = ["Alpha", "dan", "DAN", "Prakash", "Prakash"]; Assert.Equal(expected, source.RunOnce().OrderBy(e => e, StringComparer.OrdinalIgnoreCase)); } @@ -130,8 +130,8 @@ public void RunOnce() [Fact] public void FirstAndLastAreDuplicatesNullPassedAsComparer() { - int[] source = { 5, 1, 3, 2, 5 }; - int[] expected = { 1, 2, 3, 5, 5 }; + int[] source = [5, 1, 3, 2, 5]; + int[] expected = [1, 2, 3, 5, 5]; Assert.Equal(expected, source.OrderBy(e => e, null)); } @@ -139,8 +139,8 @@ public void FirstAndLastAreDuplicatesNullPassedAsComparer() [Fact] public void SourceReverseOfResultNullPassedAsComparer() { - int?[] source = { 100, 30, 9, 5, 0, -50, -75, null }; - int?[] expected = { null, -75, -50, 0, 5, 9, 30, 100 }; + int?[] source = [100, 30, 9, 5, 0, -50, -75, null]; + int?[] expected = [null, -75, -50, 0, 5, 9, 30, 100]; Assert.Equal(expected, source.OrderBy(e => e, null)); } @@ -236,8 +236,8 @@ public void EmptyOrderedToList() [Fact] public void SurviveBadComparerAlwaysReturnsPositive() { - int[] source = { 1 }; - int[] expected = { 1 }; + int[] source = [1]; + int[] expected = [1]; Assert.Equal(expected, source.OrderBy(e => e, new BadComparer1())); } @@ -373,7 +373,7 @@ public void LastOnOrdered() [Fact] public void LastOnOrderedMatchingCases() { - object[] boxedInts = new object[] { 0, 1, 2, 9, 1, 2, 3, 9, 4, 5, 7, 8, 9, 0, 1 }; + object[] boxedInts = [0, 1, 2, 9, 1, 2, 3, 9, 4, 5, 7, 8, 9, 0, 1]; Assert.Same(boxedInts[12], boxedInts.OrderBy(o => (int)o).Last()); Assert.Same(boxedInts[12], boxedInts.OrderBy(o => (int)o).LastOrDefault()); Assert.Same(boxedInts[12], boxedInts.OrderBy(o => (int)o).Last(o => (int)o % 2 == 1)); @@ -398,7 +398,7 @@ public void LastOrDefaultOnOrdered() [Fact] public void EnumeratorDoesntContinue() { - var enumerator = NumberRangeGuaranteedNotCollectionType(0, 3).Shuffle().OrderBy(i => i).GetEnumerator(); + using var enumerator = NumberRangeGuaranteedNotCollectionType(0, 3).Shuffle().OrderBy(i => i).GetEnumerator(); while (enumerator.MoveNext()) { } Assert.False(enumerator.MoveNext()); } @@ -510,7 +510,7 @@ public void TakeOne(IEnumerable source) [Fact] public void CultureOrderBy() { - string[] source = new[] { "Apple0", "\uFFFDble0", "Apple1", "\uFFFDble1", "Apple2", "\uFFFDble2" }; + string[] source = ["Apple0", "\uFFFDble0", "Apple1", "\uFFFDble1", "Apple2", "\uFFFDble2"]; CultureInfo dk = new CultureInfo("da-DK"); CultureInfo au = new CultureInfo("en-AU"); @@ -543,7 +543,7 @@ public void CultureOrderBy() using (new ThreadCultureChange(dk)) // "dk" whilst GetEnumerator { - IEnumerator s = source.OrderBy(x => x).GetEnumerator(); + using IEnumerator s = source.OrderBy(x => x).GetEnumerator(); using (new ThreadCultureChange(au)) // but "au" whilst accessing... { int idx = 0; @@ -557,7 +557,7 @@ public void CultureOrderBy() using (new ThreadCultureChange(au)) { // "au" whilst GetEnumerator - IEnumerator s = source.OrderBy(x => x).GetEnumerator(); + using IEnumerator s = source.OrderBy(x => x).GetEnumerator(); using (new ThreadCultureChange(dk)) { @@ -582,7 +582,7 @@ public void CultureOrderBy() [Fact] public void CultureOrderByElementAt() { - string[] source = new[] { "Apple0", "\uFFFDble0", "Apple1", "\uFFFDble1", "Apple2", "\uFFFDble2" }; + string[] source = ["Apple0", "\uFFFDble0", "Apple1", "\uFFFDble1", "Apple2", "\uFFFDble2"]; CultureInfo dk = new CultureInfo("da-DK"); CultureInfo au = new CultureInfo("en-AU"); diff --git a/tests/System.Linq.Tests/Tests/System.Linq/OrderDescendingTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/OrderDescendingTests.cs index 2160f668..6daff978 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/OrderDescendingTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/OrderDescendingTests.cs @@ -30,15 +30,15 @@ public void SameResultsRepeatCallsStringQuery() [Fact] public void SourceEmpty() { - int[] source = { }; + int[] source = []; Assert.Empty(source.OrderDescending()); } [Fact] public void KeySelectorReturnsNull() { - int?[] source = { null, null, null }; - int?[] expected = { null, null, null }; + int?[] source = [null, null, null]; + int?[] expected = [null, null, null]; Assert.Equal(expected, source.OrderDescending()); } @@ -46,8 +46,8 @@ public void KeySelectorReturnsNull() [Fact] public void ElementsAllSameKey() { - int?[] source = { 9, 9, 9, 9, 9, 9 }; - int?[] expected = { 9, 9, 9, 9, 9, 9 }; + int?[] source = [9, 9, 9, 9, 9, 9]; + int?[] expected = [9, 9, 9, 9, 9, 9]; Assert.Equal(expected, source.OrderDescending()); } @@ -70,8 +70,8 @@ public void KeySelectorCalled() [Fact] public void FirstAndLastAreDuplicatesCustomComparer() { - string[] source = { "Prakash", "Alpha", "DAN", "dan", "Prakash" }; - string[] expected = { "Prakash", "Prakash", "DAN", "dan", "Alpha" }; + string[] source = ["Prakash", "Alpha", "DAN", "dan", "Prakash"]; + string[] expected = ["Prakash", "Prakash", "DAN", "dan", "Alpha"]; Assert.Equal(expected, source.OrderDescending(StringComparer.OrdinalIgnoreCase)); } @@ -79,8 +79,8 @@ public void FirstAndLastAreDuplicatesCustomComparer() [Fact] public void RunOnce() { - string[] source = { "Prakash", "Alpha", "DAN", "dan", "Prakash" }; - string[] expected = { "Prakash", "Prakash", "DAN", "dan", "Alpha" }; + string[] source = ["Prakash", "Alpha", "DAN", "dan", "Prakash"]; + string[] expected = ["Prakash", "Prakash", "DAN", "dan", "Alpha"]; Assert.Equal(expected, source.RunOnce().OrderDescending(StringComparer.OrdinalIgnoreCase)); } @@ -88,8 +88,8 @@ public void RunOnce() [Fact] public void FirstAndLastAreDuplicatesNullPassedAsComparer() { - int[] source = { 5, 1, 3, 2, 5 }; - int[] expected = { 5, 5, 3, 2, 1 }; + int[] source = [5, 1, 3, 2, 5]; + int[] expected = [5, 5, 3, 2, 1]; Assert.Equal(expected, source.OrderDescending(null)); } @@ -97,8 +97,8 @@ public void FirstAndLastAreDuplicatesNullPassedAsComparer() [Fact] public void SourceReverseOfResultNullPassedAsComparer() { - int[] source = { -75, -50, 0, 5, 9, 30, 100 }; - int[] expected = { 100, 30, 9, 5, 0, -50, -75 }; + int[] source = [-75, -50, 0, 5, 9, 30, 100]; + int[] expected = [100, 30, 9, 5, 0, -50, -75]; Assert.Equal(expected, source.OrderDescending(null)); } @@ -175,7 +175,7 @@ public int Compare(int x, int y) [Fact] public void OrderByExtremeComparer() { - int[] outOfOrder = new[] { 7, 1, 0, 9, 3, 5, 4, 2, 8, 6 }; + int[] outOfOrder = [7, 1, 0, 9, 3, 5, 4, 2, 8, 6]; // The .NET Framework has a bug where the input is incorrectly ordered if the comparer // returns int.MaxValue or int.MinValue. See https://github.com/dotnet/corefx/pull/2240. diff --git a/tests/System.Linq.Tests/Tests/System.Linq/OrderTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/OrderTests.cs index f66a59be..131e7e4e 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/OrderTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/OrderTests.cs @@ -37,7 +37,7 @@ public void SameResultsRepeatCallsIntQuery() [Fact] public void SourceEmpty() { - int[] source = { }; + int[] source = []; Assert.Empty(source.Order()); } @@ -52,8 +52,8 @@ public void OrderedCount() [Fact] public void SurviveBadComparerAlwaysReturnsNegative() { - int[] source = { 1 }; - int[] expected = { 1 }; + int[] source = [1]; + int[] expected = [1]; Assert.Equal(expected, source.Order(new BadComparer2())); } @@ -61,8 +61,8 @@ public void SurviveBadComparerAlwaysReturnsNegative() [Fact] public void KeySelectorReturnsNull() { - int?[] source = { null, null, null }; - int?[] expected = { null, null, null }; + int?[] source = [null, null, null]; + int?[] expected = [null, null, null]; Assert.Equal(expected, source.Order()); } @@ -70,8 +70,8 @@ public void KeySelectorReturnsNull() [Fact] public void ElementsAllSameKey() { - int?[] source = { 9, 9, 9, 9, 9, 9 }; - int?[] expected = { 9, 9, 9, 9, 9, 9 }; + int?[] source = [9, 9, 9, 9, 9, 9]; + int?[] expected = [9, 9, 9, 9, 9, 9]; Assert.Equal(expected, source.Order()); } @@ -79,8 +79,8 @@ public void ElementsAllSameKey() [Fact] public void FirstAndLastAreDuplicatesCustomComparer() { - string[] source = { "Prakash", "Alpha", "dan", "DAN", "Prakash" }; - string[] expected = { "Alpha", "dan", "DAN", "Prakash", "Prakash" }; + string[] source = ["Prakash", "Alpha", "dan", "DAN", "Prakash"]; + string[] expected = ["Alpha", "dan", "DAN", "Prakash", "Prakash"]; Assert.Equal(expected, source.Order(StringComparer.OrdinalIgnoreCase)); } @@ -88,8 +88,8 @@ public void FirstAndLastAreDuplicatesCustomComparer() [Fact] public void RunOnce() { - string[] source = { "Prakash", "Alpha", "dan", "DAN", "Prakash" }; - string[] expected = { "Alpha", "dan", "DAN", "Prakash", "Prakash" }; + string[] source = ["Prakash", "Alpha", "dan", "DAN", "Prakash"]; + string[] expected = ["Alpha", "dan", "DAN", "Prakash", "Prakash"]; Assert.Equal(expected, source.RunOnce().Order(StringComparer.OrdinalIgnoreCase)); } @@ -97,8 +97,8 @@ public void RunOnce() [Fact] public void FirstAndLastAreDuplicatesNullPassedAsComparer() { - int[] source = { 5, 1, 3, 2, 5 }; - int[] expected = { 1, 2, 3, 5, 5 }; + int[] source = [5, 1, 3, 2, 5]; + int[] expected = [1, 2, 3, 5, 5]; Assert.Equal(expected, source.Order(null)); } @@ -106,8 +106,8 @@ public void FirstAndLastAreDuplicatesNullPassedAsComparer() [Fact] public void SourceReverseOfResultNullPassedAsComparer() { - int?[] source = { 100, 30, 9, 5, 0, -50, -75, null }; - int?[] expected = { null, -75, -50, 0, 5, 9, 30, 100 }; + int?[] source = [100, 30, 9, 5, 0, -50, -75, null]; + int?[] expected = [null, -75, -50, 0, 5, 9, 30, 100]; Assert.Equal(expected, source.Order(null)); } @@ -158,8 +158,8 @@ public void EmptyOrderedToList() [Fact] public void SurviveBadComparerAlwaysReturnsPositive() { - int[] source = { 1 }; - int[] expected = { 1 }; + int[] source = [1]; + int[] expected = [1]; Assert.Equal(expected, source.Order(new BadComparer1())); } @@ -291,7 +291,7 @@ public void LastOnOrdered() [Fact] public void LastOnOrderedMatchingCases() { - object[] boxedInts = new object[] { 0, 1, 2, 9, 1, 2, 3, 9, 4, 5, 7, 8, 9, 0, 1 }; + object[] boxedInts = [0, 1, 2, 9, 1, 2, 3, 9, 4, 5, 7, 8, 9, 0, 1]; Assert.Same(boxedInts[12], boxedInts.Order().Last()); Assert.Same(boxedInts[12], boxedInts.Order().LastOrDefault()); Assert.Same(boxedInts[12], boxedInts.Order().Last(o => (int)o % 2 == 1)); @@ -325,7 +325,7 @@ public void ElementAtOnOrdered() [Fact] public void EnumeratorDoesntContinue() { - var enumerator = NumberRangeGuaranteedNotCollectionType(0, 3).Shuffle().Order().GetEnumerator(); + using var enumerator = NumberRangeGuaranteedNotCollectionType(0, 3).Shuffle().Order().GetEnumerator(); while (enumerator.MoveNext()) { } Assert.False(enumerator.MoveNext()); } @@ -397,7 +397,7 @@ public void TakeOne(IEnumerable source) [Fact] public void CultureOrder() { - string[] source = new[] { "Apple0", "\u00C6ble0", "Apple1", "\u00C6ble1", "Apple2", "\u00C6ble2" }; + string[] source = ["Apple0", "\u00C6ble0", "Apple1", "\u00C6ble1", "Apple2", "\u00C6ble2"]; CultureInfo dk = new CultureInfo("da-DK"); CultureInfo au = new CultureInfo("en-AU"); @@ -430,7 +430,7 @@ public void CultureOrder() using (new ThreadCultureChange(dk)) // "dk" whilst GetEnumerator { - IEnumerator s = source.Order().GetEnumerator(); + using IEnumerator s = source.Order().GetEnumerator(); using (new ThreadCultureChange(au)) // but "au" whilst accessing... { int idx = 0; @@ -444,7 +444,7 @@ public void CultureOrder() using (new ThreadCultureChange(au)) { // "au" whilst GetEnumerator - IEnumerator s = source.Order().GetEnumerator(); + using IEnumerator s = source.Order().GetEnumerator(); using (new ThreadCultureChange(dk)) { @@ -469,7 +469,7 @@ public void CultureOrder() [Fact] public void CultureOrderElementAt() { - string[] source = new[] { "Apple0", "\u00C6ble0", "Apple1", "\u00C6ble1", "Apple2", "\u00C6ble2" }; + string[] source = ["Apple0", "\u00C6ble0", "Apple1", "\u00C6ble1", "Apple2", "\u00C6ble2"]; CultureInfo dk = new CultureInfo("da-DK"); CultureInfo au = new CultureInfo("en-AU"); @@ -504,7 +504,7 @@ public void CultureOrderElementAt() [Fact] public void StableSort_CustomComparerAlwaysReturns0() { - byte[] values = new byte[] { 0x45, 0x7D, 0x4B, 0x61, 0x27 }; + byte[] values = [0x45, 0x7D, 0x4B, 0x61, 0x27]; byte[] newValues = values.Order(Comparer.Create((a, b) => 0)).ToArray(); AssertExtensions.SequenceEqual(values, newValues); } diff --git a/tests/System.Linq.Tests/Tests/System.Linq/OrderedSubsetting.cs b/tests/System.Linq.Tests/Tests/System.Linq/OrderedSubsetting.cs index 74bcd6cc..d0870e20 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/OrderedSubsetting.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/OrderedSubsetting.cs @@ -107,7 +107,7 @@ public void Take() Assert.Empty(ordered.Take(0)); Assert.Empty(ordered.Take(-1)); Assert.Empty(ordered.Take(int.MinValue)); - Assert.Equal(new int[] { 0 }, ordered.Take(1)); + Assert.Equal([0], ordered.Take(1)); Assert.Equal(Enumerable.Range(0, 100), ordered.Take(101)); Assert.Equal(Enumerable.Range(0, 100), ordered.Take(int.MaxValue)); Assert.Equal(Enumerable.Range(0, 100), ordered.Take(100)); @@ -145,7 +145,7 @@ public void Skip() Assert.Equal(Enumerable.Range(0, 100), ordered.Skip(0)); Assert.Equal(Enumerable.Range(0, 100), ordered.Skip(-1)); Assert.Equal(Enumerable.Range(0, 100), ordered.Skip(int.MinValue)); - Assert.Equal(new int[] { 99 }, ordered.Skip(99)); + Assert.Equal([99], ordered.Skip(99)); Assert.Empty(ordered.Skip(101)); Assert.Empty(ordered.Skip(int.MaxValue)); Assert.Empty(ordered.Skip(100)); @@ -224,7 +224,7 @@ public void TakeAndSkip() Assert.Equal(Enumerable.Range(10, 1), ordered.Take(11).Skip(10)); } - [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsSpeedOptimized))] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsLinqSpeedOptimized))] public void TakeAndSkip_DoesntIterateRangeUnlessNecessary() { Assert.Empty(Enumerable.Range(0, int.MaxValue).Take(int.MaxValue).OrderBy(i => i).Skip(int.MaxValue - 4).Skip(15)); @@ -335,10 +335,10 @@ public void Count() [Fact] public void SkipTakesOnlyOne() { - Assert.Equal(new[] { 1 }, Enumerable.Range(1, 10).Shuffle().OrderBy(i => i).Take(1)); - Assert.Equal(new[] { 2 }, Enumerable.Range(1, 10).Shuffle().OrderBy(i => i).Skip(1).Take(1)); - Assert.Equal(new[] { 3 }, Enumerable.Range(1, 10).Shuffle().OrderBy(i => i).Take(3).Skip(2)); - Assert.Equal(new[] { 1 }, Enumerable.Range(1, 10).Shuffle().OrderBy(i => i).Take(3).Take(1)); + Assert.Equal([1], Enumerable.Range(1, 10).Shuffle().OrderBy(i => i).Take(1)); + Assert.Equal([2], Enumerable.Range(1, 10).Shuffle().OrderBy(i => i).Skip(1).Take(1)); + Assert.Equal([3], Enumerable.Range(1, 10).Shuffle().OrderBy(i => i).Take(3).Skip(2)); + Assert.Equal([1], Enumerable.Range(1, 10).Shuffle().OrderBy(i => i).Take(3).Take(1)); } [Fact] @@ -399,7 +399,7 @@ public void SingleElementCount() [Fact] public void EnumeratorDoesntContinue() { - var enumerator = NumberRangeGuaranteedNotCollectionType(0, 3).Shuffle().OrderBy(i => i).Skip(1).GetEnumerator(); + using var enumerator = NumberRangeGuaranteedNotCollectionType(0, 3).Shuffle().OrderBy(i => i).Skip(1).GetEnumerator(); while (enumerator.MoveNext()) { } Assert.False(enumerator.MoveNext()); } @@ -407,7 +407,7 @@ public void EnumeratorDoesntContinue() [Fact] public void Select() { - Assert.Equal(new[] { 0, 2, 4, 6, 8 }, Enumerable.Range(-1, 8).Shuffle().OrderBy(i => i).Skip(1).Take(5).Select(i => i * 2)); + Assert.Equal([0, 2, 4, 6, 8], Enumerable.Range(-1, 8).Shuffle().OrderBy(i => i).Skip(1).Take(5).Select(i => i * 2)); } [Fact] diff --git a/tests/System.Linq.Tests/Tests/System.Linq/RangeTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/RangeTests.cs index 0470d249..0ee62597 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/RangeTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/RangeTests.cs @@ -26,7 +26,7 @@ public static IEnumerable Range_ToArray_ProduceCorrectResult_MemberDat { for (int i = 0; i < 64; i++) { - yield return new object[] { i }; + yield return [i]; } } @@ -78,33 +78,27 @@ public void Range_ThrowExceptionOnOverflow() [Fact] public void Range_NotEnumerateAfterEnd() { - using (var rangeEnum = Enumerable.Range(1, 1).GetEnumerator()) - { - Assert.True(rangeEnum.MoveNext()); - Assert.False(rangeEnum.MoveNext()); - Assert.False(rangeEnum.MoveNext()); - } + using var rangeEnum = Enumerable.Range(1, 1).GetEnumerator(); + Assert.True(rangeEnum.MoveNext()); + Assert.False(rangeEnum.MoveNext()); + Assert.False(rangeEnum.MoveNext()); } [Fact] public void Range_EnumerableAndEnumeratorAreSame() { var rangeEnumerable = Enumerable.Range(1, 1); - using (var rangeEnumerator = rangeEnumerable.GetEnumerator()) - { - Assert.Same(rangeEnumerable, rangeEnumerator); - } + using var rangeEnumerator = rangeEnumerable.GetEnumerator(); + Assert.Same(rangeEnumerable, rangeEnumerator); } [Fact] public void Range_GetEnumeratorReturnUniqueInstances() { var rangeEnumerable = Enumerable.Range(1, 1); - using (var enum1 = rangeEnumerable.GetEnumerator()) - using (var enum2 = rangeEnumerable.GetEnumerator()) - { - Assert.NotSame(enum1, enum2); - } + using var enum1 = rangeEnumerable.GetEnumerator(); + using var enum2 = rangeEnumerable.GetEnumerator(); + Assert.NotSame(enum1, enum2); } [Fact] @@ -116,7 +110,7 @@ public void Range_ToInt32MaxValue() Assert.Equal(count, rangeEnumerable.Count()); - int[] expected = { int.MaxValue - 3, int.MaxValue - 2, int.MaxValue - 1, int.MaxValue }; + int[] expected = [int.MaxValue - 3, int.MaxValue - 2, int.MaxValue - 1, int.MaxValue]; Assert.Equal(expected, rangeEnumerable); } @@ -132,7 +126,7 @@ public void NegativeStart() { int start = -5; int count = 1; - int[] expected = { -5 }; + int[] expected = [-5]; Assert.Equal(expected, Enumerable.Range(start, count)); } @@ -142,7 +136,7 @@ public void ArbitraryStart() { int start = 12; int count = 6; - int[] expected = { 12, 13, 14, 15, 16, 17 }; + int[] expected = [12, 13, 14, 15, 16, 17]; Assert.Equal(expected, Enumerable.Range(start, count)); } @@ -174,10 +168,10 @@ public void SkipExcessive() [Fact] public void SkipTakeCanOnlyBeOne() { - Assert.Equal(new[] { 1 }, Enumerable.Range(1, 10).Take(1)); - Assert.Equal(new[] { 2 }, Enumerable.Range(1, 10).Skip(1).Take(1)); - Assert.Equal(new[] { 3 }, Enumerable.Range(1, 10).Take(3).Skip(2)); - Assert.Equal(new[] { 1 }, Enumerable.Range(1, 10).Take(3).Take(1)); + Assert.Equal([1], Enumerable.Range(1, 10).Take(1)); + Assert.Equal([2], Enumerable.Range(1, 10).Skip(1).Take(1)); + Assert.Equal([3], Enumerable.Range(1, 10).Take(3).Skip(2)); + Assert.Equal([1], Enumerable.Range(1, 10).Take(3).Take(1)); } [Fact] @@ -216,23 +210,23 @@ public void FirstOrDefault() Assert.Equal(-100, Enumerable.Range(-100, int.MaxValue).FirstOrDefault()); } - [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsSpeedOptimized))] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsLinqSpeedOptimized))] public void Last() { Assert.Equal(1000000056, Enumerable.Range(57, 1000000000).Last()); } - [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsSpeedOptimized))] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsLinqSpeedOptimized))] public void LastOrDefault() { Assert.Equal(int.MaxValue - 101, Enumerable.Range(-100, int.MaxValue).LastOrDefault()); } - [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsSpeedOptimized))] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsLinqSpeedOptimized))] public void IListImplementationIsValid() { - Validate(Enumerable.Range(42, 10), new[] { 42, 43, 44, 45, 46, 47, 48, 49, 50, 51 }); - Validate(Enumerable.Range(42, 10).Skip(3).Take(4), new[] { 45, 46, 47, 48 }); + Validate(Enumerable.Range(42, 10), [42, 43, 44, 45, 46, 47, 48, 49, 50, 51]); + Validate(Enumerable.Range(42, 10).Skip(3).Take(4), [45, 46, 47, 48]); static void Validate(IEnumerable e, int[] expected) { @@ -270,7 +264,7 @@ static void Validate(IEnumerable e, int[] expected) list.CopyTo(actual, 1); Assert.Equal(0, actual[0]); Assert.Equal(0, actual[^1]); - AssertExtensions.SequenceEqual(expected, actual.AsSpan(1, expected.Length)); + AssertExtensions.SequenceEqual(expected.AsSpan(), actual.AsSpan(1, expected.Length)); } } } diff --git a/tests/System.Linq.Tests/Tests/System.Linq/RepeatTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/RepeatTests.cs index 1c3474fb..1ed28277 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/RepeatTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/RepeatTests.cs @@ -82,33 +82,27 @@ public void Repeat_ThrowExceptionOnNegativeCount() [Fact] public void Repeat_NotEnumerateAfterEnd() { - using (var repeatEnum = Enumerable.Repeat(1, 1).GetEnumerator()) - { - Assert.True(repeatEnum.MoveNext()); - Assert.False(repeatEnum.MoveNext()); - Assert.False(repeatEnum.MoveNext()); - } + using var repeatEnum = Enumerable.Repeat(1, 1).GetEnumerator(); + Assert.True(repeatEnum.MoveNext()); + Assert.False(repeatEnum.MoveNext()); + Assert.False(repeatEnum.MoveNext()); } [Fact] public void Repeat_EnumerableAndEnumeratorAreSame() { var repeatEnumerable = Enumerable.Repeat(1, 1); - using (var repeatEnumerator = repeatEnumerable.GetEnumerator()) - { - Assert.Same(repeatEnumerable, repeatEnumerator); - } + using var repeatEnumerator = repeatEnumerable.GetEnumerator(); + Assert.Same(repeatEnumerable, repeatEnumerator); } [Fact] public void Repeat_GetEnumeratorReturnUniqueInstances() { var repeatEnumerable = Enumerable.Repeat(1, 1); - using (var enum1 = repeatEnumerable.GetEnumerator()) - using (var enum2 = repeatEnumerable.GetEnumerator()) - { - Assert.NotSame(enum1, enum2); - } + using var enum1 = repeatEnumerable.GetEnumerator(); + using var enum2 = repeatEnumerable.GetEnumerator(); + Assert.NotSame(enum1, enum2); } [Fact] @@ -126,7 +120,7 @@ public void SameResultsRepeatCallsStringQuery() [Fact] public void CountOneSingleResult() { - int[] expected = { -15 }; + int[] expected = [-15]; Assert.Equal(expected, Enumerable.Repeat(-15, 1)); } @@ -134,7 +128,7 @@ public void CountOneSingleResult() [Fact] public void RepeatArbitraryCorrectResults() { - int[] expected = { 12, 12, 12, 12, 12, 12, 12, 12 }; + int[] expected = [12, 12, 12, 12, 12, 12, 12, 12]; Assert.Equal(expected, Enumerable.Repeat(12, 8)); } @@ -142,7 +136,7 @@ public void RepeatArbitraryCorrectResults() [Fact] public void RepeatNull() { - int?[] expected = { null, null, null, null }; + int?[] expected = [null, null, null, null]; Assert.Equal(expected, Enumerable.Repeat((int?)null, 4)); } @@ -174,10 +168,10 @@ public void SkipExcessive() [Fact] public void TakeCanOnlyBeOne() { - Assert.Equal(new[] { 1 }, Enumerable.Repeat(1, 10).Take(1)); - Assert.Equal(new[] { 1 }, Enumerable.Repeat(1, 10).Skip(1).Take(1)); - Assert.Equal(new[] { 1 }, Enumerable.Repeat(1, 10).Take(3).Skip(2)); - Assert.Equal(new[] { 1 }, Enumerable.Repeat(1, 10).Take(3).Take(1)); + Assert.Equal([1], Enumerable.Repeat(1, 10).Take(1)); + Assert.Equal([1], Enumerable.Repeat(1, 10).Skip(1).Take(1)); + Assert.Equal([1], Enumerable.Repeat(1, 10).Take(3).Skip(2)); + Assert.Equal([1], Enumerable.Repeat(1, 10).Take(3).Take(1)); } [Fact] @@ -240,11 +234,11 @@ public void Count() Assert.Equal(42, Enumerable.Repeat("Test", 42).Count()); } - [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsSpeedOptimized))] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsLinqSpeedOptimized))] public void ICollectionImplementationIsValid() { - Validate(Enumerable.Repeat(42, 10), new[] { 42, 42, 42, 42, 42, 42, 42, 42, 42, 42 }); - Validate(Enumerable.Repeat(42, 10).Skip(3).Take(4), new[] { 42, 42, 42, 42 }); + Validate(Enumerable.Repeat(42, 10), [42, 42, 42, 42, 42, 42, 42, 42, 42, 42]); + Validate(Enumerable.Repeat(42, 10).Skip(3).Take(4), [42, 42, 42, 42]); static void Validate(IEnumerable e, int[] expected) { @@ -282,7 +276,7 @@ static void Validate(IEnumerable e, int[] expected) list.CopyTo(actual, 1); Assert.Equal(0, actual[0]); Assert.Equal(0, actual[^1]); - AssertExtensions.SequenceEqual(expected, actual.AsSpan(1, expected.Length)); + AssertExtensions.SequenceEqual(expected.AsSpan(), actual.AsSpan(1, expected.Length)); } } } diff --git a/tests/System.Linq.Tests/Tests/System.Linq/ReverseTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/ReverseTests.cs index 1cd337a9..d780b27b 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/ReverseTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/ReverseTests.cs @@ -11,7 +11,8 @@ public class ReverseTests : EnumerableTests [Fact] public void InvalidArguments() { - AssertExtensions.Throws("source", () => Enumerable.Reverse(null)); + AssertExtensions.Throws("source", () => Enumerable.Reverse((IEnumerable)null)); + AssertExtensions.Throws("source", () => Enumerable.Reverse((string[])null)); } [Theory] @@ -48,6 +49,40 @@ public void Reverse(IEnumerable source) Assert.Equal(actual, actual); // Repeat the enumeration against itself. } + [Theory] + [MemberData(nameof(ReverseData))] + public void ReverseArray(IEnumerable source) + { + T[] expected = source.ToArray(); + Array.Reverse(expected); + + IEnumerable actual = source.ToArray().Reverse(); + + Assert.Equal(expected, actual); + Assert.Equal(expected.Count(), actual.Count()); // Count may be optimized. + Assert.Equal(expected, actual.ToArray()); + Assert.Equal(expected, actual.ToList()); + + Assert.Equal(expected.FirstOrDefault(), actual.FirstOrDefault()); + Assert.Equal(expected.LastOrDefault(), actual.LastOrDefault()); + + for (int i = 0; i < expected.Length; i++) + { + Assert.Equal(expected[i], actual.ElementAt(i)); + + Assert.Equal(expected.Skip(i), actual.Skip(i)); + Assert.Equal(expected.Take(i), actual.Take(i)); + } + + Assert.Equal(default(T), actual.ElementAtOrDefault(-1)); + Assert.Equal(default(T), actual.ElementAtOrDefault(expected.Length)); + + Assert.Equal(expected, actual.Select(_ => _)); + Assert.Equal(expected, actual.Where(_ => true)); + + Assert.Equal(actual, actual); // Repeat the enumeration against itself. + } + [Theory, MemberData(nameof(ReverseData))] public void RunOnce(IEnumerable source) { diff --git a/tests/System.Linq.Tests/Tests/System.Linq/RightJoinTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/RightJoinTests.cs index 2ffe362b..e46cc68c 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/RightJoinTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/RightJoinTests.cs @@ -6,7 +6,6 @@ using Xunit; #if NET10_0_OR_GREATER - namespace System.Linq.Tests { public class RightJoinTests : EnumerableTests diff --git a/tests/System.Linq.Tests/Tests/System.Linq/SelectManyTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/SelectManyTests.cs index d8094baf..8328a053 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/SelectManyTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/SelectManyTests.cs @@ -35,11 +35,11 @@ public void EmptySourceResultSelectorIndexedSelector() [Fact] public void SingleElement() { - int?[] expected = { 90, 55, null, 43, 89 }; + int?[] expected = [90, 55, null, 43, 89]; StringWithIntArray[] source = - { + [ new StringWithIntArray { name = "Prakash", total = expected } - }; + ]; Assert.Equal(expected, source.SelectMany(e => e.total)); } @@ -47,13 +47,13 @@ public void SingleElement() public void NonEmptySelectingEmpty() { StringWithIntArray[] source = - { - new StringWithIntArray { name="Prakash", total=new int?[0] }, - new StringWithIntArray { name="Bob", total=new int?[0] }, - new StringWithIntArray { name="Chris", total=new int?[0] }, - new StringWithIntArray { name=null, total=new int?[0] }, - new StringWithIntArray { name="Prakash", total=new int?[0] } - }; + [ + new StringWithIntArray { name="Prakash", total=[] }, + new StringWithIntArray { name="Bob", total=[] }, + new StringWithIntArray { name="Chris", total=[] }, + new StringWithIntArray { name=null, total=[] }, + new StringWithIntArray { name="Prakash", total=[] } + ]; Assert.Empty(source.SelectMany(e => e.total)); } @@ -62,13 +62,13 @@ public void NonEmptySelectingEmpty() public void NonEmptySelectingEmptyIndexedSelector() { StringWithIntArray[] source = - { - new StringWithIntArray { name="Prakash", total=new int?[0] }, - new StringWithIntArray { name="Bob", total=new int?[0] }, - new StringWithIntArray { name="Chris", total=new int?[0] }, - new StringWithIntArray { name=null, total=new int?[0] }, - new StringWithIntArray { name="Prakash", total=new int?[0] } - }; + [ + new StringWithIntArray { name="Prakash", total=[] }, + new StringWithIntArray { name="Bob", total=[] }, + new StringWithIntArray { name="Chris", total=[] }, + new StringWithIntArray { name=null, total=[] }, + new StringWithIntArray { name="Prakash", total=[] } + ]; Assert.Empty(source.SelectMany((e, i) => e.total)); } @@ -77,13 +77,13 @@ public void NonEmptySelectingEmptyIndexedSelector() public void NonEmptySelectingEmptyWithResultSelector() { StringWithIntArray[] source = - { - new StringWithIntArray { name="Prakash", total=new int?[0] }, - new StringWithIntArray { name="Bob", total=new int?[0] }, - new StringWithIntArray { name="Chris", total=new int?[0] }, - new StringWithIntArray { name=null, total=new int?[0] }, - new StringWithIntArray { name="Prakash", total=new int?[0] } - }; + [ + new StringWithIntArray { name="Prakash", total=[] }, + new StringWithIntArray { name="Bob", total=[] }, + new StringWithIntArray { name="Chris", total=[] }, + new StringWithIntArray { name=null, total=[] }, + new StringWithIntArray { name="Prakash", total=[] } + ]; Assert.Empty(source.SelectMany(e => e.total, (e, f) => f.ToString())); } @@ -92,13 +92,13 @@ public void NonEmptySelectingEmptyWithResultSelector() public void NonEmptySelectingEmptyIndexedSelectorWithResultSelector() { StringWithIntArray[] source = - { - new StringWithIntArray { name="Prakash", total=new int?[0] }, - new StringWithIntArray { name="Bob", total=new int?[0] }, - new StringWithIntArray { name="Chris", total=new int?[0] }, - new StringWithIntArray { name=null, total=new int?[0] }, - new StringWithIntArray { name="Prakash", total=new int?[0] } - }; + [ + new StringWithIntArray { name="Prakash", total=[] }, + new StringWithIntArray { name="Bob", total=[] }, + new StringWithIntArray { name="Chris", total=[] }, + new StringWithIntArray { name=null, total=[] }, + new StringWithIntArray { name="Prakash", total=[] } + ]; Assert.Empty(source.SelectMany((e, i) => e.total, (e, f) => f.ToString())); } @@ -107,14 +107,14 @@ public void NonEmptySelectingEmptyIndexedSelectorWithResultSelector() public void ResultsSelected() { 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} } - }; - int?[] expected = { 1, 2, 3, 4, 5, 6, 8, 9, -10, 100 }; + [ + new StringWithIntArray { name="Prakash", total= [1, 2, 3, 4] }, + new StringWithIntArray { name="Bob", total= [5, 6] }, + new StringWithIntArray { name="Chris", total=[] }, + new StringWithIntArray { name=null, total= [8, 9] }, + new StringWithIntArray { name="Prakash", total= [-10, 100] } + ]; + int?[] expected = [1, 2, 3, 4, 5, 6, 8, 9, -10, 100]; Assert.Equal(expected, source.SelectMany(e => e.total)); } @@ -122,14 +122,14 @@ public void ResultsSelected() public void RunOnce() { 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} } - }; - int?[] expected = { 1, 2, 3, 4, 5, 6, 8, 9, -10, 100 }; + [ + new StringWithIntArray { name="Prakash", total= [1, 2, 3, 4] }, + new StringWithIntArray { name="Bob", total= [5, 6] }, + new StringWithIntArray { name="Chris", total=[] }, + new StringWithIntArray { name=null, total= [8, 9] }, + new StringWithIntArray { name="Prakash", total= [-10, 100] } + ]; + int?[] expected = [1, 2, 3, 4, 5, 6, 8, 9, -10, 100]; Assert.Equal(expected, source.RunOnce().SelectMany(e => e.total.RunOnce())); } @@ -142,11 +142,11 @@ public void SourceEmptyIndexUsed() [Fact] public void SingleElementIndexUsed() { - int?[] expected = { 90, 55, null, 43, 89 }; + int?[] expected = [90, 55, null, 43, 89]; StringWithIntArray[] source = - { + [ new StringWithIntArray { name = "Prakash", total = expected } - }; + ]; Assert.Equal(expected, source.SelectMany((e, index) => e.total)); } @@ -154,13 +154,13 @@ public void SingleElementIndexUsed() public void NonEmptySelectingEmptyIndexUsed() { StringWithIntArray[] source = - { - new StringWithIntArray { name="Prakash", total= new int?[0] }, - new StringWithIntArray { name="Bob", total=new int?[0] }, - new StringWithIntArray { name="Chris", total=new int?[0] }, - new StringWithIntArray { name=null, total=new int?[0] }, - new StringWithIntArray { name="Prakash", total=new int?[0] } - }; + [ + new StringWithIntArray { name="Prakash", total= [] }, + new StringWithIntArray { name="Bob", total=[] }, + new StringWithIntArray { name="Chris", total=[] }, + new StringWithIntArray { name=null, total=[] }, + new StringWithIntArray { name="Prakash", total=[] } + ]; Assert.Empty(source.SelectMany((e, index) => e.total)); } @@ -168,14 +168,14 @@ public void NonEmptySelectingEmptyIndexUsed() public void ResultsSelectedIndexUsed() { 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} } - }; - int?[] expected = { 1, 2, 3, 4, 5, 6, 8, 9, -10, 100 }; + [ + new StringWithIntArray { name="Prakash", total= [1, 2, 3, 4] }, + new StringWithIntArray { name="Bob", total= [5, 6] }, + new StringWithIntArray { name="Chris", total=[] }, + new StringWithIntArray { name=null, total= [8, 9] }, + new StringWithIntArray { name="Prakash", total= [-10, 100] } + ]; + int?[] expected = [1, 2, 3, 4, 5, 6, 8, 9, -10, 100]; Assert.Equal(expected, source.SelectMany((e, index) => e.total)); } @@ -183,13 +183,13 @@ public void ResultsSelectedIndexUsed() public void IndexCausingFirstToBeSelected() { 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} } - }; + [ + new StringWithIntArray { name="Prakash", total= [1, 2, 3, 4] }, + new StringWithIntArray { name="Bob", total= [5, 6] }, + new StringWithIntArray { name="Chris", total=[] }, + new StringWithIntArray { name=null, total= [8, 9] }, + new StringWithIntArray { name="Prakash", total= [-10, 100] } + ]; Assert.Equal(source.First().total, source.SelectMany((e, i) => i == 0 ? e.total : Enumerable.Empty())); } @@ -198,13 +198,13 @@ public void IndexCausingFirstToBeSelected() 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} } - }; + [ + new StringWithIntArray { name="Prakash", total= [1, 2, 3, 4] }, + new StringWithIntArray { name="Bob", total= [5, 6] }, + new StringWithIntArray { name="Chris", total=[] }, + new StringWithIntArray { name=null, total= [8, 9] }, + new StringWithIntArray { name="Robert", total= [-10, 100] } + ]; Assert.Equal(source.Last().total, source.SelectMany((e, i) => i == 4 ? e.total : Enumerable.Empty())); } @@ -213,27 +213,27 @@ public void IndexCausingLastToBeSelected() public void IndexOverflow() { var selected = new FastInfiniteEnumerator().SelectMany((e, i) => Enumerable.Empty()); - using (var en = selected.GetEnumerator()) - Assert.Throws(() => + using var en = selected.GetEnumerator(); + Assert.Throws(() => + { + while (en.MoveNext()) { - while (en.MoveNext()) - { - } - }); + } + }); } [Fact] public void ResultSelector() { 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", "5", "6", "8", "9", "-10", "100" }; + [ + new StringWithIntArray { name="Prakash", total= [1, 2, 3, 4] }, + new StringWithIntArray { name="Bob", total= [5, 6] }, + new StringWithIntArray { name="Chris", total=[] }, + new StringWithIntArray { name=null, total= [8, 9] }, + new StringWithIntArray { name="Prakash", total= [-10, 100] } + ]; + string[] expected = ["1", "2", "3", "4", "5", "6", "8", "9", "-10", "100"]; Assert.Equal(expected, source.SelectMany(e => e.total, (e, f) => f.ToString())); } @@ -312,14 +312,14 @@ public void NullIndexedSelector() 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" }; + [ + new StringWithIntArray { name="Prakash", total= [1, 2, 3, 4] }, + new StringWithIntArray { name="Bob", total= [5, 6] }, + new StringWithIntArray { name="Chris", total=[] }, + new StringWithIntArray { name=null, total= [8, 9] }, + new StringWithIntArray { name="Prakash", total= [-10, 100] } + ]; + string[] expected = ["1", "2", "3", "4"]; Assert.Equal(expected, source.SelectMany((e, i) => i == 0 ? e.total : Enumerable.Empty(), (e, f) => f.ToString())); } @@ -327,15 +327,15 @@ public void IndexCausingFirstToBeSelectedWithResultSelector() public void IndexCausingLastToBeSelectedWithResultSelector() { 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} } - }; - - string[] expected = { "-10", "100" }; + [ + new StringWithIntArray { name="Prakash", total= [1, 2, 3, 4] }, + new StringWithIntArray { name="Bob", total= [5, 6] }, + new StringWithIntArray { name="Chris", total=[] }, + new StringWithIntArray { name=null, total= [8, 9] }, + new StringWithIntArray { name="Robert", total= [-10, 100] } + ]; + + string[] expected = ["-10", "100"]; Assert.Equal(expected, source.SelectMany((e, i) => i == 4 ? e.total : Enumerable.Empty(), (e, f) => f.ToString())); } @@ -372,31 +372,23 @@ public void ForcedToEnumeratorDoesntEnumerateIndexedResultSel() Assert.False(en is not null && en.MoveNext()); } - [Theory] - [MemberData(nameof(ParameterizedTestsData))] - public void ParameterizedTests(IEnumerable source, Func> selector) + [Fact] + public void ParameterizedTests() { - Assert.All(CreateSources(source), source => + for (int i = 1; i <= 20; i++) { - var expected = source.Select(i => selector(i)).Aggregate((l, r) => l.Concat(r)); - var actual = source.SelectMany(selector); + Assert.All(CreateSources(Enumerable.Range(1, i)), source => + { + Func> selector = n => Enumerable.Range(i, n); - Assert.Equal(expected, actual); - Assert.Equal(expected.Count(), actual.Count()); // SelectMany may employ an optimized Count implementation. - Assert.Equal(expected.ToArray(), actual.ToArray()); - Assert.Equal(expected.ToList(), actual.ToList()); - }); - } + var expected = source.Select(i => selector(i)).Aggregate((l, r) => l.Concat(r)).ToArray(); + var actual = source.SelectMany(selector); - public static IEnumerable ParameterizedTestsData() - { - foreach (Func, IEnumerable> transform in IdentityTransforms()) - { - for (int i = 1; i <= 20; i++) - { - Func> selector = n => transform(Enumerable.Range(i, n)); - yield return new object[] { Enumerable.Range(1, i), selector }; - } + Assert.Equal(expected, actual); + Assert.Equal(expected.Length, actual.Count()); // SelectMany may employ an optimized Count implementation. + Assert.Equal(expected, actual.ToArray()); + Assert.Equal(expected, actual.ToList()); + }); } } @@ -465,12 +457,12 @@ public void DisposeAfterEnumeration(int sourceLength, int subLength) public static IEnumerable DisposeAfterEnumerationData() { - int[] lengths = { 1, 2, 3, 5, 8, 13, 21, 34 }; + int[] lengths = [1, 2, 3, 5, 8, 13, 21, 34]; return lengths.SelectMany(l => lengths, (l1, l2) => new object[] { l1, l2 }); } - [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsSpeedOptimized))] + [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsLinqSpeedOptimized))] [InlineData(new[] { int.MaxValue, 1 })] [InlineData(new[] { 2, int.MaxValue - 1 })] [InlineData(new[] { 123, 456, int.MaxValue - 100000, 123456 })] @@ -536,98 +528,91 @@ public void EvaluateSelectorOncePerItem(int count) public static IEnumerable GetToArrayDataSources() { // Marker at the end - yield return new object[] - { + yield return + [ new IEnumerable[] { - new TestEnumerable(new int[] { 0 }), - new TestEnumerable(new int[] { 1 }), - new TestEnumerable(new int[] { 2 }), - new int[] { 3 }, + new TestEnumerable([0]), + new TestEnumerable([1]), + new TestEnumerable([2]), [3], } - }; + ]; // Marker at beginning - yield return new object[] - { + yield return + [ new IEnumerable[] { - new int[] { 0 }, - new TestEnumerable(new int[] { 1 }), - new TestEnumerable(new int[] { 2 }), - new TestEnumerable(new int[] { 3 }), + [0], + new TestEnumerable([1]), + new TestEnumerable([2]), + new TestEnumerable([3]), } - }; + ]; // Marker in middle - yield return new object[] - { + yield return + [ new IEnumerable[] { - new TestEnumerable(new int[] { 0 }), - new int[] { 1 }, - new TestEnumerable(new int[] { 2 }), + new TestEnumerable([0]), [1], + new TestEnumerable([2]), } - }; + ]; // Non-marker in middle - yield return new object[] - { + yield return + [ new IEnumerable[] { - new int[] { 0 }, - new TestEnumerable(new int[] { 1 }), - new int[] { 2 }, + [0], + new TestEnumerable([1]), [2], } - }; + ]; // Big arrays (marker in middle) - yield return new object[] - { + yield return + [ new IEnumerable[] { new TestEnumerable(Enumerable.Range(0, 100).ToArray()), Enumerable.Range(100, 100).ToArray(), new TestEnumerable(Enumerable.Range(200, 100).ToArray()), } - }; + ]; // Big arrays (non-marker in middle) - yield return new object[] - { + yield return + [ new IEnumerable[] { Enumerable.Range(0, 100).ToArray(), new TestEnumerable(Enumerable.Range(100, 100).ToArray()), Enumerable.Range(200, 100).ToArray(), } - }; + ]; // Interleaved (first marker) - yield return new object[] - { + yield return + [ new IEnumerable[] { - new int[] { 0 }, - new TestEnumerable(new int[] { 1 }), - new int[] { 2 }, - new TestEnumerable(new int[] { 3 }), - new int[] { 4 }, + [0], + new TestEnumerable([1]), [2], + new TestEnumerable([3]), [4], } - }; + ]; // Interleaved (first non-marker) - yield return new object[] - { + yield return + [ new IEnumerable[] { - new TestEnumerable(new int[] { 0 }), - new int[] { 1 }, - new TestEnumerable(new int[] { 2 }), - new int[] { 3 }, - new TestEnumerable(new int[] { 4 }), + new TestEnumerable([0]), [1], + new TestEnumerable([2]), [3], + new TestEnumerable([4]), } - }; + ]; } } } diff --git a/tests/System.Linq.Tests/Tests/System.Linq/SelectTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/SelectTests.cs index ae0c86ca..3a03564a 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/SelectTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/SelectTests.cs @@ -35,7 +35,7 @@ public void SingleElement() { new { name = "Prakash", custID = 98088 } }; - string[] expected = { "Prakash" }; + string[] expected = ["Prakash"]; Assert.Equal(expected, source.Select(e => e.name)); } @@ -50,7 +50,7 @@ public void SelectProperty() new { name=(string)null, custID=30349 }, new { name="Prakash", custID=39030 } }; - string[] expected = { "Prakash", "Bob", "Chris", null, "Prakash" }; + string[] expected = ["Prakash", "Bob", "Chris", null, "Prakash"]; Assert.Equal(expected, source.Select(e => e.name)); } @@ -64,7 +64,7 @@ public void RunOnce() new { name=(string)null, custID=30349 }, new { name="Prakash", custID=39030 } }; - string[] expected = { "Prakash", "Bob", "Chris", null, "Prakash" }; + string[] expected = ["Prakash", "Bob", "Chris", null, "Prakash"]; Assert.Equal(expected, source.RunOnce().Select(e => e.name)); Assert.Equal(expected, source.ToArray().RunOnce().Select(e => e.name)); Assert.Equal(expected, source.ToList().RunOnce().Select(e => e.name)); @@ -73,7 +73,7 @@ public void RunOnce() [Fact] public void EmptyWithIndexedSelector() { - Assert.Equal(Enumerable.Empty(), Enumerable.Empty().Select((s, i) => s.Length + i)); + Assert.Equal([], Enumerable.Empty().Select((s, i) => s.Length + i)); } [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))] @@ -93,7 +93,7 @@ public void SingleElementIndexedSelector() { new { name = "Prakash", custID = 98088 } }; - string[] expected = { "Prakash" }; + string[] expected = ["Prakash"]; Assert.Equal(expected, source.Select((e, index) => e.name)); } @@ -108,7 +108,7 @@ public void SelectPropertyPassingIndex() new { name=(string)null, custID=30349 }, new { name="Prakash", custID=39030 } }; - string[] expected = { "Prakash", "Bob", "Chris", null, "Prakash" }; + string[] expected = ["Prakash", "Bob", "Chris", null, "Prakash"]; Assert.Equal(expected, source.Select((e, i) => e.name)); } @@ -120,7 +120,7 @@ public void SelectPropertyUsingIndex() new { name="Bob", custID=29099 }, new { name="Chris", custID=39033 } }; - string[] expected = { "Prakash", null, null }; + string[] expected = ["Prakash", null, null]; Assert.Equal(expected, source.Select((e, i) => i == 0 ? e.name : null)); } @@ -135,7 +135,7 @@ public void SelectPropertyPassingIndexOnLast() new { name="Allen", custID=39033 }, new { name="Chuck", custID=39033 } }; - string[] expected = { null, null, null, null, null, "Chuck" }; + string[] expected = [null, null, null, null, null, "Chuck"]; Assert.Equal(expected, source.Select((e, i) => i == 5 ? e.name : null)); } @@ -143,14 +143,14 @@ public void SelectPropertyPassingIndexOnLast() public void Overflow() { var selected = new FastInfiniteEnumerator().Select((e, i) => e); - using (var en = selected.GetEnumerator()) - Assert.Throws(() => + using var en = selected.GetEnumerator(); + Assert.Throws(() => + { + while (en.MoveNext()) { - while (en.MoveNext()) - { - } - }); + } + }); } [Fact] @@ -192,7 +192,7 @@ public void Select_SelectorIsNull_ArgumentNullExceptionThrown() public void Select_SourceIsAnArray_ExecutionIsDeferred() { bool funcCalled = false; - Func[] source = new Func[] { () => { funcCalled = true; return 1; } }; + Func[] source = [() => { funcCalled = true; return 1; }]; IEnumerable query = source.Select(d => d()); Assert.False(funcCalled); @@ -202,7 +202,14 @@ public void Select_SourceIsAnArray_ExecutionIsDeferred() public void Select_SourceIsAList_ExecutionIsDeferred() { bool funcCalled = false; - List> source = new List>() { () => { funcCalled = true; return 1; } }; + List> source = + [ + () => + { + funcCalled = true; + return 1; + } + ]; IEnumerable query = source.Select(d => d()); Assert.False(funcCalled); @@ -242,7 +249,7 @@ public void Select_SourceIsIEnumerable_ExecutionIsDeferred() public void SelectSelect_SourceIsAnArray_ExecutionIsDeferred() { bool funcCalled = false; - Func[] source = new Func[] { () => { funcCalled = true; return 1; } }; + Func[] source = [() => { funcCalled = true; return 1; }]; IEnumerable query = source.Select(d => d).Select(d => d()); Assert.False(funcCalled); @@ -252,7 +259,14 @@ public void SelectSelect_SourceIsAnArray_ExecutionIsDeferred() public void SelectSelect_SourceIsAList_ExecutionIsDeferred() { bool funcCalled = false; - List> source = new List>() { () => { funcCalled = true; return 1; } }; + List> source = + [ + () => + { + funcCalled = true; + return 1; + } + ]; IEnumerable query = source.Select(d => d).Select(d => d()); Assert.False(funcCalled); @@ -291,7 +305,7 @@ public void SelectSelect_SourceIsIEnumerable_ExecutionIsDeferred() [Fact] public void Select_SourceIsAnArray_ReturnsExpectedValues() { - int[] source = new[] { 1, 2, 3, 4, 5 }; + int[] source = [1, 2, 3, 4, 5]; Func selector = i => i + 1; IEnumerable query = source.Select(selector); @@ -310,7 +324,7 @@ public void Select_SourceIsAnArray_ReturnsExpectedValues() [Fact] public void Select_SourceIsAList_ReturnsExpectedValues() { - List source = new List { 1, 2, 3, 4, 5 }; + List source = [1, 2, 3, 4, 5]; Func selector = i => i + 1; IEnumerable query = source.Select(selector); @@ -387,12 +401,12 @@ public void Select_SourceIsIEnumerable_ReturnsExpectedValues() [Fact] public void Select_SourceIsAnArray_CurrentIsDefaultOfTAfterEnumeration() { - int[] source = new[] { 1 }; + int[] source = [1]; Func selector = i => i + 1; IEnumerable query = source.Select(selector); - var enumerator = query.GetEnumerator(); + using var enumerator = query.GetEnumerator(); while (enumerator.MoveNext()) ; Assert.Equal(default(int), enumerator.Current); @@ -401,12 +415,12 @@ public void Select_SourceIsAnArray_CurrentIsDefaultOfTAfterEnumeration() [Fact] public void Select_SourceIsAList_CurrentIsDefaultOfTAfterEnumeration() { - List source = new List() { 1 }; + List source = [1]; Func selector = i => i + 1; IEnumerable query = source.Select(selector); - var enumerator = query.GetEnumerator(); + using var enumerator = query.GetEnumerator(); while (enumerator.MoveNext()) ; Assert.Equal(default(int), enumerator.Current); @@ -420,7 +434,7 @@ public void Select_SourceIsIReadOnlyCollection_CurrentIsDefaultOfTAfterEnumerati IEnumerable query = source.Select(selector); - var enumerator = query.GetEnumerator(); + using var enumerator = query.GetEnumerator(); while (enumerator.MoveNext()) ; Assert.Equal(default(int), enumerator.Current); @@ -448,7 +462,7 @@ public void Select_SourceIsIEnumerable_CurrentIsDefaultOfTAfterEnumeration() IEnumerable query = source.Select(selector); - var enumerator = query.GetEnumerator(); + using var enumerator = query.GetEnumerator(); while (enumerator.MoveNext()) ; Assert.Equal(default(int), enumerator.Current); @@ -458,7 +472,7 @@ public void Select_SourceIsIEnumerable_CurrentIsDefaultOfTAfterEnumeration() public void SelectSelect_SourceIsAnArray_ReturnsExpectedValues() { Func selector = i => i + 1; - int[] source = new[] { 1, 2, 3, 4, 5 }; + int[] source = [1, 2, 3, 4, 5]; IEnumerable query = source.Select(selector).Select(selector); @@ -476,7 +490,7 @@ public void SelectSelect_SourceIsAnArray_ReturnsExpectedValues() [Fact] public void SelectSelect_SourceIsAList_ReturnsExpectedValues() { - List source = new List { 1, 2, 3, 4, 5 }; + List source = [1, 2, 3, 4, 5]; Func selector = i => i + 1; IEnumerable query = source.Select(selector).Select(selector); @@ -553,7 +567,7 @@ public void SelectSelect_SourceIsIEnumerable_ReturnsExpectedValues() [Fact] public void Select_SourceIsEmptyEnumerable_ReturnedCollectionHasNoElements() { - IEnumerable source = Enumerable.Empty(); + IEnumerable source = []; bool wasSelectorCalled = false; IEnumerable result = source.Select(i => { wasSelectorCalled = true; return i + 1; }); @@ -571,11 +585,11 @@ public void Select_SourceIsEmptyEnumerable_ReturnedCollectionHasNoElements() [Fact] public void Select_ExceptionThrownFromSelector_ExceptionPropagatedToTheCaller() { - int[] source = new[] { 1, 2, 3, 4, 5 }; + int[] source = [1, 2, 3, 4, 5]; Func selector = i => { throw new InvalidOperationException(); }; var result = source.Select(selector); - var enumerator = result.GetEnumerator(); + using var enumerator = result.GetEnumerator(); Assert.Throws(() => enumerator.MoveNext()); } @@ -583,7 +597,7 @@ public void Select_ExceptionThrownFromSelector_ExceptionPropagatedToTheCaller() [Fact] public void Select_ExceptionThrownFromSelector_IteratorCanBeUsedAfterExceptionIsCaught() { - int[] source = new[] { 1, 2, 3, 4, 5 }; + int[] source = [1, 2, 3, 4, 5]; Func selector = i => { if (i == 1) @@ -592,7 +606,7 @@ public void Select_ExceptionThrownFromSelector_IteratorCanBeUsedAfterExceptionIs }; var result = source.Select(selector); - var enumerator = result.GetEnumerator(); + using var enumerator = result.GetEnumerator(); Assert.Throws(() => enumerator.MoveNext()); enumerator.MoveNext(); @@ -606,7 +620,7 @@ public void Select_ExceptionThrownFromCurrentOfSourceIterator_ExceptionPropagate Func selector = i => i + 1; var result = source.Select(selector); - var enumerator = result.GetEnumerator(); + using var enumerator = result.GetEnumerator(); Assert.Throws(() => enumerator.MoveNext()); } @@ -618,7 +632,7 @@ public void Select_ExceptionThrownFromCurrentOfSourceIterator_IteratorCanBeUsedA Func selector = i => i + 1; var result = source.Select(selector); - var enumerator = result.GetEnumerator(); + using var enumerator = result.GetEnumerator(); Assert.Throws(() => enumerator.MoveNext()); enumerator.MoveNext(); @@ -649,7 +663,7 @@ public void Select_ExceptionThrownFromMoveNextOfSourceIterator_ExceptionPropagat Func selector = i => i + 1; var result = source.Select(selector); - var enumerator = result.GetEnumerator(); + using var enumerator = result.GetEnumerator(); Assert.Throws(() => enumerator.MoveNext()); } @@ -661,7 +675,7 @@ public void Select_ExceptionThrownFromMoveNextOfSourceIterator_IteratorCanBeUsed Func selector = i => i + 1; var result = source.Select(selector); - var enumerator = result.GetEnumerator(); + using var enumerator = result.GetEnumerator(); Assert.Throws(() => enumerator.MoveNext()); enumerator.MoveNext(); @@ -675,7 +689,7 @@ public void Select_ExceptionThrownFromGetEnumeratorOnSource_ExceptionPropagatedT Func selector = i => i + 1; var result = source.Select(selector); - var enumerator = result.GetEnumerator(); + using var enumerator = result.GetEnumerator(); Assert.Throws(() => enumerator.MoveNext()); } @@ -687,7 +701,7 @@ public void Select_ExceptionThrownFromGetEnumeratorOnSource_CurrentIsSetToDefaul Func selector = i => i.ToString(); var result = source.Select(selector); - var enumerator = result.GetEnumerator(); + using var enumerator = result.GetEnumerator(); Assert.Throws(() => enumerator.MoveNext()); string currentValue = enumerator.Current; @@ -700,11 +714,11 @@ public void Select_ExceptionThrownFromGetEnumeratorOnSource_CurrentIsSetToDefaul [Fact] public void Select_SourceListGetsModifiedDuringIteration_ExceptionIsPropagated() { - List source = new List() { 1, 2, 3, 4, 5 }; + List source = [1, 2, 3, 4, 5]; Func selector = i => i + 1; var result = source.Select(selector); - var enumerator = result.GetEnumerator(); + using var enumerator = result.GetEnumerator(); Assert.True(enumerator.MoveNext()); Assert.Equal(2 /* 1 + 1 */, enumerator.Current); @@ -716,7 +730,7 @@ public void Select_SourceListGetsModifiedDuringIteration_ExceptionIsPropagated() [Fact] public void Select_GetEnumeratorCalledTwice_DifferentInstancesReturned() { - int[] source = new[] { 1, 2, 3, 4, 5 }; + int[] source = [1, 2, 3, 4, 5]; var query = source.Select(i => i + 1); var enumerator1 = query.GetEnumerator(); @@ -732,11 +746,11 @@ public void Select_GetEnumeratorCalledTwice_DifferentInstancesReturned() [Fact] public void Select_ResetCalledOnEnumerator_ThrowsException() { - int[] source = new[] { 1, 2, 3, 4, 5 }; + int[] source = [1, 2, 3, 4, 5]; Func selector = i => i + 1; IEnumerable result = source.Select(selector); - IEnumerator enumerator = result.GetEnumerator(); + using IEnumerator enumerator = result.GetEnumerator(); Assert.Throws(() => enumerator.Reset()); } @@ -816,10 +830,10 @@ public void Select_SourceIsAnIList_Count() public void Select_SourceIsArray_Skip() { var source = new[] { 1, 2, 3, 4 }.Select(i => i * 2); - Assert.Equal(new[] { 6, 8 }, source.Skip(2)); - Assert.Equal(new[] { 6, 8 }, source.Skip(2).Skip(-1)); - Assert.Equal(new[] { 6, 8 }, source.Skip(1).Skip(1)); - Assert.Equal(new[] { 2, 4, 6, 8 }, source.Skip(-1)); + Assert.Equal([6, 8], source.Skip(2)); + Assert.Equal([6, 8], source.Skip(2).Skip(-1)); + Assert.Equal([6, 8], source.Skip(1).Skip(1)); + Assert.Equal([2, 4, 6, 8], source.Skip(-1)); Assert.Empty(source.Skip(4)); Assert.Empty(source.Skip(20)); } @@ -828,10 +842,10 @@ public void Select_SourceIsArray_Skip() public void Select_SourceIsList_Skip() { var source = new List { 1, 2, 3, 4 }.Select(i => i * 2); - Assert.Equal(new[] { 6, 8 }, source.Skip(2)); - Assert.Equal(new[] { 6, 8 }, source.Skip(2).Skip(-1)); - Assert.Equal(new[] { 6, 8 }, source.Skip(1).Skip(1)); - Assert.Equal(new[] { 2, 4, 6, 8 }, source.Skip(-1)); + Assert.Equal([6, 8], source.Skip(2)); + Assert.Equal([6, 8], source.Skip(2).Skip(-1)); + Assert.Equal([6, 8], source.Skip(1).Skip(1)); + Assert.Equal([2, 4, 6, 8], source.Skip(-1)); Assert.Empty(source.Skip(4)); Assert.Empty(source.Skip(20)); } @@ -840,10 +854,10 @@ public void Select_SourceIsList_Skip() public void Select_SourceIsIList_Skip() { var source = new List { 1, 2, 3, 4 }.AsReadOnly().Select(i => i * 2); - Assert.Equal(new[] { 6, 8 }, source.Skip(2)); - Assert.Equal(new[] { 6, 8 }, source.Skip(2).Skip(-1)); - Assert.Equal(new[] { 6, 8 }, source.Skip(1).Skip(1)); - Assert.Equal(new[] { 2, 4, 6, 8 }, source.Skip(-1)); + Assert.Equal([6, 8], source.Skip(2)); + Assert.Equal([6, 8], source.Skip(2).Skip(-1)); + Assert.Equal([6, 8], source.Skip(1).Skip(1)); + Assert.Equal([2, 4, 6, 8], source.Skip(-1)); Assert.Empty(source.Skip(4)); Assert.Empty(source.Skip(20)); } @@ -852,45 +866,45 @@ public void Select_SourceIsIList_Skip() public void Select_SourceIsArray_Take() { var source = new[] { 1, 2, 3, 4 }.Select(i => i * 2); - Assert.Equal(new[] { 2, 4 }, source.Take(2)); - Assert.Equal(new[] { 2, 4 }, source.Take(3).Take(2)); + Assert.Equal([2, 4], source.Take(2)); + Assert.Equal([2, 4], source.Take(3).Take(2)); Assert.Empty(source.Take(-1)); - Assert.Equal(new[] { 2, 4, 6, 8 }, source.Take(4)); - Assert.Equal(new[] { 2, 4, 6, 8 }, source.Take(40)); - Assert.Equal(new[] { 2 }, source.Take(1)); - Assert.Equal(new[] { 4 }, source.Skip(1).Take(1)); - Assert.Equal(new[] { 6 }, source.Take(3).Skip(2)); - Assert.Equal(new[] { 2 }, source.Take(3).Take(1)); + Assert.Equal([2, 4, 6, 8], source.Take(4)); + Assert.Equal([2, 4, 6, 8], source.Take(40)); + Assert.Equal([2], source.Take(1)); + Assert.Equal([4], source.Skip(1).Take(1)); + Assert.Equal([6], source.Take(3).Skip(2)); + Assert.Equal([2], source.Take(3).Take(1)); } [Fact] public void Select_SourceIsList_Take() { var source = new List { 1, 2, 3, 4 }.Select(i => i * 2); - Assert.Equal(new[] { 2, 4 }, source.Take(2)); - Assert.Equal(new[] { 2, 4 }, source.Take(3).Take(2)); + Assert.Equal([2, 4], source.Take(2)); + Assert.Equal([2, 4], source.Take(3).Take(2)); Assert.Empty(source.Take(-1)); - Assert.Equal(new[] { 2, 4, 6, 8 }, source.Take(4)); - Assert.Equal(new[] { 2, 4, 6, 8 }, source.Take(40)); - Assert.Equal(new[] { 2 }, source.Take(1)); - Assert.Equal(new[] { 4 }, source.Skip(1).Take(1)); - Assert.Equal(new[] { 6 }, source.Take(3).Skip(2)); - Assert.Equal(new[] { 2 }, source.Take(3).Take(1)); + Assert.Equal([2, 4, 6, 8], source.Take(4)); + Assert.Equal([2, 4, 6, 8], source.Take(40)); + Assert.Equal([2], source.Take(1)); + Assert.Equal([4], source.Skip(1).Take(1)); + Assert.Equal([6], source.Take(3).Skip(2)); + Assert.Equal([2], source.Take(3).Take(1)); } [Fact] public void Select_SourceIsIList_Take() { var source = new List { 1, 2, 3, 4 }.AsReadOnly().Select(i => i * 2); - Assert.Equal(new[] { 2, 4 }, source.Take(2)); - Assert.Equal(new[] { 2, 4 }, source.Take(3).Take(2)); + Assert.Equal([2, 4], source.Take(2)); + Assert.Equal([2, 4], source.Take(3).Take(2)); Assert.Empty(source.Take(-1)); - Assert.Equal(new[] { 2, 4, 6, 8 }, source.Take(4)); - Assert.Equal(new[] { 2, 4, 6, 8 }, source.Take(40)); - Assert.Equal(new[] { 2 }, source.Take(1)); - Assert.Equal(new[] { 4 }, source.Skip(1).Take(1)); - Assert.Equal(new[] { 6 }, source.Take(3).Skip(2)); - Assert.Equal(new[] { 2 }, source.Take(3).Take(1)); + Assert.Equal([2, 4, 6, 8], source.Take(4)); + Assert.Equal([2, 4, 6, 8], source.Take(40)); + Assert.Equal([2], source.Take(1)); + Assert.Equal([4], source.Skip(1).Take(1)); + Assert.Equal([6], source.Take(3).Skip(2)); + Assert.Equal([2], source.Take(3).Take(1)); } [Fact] @@ -1096,15 +1110,15 @@ public void Select_SourceIsArray_SkipRepeatCalls() public void Select_SourceIsArraySkipSelect() { var source = new[] { 1, 2, 3, 4 }.Select(i => i * 2).Skip(1).Select(i => i + 1); - Assert.Equal(new[] { 5, 7, 9 }, source); + Assert.Equal([5, 7, 9], source); } [Fact] public void Select_SourceIsArrayTakeTake() { var source = new[] { 1, 2, 3, 4 }.Select(i => i * 2).Take(2).Take(1); - Assert.Equal(new[] { 2 }, source); - Assert.Equal(new[] { 2 }, source.Take(10)); + Assert.Equal([2], source); + Assert.Equal([2], source.Take(10)); } [Fact] @@ -1167,19 +1181,17 @@ public void MoveNextAfterDispose(IEnumerable source) foreach (IEnumerable equivalentSource in identityTransforms.Select(t => t(source))) { IEnumerable result = equivalentSource.Select(i => i); - using (IEnumerator e = result.GetEnumerator()) - { - while (e.MoveNext()) ; // Loop until we reach the end of the iterator, @ which pt it gets disposed. - Assert.False(e.MoveNext()); // MoveNext should not throw an exception after Dispose. - } + using IEnumerator e = result.GetEnumerator(); + while (e.MoveNext()) ; // Loop until we reach the end of the iterator, @ which pt it gets disposed. + Assert.False(e.MoveNext()); // MoveNext should not throw an exception after Dispose. } } public static IEnumerable MoveNextAfterDisposeData() { - yield return new object[] { Array.Empty() }; - yield return new object[] { new int[1] }; - yield return new object[] { Enumerable.Range(1, 30) }; + yield return [Array.Empty()]; + yield return [new int[1]]; + yield return [Enumerable.Range(1, 30)]; } [Theory] @@ -1219,7 +1231,7 @@ public static IEnumerable RunSelectorDuringCountData() foreach (var transform in transforms) { - yield return new object[] { transform(enumerable) }; + yield return [transform(enumerable)]; } } } diff --git a/tests/System.Linq.Tests/Tests/System.Linq/SequenceEqualTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/SequenceEqualTests.cs index 7393d189..c22f4e2e 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/SequenceEqualTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/SequenceEqualTests.cs @@ -33,8 +33,8 @@ public void SameResultsRepeatCallsStringQuery() [Fact] public void BothEmpty() { - int[] first = { }; - int[] second = { }; + int[] first = []; + int[] second = []; Assert.True(first.SequenceEqual(second)); Assert.True(FlipIsCollection(first).SequenceEqual(second)); @@ -45,8 +45,8 @@ public void BothEmpty() [Fact] public void MismatchInMiddle() { - int?[] first = { 1, 2, 3, 4 }; - int?[] second = { 1, 2, 6, 4 }; + int?[] first = [1, 2, 3, 4]; + int?[] second = [1, 2, 6, 4]; Assert.False(first.SequenceEqual(second)); Assert.False(FlipIsCollection(first).SequenceEqual(second)); @@ -57,8 +57,8 @@ public void MismatchInMiddle() [Fact] public void NullComparer() { - string[] first = { "Bob", "Tim", "Chris" }; - string[] second = { "Bbo", "mTi", "rishC" }; + string[] first = ["Bob", "Tim", "Chris"]; + string[] second = ["Bbo", "mTi", "rishC"]; Assert.False(first.SequenceEqual(second, null)); Assert.False(FlipIsCollection(first).SequenceEqual(second, null)); @@ -69,8 +69,8 @@ public void NullComparer() [Fact] public void CustomComparer() { - string[] first = { "Bob", "Tim", "Chris" }; - string[] second = { "Bbo", "mTi", "rishC" }; + string[] first = ["Bob", "Tim", "Chris"]; + string[] second = ["Bbo", "mTi", "rishC"]; Assert.True(first.SequenceEqual(second, new AnagramEqualityComparer())); Assert.True(FlipIsCollection(first).SequenceEqual(second, new AnagramEqualityComparer())); @@ -81,8 +81,8 @@ public void CustomComparer() [Fact] public void RunOnce() { - string[] first = { "Bob", "Tim", "Chris" }; - string[] second = { "Bbo", "mTi", "rishC" }; + string[] first = ["Bob", "Tim", "Chris"]; + string[] second = ["Bbo", "mTi", "rishC"]; Assert.True(first.RunOnce().SequenceEqual(second.RunOnce(), new AnagramEqualityComparer())); } @@ -90,8 +90,8 @@ public void RunOnce() [Fact] public void BothSingleNullExplicitComparer() { - string[] first = { null }; - string[] second = { null }; + string[] first = [null]; + string[] second = [null]; Assert.True(first.SequenceEqual(second, StringComparer.Ordinal)); Assert.True(FlipIsCollection(first).SequenceEqual(second, StringComparer.Ordinal)); @@ -102,8 +102,8 @@ public void BothSingleNullExplicitComparer() [Fact] public void BothMatchIncludingNullElements() { - int?[] first = { -6, null, 0, -4, 9, 10, 20 }; - int?[] second = { -6, null, 0, -4, 9, 10, 20 }; + int?[] first = [-6, null, 0, -4, 9, 10, 20]; + int?[] second = [-6, null, 0, -4, 9, 10, 20]; Assert.True(first.SequenceEqual(second)); Assert.True(FlipIsCollection(first).SequenceEqual(second)); @@ -114,8 +114,8 @@ public void BothMatchIncludingNullElements() [Fact] public void EmptyWithNonEmpty() { - int?[] first = { }; - int?[] second = { 2, 3, 4 }; + int?[] first = []; + int?[] second = [2, 3, 4]; Assert.False(first.SequenceEqual(second)); Assert.False(FlipIsCollection(first).SequenceEqual(second)); @@ -126,8 +126,8 @@ public void EmptyWithNonEmpty() [Fact] public void NonEmptyWithEmpty() { - int?[] first = { 2, 3, 4 }; - int?[] second = { }; + int?[] first = [2, 3, 4]; + int?[] second = []; Assert.False(first.SequenceEqual(second)); Assert.False(FlipIsCollection(first).SequenceEqual(second)); @@ -138,8 +138,8 @@ public void NonEmptyWithEmpty() [Fact] public void MismatchingSingletons() { - int?[] first = { 2 }; - int?[] second = { 4 }; + int?[] first = [2]; + int?[] second = [4]; Assert.False(first.SequenceEqual(second)); Assert.False(FlipIsCollection(first).SequenceEqual(second)); @@ -150,8 +150,8 @@ public void MismatchingSingletons() [Fact] public void MismatchOnFirst() { - int?[] first = { 1, 2, 3, 4, 5 }; - int?[] second = { 2, 2, 3, 4, 5 }; + int?[] first = [1, 2, 3, 4, 5]; + int?[] second = [2, 2, 3, 4, 5]; Assert.False(first.SequenceEqual(second)); Assert.False(FlipIsCollection(first).SequenceEqual(second)); @@ -163,8 +163,8 @@ public void MismatchOnFirst() [Fact] public void MismatchOnLast() { - int?[] first = { 1, 2, 3, 4, 4 }; - int?[] second = { 1, 2, 3, 4, 5 }; + int?[] first = [1, 2, 3, 4, 4]; + int?[] second = [1, 2, 3, 4, 5]; Assert.False(first.SequenceEqual(second)); Assert.False(FlipIsCollection(first).SequenceEqual(second)); @@ -175,8 +175,8 @@ public void MismatchOnLast() [Fact] public void SecondLargerThanFirst() { - int?[] first = { 1, 2, 3, 4 }; - int?[] second = { 1, 2, 3, 4, 4 }; + int?[] first = [1, 2, 3, 4]; + int?[] second = [1, 2, 3, 4, 4]; Assert.False(first.SequenceEqual(second)); Assert.False(FlipIsCollection(first).SequenceEqual(second)); @@ -187,8 +187,8 @@ public void SecondLargerThanFirst() [Fact] public void FirstLargerThanSecond() { - int?[] first = { 1, 2, 3, 4, 4 }; - int?[] second = { 1, 2, 3, 4 }; + int?[] first = [1, 2, 3, 4, 4]; + int?[] second = [1, 2, 3, 4]; Assert.False(first.SequenceEqual(second)); Assert.False(FlipIsCollection(first).SequenceEqual(second)); @@ -200,7 +200,7 @@ public void FirstLargerThanSecond() public void FirstSourceNull() { int[] first = null; - int[] second = { }; + int[] second = []; AssertExtensions.Throws("first", () => first.SequenceEqual(second)); } @@ -208,7 +208,7 @@ public void FirstSourceNull() [Fact] public void SecondSourceNull() { - int[] first = { }; + int[] first = []; int[] second = null; AssertExtensions.Throws("second", () => first.SequenceEqual(second)); diff --git a/tests/System.Linq.Tests/Tests/System.Linq/ShuffleTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/ShuffleTests.cs index 79e387fd..e36efae5 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/ShuffleTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/ShuffleTests.cs @@ -6,7 +6,6 @@ using Xunit; #if NET10_0_OR_GREATER - namespace System.Linq.Tests { public class ShuffleTests : EnumerableTests diff --git a/tests/System.Linq.Tests/Tests/System.Linq/SingleOrDefaultTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/SingleOrDefaultTests.cs index 8441f8a3..c453fcc6 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/SingleOrDefaultTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/SingleOrDefaultTests.cs @@ -27,87 +27,39 @@ public void SameResultsRepeatCallsStringQuery() } [Fact] - public void EmptyIList() + public void Empty() { - int?[] source = { }; - int? expected = null; - - Assert.Equal(expected, source.SingleOrDefault()); - } - - [Fact] - public void EmptyIListDefault() - { - int?[] source = { }; - int expected = 5; - - Assert.Equal(expected, source.SingleOrDefault(5)); - } - - [Fact] - public void SingleElementIList() - { - int[] source = { 4 }; - int expected = 4; - - Assert.Equal(expected, source.SingleOrDefault()); + foreach (IEnumerable source in CreateSources([])) + { + Assert.Null(source.SingleOrDefault()); + Assert.Equal(5, source.SingleOrDefault(5)); + } } [Fact] - public void SingleElementIListDefault() + public void SingleElement() { - int[] source = { 4 }; - int expected = 4; - - Assert.Equal(expected, source.SingleOrDefault(5)); + foreach (IEnumerable source in CreateSources([4])) + { + Assert.Equal(4, source.SingleOrDefault()); + Assert.Equal(4, source.SingleOrDefault(5)); + } } [Fact] public void ManyElementIList() { - int[] source = { 4, 4, 4, 4, 4 }; - - Assert.Throws(() => source.SingleOrDefault()); - } - - [Fact] - public void ManyElementIListDefault() - { - int[] source = { 4, 4, 4, 4, 4 }; - - Assert.Throws(() => source.SingleOrDefault(5)); - } - - [Fact] - public void EmptyNotIList() - { - IEnumerable source = RepeatedNumberGuaranteedNotCollectionType(0, 0); - int expected = default(int); - - Assert.Equal(expected, source.SingleOrDefault()); - } - - [Fact] - public void SingleElementNotIList() - { - IEnumerable source = RepeatedNumberGuaranteedNotCollectionType(-5, 1); - int expected = -5; - - Assert.Equal(expected, source.SingleOrDefault()); - } - - [Fact] - public void ManyElementNotIList() - { - IEnumerable source = RepeatedNumberGuaranteedNotCollectionType(3, 5); - - Assert.Throws(() => source.SingleOrDefault()); + foreach (IEnumerable source in CreateSources([4, 4, 4, 4, 4])) + { + Assert.Throws(() => source.SingleOrDefault()); + Assert.Throws(() => source.SingleOrDefault(4)); + } } [Fact] public void EmptySourceWithPredicate() { - int[] source = { }; + int[] source = []; int expected = default(int); Assert.All(CreateSources(source), source => @@ -119,7 +71,7 @@ public void EmptySourceWithPredicate() [Fact] public void EmptySourceWithPredicateDefault() { - int[] source = { }; + int[] source = []; int expected = 5; Assert.All(CreateSources(source), source => @@ -131,7 +83,7 @@ public void EmptySourceWithPredicateDefault() [Fact] public void SingleElementPredicateTrue() { - int[] source = { 4 }; + int[] source = [4]; int expected = 4; Assert.All(CreateSources(source), source => @@ -143,7 +95,7 @@ public void SingleElementPredicateTrue() [Fact] public void SingleElementPredicateTrueDefault() { - int[] source = { 4 }; + int[] source = [4]; int expected = 4; Assert.All(CreateSources(source), source => @@ -155,7 +107,7 @@ public void SingleElementPredicateTrueDefault() [Fact] public void SingleElementPredicateFalse() { - int[] source = { 3 }; + int[] source = [3]; int expected = default(int); Assert.All(CreateSources(source), source => @@ -167,7 +119,7 @@ public void SingleElementPredicateFalse() [Fact] public void SingleElementPredicateFalseDefault() { - int[] source = { 3 }; + int[] source = [3]; int expected = 5; Assert.All(CreateSources(source), source => @@ -179,7 +131,7 @@ public void SingleElementPredicateFalseDefault() [Fact] public void ManyElementsPredicateFalseForAll() { - int[] source = { 3, 1, 7, 9, 13, 19 }; + int[] source = [3, 1, 7, 9, 13, 19]; int expected = default(int); Assert.All(CreateSources(source), source => @@ -191,7 +143,7 @@ public void ManyElementsPredicateFalseForAll() [Fact] public void ManyElementsPredicateFalseForAllDefault() { - int[] source = { 3, 1, 7, 9, 13, 19 }; + int[] source = [3, 1, 7, 9, 13, 19]; int expected = 5; Assert.All(CreateSources(source), source => @@ -203,7 +155,7 @@ public void ManyElementsPredicateFalseForAllDefault() [Fact] public void ManyElementsPredicateTrueForLast() { - int[] source = { 3, 1, 7, 9, 13, 19, 20 }; + int[] source = [3, 1, 7, 9, 13, 19, 20]; int expected = 20; Assert.All(CreateSources(source), source => @@ -215,7 +167,7 @@ public void ManyElementsPredicateTrueForLast() [Fact] public void ManyElementsPredicateTrueForLastDefault() { - int[] source = { 3, 1, 7, 9, 13, 19, 20 }; + int[] source = [3, 1, 7, 9, 13, 19, 20]; int expected = 20; Assert.All(CreateSources(source), source => @@ -227,7 +179,7 @@ public void ManyElementsPredicateTrueForLastDefault() [Fact] public void ManyElementsPredicateTrueForFirstAndFifth() { - int[] source = { 2, 3, 1, 7, 10, 13, 19, 9 }; + int[] source = [2, 3, 1, 7, 10, 13, 19, 9]; Assert.All(CreateSources(source), source => { @@ -238,7 +190,7 @@ public void ManyElementsPredicateTrueForFirstAndFifth() [Fact] public void ManyElementsPredicateTrueForFirstAndFifthDefault() { - int[] source = { 2, 3, 1, 7, 10, 13, 19, 9 }; + int[] source = [2, 3, 1, 7, 10, 13, 19, 9]; Assert.All(CreateSources(source), source => { @@ -287,7 +239,7 @@ public void ThrowsOnNullSourceDefault() [Fact] public void ThrowsOnNullPredicate() { - int[] source = { }; + int[] source = []; Func nullPredicate = null; AssertExtensions.Throws("predicate", () => source.SingleOrDefault(nullPredicate)); } @@ -295,7 +247,7 @@ public void ThrowsOnNullPredicate() [Fact] public void ThrowsOnNullPredicateDefault() { - int[] source = { }; + int[] source = []; Func nullPredicate = null; AssertExtensions.Throws("predicate", () => source.SingleOrDefault(nullPredicate, 5)); } diff --git a/tests/System.Linq.Tests/Tests/System.Linq/SingleTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/SingleTests.cs index 09e49daa..50e6f5cd 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/SingleTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/SingleTests.cs @@ -37,59 +37,37 @@ public void SameResultsRepeatCallsIntQueryWithZero() } [Fact] - public void EmptyIList() + public void Empty() { - int[] source = { }; - - Assert.Throws(() => source.Single()); + foreach (IEnumerable source in CreateSources([])) + { + Assert.Throws(() => source.Single()); + } } [Fact] - public void SingleElementIList() + public void SingleElement() { - int[] source = { 4 }; int expected = 4; - - Assert.Equal(expected, source.Single()); - } - - [Fact] - public void ManyElementIList() - { - int[] source = { 4, 4, 4, 4, 4 }; - - Assert.Throws(() => source.Single()); - } - - [Fact] - public void EmptyNotIList() - { - IEnumerable source = RepeatedNumberGuaranteedNotCollectionType(0, 0); - - Assert.Throws(() => source.Single()); - } - - [Fact] - public void SingleElementNotIList() - { - IEnumerable source = RepeatedNumberGuaranteedNotCollectionType(-5, 1); - int expected = -5; - - Assert.Equal(expected, source.Single()); + foreach (IEnumerable source in CreateSources([4])) + { + Assert.Equal(expected, source.Single()); + } } [Fact] - public void ManyElementNotIList() + public void ManyElement() { - IEnumerable source = RepeatedNumberGuaranteedNotCollectionType(3, 5); - - Assert.Throws(() => source.Single()); + foreach (IEnumerable source in CreateSources([4, 4, 4, 4, 4])) + { + Assert.Throws(() => source.Single()); + } } [Fact] public void EmptySourceWithPredicate() { - int[] source = { }; + int[] source = []; Assert.All(CreateSources(source), source => { @@ -100,7 +78,7 @@ public void EmptySourceWithPredicate() [Fact] public void SingleElementPredicateTrue() { - int[] source = { 4 }; + int[] source = [4]; int expected = 4; Assert.All(CreateSources(source), source => @@ -112,7 +90,7 @@ public void SingleElementPredicateTrue() [Fact] public void SingleElementPredicateFalse() { - int[] source = { 3 }; + int[] source = [3]; Assert.All(CreateSources(source), source => { @@ -123,7 +101,7 @@ public void SingleElementPredicateFalse() [Fact] public void ManyElementsPredicateFalseForAll() { - int[] source = { 3, 1, 7, 9, 13, 19 }; + int[] source = [3, 1, 7, 9, 13, 19]; Assert.All(CreateSources(source), source => { @@ -134,7 +112,7 @@ public void ManyElementsPredicateFalseForAll() [Fact] public void ManyElementsPredicateTrueForLast() { - int[] source = { 3, 1, 7, 9, 13, 19, 20 }; + int[] source = [3, 1, 7, 9, 13, 19, 20]; int expected = 20; Assert.All(CreateSources(source), source => @@ -146,7 +124,7 @@ public void ManyElementsPredicateTrueForLast() [Fact] public void ManyElementsPredicateTrueForFirstAndLast() { - int[] source = { 2, 3, 1, 7, 9, 13, 19, 10 }; + int[] source = [2, 3, 1, 7, 9, 13, 19, 10]; Assert.All(CreateSources(source), source => { @@ -187,7 +165,7 @@ public void ThrowsOnNullSource() [Fact] public void ThrowsOnNullPredicate() { - int[] source = { }; + int[] source = []; Func nullPredicate = null; AssertExtensions.Throws("predicate", () => source.Single(nullPredicate)); } diff --git a/tests/System.Linq.Tests/Tests/System.Linq/SkipLastTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/SkipLastTests.cs index 5a060e9e..3221b32c 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/SkipLastTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/SkipLastTests.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using Xunit; -using static System.Linq.SkipTakeData; +using static System.Linq.Tests.SkipTakeData; namespace System.Linq.Tests { @@ -19,25 +19,26 @@ public void SkipLastThrowsOnNull() [MemberData(nameof(EnumerableData), MemberType = typeof(SkipTakeData))] public void SkipLast(IEnumerable source, int count) { - Assert.All(IdentityTransforms(), transform => - { - IEnumerable equivalent = transform(source); + int[] expected = source.Reverse().Skip(count).Reverse().ToArray(); - IEnumerable expected = equivalent.Reverse().Skip(count).Reverse(); - IEnumerable actual = equivalent.SkipLast(count); + Assert.All(CreateSources(source), source => + { + IEnumerable actual = source.SkipLast(count); Assert.Equal(expected, actual); - Assert.Equal(expected.Count(), actual.Count()); + + Assert.Equal(expected.Length, actual.Count()); Assert.Equal(expected, actual.ToArray()); Assert.Equal(expected, actual.ToList()); - Assert.Equal(expected.FirstOrDefault(), actual.FirstOrDefault()); Assert.Equal(expected.LastOrDefault(), actual.LastOrDefault()); - Assert.All(Enumerable.Range(0, expected.Count()), index => + if (expected.Length > 0) { - Assert.Equal(expected.ElementAt(index), actual.ElementAt(index)); - }); + Assert.Equal(expected[0], actual.ElementAt(0)); + Assert.Equal(expected[^1], actual.ElementAt(expected.Length - 1)); + Assert.Equal(expected[expected.Length / 2], actual.ElementAt(expected.Length / 2)); + } Assert.Equal(0, actual.ElementAtOrDefault(-1)); Assert.Equal(0, actual.ElementAtOrDefault(actual.Count())); @@ -59,7 +60,7 @@ public void EvaluationBehavior(int count) current: () => index, // Yield from 1 up to the limit, inclusive. dispose: () => index ^= int.MinValue); - IEnumerator iterator = source.SkipLast(count).GetEnumerator(); + using IEnumerator iterator = source.SkipLast(count).GetEnumerator(); Assert.Equal(0, index); // Nothing should be done before MoveNext is called. for (int i = 1; i <= count; i++) diff --git a/tests/System.Linq.Tests/Tests/System.Linq/SkipTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/SkipTests.cs index 57538072..94834378 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/SkipTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/SkipTests.cs @@ -9,12 +9,6 @@ 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() { @@ -49,13 +43,13 @@ public void SkipNoneIList() [Fact] public void SkipExcessive() { - Assert.Equal(Enumerable.Empty(), NumberRangeGuaranteedNotCollectionType(0, 20).Skip(42)); + Assert.Equal([], NumberRangeGuaranteedNotCollectionType(0, 20).Skip(42)); } [Fact] public void SkipExcessiveIList() { - Assert.Equal(Enumerable.Empty(), NumberRangeGuaranteedNotCollectionType(0, 20).ToList().Skip(42)); + Assert.Equal([], NumberRangeGuaranteedNotCollectionType(0, 20).ToList().Skip(42)); } [Fact] @@ -86,139 +80,95 @@ public void SkipThrowsOnNullIList() [Fact] public void SkipOnEmpty() { - 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)); - } + foreach (IEnumerable source in CreateSources([])) + { + Assert.Equal([], source.Skip(0)); + Assert.Equal([], source.Skip(-1)); + Assert.Equal([], source.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)); + foreach (IEnumerable source in CreateSources([])) + { + Assert.Equal([], source.Skip(0)); + Assert.Equal([], source.Skip(-1)); + Assert.Equal([], source.Skip(1)); + } } [Fact] public void SkipNegative() { - 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)); + foreach (IEnumerable source in CreateSources(Enumerable.Range(0, 20))) + { + Assert.Equal(Enumerable.Range(0, 20), source.Skip(-42)); + } } [Fact] public void SameResultsRepeatCallsIntQuery() { - var q = GuaranteeNotIList(from x in new[] { 9999, 0, 888, -1, 66, -777, 1, 2, -12345 } - where x > int.MinValue - 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(); + foreach (IEnumerable source in CreateSources([9999, 0, 888, -1, 66, -777, 1, 2, -12345])) + { + IEnumerable q = from x in source + where x > int.MinValue + select x; - Assert.Equal(q.Skip(0), q.Skip(0)); + Assert.Equal(q.Skip(0), q.Skip(0)); + } } [Fact] public void SameResultsRepeatCallsStringQuery() { - 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).ToList(); + foreach (IEnumerable source in CreateSources(["!@#$%^", "C", "AAA", "", "Calling Twice", "SoS", string.Empty])) + { + IEnumerable q = from x in source + where !string.IsNullOrEmpty(x) + select x; - Assert.Equal(q.Skip(0), q.Skip(0)); + Assert.Equal(q.Skip(0), q.Skip(0)); + } } [Fact] public void SkipOne() { - int?[] source = { 3, 100, 4, null, 10 }; - int?[] expected = { 100, 4, null, 10 }; - - 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)); + int?[] expected = [100, 4, null, 10]; + foreach (IEnumerable source in CreateSources([3, 100, 4, null, 10])) + { + Assert.Equal(expected, source.Skip(1)); + } } [Fact] public void SkipAllButOne() { - int?[] source = { 3, 100, null, 4, 10 }; - int?[] expected = { 10 }; - - 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))); + int?[] expected = [10]; + foreach (IEnumerable source in CreateSources([3, 100, 4, null, 10])) + { + Assert.Equal(expected, source.Skip(4)); + } } [Fact] public void SkipOneMoreThanAll() { - int[] source = { 3, 100, 4, 10 }; - 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)); + foreach (IEnumerable source in CreateSources([3, 100, 4, 10])) + { + Assert.Empty(source.Skip(5)); + } } [Fact] public void ForcedToEnumeratorDoesntEnumerate() { - var iterator = NumberRangeGuaranteedNotCollectionType(0, 3).Skip(2); - // Don't insist on this behaviour, but check it's correct if it happens - var en = iterator as IEnumerator; - Assert.False(en is not null && en.MoveNext()); - } - - [Fact] - public void ForcedToEnumeratorDoesntEnumerateIList() - { - var iterator = (new[] { 0, 1, 2 }).Skip(2); - // Don't insist on this behaviour, but check it's correct if it happens - var en = iterator as IEnumerator; - Assert.False(en is not null && en.MoveNext()); + foreach (IEnumerable source in CreateSources(Enumerable.Range(0, 3))) + { + // Don't insist on this behaviour, but check it's correct if it happens + IEnumerable iterator = source.Skip(2); + var en = iterator as IEnumerator; + Assert.False(en is not null && en.MoveNext()); + } } [Fact] @@ -231,243 +181,150 @@ public void Count() [Fact] public void FollowWithTake() { - var source = new[] { 5, 6, 7, 8 }; - var expected = new[] { 6, 7 }; - Assert.Equal(expected, source.Skip(1).Take(2)); - } - - [Fact] - public void FollowWithTakeNotIList() - { - var source = NumberRangeGuaranteedNotCollectionType(5, 4); - var expected = new[] { 6, 7 }; - Assert.Equal(expected, source.Skip(1).Take(2)); + int[] expected = [6, 7]; + foreach (IEnumerable source in CreateSources(Enumerable.Range(5, 4))) + { + Assert.Equal(expected, source.Skip(1).Take(2)); + } } [Fact] public void FollowWithTakeThenMassiveTake() { - var source = new[] { 5, 6, 7, 8 }; - var expected = new[] { 7 }; - Assert.Equal(expected, source.Skip(2).Take(1).Take(int.MaxValue)); - } - [Fact] - public void FollowWithTakeThenMassiveTakeNotIList() - { - var source = NumberRangeGuaranteedNotCollectionType(5, 4); - var expected = new[] { 7 }; - Assert.Equal(expected, source.Skip(2).Take(1).Take(int.MaxValue)); + int[] expected = [7]; + foreach (IEnumerable source in CreateSources([5, 6, 7, 8])) + { + Assert.Equal(expected, source.Skip(2).Take(1).Take(int.MaxValue)); + } } [Fact] public void FollowWithSkip() { - var source = new[] { 1, 2, 3, 4, 5, 6 }; - var expected = new[] { 4, 5, 6 }; - Assert.Equal(expected, source.Skip(1).Skip(2).Skip(-4)); - } - - [Fact] - public void FollowWithSkipNotIList() - { - var source = NumberRangeGuaranteedNotCollectionType(1, 6); - var expected = new[] { 4, 5, 6 }; - Assert.Equal(expected, source.Skip(1).Skip(2).Skip(-4)); + int[] expected = [4, 5, 6]; + foreach (IEnumerable source in CreateSources([1, 2, 3, 4, 5, 6])) + { + Assert.Equal(expected, source.Skip(1).Skip(2).Skip(-4)); + } } [Fact] public void ElementAt() { - var source = new[] { 1, 2, 3, 4, 5, 6 }; - var remaining = source.Skip(2); - Assert.Equal(3, remaining.ElementAt(0)); - Assert.Equal(4, remaining.ElementAt(1)); - Assert.Equal(6, remaining.ElementAt(3)); - AssertExtensions.Throws("index", () => remaining.ElementAt(-1)); - AssertExtensions.Throws("index", () => remaining.ElementAt(4)); - } - - [Fact] - public void ElementAtNotIList() - { - var source = GuaranteeNotIList(new[] { 1, 2, 3, 4, 5, 6 }); - var remaining = source.Skip(2); - Assert.Equal(3, remaining.ElementAt(0)); - Assert.Equal(4, remaining.ElementAt(1)); - Assert.Equal(6, remaining.ElementAt(3)); - AssertExtensions.Throws("index", () => remaining.ElementAt(-1)); - AssertExtensions.Throws("index", () => remaining.ElementAt(4)); + foreach (IEnumerable source in CreateSources([1, 2, 3, 4, 5, 6])) + { + IEnumerable remaining = source.Skip(2); + Assert.Equal(3, remaining.ElementAt(0)); + Assert.Equal(4, remaining.ElementAt(1)); + Assert.Equal(6, remaining.ElementAt(3)); + AssertExtensions.Throws("index", () => remaining.ElementAt(-1)); + AssertExtensions.Throws("index", () => remaining.ElementAt(4)); + } } [Fact] public void ElementAtOrDefault() { - var source = new[] { 1, 2, 3, 4, 5, 6 }; - var remaining = source.Skip(2); - Assert.Equal(3, remaining.ElementAtOrDefault(0)); - Assert.Equal(4, remaining.ElementAtOrDefault(1)); - Assert.Equal(6, remaining.ElementAtOrDefault(3)); - Assert.Equal(0, remaining.ElementAtOrDefault(-1)); - Assert.Equal(0, remaining.ElementAtOrDefault(4)); - } - - [Fact] - public void ElementAtOrDefaultNotIList() - { - var source = GuaranteeNotIList(new[] { 1, 2, 3, 4, 5, 6 }); - var remaining = source.Skip(2); - Assert.Equal(3, remaining.ElementAtOrDefault(0)); - Assert.Equal(4, remaining.ElementAtOrDefault(1)); - Assert.Equal(6, remaining.ElementAtOrDefault(3)); - Assert.Equal(0, remaining.ElementAtOrDefault(-1)); - Assert.Equal(0, remaining.ElementAtOrDefault(4)); + foreach (IEnumerable source in CreateSources([1, 2, 3, 4, 5, 6])) + { + IEnumerable remaining = source.Skip(2); + Assert.Equal(3, remaining.ElementAtOrDefault(0)); + Assert.Equal(4, remaining.ElementAtOrDefault(1)); + Assert.Equal(6, remaining.ElementAtOrDefault(3)); + Assert.Equal(0, remaining.ElementAtOrDefault(-1)); + Assert.Equal(0, remaining.ElementAtOrDefault(4)); + } } [Fact] public void First() { - var source = new[] { 1, 2, 3, 4, 5 }; - Assert.Equal(1, source.Skip(0).First()); - Assert.Equal(3, source.Skip(2).First()); - Assert.Equal(5, source.Skip(4).First()); - Assert.Throws(() => source.Skip(5).First()); - } - - [Fact] - public void FirstNotIList() - { - var source = GuaranteeNotIList(new[] { 1, 2, 3, 4, 5 }); - Assert.Equal(1, source.Skip(0).First()); - Assert.Equal(3, source.Skip(2).First()); - Assert.Equal(5, source.Skip(4).First()); - Assert.Throws(() => source.Skip(5).First()); + foreach (IEnumerable source in CreateSources([1, 2, 3, 4, 5])) + { + Assert.Equal(1, source.Skip(0).First()); + Assert.Equal(3, source.Skip(2).First()); + Assert.Equal(5, source.Skip(4).First()); + Assert.Throws(() => source.Skip(5).First()); + } } [Fact] public void FirstOrDefault() { - var source = new[] { 1, 2, 3, 4, 5 }; - Assert.Equal(1, source.Skip(0).FirstOrDefault()); - Assert.Equal(3, source.Skip(2).FirstOrDefault()); - Assert.Equal(5, source.Skip(4).FirstOrDefault()); - Assert.Equal(0, source.Skip(5).FirstOrDefault()); - } - - [Fact] - public void FirstOrDefaultNotIList() - { - var source = GuaranteeNotIList(new[] { 1, 2, 3, 4, 5 }); - Assert.Equal(1, source.Skip(0).FirstOrDefault()); - Assert.Equal(3, source.Skip(2).FirstOrDefault()); - Assert.Equal(5, source.Skip(4).FirstOrDefault()); - Assert.Equal(0, source.Skip(5).FirstOrDefault()); + foreach (IEnumerable source in CreateSources([1, 2, 3, 4, 5])) + { + Assert.Equal(1, source.Skip(0).FirstOrDefault()); + Assert.Equal(3, source.Skip(2).FirstOrDefault()); + Assert.Equal(5, source.Skip(4).FirstOrDefault()); + Assert.Equal(0, source.Skip(5).FirstOrDefault()); + } } [Fact] public void Last() { - var source = new[] { 1, 2, 3, 4, 5 }; - Assert.Equal(5, source.Skip(0).Last()); - Assert.Equal(5, source.Skip(1).Last()); - Assert.Equal(5, source.Skip(4).Last()); - Assert.Throws(() => source.Skip(5).Last()); - } - - [Fact] - public void LastNotList() - { - var source = GuaranteeNotIList(new[] { 1, 2, 3, 4, 5 }); - Assert.Equal(5, source.Skip(0).Last()); - Assert.Equal(5, source.Skip(1).Last()); - Assert.Equal(5, source.Skip(4).Last()); - Assert.Throws(() => source.Skip(5).Last()); + foreach (IEnumerable source in CreateSources([1, 2, 3, 4, 5])) + { + Assert.Equal(5, source.Skip(0).Last()); + Assert.Equal(5, source.Skip(1).Last()); + Assert.Equal(5, source.Skip(4).Last()); + Assert.Throws(() => source.Skip(5).Last()); + } } [Fact] public void LastOrDefault() { - var source = new[] { 1, 2, 3, 4, 5 }; - Assert.Equal(5, source.Skip(0).LastOrDefault()); - Assert.Equal(5, source.Skip(1).LastOrDefault()); - Assert.Equal(5, source.Skip(4).LastOrDefault()); - Assert.Equal(0, source.Skip(5).LastOrDefault()); - } - - [Fact] - public void LastOrDefaultNotList() - { - var source = GuaranteeNotIList(new[] { 1, 2, 3, 4, 5 }); - Assert.Equal(5, source.Skip(0).LastOrDefault()); - Assert.Equal(5, source.Skip(1).LastOrDefault()); - Assert.Equal(5, source.Skip(4).LastOrDefault()); - Assert.Equal(0, source.Skip(5).LastOrDefault()); + foreach (IEnumerable source in CreateSources([1, 2, 3, 4, 5])) + { + Assert.Equal(5, source.Skip(0).LastOrDefault()); + Assert.Equal(5, source.Skip(1).LastOrDefault()); + Assert.Equal(5, source.Skip(4).LastOrDefault()); + Assert.Equal(0, source.Skip(5).LastOrDefault()); + } } [Fact] public void ToArray() { - var source = new[] { 1, 2, 3, 4, 5 }; - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Skip(0).ToArray()); - Assert.Equal(new[] { 2, 3, 4, 5 }, source.Skip(1).ToArray()); - Assert.Equal(5, source.Skip(4).ToArray().Single()); - Assert.Empty(source.Skip(5).ToArray()); - Assert.Empty(source.Skip(40).ToArray()); - } - - [Fact] - public void ToArrayNotList() - { - var source = GuaranteeNotIList(new[] { 1, 2, 3, 4, 5 }); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Skip(0).ToArray()); - Assert.Equal(new[] { 2, 3, 4, 5 }, source.Skip(1).ToArray()); - Assert.Equal(5, source.Skip(4).ToArray().Single()); - Assert.Empty(source.Skip(5).ToArray()); - Assert.Empty(source.Skip(40).ToArray()); + foreach (IEnumerable source in CreateSources([1, 2, 3, 4, 5])) + { + Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Skip(0).ToArray()); + Assert.Equal(new[] { 2, 3, 4, 5 }, source.Skip(1).ToArray()); + Assert.Equal(5, source.Skip(4).ToArray().Single()); + Assert.Empty(source.Skip(5).ToArray()); + Assert.Empty(source.Skip(40).ToArray()); + } } [Fact] public void ToList() { - var source = new[] { 1, 2, 3, 4, 5 }; - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Skip(0).ToList()); - Assert.Equal(new[] { 2, 3, 4, 5 }, source.Skip(1).ToList()); - Assert.Equal(5, source.Skip(4).ToList().Single()); - Assert.Empty(source.Skip(5).ToList()); - Assert.Empty(source.Skip(40).ToList()); - } - - [Fact] - public void ToListNotList() - { - var source = GuaranteeNotIList(new[] { 1, 2, 3, 4, 5 }); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Skip(0).ToList()); - Assert.Equal(new[] { 2, 3, 4, 5 }, source.Skip(1).ToList()); - Assert.Equal(5, source.Skip(4).ToList().Single()); - Assert.Empty(source.Skip(5).ToList()); - Assert.Empty(source.Skip(40).ToList()); + foreach (IEnumerable source in CreateSources([1, 2, 3, 4, 5])) + { + Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Skip(0).ToList()); + Assert.Equal(new[] { 2, 3, 4, 5 }, source.Skip(1).ToList()); + Assert.Equal(5, source.Skip(4).ToList().Single()); + Assert.Empty(source.Skip(5).ToList()); + Assert.Empty(source.Skip(40).ToList()); + } } [Fact] public void RepeatEnumerating() { - var source = new[] { 1, 2, 3, 4, 5 }; - var remaining = source.Skip(1); - Assert.Equal(remaining, remaining); - } - - [Fact] - public void RepeatEnumeratingNotList() - { - var source = GuaranteeNotIList(new[] { 1, 2, 3, 4, 5 }); - var remaining = source.Skip(1); - Assert.Equal(remaining, remaining); + foreach (IEnumerable source in CreateSources([1, 2, 3, 4, 5])) + { + IEnumerable remaining = source.Skip(1); + Assert.Equal(remaining, remaining); + } } [Fact] public void LazySkipMoreThan32Bits() { - var range = NumberRangeGuaranteedNotCollectionType(1, 100); - var skipped = range.Skip(50).Skip(int.MaxValue); // Could cause an integer overflow. + IEnumerable range = NumberRangeGuaranteedNotCollectionType(1, 100); + IEnumerable skipped = range.Skip(50).Skip(int.MaxValue); // Could cause an integer overflow. Assert.Empty(skipped); Assert.Empty(skipped); Assert.Empty(skipped.ToArray()); @@ -488,7 +345,7 @@ public void IteratorStateShouldNotChangeIfNumberOfElementsIsUnbounded() // so that it does not overflow to a negative number and enumeration does not // stop prematurely. - var iterator = new FastInfiniteEnumerator().Skip(1).GetEnumerator(); + using IEnumerator iterator = new FastInfiniteEnumerator().Skip(1).GetEnumerator(); iterator.MoveNext(); // Make sure the underlying enumerator has been initialized. FieldInfo state = iterator.GetType().GetTypeInfo() @@ -523,7 +380,7 @@ public void DisposeSource(int sourceCount, int count) current: () => 0, dispose: () => state = -1); - IEnumerator iterator = source.Skip(count).GetEnumerator(); + using IEnumerator iterator = source.Skip(count).GetEnumerator(); int iteratorCount = Math.Max(0, sourceCount - Math.Max(0, count)); Assert.All(Enumerable.Range(0, iteratorCount), _ => Assert.True(iterator.MoveNext())); diff --git a/tests/System.Linq.Tests/Tests/System.Linq/SkipWhileTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/SkipWhileTests.cs index 33a05610..276c0328 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/SkipWhileTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/SkipWhileTests.cs @@ -11,15 +11,15 @@ public class SkipWhileTests : EnumerableTests [Fact] public void Empty() { - Assert.Equal(Enumerable.Empty(), Enumerable.Empty().SkipWhile(i => i < 40)); - Assert.Equal(Enumerable.Empty(), Enumerable.Empty().SkipWhile((i, index) => i < 40)); + Assert.Equal([], Enumerable.Empty().SkipWhile(i => i < 40)); + Assert.Equal([], Enumerable.Empty().SkipWhile((i, index) => i < 40)); } [Fact] public void SkipWhileAllTrue() { - Assert.Equal(Enumerable.Empty(), Enumerable.Range(0, 20).SkipWhile(i => i < 40)); - Assert.Equal(Enumerable.Empty(), Enumerable.Range(0, 20).SkipWhile((i, idx) => i == idx)); + Assert.Equal([], Enumerable.Range(0, 20).SkipWhile(i => i < 40)); + Assert.Equal([], Enumerable.Range(0, 20).SkipWhile((i, idx) => i == idx)); } [Fact] @@ -42,10 +42,8 @@ public void SkipWhileThrowsOnNull() public void SkipWhilePassesPredicateExceptionWhenEnumerated() { var source = Enumerable.Range(-2, 5).SkipWhile(i => 1 / i <= 0); - using (var en = source.GetEnumerator()) - { - Assert.Throws(() => en.MoveNext()); - } + using var en = source.GetEnumerator(); + Assert.Throws(() => en.MoveNext()); } [Fact] @@ -66,10 +64,8 @@ public void RunOnce() public void SkipErrorWhenSourceErrors() { var source = NumberRangeGuaranteedNotCollectionType(-2, 5).Select(i => (decimal)i).Select(m => 1 / m).Skip(4); - using (var en = source.GetEnumerator()) - { - Assert.Throws(() => en.MoveNext()); - } + using var en = source.GetEnumerator(); + Assert.Throws(() => en.MoveNext()); } [Fact] @@ -95,8 +91,8 @@ public void SameResultsRepeatCallsStringQuery() [Fact] public void PredicateManyFalseOnSecond() { - int[] source = { 8, 3, 12, 4, 6, 10 }; - int[] expected = { 3, 12, 4, 6, 10 }; + int[] source = [8, 3, 12, 4, 6, 10]; + int[] expected = [3, 12, 4, 6, 10]; Assert.Equal(expected, source.SkipWhile(e => e % 2 == 0)); } @@ -104,8 +100,8 @@ public void PredicateManyFalseOnSecond() [Fact] public void PredicateManyFalseOnSecondIndex() { - int[] source = { 8, 3, 12, 4, 6, 10 }; - int[] expected = { 3, 12, 4, 6, 10 }; + int[] source = [8, 3, 12, 4, 6, 10]; + int[] expected = [3, 12, 4, 6, 10]; Assert.Equal(expected, source.SkipWhile((e, i) => e % 2 == 0)); } @@ -113,8 +109,8 @@ public void PredicateManyFalseOnSecondIndex() [Fact] public void PredicateTrueOnSecondFalseOnFirstAndOthers() { - int[] source = { 3, 2, 4, 12, 6 }; - int[] expected = { 3, 2, 4, 12, 6 }; + int[] source = [3, 2, 4, 12, 6]; + int[] expected = [3, 2, 4, 12, 6]; Assert.Equal(expected, source.SkipWhile(e => e % 2 == 0)); } @@ -122,8 +118,8 @@ public void PredicateTrueOnSecondFalseOnFirstAndOthers() [Fact] public void PredicateTrueOnSecondFalseOnFirstAndOthersIndex() { - int[] source = { 3, 2, 4, 12, 6 }; - int[] expected = { 3, 2, 4, 12, 6 }; + int[] source = [3, 2, 4, 12, 6]; + int[] expected = [3, 2, 4, 12, 6]; Assert.Equal(expected, source.SkipWhile((e, i) => e % 2 == 0)); } @@ -131,8 +127,8 @@ public void PredicateTrueOnSecondFalseOnFirstAndOthersIndex() [Fact] public void FirstExcludedByIndex() { - int[] source = { 6, 2, 5, 3, 8 }; - int[] expected = { 2, 5, 3, 8 }; + int[] source = [6, 2, 5, 3, 8]; + int[] expected = [2, 5, 3, 8]; Assert.Equal(expected, source.SkipWhile((element, index) => index == 0)); } @@ -140,8 +136,8 @@ public void FirstExcludedByIndex() [Fact] public void AllButLastExcludedByIndex() { - int[] source = { 6, 2, 5, 3, 8 }; - int[] expected = { 8 }; + int[] source = [6, 2, 5, 3, 8]; + int[] expected = [8]; Assert.Equal(expected, source.SkipWhile((element, index) => index < source.Length - 1)); } @@ -151,13 +147,13 @@ public void IndexSkipWhileOverflowBeyondIntMaxValueElements() { var skipped = new FastInfiniteEnumerator().SkipWhile((e, i) => true); - using (var en = skipped.GetEnumerator()) - Assert.Throws(() => + using var en = skipped.GetEnumerator(); + Assert.Throws(() => + { + while (en.MoveNext()) { - while (en.MoveNext()) - { - } - }); + } + }); } [Fact] diff --git a/tests/System.Linq.Tests/Tests/System.Linq/SumTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/SumTests.cs index 12748e92..b94a6873 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/SumTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/SumTests.cs @@ -98,7 +98,7 @@ public void SumOfNullableOfDecimal_SourceIsNull_ArgumentNullExceptionThrown() [Fact] public void SumOfInt_SelectorIsNull_ArgumentNullExceptionThrown() { - IEnumerable sourceInt = Enumerable.Empty(); + IEnumerable sourceInt = []; Func selector = null; AssertExtensions.Throws("selector", () => sourceInt.Sum(selector)); } @@ -107,7 +107,7 @@ public void SumOfInt_SelectorIsNull_ArgumentNullExceptionThrown() public void SumOfNullableOfInt_SelectorIsNull_ArgumentNullExceptionThrown() { - IEnumerable sourceNullableInt = Enumerable.Empty(); + IEnumerable sourceNullableInt = []; Func selector = null; AssertExtensions.Throws("selector", () => sourceNullableInt.Sum(selector)); } @@ -115,7 +115,7 @@ public void SumOfNullableOfInt_SelectorIsNull_ArgumentNullExceptionThrown() [Fact] public void SumOfLong_SelectorIsNull_ArgumentNullExceptionThrown() { - IEnumerable sourceLong = Enumerable.Empty(); + IEnumerable sourceLong = []; Func selector = null; AssertExtensions.Throws("selector", () => sourceLong.Sum(selector)); } @@ -124,7 +124,7 @@ public void SumOfLong_SelectorIsNull_ArgumentNullExceptionThrown() public void SumOfNullableOfLong_SelectorIsNull_ArgumentNullExceptionThrown() { - IEnumerable sourceNullableLong = Enumerable.Empty(); + IEnumerable sourceNullableLong = []; Func selector = null; AssertExtensions.Throws("selector", () => sourceNullableLong.Sum(selector)); } @@ -132,7 +132,7 @@ public void SumOfNullableOfLong_SelectorIsNull_ArgumentNullExceptionThrown() [Fact] public void SumOfFloat_SelectorIsNull_ArgumentNullExceptionThrown() { - IEnumerable sourceFloat = Enumerable.Empty(); + IEnumerable sourceFloat = []; Func selector = null; AssertExtensions.Throws("selector", () => sourceFloat.Sum(selector)); } @@ -141,7 +141,7 @@ public void SumOfFloat_SelectorIsNull_ArgumentNullExceptionThrown() public void SumOfNullableOfFloat_SelectorIsNull_ArgumentNullExceptionThrown() { - IEnumerable sourceNullableFloat = Enumerable.Empty(); + IEnumerable sourceNullableFloat = []; Func selector = null; AssertExtensions.Throws("selector", () => sourceNullableFloat.Sum(selector)); } @@ -149,7 +149,7 @@ public void SumOfNullableOfFloat_SelectorIsNull_ArgumentNullExceptionThrown() [Fact] public void SumOfDouble_SelectorIsNull_ArgumentNullExceptionThrown() { - IEnumerable sourceDouble = Enumerable.Empty(); + IEnumerable sourceDouble = []; Func selector = null; AssertExtensions.Throws("selector", () => sourceDouble.Sum(selector)); } @@ -158,7 +158,7 @@ public void SumOfDouble_SelectorIsNull_ArgumentNullExceptionThrown() public void SumOfNullableOfDouble_SelectorIsNull_ArgumentNullExceptionThrown() { - IEnumerable sourceNullableDouble = Enumerable.Empty(); + IEnumerable sourceNullableDouble = []; Func selector = null; AssertExtensions.Throws("selector", () => sourceNullableDouble.Sum(selector)); } @@ -166,7 +166,7 @@ public void SumOfNullableOfDouble_SelectorIsNull_ArgumentNullExceptionThrown() [Fact] public void SumOfDecimal_SelectorIsNull_ArgumentNullExceptionThrown() { - IEnumerable sourceDecimal = Enumerable.Empty(); + IEnumerable sourceDecimal = []; Func selector = null; AssertExtensions.Throws("selector", () => sourceDecimal.Sum(selector)); } @@ -175,7 +175,7 @@ public void SumOfDecimal_SelectorIsNull_ArgumentNullExceptionThrown() public void SumOfNullableOfDecimal_SelectorIsNull_ArgumentNullExceptionThrown() { - IEnumerable sourceNullableDecimal = Enumerable.Empty(); + IEnumerable sourceNullableDecimal = []; Func selector = null; AssertExtensions.Throws("selector", () => sourceNullableDecimal.Sum(selector)); } @@ -187,7 +187,7 @@ public void SumOfNullableOfDecimal_SelectorIsNull_ArgumentNullExceptionThrown() [Fact] public void SumOfInt_SourceIsEmptyCollection_ZeroReturned() { - IEnumerable sourceInt = Enumerable.Empty(); + IEnumerable sourceInt = []; Assert.Equal(0, sourceInt.Sum()); Assert.Equal(0, sourceInt.Sum(x => x)); } @@ -196,7 +196,7 @@ public void SumOfInt_SourceIsEmptyCollection_ZeroReturned() public void SumOfNullableOfInt_SourceIsEmptyCollection_ZeroReturned() { - IEnumerable sourceNullableInt = Enumerable.Empty(); + IEnumerable sourceNullableInt = []; Assert.Equal(0, sourceNullableInt.Sum()); Assert.Equal(0, sourceNullableInt.Sum(x => x)); } @@ -204,7 +204,7 @@ public void SumOfNullableOfInt_SourceIsEmptyCollection_ZeroReturned() [Fact] public void SumOfLong_SourceIsEmptyCollection_ZeroReturned() { - IEnumerable sourceLong = Enumerable.Empty(); + IEnumerable sourceLong = []; Assert.Equal(0L, sourceLong.Sum()); Assert.Equal(0L, sourceLong.Sum(x => x)); } @@ -213,7 +213,7 @@ public void SumOfLong_SourceIsEmptyCollection_ZeroReturned() public void SumOfNullableOfLong_SourceIsEmptyCollection_ZeroReturned() { - IEnumerable sourceNullableLong = Enumerable.Empty(); + IEnumerable sourceNullableLong = []; Assert.Equal(0L, sourceNullableLong.Sum()); Assert.Equal(0L, sourceNullableLong.Sum(x => x)); } @@ -221,7 +221,7 @@ public void SumOfNullableOfLong_SourceIsEmptyCollection_ZeroReturned() [Fact] public void SumOfFloat_SourceIsEmptyCollection_ZeroReturned() { - IEnumerable sourceFloat = Enumerable.Empty(); + IEnumerable sourceFloat = []; Assert.Equal(0f, sourceFloat.Sum()); Assert.Equal(0f, sourceFloat.Sum(x => x)); } @@ -230,7 +230,7 @@ public void SumOfFloat_SourceIsEmptyCollection_ZeroReturned() public void SumOfNullableOfFloat_SourceIsEmptyCollection_ZeroReturned() { - IEnumerable sourceNullableFloat = Enumerable.Empty(); + IEnumerable sourceNullableFloat = []; Assert.Equal(0f, sourceNullableFloat.Sum()); Assert.Equal(0f, sourceNullableFloat.Sum(x => x)); } @@ -238,7 +238,7 @@ public void SumOfNullableOfFloat_SourceIsEmptyCollection_ZeroReturned() [Fact] public void SumOfDouble_SourceIsEmptyCollection_ZeroReturned() { - IEnumerable sourceDouble = Enumerable.Empty(); + IEnumerable sourceDouble = []; Assert.Equal(0d, sourceDouble.Sum()); Assert.Equal(0d, sourceDouble.Sum(x => x)); } @@ -247,7 +247,7 @@ public void SumOfDouble_SourceIsEmptyCollection_ZeroReturned() public void SumOfNullableOfDouble_SourceIsEmptyCollection_ZeroReturned() { - IEnumerable sourceNullableDouble = Enumerable.Empty(); + IEnumerable sourceNullableDouble = []; Assert.Equal(0d, sourceNullableDouble.Sum()); Assert.Equal(0d, sourceNullableDouble.Sum(x => x)); } @@ -255,7 +255,7 @@ public void SumOfNullableOfDouble_SourceIsEmptyCollection_ZeroReturned() [Fact] public void SumOfDecimal_SourceIsEmptyCollection_ZeroReturned() { - IEnumerable sourceDecimal = Enumerable.Empty(); + IEnumerable sourceDecimal = []; Assert.Equal(0m, sourceDecimal.Sum()); Assert.Equal(0m, sourceDecimal.Sum(x => x)); } @@ -264,7 +264,7 @@ public void SumOfDecimal_SourceIsEmptyCollection_ZeroReturned() public void SumOfNullableOfDecimal_SourceIsEmptyCollection_ZeroReturned() { - IEnumerable sourceNullableDecimal = Enumerable.Empty(); + IEnumerable sourceNullableDecimal = []; Assert.Equal(0m, sourceNullableDecimal.Sum()); Assert.Equal(0m, sourceNullableDecimal.Sum(x => x)); } @@ -276,7 +276,7 @@ public void SumOfNullableOfDecimal_SourceIsEmptyCollection_ZeroReturned() [Fact] public void SumOfInt_SourceIsNotEmpty_ProperSumReturned() { - IEnumerable sourceInt = new int[] { 1, -2, 3, -4 }; + IEnumerable sourceInt = [1, -2, 3, -4]; Assert.Equal(-2, sourceInt.Sum()); Assert.Equal(-2, sourceInt.Sum(x => x)); } @@ -285,7 +285,7 @@ public void SumOfInt_SourceIsNotEmpty_ProperSumReturned() public void SumOfNullableOfInt_SourceIsNotEmpty_ProperSumReturned() { - IEnumerable sourceNullableInt = new int?[] { 1, -2, null, 3, -4, null }; + IEnumerable sourceNullableInt = [1, -2, null, 3, -4, null]; Assert.Equal(-2, sourceNullableInt.Sum()); Assert.Equal(-2, sourceNullableInt.Sum(x => x)); } @@ -293,7 +293,7 @@ public void SumOfNullableOfInt_SourceIsNotEmpty_ProperSumReturned() [Fact] public void SumOfLong_SourceIsNotEmpty_ProperSumReturned() { - IEnumerable sourceLong = new long[] { 1L, -2L, 3L, -4L }; + IEnumerable sourceLong = [1L, -2L, 3L, -4L]; Assert.Equal(-2L, sourceLong.Sum()); Assert.Equal(-2L, sourceLong.Sum(x => x)); } @@ -302,7 +302,7 @@ public void SumOfLong_SourceIsNotEmpty_ProperSumReturned() public void SumOfNullableOfLong_SourceIsNotEmpty_ProperSumReturned() { - IEnumerable sourceNullableLong = new long?[] { 1L, -2L, null, 3L, -4L, null }; + IEnumerable sourceNullableLong = [1L, -2L, null, 3L, -4L, null]; Assert.Equal(-2L, sourceNullableLong.Sum()); Assert.Equal(-2L, sourceNullableLong.Sum(x => x)); } @@ -310,7 +310,7 @@ public void SumOfNullableOfLong_SourceIsNotEmpty_ProperSumReturned() [Fact] public void SumOfFloat_SourceIsNotEmpty_ProperSumReturned() { - IEnumerable sourceFloat = new float[] { 1f, 0.5f, -1f, 0.5f }; + IEnumerable sourceFloat = [1f, 0.5f, -1f, 0.5f]; Assert.Equal(1f, sourceFloat.Sum()); Assert.Equal(1f, sourceFloat.Sum(x => x)); } @@ -319,7 +319,7 @@ public void SumOfFloat_SourceIsNotEmpty_ProperSumReturned() public void SumOfNullableOfFloat_SourceIsNotEmpty_ProperSumReturned() { - IEnumerable sourceNullableFloat = new float?[] { 1f, 0.5f, null, -1f, 0.5f, null }; + IEnumerable sourceNullableFloat = [1f, 0.5f, null, -1f, 0.5f, null]; Assert.Equal(1f, sourceNullableFloat.Sum()); Assert.Equal(1f, sourceNullableFloat.Sum(x => x)); } @@ -327,7 +327,7 @@ public void SumOfNullableOfFloat_SourceIsNotEmpty_ProperSumReturned() [Fact] public void SumOfDouble_SourceIsNotEmpty_ProperSumReturned() { - IEnumerable sourceDouble = new double[] { 1d, 0.5d, -1d, 0.5d }; + IEnumerable sourceDouble = [1d, 0.5d, -1d, 0.5d]; Assert.Equal(1d, sourceDouble.Sum()); Assert.Equal(1d, sourceDouble.Sum(x => x)); } @@ -336,7 +336,7 @@ public void SumOfDouble_SourceIsNotEmpty_ProperSumReturned() public void SumOfNullableOfDouble_SourceIsNotEmpty_ProperSumReturned() { - IEnumerable sourceNullableDouble = new double?[] { 1d, 0.5d, null, -1d, 0.5d, null }; + IEnumerable sourceNullableDouble = [1d, 0.5d, null, -1d, 0.5d, null]; Assert.Equal(1d, sourceNullableDouble.Sum()); Assert.Equal(1d, sourceNullableDouble.Sum(x => x)); } @@ -344,7 +344,7 @@ public void SumOfNullableOfDouble_SourceIsNotEmpty_ProperSumReturned() [Fact] public void SumOfDecimal_SourceIsNotEmpty_ProperSumReturned() { - IEnumerable sourceDecimal = new decimal[] { 1m, 0.5m, -1m, 0.5m }; + IEnumerable sourceDecimal = [1m, 0.5m, -1m, 0.5m]; Assert.Equal(1m, sourceDecimal.Sum()); Assert.Equal(1m, sourceDecimal.Sum(x => x)); } @@ -353,7 +353,7 @@ public void SumOfDecimal_SourceIsNotEmpty_ProperSumReturned() public void SumOfNullableOfDecimal_SourceIsNotEmpty_ProperSumReturned() { - IEnumerable sourceNullableDecimal = new decimal?[] { 1m, 0.5m, null, -1m, 0.5m, null }; + IEnumerable sourceNullableDecimal = [1m, 0.5m, null, -1m, 0.5m, null]; Assert.Equal(1m, sourceNullableDecimal.Sum()); Assert.Equal(1m, sourceNullableDecimal.Sum(x => x)); } @@ -372,7 +372,7 @@ public static IEnumerable SumOverflowsVerticalVectorLanes() { for (int verticalOffset = 1; verticalOffset < 6; verticalOffset++) { - yield return new object[] { element, verticalOffset }; + yield return [element, verticalOffset]; } } } @@ -380,7 +380,7 @@ public static IEnumerable SumOverflowsVerticalVectorLanes() [Fact] public void SumOfInt_SourceSumsToOverflow_OverflowExceptionThrown() { - IEnumerable sourceInt = new int[] { int.MaxValue, 1 }; + IEnumerable sourceInt = [int.MaxValue, 1]; Assert.Throws(() => sourceInt.Sum()); Assert.Throws(() => sourceInt.Sum(x => x)); } @@ -419,7 +419,7 @@ public void SumOfInt_SourceSumsToOverflowVectorVertically_OverflowExceptionThrow [Fact] public void SumOfNullableOfInt_SourceSumsToOverflow_OverflowExceptionThrown() { - IEnumerable sourceNullableInt = new int?[] { int.MaxValue, null, 1 }; + IEnumerable sourceNullableInt = [int.MaxValue, null, 1]; Assert.Throws(() => sourceNullableInt.Sum()); Assert.Throws(() => sourceNullableInt.Sum(x => x)); } @@ -427,7 +427,7 @@ public void SumOfNullableOfInt_SourceSumsToOverflow_OverflowExceptionThrown() [Fact] public void SumOfLong_SourceSumsToOverflow_OverflowExceptionThrown() { - IEnumerable sourceLong = new long[] { long.MaxValue, 1L }; + IEnumerable sourceLong = [long.MaxValue, 1L]; Assert.Throws(() => sourceLong.Sum()); Assert.Throws(() => sourceLong.Sum(x => x)); } @@ -466,7 +466,7 @@ public void SumOfLong_SourceSumsToOverflowVectorVertically_OverflowExceptionThro [Fact] public void SumOfNullableOfLong_SourceSumsToOverflow_OverflowExceptionThrown() { - IEnumerable sourceNullableLong = new long?[] { long.MaxValue, null, 1 }; + IEnumerable sourceNullableLong = [long.MaxValue, null, 1]; Assert.Throws(() => sourceNullableLong.Sum()); Assert.Throws(() => sourceNullableLong.Sum(x => x)); } @@ -474,7 +474,7 @@ public void SumOfNullableOfLong_SourceSumsToOverflow_OverflowExceptionThrown() [Fact] public void SumOfFloat_SourceSumsToOverflow_InfinityReturned() { - IEnumerable sourceFloat = new float[] { float.MaxValue, float.MaxValue }; + IEnumerable sourceFloat = [float.MaxValue, float.MaxValue]; Assert.True(float.IsPositiveInfinity(sourceFloat.Sum())); Assert.True(float.IsPositiveInfinity(sourceFloat.Sum(x => x))); } @@ -482,7 +482,7 @@ public void SumOfFloat_SourceSumsToOverflow_InfinityReturned() [Fact] public void SumOfNullableOfFloat_SourceSumsToOverflow_InfinityReturned() { - IEnumerable sourceNullableFloat = new float?[] { float.MaxValue, null, float.MaxValue }; + IEnumerable sourceNullableFloat = [float.MaxValue, null, float.MaxValue]; Assert.True(float.IsPositiveInfinity(sourceNullableFloat.Sum().Value)); Assert.True(float.IsPositiveInfinity(sourceNullableFloat.Sum(x => x).Value)); } @@ -490,7 +490,7 @@ public void SumOfNullableOfFloat_SourceSumsToOverflow_InfinityReturned() [Fact] public void SumOfDouble_SourceSumsToOverflow_InfinityReturned() { - IEnumerable sourceDouble = new double[] { double.MaxValue, double.MaxValue }; + IEnumerable sourceDouble = [double.MaxValue, double.MaxValue]; Assert.True(double.IsPositiveInfinity(sourceDouble.Sum())); Assert.True(double.IsPositiveInfinity(sourceDouble.Sum(x => x))); } @@ -498,7 +498,7 @@ public void SumOfDouble_SourceSumsToOverflow_InfinityReturned() [Fact] public void SumOfNullableOfDouble_SourceSumsToOverflow_InfinityReturned() { - IEnumerable sourceNullableDouble = new double?[] { double.MaxValue, null, double.MaxValue }; + IEnumerable sourceNullableDouble = [double.MaxValue, null, double.MaxValue]; Assert.True(double.IsPositiveInfinity(sourceNullableDouble.Sum().Value)); Assert.True(double.IsPositiveInfinity(sourceNullableDouble.Sum(x => x).Value)); } @@ -506,7 +506,7 @@ public void SumOfNullableOfDouble_SourceSumsToOverflow_InfinityReturned() [Fact] public void SumOfDecimal_SourceSumsToOverflow_OverflowExceptionThrown() { - IEnumerable sourceDecimal = new decimal[] { decimal.MaxValue, 1m }; + IEnumerable sourceDecimal = [decimal.MaxValue, 1m]; Assert.Throws(() => sourceDecimal.Sum()); Assert.Throws(() => sourceDecimal.Sum(x => x)); } @@ -514,7 +514,7 @@ public void SumOfDecimal_SourceSumsToOverflow_OverflowExceptionThrown() [Fact] public void SumOfNullableOfDecimal_SourceSumsToOverflow_OverflowExceptionThrown() { - IEnumerable sourceNullableDecimal = new decimal?[] { decimal.MaxValue, null, 1m }; + IEnumerable sourceNullableDecimal = [decimal.MaxValue, null, 1m]; Assert.Throws(() => sourceNullableDecimal.Sum()); Assert.Throws(() => sourceNullableDecimal.Sum(x => x)); } @@ -532,14 +532,14 @@ where x > int.MinValue [Fact] public void SolitaryNullableSingle() { - float?[] source = { 20.51f }; + float?[] source = [20.51f]; Assert.Equal(source.FirstOrDefault(), source.Sum()); } [Fact] public void NaNFromSingles() { - float?[] source = { 20.45f, 0f, -10.55f, float.NaN }; + float?[] source = [20.45f, 0f, -10.55f, float.NaN]; Assert.True(float.IsNaN(source.Sum().Value)); } @@ -552,7 +552,7 @@ public void NullableSingleAllNull() [Fact] public void NullableSingleToNegativeInfinity() { - float?[] source = { -float.MaxValue, -float.MaxValue }; + float?[] source = [-float.MaxValue, -float.MaxValue]; Assert.True(float.IsNegativeInfinity(source.Sum().Value)); } @@ -570,14 +570,14 @@ public void NullableSingleFromSelector() [Fact] public void SolitaryInt32() { - int[] source = { 20 }; + int[] source = [20]; Assert.Equal(source.FirstOrDefault(), source.Sum()); } [Fact] public void OverflowInt32Negative() { - int[] source = { -int.MaxValue, 0, -5, -20 }; + int[] source = [-int.MaxValue, 0, -5, -20]; Assert.Throws(() => source.Sum()); } @@ -596,7 +596,7 @@ public void Int32FromSelector() [Fact] public void SolitaryNullableInt32() { - int?[] source = { -9 }; + int?[] source = [-9]; Assert.Equal(source.FirstOrDefault(), source.Sum()); } @@ -609,7 +609,7 @@ public void NullableInt32AllNull() [Fact] public void NullableInt32NegativeOverflow() { - int?[] source = { -int.MaxValue, 0, -5, null, null, -20 }; + int?[] source = [-int.MaxValue, 0, -5, null, null, -20]; Assert.Throws(() => source.Sum()); } @@ -640,14 +640,14 @@ public void RunOnce() [Fact] public void SolitaryInt64() { - long[] source = { int.MaxValue + 20L }; + long[] source = [int.MaxValue + 20L]; Assert.Equal(source.FirstOrDefault(), source.Sum()); } [Fact] public void NullableInt64NegativeOverflow() { - long[] source = { -long.MaxValue, 0, -5, 20, -16 }; + long[] source = [-long.MaxValue, 0, -5, 20, -16]; Assert.Throws(() => source.Sum()); } @@ -666,7 +666,7 @@ public void Int64FromSelector() [Fact] public void SolitaryNullableInt64() { - long?[] source = { -int.MaxValue - 20L }; + long?[] source = [-int.MaxValue - 20L]; Assert.Equal(source.FirstOrDefault(), source.Sum()); } @@ -679,7 +679,7 @@ public void NullableInt64AllNull() [Fact] public void Int64NegativeOverflow() { - long?[] source = { -long.MaxValue, 0, -5, -20, null, null }; + long?[] source = [-long.MaxValue, 0, -5, -20, null, null]; Assert.Throws(() => source.Sum()); } @@ -698,21 +698,21 @@ public void NullableInt64FromSelector() [Fact] public void SolitaryDouble() { - double[] source = { 20.51 }; + double[] source = [20.51]; Assert.Equal(source.FirstOrDefault(), source.Sum()); } [Fact] public void DoubleWithNaN() { - double[] source = { 20.45, 0, -10.55, double.NaN }; + double[] source = [20.45, 0, -10.55, double.NaN]; Assert.True(double.IsNaN(source.Sum())); } [Fact] public void DoubleToNegativeInfinity() { - double[] source = { -double.MaxValue, -double.MaxValue }; + double[] source = [-double.MaxValue, -double.MaxValue]; Assert.True(double.IsNegativeInfinity(source.Sum())); } @@ -732,7 +732,7 @@ public void DoubleFromSelector() [Fact] public void SolitaryNullableDouble() { - double?[] source = { 20.51 }; + double?[] source = [20.51]; Assert.Equal(source.FirstOrDefault(), source.Sum()); } @@ -745,7 +745,7 @@ public void NullableDoubleAllNull() [Fact] public void NullableDoubleToNegativeInfinity() { - double?[] source = { -double.MaxValue, -double.MaxValue }; + double?[] source = [-double.MaxValue, -double.MaxValue]; Assert.True(double.IsNegativeInfinity(source.Sum().Value)); } @@ -764,14 +764,14 @@ public void NullableDoubleFromSelector() [Fact] public void SolitaryDecimal() { - decimal[] source = { 20.51m }; + decimal[] source = [20.51m]; Assert.Equal(source.FirstOrDefault(), source.Sum()); } [Fact] public void DecimalNegativeOverflow() { - decimal[] source = { -decimal.MaxValue, -decimal.MaxValue }; + decimal[] source = [-decimal.MaxValue, -decimal.MaxValue]; Assert.Throws(() => source.Sum()); } @@ -790,7 +790,7 @@ public void DecimalFromSelector() [Fact] public void SolitaryNullableDecimal() { - decimal?[] source = { 20.51m }; + decimal?[] source = [20.51m]; Assert.Equal(source.FirstOrDefault(), source.Sum()); } @@ -803,7 +803,7 @@ public void NullableDecimalAllNull() [Fact] public void NullableDecimalNegativeOverflow() { - decimal?[] source = { -decimal.MaxValue, -decimal.MaxValue }; + decimal?[] source = [-decimal.MaxValue, -decimal.MaxValue]; Assert.Throws(() => source.Sum()); } @@ -822,14 +822,14 @@ public void NullableDecimalFromSelector() [Fact] public void SolitarySingle() { - float[] source = { 20.51f }; + float[] source = [20.51f]; Assert.Equal(source.FirstOrDefault(), source.Sum()); } [Fact] public void SingleToNegativeInfinity() { - float[] source = { -float.MaxValue, -float.MaxValue }; + float[] source = [-float.MaxValue, -float.MaxValue]; Assert.True(float.IsNegativeInfinity(source.Sum())); } diff --git a/tests/System.Linq.Tests/Tests/System.Linq/TakeLastTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/TakeLastTests.cs index bd42997c..8f448303 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/TakeLastTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/TakeLastTests.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using Xunit; -using static System.Linq.SkipTakeData; +using static System.Linq.Tests.SkipTakeData; namespace System.Linq.Tests { @@ -19,25 +19,26 @@ public void SkipLastThrowsOnNull() [MemberData(nameof(EnumerableData), MemberType = typeof(SkipTakeData))] public void TakeLast(IEnumerable source, int count) { - Assert.All(IdentityTransforms(), transform => - { - IEnumerable equivalent = transform(source); + int[] expected = source.Reverse().Take(count).Reverse().ToArray(); - IEnumerable expected = equivalent.Reverse().Take(count).Reverse(); - IEnumerable actual = equivalent.TakeLast(count); + Assert.All(CreateSources(source), source => + { + IEnumerable actual = source.TakeLast(count); Assert.Equal(expected, actual); - Assert.Equal(expected.Count(), actual.Count()); + + Assert.Equal(expected.Length, actual.Count()); Assert.Equal(expected, actual.ToArray()); Assert.Equal(expected, actual.ToList()); - Assert.Equal(expected.FirstOrDefault(), actual.FirstOrDefault()); Assert.Equal(expected.LastOrDefault(), actual.LastOrDefault()); - Assert.All(Enumerable.Range(0, expected.Count()), index => + if (expected.Length > 0) { - Assert.Equal(expected.ElementAt(index), actual.ElementAt(index)); - }); + Assert.Equal(expected[0], actual.ElementAt(0)); + Assert.Equal(expected[^1], actual.ElementAt(expected.Length - 1)); + Assert.Equal(expected[expected.Length / 2], actual.ElementAt(expected.Length / 2)); + } Assert.Equal(0, actual.ElementAtOrDefault(-1)); Assert.Equal(0, actual.ElementAtOrDefault(actual.Count())); @@ -56,7 +57,7 @@ public void EvaluationBehavior(int count) current: () => index, // Yield from 1 up to the limit, inclusive. dispose: () => index ^= int.MinValue); - IEnumerator iterator = source.TakeLast(count).GetEnumerator(); + using IEnumerator iterator = source.TakeLast(count).GetEnumerator(); Assert.Equal(0, index); // Nothing should be done before MoveNext is called. for (int i = 1; i <= count; i++) diff --git a/tests/System.Linq.Tests/Tests/System.Linq/TakeTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/TakeTests.cs index f1008b32..03cd189a 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/TakeTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/TakeTests.cs @@ -72,156 +72,92 @@ public void SameResultsRepeatCallsStringQueryIList() [Fact] public void SourceEmptyCountPositive() { - var source = new int[] { }; - Assert.Empty(source.Take(5)); - - Assert.Empty(source.Take(0..5)); - Assert.Empty(source.Take(^5..5)); - Assert.Empty(source.Take(0..^0)); - Assert.Empty(source.Take(^5..^0)); - } - - [Fact] - public void SourceEmptyCountPositiveNotIList() - { - var source = NumberRangeGuaranteedNotCollectionType(0, 0); - Assert.Empty(source.Take(5)); + foreach (IEnumerable source in CreateSources([])) + { + Assert.Empty(source.Take(5)); - Assert.Empty(source.Take(0..5)); - Assert.Empty(source.Take(^5..5)); - Assert.Empty(source.Take(0..^0)); - Assert.Empty(source.Take(^5..^0)); + Assert.Empty(source.Take(0..5)); + Assert.Empty(source.Take(^5..5)); + Assert.Empty(source.Take(0..^0)); + Assert.Empty(source.Take(^5..^0)); + } } [Fact] public void SourceNonEmptyCountNegative() { - var source = new[] { 2, 5, 9, 1 }; - Assert.Empty(source.Take(-5)); - - Assert.Empty(source.Take(^9..0)); - } - - [Fact] - public void SourceNonEmptyCountNegativeNotIList() - { - var source = ForceNotCollection(new[] { 2, 5, 9, 1 }); - Assert.Empty(source.Take(-5)); - - Assert.Empty(source.Take(^9..0)); + foreach (IEnumerable source in CreateSources([2, 5, 9, 1])) + { + Assert.Empty(source.Take(-5)); + Assert.Empty(source.Take(^9..0)); + } } [Fact] public void SourceNonEmptyCountZero() { - var source = new[] { 2, 5, 9, 1 }; - Assert.Empty(source.Take(0)); - - Assert.Empty(source.Take(0..0)); - Assert.Empty(source.Take(^4..0)); - Assert.Empty(source.Take(0..^4)); - Assert.Empty(source.Take(^4..^4)); - } - - [Fact] - public void SourceNonEmptyCountZeroNotIList() - { - var source = ForceNotCollection(new[] { 2, 5, 9, 1 }); - Assert.Empty(source.Take(0)); + foreach (IEnumerable source in CreateSources([2, 5, 9, 1])) + { + Assert.Empty(source.Take(0)); - Assert.Empty(source.Take(0..0)); - Assert.Empty(source.Take(^4..0)); - Assert.Empty(source.Take(0..^4)); - Assert.Empty(source.Take(^4..^4)); + Assert.Empty(source.Take(0..0)); + Assert.Empty(source.Take(^4..0)); + Assert.Empty(source.Take(0..^4)); + Assert.Empty(source.Take(^4..^4)); + } } [Fact] public void SourceNonEmptyCountOne() { - var source = new[] { 2, 5, 9, 1 }; - int[] expected = { 2 }; - - Assert.Equal(expected, source.Take(1)); + int[] expected = [2]; - Assert.Equal(expected, source.Take(0..1)); - Assert.Equal(expected, source.Take(^4..1)); - Assert.Equal(expected, source.Take(0..^3)); - Assert.Equal(expected, source.Take(^4..^3)); - } - - [Fact] - public void SourceNonEmptyCountOneNotIList() - { - var source = ForceNotCollection(new[] { 2, 5, 9, 1 }); - int[] expected = { 2 }; - - Assert.Equal(expected, source.Take(1)); + foreach (IEnumerable source in CreateSources([2, 5, 9, 1])) + { + Assert.Equal(expected, source.Take(1)); - Assert.Equal(expected, source.Take(0..1)); - Assert.Equal(expected, source.Take(^4..1)); - Assert.Equal(expected, source.Take(0..^3)); - Assert.Equal(expected, source.Take(^4..^3)); + Assert.Equal(expected, source.Take(0..1)); + Assert.Equal(expected, source.Take(^4..1)); + Assert.Equal(expected, source.Take(0..^3)); + Assert.Equal(expected, source.Take(^4..^3)); + } } [Fact] public void SourceNonEmptyTakeAllExactly() { - var source = new[] { 2, 5, 9, 1 }; - - Assert.Equal(source, source.Take(source.Length)); - - Assert.Equal(source, source.Take(0..source.Length)); - Assert.Equal(source, source.Take(^source.Length..source.Length)); - Assert.Equal(source, source.Take(0..^0)); - Assert.Equal(source, source.Take(^source.Length..^0)); - } - - [Fact] - public void SourceNonEmptyTakeAllExactlyNotIList() - { - var source = ForceNotCollection(new[] { 2, 5, 9, 1 }); - - Assert.Equal(source, source.Take(source.Count())); + foreach (IEnumerable source in CreateSources([2, 5, 9, 1])) + { + Assert.Equal(source, source.Take(4)); - Assert.Equal(source, source.Take(0..source.Count())); - Assert.Equal(source, source.Take(^source.Count()..source.Count())); - Assert.Equal(source, source.Take(0..^0)); - Assert.Equal(source, source.Take(^source.Count()..^0)); + Assert.Equal(source, source.Take(0..4)); + Assert.Equal(source, source.Take(^4..4)); + Assert.Equal(source, source.Take(0..^0)); + Assert.Equal(source, source.Take(^4..^0)); + } } [Fact] public void SourceNonEmptyTakeAllButOne() { - var source = new[] { 2, 5, 9, 1 }; - int[] expected = { 2, 5, 9 }; + int[] expected = [2, 5, 9]; - Assert.Equal(expected, source.Take(3)); + foreach (IEnumerable source in CreateSources([2, 5, 9, 1])) + { + Assert.Equal(expected, source.Take(3)); - Assert.Equal(expected, source.Take(0..3)); - Assert.Equal(expected, source.Take(^4..3)); - Assert.Equal(expected, source.Take(0..^1)); - Assert.Equal(expected, source.Take(^4..^1)); + Assert.Equal(expected, source.Take(0..3)); + Assert.Equal(expected, source.Take(^4..3)); + Assert.Equal(expected, source.Take(0..^1)); + Assert.Equal(expected, source.Take(^4..^1)); + } } [Fact] public void RunOnce() { var source = new[] { 2, 5, 9, 1 }; - int[] expected = { 2, 5, 9 }; - - Assert.Equal(expected, source.RunOnce().Take(3)); - - Assert.Equal(expected, source.RunOnce().Take(0..3)); - Assert.Equal(expected, source.RunOnce().Take(^4..3)); - Assert.Equal(expected, source.RunOnce().Take(0..^1)); - Assert.Equal(expected, source.RunOnce().Take(^4..^1)); - } - - [Fact] - public void SourceNonEmptyTakeAllButOneNotIList() - { - var source = ForceNotCollection(new[] { 2, 5, 9, 1 }); - int[] expected = { 2, 5, 9 }; + int[] expected = [2, 5, 9]; Assert.Equal(expected, source.RunOnce().Take(3)); @@ -234,23 +170,13 @@ public void SourceNonEmptyTakeAllButOneNotIList() [Fact] public void SourceNonEmptyTakeExcessive() { - var source = new int?[] { 2, 5, null, 9, 1 }; - - Assert.Equal(source, source.Take(source.Length + 1)); - - Assert.Equal(source, source.Take(0..(source.Length + 1))); - Assert.Equal(source, source.Take(^(source.Length + 1)..(source.Length + 1))); - } - - [Fact] - public void SourceNonEmptyTakeExcessiveNotIList() - { - var source = ForceNotCollection(new int?[] { 2, 5, null, 9, 1 }); - - Assert.Equal(source, source.Take(source.Count() + 1)); + foreach (IEnumerable source in CreateSources([2, 5, null, 9, 1])) + { + Assert.Equal(source, source.Take(5)); - Assert.Equal(source, source.Take(0..(source.Count() + 1))); - Assert.Equal(source, source.Take(^(source.Count() + 1)..(source.Count() + 1))); + Assert.Equal(source, source.Take(0..5)); + Assert.Equal(source, source.Take(^6..6)); + } } [Fact] @@ -342,744 +268,408 @@ public void ForcedToEnumeratorDoesntEnumerateIList() [Fact] public void FollowWithTake() { - var source = new[] { 5, 6, 7, 8 }; var expected = new[] { 5, 6 }; - Assert.Equal(expected, source.Take(5).Take(3).Take(2).Take(40)); - Assert.Equal(expected, source.Take(0..5).Take(0..3).Take(0..2).Take(0..40)); - Assert.Equal(expected, source.Take(^4..5).Take(^4..3).Take(^3..2).Take(^2..40)); - Assert.Equal(expected, source.Take(0..^0).Take(0..^1).Take(0..^1).Take(0..^0)); - Assert.Equal(expected, source.Take(^4..^0).Take(^4..^1).Take(^3..^1).Take(^2..^0)); - } - - [Fact] - public void FollowWithTakeNotIList() - { - var source = NumberRangeGuaranteedNotCollectionType(5, 4); - var expected = new[] { 5, 6 }; - Assert.Equal(expected, source.Take(5).Take(3).Take(2)); + foreach (IEnumerable source in CreateSources([5, 6, 7, 8])) + { + Assert.Equal(expected, source.Take(5).Take(3).Take(2).Take(40)); - Assert.Equal(expected, source.Take(0..5).Take(0..3).Take(0..2)); - Assert.Equal(expected, source.Take(^4..5).Take(^4..3).Take(^3..2)); - Assert.Equal(expected, source.Take(0..^0).Take(0..^1).Take(0..^1)); - Assert.Equal(expected, source.Take(^4..^0).Take(^4..^1).Take(^3..^1)); + Assert.Equal(expected, source.Take(0..5).Take(0..3).Take(0..2).Take(0..40)); + Assert.Equal(expected, source.Take(^4..5).Take(^4..3).Take(^3..2).Take(^2..40)); + Assert.Equal(expected, source.Take(0..^0).Take(0..^1).Take(0..^1).Take(0..^0)); + Assert.Equal(expected, source.Take(^4..^0).Take(^4..^1).Take(^3..^1).Take(^2..^0)); + } } [Fact] public void FollowWithSkip() { - var source = new[] { 1, 2, 3, 4, 5, 6 }; var expected = new[] { 3, 4, 5 }; - Assert.Equal(expected, source.Take(5).Skip(2).Skip(-4)); - - Assert.Equal(expected, source.Take(0..5).Skip(2).Skip(-4)); - Assert.Equal(expected, source.Take(^6..5).Skip(2).Skip(-4)); - Assert.Equal(expected, source.Take(0..^1).Skip(2).Skip(-4)); - Assert.Equal(expected, source.Take(^6..^1).Skip(2).Skip(-4)); - } - [Fact] - public void FollowWithSkipNotIList() - { - var source = NumberRangeGuaranteedNotCollectionType(1, 6); - var expected = new[] { 3, 4, 5 }; - Assert.Equal(expected, source.Take(5).Skip(2).Skip(-4)); + foreach (IEnumerable source in CreateSources([1, 2, 3, 4, 5, 6])) + { + Assert.Equal(expected, source.Take(5).Skip(2).Skip(-4)); - Assert.Equal(expected, source.Take(0..5).Skip(2).Skip(-4)); - Assert.Equal(expected, source.Take(^6..5).Skip(2).Skip(-4)); - Assert.Equal(expected, source.Take(0..^1).Skip(2).Skip(-4)); - Assert.Equal(expected, source.Take(^6..^1).Skip(2).Skip(-4)); + Assert.Equal(expected, source.Take(0..5).Skip(2).Skip(-4)); + Assert.Equal(expected, source.Take(^6..5).Skip(2).Skip(-4)); + Assert.Equal(expected, source.Take(0..^1).Skip(2).Skip(-4)); + Assert.Equal(expected, source.Take(^6..^1).Skip(2).Skip(-4)); + } } [Fact] public void ElementAt() { - var source = new[] { 1, 2, 3, 4, 5, 6 }; - var taken0 = source.Take(3); - Assert.Equal(1, taken0.ElementAt(0)); - Assert.Equal(3, taken0.ElementAt(2)); - Assert.Throws("index", () => taken0.ElementAt(-1)); - Assert.Throws("index", () => taken0.ElementAt(3)); - - var taken1 = source.Take(0..3); - Assert.Equal(1, taken1.ElementAt(0)); - Assert.Equal(3, taken1.ElementAt(2)); - Assert.Throws("index", () => taken1.ElementAt(-1)); - Assert.Throws("index", () => taken1.ElementAt(3)); - - var taken2 = source.Take(^6..3); - Assert.Equal(1, taken2.ElementAt(0)); - Assert.Equal(3, taken2.ElementAt(2)); - Assert.Throws("index", () => taken2.ElementAt(-1)); - Assert.Throws("index", () => taken2.ElementAt(3)); - - var taken3 = source.Take(0..^3); - Assert.Equal(1, taken3.ElementAt(0)); - Assert.Equal(3, taken3.ElementAt(2)); - Assert.Throws("index", () => taken3.ElementAt(-1)); - Assert.Throws("index", () => taken3.ElementAt(3)); - - var taken4 = source.Take(^6..^3); - Assert.Equal(1, taken4.ElementAt(0)); - Assert.Equal(3, taken4.ElementAt(2)); - Assert.Throws("index", () => taken4.ElementAt(-1)); - Assert.Throws("index", () => taken4.ElementAt(3)); - } - - [Fact] - public void ElementAtNotIList() - { - var source = ForceNotCollection(new[] { 1, 2, 3, 4, 5, 6 }); - var taken0 = source.Take(3); - Assert.Equal(1, taken0.ElementAt(0)); - Assert.Equal(3, taken0.ElementAt(2)); - Assert.Throws("index", () => taken0.ElementAt(-1)); - Assert.Throws("index", () => taken0.ElementAt(3)); - - var taken1 = source.Take(0..3); - Assert.Equal(1, taken1.ElementAt(0)); - Assert.Equal(3, taken1.ElementAt(2)); - Assert.Throws("index", () => taken1.ElementAt(-1)); - Assert.Throws("index", () => taken1.ElementAt(3)); - - var taken2 = source.Take(^6..3); - Assert.Equal(1, taken2.ElementAt(0)); - Assert.Equal(3, taken2.ElementAt(2)); - Assert.Throws("index", () => taken2.ElementAt(-1)); - Assert.Throws("index", () => taken2.ElementAt(3)); - - var taken3 = source.Take(0..^3); - Assert.Equal(1, taken3.ElementAt(0)); - Assert.Equal(3, taken3.ElementAt(2)); - Assert.Throws("index", () => taken3.ElementAt(-1)); - Assert.Throws("index", () => taken3.ElementAt(3)); - - var taken4 = source.Take(^6..^3); - Assert.Equal(1, taken4.ElementAt(0)); - Assert.Equal(3, taken4.ElementAt(2)); - Assert.Throws("index", () => taken4.ElementAt(-1)); - Assert.Throws("index", () => taken4.ElementAt(3)); + foreach (IEnumerable source in CreateSources([1, 2, 3, 4, 5, 6])) + { + var taken0 = source.Take(3); + Assert.Equal(1, taken0.ElementAt(0)); + Assert.Equal(3, taken0.ElementAt(2)); + Assert.Throws("index", () => taken0.ElementAt(-1)); + Assert.Throws("index", () => taken0.ElementAt(3)); + + var taken1 = source.Take(0..3); + Assert.Equal(1, taken1.ElementAt(0)); + Assert.Equal(3, taken1.ElementAt(2)); + Assert.Throws("index", () => taken1.ElementAt(-1)); + Assert.Throws("index", () => taken1.ElementAt(3)); + + var taken2 = source.Take(^6..3); + Assert.Equal(1, taken2.ElementAt(0)); + Assert.Equal(3, taken2.ElementAt(2)); + Assert.Throws("index", () => taken2.ElementAt(-1)); + Assert.Throws("index", () => taken2.ElementAt(3)); + + var taken3 = source.Take(0..^3); + Assert.Equal(1, taken3.ElementAt(0)); + Assert.Equal(3, taken3.ElementAt(2)); + Assert.Throws("index", () => taken3.ElementAt(-1)); + Assert.Throws("index", () => taken3.ElementAt(3)); + + var taken4 = source.Take(^6..^3); + Assert.Equal(1, taken4.ElementAt(0)); + Assert.Equal(3, taken4.ElementAt(2)); + Assert.Throws("index", () => taken4.ElementAt(-1)); + Assert.Throws("index", () => taken4.ElementAt(3)); + } } [Fact] public void ElementAtOrDefault() { - var source = new[] { 1, 2, 3, 4, 5, 6 }; - var taken0 = source.Take(3); - Assert.Equal(1, taken0.ElementAtOrDefault(0)); - Assert.Equal(3, taken0.ElementAtOrDefault(2)); - Assert.Equal(0, taken0.ElementAtOrDefault(-1)); - Assert.Equal(0, taken0.ElementAtOrDefault(3)); - - var taken1 = source.Take(0..3); - Assert.Equal(1, taken1.ElementAtOrDefault(0)); - Assert.Equal(3, taken1.ElementAtOrDefault(2)); - Assert.Equal(0, taken1.ElementAtOrDefault(-1)); - Assert.Equal(0, taken1.ElementAtOrDefault(3)); - - var taken2 = source.Take(^6..3); - Assert.Equal(1, taken2.ElementAtOrDefault(0)); - Assert.Equal(3, taken2.ElementAtOrDefault(2)); - Assert.Equal(0, taken2.ElementAtOrDefault(-1)); - Assert.Equal(0, taken2.ElementAtOrDefault(3)); - - var taken3 = source.Take(0..^3); - Assert.Equal(1, taken3.ElementAtOrDefault(0)); - Assert.Equal(3, taken3.ElementAtOrDefault(2)); - Assert.Equal(0, taken3.ElementAtOrDefault(-1)); - Assert.Equal(0, taken3.ElementAtOrDefault(3)); - - var taken4 = source.Take(^6..^3); - Assert.Equal(1, taken4.ElementAtOrDefault(0)); - Assert.Equal(3, taken4.ElementAtOrDefault(2)); - Assert.Equal(0, taken4.ElementAtOrDefault(-1)); - Assert.Equal(0, taken4.ElementAtOrDefault(3)); - } - - [Fact] - public void ElementAtOrDefaultNotIList() - { - var source = ForceNotCollection(new[] { 1, 2, 3, 4, 5, 6 }); - var taken0 = source.Take(3); - Assert.Equal(1, taken0.ElementAtOrDefault(0)); - Assert.Equal(3, taken0.ElementAtOrDefault(2)); - Assert.Equal(0, taken0.ElementAtOrDefault(-1)); - Assert.Equal(0, taken0.ElementAtOrDefault(3)); - - var taken1 = source.Take(0..3); - Assert.Equal(1, taken1.ElementAtOrDefault(0)); - Assert.Equal(3, taken1.ElementAtOrDefault(2)); - Assert.Equal(0, taken1.ElementAtOrDefault(-1)); - Assert.Equal(0, taken1.ElementAtOrDefault(3)); - - var taken2 = source.Take(^6..3); - Assert.Equal(1, taken2.ElementAtOrDefault(0)); - Assert.Equal(3, taken2.ElementAtOrDefault(2)); - Assert.Equal(0, taken2.ElementAtOrDefault(-1)); - Assert.Equal(0, taken2.ElementAtOrDefault(3)); - - var taken3 = source.Take(0..^3); - Assert.Equal(1, taken3.ElementAtOrDefault(0)); - Assert.Equal(3, taken3.ElementAtOrDefault(2)); - Assert.Equal(0, taken3.ElementAtOrDefault(-1)); - Assert.Equal(0, taken3.ElementAtOrDefault(3)); - - var taken4 = source.Take(^6..^3); - Assert.Equal(1, taken4.ElementAtOrDefault(0)); - Assert.Equal(3, taken4.ElementAtOrDefault(2)); - Assert.Equal(0, taken4.ElementAtOrDefault(-1)); - Assert.Equal(0, taken4.ElementAtOrDefault(3)); + foreach (IEnumerable source in CreateSources([1, 2, 3, 4, 5, 6])) + { + var taken0 = source.Take(3); + Assert.Equal(1, taken0.ElementAtOrDefault(0)); + Assert.Equal(3, taken0.ElementAtOrDefault(2)); + Assert.Equal(0, taken0.ElementAtOrDefault(-1)); + Assert.Equal(0, taken0.ElementAtOrDefault(3)); + + var taken1 = source.Take(0..3); + Assert.Equal(1, taken1.ElementAtOrDefault(0)); + Assert.Equal(3, taken1.ElementAtOrDefault(2)); + Assert.Equal(0, taken1.ElementAtOrDefault(-1)); + Assert.Equal(0, taken1.ElementAtOrDefault(3)); + + var taken2 = source.Take(^6..3); + Assert.Equal(1, taken2.ElementAtOrDefault(0)); + Assert.Equal(3, taken2.ElementAtOrDefault(2)); + Assert.Equal(0, taken2.ElementAtOrDefault(-1)); + Assert.Equal(0, taken2.ElementAtOrDefault(3)); + + var taken3 = source.Take(0..^3); + Assert.Equal(1, taken3.ElementAtOrDefault(0)); + Assert.Equal(3, taken3.ElementAtOrDefault(2)); + Assert.Equal(0, taken3.ElementAtOrDefault(-1)); + Assert.Equal(0, taken3.ElementAtOrDefault(3)); + + var taken4 = source.Take(^6..^3); + Assert.Equal(1, taken4.ElementAtOrDefault(0)); + Assert.Equal(3, taken4.ElementAtOrDefault(2)); + Assert.Equal(0, taken4.ElementAtOrDefault(-1)); + Assert.Equal(0, taken4.ElementAtOrDefault(3)); + } } [Fact] public void First() { - var source = new[] { 1, 2, 3, 4, 5 }; - Assert.Equal(1, source.Take(1).First()); - Assert.Equal(1, source.Take(4).First()); - Assert.Equal(1, source.Take(40).First()); - Assert.Throws(() => source.Take(0).First()); - Assert.Throws(() => source.Skip(5).Take(10).First()); - - Assert.Equal(1, source.Take(0..1).First()); - Assert.Equal(1, source.Take(0..4).First()); - Assert.Equal(1, source.Take(0..40).First()); - Assert.Throws(() => source.Take(0..0).First()); - Assert.Throws(() => source.Skip(5).Take(0..10).First()); - - Assert.Equal(1, source.Take(^5..1).First()); - Assert.Equal(1, source.Take(^5..4).First()); - Assert.Equal(1, source.Take(^5..40).First()); - Assert.Throws(() => source.Take(^5..0).First()); - Assert.Throws(() => source.Skip(5).Take(^5..10).First()); - - Assert.Equal(1, source.Take(0..^4).First()); - Assert.Equal(1, source.Take(0..^1).First()); - Assert.Equal(1, source.Take(0..^0).First()); - Assert.Throws(() => source.Take(0..^5).First()); - Assert.Throws(() => source.Skip(5).Take(0..^5).First()); - - Assert.Equal(1, source.Take(^5..^4).First()); - Assert.Equal(1, source.Take(^5..^1).First()); - Assert.Equal(1, source.Take(^5..^0).First()); - Assert.Throws(() => source.Take(^5..^5).First()); - Assert.Throws(() => source.Skip(5).Take(^10..^0).First()); - } - - [Fact] - public void FirstNotIList() - { - var source = ForceNotCollection(new[] { 1, 2, 3, 4, 5 }); - Assert.Equal(1, source.Take(1).First()); - Assert.Equal(1, source.Take(4).First()); - Assert.Equal(1, source.Take(40).First()); - Assert.Throws(() => source.Take(0).First()); - Assert.Throws(() => source.Skip(5).Take(10).First()); - - Assert.Equal(1, source.Take(0..1).First()); - Assert.Equal(1, source.Take(0..4).First()); - Assert.Equal(1, source.Take(0..40).First()); - Assert.Throws(() => source.Take(0..0).First()); - Assert.Throws(() => source.Skip(5).Take(0..10).First()); - - Assert.Equal(1, source.Take(^5..1).First()); - Assert.Equal(1, source.Take(^5..4).First()); - Assert.Equal(1, source.Take(^5..40).First()); - Assert.Throws(() => source.Take(^5..0).First()); - Assert.Throws(() => source.Skip(5).Take(^5..10).First()); - - Assert.Equal(1, source.Take(0..^4).First()); - Assert.Equal(1, source.Take(0..^1).First()); - Assert.Equal(1, source.Take(0..^0).First()); - Assert.Throws(() => source.Take(0..^5).First()); - Assert.Throws(() => source.Skip(5).Take(0..^5).First()); - - Assert.Equal(1, source.Take(^5..^4).First()); - Assert.Equal(1, source.Take(^5..^1).First()); - Assert.Equal(1, source.Take(^5..^0).First()); - Assert.Throws(() => source.Take(^5..^5).First()); - Assert.Throws(() => source.Skip(5).Take(^10..^0).First()); + foreach (IEnumerable source in CreateSources([1, 2, 3, 4, 5])) + { + Assert.Equal(1, source.Take(1).First()); + Assert.Equal(1, source.Take(4).First()); + Assert.Equal(1, source.Take(40).First()); + Assert.Throws(() => source.Take(0).First()); + Assert.Throws(() => source.Skip(5).Take(10).First()); + + Assert.Equal(1, source.Take(0..1).First()); + Assert.Equal(1, source.Take(0..4).First()); + Assert.Equal(1, source.Take(0..40).First()); + Assert.Throws(() => source.Take(0..0).First()); + Assert.Throws(() => source.Skip(5).Take(0..10).First()); + + Assert.Equal(1, source.Take(^5..1).First()); + Assert.Equal(1, source.Take(^5..4).First()); + Assert.Equal(1, source.Take(^5..40).First()); + Assert.Throws(() => source.Take(^5..0).First()); + Assert.Throws(() => source.Skip(5).Take(^5..10).First()); + + Assert.Equal(1, source.Take(0..^4).First()); + Assert.Equal(1, source.Take(0..^1).First()); + Assert.Equal(1, source.Take(0..^0).First()); + Assert.Throws(() => source.Take(0..^5).First()); + Assert.Throws(() => source.Skip(5).Take(0..^5).First()); + + Assert.Equal(1, source.Take(^5..^4).First()); + Assert.Equal(1, source.Take(^5..^1).First()); + Assert.Equal(1, source.Take(^5..^0).First()); + Assert.Throws(() => source.Take(^5..^5).First()); + Assert.Throws(() => source.Skip(5).Take(^10..^0).First()); + } } [Fact] public void FirstOrDefault() { - var source = new[] { 1, 2, 3, 4, 5 }; - Assert.Equal(1, source.Take(1).FirstOrDefault()); - Assert.Equal(1, source.Take(4).FirstOrDefault()); - Assert.Equal(1, source.Take(40).FirstOrDefault()); - Assert.Equal(0, source.Take(0).FirstOrDefault()); - Assert.Equal(0, source.Skip(5).Take(10).FirstOrDefault()); - - Assert.Equal(1, source.Take(0..1).FirstOrDefault()); - Assert.Equal(1, source.Take(0..4).FirstOrDefault()); - Assert.Equal(1, source.Take(0..40).FirstOrDefault()); - Assert.Equal(0, source.Take(0..0).FirstOrDefault()); - Assert.Equal(0, source.Skip(5).Take(0..10).FirstOrDefault()); - - Assert.Equal(1, source.Take(^5..1).FirstOrDefault()); - Assert.Equal(1, source.Take(^5..4).FirstOrDefault()); - Assert.Equal(1, source.Take(^5..40).FirstOrDefault()); - Assert.Equal(0, source.Take(^5..0).FirstOrDefault()); - Assert.Equal(0, source.Skip(5).Take(^10..10).FirstOrDefault()); - - Assert.Equal(1, source.Take(0..^4).FirstOrDefault()); - Assert.Equal(1, source.Take(0..^1).FirstOrDefault()); - Assert.Equal(1, source.Take(0..^0).FirstOrDefault()); - Assert.Equal(0, source.Take(0..^5).FirstOrDefault()); - Assert.Equal(0, source.Skip(5).Take(0..^10).FirstOrDefault()); - - Assert.Equal(1, source.Take(^5..^4).FirstOrDefault()); - Assert.Equal(1, source.Take(^5..^1).FirstOrDefault()); - Assert.Equal(1, source.Take(^5..^0).FirstOrDefault()); - Assert.Equal(0, source.Take(^5..^5).FirstOrDefault()); - Assert.Equal(0, source.Skip(5).Take(^10..^0).FirstOrDefault()); - } - - [Fact] - public void FirstOrDefaultNotIList() - { - var source = ForceNotCollection(new[] { 1, 2, 3, 4, 5 }); - Assert.Equal(1, source.Take(1).FirstOrDefault()); - Assert.Equal(1, source.Take(4).FirstOrDefault()); - Assert.Equal(1, source.Take(40).FirstOrDefault()); - Assert.Equal(0, source.Take(0).FirstOrDefault()); - Assert.Equal(0, source.Skip(5).Take(10).FirstOrDefault()); - - Assert.Equal(1, source.Take(0..1).FirstOrDefault()); - Assert.Equal(1, source.Take(0..4).FirstOrDefault()); - Assert.Equal(1, source.Take(0..40).FirstOrDefault()); - Assert.Equal(0, source.Take(0..0).FirstOrDefault()); - Assert.Equal(0, source.Skip(5).Take(0..10).FirstOrDefault()); - - Assert.Equal(1, source.Take(^5..1).FirstOrDefault()); - Assert.Equal(1, source.Take(^5..4).FirstOrDefault()); - Assert.Equal(1, source.Take(^5..40).FirstOrDefault()); - Assert.Equal(0, source.Take(^5..0).FirstOrDefault()); - Assert.Equal(0, source.Skip(5).Take(^10..10).FirstOrDefault()); - - Assert.Equal(1, source.Take(0..^4).FirstOrDefault()); - Assert.Equal(1, source.Take(0..^1).FirstOrDefault()); - Assert.Equal(1, source.Take(0..^0).FirstOrDefault()); - Assert.Equal(0, source.Take(0..^5).FirstOrDefault()); - Assert.Equal(0, source.Skip(5).Take(0..^10).FirstOrDefault()); - - Assert.Equal(1, source.Take(^5..^4).FirstOrDefault()); - Assert.Equal(1, source.Take(^5..^1).FirstOrDefault()); - Assert.Equal(1, source.Take(^5..^0).FirstOrDefault()); - Assert.Equal(0, source.Take(^5..^5).FirstOrDefault()); - Assert.Equal(0, source.Skip(5).Take(^10..^0).FirstOrDefault()); + foreach (IEnumerable source in CreateSources([1, 2, 3, 4, 5])) + { + Assert.Equal(1, source.Take(1).FirstOrDefault()); + Assert.Equal(1, source.Take(4).FirstOrDefault()); + Assert.Equal(1, source.Take(40).FirstOrDefault()); + Assert.Equal(0, source.Take(0).FirstOrDefault()); + Assert.Equal(0, source.Skip(5).Take(10).FirstOrDefault()); + + Assert.Equal(1, source.Take(0..1).FirstOrDefault()); + Assert.Equal(1, source.Take(0..4).FirstOrDefault()); + Assert.Equal(1, source.Take(0..40).FirstOrDefault()); + Assert.Equal(0, source.Take(0..0).FirstOrDefault()); + Assert.Equal(0, source.Skip(5).Take(0..10).FirstOrDefault()); + + Assert.Equal(1, source.Take(^5..1).FirstOrDefault()); + Assert.Equal(1, source.Take(^5..4).FirstOrDefault()); + Assert.Equal(1, source.Take(^5..40).FirstOrDefault()); + Assert.Equal(0, source.Take(^5..0).FirstOrDefault()); + Assert.Equal(0, source.Skip(5).Take(^10..10).FirstOrDefault()); + + Assert.Equal(1, source.Take(0..^4).FirstOrDefault()); + Assert.Equal(1, source.Take(0..^1).FirstOrDefault()); + Assert.Equal(1, source.Take(0..^0).FirstOrDefault()); + Assert.Equal(0, source.Take(0..^5).FirstOrDefault()); + Assert.Equal(0, source.Skip(5).Take(0..^10).FirstOrDefault()); + + Assert.Equal(1, source.Take(^5..^4).FirstOrDefault()); + Assert.Equal(1, source.Take(^5..^1).FirstOrDefault()); + Assert.Equal(1, source.Take(^5..^0).FirstOrDefault()); + Assert.Equal(0, source.Take(^5..^5).FirstOrDefault()); + Assert.Equal(0, source.Skip(5).Take(^10..^0).FirstOrDefault()); + } } [Fact] public void Last() { - var source = new[] { 1, 2, 3, 4, 5 }; - Assert.Equal(1, source.Take(1).Last()); - Assert.Equal(5, source.Take(5).Last()); - Assert.Equal(5, source.Take(40).Last()); - Assert.Throws(() => source.Take(0).Last()); - Assert.Throws(() => Array.Empty().Take(40).Last()); - - Assert.Equal(1, source.Take(0..1).Last()); - Assert.Equal(5, source.Take(0..5).Last()); - Assert.Equal(5, source.Take(0..40).Last()); - Assert.Throws(() => source.Take(0..0).Last()); - Assert.Throws(() => Array.Empty().Take(0..40).Last()); - - Assert.Equal(1, source.Take(^5..1).Last()); - Assert.Equal(5, source.Take(^5..5).Last()); - Assert.Equal(5, source.Take(^5..40).Last()); - Assert.Throws(() => source.Take(^5..0).Last()); - Assert.Throws(() => Array.Empty().Take(^5..40).Last()); - - Assert.Equal(1, source.Take(0..^4).Last()); - Assert.Equal(5, source.Take(0..^0).Last()); - Assert.Equal(5, source.Take(3..^0).Last()); - Assert.Throws(() => source.Take(0..^5).Last()); - Assert.Throws(() => Array.Empty().Take(0..^0).Last()); - - Assert.Equal(1, source.Take(^5..^4).Last()); - Assert.Equal(5, source.Take(^5..^0).Last()); - Assert.Equal(5, source.Take(^5..^0).Last()); - Assert.Throws(() => source.Take(^5..^5).Last()); - Assert.Throws(() => Array.Empty().Take(^40..^0).Last()); - } - - [Fact] - public void LastNotIList() - { - var source = ForceNotCollection(new[] { 1, 2, 3, 4, 5 }); - Assert.Equal(1, source.Take(1).Last()); - Assert.Equal(5, source.Take(5).Last()); - Assert.Equal(5, source.Take(40).Last()); - Assert.Throws(() => source.Take(0).Last()); - Assert.Throws(() => ForceNotCollection(Array.Empty()).Take(40).Last()); - - Assert.Equal(1, source.Take(0..1).Last()); - Assert.Equal(5, source.Take(0..5).Last()); - Assert.Equal(5, source.Take(0..40).Last()); - Assert.Throws(() => source.Take(0..0).Last()); - Assert.Throws(() => ForceNotCollection(Array.Empty()).Take(0..40).Last()); - - Assert.Equal(1, source.Take(^5..1).Last()); - Assert.Equal(5, source.Take(^5..5).Last()); - Assert.Equal(5, source.Take(^5..40).Last()); - Assert.Throws(() => source.Take(^5..0).Last()); - Assert.Throws(() => ForceNotCollection(Array.Empty()).Take(^5..40).Last()); - - Assert.Equal(1, source.Take(0..^4).Last()); - Assert.Equal(5, source.Take(0..^0).Last()); - Assert.Equal(5, source.Take(3..^0).Last()); - Assert.Throws(() => source.Take(0..^5).Last()); - Assert.Throws(() => ForceNotCollection(Array.Empty()).Take(0..^0).Last()); - - Assert.Equal(1, source.Take(^5..^4).Last()); - Assert.Equal(5, source.Take(^5..^0).Last()); - Assert.Equal(5, source.Take(^5..^0).Last()); - Assert.Throws(() => source.Take(^5..^5).Last()); - Assert.Throws(() => ForceNotCollection(Array.Empty()).Take(^40..^0).Last()); + foreach (var source in CreateSources([1, 2, 3, 4, 5])) + { + Assert.Equal(1, source.Take(1).Last()); + Assert.Equal(2, source.Take(2).Last()); + Assert.Equal(3, source.Take(3).Last()); + Assert.Equal(4, source.Take(4).Last()); + Assert.Equal(5, source.Take(5).Last()); + Assert.Equal(5, source.Take(6).Last()); + Assert.Equal(5, source.Take(40).Last()); + Assert.Throws(() => source.Take(0).Last()); + Assert.Throws(() => Array.Empty().Take(40).Last()); + + Assert.Equal(1, source.Take(0..1).Last()); + Assert.Equal(5, source.Take(0..5).Last()); + Assert.Equal(5, source.Take(0..40).Last()); + Assert.Throws(() => source.Take(0..0).Last()); + Assert.Throws(() => Array.Empty().Take(0..40).Last()); + + Assert.Equal(1, source.Take(^5..1).Last()); + Assert.Equal(5, source.Take(^5..5).Last()); + Assert.Equal(5, source.Take(^5..40).Last()); + Assert.Throws(() => source.Take(^5..0).Last()); + Assert.Throws(() => Array.Empty().Take(^5..40).Last()); + + Assert.Equal(1, source.Take(0..^4).Last()); + Assert.Equal(5, source.Take(0..^0).Last()); + Assert.Equal(5, source.Take(3..^0).Last()); + Assert.Throws(() => source.Take(0..^5).Last()); + Assert.Throws(() => Array.Empty().Take(0..^0).Last()); + + Assert.Equal(1, source.Take(^5..^4).Last()); + Assert.Equal(5, source.Take(^5..^0).Last()); + Assert.Equal(5, source.Take(^5..^0).Last()); + Assert.Throws(() => source.Take(^5..^5).Last()); + Assert.Throws(() => Array.Empty().Take(^40..^0).Last()); + } } [Fact] public void LastOrDefault() { - var source = new[] { 1, 2, 3, 4, 5 }; - Assert.Equal(1, source.Take(1).LastOrDefault()); - Assert.Equal(5, source.Take(5).LastOrDefault()); - Assert.Equal(5, source.Take(40).LastOrDefault()); - Assert.Equal(0, source.Take(0).LastOrDefault()); - Assert.Equal(0, Array.Empty().Take(40).LastOrDefault()); - - Assert.Equal(1, source.Take(0..1).LastOrDefault()); - Assert.Equal(5, source.Take(0..5).LastOrDefault()); - Assert.Equal(5, source.Take(0..40).LastOrDefault()); - Assert.Equal(0, source.Take(0..0).LastOrDefault()); - Assert.Equal(0, Array.Empty().Take(0..40).LastOrDefault()); - - Assert.Equal(1, source.Take(^5..1).LastOrDefault()); - Assert.Equal(5, source.Take(^5..5).LastOrDefault()); - Assert.Equal(5, source.Take(^5..40).LastOrDefault()); - Assert.Equal(0, source.Take(^5..0).LastOrDefault()); - Assert.Equal(0, Array.Empty().Take(^5..40).LastOrDefault()); - - Assert.Equal(1, source.Take(0..^4).LastOrDefault()); - Assert.Equal(5, source.Take(0..^0).LastOrDefault()); - Assert.Equal(5, source.Take(3..^0).LastOrDefault()); - Assert.Equal(0, source.Take(0..^5).LastOrDefault()); - Assert.Equal(0, Array.Empty().Take(0..^0).LastOrDefault()); - - Assert.Equal(1, source.Take(^5..^4).LastOrDefault()); - Assert.Equal(5, source.Take(^5..^0).LastOrDefault()); - Assert.Equal(5, source.Take(^40..^0).LastOrDefault()); - Assert.Equal(0, source.Take(^5..^5).LastOrDefault()); - Assert.Equal(0, Array.Empty().Take(^40..^0).LastOrDefault()); - } - - [Fact] - public void LastOrDefaultNotIList() - { - var source = ForceNotCollection(new[] { 1, 2, 3, 4, 5 }); - Assert.Equal(1, source.Take(1).LastOrDefault()); - Assert.Equal(5, source.Take(5).LastOrDefault()); - Assert.Equal(5, source.Take(40).LastOrDefault()); - Assert.Equal(0, source.Take(0).LastOrDefault()); - Assert.Equal(0, ForceNotCollection(Array.Empty()).Take(40).LastOrDefault()); - - Assert.Equal(1, source.Take(0..1).LastOrDefault()); - Assert.Equal(5, source.Take(0..5).LastOrDefault()); - Assert.Equal(5, source.Take(0..40).LastOrDefault()); - Assert.Equal(0, source.Take(0..0).LastOrDefault()); - Assert.Equal(0, ForceNotCollection(Array.Empty()).Take(0..40).LastOrDefault()); - - Assert.Equal(1, source.Take(^5..1).LastOrDefault()); - Assert.Equal(5, source.Take(^5..5).LastOrDefault()); - Assert.Equal(5, source.Take(^5..40).LastOrDefault()); - Assert.Equal(0, source.Take(^5..0).LastOrDefault()); - Assert.Equal(0, ForceNotCollection(Array.Empty()).Take(^5..40).LastOrDefault()); - - Assert.Equal(1, source.Take(0..^4).LastOrDefault()); - Assert.Equal(5, source.Take(0..^0).LastOrDefault()); - Assert.Equal(5, source.Take(3..^0).LastOrDefault()); - Assert.Equal(0, source.Take(0..^5).LastOrDefault()); - Assert.Equal(0, ForceNotCollection(Array.Empty()).Take(0..^0).LastOrDefault()); - - Assert.Equal(1, source.Take(^5..^4).LastOrDefault()); - Assert.Equal(5, source.Take(^5..^0).LastOrDefault()); - Assert.Equal(5, source.Take(^40..^0).LastOrDefault()); - Assert.Equal(0, source.Take(^5..^5).LastOrDefault()); - Assert.Equal(0, ForceNotCollection(Array.Empty()).Take(^40..^0).LastOrDefault()); + foreach (var source in CreateSources([1, 2, 3, 4, 5])) + { + Assert.Equal(1, source.Take(1).LastOrDefault()); + Assert.Equal(2, source.Take(2).LastOrDefault()); + Assert.Equal(3, source.Take(3).LastOrDefault()); + Assert.Equal(4, source.Take(4).LastOrDefault()); + Assert.Equal(5, source.Take(5).LastOrDefault()); + Assert.Equal(5, source.Take(6).LastOrDefault()); + Assert.Equal(5, source.Take(40).LastOrDefault()); + Assert.Equal(0, source.Take(0).LastOrDefault()); + Assert.Equal(0, Array.Empty().Take(40).LastOrDefault()); + + Assert.Equal(1, source.Take(0..1).LastOrDefault()); + Assert.Equal(5, source.Take(0..5).LastOrDefault()); + Assert.Equal(5, source.Take(0..40).LastOrDefault()); + Assert.Equal(0, source.Take(0..0).LastOrDefault()); + Assert.Equal(0, Array.Empty().Take(0..40).LastOrDefault()); + + Assert.Equal(1, source.Take(^5..1).LastOrDefault()); + Assert.Equal(5, source.Take(^5..5).LastOrDefault()); + Assert.Equal(5, source.Take(^5..40).LastOrDefault()); + Assert.Equal(0, source.Take(^5..0).LastOrDefault()); + Assert.Equal(0, Array.Empty().Take(^5..40).LastOrDefault()); + + Assert.Equal(1, source.Take(0..^4).LastOrDefault()); + Assert.Equal(5, source.Take(0..^0).LastOrDefault()); + Assert.Equal(5, source.Take(3..^0).LastOrDefault()); + Assert.Equal(0, source.Take(0..^5).LastOrDefault()); + Assert.Equal(0, Array.Empty().Take(0..^0).LastOrDefault()); + + Assert.Equal(1, source.Take(^5..^4).LastOrDefault()); + Assert.Equal(5, source.Take(^5..^0).LastOrDefault()); + Assert.Equal(5, source.Take(^40..^0).LastOrDefault()); + Assert.Equal(0, source.Take(^5..^5).LastOrDefault()); + Assert.Equal(0, Array.Empty().Take(^40..^0).LastOrDefault()); + } } [Fact] public void ToArray() { - var source = new[] { 1, 2, 3, 4, 5 }; - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(5).ToArray()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(6).ToArray()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(40).ToArray()); - Assert.Equal(new[] { 1, 2, 3, 4 }, source.Take(4).ToArray()); - Assert.Equal(1, source.Take(1).ToArray().Single()); - Assert.Empty(source.Take(0).ToArray()); - Assert.Empty(source.Take(-10).ToArray()); - - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(0..5).ToArray()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(0..6).ToArray()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(0..40).ToArray()); - Assert.Equal(new[] { 1, 2, 3, 4 }, source.Take(0..4).ToArray()); - Assert.Equal(1, source.Take(0..1).ToArray().Single()); - Assert.Empty(source.Take(0..0).ToArray()); - - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^5..5).ToArray()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^5..6).ToArray()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^5..40).ToArray()); - Assert.Equal(new[] { 1, 2, 3, 4 }, source.Take(^5..4).ToArray()); - Assert.Equal(1, source.Take(^5..1).ToArray().Single()); - Assert.Empty(source.Take(^5..0).ToArray()); - Assert.Empty(source.Take(^15..0).ToArray()); - - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(0..^0).ToArray()); - Assert.Equal(new[] { 1, 2, 3, 4 }, source.Take(0..^1).ToArray()); - Assert.Equal(1, source.Take(0..^4).ToArray().Single()); - Assert.Empty(source.Take(0..^5).ToArray()); - Assert.Empty(source.Take(0..^15).ToArray()); - - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^5..^0).ToArray()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^6..^0).ToArray()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^45..^0).ToArray()); - Assert.Equal(new[] { 1, 2, 3, 4 }, source.Take(^5..^1).ToArray()); - Assert.Equal(1, source.Take(^5..^4).ToArray().Single()); - Assert.Empty(source.Take(^5..^5).ToArray()); - Assert.Empty(source.Take(^15..^5).ToArray()); - } - - [Fact] - public void ToArrayNotList() - { - var source = ForceNotCollection(new[] { 1, 2, 3, 4, 5 }); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(5).ToArray()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(6).ToArray()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(40).ToArray()); - Assert.Equal(new[] { 1, 2, 3, 4 }, source.Take(4).ToArray()); - Assert.Equal(1, source.Take(1).ToArray().Single()); - Assert.Empty(source.Take(0).ToArray()); - Assert.Empty(source.Take(-10).ToArray()); - - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(0..5).ToArray()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(0..6).ToArray()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(0..40).ToArray()); - Assert.Equal(new[] { 1, 2, 3, 4 }, source.Take(0..4).ToArray()); - Assert.Equal(1, source.Take(0..1).ToArray().Single()); - Assert.Empty(source.Take(0..0).ToArray()); - - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^5..5).ToArray()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^5..6).ToArray()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^5..40).ToArray()); - Assert.Equal(new[] { 1, 2, 3, 4 }, source.Take(^5..4).ToArray()); - Assert.Equal(1, source.Take(^5..1).ToArray().Single()); - Assert.Empty(source.Take(^5..0).ToArray()); - Assert.Empty(source.Take(^15..0).ToArray()); - - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(0..^0).ToArray()); - Assert.Equal(new[] { 1, 2, 3, 4 }, source.Take(0..^1).ToArray()); - Assert.Equal(1, source.Take(0..^4).ToArray().Single()); - Assert.Empty(source.Take(0..^5).ToArray()); - Assert.Empty(source.Take(0..^15).ToArray()); - - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^5..^0).ToArray()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^6..^0).ToArray()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^45..^0).ToArray()); - Assert.Equal(new[] { 1, 2, 3, 4 }, source.Take(^5..^1).ToArray()); - Assert.Equal(1, source.Take(^5..^4).ToArray().Single()); - Assert.Empty(source.Take(^5..^5).ToArray()); - Assert.Empty(source.Take(^15..^5).ToArray()); + foreach (var source in CreateSources([1, 2, 3, 4, 5])) + { + Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(5).ToArray()); + Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(6).ToArray()); + Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(40).ToArray()); + Assert.Equal(new[] { 1, 2, 3, 4 }, source.Take(4).ToArray()); + Assert.Equal(1, source.Take(1).ToArray().Single()); + Assert.Empty(source.Take(0).ToArray()); + Assert.Empty(source.Take(-10).ToArray()); + + Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(0..5).ToArray()); + Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(0..6).ToArray()); + Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(0..40).ToArray()); + Assert.Equal(new[] { 1, 2, 3, 4 }, source.Take(0..4).ToArray()); + Assert.Equal(1, source.Take(0..1).ToArray().Single()); + Assert.Empty(source.Take(0..0).ToArray()); + + Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^5..5).ToArray()); + Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^5..6).ToArray()); + Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^5..40).ToArray()); + Assert.Equal(new[] { 1, 2, 3, 4 }, source.Take(^5..4).ToArray()); + Assert.Equal(1, source.Take(^5..1).ToArray().Single()); + Assert.Empty(source.Take(^5..0).ToArray()); + Assert.Empty(source.Take(^15..0).ToArray()); + + Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(0..^0).ToArray()); + Assert.Equal(new[] { 1, 2, 3, 4 }, source.Take(0..^1).ToArray()); + Assert.Equal(1, source.Take(0..^4).ToArray().Single()); + Assert.Empty(source.Take(0..^5).ToArray()); + Assert.Empty(source.Take(0..^15).ToArray()); + + Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^5..^0).ToArray()); + Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^6..^0).ToArray()); + Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^45..^0).ToArray()); + Assert.Equal(new[] { 1, 2, 3, 4 }, source.Take(^5..^1).ToArray()); + Assert.Equal(1, source.Take(^5..^4).ToArray().Single()); + Assert.Empty(source.Take(^5..^5).ToArray()); + Assert.Empty(source.Take(^15..^5).ToArray()); + } } [Fact] public void ToList() { - var source = new[] { 1, 2, 3, 4, 5 }; - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(5).ToList()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(6).ToList()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(40).ToList()); - Assert.Equal(new[] { 1, 2, 3, 4 }, source.Take(4).ToList()); - Assert.Equal(1, source.Take(1).ToList().Single()); - Assert.Empty(source.Take(0).ToList()); - Assert.Empty(source.Take(-10).ToList()); - - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(0..5).ToList()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(0..6).ToList()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(0..40).ToList()); - Assert.Equal(new[] { 1, 2, 3, 4 }, source.Take(0..4).ToList()); - Assert.Equal(1, source.Take(0..1).ToList().Single()); - Assert.Empty(source.Take(0..0).ToList()); - - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^5..5).ToList()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^5..6).ToList()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^5..40).ToList()); - Assert.Equal(new[] { 1, 2, 3, 4 }, source.Take(^5..4).ToList()); - Assert.Equal(1, source.Take(^5..1).ToList().Single()); - Assert.Empty(source.Take(^5..0).ToList()); - Assert.Empty(source.Take(^15..0).ToList()); - - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(0..^0).ToList()); - Assert.Equal(new[] { 1, 2, 3, 4 }, source.Take(0..^1).ToList()); - Assert.Equal(1, source.Take(0..^4).ToList().Single()); - Assert.Empty(source.Take(0..^5).ToList()); - Assert.Empty(source.Take(0..^15).ToList()); - - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^5..^0).ToList()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^6..^0).ToList()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^45..^0).ToList()); - Assert.Equal(new[] { 1, 2, 3, 4 }, source.Take(^5..^1).ToList()); - Assert.Equal(1, source.Take(^5..^4).ToList().Single()); - Assert.Empty(source.Take(^5..^5).ToList()); - Assert.Empty(source.Take(^15..^5).ToList()); - } - - [Fact] - public void ToListNotList() - { - var source = ForceNotCollection(new[] { 1, 2, 3, 4, 5 }); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(5).ToList()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(6).ToList()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(40).ToList()); - Assert.Equal(new[] { 1, 2, 3, 4 }, source.Take(4).ToList()); - Assert.Equal(1, source.Take(1).ToList().Single()); - Assert.Empty(source.Take(0).ToList()); - Assert.Empty(source.Take(-10).ToList()); - - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(0..5).ToList()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(0..6).ToList()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(0..40).ToList()); - Assert.Equal(new[] { 1, 2, 3, 4 }, source.Take(0..4).ToList()); - Assert.Equal(1, source.Take(0..1).ToList().Single()); - Assert.Empty(source.Take(0..0).ToList()); - - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^5..5).ToList()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^5..6).ToList()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^5..40).ToList()); - Assert.Equal(new[] { 1, 2, 3, 4 }, source.Take(^5..4).ToList()); - Assert.Equal(1, source.Take(^5..1).ToList().Single()); - Assert.Empty(source.Take(^5..0).ToList()); - Assert.Empty(source.Take(^15..0).ToList()); - - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(0..^0).ToList()); - Assert.Equal(new[] { 1, 2, 3, 4 }, source.Take(0..^1).ToList()); - Assert.Equal(1, source.Take(0..^4).ToList().Single()); - Assert.Empty(source.Take(0..^5).ToList()); - Assert.Empty(source.Take(0..^15).ToList()); - - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^5..^0).ToList()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^6..^0).ToList()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^45..^0).ToList()); - Assert.Equal(new[] { 1, 2, 3, 4 }, source.Take(^5..^1).ToList()); - Assert.Equal(1, source.Take(^5..^4).ToList().Single()); - Assert.Empty(source.Take(^5..^5).ToList()); - Assert.Empty(source.Take(^15..^5).ToList()); + foreach (var source in CreateSources([1, 2, 3, 4, 5])) + { + Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(5).ToList()); + Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(6).ToList()); + Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(40).ToList()); + Assert.Equal(new[] { 1, 2, 3, 4 }, source.Take(4).ToList()); + Assert.Equal(1, source.Take(1).ToList().Single()); + Assert.Empty(source.Take(0).ToList()); + Assert.Empty(source.Take(-10).ToList()); + + Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(0..5).ToList()); + Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(0..6).ToList()); + Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(0..40).ToList()); + Assert.Equal(new[] { 1, 2, 3, 4 }, source.Take(0..4).ToList()); + Assert.Equal(1, source.Take(0..1).ToList().Single()); + Assert.Empty(source.Take(0..0).ToList()); + + Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^5..5).ToList()); + Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^5..6).ToList()); + Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^5..40).ToList()); + Assert.Equal(new[] { 1, 2, 3, 4 }, source.Take(^5..4).ToList()); + Assert.Equal(1, source.Take(^5..1).ToList().Single()); + Assert.Empty(source.Take(^5..0).ToList()); + Assert.Empty(source.Take(^15..0).ToList()); + + Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(0..^0).ToList()); + Assert.Equal(new[] { 1, 2, 3, 4 }, source.Take(0..^1).ToList()); + Assert.Equal(1, source.Take(0..^4).ToList().Single()); + Assert.Empty(source.Take(0..^5).ToList()); + Assert.Empty(source.Take(0..^15).ToList()); + + Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^5..^0).ToList()); + Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^6..^0).ToList()); + Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^45..^0).ToList()); + Assert.Equal(new[] { 1, 2, 3, 4 }, source.Take(^5..^1).ToList()); + Assert.Equal(1, source.Take(^5..^4).ToList().Single()); + Assert.Empty(source.Take(^5..^5).ToList()); + Assert.Empty(source.Take(^15..^5).ToList()); + } } [Fact] public void TakeCanOnlyBeOneList() { - var source = new[] { 2, 4, 6, 8, 10 }; - Assert.Equal(new[] { 2 }, source.Take(1)); - Assert.Equal(new[] { 4 }, source.Skip(1).Take(1)); - Assert.Equal(new[] { 6 }, source.Take(3).Skip(2)); - Assert.Equal(new[] { 2 }, source.Take(3).Take(1)); - - Assert.Equal(new[] { 2 }, source.Take(0..1)); - Assert.Equal(new[] { 4 }, source.Skip(1).Take(0..1)); - Assert.Equal(new[] { 6 }, source.Take(0..3).Skip(2)); - Assert.Equal(new[] { 2 }, source.Take(0..3).Take(0..1)); - - Assert.Equal(new[] { 2 }, source.Take(^5..1)); - Assert.Equal(new[] { 4 }, source.Skip(1).Take(^4..1)); - Assert.Equal(new[] { 6 }, source.Take(^5..3).Skip(2)); - Assert.Equal(new[] { 2 }, source.Take(^5..3).Take(^4..1)); - - Assert.Equal(new[] { 2 }, source.Take(0..^4)); - Assert.Equal(new[] { 4 }, source.Skip(1).Take(0..^3)); - Assert.Equal(new[] { 6 }, source.Take(0..^2).Skip(2)); - Assert.Equal(new[] { 2 }, source.Take(0..^2).Take(0..^2)); - - Assert.Equal(new[] { 2 }, source.Take(^5..^4)); - Assert.Equal(new[] { 4 }, source.Skip(1).Take(^4..^3)); - Assert.Equal(new[] { 6 }, source.Take(^5..^2).Skip(2)); - Assert.Equal(new[] { 2 }, source.Take(^5..^2).Take(^4..^2)); - } - - [Fact] - public void TakeCanOnlyBeOneNotList() - { - var source = ForceNotCollection(new[] { 2, 4, 6, 8, 10 }); - Assert.Equal(new[] { 2 }, source.Take(1)); - Assert.Equal(new[] { 4 }, source.Skip(1).Take(1)); - Assert.Equal(new[] { 6 }, source.Take(3).Skip(2)); - Assert.Equal(new[] { 2 }, source.Take(3).Take(1)); - - Assert.Equal(new[] { 2 }, source.Take(0..1)); - Assert.Equal(new[] { 4 }, source.Skip(1).Take(0..1)); - Assert.Equal(new[] { 6 }, source.Take(0..3).Skip(2)); - Assert.Equal(new[] { 2 }, source.Take(0..3).Take(0..1)); - - Assert.Equal(new[] { 2 }, source.Take(^5..1)); - Assert.Equal(new[] { 4 }, source.Skip(1).Take(^4..1)); - Assert.Equal(new[] { 6 }, source.Take(^5..3).Skip(2)); - Assert.Equal(new[] { 2 }, source.Take(^5..3).Take(^4..1)); - - Assert.Equal(new[] { 2 }, source.Take(0..^4)); - Assert.Equal(new[] { 4 }, source.Skip(1).Take(0..^3)); - Assert.Equal(new[] { 6 }, source.Take(0..^2).Skip(2)); - Assert.Equal(new[] { 2 }, source.Take(0..^2).Take(0..^2)); - - Assert.Equal(new[] { 2 }, source.Take(^5..^4)); - Assert.Equal(new[] { 4 }, source.Skip(1).Take(^4..^3)); - Assert.Equal(new[] { 6 }, source.Take(^5..^2).Skip(2)); - Assert.Equal(new[] { 2 }, source.Take(^5..^2).Take(^4..^2)); + foreach (var source in CreateSources([2, 4, 6, 8, 10])) + { + Assert.Equal([2], source.Take(1)); + Assert.Equal([4], source.Skip(1).Take(1)); + Assert.Equal([6], source.Take(3).Skip(2)); + Assert.Equal([2], source.Take(3).Take(1)); + + Assert.Equal([2], source.Take(0..1)); + Assert.Equal([4], source.Skip(1).Take(0..1)); + Assert.Equal([6], source.Take(0..3).Skip(2)); + Assert.Equal([2], source.Take(0..3).Take(0..1)); + + Assert.Equal([2], source.Take(^5..1)); + Assert.Equal([4], source.Skip(1).Take(^4..1)); + Assert.Equal([6], source.Take(^5..3).Skip(2)); + Assert.Equal([2], source.Take(^5..3).Take(^4..1)); + + Assert.Equal([2], source.Take(0..^4)); + Assert.Equal([4], source.Skip(1).Take(0..^3)); + Assert.Equal([6], source.Take(0..^2).Skip(2)); + Assert.Equal([2], source.Take(0..^2).Take(0..^2)); + + Assert.Equal([2], source.Take(^5..^4)); + Assert.Equal([4], source.Skip(1).Take(^4..^3)); + Assert.Equal([6], source.Take(^5..^2).Skip(2)); + Assert.Equal([2], source.Take(^5..^2).Take(^4..^2)); + } } [Fact] public void RepeatEnumerating() { - var source = new[] { 1, 2, 3, 4, 5 }; - var taken1 = source.Take(3); - Assert.Equal(taken1, taken1); - - var taken2 = source.Take(0..3); - Assert.Equal(taken2, taken2); - - var taken3 = source.Take(^5..3); - Assert.Equal(taken3, taken3); - - var taken4 = source.Take(0..^2); - Assert.Equal(taken4, taken4); - - var taken5 = source.Take(^5..^2); - Assert.Equal(taken5, taken5); - } - - [Fact] - public void RepeatEnumeratingNotList() - { - var source = ForceNotCollection(new[] { 1, 2, 3, 4, 5 }); - var taken1 = source.Take(3); - Assert.Equal(taken1, taken1); + foreach (var source in CreateSources([1, 2, 3, 4, 5])) + { + var taken1 = source.Take(3); + Assert.Equal(taken1, taken1); - var taken2 = source.Take(0..3); - Assert.Equal(taken2, taken2); + var taken2 = source.Take(0..3); + Assert.Equal(taken2, taken2); - var taken3 = source.Take(^5..3); - Assert.Equal(taken3, taken3); + var taken3 = source.Take(^5..3); + Assert.Equal(taken3, taken3); - var taken4 = source.Take(0..^2); - Assert.Equal(taken4, taken4); + var taken4 = source.Take(0..^2); + Assert.Equal(taken4, taken4); - var taken5 = source.Take(^5..^2); - Assert.Equal(taken5, taken5); + var taken5 = source.Take(^5..^2); + Assert.Equal(taken5, taken5); + } } - [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsSpeedOptimized))] + [ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsLinqSpeedOptimized))] [InlineData(1000)] [InlineData(1000000)] [InlineData(int.MaxValue)] @@ -1307,7 +897,7 @@ public void DisposeSource(int sourceCount, int count) }, 5); - IEnumerator iterator0 = source[0].Take(count).GetEnumerator(); + using IEnumerator iterator0 = source[0].Take(count).GetEnumerator(); int iteratorCount0 = Math.Min(sourceCount, Math.Max(0, count)); Assert.All(Enumerable.Range(0, iteratorCount0), _ => Assert.True(iterator0.MoveNext())); @@ -1320,7 +910,7 @@ public void DisposeSource(int sourceCount, int count) Assert.Equal(isItertorNotEmpty0, isIteratorDisposed[0]); int end = Math.Max(0, count); - IEnumerator iterator1 = source[1].Take(0..end).GetEnumerator(); + using IEnumerator iterator1 = source[1].Take(0..end).GetEnumerator(); Assert.All(Enumerable.Range(0, Math.Min(sourceCount, Math.Max(0, count))), _ => Assert.True(iterator1.MoveNext())); Assert.False(iterator1.MoveNext()); // When startIndex end and endIndex are both not from end and startIndex >= endIndex, Take(Range) returns an empty array. @@ -1330,19 +920,19 @@ public void DisposeSource(int sourceCount, int count) int startIndexFromEnd = Math.Max(sourceCount, end); int endIndexFromEnd = Math.Max(0, sourceCount - end); - IEnumerator iterator2 = source[2].Take(^startIndexFromEnd..end).GetEnumerator(); + using IEnumerator iterator2 = source[2].Take(^startIndexFromEnd..end).GetEnumerator(); Assert.All(Enumerable.Range(0, Math.Min(sourceCount, Math.Max(0, count))), _ => Assert.True(iterator2.MoveNext())); Assert.False(iterator2.MoveNext()); // When startIndex is ^0, Take(Range) returns an empty array. bool isIteratorNotEmpty2 = startIndexFromEnd != 0; Assert.Equal(isIteratorNotEmpty2, isIteratorDisposed[2]); - IEnumerator iterator3 = source[3].Take(0..^endIndexFromEnd).GetEnumerator(); + using IEnumerator iterator3 = source[3].Take(0..^endIndexFromEnd).GetEnumerator(); Assert.All(Enumerable.Range(0, Math.Min(sourceCount, Math.Max(0, count))), _ => Assert.True(iterator3.MoveNext())); Assert.False(iterator3.MoveNext()); Assert.True(isIteratorDisposed[3]); - IEnumerator iterator4 = source[4].Take(^startIndexFromEnd..^endIndexFromEnd).GetEnumerator(); + using IEnumerator iterator4 = source[4].Take(^startIndexFromEnd..^endIndexFromEnd).GetEnumerator(); Assert.All(Enumerable.Range(0, Math.Min(sourceCount, Math.Max(0, count))), _ => Assert.True(iterator4.MoveNext())); Assert.False(iterator4.MoveNext()); // When startIndex is ^0, @@ -1396,13 +986,13 @@ public void DisposeSource_EndIndexFromEnd_ShouldDisposeOnCompletedEnumeration() [Fact] public void OutOfBoundNoException() { - Func source = () => new[] { 1, 2, 3, 4, 5 }; + Func source = () => [1, 2, 3, 4, 5]; Assert.Equal(source(), source().Take(0..6)); Assert.Equal(source(), source().Take(0..int.MaxValue)); - Assert.Equal(new int[] { 1, 2, 3, 4 }, source().Take(^10..4)); - Assert.Equal(new int[] { 1, 2, 3, 4 }, source().Take(^int.MaxValue..4)); + Assert.Equal([1, 2, 3, 4], source().Take(^10..4)); + Assert.Equal([1, 2, 3, 4], source().Take(^int.MaxValue..4)); Assert.Equal(source(), source().Take(^10..6)); Assert.Equal(source(), source().Take(^int.MaxValue..6)); Assert.Equal(source(), source().Take(^10..int.MaxValue)); @@ -1417,8 +1007,8 @@ public void OutOfBoundNoException() Assert.Empty(source().Take(int.MaxValue..^6)); Assert.Empty(source().Take(int.MaxValue..^int.MaxValue)); - Assert.Equal(new int[] { 1, 2, 3, 4 }, source().Take(^10..^1)); - Assert.Equal(new int[] { 1, 2, 3, 4 }, source().Take(^int.MaxValue..^1)); + Assert.Equal([1, 2, 3, 4], source().Take(^10..^1)); + Assert.Equal([1, 2, 3, 4], source().Take(^int.MaxValue..^1)); Assert.Empty(source().Take(^0..^6)); Assert.Empty(source().Take(^1..^6)); Assert.Empty(source().Take(^6..^6)); @@ -1438,8 +1028,8 @@ public void OutOfBoundNoExceptionNotList() Assert.Equal(source, ForceNotCollection(source).Take(0..6)); Assert.Equal(source, ForceNotCollection(source).Take(0..int.MaxValue)); - Assert.Equal(new int[] { 1, 2, 3, 4 }, ForceNotCollection(source).Take(^10..4)); - Assert.Equal(new int[] { 1, 2, 3, 4 }, ForceNotCollection(source).Take(^int.MaxValue..4)); + Assert.Equal([1, 2, 3, 4], ForceNotCollection(source).Take(^10..4)); + Assert.Equal([1, 2, 3, 4], ForceNotCollection(source).Take(^int.MaxValue..4)); Assert.Equal(source, ForceNotCollection(source).Take(^10..6)); Assert.Equal(source, ForceNotCollection(source).Take(^int.MaxValue..6)); Assert.Equal(source, ForceNotCollection(source).Take(^10..int.MaxValue)); @@ -1454,8 +1044,8 @@ public void OutOfBoundNoExceptionNotList() Assert.Empty(ForceNotCollection(source).Take(int.MaxValue..^6)); Assert.Empty(ForceNotCollection(source).Take(int.MaxValue..^int.MaxValue)); - Assert.Equal(new int[] { 1, 2, 3, 4 }, ForceNotCollection(source).Take(^10..^1)); - Assert.Equal(new int[] { 1, 2, 3, 4 }, ForceNotCollection(source).Take(^int.MaxValue..^1)); + Assert.Equal([1, 2, 3, 4], ForceNotCollection(source).Take(^10..^1)); + Assert.Equal([1, 2, 3, 4], ForceNotCollection(source).Take(^int.MaxValue..^1)); Assert.Empty(ForceNotCollection(source).Take(^0..^6)); Assert.Empty(ForceNotCollection(source).Take(^1..^6)); Assert.Empty(ForceNotCollection(source).Take(^6..^6)); @@ -1475,8 +1065,8 @@ public void OutOfBoundNoExceptionListPartition() Assert.Equal(source, ListPartitionOrEmpty(source).Take(0..6)); Assert.Equal(source, ListPartitionOrEmpty(source).Take(0..int.MaxValue)); - Assert.Equal(new int[] { 1, 2, 3, 4 }, ListPartitionOrEmpty(source).Take(^10..4)); - Assert.Equal(new int[] { 1, 2, 3, 4 }, ListPartitionOrEmpty(source).Take(^int.MaxValue..4)); + Assert.Equal([1, 2, 3, 4], ListPartitionOrEmpty(source).Take(^10..4)); + Assert.Equal([1, 2, 3, 4], ListPartitionOrEmpty(source).Take(^int.MaxValue..4)); Assert.Equal(source, ListPartitionOrEmpty(source).Take(^10..6)); Assert.Equal(source, ListPartitionOrEmpty(source).Take(^int.MaxValue..6)); Assert.Equal(source, ListPartitionOrEmpty(source).Take(^10..int.MaxValue)); @@ -1491,8 +1081,8 @@ public void OutOfBoundNoExceptionListPartition() Assert.Empty(ListPartitionOrEmpty(source).Take(int.MaxValue..^6)); Assert.Empty(ListPartitionOrEmpty(source).Take(int.MaxValue..^int.MaxValue)); - Assert.Equal(new int[] { 1, 2, 3, 4 }, ListPartitionOrEmpty(source).Take(^10..^1)); - Assert.Equal(new int[] { 1, 2, 3, 4 }, ListPartitionOrEmpty(source).Take(^int.MaxValue..^1)); + Assert.Equal([1, 2, 3, 4], ListPartitionOrEmpty(source).Take(^10..^1)); + Assert.Equal([1, 2, 3, 4], ListPartitionOrEmpty(source).Take(^int.MaxValue..^1)); Assert.Empty(ListPartitionOrEmpty(source).Take(^0..^6)); Assert.Empty(ListPartitionOrEmpty(source).Take(^1..^6)); Assert.Empty(ListPartitionOrEmpty(source).Take(^6..^6)); @@ -1512,8 +1102,8 @@ public void OutOfBoundNoExceptionEnumerablePartition() Assert.Equal(source, EnumerablePartitionOrEmpty(source).Take(0..6)); Assert.Equal(source, EnumerablePartitionOrEmpty(source).Take(0..int.MaxValue)); - Assert.Equal(new int[] { 1, 2, 3, 4 }, EnumerablePartitionOrEmpty(source).Take(^10..4)); - Assert.Equal(new int[] { 1, 2, 3, 4 }, EnumerablePartitionOrEmpty(source).Take(^int.MaxValue..4)); + Assert.Equal([1, 2, 3, 4], EnumerablePartitionOrEmpty(source).Take(^10..4)); + Assert.Equal([1, 2, 3, 4], EnumerablePartitionOrEmpty(source).Take(^int.MaxValue..4)); Assert.Equal(source, EnumerablePartitionOrEmpty(source).Take(^10..6)); Assert.Equal(source, EnumerablePartitionOrEmpty(source).Take(^int.MaxValue..6)); Assert.Equal(source, EnumerablePartitionOrEmpty(source).Take(^10..int.MaxValue)); @@ -1528,8 +1118,8 @@ public void OutOfBoundNoExceptionEnumerablePartition() Assert.Empty(EnumerablePartitionOrEmpty(source).Take(int.MaxValue..^6)); Assert.Empty(EnumerablePartitionOrEmpty(source).Take(int.MaxValue..^int.MaxValue)); - Assert.Equal(new int[] { 1, 2, 3, 4 }, EnumerablePartitionOrEmpty(source).Take(^10..^1)); - Assert.Equal(new int[] { 1, 2, 3, 4 }, EnumerablePartitionOrEmpty(source).Take(^int.MaxValue..^1)); + Assert.Equal([1, 2, 3, 4], EnumerablePartitionOrEmpty(source).Take(^10..^1)); + Assert.Equal([1, 2, 3, 4], EnumerablePartitionOrEmpty(source).Take(^int.MaxValue..^1)); Assert.Empty(EnumerablePartitionOrEmpty(source).Take(^0..^6)); Assert.Empty(EnumerablePartitionOrEmpty(source).Take(^1..^6)); Assert.Empty(EnumerablePartitionOrEmpty(source).Take(^6..^6)); @@ -1547,26 +1137,26 @@ public void MutableSource() var source1 = new List() { 0, 1, 2, 3, 4 }; var query1 = source1.Take(3); source1.RemoveAt(0); - source1.InsertRange(2, new[] { -1, -2 }); - Assert.Equal(new[] { 1, 2, -1 }, query1); + source1.InsertRange(2, [-1, -2]); + Assert.Equal([1, 2, -1], query1); var source2 = new List() { 0, 1, 2, 3, 4 }; var query2 = source2.Take(0..3); source2.RemoveAt(0); - source2.InsertRange(2, new[] { -1, -2 }); - Assert.Equal(new[] { 1, 2, -1 }, query2); + source2.InsertRange(2, [-1, -2]); + Assert.Equal([1, 2, -1], query2); var source3 = new List() { 0, 1, 2, 3, 4 }; var query3 = source3.Take(^6..3); source3.RemoveAt(0); - source3.InsertRange(2, new[] { -1, -2 }); - Assert.Equal(new[] { 1, 2, -1 }, query3); + source3.InsertRange(2, [-1, -2]); + Assert.Equal([1, 2, -1], query3); var source4 = new List() { 0, 1, 2, 3, 4 }; var query4 = source4.Take(^6..^3); source4.RemoveAt(0); - source4.InsertRange(2, new[] { -1, -2 }); - Assert.Equal(new[] { 1, 2, -1 }, query4); + source4.InsertRange(2, [-1, -2]); + Assert.Equal([1, 2, -1], query4); } [Fact] @@ -1575,32 +1165,32 @@ public void MutableSourceNotList() var source1 = new List() { 0, 1, 2, 3, 4 }; var query1 = ForceNotCollection(source1).Select(i => i).Take(3); source1.RemoveAt(0); - source1.InsertRange(2, new[] { -1, -2 }); - Assert.Equal(new[] { 1, 2, -1 }, query1); + source1.InsertRange(2, [-1, -2]); + Assert.Equal([1, 2, -1], query1); var source2 = new List() { 0, 1, 2, 3, 4 }; var query2 = ForceNotCollection(source2).Select(i => i).Take(0..3); source2.RemoveAt(0); - source2.InsertRange(2, new[] { -1, -2 }); - Assert.Equal(new[] { 1, 2, -1 }, query2); + source2.InsertRange(2, [-1, -2]); + Assert.Equal([1, 2, -1], query2); var source3 = new List() { 0, 1, 2, 3, 4 }; var query3 = ForceNotCollection(source3).Select(i => i).Take(^6..3); source3.RemoveAt(0); - source3.InsertRange(2, new[] { -1, -2 }); - Assert.Equal(new[] { 1, 2, -1 }, query3); + source3.InsertRange(2, [-1, -2]); + Assert.Equal([1, 2, -1], query3); var source4 = new List() { 0, 1, 2, 3, 4 }; var query4 = ForceNotCollection(source4).Select(i => i).Take(^6..^3); source4.RemoveAt(0); - source4.InsertRange(2, new[] { -1, -2 }); - Assert.Equal(new[] { 1, 2, -1 }, query4); + source4.InsertRange(2, [-1, -2]); + Assert.Equal([1, 2, -1], query4); } [Fact] public void NonEmptySource_ConsistencyWithCountable() { - Func source = () => new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + Func source = () => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; // Multiple elements in the middle. Assert.Equal(source()[^9..5], source().Take(^9..5)); @@ -1646,7 +1236,7 @@ public void NonEmptySource_ConsistencyWithCountable() [Fact] public void NonEmptySource_ConsistencyWithCountable_NotList() { - int[] source = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + int[] source = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; // Multiple elements in the middle. Assert.Equal(source[^9..5], ForceNotCollection(source).Take(^9..5)); @@ -1692,7 +1282,7 @@ public void NonEmptySource_ConsistencyWithCountable_NotList() [Fact] public void NonEmptySource_ConsistencyWithCountable_ListPartition() { - int[] source = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + int[] source = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; // Multiple elements in the middle. Assert.Equal(source[^9..5], ListPartitionOrEmpty(source).Take(^9..5)); @@ -1738,7 +1328,7 @@ public void NonEmptySource_ConsistencyWithCountable_ListPartition() [Fact] public void NonEmptySource_ConsistencyWithCountable_EnumerablePartition() { - int[] source = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + int[] source = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; // Multiple elements in the middle. Assert.Equal(source[^9..5], EnumerablePartitionOrEmpty(source).Take(^9..5)); @@ -1784,7 +1374,7 @@ public void NonEmptySource_ConsistencyWithCountable_EnumerablePartition() [Fact] public void NonEmptySource_DoNotThrowException() { - Func source = () => new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + Func source = () => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; Assert.Empty(source().Take(3..2)); Assert.Empty(source().Take(6..^5)); @@ -1795,7 +1385,7 @@ public void NonEmptySource_DoNotThrowException() [Fact] public void NonEmptySource_DoNotThrowException_NotList() { - int[] source = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + int[] source = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; Assert.Empty(ForceNotCollection(source).Take(3..2)); Assert.Empty(ForceNotCollection(source).Take(6..^5)); @@ -1806,7 +1396,7 @@ public void NonEmptySource_DoNotThrowException_NotList() [Fact] public void NonEmptySource_DoNotThrowException_ListPartition() { - int[] source = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + int[] source = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; Assert.Empty(ListPartitionOrEmpty(source).Take(3..2)); Assert.Empty(ListPartitionOrEmpty(source).Take(6..^5)); @@ -1817,7 +1407,7 @@ public void NonEmptySource_DoNotThrowException_ListPartition() [Fact] public void NonEmptySource_DoNotThrowException_EnumerablePartition() { - int[] source = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + int[] source = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; Assert.Empty(EnumerablePartitionOrEmpty(source).Take(3..2)); Assert.Empty(EnumerablePartitionOrEmpty(source).Take(6..^5)); @@ -1828,7 +1418,7 @@ public void NonEmptySource_DoNotThrowException_EnumerablePartition() [Fact] public void EmptySource_DoNotThrowException() { - Func source = () => new int[] { }; + Func source = () => []; // Multiple elements in the middle. Assert.Empty(source().Take(^9..5)); @@ -1880,7 +1470,7 @@ public void EmptySource_DoNotThrowException() [Fact] public void EmptySource_DoNotThrowException_NotList() { - int[] source = { }; + int[] source = []; // Multiple elements in the middle. Assert.Empty(ForceNotCollection(source).Take(^9..5)); @@ -1932,7 +1522,7 @@ public void EmptySource_DoNotThrowException_NotList() [Fact] public void EmptySource_DoNotThrowException_ListPartition() { - int[] source = { }; + int[] source = []; // Multiple elements in the middle. Assert.Empty(ListPartitionOrEmpty(source).Take(^9..5)); @@ -1984,7 +1574,7 @@ public void EmptySource_DoNotThrowException_ListPartition() [Fact] public void EmptySource_DoNotThrowException_EnumerablePartition() { - int[] source = { }; + int[] source = []; // Multiple elements in the middle. Assert.Empty(EnumerablePartitionOrEmpty(source).Take(^9..5)); @@ -2033,7 +1623,7 @@ public void EmptySource_DoNotThrowException_EnumerablePartition() Assert.Empty(EnumerablePartitionOrEmpty(source).Take(^6..^7)); } - [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsSpeedOptimized))] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsLinqSpeedOptimized))] public void SkipTakeOnIListIsIList() { IList list = new ReadOnlyCollection(Enumerable.Range(0, 100).ToList()); diff --git a/tests/System.Linq.Tests/Tests/System.Linq/TakeWhileTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/TakeWhileTests.cs index 1744e1fa..0ad0aafc 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/TakeWhileTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/TakeWhileTests.cs @@ -11,8 +11,8 @@ public class TakeWhileTests : EnumerableTests [Fact] public void Empty() { - Assert.Equal(Enumerable.Empty(), Enumerable.Empty().TakeWhile(i => i < 40)); - Assert.Equal(Enumerable.Empty(), Enumerable.Empty().TakeWhile((i, index) => i < 40)); + Assert.Equal([], Enumerable.Empty().TakeWhile(i => i < 40)); + Assert.Equal([], Enumerable.Empty().TakeWhile((i, index) => i < 40)); } [Fact] @@ -50,22 +50,22 @@ public void SourceEmptyIndexed() [Fact] public void SourceNonEmptyPredicateFalseForAll() { - int[] source = { 9, 7, 15, 3, 11 }; + int[] source = [9, 7, 15, 3, 11]; Assert.Empty(source.TakeWhile(x => x % 2 == 0)); } [Fact] public void SourceNonEmptyPredicateFalseForAllWithIndex() { - int[] source = { 9, 7, 15, 3, 11 }; + int[] source = [9, 7, 15, 3, 11]; Assert.Empty(source.TakeWhile((x, i) => x % 2 == 0)); } [Fact] public void SourceNonEmptyPredicateTrueSomeFalseSecond() { - int[] source = { 8, 3, 12, 4, 6, 10 }; - int[] expected = { 8 }; + int[] source = [8, 3, 12, 4, 6, 10]; + int[] expected = [8]; Assert.Equal(expected, source.TakeWhile(x => x % 2 == 0)); } @@ -73,8 +73,8 @@ public void SourceNonEmptyPredicateTrueSomeFalseSecond() [Fact] public void SourceNonEmptyPredicateTrueSomeFalseSecondWithIndex() { - int[] source = { 8, 3, 12, 4, 6, 10 }; - int[] expected = { 8 }; + int[] source = [8, 3, 12, 4, 6, 10]; + int[] expected = [8]; Assert.Equal(expected, source.TakeWhile((x, i) => x % 2 == 0)); } @@ -82,22 +82,22 @@ public void SourceNonEmptyPredicateTrueSomeFalseSecondWithIndex() [Fact] public void SourceNonEmptyPredicateTrueSomeFalseFirst() { - int[] source = { 3, 2, 4, 12, 6 }; + int[] source = [3, 2, 4, 12, 6]; Assert.Empty(source.TakeWhile(x => x % 2 == 0)); } [Fact] public void SourceNonEmptyPredicateTrueSomeFalseFirstWithIndex() { - int[] source = { 3, 2, 4, 12, 6 }; + int[] source = [3, 2, 4, 12, 6]; Assert.Empty(source.TakeWhile((x, i) => x % 2 == 0)); } [Fact] public void FirstTakenByIndex() { - int[] source = { 6, 2, 5, 3, 8 }; - int[] expected = { 6 }; + int[] source = [6, 2, 5, 3, 8]; + int[] expected = [6]; Assert.Equal(expected, source.TakeWhile((element, index) => index == 0)); } @@ -105,8 +105,8 @@ public void FirstTakenByIndex() [Fact] public void AllButLastTakenByIndex() { - int[] source = { 6, 2, 5, 3, 8 }; - int[] expected = { 6, 2, 5, 3 }; + int[] source = [6, 2, 5, 3, 8]; + int[] expected = [6, 2, 5, 3]; Assert.Equal(expected, source.TakeWhile((element, index) => index < source.Length - 1)); } @@ -114,11 +114,11 @@ public void AllButLastTakenByIndex() [Fact] public void RunOnce() { - int[] source = { 8, 3, 12, 4, 6, 10 }; - int[] expected = { 8 }; + int[] source = [8, 3, 12, 4, 6, 10]; + int[] expected = [8]; Assert.Equal(expected, source.RunOnce().TakeWhile(x => x % 2 == 0)); - source = new[] { 6, 2, 5, 3, 8 }; - expected = new[] { 6, 2, 5, 3 }; + source = [6, 2, 5, 3, 8]; + expected = [6, 2, 5, 3]; Assert.Equal(expected, source.RunOnce().TakeWhile((element, index) => index < source.Length - 1)); } @@ -127,13 +127,13 @@ public void IndexTakeWhileOverflowBeyondIntMaxValueElements() { var taken = new FastInfiniteEnumerator().TakeWhile((e, i) => true); - using (var en = taken.GetEnumerator()) - Assert.Throws(() => + using var en = taken.GetEnumerator(); + Assert.Throws(() => + { + while (en.MoveNext()) { - while (en.MoveNext()) - { - } - }); + } + }); } [Fact] @@ -146,7 +146,7 @@ public void ThrowsOnNullSource() [Fact] public void ThrowsOnNullPredicate() { - int[] source = { 1, 2, 3 }; + int[] source = [1, 2, 3]; Func nullPredicate = null; AssertExtensions.Throws("predicate", () => source.TakeWhile(nullPredicate)); @@ -162,7 +162,7 @@ public void ThrowsOnNullSourceIndexed() [Fact] public void ThrowsOnNullPredicateIndexed() { - int[] source = { 1, 2, 3 }; + int[] source = [1, 2, 3]; Func nullPredicate = null; AssertExtensions.Throws("predicate", () => source.TakeWhile(nullPredicate)); diff --git a/tests/System.Linq.Tests/Tests/System.Linq/ThenByDescendingTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/ThenByDescendingTests.cs index 4af13bc5..46ed4df5 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/ThenByDescendingTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/ThenByDescendingTests.cs @@ -39,7 +39,7 @@ public void SameResultsRepeatCallsStringQuery() [Fact] public void SourceEmpty() { - int[] source = { }; + int[] source = []; Assert.Empty(source.OrderBy(e => e).ThenByDescending(e => e)); } @@ -95,7 +95,7 @@ public void OrderIsStable() var source = @"Because I could not stop for Death - He kindly stopped for me - The Carriage held but just Ourselves - -And Immortality.".Split(new[] { ' ', '\n', '\r', '-' }, StringSplitOptions.RemoveEmptyEntries); +And Immortality.".Split([' ', '\n', '\r', '-'], StringSplitOptions.RemoveEmptyEntries); var expected = new[] { "stopped", "kindly", "could", "stop", "held", "just", "not", "for", "for", "but", "me", @@ -111,7 +111,7 @@ public void OrderIsStableCustomComparer() var source = @"Because I could not stop for Death - He kindly stopped for me - The Carriage held but just Ourselves - -And Immortality.".Split(new[] { ' ', '\n', '\r', '-' }, StringSplitOptions.RemoveEmptyEntries); +And Immortality.".Split([' ', '\n', '\r', '-'], StringSplitOptions.RemoveEmptyEntries); var expected = new[] { "me", "not", "for", "for", "but", "stop", "held", "just", "could", "kindly", "stopped", @@ -127,7 +127,7 @@ public void RunOnce() var source = @"Because I could not stop for Death - He kindly stopped for me - The Carriage held but just Ourselves - -And Immortality.".Split(new[] { ' ', '\n', '\r', '-' }, StringSplitOptions.RemoveEmptyEntries); +And Immortality.".Split([' ', '\n', '\r', '-'], StringSplitOptions.RemoveEmptyEntries); var expected = new[] { "me", "not", "for", "for", "but", "stop", "held", "just", "could", "kindly", "stopped", diff --git a/tests/System.Linq.Tests/Tests/System.Linq/ThenByTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/ThenByTests.cs index 5d8ec042..bb1618cb 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/ThenByTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/ThenByTests.cs @@ -40,7 +40,7 @@ public void SameResultsRepeatCallsStringQuery() [Fact] public void SourceEmpty() { - int[] source = { }; + int[] source = []; Assert.Empty(source.OrderBy(e => e).ThenBy(e => e)); } @@ -116,7 +116,7 @@ public void OrderIsStable() var source = @"Because I could not stop for Death - He kindly stopped for me - The Carriage held but just Ourselves - -And Immortality.".Split(new[] { ' ', '\n', '\r', '-' }, StringSplitOptions.RemoveEmptyEntries); +And Immortality.".Split([' ', '\n', '\r', '-'], StringSplitOptions.RemoveEmptyEntries); var expected = new[] { "me", "not", "for", "for", "but", "stop", "held", "just", "could", "kindly", "stopped", @@ -132,7 +132,7 @@ public void RunOnce() var source = @"Because I could not stop for Death - He kindly stopped for me - The Carriage held but just Ourselves - -And Immortality.".Split(new[] { ' ', '\n', '\r', '-' }, StringSplitOptions.RemoveEmptyEntries); +And Immortality.".Split([' ', '\n', '\r', '-'], StringSplitOptions.RemoveEmptyEntries); var expected = new[] { "me", "not", "for", "for", "but", "stop", "held", "just", "could", "kindly", "stopped", diff --git a/tests/System.Linq.Tests/Tests/System.Linq/ToArrayTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/ToArrayTests.cs index b7cb8377..e6f5751d 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/ToArrayTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/ToArrayTests.cs @@ -18,7 +18,7 @@ public class ToArrayTests : EnumerableTests [Fact] public void ToArray_CreateACopyWhenNotEmpty() { - int[] sourceArray = new int[] { 1, 2, 3, 4, 5 }; + int[] sourceArray = [1, 2, 3, 4, 5]; int[] resultArray = sourceArray.ToArray(); Assert.NotSame(sourceArray, resultArray); @@ -28,7 +28,7 @@ public void ToArray_CreateACopyWhenNotEmpty() [Fact] public void ToArray_UseArrayEmptyWhenEmpty() { - int[] emptySourceArray = Array.Empty(); + int[] emptySourceArray = []; Assert.Same(emptySourceArray.ToArray(), emptySourceArray.ToArray()); @@ -71,7 +71,7 @@ public void ToArray_WorkWithEmptyCollection() [Fact] public void ToArray_ProduceCorrectArray() { - int[] sourceArray = new int[] { 1, 2, 3, 4, 5, 6, 7 }; + int[] sourceArray = [1, 2, 3, 4, 5, 6, 7]; RunToArrayOnAllCollectionTypes(sourceArray, resultArray => { @@ -79,7 +79,7 @@ public void ToArray_ProduceCorrectArray() Assert.Equal(sourceArray, resultArray); }); - string[] sourceStringArray = new string[] { "1", "2", "3", "4", "5", "6", "7", "8" }; + string[] sourceStringArray = ["1", "2", "3", "4", "5", "6", "7", "8"]; RunToArrayOnAllCollectionTypes(sourceStringArray, resultStringArray => { @@ -101,7 +101,7 @@ public void RunOnce() [Fact] public void ToArray_TouchCountWithICollection() { - TestCollection source = new TestCollection(new int[] { 1, 2, 3, 4 }); + TestCollection source = new TestCollection([1, 2, 3, 4]); var resultArray = source.ToArray(); Assert.Equal(source, resultArray); @@ -120,7 +120,7 @@ public void ToArray_ThrowArgumentNullExceptionWhenSourceIsNull() [Fact] public void ToArray_UseCopyToWithICollection() { - TestCollection source = new TestCollection(new int[] { 1, 2, 3, 4 }); + TestCollection source = new TestCollection([1, 2, 3, 4]); var resultArray = source.ToArray(); Assert.Equal(source, resultArray); @@ -225,7 +225,7 @@ public void EmptyArraysSameObject() [Fact] public void SourceIsEmptyICollectionT() { - int[] source = { }; + int[] source = []; ICollection collection = source as ICollection; @@ -236,8 +236,8 @@ public void SourceIsEmptyICollectionT() [Fact] public void SourceIsICollectionTWithFewElements() { - int?[] source = { -5, null, 0, 10, 3, -1, null, 4, 9 }; - int?[] expected = { -5, null, 0, 10, 3, -1, null, 4, 9 }; + int?[] source = [-5, null, 0, 10, 3, -1, null, 4, 9]; + int?[] expected = [-5, null, 0, 10, 3, -1, null, 4, 9]; ICollection collection = source as ICollection; @@ -259,7 +259,7 @@ public void SourceNotICollectionAndIsEmpty() public void SourceNotICollectionAndHasElements() { IEnumerable source = NumberRangeGuaranteedNotCollectionType(-4, 10); - int[] expected = { -4, -3, -2, -1, 0, 1, 2, 3, 4, 5 }; + int[] expected = [-4, -3, -2, -1, 0, 1, 2, 3, 4, 5]; Assert.Null(source as ICollection); @@ -270,7 +270,7 @@ public void SourceNotICollectionAndHasElements() public void SourceNotICollectionAndAllNull() { IEnumerable source = RepeatedNullableNumberGuaranteedNotCollectionType(null, 5); - int?[] expected = { null, null, null, null, null }; + int?[] expected = [null, null, null, null, null]; Assert.Null(source as ICollection); @@ -363,25 +363,25 @@ private enum Enum1 [Fact] public void ToArray_Cast() { - Enum0[] source = { Enum0.First, Enum0.Second, Enum0.Third }; + Enum0[] source = [Enum0.First, Enum0.Second, Enum0.Third]; var cast = source.Cast(); Assert.IsType(cast); var castArray = cast.ToArray(); Assert.IsType(castArray); - Assert.Equal(new[] { Enum1.First, Enum1.Second, Enum1.Third }, castArray); + Assert.Equal([Enum1.First, Enum1.Second, Enum1.Third], castArray); } public static IEnumerable ToArrayShouldWorkWithSpecialLengthLazyEnumerables_MemberData() { // Return array sizes that should be small enough not to OOM int MaxPower = PlatformDetection.IsBrowser ? 15 : 18; - yield return new object[] { 1 }; - yield return new object[] { 2 }; + yield return [1]; + yield return [2]; for (int i = 2; i <= MaxPower; i++) { - yield return new object[] { (i << i) - 1 }; - yield return new object[] { (i << i) }; - yield return new object[] { (i << i) + 1 }; + yield return [(i << i) - 1]; + yield return [(i << i)]; + yield return [(i << i) + 1]; } } } diff --git a/tests/System.Linq.Tests/Tests/System.Linq/ToDictionaryTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/ToDictionaryTests.cs index b02cd317..c56a0c86 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/ToDictionaryTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/ToDictionaryTests.cs @@ -86,7 +86,7 @@ public void ToDictionaryFromKv_WorkWithEmptyCollection() [Fact] public void ToDictionary_ProduceCorrectDictionary() { - int[] sourceArray = new int[] { 1, 2, 3, 4, 5, 6, 7 }; + int[] sourceArray = [1, 2, 3, 4, 5, 6, 7]; RunToDictionaryOnAllCollectionTypes(sourceArray, resultDictionary => { @@ -95,7 +95,7 @@ public void ToDictionary_ProduceCorrectDictionary() Assert.Equal(sourceArray, resultDictionary.Values); }); - string[] sourceStringArray = new string[] { "1", "2", "3", "4", "5", "6", "7", "8" }; + string[] sourceStringArray = ["1", "2", "3", "4", "5", "6", "7", "8"]; RunToDictionaryOnAllCollectionTypes(sourceStringArray, resultDictionary => { @@ -108,7 +108,7 @@ public void ToDictionary_ProduceCorrectDictionary() [Fact] public void ToDictionaryFromKv_ProduceCorrectDictionary() { - int[] sourceArray = new[] { 1, 2, 3, 4, 5, 6, 7 }; + int[] sourceArray = [1, 2, 3, 4, 5, 6, 7]; RunToDictionaryFromKvOnAllCollectionTypes(sourceArray, resultDictionary => { @@ -117,7 +117,7 @@ public void ToDictionaryFromKv_ProduceCorrectDictionary() Assert.Equal(sourceArray, resultDictionary.Values); }); - string[] sourceStringArray = new[] { "1", "2", "3", "4", "5", "6", "7", "8" }; + string[] sourceStringArray = ["1", "2", "3", "4", "5", "6", "7", "8"]; RunToDictionaryFromKvOnAllCollectionTypes(sourceStringArray, resultDictionary => { @@ -149,7 +149,7 @@ public void RunOnce() public void ToDictionary_PassCustomComparer() { EqualityComparer comparer = EqualityComparer.Create((x, y) => x == y, x => x); - TestCollection collection = new TestCollection(new int[] { 1, 2, 3, 4, 5, 6 }); + TestCollection collection = new TestCollection([1, 2, 3, 4, 5, 6]); Dictionary result1 = collection.ToDictionary(key => key, comparer); Assert.Same(comparer, result1.Comparer); @@ -167,7 +167,7 @@ public void ToDictionary_PassCustomComparer() [Fact] public void ToDictionary_UseDefaultComparerOnNull() { - TestCollection collection = new TestCollection(new int[] { 1, 2, 3, 4, 5, 6 }); + TestCollection collection = new TestCollection([1, 2, 3, 4, 5, 6]); Dictionary result1 = collection.ToDictionary(key => key, comparer: null); Assert.Same(EqualityComparer.Default, result1.Comparer); @@ -179,7 +179,7 @@ public void ToDictionary_UseDefaultComparerOnNull() [Fact] public void ToDictionary_UseDefaultComparer() { - TestCollection collection = new TestCollection(new[] { 1, 2, 3, 4, 5, 6 }); + TestCollection collection = new TestCollection([1, 2, 3, 4, 5, 6]); Dictionary result1 = collection.ToDictionary(key => key); Assert.Same(EqualityComparer.Default, result1.Comparer); @@ -197,7 +197,7 @@ public void ToDictionary_UseDefaultComparer() [Fact] public void ToDictionary_KeyValueSelectorsWork() { - TestCollection collection = new TestCollection(new int[] { 1, 2, 3, 4, 5, 6 }); + TestCollection collection = new TestCollection([1, 2, 3, 4, 5, 6]); Dictionary result = collection.ToDictionary(key => key + 10, val => val + 100); @@ -218,7 +218,7 @@ public void ToDictionary_ThrowArgumentNullExceptionWhenSourceIsNull() [Fact] public void ToDictionary_ThrowArgumentNullExceptionWhenKeySelectorIsNull() { - int[] source = new int[0]; + int[] source = []; Func keySelector = null; AssertExtensions.Throws("keySelector", () => source.ToDictionary(keySelector)); } @@ -226,7 +226,7 @@ public void ToDictionary_ThrowArgumentNullExceptionWhenKeySelectorIsNull() [Fact] public void ToDictionary_ThrowArgumentNullExceptionWhenValueSelectorIsNull() { - int[] source = new int[0]; + int[] source = []; Func keySelector = key => key; Func valueSelector = null; AssertExtensions.Throws("elementSelector", () => source.ToDictionary(keySelector, valueSelector)); @@ -243,7 +243,7 @@ public void ToDictionary_ThrowArgumentNullExceptionWhenSourceIsNullElementSelect [Fact] public void ToDictionary_ThrowArgumentNullExceptionWhenKeySelectorIsNullElementSelector() { - int[] source = new int[0]; + int[] source = []; Func keySelector = null; AssertExtensions.Throws("keySelector", () => source.ToDictionary(keySelector, e => e)); } @@ -251,7 +251,7 @@ public void ToDictionary_ThrowArgumentNullExceptionWhenKeySelectorIsNullElementS [Fact] public void ToDictionary_KeySelectorThrowException() { - int[] source = new int[] { 1, 2, 3 }; + int[] source = [1, 2, 3]; Func keySelector = key => { if (key == 1) @@ -266,7 +266,7 @@ public void ToDictionary_KeySelectorThrowException() [Fact] public void ToDictionary_ThrowWhenKeySelectorReturnNull() { - int[] source = new int[] { 1, 2, 3 }; + int[] source = [1, 2, 3]; Func keySelector = key => null; AssertExtensions.Throws("key", () => source.ToDictionary(keySelector)); @@ -275,7 +275,7 @@ public void ToDictionary_ThrowWhenKeySelectorReturnNull() [Fact] public void ToDictionary_ThrowWhenKeySelectorReturnSameValueTwice() { - int[] source = new int[] { 1, 2, 3 }; + int[] source = [1, 2, 3]; Func keySelector = key => 1; AssertExtensions.Throws(null, () => source.ToDictionary(keySelector)); @@ -284,7 +284,7 @@ public void ToDictionary_ThrowWhenKeySelectorReturnSameValueTwice() [Fact] public void ToDictionary_ValueSelectorThrowException() { - int[] source = new int[] { 1, 2, 3 }; + int[] source = [1, 2, 3]; Func keySelector = key => key; Func valueSelector = value => { @@ -308,12 +308,12 @@ public void ThrowsOnNullKey() source.ToDictionary(e => e.Name); // Doesn't throw; - source = new[] - { + source = + [ new { Name = "Chris", Score = 50 }, new { Name = "Bob", Score = 95 }, new { Name = default(string), Score = 55 } - }; + ]; AssertExtensions.Throws("key", () => source.ToDictionary(e => e.Name)); @@ -348,12 +348,12 @@ public void ThrowsOnNullKeyCustomComparer() source.ToDictionary(e => e.Name, new AnagramEqualityComparer()); // Doesn't throw; - source = new[] - { + source = + [ new { Name = "Chris", Score = 50 }, new { Name = "Bob", Score = 95 }, new { Name = default(string), Score = 55 } - }; + ]; AssertExtensions.Throws("key", () => source.ToDictionary(e => e.Name, new AnagramEqualityComparer())); } @@ -370,12 +370,12 @@ public void ThrowsOnNullKeyValueSelector() source.ToDictionary(e => e.Name, e => e); // Doesn't throw; - source = new[] - { + source = + [ new { Name = "Chris", Score = 50 }, new { Name = "Bob", Score = 95 }, new { Name = default(string), Score = 55 } - }; + ]; AssertExtensions.Throws("key", () => source.ToDictionary(e => e.Name, e => e)); } @@ -392,12 +392,12 @@ public void ThrowsOnNullKeyCustomComparerValueSelector() source.ToDictionary(e => e.Name, e => e, new AnagramEqualityComparer()); // Doesn't throw; - source = new[] - { + source = + [ new { Name = "Chris", Score = 50 }, new { Name = "Bob", Score = 95 }, new { Name = default(string), Score = 55 } - }; + ]; AssertExtensions.Throws("key", () => source.ToDictionary(e => e.Name, e => e, new AnagramEqualityComparer())); } @@ -438,27 +438,25 @@ private static void AssertMatches(IEnumerable keys, IEnumerable valu Assert.NotNull(dict); Assert.NotNull(keys); Assert.NotNull(values); - using (var ke = keys.GetEnumerator()) + using var ke = keys.GetEnumerator(); + foreach (var value in values) { - foreach (var value in values) - { - Assert.True(ke.MoveNext()); - var key = ke.Current; - E dictValue; - Assert.True(dict.TryGetValue(key, out dictValue)); - Assert.Equal(value, dictValue); - dict.Remove(key); - } - Assert.False(ke.MoveNext()); - Assert.Empty(dict); + Assert.True(ke.MoveNext()); + var key = ke.Current; + E dictValue; + Assert.True(dict.TryGetValue(key, out dictValue)); + Assert.Equal(value, dictValue); + dict.Remove(key); } + Assert.False(ke.MoveNext()); + Assert.Empty(dict); } [Fact] public void EmptySource() { - int[] elements = new int[] { }; - string[] keys = new string[] { }; + int[] elements = []; + string[] keys = []; var source = keys.Zip(elements, (k, e) => new { Name = k, Score = e }); AssertMatches(keys, elements, source.ToDictionary(e => e.Name, e => e.Score, new AnagramEqualityComparer())); @@ -467,8 +465,8 @@ public void EmptySource() [Fact] public void OneElementNullComparer() { - int[] elements = new int[] { 5 }; - string[] keys = new string[] { "Bob" }; + int[] elements = [5]; + string[] keys = ["Bob"]; var source = new[] { new { Name = keys[0], Score = elements[0] } }; AssertMatches(keys, elements, source.ToDictionary(e => e.Name, e => e.Score, null)); @@ -477,7 +475,7 @@ public void OneElementNullComparer() [Fact] public void SeveralElementsCustomComparerer() { - string[] keys = new string[] { "Bob", "Zen", "Prakash", "Chris", "Sachin" }; + string[] keys = ["Bob", "Zen", "Prakash", "Chris", "Sachin"]; var source = new[] { new { Name = "Bbo", Score = 95 }, @@ -493,9 +491,9 @@ public void SeveralElementsCustomComparerer() [Fact] public void NullCoalescedKeySelector() { - string[] elements = new string[] { null }; - string[] keys = new string[] { string.Empty }; - string[] source = new string[] { null }; + string[] elements = [null]; + string[] keys = [string.Empty]; + string[] source = [null]; AssertMatches(keys, elements, source.ToDictionary(e => e ?? string.Empty, e => e, EqualityComparer.Default)); diff --git a/tests/System.Linq.Tests/Tests/System.Linq/ToListTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/ToListTests.cs index ba31b821..8b1d2316 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/ToListTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/ToListTests.cs @@ -16,7 +16,7 @@ public class ToListTests : EnumerableTests [Fact] public void ToList_AlwaysCreateACopy() { - List sourceList = new List() { 1, 2, 3, 4, 5 }; + List sourceList = [1, 2, 3, 4, 5]; List resultList = sourceList.ToList(); Assert.NotSame(sourceList, resultList); @@ -48,7 +48,7 @@ public void ToList_WorkWithEmptyCollection() [Fact] public void ToList_ProduceCorrectList() { - int[] sourceArray = new int[] { 1, 2, 3, 4, 5, 6, 7 }; + int[] sourceArray = [1, 2, 3, 4, 5, 6, 7]; RunToListOnAllCollectionTypes(sourceArray, resultList => { @@ -56,7 +56,7 @@ public void ToList_ProduceCorrectList() Assert.Equal(sourceArray, resultList); }); - string[] sourceStringArray = new string[] { "1", "2", "3", "4", "5", "6", "7", "8" }; + string[] sourceStringArray = ["1", "2", "3", "4", "5", "6", "7", "8"]; RunToListOnAllCollectionTypes(sourceStringArray, resultStringList => { @@ -75,7 +75,7 @@ public void RunOnce() [Fact] public void ToList_TouchCountWithICollection() { - TestCollection source = new TestCollection(new int[] { 1, 2, 3, 4 }); + TestCollection source = new TestCollection([1, 2, 3, 4]); var resultList = source.ToList(); Assert.Equal(source, resultList); @@ -94,7 +94,7 @@ public void ToList_ThrowArgumentNullExceptionWhenSourceIsNull() [Fact] public void ToList_UseCopyToWithICollection() { - TestCollection source = new TestCollection(new int[] { 1, 2, 3, 4 }); + TestCollection source = new TestCollection([1, 2, 3, 4]); var resultList = source.ToList(); Assert.Equal(source, resultList); @@ -193,7 +193,7 @@ public void SameResultsRepeatCallsFromWhereOnStringQuery() [Fact] public void SourceIsEmptyICollectionT() { - int[] source = { }; + int[] source = []; ICollection collection = source as ICollection; @@ -204,8 +204,8 @@ public void SourceIsEmptyICollectionT() [Fact] public void SourceIsICollectionTWithFewElements() { - int?[] source = { -5, null, 0, 10, 3, -1, null, 4, 9 }; - int?[] expected = { -5, null, 0, 10, 3, -1, null, 4, 9 }; + int?[] source = [-5, null, 0, 10, 3, -1, null, 4, 9]; + int?[] expected = [-5, null, 0, 10, 3, -1, null, 4, 9]; ICollection collection = source as ICollection; @@ -224,7 +224,7 @@ public void SourceNotICollectionAndIsEmpty() public void SourceNotICollectionAndHasElements() { IEnumerable source = NumberRangeGuaranteedNotCollectionType(-4, 10); - int[] expected = { -4, -3, -2, -1, 0, 1, 2, 3, 4, 5 }; + int[] expected = [-4, -3, -2, -1, 0, 1, 2, 3, 4, 5]; Assert.Null(source as ICollection); @@ -235,7 +235,7 @@ public void SourceNotICollectionAndHasElements() public void SourceNotICollectionAndAllNull() { IEnumerable source = RepeatedNullableNumberGuaranteedNotCollectionType(null, 5); - int?[] expected = { null, null, null, null, null }; + int?[] expected = [null, null, null, null, null]; Assert.Null(source as ICollection); diff --git a/tests/System.Linq.Tests/Tests/System.Linq/ToLookupTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/ToLookupTests.cs index f9d97e63..c6f31721 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/ToLookupTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/ToLookupTests.cs @@ -54,7 +54,7 @@ from x4 in q2 [Fact] public void Empty() { - AssertMatches(Enumerable.Empty(), Enumerable.Empty(), Enumerable.Empty().ToLookup(i => i)); + AssertMatches([], [], Enumerable.Empty().ToLookup(i => i)); Assert.False(Enumerable.Empty().ToLookup(i => i).Contains(0)); Assert.Empty(Enumerable.Empty().ToLookup(i => i)[0]); } @@ -62,8 +62,8 @@ public void Empty() [Fact] public void NullKeyIncluded() { - string[] key = { "Chris", "Bob", null, "Tim" }; - int[] element = { 50, 95, 55, 90 }; + string[] key = ["Chris", "Bob", null, "Tim"]; + int[] element = [50, 95, 55, 90]; var source = key.Zip(element, (k, e) => new { Name = k, Score = e }); AssertMatches(key, source, source.ToLookup(e => e.Name)); @@ -72,8 +72,8 @@ public void NullKeyIncluded() [Fact] public void OneElementCustomComparer() { - string[] key = { "Chris" }; - int[] element = { 50 }; + string[] key = ["Chris"]; + int[] element = [50]; var source = new[] { new { Name = "risCh", Score = 50 } }; AssertMatches(key, source, source.ToLookup(e => e.Name, new AnagramEqualityComparer())); @@ -82,8 +82,8 @@ public void OneElementCustomComparer() [Fact] public void UniqueElementsElementSelector() { - string[] key = { "Chris", "Prakash", "Tim", "Robert", "Brian" }; - int[] element = { 50, 100, 95, 60, 80 }; + string[] key = ["Chris", "Prakash", "Tim", "Robert", "Brian"]; + int[] element = [50, 100, 95, 60, 80]; var source = new[] { new { Name = key[0], Score = element[0] }, @@ -99,8 +99,8 @@ public void UniqueElementsElementSelector() [Fact] public void DuplicateKeys() { - string[] key = { "Chris", "Prakash", "Robert" }; - int[] element = { 50, 80, 100, 95, 99, 56 }; + string[] key = ["Chris", "Prakash", "Robert"]; + int[] element = [50, 80, 100, 95, 99, 56]; var source = new[] { new { Name = key[0], Score = element[0] }, @@ -117,8 +117,8 @@ public void DuplicateKeys() [Fact] public void RunOnce() { - string[] key = { "Chris", "Prakash", "Robert" }; - int[] element = { 50, 80, 100, 95, 99, 56 }; + string[] key = ["Chris", "Prakash", "Robert"]; + int[] element = [50, 80, 100, 95, 99, 56]; var source = new[] { new { Name = key[0], Score = element[0] }, @@ -135,8 +135,8 @@ public void RunOnce() [Fact] public void Count() { - string[] key = { "Chris", "Prakash", "Robert" }; - int[] element = { 50, 80, 100, 95, 99, 56 }; + string[] key = ["Chris", "Prakash", "Robert"]; + int[] element = [50, 80, 100, 95, 99, 56]; var source = new[] { new { Name = key[0], Score = element[0] }, @@ -153,8 +153,8 @@ public void Count() [Fact] public void EmptySource() { - string[] key = { }; - int[] element = { }; + string[] key = []; + int[] element = []; var source = key.Zip(element, (k, e) => new { Name = k, Score = e }); AssertMatches(key, element, source.ToLookup(e => e.Name, e => e.Score, new AnagramEqualityComparer())); @@ -163,9 +163,9 @@ public void EmptySource() [Fact] public void SingleNullKeyAndElement() { - string[] key = { null }; - string[] element = { null }; - string[] source = new string[] { null }; + string[] key = [null]; + string[] element = [null]; + string[] source = [null]; AssertMatches(key, element, source.ToLookup(e => e, e => e, EqualityComparer.Default)); } diff --git a/tests/System.Linq.Tests/Tests/System.Linq/UnionTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/UnionTests.cs index f1a45eff..7e3f953a 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/UnionTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/UnionTests.cs @@ -74,27 +74,27 @@ public void SameResultsRepeatCallsMultipleUnions() [Fact] public void BothEmpty() { - int[] first = { }; - int[] second = { }; + int[] first = []; + int[] second = []; Assert.Empty(first.Union(second)); } [Fact] public void ManyEmpty() { - int[] first = { }; - int[] second = { }; - int[] third = { }; - int[] fourth = { }; + int[] first = []; + int[] second = []; + int[] third = []; + int[] fourth = []; Assert.Empty(first.Union(second).Union(third).Union(fourth)); } [Fact] public void CustomComparer() { - string[] first = { "Bob", "Robert", "Tim", "Matt", "miT" }; - string[] second = { "ttaM", "Charlie", "Bbo" }; - string[] expected = { "Bob", "Robert", "Tim", "Matt", "Charlie" }; + string[] first = ["Bob", "Robert", "Tim", "Matt", "miT"]; + string[] second = ["ttaM", "Charlie", "Bbo"]; + string[] expected = ["Bob", "Robert", "Tim", "Matt", "Charlie"]; var comparer = new AnagramEqualityComparer(); @@ -104,9 +104,9 @@ public void CustomComparer() [Fact] public void RunOnce() { - string[] first = { "Bob", "Robert", "Tim", "Matt", "miT" }; - string[] second = { "ttaM", "Charlie", "Bbo" }; - string[] expected = { "Bob", "Robert", "Tim", "Matt", "Charlie" }; + string[] first = ["Bob", "Robert", "Tim", "Matt", "miT"]; + string[] second = ["ttaM", "Charlie", "Bbo"]; + string[] expected = ["Bob", "Robert", "Tim", "Matt", "Charlie"]; var comparer = new AnagramEqualityComparer(); Assert.Equal(expected, first.RunOnce().Union(second.RunOnce(), comparer), comparer); @@ -116,7 +116,7 @@ public void RunOnce() public void FirstNullCustomComparer() { string[] first = null; - string[] second = { "ttaM", "Charlie", "Bbo" }; + string[] second = ["ttaM", "Charlie", "Bbo"]; var ane = AssertExtensions.Throws("first", () => first.Union(second, new AnagramEqualityComparer())); } @@ -124,7 +124,7 @@ public void FirstNullCustomComparer() [Fact] public void SecondNullCustomComparer() { - string[] first = { "Bob", "Robert", "Tim", "Matt", "miT" }; + string[] first = ["Bob", "Robert", "Tim", "Matt", "miT"]; string[] second = null; var ane = AssertExtensions.Throws("second", () => first.Union(second, new AnagramEqualityComparer())); @@ -134,7 +134,7 @@ public void SecondNullCustomComparer() public void FirstNullNoComparer() { string[] first = null; - string[] second = { "ttaM", "Charlie", "Bbo" }; + string[] second = ["ttaM", "Charlie", "Bbo"]; var ane = AssertExtensions.Throws("first", () => first.Union(second)); } @@ -142,7 +142,7 @@ public void FirstNullNoComparer() [Fact] public void SecondNullNoComparer() { - string[] first = { "Bob", "Robert", "Tim", "Matt", "miT" }; + string[] first = ["Bob", "Robert", "Tim", "Matt", "miT"]; string[] second = null; var ane = AssertExtensions.Throws("second", () => first.Union(second)); @@ -151,9 +151,9 @@ public void SecondNullNoComparer() [Fact] public void SingleNullWithEmpty() { - string[] first = { null }; - string[] second = new string[0]; - string[] expected = { null }; + string[] first = [null]; + string[] second = []; + string[] expected = [null]; Assert.Equal(expected, first.Union(second, EqualityComparer.Default)); } @@ -161,9 +161,9 @@ public void SingleNullWithEmpty() [Fact] public void NullEmptyStringMix() { - string[] first = { null, null, string.Empty }; - string[] second = { null, null }; - string[] expected = { null, string.Empty }; + string[] first = [null, null, string.Empty]; + string[] second = [null, null]; + string[] expected = [null, string.Empty]; Assert.Equal(expected, first.Union(second, EqualityComparer.Default)); } @@ -171,9 +171,9 @@ public void NullEmptyStringMix() [Fact] public void DoubleNullWithEmpty() { - string[] first = { null, null }; - string[] second = new string[0]; - string[] expected = { null }; + string[] first = [null, null]; + string[] second = []; + string[] expected = [null]; Assert.Equal(expected, first.Union(second, EqualityComparer.Default)); } @@ -181,9 +181,9 @@ public void DoubleNullWithEmpty() [Fact] public void EmptyWithNonEmpty() { - int[] first = { }; - int[] second = { 2, 4, 5, 3, 2, 3, 9 }; - int[] expected = { 2, 4, 5, 3, 9 }; + int[] first = []; + int[] second = [2, 4, 5, 3, 2, 3, 9]; + int[] expected = [2, 4, 5, 3, 9]; Assert.Equal(expected, first.Union(second)); } @@ -191,9 +191,9 @@ public void EmptyWithNonEmpty() [Fact] public void NonEmptyWithEmpty() { - int[] first = { 2, 4, 5, 3, 2, 3, 9 }; - int[] second = { }; - int[] expected = { 2, 4, 5, 3, 9 }; + int[] first = [2, 4, 5, 3, 2, 3, 9]; + int[] second = []; + int[] expected = [2, 4, 5, 3, 9]; Assert.Equal(expected, first.Union(second)); } @@ -201,9 +201,9 @@ public void NonEmptyWithEmpty() [Fact] public void CommonElementsShared() { - int[] first = { 1, 2, 3, 4, 5, 6 }; - int[] second = { 6, 7, 7, 7, 8, 1 }; - int[] expected = { 1, 2, 3, 4, 5, 6, 7, 8 }; + int[] first = [1, 2, 3, 4, 5, 6]; + int[] second = [6, 7, 7, 7, 8, 1]; + int[] expected = [1, 2, 3, 4, 5, 6, 7, 8]; Assert.Equal(expected, first.Union(second)); } @@ -211,9 +211,9 @@ public void CommonElementsShared() [Fact] public void SameElementRepeated() { - int[] first = { 1, 1, 1, 1, 1, 1 }; - int[] second = { 1, 1, 1, 1, 1, 1 }; - int[] expected = { 1 }; + int[] first = [1, 1, 1, 1, 1, 1]; + int[] second = [1, 1, 1, 1, 1, 1]; + int[] expected = [1]; Assert.Equal(expected, first.Union(second)); } @@ -221,9 +221,9 @@ public void SameElementRepeated() [Fact] public void RepeatedElementsWithSingleElement() { - int[] first = { 1, 2, 3, 5, 3, 6 }; - int[] second = { 7 }; - int[] expected = { 1, 2, 3, 5, 6, 7 }; + int[] first = [1, 2, 3, 5, 3, 6]; + int[] second = [7]; + int[] expected = [1, 2, 3, 5, 6, 7]; Assert.Equal(expected, first.Union(second)); } @@ -231,9 +231,9 @@ public void RepeatedElementsWithSingleElement() [Fact] public void SingleWithAllUnique() { - int?[] first = { 2 }; - int?[] second = { 3, null, 4, 5 }; - int?[] expected = { 2, 3, null, 4, 5 }; + int?[] first = [2]; + int?[] second = [3, null, 4, 5]; + int?[] expected = [2, 3, null, 4, 5]; Assert.Equal(expected, first.Union(second)); } @@ -241,9 +241,9 @@ public void SingleWithAllUnique() [Fact] public void EachHasRepeatsBetweenAndAmongstThemselves() { - int?[] first = { 1, 2, 3, 4, null, 5, 1 }; - int?[] second = { 6, 2, 3, 4, 5, 6 }; - int?[] expected = { 1, 2, 3, 4, null, 5, 6 }; + int?[] first = [1, 2, 3, 4, null, 5, 1]; + int?[] second = [6, 2, 3, 4, 5, 6]; + int?[] expected = [1, 2, 3, 4, null, 5, 6]; Assert.Equal(expected, first.Union(second)); } @@ -251,11 +251,11 @@ public void EachHasRepeatsBetweenAndAmongstThemselves() [Fact] public void EachHasRepeatsBetweenAndAmongstThemselvesMultipleUnions() { - int?[] first = { 1, 2, 3, 4, null, 5, 1 }; - int?[] second = { 6, 2, 3, 4, 5, 6 }; - int?[] third = { 2, 8, 2, 3, 2, 8 }; - int?[] fourth = { null, 1, 7, 2, 7 }; - int?[] expected = { 1, 2, 3, 4, null, 5, 6, 8, 7 }; + int?[] first = [1, 2, 3, 4, null, 5, 1]; + int?[] second = [6, 2, 3, 4, 5, 6]; + int?[] third = [2, 8, 2, 3, 2, 8]; + int?[] fourth = [null, 1, 7, 2, 7]; + int?[] expected = [1, 2, 3, 4, null, 5, 6, 8, 7]; Assert.Equal(expected, first.Union(second).Union(third).Union(fourth)); } @@ -263,11 +263,11 @@ public void EachHasRepeatsBetweenAndAmongstThemselvesMultipleUnions() [Fact] public void MultipleUnionsCustomComparer() { - int?[] first = { 1, 102, 903, 204, null, 5, 601 }; - int?[] second = { 6, 202, 903, 204, 5, 106 }; - int?[] third = { 2, 308, 2, 103, 802, 308 }; - int?[] fourth = { null, 101, 207, 202, 207 }; - int?[] expected = { 1, 102, 903, 204, null, 5, 6, 308, 207 }; + int?[] first = [1, 102, 903, 204, null, 5, 601]; + int?[] second = [6, 202, 903, 204, 5, 106]; + int?[] third = [2, 308, 2, 103, 802, 308]; + int?[] fourth = [null, 101, 207, 202, 207]; + int?[] expected = [1, 102, 903, 204, null, 5, 6, 308, 207]; Assert.Equal(expected, first.Union(second, new Modulo100EqualityComparer()).Union(third, new Modulo100EqualityComparer()).Union(fourth, new Modulo100EqualityComparer())); } @@ -275,12 +275,13 @@ public void MultipleUnionsCustomComparer() [Fact] public void MultipleUnionsDifferentComparers() { - string[] first = { "Alpha", "Bravo", "Charlie", "Bravo", "Delta", "atleD", "ovarB" }; - string[] second = { "Charlie", "Delta", "Echo", "Foxtrot", "Foxtrot", "choE" }; - string[] third = { "trotFox", "Golf", "Alpha", "choE", "Tango" }; + string[] first = ["Alpha", "Bravo", "Charlie", "Bravo", "Delta", "atleD", "ovarB"]; + string[] second = ["Charlie", "Delta", "Echo", "Foxtrot", "Foxtrot", "choE"]; + string[] third = ["trotFox", "Golf", "Alpha", "choE", "Tango"]; - string[] plainThenAnagram = { "Alpha", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "Golf", "Tango" }; - string[] anagramThenPlain = { "Alpha", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "trotFox", "Golf", "choE", "Tango" }; + string[] plainThenAnagram = ["Alpha", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "Golf", "Tango"]; + string[] anagramThenPlain = ["Alpha", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "trotFox", "Golf", "choE", "Tango" + ]; Assert.Equal(plainThenAnagram, first.Union(second).Union(third, new AnagramEqualityComparer())); Assert.Equal(anagramThenPlain, first.Union(second, new AnagramEqualityComparer()).Union(third)); @@ -289,9 +290,9 @@ public void MultipleUnionsDifferentComparers() [Fact] public void NullEqualityComparer() { - string[] first = { "Bob", "Robert", "Tim", "Matt", "miT" }; - string[] second = { "ttaM", "Charlie", "Bbo" }; - string[] expected = { "Bob", "Robert", "Tim", "Matt", "miT", "ttaM", "Charlie", "Bbo" }; + string[] first = ["Bob", "Robert", "Tim", "Matt", "miT"]; + string[] second = ["ttaM", "Charlie", "Bbo"]; + string[] expected = ["Bob", "Robert", "Tim", "Matt", "miT", "ttaM", "Charlie", "Bbo"]; Assert.Equal(expected, first.Union(second, null)); } @@ -308,7 +309,10 @@ public void ForcedToEnumeratorDoesntEnumerate() [Fact] public void ForcedToEnumeratorDoesntEnumerateMultipleUnions() { - var iterator = NumberRangeGuaranteedNotCollectionType(0, 3).Union(Enumerable.Range(0, 3)).Union(Enumerable.Range(2, 4)).Union(new[] { 9, 2, 4 }); + var iterator = NumberRangeGuaranteedNotCollectionType(0, 3) + .Union(Enumerable.Range(0, 3)) + .Union(Enumerable.Range(2, 4)) + .Union([9, 2, 4]); // Don't insist on this behaviour, but check it's correct if it happens var en = iterator as IEnumerator; Assert.False(en is not null && en.MoveNext()); @@ -317,9 +321,9 @@ public void ForcedToEnumeratorDoesntEnumerateMultipleUnions() [Fact] public void ToArray() { - string[] first = { "Bob", "Robert", "Tim", "Matt", "miT" }; - string[] second = { "ttaM", "Charlie", "Bbo" }; - string[] expected = { "Bob", "Robert", "Tim", "Matt", "miT", "ttaM", "Charlie", "Bbo" }; + string[] first = ["Bob", "Robert", "Tim", "Matt", "miT"]; + string[] second = ["ttaM", "Charlie", "Bbo"]; + string[] expected = ["Bob", "Robert", "Tim", "Matt", "miT", "ttaM", "Charlie", "Bbo"]; Assert.Equal(expected, first.Union(second).ToArray()); } @@ -327,10 +331,10 @@ public void ToArray() [Fact] public void ToArrayMultipleUnion() { - string[] first = { "Bob", "Robert", "Tim", "Matt", "miT" }; - string[] second = { "ttaM", "Charlie", "Bbo" }; - string[] third = { "Bob", "Albert", "Tim" }; - string[] expected = { "Bob", "Robert", "Tim", "Matt", "miT", "ttaM", "Charlie", "Bbo", "Albert" }; + string[] first = ["Bob", "Robert", "Tim", "Matt", "miT"]; + string[] second = ["ttaM", "Charlie", "Bbo"]; + string[] third = ["Bob", "Albert", "Tim"]; + string[] expected = ["Bob", "Robert", "Tim", "Matt", "miT", "ttaM", "Charlie", "Bbo", "Albert"]; Assert.Equal(expected, first.Union(second).Union(third).ToArray()); } @@ -338,9 +342,9 @@ public void ToArrayMultipleUnion() [Fact] public void ToList() { - string[] first = { "Bob", "Robert", "Tim", "Matt", "miT" }; - string[] second = { "ttaM", "Charlie", "Bbo" }; - string[] expected = { "Bob", "Robert", "Tim", "Matt", "miT", "ttaM", "Charlie", "Bbo" }; + string[] first = ["Bob", "Robert", "Tim", "Matt", "miT"]; + string[] second = ["ttaM", "Charlie", "Bbo"]; + string[] expected = ["Bob", "Robert", "Tim", "Matt", "miT", "ttaM", "Charlie", "Bbo"]; Assert.Equal(expected, first.Union(second).ToList()); } @@ -348,10 +352,10 @@ public void ToList() [Fact] public void ToListMultipleUnion() { - string[] first = { "Bob", "Robert", "Tim", "Matt", "miT" }; - string[] second = { "ttaM", "Charlie", "Bbo" }; - string[] third = { "Bob", "Albert", "Tim" }; - string[] expected = { "Bob", "Robert", "Tim", "Matt", "miT", "ttaM", "Charlie", "Bbo", "Albert" }; + string[] first = ["Bob", "Robert", "Tim", "Matt", "miT"]; + string[] second = ["ttaM", "Charlie", "Bbo"]; + string[] third = ["Bob", "Albert", "Tim"]; + string[] expected = ["Bob", "Robert", "Tim", "Matt", "miT", "ttaM", "Charlie", "Bbo", "Albert"]; Assert.Equal(expected, first.Union(second).Union(third).ToList()); } @@ -359,17 +363,17 @@ public void ToListMultipleUnion() [Fact] public void Count() { - string[] first = { "Bob", "Robert", "Tim", "Matt", "miT" }; - string[] second = { "ttaM", "Charlie", "Bbo" }; + string[] first = ["Bob", "Robert", "Tim", "Matt", "miT"]; + string[] second = ["ttaM", "Charlie", "Bbo"]; Assert.Equal(8, first.Union(second).Count()); } [Fact] public void CountMultipleUnion() { - string[] first = { "Bob", "Robert", "Tim", "Matt", "miT" }; - string[] second = { "ttaM", "Charlie", "Bbo" }; - string[] third = { "Bob", "Albert", "Tim" }; + string[] first = ["Bob", "Robert", "Tim", "Matt", "miT"]; + string[] second = ["ttaM", "Charlie", "Bbo"]; + string[] third = ["Bob", "Albert", "Tim"]; Assert.Equal(9, first.Union(second).Union(third).Count()); } @@ -377,8 +381,8 @@ public void CountMultipleUnion() [Fact] public void RepeatEnumerating() { - string[] first = { "Bob", "Robert", "Tim", "Matt", "miT" }; - string[] second = { "ttaM", "Charlie", "Bbo" }; + string[] first = ["Bob", "Robert", "Tim", "Matt", "miT"]; + string[] second = ["ttaM", "Charlie", "Bbo"]; var result = first.Union(second); @@ -388,9 +392,9 @@ public void RepeatEnumerating() [Fact] public void RepeatEnumeratingMultipleUnions() { - string[] first = { "Bob", "Robert", "Tim", "Matt", "miT" }; - string[] second = { "ttaM", "Charlie", "Bbo" }; - string[] third = { "Matt", "Albert", "Ichabod" }; + string[] first = ["Bob", "Robert", "Tim", "Matt", "miT"]; + string[] second = ["ttaM", "Charlie", "Bbo"]; + string[] third = ["Matt", "Albert", "Ichabod"]; var result = first.Union(second).Union(third); @@ -401,24 +405,24 @@ public void RepeatEnumeratingMultipleUnions() public void HashSetWithBuiltInComparer_HashSetContainsNotUsed() { IEnumerable input1 = new HashSet(StringComparer.OrdinalIgnoreCase) { "a" }; - IEnumerable input2 = new[] { "A" }; + IEnumerable input2 = ["A"]; - Assert.Equal(new[] { "a", "A" }, input1.Union(input2)); - Assert.Equal(new[] { "a", "A" }, input1.Union(input2, null)); - Assert.Equal(new[] { "a", "A" }, input1.Union(input2, EqualityComparer.Default)); - Assert.Equal(new[] { "a" }, input1.Union(input2, StringComparer.OrdinalIgnoreCase)); + Assert.Equal(["a", "A"], input1.Union(input2)); + Assert.Equal(["a", "A"], input1.Union(input2, null)); + Assert.Equal(["a", "A"], input1.Union(input2, EqualityComparer.Default)); + Assert.Equal(["a"], input1.Union(input2, StringComparer.OrdinalIgnoreCase)); - Assert.Equal(new[] { "A", "a" }, input2.Union(input1)); - Assert.Equal(new[] { "A", "a" }, input2.Union(input1, null)); - Assert.Equal(new[] { "A", "a" }, input2.Union(input1, EqualityComparer.Default)); - Assert.Equal(new[] { "A" }, input2.Union(input1, StringComparer.OrdinalIgnoreCase)); + Assert.Equal(["A", "a"], input2.Union(input1)); + Assert.Equal(["A", "a"], input2.Union(input1, null)); + Assert.Equal(["A", "a"], input2.Union(input1, EqualityComparer.Default)); + Assert.Equal(["A"], input2.Union(input1, StringComparer.OrdinalIgnoreCase)); } [Fact] public void UnionBy_FirstNull_ThrowsArgumentNullException() { string[] first = null; - string[] second = { "bBo", "shriC" }; + string[] second = ["bBo", "shriC"]; AssertExtensions.Throws("first", () => first.UnionBy(second, x => x)); AssertExtensions.Throws("first", () => first.UnionBy(second, x => x, new AnagramEqualityComparer())); @@ -427,7 +431,7 @@ public void UnionBy_FirstNull_ThrowsArgumentNullException() [Fact] public void UnionBy_SecondNull_ThrowsArgumentNullException() { - string[] first = { "Bob", "Tim", "Robert", "Chris" }; + string[] first = ["Bob", "Tim", "Robert", "Chris"]; string[] second = null; AssertExtensions.Throws("second", () => first.UnionBy(second, x => x)); @@ -437,8 +441,8 @@ public void UnionBy_SecondNull_ThrowsArgumentNullException() [Fact] public void UnionBy_KeySelectorNull_ThrowsArgumentNullException() { - string[] first = { "Bob", "Tim", "Robert", "Chris" }; - string[] second = { "bBo", "shriC" }; + string[] first = ["Bob", "Tim", "Robert", "Chris"]; + string[] second = ["bBo", "shriC"]; Func keySelector = null; AssertExtensions.Throws("keySelector", () => first.UnionBy(second, keySelector)); @@ -476,7 +480,7 @@ public static IEnumerable UnionBy_TestData() expected: Enumerable.Range(0, 20)); yield return WrapArgs( - first: Enumerable.Empty(), + first: [], second: Enumerable.Range(0, 5), keySelector: x => x, comparer: null, @@ -484,7 +488,7 @@ public static IEnumerable UnionBy_TestData() yield return WrapArgs( first: Enumerable.Repeat(5, 20), - second: Enumerable.Empty(), + second: [], keySelector: x => x, comparer: null, expected: Enumerable.Repeat(5, 1)); @@ -497,18 +501,18 @@ public static IEnumerable UnionBy_TestData() expected: Enumerable.Repeat(5, 1)); yield return WrapArgs( - first: new string[] { "Bob", "Tim", "Robert", "Chris" }, - second: new string[] { "bBo", "shriC" }, + first: ["Bob", "Tim", "Robert", "Chris"], + second: ["bBo", "shriC"], keySelector: x => x, null, - expected: new string[] { "Bob", "Tim", "Robert", "Chris", "bBo", "shriC" }); + expected: ["Bob", "Tim", "Robert", "Chris", "bBo", "shriC"]); yield return WrapArgs( - first: new string[] { "Bob", "Tim", "Robert", "Chris" }, - second: new string[] { "bBo", "shriC" }, + first: ["Bob", "Tim", "Robert", "Chris"], + second: ["bBo", "shriC"], keySelector: x => x, new AnagramEqualityComparer(), - expected: new string[] { "Bob", "Tim", "Robert", "Chris" }); + expected: ["Bob", "Tim", "Robert", "Chris"]); yield return WrapArgs( first: new (string Name, int Age)[] { ("Tom", 20), ("Dick", 30), ("Harry", 40), ("Martin", 20) }, @@ -532,7 +536,7 @@ public static IEnumerable UnionBy_TestData() expected: new (string Name, int Age)[] { ("Tom", 20), ("Dick", 30), ("Harry", 40), ("Martin", 20), ("Toby", 33) }); object[] WrapArgs(IEnumerable first, IEnumerable second, Func keySelector, IEqualityComparer? comparer, IEnumerable expected) - => new object[] { first, second, keySelector, comparer, expected }; + => [first, second, keySelector, comparer, expected]; } } } diff --git a/tests/System.Linq.Tests/Tests/System.Linq/WhereTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/WhereTests.cs index e1c781a5..147efa0d 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/WhereTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/WhereTests.cs @@ -42,7 +42,7 @@ public void Where_PredicateIsNull_ArgumentNullExceptionThrown() public void Where_Array_ExecutionIsDeferred() { bool funcCalled = false; - Func[] source = { () => { funcCalled = true; return true; } }; + Func[] source = [() => { funcCalled = true; return true; }]; IEnumerable> query = source.Where(value => value()); Assert.False(funcCalled); @@ -55,7 +55,14 @@ public void Where_Array_ExecutionIsDeferred() public void Where_List_ExecutionIsDeferred() { bool funcCalled = false; - List> source = new List>() { () => { funcCalled = true; return true; } }; + List> source = + [ + () => + { + funcCalled = true; + return true; + } + ]; IEnumerable> query = source.Where(value => value()); Assert.False(funcCalled); @@ -107,7 +114,7 @@ public void Where_IEnumerable_ExecutionIsDeferred() public void WhereWhere_Array_ExecutionIsDeferred() { bool funcCalled = false; - Func[] source = new Func[] { () => { funcCalled = true; return true; } }; + Func[] source = [() => { funcCalled = true; return true; }]; IEnumerable> query = source.Where(value => value()).Where(value => value()); Assert.False(funcCalled); @@ -120,7 +127,14 @@ public void WhereWhere_Array_ExecutionIsDeferred() public void WhereWhere_List_ExecutionIsDeferred() { bool funcCalled = false; - List> source = new List>() { () => { funcCalled = true; return true; } }; + List> source = + [ + () => + { + funcCalled = true; + return true; + } + ]; IEnumerable> query = source.Where(value => value()).Where(value => value()); Assert.False(funcCalled); @@ -175,7 +189,7 @@ public void WhereWhere_IEnumerable_ExecutionIsDeferred() [Fact] public void Where_Array_ReturnsExpectedValues_True() { - int[] source = new[] { 1, 2, 3, 4, 5 }; + int[] source = [1, 2, 3, 4, 5]; Func truePredicate = (value) => true; IEnumerable result = source.Where(truePredicate); @@ -190,7 +204,7 @@ public void Where_Array_ReturnsExpectedValues_True() [Fact] public void Where_Array_ReturnsExpectedValues_False() { - int[] source = new[] { 1, 2, 3, 4, 5 }; + int[] source = [1, 2, 3, 4, 5]; Func falsePredicate = (value) => false; IEnumerable result = source.Where(falsePredicate); @@ -201,7 +215,7 @@ public void Where_Array_ReturnsExpectedValues_False() [Fact] public void Where_Array_ReturnsExpectedValues_Complex() { - int[] source = new[] { 2, 1, 3, 5, 4 }; + int[] source = [2, 1, 3, 5, 4]; Func complexPredicate = (value, index) => { return (value == index); }; IEnumerable result = source.Where(complexPredicate); @@ -214,7 +228,7 @@ public void Where_Array_ReturnsExpectedValues_Complex() [Fact] public void Where_List_ReturnsExpectedValues_True() { - List source = new List { 1, 2, 3, 4, 5 }; + List source = [1, 2, 3, 4, 5]; Func truePredicate = (value) => true; IEnumerable result = source.Where(truePredicate); @@ -229,7 +243,7 @@ public void Where_List_ReturnsExpectedValues_True() [Fact] public void Where_List_ReturnsExpectedValues_False() { - List source = new List { 1, 2, 3, 4, 5 }; + List source = [1, 2, 3, 4, 5]; Func falsePredicate = (value) => false; IEnumerable result = source.Where(falsePredicate); @@ -240,7 +254,7 @@ public void Where_List_ReturnsExpectedValues_False() [Fact] public void Where_List_ReturnsExpectedValues_Complex() { - List source = new List { 2, 1, 3, 5, 4 }; + List source = [2, 1, 3, 5, 4]; Func complexPredicate = (value, index) => { return (value == index); }; IEnumerable result = source.Where(complexPredicate); @@ -370,7 +384,7 @@ public void Where_IEnumerable_ReturnsExpectedValues_Complex() [Fact] public void Where_EmptyEnumerable_ReturnsNoElements() { - IEnumerable source = Enumerable.Empty(); + IEnumerable source = []; bool wasSelectorCalled = false; IEnumerable result = source.Where(value => { wasSelectorCalled = true; return true; }); @@ -388,10 +402,10 @@ public void Where_EmptyEnumerable_ReturnsNoElementsWithIndex() [Fact] public void Where_Array_CurrentIsDefaultOfTAfterEnumeration() { - int[] source = new[] { 1 }; + int[] source = [1]; Func truePredicate = (value) => true; - var enumerator = source.Where(truePredicate).GetEnumerator(); + using var enumerator = source.Where(truePredicate).GetEnumerator(); while (enumerator.MoveNext()) ; Assert.Equal(default(int), enumerator.Current); @@ -400,10 +414,10 @@ public void Where_Array_CurrentIsDefaultOfTAfterEnumeration() [Fact] public void Where_List_CurrentIsDefaultOfTAfterEnumeration() { - List source = new List() { 1 }; + List source = [1]; Func truePredicate = (value) => true; - var enumerator = source.Where(truePredicate).GetEnumerator(); + using var enumerator = source.Where(truePredicate).GetEnumerator(); while (enumerator.MoveNext()) ; Assert.Equal(default(int), enumerator.Current); @@ -415,7 +429,7 @@ public void Where_IReadOnlyCollection_CurrentIsDefaultOfTAfterEnumeration() IReadOnlyCollection source = new ReadOnlyCollection(new List() { 1 }); Func truePredicate = (value) => true; - var enumerator = source.Where(truePredicate).GetEnumerator(); + using var enumerator = source.Where(truePredicate).GetEnumerator(); while (enumerator.MoveNext()) ; Assert.Equal(default(int), enumerator.Current); @@ -439,7 +453,7 @@ public void Where_IEnumerable_CurrentIsDefaultOfTAfterEnumeration() IEnumerable source = Enumerable.Repeat(1, 1); Func truePredicate = (value) => true; - var enumerator = source.Where(truePredicate).GetEnumerator(); + using var enumerator = source.Where(truePredicate).GetEnumerator(); while (enumerator.MoveNext()) ; Assert.Equal(default(int), enumerator.Current); @@ -448,7 +462,7 @@ public void Where_IEnumerable_CurrentIsDefaultOfTAfterEnumeration() [Fact] public void WhereWhere_Array_ReturnsExpectedValues() { - int[] source = new[] { 1, 2, 3, 4, 5 }; + int[] source = [1, 2, 3, 4, 5]; Func evenPredicate = (value) => value % 2 == 0; IEnumerable result = source.Where(evenPredicate).Where(evenPredicate); @@ -461,7 +475,7 @@ public void WhereWhere_Array_ReturnsExpectedValues() [Fact] public void WhereWhere_List_ReturnsExpectedValues() { - List source = new List { 1, 2, 3, 4, 5 }; + List source = [1, 2, 3, 4, 5]; Func evenPredicate = (value) => value % 2 == 0; IEnumerable result = source.Where(evenPredicate).Where(evenPredicate); @@ -513,7 +527,7 @@ public void WhereWhere_IEnumerable_ReturnsExpectedValues() [Fact] public void WhereSelect_Array_ReturnsExpectedValues() { - int[] source = new[] { 1, 2, 3, 4, 5 }; + int[] source = [1, 2, 3, 4, 5]; Func evenPredicate = (value) => value % 2 == 0; Func addSelector = (value) => value + 1; @@ -527,7 +541,7 @@ public void WhereSelect_Array_ReturnsExpectedValues() [Fact] public void WhereSelectSelect_Array_ReturnsExpectedValues() { - int[] source = new[] { 1, 2, 3, 4, 5 }; + int[] source = [1, 2, 3, 4, 5]; Func evenPredicate = (value) => value % 2 == 0; Func addSelector = (value) => value + 1; @@ -541,7 +555,7 @@ public void WhereSelectSelect_Array_ReturnsExpectedValues() [Fact] public void WhereSelect_List_ReturnsExpectedValues() { - List source = new List { 1, 2, 3, 4, 5 }; + List source = [1, 2, 3, 4, 5]; Func evenPredicate = (value) => value % 2 == 0; Func addSelector = (value) => value + 1; @@ -555,7 +569,7 @@ public void WhereSelect_List_ReturnsExpectedValues() [Fact] public void WhereSelectSelect_List_ReturnsExpectedValues() { - List source = new List { 1, 2, 3, 4, 5 }; + List source = [1, 2, 3, 4, 5]; Func evenPredicate = (value) => value % 2 == 0; Func addSelector = (value) => value + 1; @@ -653,7 +667,7 @@ public void WhereSelectSelect_IEnumerable_ReturnsExpectedValues() [Fact] public void SelectWhere_Array_ReturnsExpectedValues() { - int[] source = new[] { 1, 2, 3, 4, 5 }; + int[] source = [1, 2, 3, 4, 5]; Func evenPredicate = (value) => value % 2 == 0; Func addSelector = (value) => value + 1; @@ -668,7 +682,7 @@ public void SelectWhere_Array_ReturnsExpectedValues() [Fact] public void SelectWhere_List_ReturnsExpectedValues() { - List source = new List { 1, 2, 3, 4, 5 }; + List source = [1, 2, 3, 4, 5]; Func evenPredicate = (value) => value % 2 == 0; Func addSelector = (value) => value + 1; @@ -732,7 +746,7 @@ public void SelectWhere_IEnumerable_ReturnsExpectedValues() [Fact] public void Where_PredicateThrowsException() { - int[] source = new[] { 1, 2, 3, 4, 5 }; + int[] source = [1, 2, 3, 4, 5]; Func predicate = value => { if (value == 1) @@ -742,7 +756,7 @@ public void Where_PredicateThrowsException() return true; }; - var enumerator = source.Where(predicate).GetEnumerator(); + using var enumerator = source.Where(predicate).GetEnumerator(); // Ensure the first MoveNext call throws an exception Assert.Throws(() => enumerator.MoveNext()); @@ -762,7 +776,7 @@ public void Where_SourceThrowsOnCurrent() IEnumerable source = new ThrowsOnCurrentEnumerator(); Func truePredicate = (value) => true; - var enumerator = source.Where(truePredicate).GetEnumerator(); + using var enumerator = source.Where(truePredicate).GetEnumerator(); // Ensure the first MoveNext call throws an exception Assert.Throws(() => enumerator.MoveNext()); @@ -778,7 +792,7 @@ public void Where_SourceThrowsOnMoveNext() IEnumerable source = new ThrowsOnMoveNext(); Func truePredicate = (value) => true; - var enumerator = source.Where(truePredicate).GetEnumerator(); + using var enumerator = source.Where(truePredicate).GetEnumerator(); // Ensure the first MoveNext call throws an exception Assert.Throws(() => enumerator.MoveNext()); @@ -798,7 +812,7 @@ public void Where_SourceThrowsOnGetEnumerator() IEnumerable source = new ThrowsOnGetEnumerator(); Func truePredicate = (value) => true; - var enumerator = source.Where(truePredicate).GetEnumerator(); + using var enumerator = source.Where(truePredicate).GetEnumerator(); // Ensure the first MoveNext call throws an exception Assert.Throws(() => enumerator.MoveNext()); @@ -815,8 +829,8 @@ public void Where_SourceThrowsOnGetEnumerator() [Fact] public void Select_ResetEnumerator_ThrowsException() { - int[] source = new[] { 1, 2, 3, 4, 5 }; - IEnumerator enumerator = source.Where(value => true).GetEnumerator(); + int[] source = [1, 2, 3, 4, 5]; + using IEnumerator enumerator = source.Where(value => true).GetEnumerator(); // The .NET Framework throws a NotImplementedException. // See https://github.com/dotnet/corefx/pull/2959. @@ -826,10 +840,10 @@ public void Select_ResetEnumerator_ThrowsException() [Fact] public void Where_SourceThrowsOnConcurrentModification() { - List source = new List() { 1, 2, 3, 4, 5 }; + List source = [1, 2, 3, 4, 5]; Func truePredicate = (value) => true; - var enumerator = source.Where(truePredicate).GetEnumerator(); + using var enumerator = source.Where(truePredicate).GetEnumerator(); Assert.True(enumerator.MoveNext()); Assert.Equal(1, enumerator.Current); @@ -843,16 +857,14 @@ public void Where_SourceThrowsOnConcurrentModification() [Fact] public void Where_GetEnumeratorReturnsUniqueInstances() { - int[] source = new[] { 1, 2, 3, 4, 5 }; + int[] source = [1, 2, 3, 4, 5]; var result = source.Where(value => true); - using (var enumerator1 = result.GetEnumerator()) - using (var enumerator2 = result.GetEnumerator()) - { - Assert.Same(result, enumerator1); - Assert.NotSame(enumerator1, enumerator2); - } + using var enumerator1 = result.GetEnumerator(); + using var enumerator2 = result.GetEnumerator(); + Assert.Same(result, enumerator1); + Assert.NotSame(enumerator1, enumerator2); } [Fact] @@ -879,51 +891,51 @@ public void SameResultsRepeatCallsStringQuery() [Fact] public void SingleElementPredicateFalse() { - int[] source = { 3 }; + int[] source = [3]; Assert.DoesNotContain(source, IsEven); } [Fact] public void PredicateFalseForAll() { - int[] source = { 9, 7, 15, 3, 27 }; + int[] source = [9, 7, 15, 3, 27]; Assert.DoesNotContain(source, IsEven); } [Fact] public void PredicateTrueFirstOnly() { - int[] source = { 10, 9, 7, 15, 3, 27 }; + int[] source = [10, 9, 7, 15, 3, 27]; Assert.Equal(source.Take(1), source.Where(IsEven)); } [Fact] public void PredicateTrueLastOnly() { - int[] source = { 9, 7, 15, 3, 27, 20 }; + int[] source = [9, 7, 15, 3, 27, 20]; Assert.Equal(source.Skip(source.Length - 1), source.Where(IsEven)); } [Fact] public void PredicateTrueFirstThirdSixth() { - int[] source = { 20, 7, 18, 9, 7, 10, 21 }; - int[] expected = { 20, 18, 10 }; + int[] source = [20, 7, 18, 9, 7, 10, 21]; + int[] expected = [20, 18, 10]; Assert.Equal(expected, source.Where(IsEven)); } [Fact] public void RunOnce() { - int[] source = { 20, 7, 18, 9, 7, 10, 21 }; - int[] expected = { 20, 18, 10 }; + int[] source = [20, 7, 18, 9, 7, 10, 21]; + int[] expected = [20, 18, 10]; Assert.Equal(expected, source.RunOnce().Where(IsEven)); } [Fact] public void SourceAllNullsPredicateTrue() { - int?[] source = { null, null, null, null }; + int?[] source = [null, null, null, null]; Assert.Equal(source, source.Where(num => true)); } @@ -936,64 +948,64 @@ public void SourceEmptyIndexedPredicate() [Fact] public void SingleElementIndexedPredicateTrue() { - int[] source = { 2 }; + int[] source = [2]; Assert.Equal(source, source.Where((e, i) => e % 2 == 0)); } [Fact] public void SingleElementIndexedPredicateFalse() { - int[] source = { 3 }; + int[] source = [3]; Assert.Empty(source.Where((e, i) => e % 2 == 0)); } [Fact] public void IndexedPredicateFalseForAll() { - int[] source = { 9, 7, 15, 3, 27 }; + int[] source = [9, 7, 15, 3, 27]; Assert.Empty(source.Where((e, i) => e % 2 == 0)); } [Fact] public void IndexedPredicateTrueFirstOnly() { - int[] source = { 10, 9, 7, 15, 3, 27 }; + int[] source = [10, 9, 7, 15, 3, 27]; Assert.Equal(source.Take(1), source.Where((e, i) => e % 2 == 0)); } [Fact] public void IndexedPredicateTrueLastOnly() { - int[] source = { 9, 7, 15, 3, 27, 20 }; + int[] source = [9, 7, 15, 3, 27, 20]; Assert.Equal(source.Skip(source.Length - 1), source.Where((e, i) => e % 2 == 0)); } [Fact] public void IndexedPredicateTrueFirstThirdSixth() { - int[] source = { 20, 7, 18, 9, 7, 10, 21 }; - int[] expected = { 20, 18, 10 }; + int[] source = [20, 7, 18, 9, 7, 10, 21]; + int[] expected = [20, 18, 10]; Assert.Equal(expected, source.Where((e, i) => e % 2 == 0)); } [Fact] public void SourceAllNullsIndexedPredicateTrue() { - int?[] source = { null, null, null, null }; + int?[] source = [null, null, null, null]; Assert.Equal(source, source.Where((num, index) => true)); } [Fact] public void PredicateSelectsFirst() { - int[] source = { -40, 20, 100, 5, 4, 9 }; + int[] source = [-40, 20, 100, 5, 4, 9]; Assert.Equal(source.Take(1), source.Where((e, i) => i == 0)); } [Fact] public void PredicateSelectsLast() { - int[] source = { -40, 20, 100, 5, 4, 9 }; + int[] source = [-40, 20, 100, 5, 4, 9]; Assert.Equal(source.Skip(source.Length - 1), source.Where((e, i) => i == source.Length - 1)); } @@ -1001,13 +1013,13 @@ public void PredicateSelectsLast() public void IndexOverflows() { var infiniteWhere = new FastInfiniteEnumerator().Where((e, i) => true); - using (var en = infiniteWhere.GetEnumerator()) - Assert.Throws(() => + using var en = infiniteWhere.GetEnumerator(); + Assert.Throws(() => + { + while (en.MoveNext()) { - while (en.MoveNext()) - { - } - }); + } + }); } [Fact] @@ -1078,63 +1090,57 @@ public void ToCollection(IEnumerable source) Assert.Equal(source, equivalent.ToList()); Assert.Equal(source.Count(), equivalent.Count()); // Count may be optimized. The above asserts do not imply this will pass. - using (IEnumerator en = equivalent.GetEnumerator()) + using IEnumerator en = equivalent.GetEnumerator(); + for (int i = 0; i < equivalent.Count(); i++) { - for (int i = 0; i < equivalent.Count(); i++) - { - Assert.True(en.MoveNext()); - } + Assert.True(en.MoveNext()); + } - Assert.False(en.MoveNext()); // No more items, this should dispose. - Assert.Equal(0, en.Current); // Reset to default value + Assert.False(en.MoveNext()); // No more items, this should dispose. + Assert.Equal(0, en.Current); // Reset to default value - Assert.False(en.MoveNext()); // Want to be sure MoveNext after disposing still works. - Assert.Equal(0, en.Current); - } + Assert.False(en.MoveNext()); // Want to be sure MoveNext after disposing still works. + Assert.Equal(0, en.Current); } } [Fact] public void WhereFirstLast() { - Assert.All(IdentityTransforms(), transform => + Assert.All(CreateSources(Enumerable.Range(0, 10)), source => { - IEnumerable data = transform(Enumerable.Range(0, 10)); + Assert.Equal(3, source.Where(i => i == 3).First()); + Assert.Equal(0, source.Where(i => i % 2 == 0).First()); - Assert.Equal(3, data.Where(i => i == 3).First()); - Assert.Equal(0, data.Where(i => i % 2 == 0).First()); + Assert.Equal(3, source.Where(i => i == 3).Last()); + Assert.Equal(8, source.Where(i => i % 2 == 0).Last()); - Assert.Equal(3, data.Where(i => i == 3).Last()); - Assert.Equal(8, data.Where(i => i % 2 == 0).Last()); + Assert.Equal(3, source.Where(i => i == 3).ElementAt(0)); + Assert.Equal(8, source.Where(i => i % 2 == 0).ElementAt(4)); - Assert.Equal(3, data.Where(i => i == 3).ElementAt(0)); - Assert.Equal(8, data.Where(i => i % 2 == 0).ElementAt(4)); - - Assert.Throws(() => data.Where(i => i == 10).First()); - Assert.Throws(() => data.Where(i => i == 10).Last()); - Assert.Throws(() => data.Where(i => i == 10).ElementAt(0)); + Assert.Throws(() => source.Where(i => i == 10).First()); + Assert.Throws(() => source.Where(i => i == 10).Last()); + Assert.Throws(() => source.Where(i => i == 10).ElementAt(0)); }); } [Fact] public void WhereSelectFirstLast() { - Assert.All(IdentityTransforms(), transform => + Assert.All(CreateSources(Enumerable.Range(0, 10)), source => { - IEnumerable data = transform(Enumerable.Range(0, 10)); - - Assert.Equal(6, data.Where(i => i == 3).Select(i => i * 2).First()); - Assert.Equal(0, data.Where(i => i % 2 == 0).Select(i => i * 2).First()); + Assert.Equal(6, source.Where(i => i == 3).Select(i => i * 2).First()); + Assert.Equal(0, source.Where(i => i % 2 == 0).Select(i => i * 2).First()); - Assert.Equal(6, data.Where(i => i == 3).Select(i => i * 2).Last()); - Assert.Equal(16, data.Where(i => i % 2 == 0).Select(i => i * 2).Last()); + Assert.Equal(6, source.Where(i => i == 3).Select(i => i * 2).Last()); + Assert.Equal(16, source.Where(i => i % 2 == 0).Select(i => i * 2).Last()); - Assert.Equal(6, data.Where(i => i == 3).Select(i => i * 2).ElementAt(0)); - Assert.Equal(16, data.Where(i => i % 2 == 0).Select(i => i * 2).ElementAt(4)); + Assert.Equal(6, source.Where(i => i == 3).Select(i => i * 2).ElementAt(0)); + Assert.Equal(16, source.Where(i => i % 2 == 0).Select(i => i * 2).ElementAt(4)); - Assert.Throws(() => data.Where(i => i == 10).Select(i => i * 2).First()); - Assert.Throws(() => data.Where(i => i == 10).Select(i => i * 2).Last()); - Assert.Throws(() => data.Where(i => i == 10).Select(i => i * 2).ElementAt(0)); + Assert.Throws(() => source.Where(i => i == 10).Select(i => i * 2).First()); + Assert.Throws(() => source.Where(i => i == 10).Select(i => i * 2).Last()); + Assert.Throws(() => source.Where(i => i == 10).Select(i => i * 2).ElementAt(0)); }); } @@ -1142,9 +1148,9 @@ public static IEnumerable ToCollectionData() { IEnumerable seq = GenerateRandomSequnce(seed: 0xdeadbeef, count: 10); - foreach (IEnumerable seq2 in IdentityTransforms().Select(t => t(seq))) + foreach (IEnumerable seq2 in CreateSources(seq)) { - yield return new object[] { seq2 }; + yield return [seq2]; } } diff --git a/tests/System.Linq.Tests/Tests/System.Linq/ZipTests.cs b/tests/System.Linq.Tests/Tests/System.Linq/ZipTests.cs index f1546193..58890f50 100644 --- a/tests/System.Linq.Tests/Tests/System.Linq/ZipTests.cs +++ b/tests/System.Linq.Tests/Tests/System.Linq/ZipTests.cs @@ -11,9 +11,9 @@ public class ZipTests : EnumerableTests [Fact] public void ImplicitTypeParameters() { - IEnumerable first = new int[] { 1, 2, 3 }; - IEnumerable second = new int[] { 2, 5, 9 }; - IEnumerable expected = new int[] { 3, 7, 12 }; + IEnumerable first = [1, 2, 3]; + IEnumerable second = [2, 5, 9]; + IEnumerable expected = [3, 7, 12]; Assert.Equal(expected, first.Zip(second, (x, y) => x + y)); } @@ -21,9 +21,9 @@ public void ImplicitTypeParameters() [Fact] public void ExplicitTypeParameters() { - IEnumerable first = new int[] { 1, 2, 3 }; - IEnumerable second = new int[] { 2, 5, 9 }; - IEnumerable expected = new int[] { 3, 7, 12 }; + IEnumerable first = [1, 2, 3]; + IEnumerable second = [2, 5, 9]; + IEnumerable expected = [3, 7, 12]; Assert.Equal(expected, first.Zip(second, (x, y) => x + y)); } @@ -32,7 +32,7 @@ public void ExplicitTypeParameters() public void FirstIsNull() { IEnumerable first = null; - IEnumerable second = new int[] { 2, 5, 9 }; + IEnumerable second = [2, 5, 9]; AssertExtensions.Throws("first", () => first.Zip(second, (x, y) => x + y)); } @@ -40,7 +40,7 @@ public void FirstIsNull() [Fact] public void SecondIsNull() { - IEnumerable first = new int[] { 1, 2, 3 }; + IEnumerable first = [1, 2, 3]; IEnumerable second = null; AssertExtensions.Throws("second", () => first.Zip(second, (x, y) => x + y)); @@ -49,8 +49,8 @@ public void SecondIsNull() [Fact] public void FuncIsNull() { - IEnumerable first = new int[] { 1, 2, 3 }; - IEnumerable second = new int[] { 2, 4, 6 }; + IEnumerable first = [1, 2, 3]; + IEnumerable second = [2, 4, 6]; Func func = null; AssertExtensions.Throws("resultSelector", () => first.Zip(second, func)); @@ -59,14 +59,14 @@ public void FuncIsNull() [Fact] public void ExceptionThrownFromFirstsEnumerator() { - ThrowsOnMatchEnumerable first = new ThrowsOnMatchEnumerable(new int[] { 1, 3, 3 }, 2); - IEnumerable second = new int[] { 2, 4, 6 }; + ThrowsOnMatchEnumerable first = new ThrowsOnMatchEnumerable([1, 3, 3], 2); + IEnumerable second = [2, 4, 6]; Func func = (x, y) => x + y; - IEnumerable expected = new int[] { 3, 7, 9 }; + IEnumerable expected = [3, 7, 9]; Assert.Equal(expected, first.Zip(second, func)); - first = new ThrowsOnMatchEnumerable(new int[] { 1, 2, 3 }, 2); + first = new ThrowsOnMatchEnumerable([1, 2, 3], 2); var zip = first.Zip(second, func); @@ -76,14 +76,14 @@ public void ExceptionThrownFromFirstsEnumerator() [Fact] public void ExceptionThrownFromSecondsEnumerator() { - ThrowsOnMatchEnumerable second = new ThrowsOnMatchEnumerable(new int[] { 1, 3, 3 }, 2); - IEnumerable first = new int[] { 2, 4, 6 }; + ThrowsOnMatchEnumerable second = new ThrowsOnMatchEnumerable([1, 3, 3], 2); + IEnumerable first = [2, 4, 6]; Func func = (x, y) => x + y; - IEnumerable expected = new int[] { 3, 7, 9 }; + IEnumerable expected = [3, 7, 9]; Assert.Equal(expected, first.Zip(second, func)); - second = new ThrowsOnMatchEnumerable(new int[] { 1, 2, 3 }, 2); + second = new ThrowsOnMatchEnumerable([1, 2, 3], 2); var zip = first.Zip(second, func); @@ -93,10 +93,10 @@ public void ExceptionThrownFromSecondsEnumerator() [Fact] public void FirstAndSecondEmpty() { - IEnumerable first = new int[] { }; - IEnumerable second = new int[] { }; + IEnumerable first = []; + IEnumerable second = []; Func func = (x, y) => x + y; - IEnumerable expected = new int[] { }; + IEnumerable expected = []; Assert.Equal(expected, first.Zip(second, func)); } @@ -105,10 +105,10 @@ public void FirstAndSecondEmpty() [Fact] public void FirstEmptySecondSingle() { - IEnumerable first = new int[] { }; - IEnumerable second = new int[] { 2 }; + IEnumerable first = []; + IEnumerable second = [2]; Func func = (x, y) => x + y; - IEnumerable expected = new int[] { }; + IEnumerable expected = []; Assert.Equal(expected, first.Zip(second, func)); } @@ -116,10 +116,10 @@ public void FirstEmptySecondSingle() [Fact] public void FirstEmptySecondMany() { - IEnumerable first = new int[] { }; - IEnumerable second = new int[] { 2, 4, 8 }; + IEnumerable first = []; + IEnumerable second = [2, 4, 8]; Func func = (x, y) => x + y; - IEnumerable expected = new int[] { }; + IEnumerable expected = []; Assert.Equal(expected, first.Zip(second, func)); } @@ -128,10 +128,10 @@ public void FirstEmptySecondMany() [Fact] public void SecondEmptyFirstSingle() { - IEnumerable first = new int[] { 1 }; - IEnumerable second = new int[] { }; + IEnumerable first = [1]; + IEnumerable second = []; Func func = (x, y) => x + y; - IEnumerable expected = new int[] { }; + IEnumerable expected = []; Assert.Equal(expected, first.Zip(second, func)); } @@ -139,10 +139,10 @@ public void SecondEmptyFirstSingle() [Fact] public void SecondEmptyFirstMany() { - IEnumerable first = new int[] { 1, 2, 3 }; - IEnumerable second = new int[] { }; + IEnumerable first = [1, 2, 3]; + IEnumerable second = []; Func func = (x, y) => x + y; - IEnumerable expected = new int[] { }; + IEnumerable expected = []; Assert.Equal(expected, first.Zip(second, func)); } @@ -150,10 +150,10 @@ public void SecondEmptyFirstMany() [Fact] public void FirstAndSecondSingle() { - IEnumerable first = new int[] { 1 }; - IEnumerable second = new int[] { 2 }; + IEnumerable first = [1]; + IEnumerable second = [2]; Func func = (x, y) => x + y; - IEnumerable expected = new int[] { 3 }; + IEnumerable expected = [3]; Assert.Equal(expected, first.Zip(second, func)); } @@ -161,10 +161,10 @@ public void FirstAndSecondSingle() [Fact] public void FirstAndSecondEqualSize() { - IEnumerable first = new int[] { 1, 2, 3 }; - IEnumerable second = new int[] { 2, 3, 4 }; + IEnumerable first = [1, 2, 3]; + IEnumerable second = [2, 3, 4]; Func func = (x, y) => x + y; - IEnumerable expected = new int[] { 3, 5, 7 }; + IEnumerable expected = [3, 5, 7]; Assert.Equal(expected, first.Zip(second, func)); } @@ -172,10 +172,10 @@ public void FirstAndSecondEqualSize() [Fact] public void SecondOneMoreThanFirst() { - IEnumerable first = new int[] { 1, 2 }; - IEnumerable second = new int[] { 2, 4, 8 }; + IEnumerable first = [1, 2]; + IEnumerable second = [2, 4, 8]; Func func = (x, y) => x + y; - IEnumerable expected = new int[] { 3, 6 }; + IEnumerable expected = [3, 6]; Assert.Equal(expected, first.Zip(second, func)); } @@ -184,10 +184,10 @@ public void SecondOneMoreThanFirst() [Fact] public void SecondManyMoreThanFirst() { - IEnumerable first = new int[] { 1, 2 }; - IEnumerable second = new int[] { 2, 4, 8, 16 }; + IEnumerable first = [1, 2]; + IEnumerable second = [2, 4, 8, 16]; Func func = (x, y) => x + y; - IEnumerable expected = new int[] { 3, 6 }; + IEnumerable expected = [3, 6]; Assert.Equal(expected, first.Zip(second, func)); } @@ -195,10 +195,10 @@ public void SecondManyMoreThanFirst() [Fact] public void FirstOneMoreThanSecond() { - IEnumerable first = new int[] { 1, 2, 3 }; - IEnumerable second = new int[] { 2, 4 }; + IEnumerable first = [1, 2, 3]; + IEnumerable second = [2, 4]; Func func = (x, y) => x + y; - IEnumerable expected = new int[] { 3, 6 }; + IEnumerable expected = [3, 6]; Assert.Equal(expected, first.Zip(second, func)); } @@ -207,10 +207,10 @@ public void FirstOneMoreThanSecond() [Fact] public void FirstManyMoreThanSecond() { - IEnumerable first = new int[] { 1, 2, 3, 4 }; - IEnumerable second = new int[] { 2, 4 }; + IEnumerable first = [1, 2, 3, 4]; + IEnumerable second = [2, 4]; Func func = (x, y) => x + y; - IEnumerable expected = new int[] { 3, 6 }; + IEnumerable expected = [3, 6]; Assert.Equal(expected, first.Zip(second, func)); } @@ -219,15 +219,15 @@ public void FirstManyMoreThanSecond() [Fact] public void DelegateFuncChanged() { - IEnumerable first = new int[] { 1, 2, 3, 4 }; - IEnumerable second = new int[] { 2, 4, 8 }; + IEnumerable first = [1, 2, 3, 4]; + IEnumerable second = [2, 4, 8]; Func func = (x, y) => x + y; - IEnumerable expected = new int[] { 3, 6, 11 }; + IEnumerable expected = [3, 6, 11]; Assert.Equal(expected, first.Zip(second, func)); func = (x, y) => x - y; - expected = new int[] { -1, -2, -5 }; + expected = [-1, -2, -5]; Assert.Equal(expected, first.Zip(second, func)); } @@ -235,13 +235,13 @@ public void DelegateFuncChanged() [Fact] public void LambdaFuncChanged() { - IEnumerable first = new int[] { 1, 2, 3, 4 }; - IEnumerable second = new int[] { 2, 4, 8 }; - IEnumerable expected = new int[] { 3, 6, 11 }; + IEnumerable first = [1, 2, 3, 4]; + IEnumerable second = [2, 4, 8]; + IEnumerable expected = [3, 6, 11]; Assert.Equal(expected, first.Zip(second, (x, y) => x + y)); - expected = new int[] { -1, -2, -5 }; + expected = [-1, -2, -5]; Assert.Equal(expected, first.Zip(second, (x, y) => x - y)); } @@ -249,10 +249,10 @@ public void LambdaFuncChanged() [Fact] public void FirstHasFirstElementNull() { - IEnumerable first = new[] { (int?)null, 2, 3, 4 }; - IEnumerable second = new int[] { 2, 4, 8 }; + IEnumerable first = [(int?)null, 2, 3, 4]; + IEnumerable second = [2, 4, 8]; Func func = (x, y) => x + y; - IEnumerable expected = new int?[] { null, 6, 11 }; + IEnumerable expected = [null, 6, 11]; Assert.Equal(expected, first.Zip(second, func)); } @@ -260,10 +260,10 @@ public void FirstHasFirstElementNull() [Fact] public void FirstHasLastElementNull() { - IEnumerable first = new[] { 1, 2, (int?)null }; - IEnumerable second = new int[] { 2, 4, 6, 8 }; + IEnumerable first = [1, 2, (int?)null]; + IEnumerable second = [2, 4, 6, 8]; Func func = (x, y) => x + y; - IEnumerable expected = new int?[] { 3, 6, null }; + IEnumerable expected = [3, 6, null]; Assert.Equal(expected, first.Zip(second, func)); } @@ -271,10 +271,10 @@ public void FirstHasLastElementNull() [Fact] public void FirstHasMiddleNullValue() { - IEnumerable first = new[] { 1, (int?)null, 3 }; - IEnumerable second = new int[] { 2, 4, 6, 8 }; + IEnumerable first = [1, (int?)null, 3]; + IEnumerable second = [2, 4, 6, 8]; Func func = (x, y) => x + y; - IEnumerable expected = new int?[] { 3, null, 9 }; + IEnumerable expected = [3, null, 9]; Assert.Equal(expected, first.Zip(second, func)); } @@ -282,10 +282,10 @@ public void FirstHasMiddleNullValue() [Fact] public void FirstAllElementsNull() { - IEnumerable first = new int?[] { null, null, null }; - IEnumerable second = new int[] { 2, 4, 6, 8 }; + IEnumerable first = [null, null, null]; + IEnumerable second = [2, 4, 6, 8]; Func func = (x, y) => x + y; - IEnumerable expected = new int?[] { null, null, null }; + IEnumerable expected = [null, null, null]; Assert.Equal(expected, first.Zip(second, func)); } @@ -293,10 +293,10 @@ public void FirstAllElementsNull() [Fact] public void SecondHasFirstElementNull() { - IEnumerable first = new int[] { 1, 2, 3, 4 }; - IEnumerable second = new int?[] { null, 4, 6 }; + IEnumerable first = [1, 2, 3, 4]; + IEnumerable second = [null, 4, 6]; Func func = (x, y) => x + y; - IEnumerable expected = new int?[] { null, 6, 9 }; + IEnumerable expected = [null, 6, 9]; Assert.Equal(expected, first.Zip(second, func)); } @@ -304,10 +304,10 @@ public void SecondHasFirstElementNull() [Fact] public void SecondHasLastElementNull() { - IEnumerable first = new int[] { 1, 2, 3, 4 }; - IEnumerable second = new int?[] { 2, 4, null }; + IEnumerable first = [1, 2, 3, 4]; + IEnumerable second = [2, 4, null]; Func func = (x, y) => x + y; - IEnumerable expected = new int?[] { 3, 6, null }; + IEnumerable expected = [3, 6, null]; Assert.Equal(expected, first.Zip(second, func)); } @@ -315,10 +315,10 @@ public void SecondHasLastElementNull() [Fact] public void SecondHasMiddleElementNull() { - IEnumerable first = new int[] { 1, 2, 3, 4 }; - IEnumerable second = new int?[] { 2, null, 6 }; + IEnumerable first = [1, 2, 3, 4]; + IEnumerable second = [2, null, 6]; Func func = (x, y) => x + y; - IEnumerable expected = new int?[] { 3, null, 9 }; + IEnumerable expected = [3, null, 9]; Assert.Equal(expected, first.Zip(second, func)); } @@ -326,10 +326,10 @@ public void SecondHasMiddleElementNull() [Fact] public void SecondHasAllElementsNull() { - IEnumerable first = new int[] { 1, 2, 3, 4 }; - IEnumerable second = new int?[] { null, null, null }; + IEnumerable first = [1, 2, 3, 4]; + IEnumerable second = [null, null, null]; Func func = (x, y) => x + y; - IEnumerable expected = new int?[] { null, null, null }; + IEnumerable expected = [null, null, null]; Assert.Equal(expected, first.Zip(second, func)); } @@ -337,10 +337,10 @@ public void SecondHasAllElementsNull() [Fact] public void SecondLargerFirstAllNull() { - IEnumerable first = new int?[] { null, null, null, null }; - IEnumerable second = new int?[] { null, null, null }; + IEnumerable first = [null, null, null, null]; + IEnumerable second = [null, null, null]; Func func = (x, y) => x + y; - IEnumerable expected = new int?[] { null, null, null }; + IEnumerable expected = [null, null, null]; Assert.Equal(expected, first.Zip(second, func)); } @@ -349,10 +349,10 @@ public void SecondLargerFirstAllNull() [Fact] public void FirstSameSizeSecondAllNull() { - IEnumerable first = new int?[] { null, null, null }; - IEnumerable second = new int?[] { null, null, null }; + IEnumerable first = [null, null, null]; + IEnumerable second = [null, null, null]; Func func = (x, y) => x + y; - IEnumerable expected = new int?[] { null, null, null }; + IEnumerable expected = [null, null, null]; Assert.Equal(expected, first.Zip(second, func)); } @@ -360,10 +360,10 @@ public void FirstSameSizeSecondAllNull() [Fact] public void FirstSmallerSecondAllNull() { - IEnumerable first = new int?[] { null, null, null }; - IEnumerable second = new int?[] { null, null, null, null }; + IEnumerable first = [null, null, null]; + IEnumerable second = [null, null, null, null]; Func func = (x, y) => x + y; - IEnumerable expected = new int?[] { null, null, null }; + IEnumerable expected = [null, null, null]; Assert.Equal(expected, first.Zip(second, func)); } @@ -380,10 +380,10 @@ public void ForcedToEnumeratorDoesntEnumerate() [Fact] public void RunOnce() { - IEnumerable first = new[] { 1, (int?)null, 3 }; - IEnumerable second = new[] { 2, 4, 6, 8 }; + IEnumerable first = [1, (int?)null, 3]; + IEnumerable second = [2, 4, 6, 8]; Func func = (x, y) => x + y; - IEnumerable expected = new int?[] { 3, null, 9 }; + IEnumerable expected = [3, null, 9]; Assert.Equal(expected, first.RunOnce().Zip(second.RunOnce(), func)); } @@ -391,9 +391,9 @@ public void RunOnce() [Fact] public void Zip2_ImplicitTypeParameters() { - IEnumerable first = new int[] { 1, 2, 3 }; - IEnumerable second = new int[] { 2, 5, 9 }; - IEnumerable<(int, int)> expected = new (int, int)[] { (1, 2), (2, 5), (3, 9) }; + IEnumerable first = [1, 2, 3]; + IEnumerable second = [2, 5, 9]; + IEnumerable<(int, int)> expected = [(1, 2), (2, 5), (3, 9)]; Assert.Equal(expected, first.Zip(second)); } @@ -401,9 +401,9 @@ public void Zip2_ImplicitTypeParameters() [Fact] public void Zip2_ExplicitTypeParameters() { - IEnumerable first = new int[] { 1, 2, 3 }; - IEnumerable second = new int[] { 2, 5, 9 }; - IEnumerable<(int, int)> expected = new (int, int)[] { (1, 2), (2, 5), (3, 9) }; + IEnumerable first = [1, 2, 3]; + IEnumerable second = [2, 5, 9]; + IEnumerable<(int, int)> expected = [(1, 2), (2, 5), (3, 9)]; Assert.Equal(expected, first.Zip(second)); } @@ -412,7 +412,7 @@ public void Zip2_ExplicitTypeParameters() public void Zip2_FirstIsNull() { IEnumerable first = null; - IEnumerable second = new int[] { 2, 5, 9 }; + IEnumerable second = [2, 5, 9]; AssertExtensions.Throws("first", () => first.Zip(second)); } @@ -420,7 +420,7 @@ public void Zip2_FirstIsNull() [Fact] public void Zip2_SecondIsNull() { - IEnumerable first = new int[] { 1, 2, 3 }; + IEnumerable first = [1, 2, 3]; IEnumerable second = null; AssertExtensions.Throws("second", () => first.Zip(second)); @@ -429,13 +429,13 @@ public void Zip2_SecondIsNull() [Fact] public void Zip2_ExceptionThrownFromFirstsEnumerator() { - ThrowsOnMatchEnumerable first = new ThrowsOnMatchEnumerable(new int[] { 1, 3, 3 }, 2); - IEnumerable second = new int[] { 2, 4, 6 }; - IEnumerable<(int, int)> expected = new (int, int)[] { (1, 2), (3, 4), (3, 6) }; + ThrowsOnMatchEnumerable first = new ThrowsOnMatchEnumerable([1, 3, 3], 2); + IEnumerable second = [2, 4, 6]; + IEnumerable<(int, int)> expected = [(1, 2), (3, 4), (3, 6)]; Assert.Equal(expected, first.Zip(second)); - first = new ThrowsOnMatchEnumerable(new int[] { 1, 2, 3 }, 2); + first = new ThrowsOnMatchEnumerable([1, 2, 3], 2); IEnumerable<(int, int)> zip = first.Zip(second); @@ -445,13 +445,13 @@ public void Zip2_ExceptionThrownFromFirstsEnumerator() [Fact] public void Zip2_ExceptionThrownFromSecondsEnumerator() { - ThrowsOnMatchEnumerable second = new ThrowsOnMatchEnumerable(new int[] { 1, 3, 3 }, 2); - IEnumerable first = new int[] { 2, 4, 6 }; - IEnumerable<(int, int)> expected = new (int, int)[] { (2, 1), (4, 3), (6, 3) }; + ThrowsOnMatchEnumerable second = new ThrowsOnMatchEnumerable([1, 3, 3], 2); + IEnumerable first = [2, 4, 6]; + IEnumerable<(int, int)> expected = [(2, 1), (4, 3), (6, 3)]; Assert.Equal(expected, first.Zip(second)); - second = new ThrowsOnMatchEnumerable(new int[] { 1, 2, 3 }, 2); + second = new ThrowsOnMatchEnumerable([1, 2, 3], 2); IEnumerable<(int, int)> zip = first.Zip(second); @@ -461,9 +461,9 @@ public void Zip2_ExceptionThrownFromSecondsEnumerator() [Fact] public void Zip2_FirstAndSecondEmpty() { - IEnumerable first = new int[] { }; - IEnumerable second = new int[] { }; - IEnumerable<(int, int)> expected = new (int, int)[] { }; + IEnumerable first = []; + IEnumerable second = []; + IEnumerable<(int, int)> expected = []; Assert.Equal(expected, first.Zip(second)); } @@ -471,9 +471,9 @@ public void Zip2_FirstAndSecondEmpty() [Fact] public void Zip2_FirstEmptySecondSingle() { - IEnumerable first = new int[] { }; - IEnumerable second = new int[] { 2 }; - IEnumerable<(int, int)> expected = new (int, int)[] { }; + IEnumerable first = []; + IEnumerable second = [2]; + IEnumerable<(int, int)> expected = []; Assert.Equal(expected, first.Zip(second)); } @@ -481,9 +481,9 @@ public void Zip2_FirstEmptySecondSingle() [Fact] public void Zip2_FirstEmptySecondMany() { - IEnumerable first = new int[] { }; - IEnumerable second = new int[] { 2, 4, 8 }; - IEnumerable<(int, int)> expected = new (int, int)[] { }; + IEnumerable first = []; + IEnumerable second = [2, 4, 8]; + IEnumerable<(int, int)> expected = []; Assert.Equal(expected, first.Zip(second)); } @@ -491,9 +491,9 @@ public void Zip2_FirstEmptySecondMany() [Fact] public void Zip2_SecondEmptyFirstSingle() { - IEnumerable first = new int[] { 1 }; - IEnumerable second = new int[] { }; - IEnumerable<(int, int)> expected = new (int, int)[] { }; + IEnumerable first = [1]; + IEnumerable second = []; + IEnumerable<(int, int)> expected = []; Assert.Equal(expected, first.Zip(second)); } @@ -501,9 +501,9 @@ public void Zip2_SecondEmptyFirstSingle() [Fact] public void Zip2_SecondEmptyFirstMany() { - IEnumerable first = new int[] { 1, 2, 3 }; - IEnumerable second = new int[] { }; - IEnumerable<(int, int)> expected = new (int, int)[] { }; + IEnumerable first = [1, 2, 3]; + IEnumerable second = []; + IEnumerable<(int, int)> expected = []; Assert.Equal(expected, first.Zip(second)); } @@ -511,9 +511,9 @@ public void Zip2_SecondEmptyFirstMany() [Fact] public void Zip2_FirstAndSecondSingle() { - IEnumerable first = new int[] { 1 }; - IEnumerable second = new int[] { 2 }; - IEnumerable<(int, int)> expected = new (int, int)[] { (1, 2) }; + IEnumerable first = [1]; + IEnumerable second = [2]; + IEnumerable<(int, int)> expected = [(1, 2)]; Assert.Equal(expected, first.Zip(second)); } @@ -521,9 +521,9 @@ public void Zip2_FirstAndSecondSingle() [Fact] public void Zip2_FirstAndSecondEqualSize() { - IEnumerable first = new int[] { 1, 2, 3 }; - IEnumerable second = new int[] { 2, 3, 4 }; - IEnumerable<(int, int)> expected = new (int, int)[] { (1, 2), (2, 3), (3, 4) }; + IEnumerable first = [1, 2, 3]; + IEnumerable second = [2, 3, 4]; + IEnumerable<(int, int)> expected = [(1, 2), (2, 3), (3, 4)]; Assert.Equal(expected, first.Zip(second)); } @@ -531,9 +531,9 @@ public void Zip2_FirstAndSecondEqualSize() [Fact] public void Zip2_SecondOneMoreThanFirst() { - IEnumerable first = new int[] { 1, 2 }; - IEnumerable second = new int[] { 2, 4, 8 }; - IEnumerable<(int, int)> expected = new (int, int)[] { (1, 2), (2, 4) }; + IEnumerable first = [1, 2]; + IEnumerable second = [2, 4, 8]; + IEnumerable<(int, int)> expected = [(1, 2), (2, 4)]; Assert.Equal(expected, first.Zip(second)); } @@ -542,9 +542,9 @@ public void Zip2_SecondOneMoreThanFirst() [Fact] public void Zip2_SecondManyMoreThanFirst() { - IEnumerable first = new int[] { 1, 2 }; - IEnumerable second = new int[] { 2, 4, 8, 16 }; - IEnumerable<(int, int)> expected = new (int, int)[] { (1, 2), (2, 4) }; + IEnumerable first = [1, 2]; + IEnumerable second = [2, 4, 8, 16]; + IEnumerable<(int, int)> expected = [(1, 2), (2, 4)]; Assert.Equal(expected, first.Zip(second)); } @@ -552,9 +552,9 @@ public void Zip2_SecondManyMoreThanFirst() [Fact] public void Zip2_FirstOneMoreThanSecond() { - IEnumerable first = new int[] { 1, 2, 3 }; - IEnumerable second = new int[] { 2, 4 }; - IEnumerable<(int, int)> expected = new (int, int)[] { (1, 2), (2, 4) }; + IEnumerable first = [1, 2, 3]; + IEnumerable second = [2, 4]; + IEnumerable<(int, int)> expected = [(1, 2), (2, 4)]; Assert.Equal(expected, first.Zip(second)); } @@ -562,9 +562,9 @@ public void Zip2_FirstOneMoreThanSecond() [Fact] public void Zip2_FirstManyMoreThanSecond() { - IEnumerable first = new int[] { 1, 2, 3, 4 }; - IEnumerable second = new int[] { 2, 4 }; - IEnumerable<(int, int)> expected = new (int, int)[] { (1, 2), (2, 4) }; + IEnumerable first = [1, 2, 3, 4]; + IEnumerable second = [2, 4]; + IEnumerable<(int, int)> expected = [(1, 2), (2, 4)]; Assert.Equal(expected, first.Zip(second)); } @@ -572,9 +572,9 @@ public void Zip2_FirstManyMoreThanSecond() [Fact] public void Zip2_RunOnce() { - IEnumerable first = new[] { 1, (int?)null, 3 }; - IEnumerable second = new[] { 2, 4, 6, 8 }; - IEnumerable<(int?, int)> expected = new (int?, int)[] { (1, 2), (null, 4), (3, 6) }; + IEnumerable first = [1, (int?)null, 3]; + IEnumerable second = [2, 4, 6, 8]; + IEnumerable<(int?, int)> expected = [(1, 2), (null, 4), (3, 6)]; Assert.Equal(expected, first.RunOnce().Zip(second.RunOnce())); } @@ -582,22 +582,22 @@ public void Zip2_RunOnce() [Fact] public void Zip2_NestedTuple() { - IEnumerable first = new[] { 1, 3, 5 }; - IEnumerable second = new[] { 2, 4, 6 }; - IEnumerable<(int, int)> third = new[] { (1, 2), (3, 4), (5, 6) }; + IEnumerable first = [1, 3, 5]; + IEnumerable second = [2, 4, 6]; + IEnumerable<(int, int)> third = [(1, 2), (3, 4), (5, 6)]; Assert.Equal(third, first.Zip(second)); - IEnumerable fourth = new[] { "one", "two", "three" }; + IEnumerable fourth = ["one", "two", "three"]; - IEnumerable<((int, int), string)> final = new[] { ((1, 2), "one"), ((3, 4), "two"), ((5, 6), "three") }; + IEnumerable<((int, int), string)> final = [((1, 2), "one"), ((3, 4), "two"), ((5, 6), "three")]; Assert.Equal(final, third.Zip(fourth)); } [Fact] public void Zip2_TupleNames() { - var t = new[] { 1, 2, 3 }.Zip(new[] { 2, 4, 6 }).First(); + var t = new[] { 1, 2, 3 }.Zip([2, 4, 6]).First(); Assert.Equal(t.Item1, t.First); Assert.Equal(t.Item2, t.Second); } @@ -606,8 +606,8 @@ public void Zip2_TupleNames() public void Zip3_FirstIsNull() { IEnumerable first = null; - IEnumerable second = new[] { 4, 5, 6 }; - IEnumerable third = new[] { 7, 8, 9 }; + IEnumerable second = [4, 5, 6]; + IEnumerable third = [7, 8, 9]; AssertExtensions.Throws("first", () => first.Zip(second, third)); } @@ -615,9 +615,9 @@ public void Zip3_FirstIsNull() [Fact] public void Zip3_SecondIsNull() { - IEnumerable first = new[] { 1, 2, 3 }; + IEnumerable first = [1, 2, 3]; IEnumerable second = null; - IEnumerable third = new[] { 4, 5, 6 }; + IEnumerable third = [4, 5, 6]; AssertExtensions.Throws("second", () => first.Zip(second, third)); } @@ -625,8 +625,8 @@ public void Zip3_SecondIsNull() [Fact] public void Zip3_ThirdIsNull() { - IEnumerable first = new[] { 1, 2, 3 }; - IEnumerable second = new[] { 4, 5, 6 }; + IEnumerable first = [1, 2, 3]; + IEnumerable second = [4, 5, 6]; IEnumerable third = null; AssertExtensions.Throws("third", () => first.Zip(second, third)); @@ -635,10 +635,10 @@ public void Zip3_ThirdIsNull() [Fact] public void Zip3_ThirdEmpty() { - IEnumerable first = new[] { 1, 2, 3 }; - IEnumerable second = new[] { 4, 5, 6 }; - IEnumerable third = new int[] { }; - IEnumerable<(int, int, int)> expected = new (int, int, int)[] { }; + IEnumerable first = [1, 2, 3]; + IEnumerable second = [4, 5, 6]; + IEnumerable third = []; + IEnumerable<(int, int, int)> expected = []; Assert.Equal(expected, first.Zip(second, third)); } @@ -646,10 +646,10 @@ public void Zip3_ThirdEmpty() [Fact] public void Zip3_ImplicitTypeParameters() { - IEnumerable first = new[] { 1, 2 }; - IEnumerable second = new[] { 3, 4 }; - IEnumerable third = new[] { 5, 6 }; - IEnumerable<(int, int, int)> expected = new[] { (1, 3, 5), (2, 4, 6) }; + IEnumerable first = [1, 2]; + IEnumerable second = [3, 4]; + IEnumerable third = [5, 6]; + IEnumerable<(int, int, int)> expected = [(1, 3, 5), (2, 4, 6)]; Assert.Equal(expected, first.Zip(second, third)); } @@ -657,10 +657,10 @@ public void Zip3_ImplicitTypeParameters() [Fact] public void Zip3_ExplicitTypeParameters() { - IEnumerable first = new[] { 1, 2 }; - IEnumerable second = new[] { 3, 4 }; - IEnumerable third = new[] { 5, 6 }; - IEnumerable<(int, int, int)> expected = new[] { (1, 3, 5), (2, 4, 6) }; + IEnumerable first = [1, 2]; + IEnumerable second = [3, 4]; + IEnumerable third = [5, 6]; + IEnumerable<(int, int, int)> expected = [(1, 3, 5), (2, 4, 6)]; Assert.Equal(expected, first.Zip(second, third)); } @@ -668,10 +668,10 @@ public void Zip3_ExplicitTypeParameters() [Fact] public void Zip3_ThirdOneMore() { - IEnumerable first = new[] { 1, 2 }; - IEnumerable second = new[] { 3, 4 }; - IEnumerable third = new[] { 5, 6, 7 }; - IEnumerable<(int, int, int)> expected = new[] { (1, 3, 5), (2, 4, 6) }; + IEnumerable first = [1, 2]; + IEnumerable second = [3, 4]; + IEnumerable third = [5, 6, 7]; + IEnumerable<(int, int, int)> expected = [(1, 3, 5), (2, 4, 6)]; Assert.Equal(expected, first.Zip(second, third)); } @@ -679,10 +679,10 @@ public void Zip3_ThirdOneMore() [Fact] public void Zip3_ThirdManyMore() { - IEnumerable first = new[] { 1, 2 }; - IEnumerable second = new[] { 3, 4 }; - IEnumerable third = new[] { 5, 6, 7, 8 }; - IEnumerable<(int, int, int)> expected = new[] { (1, 3, 5), (2, 4, 6) }; + IEnumerable first = [1, 2]; + IEnumerable second = [3, 4]; + IEnumerable third = [5, 6, 7, 8]; + IEnumerable<(int, int, int)> expected = [(1, 3, 5), (2, 4, 6)]; Assert.Equal(expected, first.Zip(second, third)); } @@ -690,10 +690,10 @@ public void Zip3_ThirdManyMore() [Fact] public void Zip3_ThirdOneLess() { - IEnumerable first = new[] { 1, 2 }; - IEnumerable second = new[] { 3, 4 }; - IEnumerable third = new[] { 5 }; - IEnumerable<(int, int, int)> expected = new[] { (1, 3, 5) }; + IEnumerable first = [1, 2]; + IEnumerable second = [3, 4]; + IEnumerable third = [5]; + IEnumerable<(int, int, int)> expected = [(1, 3, 5)]; Assert.Equal(expected, first.Zip(second, third)); } @@ -701,10 +701,10 @@ public void Zip3_ThirdOneLess() [Fact] public void Zip3_ThirdManyLess() { - IEnumerable first = new[] { 1, 2, 3 }; - IEnumerable second = new[] { 3, 4, 5 }; - IEnumerable third = new[] { 5 }; - IEnumerable<(int, int, int)> expected = new[] { (1, 3, 5) }; + IEnumerable first = [1, 2, 3]; + IEnumerable second = [3, 4, 5]; + IEnumerable third = [5]; + IEnumerable<(int, int, int)> expected = [(1, 3, 5)]; Assert.Equal(expected, first.Zip(second, third)); } @@ -712,10 +712,10 @@ public void Zip3_ThirdManyLess() [Fact] public void Zip3_RunOnce() { - IEnumerable first = new[] { 1, 2 }; - IEnumerable second = new[] { 3, 4 }; - IEnumerable third = new[] { 5, 6 }; - IEnumerable<(int, int, int)> expected = new[] { (1, 3, 5), (2, 4, 6) }; + IEnumerable first = [1, 2]; + IEnumerable second = [3, 4]; + IEnumerable third = [5, 6]; + IEnumerable<(int, int, int)> expected = [(1, 3, 5), (2, 4, 6)]; Assert.Equal(expected, first.RunOnce().Zip(second.RunOnce(), third.RunOnce())); } diff --git a/tests/System.Linq.Tests/Tests/ZLinq/AggregateByTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/AggregateByTests.cs index 06702145..b2e1b784 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/AggregateByTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/AggregateByTests.cs @@ -13,8 +13,8 @@ public void Empty() { Assert.All(IdentityTransforms(), transform => { - Assert.Equal(Enumerable.Empty>(), transform(Enumerable.Empty()).AggregateBy(i => i, i => i, (a, i) => a + i)); - Assert.Equal(Enumerable.Empty>(), transform(Enumerable.Empty()).AggregateBy(i => i, 0, (a, i) => a + i)); + Assert.Equal([], transform([]).AggregateBy(i => i, i => i, (a, i) => a + i)); + Assert.Equal([], transform([]).AggregateBy(i => i, 0, (a, i) => a + i)); }); } @@ -68,7 +68,7 @@ public void AggregateBy_SourceThrowsOnGetEnumerator() { IEnumerable source = new ThrowsOnGetEnumerator(); - var enumerator = source.AggregateBy(x => x, 0, (x, y) => x + y).GetEnumerator(); + using var enumerator = source.AggregateBy(x => x, 0, (x, y) => x + y).GetEnumerator(); try { @@ -85,7 +85,7 @@ public void AggregateBy_SourceThrowsOnMoveNext() { IEnumerable source = new ThrowsOnMoveNext(); - var enumerator = source.AggregateBy(x => x, 0, (x, y) => x + y).GetEnumerator(); + using var enumerator = source.AggregateBy(x => x, 0, (x, y) => x + y).GetEnumerator(); try { @@ -102,7 +102,7 @@ public void AggregateBy_SourceThrowsOnCurrent() { IEnumerable source = new ThrowsOnCurrentEnumerator(); - var enumerator = source.AggregateBy(x => x, 0, (x, y) => x + y).GetEnumerator(); + using var enumerator = source.AggregateBy(x => x, 0, (x, y) => x + y).GetEnumerator(); try { @@ -123,7 +123,7 @@ public void AggregateBy_HasExpectedOutput() seedSelector: x => 0, func: (x, y) => x + y, comparer: null, - expected: Enumerable.Empty>()); + expected: []); Validate( source: Enumerable.Range(0, 10), @@ -158,7 +158,7 @@ public void AggregateBy_HasExpectedOutput() expected: Enumerable.Repeat(5, 1).Select(x => new KeyValuePair(x, 100)).ToArray()); Validate( - source: new string[] { "Bob", "bob", "tim", "Bob", "Tim" }, + source: ["Bob", "bob", "tim", "Bob", "Tim"], keySelector: x => x, seedSelector: x => string.Empty, func: (x, y) => x + y, @@ -172,7 +172,7 @@ public void AggregateBy_HasExpectedOutput() ]); Validate( - source: new string[] { "Bob", "bob", "tim", "Bob", "Tim" }, + source: ["Bob", "bob", "tim", "Bob", "Tim"], keySelector: x => x, seedSelector: x => string.Empty, func: (x, y) => x + y, @@ -244,7 +244,7 @@ public void GroupBy() seedSelector: _ => new List(), func: (group, element) => { group.Add(element); return group; }); - var e = oddsEvens.GetEnumerator(); + using var e = oddsEvens.GetEnumerator(); Assert.True(e.MoveNext()); KeyValuePair> oddsItem = e.Current; @@ -274,7 +274,7 @@ public void LongCountBy() seed: 0L, func: (count, _) => ++count); - var e = oddsEvens.GetEnumerator(); + using var e = oddsEvens.GetEnumerator(); Assert.True(e.MoveNext()); KeyValuePair oddsItem = e.Current; diff --git a/tests/System.Linq.Tests/Tests/ZLinq/AggregateTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/AggregateTests.cs index 16409bfd..067977f0 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/AggregateTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/AggregateTests.cs @@ -32,7 +32,7 @@ public void SameResultsRepeatCallsStringQuery() [Fact] public void EmptySource() { - int[] source = { }; + int[] source = []; Assert.All(CreateSources(source), source => { @@ -44,7 +44,7 @@ public void EmptySource() [Fact] public void SingleElement() { - int[] source = { 5 }; + int[] source = [5]; int expected = 5; Assert.All(CreateSources(source), source => @@ -56,7 +56,7 @@ public void SingleElement() [Fact] public void SingleElementRunOnce() { - int[] source = { 5 }; + int[] source = [5]; int expected = 5; Assert.All(CreateSources(source), source => @@ -68,7 +68,7 @@ public void SingleElementRunOnce() [Fact] public void TwoElements() { - int[] source = { 5, 6 }; + int[] source = [5, 6]; int expected = 11; Assert.All(CreateSources(source), source => @@ -80,7 +80,7 @@ public void TwoElements() [Fact] public void MultipleElements() { - int[] source = { 5, 6, 0, -4 }; + int[] source = [5, 6, 0, -4]; int expected = 7; Assert.All(CreateSources(source), source => @@ -92,7 +92,7 @@ public void MultipleElements() [Fact] public void MultipleElementsRunOnce() { - int[] source = { 5, 6, 0, -4 }; + int[] source = [5, 6, 0, -4]; int expected = 7; Assert.All(CreateSources(source), source => @@ -104,7 +104,7 @@ public void MultipleElementsRunOnce() [Fact] public void EmptySourceAndSeed() { - int[] source = { }; + int[] source = []; long seed = 2; long expected = 2; @@ -117,7 +117,7 @@ public void EmptySourceAndSeed() [Fact] public void SingleElementAndSeed() { - int[] source = { 5 }; + int[] source = [5]; long seed = 2; long expected = 10; @@ -130,7 +130,7 @@ public void SingleElementAndSeed() [Fact] public void TwoElementsAndSeed() { - int[] source = { 5, 6 }; + int[] source = [5, 6]; long seed = 2; long expected = 60; @@ -143,7 +143,7 @@ public void TwoElementsAndSeed() [Fact] public void MultipleElementsAndSeed() { - int[] source = { 5, 6, 2, -4 }; + int[] source = [5, 6, 2, -4]; long seed = 2; long expected = -480; @@ -156,7 +156,7 @@ public void MultipleElementsAndSeed() [Fact] public void MultipleElementsAndSeedRunOnce() { - int[] source = { 5, 6, 2, -4 }; + int[] source = [5, 6, 2, -4]; long seed = 2; long expected = -480; @@ -169,7 +169,7 @@ public void MultipleElementsAndSeedRunOnce() [Fact] public void NoElementsSeedResultSeletor() { - int[] source = { }; + int[] source = []; long seed = 2; double expected = 7; @@ -182,7 +182,7 @@ public void NoElementsSeedResultSeletor() [Fact] public void SingleElementSeedResultSelector() { - int[] source = { 5 }; + int[] source = [5]; long seed = 2; long expected = 15; @@ -195,7 +195,7 @@ public void SingleElementSeedResultSelector() [Fact] public void TwoElementsSeedResultSelector() { - int[] source = { 5, 6 }; + int[] source = [5, 6]; long seed = 2; long expected = 65; @@ -208,7 +208,7 @@ public void TwoElementsSeedResultSelector() [Fact] public void MultipleElementsSeedResultSelector() { - int[] source = { 5, 6, 2, -4 }; + int[] source = [5, 6, 2, -4]; long seed = 2; long expected = -475; @@ -221,7 +221,7 @@ public void MultipleElementsSeedResultSelector() [Fact] public void MultipleElementsSeedResultSelectorRunOnce() { - int[] source = { 5, 6, 2, -4 }; + int[] source = [5, 6, 2, -4]; long seed = 2; long expected = -475; diff --git a/tests/System.Linq.Tests/Tests/ZLinq/AllTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/AllTests.cs index 3c1cb146..c41d0ed2 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/AllTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/AllTests.cs @@ -32,20 +32,20 @@ public void SameResultsRepeatCallsStringQuery() public static IEnumerable All_TestData() { Func isEvenFunc = IsEven; - yield return new object[] { new int[0], isEvenFunc, true }; - yield return new object[] { new int[] { 3 }, isEvenFunc, false }; - yield return new object[] { new int[] { 4 }, isEvenFunc, true }; - yield return new object[] { new int[] { 3 }, isEvenFunc, false }; - yield return new object[] { new int[] { 4, 8, 3, 5, 10, 20, 12 }, isEvenFunc, false }; - yield return new object[] { new int[] { 4, 2, 10, 12, 8, 6, 3 }, isEvenFunc, false }; - yield return new object[] { new int[] { 4, 2, 10, 12, 8, 6, 14 }, isEvenFunc, true }; + yield return [new int[0], isEvenFunc, true]; + yield return [new int[] { 3 }, isEvenFunc, false]; + yield return [new int[] { 4 }, isEvenFunc, true]; + yield return [new int[] { 3 }, isEvenFunc, false]; + yield return [new int[] { 4, 8, 3, 5, 10, 20, 12 }, isEvenFunc, false]; + yield return [new int[] { 4, 2, 10, 12, 8, 6, 3 }, isEvenFunc, false]; + yield return [new int[] { 4, 2, 10, 12, 8, 6, 14 }, isEvenFunc, true]; int[] range = Enumerable.Range(1, 10).ToArray(); - yield return new object[] { range, (Func)(i => i > 0), true }; + yield return [range, (Func)(i => i > 0), true]; for (int j = 1; j <= 10; j++) { int k = j; // Local copy for iterator - yield return new object[] { range, (Func)(i => i > k), false }; + yield return [range, (Func)(i => i > k), false]; } } diff --git a/tests/System.Linq.Tests/Tests/ZLinq/AnyTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/AnyTests.cs index 8f40e71f..96aa5404 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/AnyTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/AnyTests.cs @@ -56,22 +56,22 @@ public void Any_GroupBy() public static IEnumerable TestDataWithPredicate() { - yield return new object[] { new int[0], null, false }; - yield return new object[] { new int[] { 3 }, null, true }; + yield return [new int[0], null, false]; + yield return [new int[] { 3 }, null, true]; Func isEvenFunc = IsEven; - yield return new object[] { new int[0], isEvenFunc, false }; - yield return new object[] { new int[] { 4 }, isEvenFunc, true }; - yield return new object[] { new int[] { 5 }, isEvenFunc, false }; - yield return new object[] { new int[] { 5, 9, 3, 7, 4 }, isEvenFunc, true }; - yield return new object[] { new int[] { 5, 8, 9, 3, 7, 11 }, isEvenFunc, true }; + yield return [new int[0], isEvenFunc, false]; + yield return [new int[] { 4 }, isEvenFunc, true]; + yield return [new int[] { 5 }, isEvenFunc, false]; + yield return [new int[] { 5, 9, 3, 7, 4 }, isEvenFunc, true]; + yield return [new int[] { 5, 8, 9, 3, 7, 11 }, isEvenFunc, true]; int[] range = Enumerable.Range(1, 10).ToArray(); - yield return new object[] { range, (Func)(i => i > 10), false }; + yield return [range, (Func)(i => i > 10), false]; for (int j = 0; j <= 9; j++) { int k = j; // Local copy for iterator - yield return new object[] { range, (Func)(i => i > k), true }; + yield return [range, (Func)(i => i > k), true]; } } @@ -111,7 +111,7 @@ public void AnyRunOnce(IEnumerable source, Func predicate, bool [Fact] public void NullObjectsInArray_Included() { - int?[] source = { null, null, null, null }; + int?[] source = [null, null, null, null]; Assert.All(CreateSources(source), source => { Assert.True(source.Any()); diff --git a/tests/System.Linq.Tests/Tests/ZLinq/AppendPrependTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/AppendPrependTests.cs index 8983f75c..794de646 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/AppendPrependTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/AppendPrependTests.cs @@ -16,7 +16,7 @@ public void SameResultsRepeatCallsIntQueryAppend() select x1; Assert.Equal(q1.Append(42), q1.Append(42)); - Assert.Equal(q1.Append(42), q1.Concat(new int?[] { 42 })); + Assert.Equal(q1.Append(42), q1.Concat([42])); } [Fact] @@ -36,7 +36,7 @@ public void SameResultsRepeatCallsStringQueryAppend() select x1; Assert.Equal(q1.Append("hi"), q1.Append("hi")); - Assert.Equal(q1.Append("hi"), q1.Concat(new string[] { "hi" })); + Assert.Equal(q1.Append("hi"), q1.Concat(["hi"])); } [Fact] @@ -61,7 +61,7 @@ public void RepeatIteration() [Fact] public void EmptyAppend() { - int[] first = { }; + int[] first = []; Assert.All(CreateSources(first), first => { Assert.Single(first.Append(42), 42); @@ -71,7 +71,7 @@ public void EmptyAppend() [Fact] public void EmptyPrepend() { - string[] first = { }; + string[] first = []; Assert.All(CreateSources(first), first => { Assert.Single(first.Prepend("aa"), "aa"); @@ -150,12 +150,12 @@ public void AppendCombinations() var app1ba = app0b.Append(9); var app1bb = app0b.Append(10); - Assert.Equal(new[] { 0, 1, 2, 3, 4, 5 }, app0a); - Assert.Equal(new[] { 0, 1, 2, 3, 4, 6 }, app0b); - Assert.Equal(new[] { 0, 1, 2, 3, 4, 5, 7 }, app1aa); - Assert.Equal(new[] { 0, 1, 2, 3, 4, 5, 8 }, app1ab); - Assert.Equal(new[] { 0, 1, 2, 3, 4, 6, 9 }, app1ba); - Assert.Equal(new[] { 0, 1, 2, 3, 4, 6, 10 }, app1bb); + Assert.Equal([0, 1, 2, 3, 4, 5], app0a); + Assert.Equal([0, 1, 2, 3, 4, 6], app0b); + Assert.Equal([0, 1, 2, 3, 4, 5, 7], app1aa); + Assert.Equal([0, 1, 2, 3, 4, 5, 8], app1ab); + Assert.Equal([0, 1, 2, 3, 4, 6, 9], app1ba); + Assert.Equal([0, 1, 2, 3, 4, 6, 10], app1bb); } [Fact] @@ -169,12 +169,12 @@ public void PrependCombinations() var pre1ba = pre0b.Prepend(9); var pre1bb = pre0b.Prepend(10); - Assert.Equal(new[] { 5, 0, 1, 2, 3 }, pre0a); - Assert.Equal(new[] { 6, 0, 1, 2, 3 }, pre0b); - Assert.Equal(new[] { 7, 5, 0, 1, 2, 3 }, pre1aa); - Assert.Equal(new[] { 8, 5, 0, 1, 2, 3 }, pre1ab); - Assert.Equal(new[] { 9, 6, 0, 1, 2, 3 }, pre1ba); - Assert.Equal(new[] { 10, 6, 0, 1, 2, 3 }, pre1bb); + Assert.Equal([5, 0, 1, 2, 3], pre0a); + Assert.Equal([6, 0, 1, 2, 3], pre0b); + Assert.Equal([7, 5, 0, 1, 2, 3], pre1aa); + Assert.Equal([8, 5, 0, 1, 2, 3], pre1ab); + Assert.Equal([9, 6, 0, 1, 2, 3], pre1ba); + Assert.Equal([10, 6, 0, 1, 2, 3], pre1bb); } [Fact] diff --git a/tests/System.Linq.Tests/Tests/ZLinq/AverageTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/AverageTests.cs index 6bb6bd85..fa5d0720 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/AverageTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/AverageTests.cs @@ -87,10 +87,10 @@ public void Int_EmptySource_ThrowsInvalidOperationException() { foreach (IEnumerable source in new IEnumerable[] { - Array.Empty(), + [], new List(), - Enumerable.Empty(), - new TestEnumerable(Array.Empty()) + [], + new TestEnumerable([]) }) { Assert.Throws(() => source.Average()); @@ -124,8 +124,7 @@ public static IEnumerable Int_TestData() for (int c = 1; c <= i; c++) sum += c; double expected = (double)sum / i; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(1, i)), expected }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(1, i).ToArray()), expected }; + yield return [Enumerable.Range(1, i).Shuffle().ToArray(), expected]; } } @@ -166,12 +165,12 @@ public void Int_WithSelector() public static IEnumerable NullableInt_TestData() { - yield return new object[] { new int?[0], null }; - yield return new object[] { new int?[] { -5 }, -5.0 }; - yield return new object[] { new int?[] { 0, 0, 0, 0, 0 }, 0.0 }; - yield return new object[] { new int?[] { 5, -10, null, null, null, 15, 40, 28, null, null }, 15.6 }; - yield return new object[] { new int?[] { null, null, null, null, 50 }, 50.0 }; - yield return new object[] { new int?[] { null, null, null, null, null }, null }; + yield return [new int?[0], null]; + yield return [new int?[] { -5 }, -5.0]; + yield return [new int?[] { 0, 0, 0, 0, 0 }, 0.0]; + yield return [new int?[] { 5, -10, null, null, null, 15, 40, 28, null, null }, 15.6]; + yield return [new int?[] { null, null, null, null, 50 }, 50.0]; + yield return [new int?[] { null, null, null, null, null }, null]; } [Theory] @@ -218,10 +217,10 @@ public void Long_EmptySource_ThrowsInvalidOperationException() { foreach (IEnumerable source in new IEnumerable[] { - Array.Empty(), + [], new List(), - Enumerable.Empty(), - new TestEnumerable(Array.Empty()) + [], + new TestEnumerable([]) }) { Assert.Throws(() => source.Average()); @@ -245,9 +244,9 @@ public void Long_NullSelector_ThrowsArgumentNullException() public static IEnumerable Long_TestData() { - yield return new object[] { new long[] { long.MaxValue }, long.MaxValue }; - yield return new object[] { new long[] { 0, 0, 0, 0, 0 }, 0 }; - yield return new object[] { new long[] { 5, -10, 15, 40, 28 }, 15.6 }; + yield return [new long[] { long.MaxValue }, long.MaxValue]; + yield return [new long[] { 0, 0, 0, 0, 0 }, 0]; + yield return [new long[] { 5, -10, 15, 40, 28 }, 15.6]; for (int i = 1; i <= 33; i++) { @@ -255,8 +254,7 @@ public static IEnumerable Long_TestData() for (int c = 1; c <= i; c++) sum += c; double expected = (double)sum / i; - // yield return new object[] { Shuffler.Shuffle(Enumerable.Range(1, i).Select(i => (long)i)), expected }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(1, i).Select(i => (long)i).ToArray()), expected }; + yield return [Enumerable.Range(1, i).Select(i => (long)i).Shuffle().ToArray(), expected]; } } @@ -288,19 +286,19 @@ public void Long_FromSelector() [Fact] public void Long_SumTooLarge_ThrowsOverflowException() { - long[] source = new long[] { long.MaxValue, long.MaxValue }; + long[] source = [long.MaxValue, long.MaxValue]; Assert.Throws(() => source.Average()); } public static IEnumerable NullableLong_TestData() { - yield return new object[] { new long?[0], null }; - yield return new object[] { new long?[] { long.MaxValue }, (double)long.MaxValue }; - yield return new object[] { new long?[] { 0, 0, 0, 0, 0 }, 0.0 }; - yield return new object[] { new long?[] { 5, -10, null, null, null, 15, 40, 28, null, null }, 15.6 }; - yield return new object[] { new long?[] { null, null, null, null, 50 }, 50.0 }; - yield return new object[] { new long?[] { null, null, null, null, null }, null }; + yield return [new long?[0], null]; + yield return [new long?[] { long.MaxValue }, (double)long.MaxValue]; + yield return [new long?[] { 0, 0, 0, 0, 0 }, 0.0]; + yield return [new long?[] { 5, -10, null, null, null, 15, 40, 28, null, null }, 15.6]; + yield return [new long?[] { null, null, null, null, 50 }, 50.0]; + yield return [new long?[] { null, null, null, null, null }, null]; } [Theory] @@ -347,10 +345,10 @@ public void Double_EmptySource_ThrowsInvalidOperationException() { foreach (IEnumerable source in new IEnumerable[] { - Array.Empty(), + [], new List(), - Enumerable.Empty(), - new TestEnumerable(Array.Empty()) + [], + new TestEnumerable([]) }) { Assert.Throws(() => source.Average()); @@ -374,10 +372,10 @@ public void Double_NullSelector_ThrowsArgumentNullException() public static IEnumerable Double_TestData() { - yield return new object[] { new double[] { double.MaxValue }, double.MaxValue }; - yield return new object[] { new double[] { 0.0, 0.0, 0.0, 0.0, 0.0 }, 0 }; - yield return new object[] { new double[] { 5.5, -10, 15.5, 40.5, 28.5 }, 16 }; - yield return new object[] { new double[] { 5.58, double.NaN, 30, 4.55, 19.38 }, double.NaN }; + yield return [new double[] { double.MaxValue }, double.MaxValue]; + yield return [new double[] { 0.0, 0.0, 0.0, 0.0, 0.0 }, 0]; + yield return [new double[] { 5.5, -10, 15.5, 40.5, 28.5 }, 16]; + yield return [new double[] { 5.58, double.NaN, 30, 4.55, 19.38 }, double.NaN]; for (int i = 1; i <= 33; i++) { @@ -385,8 +383,7 @@ public static IEnumerable Double_TestData() for (int c = 1; c <= i; c++) sum += c; double expected = (double)sum / i; - // yield return new object[] { Shuffler.Shuffle(Enumerable.Range(1, i).Select(i => (double)i)), expected }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(1, i).Select(i => (double)i).ToArray()), expected }; + yield return [Enumerable.Range(1, i).Select(i => (double)i).Shuffle().ToArray(), expected]; } } @@ -417,13 +414,13 @@ public void Double_WithSelector() public static IEnumerable NullableDouble_TestData() { - yield return new object[] { new double?[0], null }; - yield return new object[] { new double?[] { double.MinValue }, double.MinValue }; - yield return new object[] { new double?[] { 0, 0, 0, 0, 0 }, 0.0 }; - yield return new object[] { new double?[] { 5.5, 0, null, null, null, 15.5, 40.5, null, null, -23.5 }, 7.6 }; - yield return new object[] { new double?[] { null, null, null, null, 45 }, 45.0 }; - yield return new object[] { new double?[] { -23.5, 0, double.NaN, 54.3, 0.56 }, double.NaN }; - yield return new object[] { new double?[] { null, null, null, null, null }, null }; + yield return [new double?[0], null]; + yield return [new double?[] { double.MinValue }, double.MinValue]; + yield return [new double?[] { 0, 0, 0, 0, 0 }, 0.0]; + yield return [new double?[] { 5.5, 0, null, null, null, 15.5, 40.5, null, null, -23.5 }, 7.6]; + yield return [new double?[] { null, null, null, null, 45 }, 45.0]; + yield return [new double?[] { -23.5, 0, double.NaN, 54.3, 0.56 }, double.NaN]; + yield return [new double?[] { null, null, null, null, null }, null]; } [Theory] @@ -470,10 +467,10 @@ public void Decimal_EmptySource_ThrowsInvalidOperationException() { foreach (IEnumerable source in new IEnumerable[] { - Array.Empty(), + [], new List(), - Enumerable.Empty(), - new TestEnumerable(Array.Empty()) + [], + new TestEnumerable([]) }) { Assert.Throws(() => source.Average()); @@ -497,9 +494,9 @@ public void Decimal_NullSelector_ThrowsArgumentNullException() public static IEnumerable Decimal_TestData() { - yield return new object[] { new decimal[] { decimal.MaxValue }, decimal.MaxValue }; - yield return new object[] { new decimal[] { 0.0m, 0.0m, 0.0m, 0.0m, 0.0m }, 0 }; - yield return new object[] { new decimal[] { 5.5m, -10m, 15.5m, 40.5m, 28.5m }, 16 }; + yield return [new decimal[] { decimal.MaxValue }, decimal.MaxValue]; + yield return [new decimal[] { 0.0m, 0.0m, 0.0m, 0.0m, 0.0m }, 0]; + yield return [new decimal[] { 5.5m, -10m, 15.5m, 40.5m, 28.5m }, 16]; for (int i = 1; i <= 33; i++) { @@ -507,8 +504,7 @@ public static IEnumerable Decimal_TestData() for (int c = 1; c <= i; c++) sum += c; decimal expected = (decimal)sum / i; - // yield return new object[] { Shuffler.Shuffle(Enumerable.Range(1, i).Select(i => (decimal)i)), expected }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(1, i).Select(i => (decimal)i).ToArray()), expected }; + yield return [Enumerable.Range(1, i).Select(i => (decimal)i).Shuffle().ToArray(), expected]; } } @@ -539,12 +535,12 @@ public void Decimal_WithSelector() public static IEnumerable NullableDecimal_TestData() { - yield return new object[] { new decimal?[0], null }; - yield return new object[] { new decimal?[] { decimal.MinValue }, decimal.MinValue }; - yield return new object[] { new decimal?[] { 0m, 0m, 0m, 0m, 0m }, 0m }; - yield return new object[] { new decimal?[] { 5.5m, 0, null, null, null, 15.5m, 40.5m, null, null, -23.5m }, 7.6m }; - yield return new object[] { new decimal?[] { null, null, null, null, 45m }, 45m }; - yield return new object[] { new decimal?[] { null, null, null, null, null }, null }; + yield return [new decimal?[0], null]; + yield return [new decimal?[] { decimal.MinValue }, decimal.MinValue]; + yield return [new decimal?[] { 0m, 0m, 0m, 0m, 0m }, 0m]; + yield return [new decimal?[] { 5.5m, 0, null, null, null, 15.5m, 40.5m, null, null, -23.5m }, 7.6m]; + yield return [new decimal?[] { null, null, null, null, 45m }, 45m]; + yield return [new decimal?[] { null, null, null, null, null }, null]; } [Theory] @@ -589,7 +585,7 @@ public void NullableDecimal_WithSelector() [Fact] public void NullableDecimal_SumTooLarge_ThrowsOverflowException() { - decimal?[] source = new decimal?[] { decimal.MaxValue, decimal.MaxValue }; + decimal?[] source = [decimal.MaxValue, decimal.MaxValue]; Assert.Throws(() => source.Average()); } @@ -599,10 +595,10 @@ public void Float_EmptySource_ThrowsInvalidOperationException() { foreach (IEnumerable source in new IEnumerable[] { - Array.Empty(), + [], new List(), - Enumerable.Empty(), - new TestEnumerable(Array.Empty()) + [], + new TestEnumerable([]) }) { Assert.Throws(() => source.Average()); @@ -626,9 +622,9 @@ public void Float_NullSelector_ThrowsArgumentNullException() public static IEnumerable Float_TestData() { - yield return new object[] { new float[] { float.MaxValue }, float.MaxValue }; - yield return new object[] { new float[] { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f }, 0f }; - yield return new object[] { new float[] { 5.5f, -10f, 15.5f, 40.5f, 28.5f }, 16f }; + yield return [new float[] { float.MaxValue }, float.MaxValue]; + yield return [new float[] { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f }, 0f]; + yield return [new float[] { 5.5f, -10f, 15.5f, 40.5f, 28.5f }, 16f]; for (int i = 1; i <= 33; i++) { @@ -636,8 +632,7 @@ public static IEnumerable Float_TestData() for (int c = 1; c <= i; c++) sum += c; float expected = (float)sum / i; - // yield return new object[] { Shuffler.Shuffle(Enumerable.Range(1, i).Select(i => (float)i)), expected }; - yield return new object[] { Shuffler.Shuffle(Enumerable.Range(1, i).Select(i => (float)i).ToArray()), expected }; + yield return [Enumerable.Range(1, i).Select(i => (float)i).Shuffle().ToArray(), expected]; } } diff --git a/tests/System.Linq.Tests/Tests/ZLinq/CastTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/CastTests.cs index 347e42e8..66feeb3e 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/CastTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/CastTests.cs @@ -38,7 +38,7 @@ public void CastByteToUShortThrows() [Fact] public void EmptySource() { - object[] source = { }; + object[] source = []; Assert.Empty(source.Cast()); } @@ -46,8 +46,8 @@ public void EmptySource() public void NullableIntFromAppropriateObjects() { int? i = 10; - object[] source = { -4, 1, 2, 3, 9, i }; - int?[] expected = { -4, 1, 2, 3, 9, i }; + object[] source = [-4, 1, 2, 3, 9, i]; + int?[] expected = [-4, 1, 2, 3, 9, i]; Assert.Equal(expected, source.Cast()); } @@ -56,8 +56,8 @@ public void NullableIntFromAppropriateObjects() public void NullableIntFromAppropriateObjectsRunOnce() { int? i = 10; - object[] source = { -4, 1, 2, 3, 9, i }; - int?[] expected = { -4, 1, 2, 3, 9, i }; + object[] source = [-4, 1, 2, 3, 9, i]; + int?[] expected = [-4, 1, 2, 3, 9, i]; Assert.Equal(expected, source.RunOnce().Cast()); } @@ -66,7 +66,7 @@ public void NullableIntFromAppropriateObjectsRunOnce() public void LongFromNullableIntInObjectsThrows() { int? i = 10; - object[] source = { -4, 1, 2, 3, 9, i }; + object[] source = [-4, 1, 2, 3, 9, i]; Assert.Throws(() => { @@ -78,7 +78,7 @@ public void LongFromNullableIntInObjectsThrows() public void LongFromNullableIntInObjectsIncludingNullThrows() { int? i = 10; - object[] source = { -4, 1, 2, 3, 9, null, i }; + object[] source = [-4, 1, 2, 3, 9, null, i]; Assert.Throws(() => { @@ -90,8 +90,8 @@ public void LongFromNullableIntInObjectsIncludingNullThrows() public void NullableIntFromAppropriateObjectsIncludingNull() { int? i = 10; - object[] source = { -4, 1, 2, 3, 9, null, i }; - int?[] expected = { -4, 1, 2, 3, 9, null, i }; + object[] source = [-4, 1, 2, 3, 9, null, i]; + int?[] expected = [-4, 1, 2, 3, 9, null, i]; Assert.Equal(expected, source.Cast()); } @@ -99,8 +99,8 @@ public void NullableIntFromAppropriateObjectsIncludingNull() [Fact] public void ThrowOnUncastableItem() { - object[] source = { -4, 1, 2, 3, 9, "45" }; - int[] expectedBeginning = { -4, 1, 2, 3, 9 }; + object[] source = [-4, 1, 2, 3, 9, "45"]; + int[] expectedBeginning = [-4, 1, 2, 3, 9]; var cast = source.Cast(); try @@ -127,7 +127,7 @@ public void ThrowOnUncastableItem() [Fact] public void ThrowCastingIntToDouble() { - int[] source = new int[] { -4, 1, 2, 9 }; + int[] source = [-4, 1, 2, 9]; Assert.Throws(() => { @@ -138,7 +138,7 @@ public void ThrowCastingIntToDouble() private static void TestCastThrow(object o) { byte? i = 10; - object[] source = { -1, 0, o, i }; + object[] source = [-1, 0, o, i]; Assert.Throws(() => { @@ -156,8 +156,8 @@ public void ThrowOnHeterogenousSource() [Fact] public void CastToString() { - object[] source = { "Test1", "4.5", null, "Test2" }; - string[] expected = { "Test1", "4.5", null, "Test2" }; + object[] source = ["Test1", "4.5", null, "Test2"]; + string[] expected = ["Test1", "4.5", null, "Test2"]; Assert.Equal(expected, source.Cast()); } @@ -165,8 +165,8 @@ public void CastToString() [Fact] public void CastToStringRunOnce() { - object[] source = { "Test1", "4.5", null, "Test2" }; - string[] expected = { "Test1", "4.5", null, "Test2" }; + object[] source = ["Test1", "4.5", null, "Test2"]; + string[] expected = ["Test1", "4.5", null, "Test2"]; Assert.Equal(expected, source.RunOnce().Cast()); } @@ -180,7 +180,7 @@ public void ArrayConversionThrows() [Fact] public void FirstElementInvalidForCast() { - object[] source = { "Test", 3, 5, 10 }; + object[] source = ["Test", 3, 5, 10]; Assert.Throws(() => { @@ -191,7 +191,7 @@ public void FirstElementInvalidForCast() [Fact] public void LastElementInvalidForCast() { - object[] source = { -5, 9, 0, 5, 9, "Test" }; + object[] source = [-5, 9, 0, 5, 9, "Test"]; Assert.Throws(() => { @@ -202,8 +202,8 @@ public void LastElementInvalidForCast() [Fact] public void NullableIntFromNullsAndInts() { - object[] source = { 3, null, 5, -4, 0, null, 9 }; - int?[] expected = { 3, null, 5, -4, 0, null, 9 }; + object[] source = [3, null, 5, -4, 0, null, 9]; + int?[] expected = [3, null, 5, -4, 0, null, 9]; Assert.Equal(expected, source.Cast()); } @@ -211,7 +211,7 @@ public void NullableIntFromNullsAndInts() [Fact] public void ThrowCastingIntToLong() { - int[] source = new int[] { -4, 1, 2, 3, 9 }; + int[] source = [-4, 1, 2, 3, 9]; Assert.Throws(() => { @@ -222,7 +222,7 @@ public void ThrowCastingIntToLong() [Fact] public void ThrowCastingIntToNullableLong() { - int[] source = new int[] { -4, 1, 2, 3, 9 }; + int[] source = [-4, 1, 2, 3, 9]; Assert.Throws(() => { @@ -233,7 +233,7 @@ public void ThrowCastingIntToNullableLong() [Fact] public void ThrowCastingNullableIntToLong() { - int?[] source = new int?[] { -4, 1, 2, 3, 9 }; + int?[] source = [-4, 1, 2, 3, 9]; Assert.Throws(() => { @@ -244,7 +244,7 @@ public void ThrowCastingNullableIntToLong() [Fact] public void ThrowCastingNullableIntToNullableLong() { - int?[] source = new int?[] { -4, 1, 2, 3, 9, null }; + int?[] source = [-4, 1, 2, 3, 9, null]; Assert.Throws(() => { @@ -255,7 +255,7 @@ public void ThrowCastingNullableIntToNullableLong() [Fact] public void CastingNullToNonnullableIsNullReferenceException() { - int?[] source = new int?[] { -4, 1, null, 3 }; + int?[] source = [-4, 1, null, 3]; var cast = source.Cast(); Assert.Throws(() => { @@ -281,7 +281,7 @@ public void ForcedToEnumeratorDoesntEnumerate() [Fact(Skip = SkipReason.RefStruct)] public void TargetTypeIsSourceType_Nop() { - object[] values = new string[] { "hello", "world" }; + object[] values = ["hello", "world"]; Assert.Same(values, values.Cast()); } @@ -307,71 +307,71 @@ public void CastOnMultidimensionalArraySucceeds() [Fact] public void CastCountReturnsExpectedLength() { - object[] objects = new object[] { "hello", "world" }; + object[] objects = ["hello", "world"]; Assert.Equal(2, objects.Cast().Count()); } [Fact] public void CastFirstReturnsFirstElement() { - object[] objects = new object[] { "hello", "world" }; + object[] objects = ["hello", "world"]; Assert.Equal("hello", objects.Cast().First()); } [Fact] public void CastFirstOnEmptySequenceThrows() { - object[] objects = Array.Empty(); + object[] objects = []; Assert.Throws(() => objects.Cast().First()); } [Fact] public void CastLastReturnsLastElement() { - object[] objects = new object[] { "hello", "world" }; + object[] objects = ["hello", "world"]; Assert.Equal("world", objects.Cast().Last()); } [Fact] public void CastElementAtReturnsExpectedElement() { - object[] objects = new object[] { "hello", "world" }; + object[] objects = ["hello", "world"]; Assert.Equal("world", objects.Cast().ElementAt(1)); } [Fact] public void CastElementAtOutOfRangeThrows() { - object[] objects = new object[] { "hello", "world" }; + object[] objects = ["hello", "world"]; Assert.Throws(() => objects.Cast().ElementAt(2)); } [Fact] public void CastLastOnEmptySequenceThrows() { - object[] objects = Array.Empty(); + object[] objects = []; Assert.Throws(() => objects.Cast().Last()); } [Fact] public void CastSelectProcessesEachElement() { - object[] objects = new object[] { "hello", "world!" }; - Assert.Equal(new[] { 5, 6 }, objects.Cast().Select(s => s.Length)); + object[] objects = ["hello", "world!"]; + Assert.Equal([5, 6], objects.Cast().Select(s => s.Length)); } [Fact] public void CastSkipSkipsElements() { - object[] objects = new object[] { "hello", "there", "world" }; - Assert.Equal(new[] { "world" }, objects.Cast().Skip(2)); + object[] objects = ["hello", "there", "world"]; + Assert.Equal(["world"], objects.Cast().Skip(2)); } [Fact] public void CastTakeTakesElements() { - object[] objects = new object[] { "hello", "there", "world" }; - Assert.Equal(new[] { "hello", "there" }, objects.Cast().Take(2)); + object[] objects = ["hello", "there", "world"]; + Assert.Equal(["hello", "there"], objects.Cast().Take(2)); } } } diff --git a/tests/System.Linq.Tests/Tests/ZLinq/ChunkTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/ChunkTests.cs index 9b1c6f1a..8a9371d4 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/ChunkTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/ChunkTests.cs @@ -10,7 +10,7 @@ public class ChunkTests : EnumerableTests [Fact] public void Empty() { - Assert.Equal(Enumerable.Empty(), Enumerable.Empty().Chunk(4)); + Assert.Equal([], Enumerable.Empty().Chunk(4)); } [Fact] @@ -25,7 +25,7 @@ public void ThrowsOnNullSource() [InlineData(-1)] public void ThrowsWhenSizeIsNonPositive(int size) { - int[] source = { 1 }; + int[] source = [1]; AssertExtensions.Throws("size", () => source.Chunk(size)); } @@ -34,7 +34,7 @@ public void ChunkSourceLazily() { using var chunks = new FastInfiniteEnumerator().Chunk(5).GetEnumerator(); chunks.MoveNext(); - Assert.Equal(new[] { 0, 0, 0, 0, 0 }, chunks.Current); + Assert.Equal([0, 0, 0, 0, 0], chunks.Current); Assert.True(chunks.MoveNext()); } @@ -42,10 +42,8 @@ public void ChunkSourceLazily() [InlineData(new[] { 9999, 0, 888, -1, 66, -777, 1, 2, -12345 })] public void ChunkSourceRepeatCalls(int[] array) { - Assert.All(IdentityTransforms(), t => + Assert.All(CreateSources(array), source => { - IEnumerable source = t(array); - Assert.Equal(source.Chunk(3), source.Chunk(3)); }); } @@ -54,17 +52,15 @@ public void ChunkSourceRepeatCalls(int[] array) [InlineData(new[] { 9999, 0, 888, -1, 66, -777, 1, 2, -12345 })] public void ChunkSourceEvenly(int[] array) { - Assert.All(IdentityTransforms(), t => + Assert.All(CreateSources(array), source => { - IEnumerable source = t(array); - using var chunks = source.Chunk(3).GetEnumerator(); chunks.MoveNext(); - Assert.Equal(new[] { 9999, 0, 888 }, chunks.Current); + Assert.Equal([9999, 0, 888], chunks.Current); chunks.MoveNext(); - Assert.Equal(new[] { -1, 66, -777 }, chunks.Current); + Assert.Equal([-1, 66, -777], chunks.Current); chunks.MoveNext(); - Assert.Equal(new[] { 1, 2, -12345 }, chunks.Current); + Assert.Equal([1, 2, -12345], chunks.Current); Assert.False(chunks.MoveNext()); }); } @@ -73,17 +69,15 @@ public void ChunkSourceEvenly(int[] array) [InlineData(new[] { 9999, 0, 888, -1, 66, -777, 1, 2 })] public void ChunkSourceUnevenly(int[] array) { - Assert.All(IdentityTransforms(), t => + Assert.All(CreateSources(array), source => { - IEnumerable source = t(array); - using var chunks = source.Chunk(3).GetEnumerator(); chunks.MoveNext(); - Assert.Equal(new[] { 9999, 0, 888 }, chunks.Current); + Assert.Equal([9999, 0, 888], chunks.Current); chunks.MoveNext(); - Assert.Equal(new[] { -1, 66, -777 }, chunks.Current); + Assert.Equal([-1, 66, -777], chunks.Current); chunks.MoveNext(); - Assert.Equal(new[] { 1, 2 }, chunks.Current); + Assert.Equal([1, 2], chunks.Current); Assert.False(chunks.MoveNext()); }); } @@ -92,13 +86,11 @@ public void ChunkSourceUnevenly(int[] array) [InlineData(new[] { 9999, 0 })] public void ChunkSourceSmallerThanMaxSize(int[] array) { - Assert.All(IdentityTransforms(), t => + Assert.All(CreateSources(array), source => { - IEnumerable source = t(array); - using var chunks = source.Chunk(3).GetEnumerator(); chunks.MoveNext(); - Assert.Equal(new[] { 9999, 0 }, chunks.Current); + Assert.Equal([9999, 0], chunks.Current); Assert.False(chunks.MoveNext()); }); } @@ -107,10 +99,8 @@ public void ChunkSourceSmallerThanMaxSize(int[] array) [InlineData(new int[0])] public void EmptySourceYieldsNoChunks(int[] array) { - Assert.All(IdentityTransforms(), t => + Assert.All(CreateSources(array), source => { - IEnumerable source = t(array); - using var chunks = source.Chunk(3).GetEnumerator(); Assert.False(chunks.MoveNext()); }); @@ -126,7 +116,7 @@ public void RemovingFromSourceBeforeIterating() var chunks = list.Chunk(3); list.Remove(66); - Assert.Equal(new[] { new[] { 9999, 0, 888 }, new[] { -1, -777, 1 }, new[] { 2, -12345 } }, chunks); + Assert.Equal([[9999, 0, 888], [-1, -777, 1], [2, -12345]], chunks); } [Fact] @@ -139,7 +129,7 @@ public void AddingToSourceBeforeIterating() var chunks = list.Chunk(3); list.Add(10); - Assert.Equal(new[] { new[] { 9999, 0, 888 }, new[] { -1, 66, -777 }, new[] { 1, 2, -12345 }, new[] { 10 } }, chunks); + Assert.Equal([[9999, 0, 888], [-1, 66, -777], [1, 2, -12345], [10]], chunks); } // reproduces https://github.com/dotnet/runtime/issues/67132 @@ -148,7 +138,7 @@ public void DoesNotPrematurelyAllocateHugeArray() { int[][] chunks = Enumerable.Range(0, 10).Chunk(int.MaxValue).ToArray(); - Assert.Equal(new[] { Enumerable.Range(0, 10).ToArray() }, chunks); + Assert.Equal([Enumerable.Range(0, 10).ToArray()], chunks); } } } diff --git a/tests/System.Linq.Tests/Tests/ZLinq/ConcatTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/ConcatTests.cs index 0bcccd55..8c16f766 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/ConcatTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/ConcatTests.cs @@ -31,10 +31,10 @@ private static void SameResultsWithQueryAndRepeatCallsWorker(IEnumerable f var valueEnumerable1 = from item in first select item; var valueEnumerable2 = from item in second select item; - VerifyEqualsWorker(valueEnumerable1.Concat(valueEnumerable2).ToArray(), - valueEnumerable1.Concat(valueEnumerable2).ToArray()); - VerifyEqualsWorker(valueEnumerable2.Concat(valueEnumerable1).ToArray(), - valueEnumerable2.Concat(valueEnumerable1).ToArray()); + Assert.Equal(valueEnumerable1.Concat(valueEnumerable2), + valueEnumerable1.Concat(valueEnumerable2)); + Assert.Equal(valueEnumerable2.Concat(valueEnumerable1), + valueEnumerable2.Concat(valueEnumerable1)); } [Theory] @@ -43,10 +43,10 @@ private static void SameResultsWithQueryAndRepeatCallsWorker(IEnumerable f [InlineData(new int[] { 2, 3, 5, 9 }, new int[] { 8, 10 }, new int[] { 2, 3, 5, 9, 8, 10 })] // Neither side is empty public void PossiblyEmptyInputs(IEnumerable first, IEnumerable second, IEnumerable expected) { - VerifyEqualsWorker(expected, first.Concat(second).ToArray()); - VerifyEqualsWorker( + Assert.Equal(expected, first.Concat(second)); + Assert.Equal( expected.Skip(first.Count()).Concat(expected.Take(first.Count())).ToArray(), - second.Concat(first).ToArray()); // Swap the inputs around + second.Concat(first)); // Swap the inputs around } [Fact(Skip = SkipReason.EnumeratorBehaviorDifference)] @@ -84,7 +84,7 @@ public void SecondNull() public void VerifyEquals(IEnumerable expected, IEnumerable actual) { // workaround: xUnit type inference doesn't work if the input type is not T (like IEnumerable) - VerifyEqualsWorker(expected, actual); + Assert.Equal(expected, actual); } [Theory] @@ -137,23 +137,6 @@ public void First_Last_ElementAt(IEnumerable _, IEnumerable actual) } } - private static void VerifyEqualsWorker(IEnumerable expected, IEnumerable actual) - { - // Returns a list of functions that, when applied to enumerable, should return - // another one that has equivalent contents. - var identityTransforms = IdentityTransforms(); - - // We run the transforms N^2 times, by testing all transforms - // of expected against all transforms of actual. - foreach (var outTransform in identityTransforms) - { - foreach (var inTransform in identityTransforms) - { - Assert.Equal(outTransform(expected), inTransform(actual)); - } - } - } - public static IEnumerable ArraySourcesData() => GenerateSourcesData(outerTransform: e => e); public static IEnumerable SelectArraySourcesData() => GenerateSourcesData(outerTransform: e => e.Select(i => i).ToArray()); @@ -166,8 +149,8 @@ private static void VerifyEqualsWorker(IEnumerable expected, IEnumerable ConcatOfConcatsData() { - yield return new object[] - { + yield return + [ Enumerable.Range(0, 20), Enumerable.Concat( Enumerable.Concat( @@ -176,7 +159,7 @@ public static IEnumerable ConcatOfConcatsData() Enumerable.Concat( Enumerable.Range(10, 3), Enumerable.Range(13, 7))) - }; + ]; } public static IEnumerable ConcatWithSelfData() @@ -184,7 +167,7 @@ public static IEnumerable ConcatWithSelfData() var source = Enumerable.Repeat(1, 4).Concat(Enumerable.Repeat(1, 5)); var source2 = source.Concat(source); - yield return new object[] { Enumerable.Repeat(1, 18), source2.ToArray() }; + yield return [Enumerable.Repeat(1, 18), source2.ToArray()]; } public static IEnumerable ChainedCollectionConcatData() => GenerateSourcesData(innerTransform: e => e.ToList()); @@ -231,7 +214,7 @@ public static IEnumerable AppendedPrependedConcatAlternationsData() } } - yield return new object[] { expected.ToArray(), actual.ToArray() }; + yield return [expected.ToArray(), actual.ToArray()]; actual = foundation; expected.Clear(); @@ -243,26 +226,26 @@ public static IEnumerable ConcatWithEmptyEnumerableData() { List baseList = [0, 1, 2, 3, 4]; - yield return new object[] - { + yield return + [ Enumerable.Range(0, 5), Enumerable.Concat(Enumerable.Concat(new List(), new List()), baseList) - }; - yield return new object[] - { + ]; + yield return + [ Enumerable.Range(0, 5), Enumerable.Concat(new List(), baseList) - }; - yield return new object[] - { + ]; + yield return + [ Enumerable.Range(0, 5), Enumerable.Concat(Enumerable.Concat(baseList, new List()), new List()) - }; - yield return new object[] - { + ]; + yield return + [ Enumerable.Range(0, 5), Enumerable.Concat(baseList, new List()) - }; + ]; } private static IEnumerable GenerateSourcesData( @@ -281,7 +264,7 @@ private static IEnumerable GenerateSourcesData( actual = outerTransform(actual.Concat(innerTransform(Enumerable.Range(j * 3, 3))).ToArray()); } - yield return new object[] { expected, actual }; + yield return [expected, actual]; } } @@ -291,7 +274,7 @@ public void ManyConcats(IEnumerable> sources) { foreach (var transform in IdentityTransforms()) { - IEnumerable concatee = Enumerable.Empty(); + IEnumerable concatee = []; foreach (var source in sources) { // ZLinq can't assigned nested query to same variable. @@ -299,7 +282,7 @@ public void ManyConcats(IEnumerable> sources) } Assert.Equal(sources.Sum(s => s.Count()), concatee.Count()); - VerifyEqualsWorker(sources.SelectMany(s => s).ToArray(), concatee); + Assert.Equal(sources.SelectMany(s => s).ToArray(), concatee.ToArray()); } } @@ -309,7 +292,7 @@ public void ManyConcatsRunOnce(IEnumerable> sources) { foreach (var transform in IdentityTransforms()) { - IEnumerable concatee = Enumerable.Empty(); + IEnumerable concatee = []; foreach (var source in sources) { // ZLinq can't assigned nested query to same variable. @@ -322,10 +305,10 @@ public void ManyConcatsRunOnce(IEnumerable> sources) public static IEnumerable ManyConcatsData() { - yield return new object[] { Enumerable.Repeat(Enumerable.Empty(), 256) }; - yield return new object[] { Enumerable.Repeat(Enumerable.Repeat(6, 1), 256) }; + yield return [Enumerable.Repeat(Enumerable.Empty(), 256)]; + yield return [Enumerable.Repeat(Enumerable.Repeat(6, 1), 256)]; // Make sure Concat doesn't accidentally swap around the sources, e.g. [3, 4], [1, 2] should not become [1..4] - yield return new object[] { Enumerable.Range(0, 500).Select(i => Enumerable.Repeat(i, 1)).Reverse().ToArray() }; + yield return [Enumerable.Range(0, 500).Select(i => Enumerable.Repeat(i, 1)).Reverse().ToArray()]; } [Fact] @@ -371,17 +354,17 @@ public void CountOfConcatCollectionChainShouldBeResilientToStackOverflow() // For perf reasons (this test can take a long time to run) // we use a for-loop manually rather than .Repeat and .Aggregate - IEnumerable concatChain = Array.Empty(); // note: all items in this chain must implement ICollection + IEnumerable concatChain = []; // note: all items in this chain must implement ICollection for (int i = 0; i < NumberOfConcats; i++) { - concatChain = concatChain.Concat(Array.Empty()).ToArray(); + concatChain = concatChain.Concat([]).ToArray(); } Assert.Empty(concatChain); // should not throw a StackOverflowException // ToArray needs the count as well, and the process of copying all of the collections // to the array should also not be recursive. - Assert.Equal(new int[] { }, concatChain.ToArray()); - Assert.Equal(new List { }, concatChain.ToList()); // ToList also gets the count beforehand + Assert.Equal([], concatChain.ToArray()); + Assert.Equal([], concatChain.ToList()); // ToList also gets the count beforehand } [Fact] @@ -407,7 +390,7 @@ public void CountOfConcatEnumerableChainShouldBeResilientToStackOverflow() // would take quite long. for (int i = 0; i < NumberOfConcats; i++) { - concatChain = concatChain.Concat(Array.Empty()).ToArray(); + concatChain = concatChain.Concat([]).ToArray(); } Assert.Empty(concatChain); @@ -433,11 +416,11 @@ public void GettingFirstEnumerableShouldBeResilientToStackOverflow() // Start with a lazy seed. // The seed holds 1 item, so during the first MoveNext we won't have to // backtrack through the linked list 30000 times. This is for test perf. - IEnumerable concatChain = ForceNotCollection(new int[] { 0xf00 }); + IEnumerable concatChain = ForceNotCollection([0xf00]); for (int i = 0; i < NumberOfConcats; i++) { - concatChain = concatChain.Concat(Array.Empty()).ToArray(); + concatChain = concatChain.Concat([]).ToArray(); } using (IEnumerator en = concatChain.GetEnumerator()) @@ -469,11 +452,11 @@ public void GetEnumerableOfConcatCollectionChainFollowedByEnumerableNodeShouldBe // This time, start with an ICollection seed. We want the subsequent Concats in // the loop to produce collection iterators. - IEnumerable concatChain = new int[] { 0xf00 }; + IEnumerable concatChain = [0xf00]; for (int i = 0; i < NumberOfConcats - 1; i++) { - concatChain = concatChain.Concat(Array.Empty()).ToArray(); + concatChain = concatChain.Concat([]).ToArray(); } // Finally, link an enumerable iterator at the head of the list. @@ -510,98 +493,98 @@ public void CollectionInterleavedWithLazyEnumerables_ToArray(IEnumerable[] public static IEnumerable GetToArrayDataSources() { // Marker at the end - yield return new object[] - { + yield return + [ new IEnumerable[] { - new TestEnumerable(new int[] { 0 }), - new TestEnumerable(new int[] { 1 }), - new TestEnumerable(new int[] { 2 }), - new int[] { 3 }, + new TestEnumerable([0]), + new TestEnumerable([1]), + new TestEnumerable([2]), [3], + } - }; + ]; // Marker at beginning - yield return new object[] - { + yield return + [ new IEnumerable[] { - new int[] { 0 }, - new TestEnumerable(new int[] { 1 }), - new TestEnumerable(new int[] { 2 }), - new TestEnumerable(new int[] { 3 }), + [0], + new TestEnumerable([1]), + new TestEnumerable([2]), + new TestEnumerable([3]), } - }; + ]; // Marker in middle - yield return new object[] - { + yield return + [ new IEnumerable[] { - new TestEnumerable(new int[] { 0 }), - new int[] { 1 }, - new TestEnumerable(new int[] { 2 }), + new TestEnumerable([0]), [1], + + new TestEnumerable([2]), } - }; + ]; // Non-marker in middle - yield return new object[] - { + yield return + [ new IEnumerable[] { - new int[] { 0 }, - new TestEnumerable(new int[] { 1 }), - new int[] { 2 }, + [0], + new TestEnumerable([1]), [2], + } - }; + ]; // Big arrays (marker in middle) - yield return new object[] - { + yield return + [ new IEnumerable[] { new TestEnumerable(Enumerable.Range(0, 100).ToArray()), Enumerable.Range(100, 100).ToArray(), new TestEnumerable(Enumerable.Range(200, 100).ToArray()), } - }; + ]; // Big arrays (non-marker in middle) - yield return new object[] - { + yield return + [ new IEnumerable[] { Enumerable.Range(0, 100).ToArray(), new TestEnumerable(Enumerable.Range(100, 100).ToArray()), Enumerable.Range(200, 100).ToArray(), } - }; + ]; // Interleaved (first marker) - yield return new object[] - { + yield return + [ new IEnumerable[] { - new int[] { 0 }, - new TestEnumerable(new int[] { 1 }), - new int[] { 2 }, - new TestEnumerable(new int[] { 3 }), - new int[] { 4 }, + [0], + new TestEnumerable([1]), [2], + new TestEnumerable([3]), [4], + + } - }; + ]; // Interleaved (first non-marker) - yield return new object[] - { + yield return + [ new IEnumerable[] { - new TestEnumerable(new int[] { 0 }), - new int[] { 1 }, - new TestEnumerable(new int[] { 2 }), - new int[] { 3 }, - new TestEnumerable(new int[] { 4 }), + new TestEnumerable([0]), [1], + new TestEnumerable([2]), [3], + + + new TestEnumerable([4]), } - }; + ]; } } } diff --git a/tests/System.Linq.Tests/Tests/ZLinq/ConsistencyTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/ConsistencyTests.cs index 05213db5..68c8a8ce 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/ConsistencyTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/ConsistencyTests.cs @@ -23,10 +23,10 @@ public static void MatchSequencePattern() MethodInfo queryableNotInEnumerable = GetMissingExtensionMethod( typeof(Queryable), typeof(Enumerable), - new[] { + [ nameof(Queryable.AsQueryable) - } - ); + ] + ); Assert.True(queryableNotInEnumerable is null, string.Format("Queryable method {0} not defined by Enumerable", queryableNotInEnumerable)); } diff --git a/tests/System.Linq.Tests/Tests/ZLinq/ContainsTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/ContainsTests.cs index a7055dac..121d9b27 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/ContainsTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/ContainsTests.cs @@ -32,17 +32,17 @@ public static IEnumerable Int_TestData() { foreach (Func, IEnumerable> transform in IdentityTransforms()) { - yield return new object[] { transform(new int[0]), 6, false }; - yield return new object[] { transform(new int[] { 8, 10, 3, 0, -8 }), 6, false }; - yield return new object[] { transform(new int[] { 8, 10, 3, 0, -8 }), 8, true }; - yield return new object[] { transform(new int[] { 8, 10, 3, 0, -8 }), -8, true }; - yield return new object[] { transform(new int[] { 8, 0, 10, 3, 0, -8, 0 }), 0, true }; - - yield return new object[] { transform(Enumerable.Range(0, 0)), 0, false }; - yield return new object[] { transform(Enumerable.Range(4, 5)), 3, false }; - yield return new object[] { transform(Enumerable.Range(3, 5)), 3, true }; - yield return new object[] { transform(Enumerable.Range(3, 5)), 7, true }; - yield return new object[] { transform(Enumerable.Range(10, 3)), 10, true }; + yield return [transform([]), 6, false]; + yield return [transform([8, 10, 3, 0, -8]), 6, false]; + yield return [transform([8, 10, 3, 0, -8]), 8, true]; + yield return [transform([8, 10, 3, 0, -8]), -8, true]; + yield return [transform([8, 0, 10, 3, 0, -8, 0]), 0, true]; + + yield return [transform(Enumerable.Range(0, 0)), 0, false]; + yield return [transform(Enumerable.Range(4, 5)), 3, false]; + yield return [transform(Enumerable.Range(3, 5)), 3, true]; + yield return [transform(Enumerable.Range(3, 5)), 7, true]; + yield return [transform(Enumerable.Range(10, 3)), 10, true]; } } diff --git a/tests/System.Linq.Tests/Tests/ZLinq/CountByTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/CountByTests.cs index 90ca43a4..2df363f1 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/CountByTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/CountByTests.cs @@ -20,7 +20,7 @@ public void CountBy_SourceNull_ThrowsArgumentNullException() [Fact] public void CountBy_KeySelectorNull_ThrowsArgumentNullException() { - string[] source = { "Bob", "Tim", "Robert", "Chris" }; + string[] source = ["Bob", "Tim", "Robert", "Chris"]; Func keySelector = null; AssertExtensions.Throws("keySelector", () => source.CountBy(keySelector)); @@ -70,7 +70,7 @@ public void CountBy_HasExpectedOutput() source: Enumerable.Empty(), keySelector: x => x, comparer: null, - expected: Enumerable.Empty>()); + expected: []); Validate( source: Enumerable.Range(0, 10), @@ -97,7 +97,7 @@ public void CountBy_HasExpectedOutput() expected: Enumerable.Repeat(5, 1).Select(x => new KeyValuePair(x, 20)).ToArray()); Validate( - source: new string[] { "Bob", "bob", "tim", "Bob", "Tim" }, + source: ["Bob", "bob", "tim", "Bob", "Tim"], keySelector: x => x, null, expected: @@ -109,7 +109,7 @@ public void CountBy_HasExpectedOutput() ]); Validate( - source: new string[] { "Bob", "bob", "tim", "Bob", "Tim" }, + source: ["Bob", "bob", "tim", "Bob", "Tim"], keySelector: x => x, StringComparer.OrdinalIgnoreCase, expected: diff --git a/tests/System.Linq.Tests/Tests/ZLinq/CountTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/CountTests.cs index 7317cd5a..ae3921bd 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/CountTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/CountTests.cs @@ -30,18 +30,18 @@ public void SameResultsRepeatCallsStringQuery() public static IEnumerable Int_TestData() { - yield return new object[] { new int[0], null, 0 }; + yield return [new int[0], null, 0]; Func isEvenFunc = IsEven; - yield return new object[] { new int[0], isEvenFunc, 0 }; - yield return new object[] { new int[] { 4 }, isEvenFunc, 1 }; - yield return new object[] { new int[] { 5 }, isEvenFunc, 0 }; - yield return new object[] { new int[] { 2, 5, 7, 9, 29, 10 }, isEvenFunc, 2 }; - yield return new object[] { new int[] { 2, 20, 22, 100, 50, 10 }, isEvenFunc, 6 }; + yield return [new int[0], isEvenFunc, 0]; + yield return [new int[] { 4 }, isEvenFunc, 1]; + yield return [new int[] { 5 }, isEvenFunc, 0]; + yield return [new int[] { 2, 5, 7, 9, 29, 10 }, isEvenFunc, 2]; + yield return [new int[] { 2, 20, 22, 100, 50, 10 }, isEvenFunc, 6]; - yield return new object[] { RepeatedNumberGuaranteedNotCollectionType(0, 0), null, 0 }; - yield return new object[] { RepeatedNumberGuaranteedNotCollectionType(5, 1), null, 1 }; - yield return new object[] { RepeatedNumberGuaranteedNotCollectionType(5, 10), null, 10 }; + yield return [RepeatedNumberGuaranteedNotCollectionType(0, 0), null, 0]; + yield return [RepeatedNumberGuaranteedNotCollectionType(5, 1), null, 1]; + yield return [RepeatedNumberGuaranteedNotCollectionType(5, 10), null, 10]; } [Theory] @@ -80,7 +80,7 @@ public void IntRunOnce(IEnumerable source, Func predicate, int e [Fact] public void NullableIntArray_IncludesNullObjects() { - int?[] data = { -10, 4, 9, null, 11 }; + int?[] data = [-10, 4, 9, null, 11]; Assert.Equal(5, data.Count()); } @@ -99,9 +99,9 @@ public void RunOnce(int count, IEnumerable enumerable) private static IEnumerable EnumerateCollectionTypesAndCounts(int count, IEnumerable enumerable) { - foreach (var transform in IdentityTransforms()) + foreach (IEnumerable source in CreateSources(enumerable)) { - yield return new object[] { count, transform(enumerable) }; + yield return [count, source]; } } @@ -148,12 +148,12 @@ public void NonEnumeratedCount_SupportedEnumerables_ShouldReturnExpectedCount() Assert.Equal(source.Length, actualCount); } { - var source = new List(new int[] { 1, 2, 3, 4 }); + var source = new List([1, 2, 3, 4]); Assert.True(source.TryGetNonEnumeratedCount(out int actualCount)); Assert.Equal(source.Count, actualCount); } { - var source = new Stack(new int[] { 1, 2, 3, 4 }); + var source = new Stack([1, 2, 3, 4]); Assert.True(source.TryGetNonEnumeratedCount(out int actualCount)); Assert.Equal(source.Count, actualCount); } @@ -162,19 +162,19 @@ public void NonEnumeratedCount_SupportedEnumerables_ShouldReturnExpectedCount() Assert.True(source.TryGetNonEnumeratedCount(out int actualCount)); Assert.Equal(0, actualCount); } + { + var source = Enumerable.Range(1, 100); + Assert.True(source.TryGetNonEnumeratedCount(out int actualCount)); + Assert.Equal(100, actualCount); + } + { + var source = Enumerable.Repeat(1, 80); + Assert.True(source.TryGetNonEnumeratedCount(out int actualCount)); + Assert.Equal(80, actualCount); + } - if (PlatformDetection.IsSpeedOptimized) + if (PlatformDetection.IsLinqSpeedOptimized) { - { - var source = Enumerable.Range(1, 100); - Assert.True(source.TryGetNonEnumeratedCount(out int actualCount)); - Assert.Equal(source.Count(), actualCount); - } - { - var source = Enumerable.Repeat(1, 80); - Assert.True(source.TryGetNonEnumeratedCount(out int actualCount)); - Assert.Equal(source.Count(), actualCount); - } { var source = Enumerable.Range(1, 50).Select(x => x + 1); Assert.True(source.TryGetNonEnumeratedCount(out int actualCount)); @@ -233,7 +233,7 @@ public void NonEnumeratedCount_UnsupportedEnumerables_ShouldReturnFalse() Assert.Equal(0, actualCount); } - if (!PlatformDetection.IsSpeedOptimized) + if (!PlatformDetection.IsLinqSpeedOptimized) { { var source = Enumerable.Range(1, 100); diff --git a/tests/System.Linq.Tests/Tests/ZLinq/DefaultIfEmptyTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/DefaultIfEmptyTests.cs index 6869ec83..58e44a51 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/DefaultIfEmptyTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/DefaultIfEmptyTests.cs @@ -30,14 +30,14 @@ public void SameResultsRepeatCallsEmptyQuery() public static IEnumerable 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(), 0, new int[] { 0 } }; + yield return [new int[0], 0, new int[] { 0 }]; + yield return [new int[] { 3 }, 0, new int[] { 3 }]; + yield return [new int[] { 3, -1, 0, 10, 15 }, 0, new int[] { 3, -1, 0, 10, 15 }]; + + yield return [new int[0], -10, new int[] { -10 }]; + yield return [new int[] { 3 }, 9, new int[] { 3 }]; + yield return [new int[] { 3, -1, 0, 10, 15 }, 9, new int[] { 3, -1, 0, 10, 15 }]; + yield return [Enumerable.Empty(), 0, new int[] { 0 }]; } [Theory] @@ -78,16 +78,16 @@ public static void DefaultIfEmptyRunOnce(IEnumerable source, int defaultVal [Fact] public void NullableArray_Empty_WithoutDefaultValue() { - int?[] source = new int?[0]; - Assert.Equal(new int?[] { null }, source.DefaultIfEmpty()); + int?[] source = []; + Assert.Equal([null], source.DefaultIfEmpty()); } [Fact] public void NullableArray_Empty_WithDefaultValue() { - int?[] source = new int?[0]; + int?[] source = []; int? defaultValue = 9; - Assert.Equal(new int?[] { defaultValue }, source.DefaultIfEmpty(defaultValue)); + Assert.Equal([defaultValue], source.DefaultIfEmpty(defaultValue)); } [Fact] @@ -120,7 +120,7 @@ public void First_Last_ElementAt() Assert.Throws(() => nonEmpty.ElementAt(-1)); Assert.Throws(() => nonEmpty.ElementAt(4)); - IEnumerable empty = Enumerable.Empty(); + IEnumerable empty = []; Assert.Equal(42, empty.DefaultIfEmpty(42).First()); Assert.Equal(42, empty.DefaultIfEmpty(42).Last()); Assert.Equal(42, empty.DefaultIfEmpty(42).ElementAt(0)); diff --git a/tests/System.Linq.Tests/Tests/ZLinq/DistinctTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/DistinctTests.cs index 31bd6104..ae6c64d7 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/DistinctTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/DistinctTests.cs @@ -33,22 +33,22 @@ where string.IsNullOrEmpty(x) [Fact] public void EmptySource() { - int[] source = { }; + int[] source = []; Assert.Empty(source.Distinct()); } [Fact] public void EmptySourceRunOnce() { - int[] source = { }; + int[] source = []; Assert.Empty(source.RunOnce().Distinct()); } [Fact] public void SingleNullElementExplicitlyUseDefaultComparer() { - string[] source = { null }; - string[] expected = { null }; + string[] source = [null]; + string[] expected = [null]; Assert.Equal(expected, source.Distinct(EqualityComparer.Default)); } @@ -56,8 +56,8 @@ public void SingleNullElementExplicitlyUseDefaultComparer() [Fact] public void EmptyStringDistinctFromNull() { - string[] source = { null, null, string.Empty }; - string[] expected = { null, string.Empty }; + string[] source = [null, null, string.Empty]; + string[] expected = [null, string.Empty]; Assert.Equal(expected, source.Distinct(EqualityComparer.Default)); } @@ -65,8 +65,8 @@ public void EmptyStringDistinctFromNull() [Fact] public void CollapsDuplicateNulls() { - string[] source = { null, null }; - string[] expected = { null }; + string[] source = [null, null]; + string[] expected = [null]; Assert.Equal(expected, source.Distinct(EqualityComparer.Default)); } @@ -74,8 +74,8 @@ public void CollapsDuplicateNulls() [Fact] public void SourceAllDuplicates() { - int[] source = { 5, 5, 5, 5, 5, 5 }; - int[] expected = { 5 }; + int[] source = [5, 5, 5, 5, 5, 5]; + int[] expected = [5]; Assert.Equal(expected, source.Distinct()); } @@ -83,7 +83,7 @@ public void SourceAllDuplicates() [Fact] public void AllUnique() { - int[] source = { 2, -5, 0, 6, 10, 9 }; + int[] source = [2, -5, 0, 6, 10, 9]; Assert.Equal(source, source.Distinct()); } @@ -91,8 +91,8 @@ public void AllUnique() [Fact] public void SomeDuplicatesIncludingNulls() { - int?[] source = { 1, 1, 1, 2, 2, 2, null, null }; - int?[] expected = { 1, 2, null }; + int?[] source = [1, 1, 1, 2, 2, 2, null, null]; + int?[] expected = [1, 2, null]; Assert.Equal(expected, source.Distinct()); } @@ -100,8 +100,8 @@ public void SomeDuplicatesIncludingNulls() [Fact] public void SomeDuplicatesIncludingNullsRunOnce() { - int?[] source = { 1, 1, 1, 2, 2, 2, null, null }; - int?[] expected = { 1, 2, null }; + int?[] source = [1, 1, 1, 2, 2, 2, null, null]; + int?[] expected = [1, 2, null]; Assert.Equal(expected, source.RunOnce().Distinct()); } @@ -109,8 +109,8 @@ public void SomeDuplicatesIncludingNullsRunOnce() [Fact] public void LastSameAsFirst() { - int[] source = { 1, 2, 3, 4, 5, 1 }; - int[] expected = { 1, 2, 3, 4, 5 }; + int[] source = [1, 2, 3, 4, 5, 1]; + int[] expected = [1, 2, 3, 4, 5]; Assert.Equal(expected, source.Distinct()); } @@ -119,8 +119,8 @@ public void LastSameAsFirst() [Fact] public void RepeatsNonConsecutive() { - int[] source = { 1, 1, 2, 2, 4, 3, 1, 3, 2 }; - int[] expected = { 1, 2, 4, 3 }; + int[] source = [1, 1, 2, 2, 4, 3, 1, 3, 2]; + int[] expected = [1, 2, 4, 3]; Assert.Equal(expected, source.Distinct()); } @@ -128,8 +128,8 @@ public void RepeatsNonConsecutive() [Fact] public void RepeatsNonConsecutiveRunOnce() { - int[] source = { 1, 1, 2, 2, 4, 3, 1, 3, 2 }; - int[] expected = { 1, 2, 4, 3 }; + int[] source = [1, 1, 2, 2, 4, 3, 1, 3, 2]; + int[] expected = [1, 2, 4, 3]; Assert.Equal(expected, source.RunOnce().Distinct()); } @@ -137,8 +137,8 @@ public void RepeatsNonConsecutiveRunOnce() [Fact] public void NullComparer() { - string[] source = { "Bob", "Tim", "bBo", "miT", "Robert", "iTm" }; - string[] expected = { "Bob", "Tim", "bBo", "miT", "Robert", "iTm" }; + string[] source = ["Bob", "Tim", "bBo", "miT", "Robert", "iTm"]; + string[] expected = ["Bob", "Tim", "bBo", "miT", "Robert", "iTm"]; Assert.Equal(expected, source.Distinct()); } @@ -162,8 +162,8 @@ public void NullSourceCustomComparer() [Fact] public void CustomEqualityComparer() { - string[] source = { "Bob", "Tim", "bBo", "miT", "Robert", "iTm" }; - string[] expected = { "Bob", "Tim", "Robert" }; + string[] source = ["Bob", "Tim", "bBo", "miT", "Robert", "iTm"]; + string[] expected = ["Bob", "Tim", "Robert"]; Assert.Equal(expected, source.Distinct(new AnagramEqualityComparer()), new AnagramEqualityComparer()); } @@ -171,8 +171,8 @@ public void CustomEqualityComparer() [Fact] public void CustomEqualityComparerRunOnce() { - string[] source = { "Bob", "Tim", "bBo", "miT", "Robert", "iTm" }; - string[] expected = { "Bob", "Tim", "Robert" }; + string[] source = ["Bob", "Tim", "bBo", "miT", "Robert", "iTm"]; + string[] expected = ["Bob", "Tim", "Robert"]; Assert.Equal(expected, source.RunOnce().Distinct(new AnagramEqualityComparer()), new AnagramEqualityComparer()); } @@ -197,31 +197,24 @@ public void FindDistinctAndValidate(IEnumerable original) public static IEnumerable SequencesWithDuplicates() { // Validate an array of different numeric data types. - yield return new object[] { new int[] { 1, 1, 1, 2, 3, 5, 5, 6, 6, 10 } }; - yield return new object[] { new long[] { 1, 1, 1, 2, 3, 5, 5, 6, 6, 10 } }; - yield return new object[] { new float[] { 1, 1, 1, 2, 3, 5, 5, 6, 6, 10 } }; - yield return new object[] { new double[] { 1, 1, 1, 2, 3, 5, 5, 6, 6, 10 } }; - yield return new object[] { new decimal[] { 1, 1, 1, 2, 3, 5, 5, 6, 6, 10 } }; + yield return [new int[] { 1, 1, 1, 2, 3, 5, 5, 6, 6, 10 }]; + yield return [new long[] { 1, 1, 1, 2, 3, 5, 5, 6, 6, 10 }]; + yield return [new float[] { 1, 1, 1, 2, 3, 5, 5, 6, 6, 10 }]; + yield return [new double[] { 1, 1, 1, 2, 3, 5, 5, 6, 6, 10 }]; + yield return [new decimal[] { 1, 1, 1, 2, 3, 5, 5, 6, 6, 10 }]; // Try strings - yield return new object[] { new [] + yield return + [ + new [] { "add", - "add", - "subtract", - "multiply", - "divide", - "divide2", - "subtract", - "add", - "power", - "exponent", "hello", "class", "namespace", "namespace", "namespace", } - }; + ]; } [Fact(Skip = SkipReason.EnumeratorBehaviorDifference)] @@ -236,8 +229,8 @@ public void ForcedToEnumeratorDoesntEnumerate() [Fact] public void ToArray() { - int?[] source = { 1, 1, 1, 2, 2, 2, null, null }; - int?[] expected = { 1, 2, null }; + int?[] source = [1, 1, 1, 2, 2, 2, null, null]; + int?[] expected = [1, 2, null]; Assert.Equal(expected, source.Distinct().ToArray()); } @@ -245,8 +238,8 @@ public void ToArray() [Fact] public void ToList() { - int?[] source = { 1, 1, 1, 2, 2, 2, null, null }; - int?[] expected = { 1, 2, null }; + int?[] source = [1, 1, 1, 2, 2, 2, null, null]; + int?[] expected = [1, 2, null]; Assert.Equal(expected, source.Distinct().ToList()); } @@ -254,14 +247,14 @@ public void ToList() [Fact] public void Count() { - int?[] source = { 1, 1, 1, 2, 2, 2, null, null }; + int?[] source = [1, 1, 1, 2, 2, 2, null, null]; Assert.Equal(3, source.Distinct().Count()); } [Fact] public void RepeatEnumerating() { - int?[] source = { 1, 1, 1, 2, 2, 2, null, null }; + int?[] source = [1, 1, 1, 2, 2, 2, null, null]; var result = source.Distinct(); @@ -280,7 +273,7 @@ public void DistinctBy_SourceNull_ThrowsArgumentNullException() [Fact] public void DistinctBy_KeySelectorNull_ThrowsArgumentNullException() { - string[] source = { "Bob", "Tim", "Robert", "Chris" }; + string[] source = ["Bob", "Tim", "Robert", "Chris"]; Func keySelector = null; AssertExtensions.Throws("keySelector", () => source.DistinctBy(keySelector)); @@ -307,7 +300,7 @@ public static IEnumerable DistinctBy_TestData() source: Array.Empty(), keySelector: x => x, comparer: null, - expected: Enumerable.Empty()); + expected: []); yield return WrapArgs( source: Enumerable.Range(0, 10), @@ -319,7 +312,7 @@ public static IEnumerable DistinctBy_TestData() source: Enumerable.Range(5, 10), keySelector: x => true, comparer: null, - expected: new int[] { 5 }); + expected: [5]); yield return WrapArgs( source: Enumerable.Range(0, 20), @@ -334,16 +327,16 @@ public static IEnumerable DistinctBy_TestData() expected: Enumerable.Repeat(5, 1)); yield return WrapArgs( - source: new string[] { "Bob", "bob", "tim", "Bob", "Tim" }, + source: ["Bob", "bob", "tim", "Bob", "Tim"], keySelector: x => x, null, - expected: new string[] { "Bob", "bob", "tim", "Tim" }); + expected: ["Bob", "bob", "tim", "Tim"]); yield return WrapArgs( - source: new string[] { "Bob", "bob", "tim", "Bob", "Tim" }, + source: ["Bob", "bob", "tim", "Bob", "Tim"], keySelector: x => x, StringComparer.OrdinalIgnoreCase, - expected: new string[] { "Bob", "tim" }); + expected: ["Bob", "tim"]); yield return WrapArgs( source: new (string Name, int Age)[] { ("Tom", 20), ("Dick", 30), ("Harry", 40) }, @@ -370,7 +363,7 @@ public static IEnumerable DistinctBy_TestData() expected: new (string Name, int Age)[] { ("Bob", 20), ("Harry", 40) }); object[] WrapArgs(IEnumerable source, Func keySelector, IEqualityComparer? comparer, IEnumerable expected) - => new object[] { source, keySelector, comparer, expected }; + => [source, keySelector, comparer, expected]; } } } diff --git a/tests/System.Linq.Tests/Tests/ZLinq/ElementAtOrDefaultTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/ElementAtOrDefaultTests.cs index 58422a11..24343749 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/ElementAtOrDefaultTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/ElementAtOrDefaultTests.cs @@ -33,18 +33,18 @@ public void SameResultsRepeatCallsStringQuery() public static IEnumerable TestData() { - yield return new object[] { NumberRangeGuaranteedNotCollectionType(9, 1), 0, 1, 9 }; - yield return new object[] { NumberRangeGuaranteedNotCollectionType(9, 10), 9, 1, 18 }; - yield return new object[] { NumberRangeGuaranteedNotCollectionType(-4, 10), 3, 7, -1 }; - - yield return new object[] { new int[] { 1, 2, 3, 4 }, 4, 0, 0 }; - yield return new object[] { new int[0], 0, 0, 0 }; - yield return new object[] { new int[] { -4 }, 0, 1, -4 }; - yield return new object[] { new int[] { 9, 8, 0, -5, 10 }, 4, 1, 10 }; - - yield return new object[] { NumberRangeGuaranteedNotCollectionType(-4, 5), -1, 6, 0 }; - yield return new object[] { NumberRangeGuaranteedNotCollectionType(5, 5), 5, 0, 0 }; - yield return new object[] { NumberRangeGuaranteedNotCollectionType(0, 0), 0, 0, 0 }; + yield return [NumberRangeGuaranteedNotCollectionType(9, 1), 0, 1, 9]; + yield return [NumberRangeGuaranteedNotCollectionType(9, 10), 9, 1, 18]; + yield return [NumberRangeGuaranteedNotCollectionType(-4, 10), 3, 7, -1]; + + yield return [new int[] { 1, 2, 3, 4 }, 4, 0, 0]; + yield return [new int[0], 0, 0, 0]; + yield return [new int[] { -4 }, 0, 1, -4]; + yield return [new int[] { 9, 8, 0, -5, 10 }, 4, 1, 10]; + + yield return [NumberRangeGuaranteedNotCollectionType(-4, 5), -1, 6, 0]; + yield return [NumberRangeGuaranteedNotCollectionType(5, 5), 5, 0, 0]; + yield return [NumberRangeGuaranteedNotCollectionType(0, 0), 0, 0, 0]; } [Theory] @@ -123,7 +123,7 @@ public void MutableSource() Assert.Equal(2, source.ElementAtOrDefault(new Index(2))); Assert.Equal(2, source.ElementAtOrDefault(^3)); - source.InsertRange(3, new[] { -1, -2 }); + source.InsertRange(3, [-1, -2]); source.RemoveAt(0); Assert.Equal(-1, source.ElementAtOrDefault(2)); Assert.Equal(-1, source.ElementAtOrDefault(new Index(2))); @@ -148,7 +148,7 @@ public void MutableSourceNotList() var query1 = ForceNotCollection(source).Select(i => i); var query2 = ForceNotCollection(source).Select(i => i); var query3 = ForceNotCollection(source).Select(i => i); - source.InsertRange(3, new[] { -1, -2 }); + source.InsertRange(3, [-1, -2]); source.RemoveAt(0); Assert.Equal(-1, query1.ElementAtOrDefault(2)); Assert.Equal(-1, query2.ElementAtOrDefault(new Index(2))); @@ -198,7 +198,7 @@ public void EnumerateElements() [Fact] public void NonEmptySource_Consistency() { - int?[] source = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + int?[] source = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; Assert.Equal(5, source.ElementAtOrDefault(5)); Assert.Equal(5, source.ElementAtOrDefault(new Index(5))); @@ -229,7 +229,7 @@ public void NonEmptySource_Consistency() [Fact] public void NonEmptySource_Consistency_NotList() { - int?[] source = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + int?[] source = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; Assert.Equal(5, ForceNotCollection(source).ElementAtOrDefault(5)); Assert.Equal(5, ForceNotCollection(source).ElementAtOrDefault(new Index(5))); @@ -280,7 +280,7 @@ public void NonEmptySource_Consistency_NotList() [Fact] public void NonEmptySource_Consistency_ListPartition() { - int?[] source = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + int?[] source = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; Assert.Equal(5, ListPartitionOrEmpty(source).ElementAtOrDefault(5)); Assert.Equal(5, ListPartitionOrEmpty(source).ElementAtOrDefault(new Index(5))); @@ -311,7 +311,7 @@ public void NonEmptySource_Consistency_ListPartition() [Fact] public void NonEmptySource_Consistency_EnumerablePartition() { - int?[] source = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + int?[] source = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; Assert.Equal(5, EnumerablePartitionOrEmpty(source).ElementAtOrDefault(5)); Assert.Equal(5, EnumerablePartitionOrEmpty(source).ElementAtOrDefault(new Index(5))); @@ -342,7 +342,7 @@ public void NonEmptySource_Consistency_EnumerablePartition() [Fact] public void EmptySource_Consistency() { - int?[] source = { }; + int?[] source = []; Assert.Null(source.ElementAtOrDefault(1)); Assert.Null(source.ElementAtOrDefault(-1)); @@ -363,7 +363,7 @@ public void EmptySource_Consistency() [Fact] public void EmptySource_Consistency_NotList() { - int?[] source = { }; + int?[] source = []; Assert.Null(ForceNotCollection(source).ElementAtOrDefault(1)); Assert.Null(ForceNotCollection(source).ElementAtOrDefault(-1)); @@ -384,7 +384,7 @@ public void EmptySource_Consistency_NotList() [Fact] public void EmptySource_Consistency_ListPartition() { - int?[] source = { }; + int?[] source = []; Assert.Null(ListPartitionOrEmpty(source).ElementAtOrDefault(1)); Assert.Null(ListPartitionOrEmpty(source).ElementAtOrDefault(-1)); @@ -405,7 +405,7 @@ public void EmptySource_Consistency_ListPartition() [Fact] public void EmptySource_Consistency_EnumerablePartition() { - int?[] source = { }; + int?[] source = []; Assert.Null(EnumerablePartitionOrEmpty(source).ElementAtOrDefault(1)); Assert.Null(EnumerablePartitionOrEmpty(source).ElementAtOrDefault(-1)); diff --git a/tests/System.Linq.Tests/Tests/ZLinq/ElementAtTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/ElementAtTests.cs index ca28d8e1..94fc7a4d 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/ElementAtTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/ElementAtTests.cs @@ -33,12 +33,12 @@ public void SameResultsRepeatCallsStringQuery() public static IEnumerable TestData() { - yield return new object[] { NumberRangeGuaranteedNotCollectionType(9, 1), 0, 1, 9 }; - yield return new object[] { NumberRangeGuaranteedNotCollectionType(9, 10), 9, 1, 18 }; - yield return new object[] { NumberRangeGuaranteedNotCollectionType(-4, 10), 3, 7, -1 }; + yield return [NumberRangeGuaranteedNotCollectionType(9, 1), 0, 1, 9]; + yield return [NumberRangeGuaranteedNotCollectionType(9, 10), 9, 1, 18]; + yield return [NumberRangeGuaranteedNotCollectionType(-4, 10), 3, 7, -1]; - yield return new object[] { new int[] { -4 }, 0, 1, -4 }; - yield return new object[] { new int[] { 9, 8, 0, -5, 10 }, 4, 1, 10 }; + yield return [new int[] { -4 }, 0, 1, -4]; + yield return [new int[] { 9, 8, 0, -5, 10 }, 4, 1, 10]; } [Theory] @@ -99,7 +99,7 @@ public void InvalidIndex_ThrowsArgumentOutOfRangeException() [Fact] public void NullableArray_ValidIndex_ReturnsCorrectObject() { - int?[] source = { 9, 8, null, -5, 10 }; + int?[] source = [9, 8, null, -5, 10]; Assert.Null(source.ElementAt(2)); Assert.Equal(-5, source.ElementAt(3)); @@ -131,7 +131,7 @@ public void MutableSource() } { - source.InsertRange(3, new[] { -1, -2 }); + source.InsertRange(3, [-1, -2]); source.RemoveAt(0); Assert.Equal(-1, source.ElementAt(2)); Assert.Equal(-1, source.ElementAt(new Index(2))); @@ -157,7 +157,7 @@ public void MutableSourceNotList() var query1 = ForceNotCollection(source).Select(i => i); var query2 = ForceNotCollection(source).Select(i => i); var query3 = ForceNotCollection(source).Select(i => i); - source.InsertRange(3, new[] { -1, -2 }); + source.InsertRange(3, [-1, -2]); source.RemoveAt(0); Assert.Equal(-1, query1.ElementAt(2)); Assert.Equal(-1, query2.ElementAt(new Index(2))); @@ -207,7 +207,7 @@ public void EnumerateElements() [Fact] public void NonEmptySource_Consistency() { - int[] source = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + int[] source = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; Assert.Equal(5, source.ElementAt(5)); Assert.Equal(5, source.ElementAt(new Index(5))); @@ -238,7 +238,7 @@ public void NonEmptySource_Consistency() [Fact] public void NonEmptySource_Consistency_ThrowsIListIndexerException() { - int[] source = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + int[] source = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; Assert.Throws(() => source.ElementAt(-1)); Assert.Throws(() => source.ElementAt(^11)); // ImmutableArray implements IList. ElementAt calls ImmutableArray's indexer, which throws IndexOutOfRangeException instead of ArgumentOutOfRangeException. @@ -256,7 +256,7 @@ public void NonEmptySource_Consistency_ThrowsIListIndexerException() [Fact] public void NonEmptySource_Consistency_NotList() { - int[] source = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + int[] source = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; Assert.Equal(5, ForceNotCollection(source).ElementAt(5)); Assert.Equal(5, ForceNotCollection(source).ElementAt(new Index(5))); @@ -289,7 +289,7 @@ public void NonEmptySource_Consistency_NotList() [Fact] public void NonEmptySource_Consistency_ListPartition() { - int[] source = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + int[] source = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; Assert.Equal(5, ListPartitionOrEmpty(source).ElementAt(5)); Assert.Equal(5, ListPartitionOrEmpty(source).ElementAt(new Index(5))); @@ -320,7 +320,7 @@ public void NonEmptySource_Consistency_ListPartition() [Fact] public void NonEmptySource_Consistency_EnumerablePartition() { - int[] source = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + int[] source = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; Assert.Equal(5, EnumerablePartitionOrEmpty(source).ElementAt(5)); Assert.Equal(5, EnumerablePartitionOrEmpty(source).ElementAt(new Index(5))); @@ -351,7 +351,7 @@ public void NonEmptySource_Consistency_EnumerablePartition() [Fact] public void NonEmptySource_Consistency_Collection() { - int[] source = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + int[] source = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; Assert.Equal(5, new TestCollection(source).ElementAt(5)); Assert.Equal(5, new TestCollection(source).ElementAt(new Index(5))); @@ -382,7 +382,7 @@ public void NonEmptySource_Consistency_Collection() [Fact] public void NonEmptySource_Consistency_NonGenericCollection() { - int[] source = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + int[] source = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; Assert.Equal(5, new TestNonGenericCollection(source.ToArray()).ElementAt(5)); Assert.Equal(5, new TestNonGenericCollection(source.ToArray()).ElementAt(new Index(5))); @@ -413,7 +413,7 @@ public void NonEmptySource_Consistency_NonGenericCollection() [Fact] public void EmptySource_Consistency() { - int[] source = { }; + int[] source = []; Assert.Throws(() => source.ElementAt(1)); Assert.Throws(() => source.ElementAt(-1)); @@ -434,7 +434,7 @@ public void EmptySource_Consistency() [Fact] public void EmptySource_Consistency_ThrowsIListIndexerException() { - int[] source = { }; + int[] source = []; Assert.Throws(() => source.ElementAt(-1)); Assert.Throws(() => source.ElementAt(^1)); // ImmutableArray implements IList. ElementAt calls ImmutableArray's indexer, which throws IndexOutOfRangeException instead of ArgumentOutOfRangeException. @@ -455,7 +455,7 @@ public void EmptySource_Consistency_ThrowsIListIndexerException() [Fact] public void EmptySource_Consistency_NotList() { - int[] source = { }; + int[] source = []; Assert.Throws(() => ForceNotCollection(source).ElementAt(1)); Assert.Throws(() => ForceNotCollection(source).ElementAt(-1)); @@ -476,7 +476,7 @@ public void EmptySource_Consistency_NotList() [Fact] public void EmptySource_Consistency_ListPartition() { - int[] source = { }; + int[] source = []; Assert.Throws(() => ListPartitionOrEmpty(source).ElementAt(1)); Assert.Throws(() => ListPartitionOrEmpty(source).ElementAt(-1)); @@ -497,7 +497,7 @@ public void EmptySource_Consistency_ListPartition() [Fact] public void EmptySource_Consistency_EnumerablePartition() { - int[] source = { }; + int[] source = []; Assert.Throws(() => EnumerablePartitionOrEmpty(source).ElementAt(1)); Assert.Throws(() => EnumerablePartitionOrEmpty(source).ElementAt(-1)); @@ -518,7 +518,7 @@ public void EmptySource_Consistency_EnumerablePartition() [Fact] public void EmptySource_Consistency_Collection() { - int[] source = { }; + int[] source = []; Assert.Throws(() => new TestCollection(source).ElementAt(1)); Assert.Throws(() => new TestCollection(source).ElementAt(-1)); @@ -539,7 +539,7 @@ public void EmptySource_Consistency_Collection() [Fact] public void EmptySource_Consistency_NonGenericCollection() { - int[] source = { }; + int[] source = []; Assert.Throws(() => new TestNonGenericCollection(source.ToArray()).ElementAt(1)); Assert.Throws(() => new TestNonGenericCollection(source.ToArray()).ElementAt(-1)); diff --git a/tests/System.Linq.Tests/Tests/ZLinq/ExceptTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/ExceptTests.cs index c668ebb2..a9431a6b 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/ExceptTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/ExceptTests.cs @@ -32,10 +32,10 @@ public void SameResultsRepeatCallsStringQuery() public static IEnumerable Int_TestData() { - yield return new object[] { new int[0], new int[0], null, new int[0] }; - yield return new object[] { new int[0], new int[] { -6, -8, -6, 2, 0, 0, 5, 6 }, null, new int[0] }; + yield return [new int[0], new int[0], null, new int[0]]; + yield return [new int[0], new int[] { -6, -8, -6, 2, 0, 0, 5, 6 }, null, new int[0]]; - yield return new object[] { new int[] { 1, 1, 1, 1, 1 }, new int[] { 2, 3, 4 }, null, new int[] { 1 } }; + yield return [new int[] { 1, 1, 1, 1, 1 }, new int[] { 2, 3, 4 }, null, new int[] { 1 }]; } [Theory] @@ -52,12 +52,11 @@ public void Int(IEnumerable first, IEnumerable second, IEqualityCompar public static IEnumerable String_TestData() { IEqualityComparer defaultComparer = EqualityComparer.Default; - yield return new object[] { new string[1], new string[0], defaultComparer, new string[1] }; - yield return new object[] { new string[] { null, null, string.Empty }, new string[1], defaultComparer, new string[] { string.Empty } }; - yield return new object[] { new string[2], new string[0], defaultComparer, new string[1] }; - - yield return new object[] { new string[] { "Bob", "Tim", "Robert", "Chris" }, new string[] { "bBo", "shriC" }, null, new string[] { "Bob", "Tim", "Robert", "Chris" } }; - yield return new object[] { new string[] { "Bob", "Tim", "Robert", "Chris" }, new string[] { "bBo", "shriC" }, new AnagramEqualityComparer(), new string[] { "Tim", "Robert" } }; + yield return [new string[1], new string[0], defaultComparer, new string[1]]; + yield return [new string[] { null, null, string.Empty }, new string[1], defaultComparer, new string[] { string.Empty }]; + yield return [new string[2], new string[0], defaultComparer, new string[1]]; + yield return [new string[] { "Bob", "Tim", "Robert", "Chris" }, new string[] { "bBo", "shriC" }, null, new string[] { "Bob", "Tim", "Robert", "Chris" }]; + yield return [new string[] { "Bob", "Tim", "Robert", "Chris" }, new string[] { "bBo", "shriC" }, new AnagramEqualityComparer(), new string[] { "Tim", "Robert" }]; } [Theory] @@ -73,10 +72,9 @@ public void String(IEnumerable first, IEnumerable second, IEqual public static IEnumerable NullableInt_TestData() { - yield return new object[] { new int?[] { -6, -8, -6, 2, 0, 0, 5, 6, null, null }, new int?[0], new int?[] { -6, -8, 2, 0, 5, 6, null } }; - - yield return new object[] { new int?[] { 1, 2, 2, 3, 4, 5 }, new int?[] { 5, 3, 2, 6, 6, 3, 1, null, null }, new int?[] { 4 } }; - yield return new object[] { new int?[] { 2, 3, null, 2, null, 4, 5 }, new int?[] { 1, 9, null, 4 }, new int?[] { 2, 3, 5 } }; + yield return [new int?[] { -6, -8, -6, 2, 0, 0, 5, 6, null, null }, new int?[0], new int?[] { -6, -8, 2, 0, 5, 6, null }]; + yield return [new int?[] { 1, 2, 2, 3, 4, 5 }, new int?[] { 5, 3, 2, 6, 6, 3, 1, null, null }, new int?[] { 4 }]; + yield return [new int?[] { 2, 3, null, 2, null, 4, 5 }, new int?[] { 1, 9, null, 4 }, new int?[] { 2, 3, 5 }]; } [Theory] @@ -97,7 +95,7 @@ public void NullableIntRunOnce(IEnumerable first, IEnumerable second public void FirstNull_ThrowsArgumentNullException() { string[] first = null; - string[] second = { "bBo", "shriC" }; + string[] second = ["bBo", "shriC"]; AssertExtensions.Throws(() => first.Except(second)); AssertExtensions.Throws(() => first.Except(second, new AnagramEqualityComparer())); @@ -106,7 +104,7 @@ public void FirstNull_ThrowsArgumentNullException() [Fact] public void SecondNull_ThrowsArgumentNullException() { - string[] first = { "Bob", "Tim", "Robert", "Chris" }; + string[] first = ["Bob", "Tim", "Robert", "Chris"]; string[] second = null; AssertExtensions.Throws("second", () => first.Except(second)); @@ -126,24 +124,24 @@ public void ForcedToEnumeratorDoesntEnumerate() public void HashSetWithBuiltInComparer_HashSetContainsNotUsed() { IEnumerable input1 = new HashSet(StringComparer.OrdinalIgnoreCase) { "a" }; - IEnumerable input2 = new[] { "A" }; + IEnumerable input2 = ["A"]; - Assert.Equal(new[] { "a" }, input1.Except(input2)); - Assert.Equal(new[] { "a" }, input1.Except(input2, null)); - Assert.Equal(new[] { "a" }, input1.Except(input2, EqualityComparer.Default)); - Assert.Equal(Enumerable.Empty(), input1.Except(input2, StringComparer.OrdinalIgnoreCase)); + Assert.Equal(["a"], input1.Except(input2)); + Assert.Equal(["a"], input1.Except(input2, null)); + Assert.Equal(["a"], input1.Except(input2, EqualityComparer.Default)); + Assert.Equal([], input1.Except(input2, StringComparer.OrdinalIgnoreCase)); - Assert.Equal(new[] { "A" }, input2.Except(input1)); - Assert.Equal(new[] { "A" }, input2.Except(input1, null)); - Assert.Equal(new[] { "A" }, input2.Except(input1, EqualityComparer.Default)); - Assert.Equal(Enumerable.Empty(), input2.Except(input1, StringComparer.OrdinalIgnoreCase)); + Assert.Equal(["A"], input2.Except(input1)); + Assert.Equal(["A"], input2.Except(input1, null)); + Assert.Equal(["A"], input2.Except(input1, EqualityComparer.Default)); + Assert.Equal([], input2.Except(input1, StringComparer.OrdinalIgnoreCase)); } [Fact] public void ExceptBy_FirstNull_ThrowsArgumentNullException() { string[] first = null; - string[] second = { "bBo", "shriC" }; + string[] second = ["bBo", "shriC"]; AssertExtensions.Throws(() => first.ExceptBy(second, x => x)); AssertExtensions.Throws(() => first.ExceptBy(second, x => x, new AnagramEqualityComparer())); @@ -152,7 +150,7 @@ public void ExceptBy_FirstNull_ThrowsArgumentNullException() [Fact] public void ExceptBy_SecondNull_ThrowsArgumentNullException() { - string[] first = { "Bob", "Tim", "Robert", "Chris" }; + string[] first = ["Bob", "Tim", "Robert", "Chris"]; string[] second = null; AssertExtensions.Throws("second", () => first.ExceptBy(second, x => x)); @@ -162,8 +160,8 @@ public void ExceptBy_SecondNull_ThrowsArgumentNullException() [Fact] public void ExceptBy_KeySelectorNull_ThrowsArgumentNullException() { - string[] first = { "Bob", "Tim", "Robert", "Chris" }; - string[] second = { "bBo", "shriC" }; + string[] first = ["Bob", "Tim", "Robert", "Chris"]; + string[] second = ["bBo", "shriC"]; Func keySelector = null; AssertExtensions.Throws("keySelector", () => first.ExceptBy(second, keySelector)); @@ -195,7 +193,7 @@ public static IEnumerable ExceptBy_TestData() yield return WrapArgs( first: Enumerable.Repeat(5, 20), - second: Enumerable.Empty(), + second: [], keySelector: x => x, comparer: null, expected: Enumerable.Repeat(5, 1)); @@ -205,45 +203,45 @@ public static IEnumerable ExceptBy_TestData() second: Enumerable.Repeat(5, 3), keySelector: x => x, comparer: null, - expected: Enumerable.Empty()); + expected: []); yield return WrapArgs( - first: new string[] { "Bob", "Tim", "Robert", "Chris" }, - second: new string[] { "bBo", "shriC" }, + first: ["Bob", "Tim", "Robert", "Chris"], + second: ["bBo", "shriC"], keySelector: x => x, null, - expected: new string[] { "Bob", "Tim", "Robert", "Chris" }); + expected: ["Bob", "Tim", "Robert", "Chris"]); yield return WrapArgs( - first: new string[] { "Bob", "Tim", "Robert", "Chris" }, - second: new string[] { "bBo", "shriC" }, + first: ["Bob", "Tim", "Robert", "Chris"], + second: ["bBo", "shriC"], keySelector: x => x, new AnagramEqualityComparer(), - expected: new string[] { "Tim", "Robert" }); + expected: ["Tim", "Robert"]); yield return WrapArgs( first: new (string Name, int Age)[] { ("Tom", 20), ("Dick", 30), ("Harry", 40) }, - second: new int[] { 15, 20, 40 }, + second: [15, 20, 40], keySelector: x => x.Age, comparer: null, expected: new (string Name, int Age)[] { ("Dick", 30) }); yield return WrapArgs( first: new (string Name, int Age)[] { ("Tom", 20), ("Dick", 30), ("Harry", 40) }, - second: new string[] { "moT" }, + second: ["moT"], keySelector: x => x.Name, comparer: null, expected: new (string Name, int Age)[] { ("Tom", 20), ("Dick", 30), ("Harry", 40) }); yield return WrapArgs( first: new (string Name, int Age)[] { ("Tom", 20), ("Dick", 30), ("Harry", 40) }, - second: new string[] { "moT" }, + second: ["moT"], keySelector: x => x.Name, comparer: new AnagramEqualityComparer(), expected: new (string Name, int Age)[] { ("Dick", 30), ("Harry", 40) }); object[] WrapArgs(IEnumerable first, IEnumerable second, Func keySelector, IEqualityComparer? comparer, IEnumerable expected) - => new object[] { first, second, keySelector, comparer, expected }; + => [first, second, keySelector, comparer, expected]; } } } diff --git a/tests/System.Linq.Tests/Tests/ZLinq/FirstOrDefaultTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/FirstOrDefaultTests.cs index 0662eb71..7d412eb9 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/FirstOrDefaultTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/FirstOrDefaultTests.cs @@ -31,7 +31,7 @@ public void SameResultsRepeatCallsStringQuery() private static void TestEmptyIList() { - T[] source = { }; + T[] source = []; T expected = default(T); Assert.IsAssignableFrom>(source); @@ -41,7 +41,7 @@ private static void TestEmptyIList() private static void TestEmptyIListDefault(T defaultValue) { - T[] source = { }; + T[] source = []; Assert.IsAssignableFrom>(source); @@ -68,7 +68,7 @@ public void EmptyIListDefault() [Fact] public void IListTOneElement() { - int[] source = { 5 }; + int[] source = [5]; int expected = 5; Assert.IsAssignableFrom>(source); @@ -79,7 +79,7 @@ public void IListTOneElement() [Fact] public void IListOneElementDefault() { - int[] source = { 5 }; + int[] source = [5]; int expected = 5; Assert.IsAssignableFrom>(source); @@ -90,7 +90,7 @@ public void IListOneElementDefault() [Fact] public void IListTManyElementsFirstIsDefault() { - int?[] source = { null, -10, 2, 4, 3, 0, 2 }; + int?[] source = [null, -10, 2, 4, 3, 0, 2]; int? expected = null; Assert.IsAssignableFrom>(source); @@ -101,7 +101,7 @@ public void IListTManyElementsFirstIsDefault() [Fact] public void IListTManyElementsFirstIsNotDefault() { - int?[] source = { 19, null, -10, 2, 4, 3, 0, 2 }; + int?[] source = [19, null, -10, 2, 4, 3, 0, 2]; int? expected = 19; Assert.IsAssignableFrom>(source); @@ -172,7 +172,7 @@ public void ManyElementsNotIListT() [Fact] public void EmptySource() { - int?[] source = { }; + int?[] source = []; Assert.All(CreateSources(source), source => { @@ -184,7 +184,7 @@ public void EmptySource() [Fact] public void OneElementTruePredicate() { - int[] source = { 4 }; + int[] source = [4]; Func predicate = IsEven; int expected = 4; @@ -197,7 +197,7 @@ public void OneElementTruePredicate() [Fact] public void OneElementTruePredicateDefault() { - int[] source = { 4 }; + int[] source = [4]; Func predicate = IsEven; int expected = 4; @@ -210,7 +210,7 @@ public void OneElementTruePredicateDefault() [Fact] public void ManyElementsPredicateFalseForAll() { - int[] source = { 9, 5, 1, 3, 17, 21 }; + int[] source = [9, 5, 1, 3, 17, 21]; Func predicate = IsEven; int expected = default(int); @@ -223,7 +223,7 @@ public void ManyElementsPredicateFalseForAll() [Fact] public void ManyElementsPredicateFalseForAllDefault() { - int[] source = { 9, 5, 1, 3, 17, 21 }; + int[] source = [9, 5, 1, 3, 17, 21]; Func predicate = IsEven; int expected = 5; @@ -236,7 +236,7 @@ public void ManyElementsPredicateFalseForAllDefault() [Fact] public void PredicateTrueOnlyForLast() { - int[] source = { 9, 5, 1, 3, 17, 21, 50 }; + int[] source = [9, 5, 1, 3, 17, 21, 50]; Func predicate = IsEven; int expected = 50; @@ -249,7 +249,7 @@ public void PredicateTrueOnlyForLast() [Fact] public void PredicateTrueOnlyForLastDefault() { - int[] source = { 9, 5, 1, 3, 17, 21, 50 }; + int[] source = [9, 5, 1, 3, 17, 21, 50]; Func predicate = IsEven; int expected = 50; @@ -262,7 +262,7 @@ public void PredicateTrueOnlyForLastDefault() [Fact] public void PredicateTrueForSome() { - int[] source = { 3, 7, 10, 7, 9, 2, 11, 17, 13, 8 }; + int[] source = [3, 7, 10, 7, 9, 2, 11, 17, 13, 8]; Func predicate = IsEven; int expected = 10; @@ -275,7 +275,7 @@ public void PredicateTrueForSome() [Fact] public void PredicateTrueForSomeDefault() { - int[] source = { 3, 7, 10, 7, 9, 2, 11, 17, 13, 8 }; + int[] source = [3, 7, 10, 7, 9, 2, 11, 17, 13, 8]; Func predicate = IsEven; int expected = 10; @@ -288,7 +288,7 @@ public void PredicateTrueForSomeDefault() [Fact] public void PredicateTrueForSomeRunOnce() { - int[] source = { 3, 7, 10, 7, 9, 2, 11, 17, 13, 8 }; + int[] source = [3, 7, 10, 7, 9, 2, 11, 17, 13, 8]; Func predicate = IsEven; int expected = 10; diff --git a/tests/System.Linq.Tests/Tests/ZLinq/FirstTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/FirstTests.cs index 1ddb3a9e..c87b00bf 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/FirstTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/FirstTests.cs @@ -31,7 +31,7 @@ public void SameResultsRepeatCallsStringQuery() private static void TestEmptyIList() { - T[] source = { }; + T[] source = []; Assert.NotNull(source as IList); @@ -50,7 +50,7 @@ public void EmptyIListT() [Fact] public void IListTOneElement() { - int[] source = { 5 }; + int[] source = [5]; int expected = 5; Assert.NotNull(source as IList); @@ -61,7 +61,7 @@ public void IListTOneElement() [Fact] public void IListTManyElementsFirstIsDefault() { - int?[] source = { null, -10, 2, 4, 3, 0, 2 }; + int?[] source = [null, -10, 2, 4, 3, 0, 2]; int? expected = null; Assert.IsAssignableFrom>(source); @@ -72,7 +72,7 @@ public void IListTManyElementsFirstIsDefault() [Fact] public void IListTManyElementsFirstIsNotDefault() { - int?[] source = { 19, null, -10, 2, 4, 3, 0, 2 }; + int?[] source = [19, null, -10, 2, 4, 3, 0, 2]; int? expected = 19; Assert.IsAssignableFrom>(source); @@ -128,7 +128,7 @@ public void ManyElementsNotIListT() [Fact] public void EmptySource() { - int[] source = { }; + int[] source = []; Assert.All(CreateSources(source), source => { Assert.Throws(() => source.First(x => true)); @@ -139,7 +139,7 @@ public void EmptySource() [Fact] public void OneElementTruePredicate() { - int[] source = { 4 }; + int[] source = [4]; Func predicate = IsEven; int expected = 4; @@ -152,7 +152,7 @@ public void OneElementTruePredicate() [Fact] public void ManyElementsPredicateFalseForAll() { - int[] source = { 9, 5, 1, 3, 17, 21 }; + int[] source = [9, 5, 1, 3, 17, 21]; Func predicate = IsEven; Assert.All(CreateSources(source), source => @@ -164,7 +164,7 @@ public void ManyElementsPredicateFalseForAll() [Fact] public void PredicateTrueOnlyForLast() { - int[] source = { 9, 5, 1, 3, 17, 21, 50 }; + int[] source = [9, 5, 1, 3, 17, 21, 50]; Func predicate = IsEven; int expected = 50; @@ -177,7 +177,7 @@ public void PredicateTrueOnlyForLast() [Fact] public void PredicateTrueForSome() { - int[] source = { 3, 7, 10, 7, 9, 2, 11, 17, 13, 8 }; + int[] source = [3, 7, 10, 7, 9, 2, 11, 17, 13, 8]; Func predicate = IsEven; int expected = 10; @@ -187,7 +187,7 @@ public void PredicateTrueForSome() [Fact] public void PredicateTrueForSomeRunOnce() { - int[] source = { 3, 7, 10, 7, 9, 2, 11, 17, 13, 8 }; + int[] source = [3, 7, 10, 7, 9, 2, 11, 17, 13, 8]; Func predicate = IsEven; int expected = 10; diff --git a/tests/System.Linq.Tests/Tests/ZLinq/GroupByTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/GroupByTests.cs index 17cae96c..c57180b6 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/GroupByTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/GroupByTests.cs @@ -28,7 +28,7 @@ private static void AssertGroupingCorrect(IEnumerable keys Assert.NotNull(keys); Dictionary> dict = new Dictionary>(keyComparer); - List groupingForNullKeys = new List(); + List groupingForNullKeys = []; using (IEnumerator elEn = elements.GetEnumerator()) using (IEnumerator keyEn = keys.GetEnumerator()) { @@ -46,7 +46,7 @@ private static void AssertGroupingCorrect(IEnumerable keys { List list; if (!dict.TryGetValue(key, out list)) - dict.Add(key, list = new List()); + dict.Add(key, list = []); list.Add(elEn.Current); } } @@ -195,8 +195,8 @@ public void Grouping_IList_IndexOf() [Fact] public void SingleNullKeySingleNullElement() { - string[] key = { null }; - string[] element = { null }; + string[] key = [null]; + string[] element = [null]; AssertGroupingCorrect(key, element, new string[] { null }.GroupBy(e => e, e => e, EqualityComparer.Default).ToArray(), EqualityComparer.Default); } @@ -204,18 +204,18 @@ public void SingleNullKeySingleNullElement() [Fact] public void EmptySource() { - string[] key = { }; - int[] element = { }; - Record[] source = { }; + string[] key = []; + int[] element = []; + Record[] source = []; Assert.Empty(new Record[] { }.GroupBy(e => e.Name, e => e.Score, new AnagramEqualityComparer())); } [Fact] public void EmptySourceRunOnce() { - string[] key = { }; - int[] element = { }; - Record[] source = { }; + string[] key = []; + int[] element = []; + Record[] source = []; Assert.Empty(new Record[] { }.RunOnce().GroupBy(e => e.Name, e => e.Score, new AnagramEqualityComparer())); } @@ -251,15 +251,15 @@ public void SourceIsNullResultSelectorUsedNoComparerOrElementSelector() [Fact] public void KeySelectorNull() { - Record[] source = new[] - { + Record[] source = + [ new Record { Name = "Tim", Score = 55 }, new Record { Name = "Chris", Score = 49 }, new Record { Name = "Robert", Score = -100 }, new Record { Name = "Chris", Score = 24 }, new Record { Name = "Prakash", Score = 9 }, new Record { Name = "Tim", Score = 25 } - }; + ]; AssertExtensions.Throws("keySelector", () => source.GroupBy(null, e => e.Score, new AnagramEqualityComparer())); AssertExtensions.Throws("keySelector", () => source.GroupBy(null, new AnagramEqualityComparer())); @@ -268,15 +268,15 @@ public void KeySelectorNull() [Fact] public void KeySelectorNullResultSelectorUsed() { - Record[] source = new[] - { + Record[] source = + [ new Record { Name = "Tim", Score = 55 }, new Record { Name = "Chris", Score = 49 }, new Record { Name = "Robert", Score = -100 }, new Record { Name = "Chris", Score = 24 }, new Record { Name = "Prakash", Score = 9 }, new Record { Name = "Tim", Score = 25 } - }; + ]; AssertExtensions.Throws("keySelector", () => source.GroupBy(null, e => e.Score, (k, es) => es.Sum(), new AnagramEqualityComparer())); } @@ -284,15 +284,15 @@ public void KeySelectorNullResultSelectorUsed() [Fact] public void KeySelectorNullResultSelectorUsedNoComparer() { - Record[] source = new[] - { + Record[] source = + [ new Record { Name = "Tim", Score = 55 }, new Record { Name = "Chris", Score = 49 }, new Record { Name = "Robert", Score = -100 }, new Record { Name = "Chris", Score = 24 }, new Record { Name = "Prakash", Score = 9 }, new Record { Name = "Tim", Score = 25 } - }; + ]; Func keySelector = null; @@ -302,8 +302,8 @@ public void KeySelectorNullResultSelectorUsedNoComparer() [Fact] public void KeySelectorNullResultSelectorUsedNoElementSelector() { - string[] key = { "Tim", "Tim", "Tim", "Tim" }; - int[] element = { 60, -10, 40, 100 }; + string[] key = ["Tim", "Tim", "Tim", "Tim"]; + int[] element = [60, -10, 40, 100]; AssertExtensions.Throws("keySelector", () => { @@ -315,15 +315,15 @@ public void KeySelectorNullResultSelectorUsedNoElementSelector() [Fact] public void ElementSelectorNull() { - Record[] source = new[] - { + Record[] source = + [ new Record { Name = "Tim", Score = 55 }, new Record { Name = "Chris", Score = 49 }, new Record { Name = "Robert", Score = -100 }, new Record { Name = "Chris", Score = 24 }, new Record { Name = "Prakash", Score = 9 }, new Record { Name = "Tim", Score = 25 } - }; + ]; Func elementSelector = null; @@ -333,15 +333,15 @@ public void ElementSelectorNull() [Fact] public void ElementSelectorNullResultSelectorUsedNoComparer() { - Record[] source = new[] - { + Record[] source = + [ new Record { Name = "Tim", Score = 55 }, new Record { Name = "Chris", Score = 49 }, new Record { Name = "Robert", Score = -100 }, new Record { Name = "Chris", Score = 24 }, new Record { Name = "Prakash", Score = 9 }, new Record { Name = "Tim", Score = 25 } - }; + ]; Func elementSelector = null; @@ -351,14 +351,14 @@ public void ElementSelectorNullResultSelectorUsedNoComparer() [Fact] public void ResultSelectorNull() { - Record[] source = { + Record[] source = [ new Record { Name = "Tim", Score = 55 }, new Record { Name = "Chris", Score = 49 }, new Record { Name = "Robert", Score = -100 }, new Record { Name = "Chris", Score = 24 }, new Record { Name = "Prakash", Score = 9 }, new Record { Name = "Tim", Score = 25 } - }; + ]; Func, long> resultSelector = null; @@ -368,14 +368,14 @@ public void ResultSelectorNull() [Fact] public void ResultSelectorNullNoComparer() { - Record[] source = { + Record[] source = [ new Record { Name = "Tim", Score = 55 }, new Record { Name = "Chris", Score = 49 }, new Record { Name = "Robert", Score = -100 }, new Record { Name = "Chris", Score = 24 }, new Record { Name = "Prakash", Score = 9 }, new Record { Name = "Tim", Score = 25 } - }; + ]; Func, long> resultSelector = null; @@ -385,14 +385,14 @@ public void ResultSelectorNullNoComparer() [Fact] public void ResultSelectorNullNoElementSelector() { - Record[] source = { + Record[] source = [ new Record { Name = "Tim", Score = 55 }, new Record { Name = "Chris", Score = 49 }, new Record { Name = "Robert", Score = -100 }, new Record { Name = "Chris", Score = 24 }, new Record { Name = "Prakash", Score = 9 }, new Record { Name = "Tim", Score = 25 } - }; + ]; Func, long> resultSelector = null; @@ -402,8 +402,8 @@ public void ResultSelectorNullNoElementSelector() [Fact] public void ResultSelectorNullNoElementSelectorCustomComparer() { - string[] key = { "Tim", "Tim", "Tim", "Tim" }; - int[] element = { 60, -10, 40, 100 }; + string[] key = ["Tim", "Tim", "Tim", "Tim"]; + int[] element = [60, -10, 40, 100]; Func, long> resultSelector = null; @@ -417,26 +417,26 @@ public void ResultSelectorNullNoElementSelectorCustomComparer() [Fact] public void EmptySourceWithResultSelector() { - string[] key = { }; - int[] element = { }; - Record[] source = { }; + string[] key = []; + int[] element = []; + Record[] source = []; Assert.Empty(new Record[] { }.GroupBy(e => e.Name, e => e.Score, (k, es) => (long)(k ?? " ").Length * es.Sum(), new AnagramEqualityComparer())); } [Fact] public void DuplicateKeysCustomComparer() { - string[] key = { "Tim", "Tim", "Chris", "Chris", "Robert", "Prakash" }; - int[] element = { 55, 25, 49, 24, -100, 9 }; - Record[] source = { + string[] key = ["Tim", "Tim", "Chris", "Chris", "Robert", "Prakash"]; + int[] element = [55, 25, 49, 24, -100, 9]; + Record[] source = [ new Record { Name = "Tim", Score = 55 }, new Record { Name = "Chris", Score = 49 }, new Record { Name = "Robert", Score = -100 }, new Record { Name = "Chris", Score = 24 }, new Record { Name = "Prakash", Score = 9 }, new Record { Name = "miT", Score = 25 } - }; - long[] expected = { 240, 365, -600, 63 }; + ]; + long[] expected = [240, 365, -600, 63]; Assert.Equal(expected, source.GroupBy(e => e.Name, e => e.Score, (k, es) => (long)(k ?? " ").Length * es.Sum(), new AnagramEqualityComparer())); } @@ -444,17 +444,17 @@ public void DuplicateKeysCustomComparer() [Fact] public void DuplicateKeysCustomComparerRunOnce() { - string[] key = { "Tim", "Tim", "Chris", "Chris", "Robert", "Prakash" }; - int[] element = { 55, 25, 49, 24, -100, 9 }; - Record[] source = { + string[] key = ["Tim", "Tim", "Chris", "Chris", "Robert", "Prakash"]; + int[] element = [55, 25, 49, 24, -100, 9]; + Record[] source = [ new Record { Name = "Tim", Score = 55 }, new Record { Name = "Chris", Score = 49 }, new Record { Name = "Robert", Score = -100 }, new Record { Name = "Chris", Score = 24 }, new Record { Name = "Prakash", Score = 9 }, new Record { Name = "miT", Score = 25 } - }; - long[] expected = { 240, 365, -600, 63 }; + ]; + long[] expected = [240, 365, -600, 63]; Assert.Equal(expected, source.RunOnce().GroupBy(e => e.Name, e => e.Score, (k, es) => (long)(k ?? " ").Length * es.Sum(), new AnagramEqualityComparer())); } @@ -462,17 +462,17 @@ public void DuplicateKeysCustomComparerRunOnce() [Fact] public void NullComparer() { - string[] key = { "Tim", null, null, "Robert", "Chris", "miT" }; - int[] element = { 55, 49, 9, -100, 24, 25 }; - Record[] source = { + string[] key = ["Tim", null, null, "Robert", "Chris", "miT"]; + int[] element = [55, 49, 9, -100, 24, 25]; + Record[] source = [ new Record { Name = "Tim", Score = 55 }, new Record { Name = null, Score = 49 }, new Record { Name = "Robert", Score = -100 }, new Record { Name = "Chris", Score = 24 }, new Record { Name = null, Score = 9 }, new Record { Name = "miT", Score = 25 } - }; - long[] expected = { 165, 58, -600, 120, 75 }; + ]; + long[] expected = [165, 58, -600, 120, 75]; Assert.Equal(expected, source.GroupBy(e => e.Name, e => e.Score, (k, es) => (long)(k ?? " ").Length * es.Sum(), null)); } @@ -480,17 +480,17 @@ public void NullComparer() [Fact] public void NullComparerRunOnce() { - string[] key = { "Tim", null, null, "Robert", "Chris", "miT" }; - int[] element = { 55, 49, 9, -100, 24, 25 }; - Record[] source = { + string[] key = ["Tim", null, null, "Robert", "Chris", "miT"]; + int[] element = [55, 49, 9, -100, 24, 25]; + Record[] source = [ new Record { Name = "Tim", Score = 55 }, new Record { Name = null, Score = 49 }, new Record { Name = "Robert", Score = -100 }, new Record { Name = "Chris", Score = 24 }, new Record { Name = null, Score = 9 }, new Record { Name = "miT", Score = 25 } - }; - long[] expected = { 165, 58, -600, 120, 75 }; + ]; + long[] expected = [165, 58, -600, 120, 75]; Assert.Equal(expected, source.RunOnce().GroupBy(e => e.Name, e => e.Score, (k, es) => (long)(k ?? " ").Length * es.Sum(), null)); } @@ -498,8 +498,8 @@ public void NullComparerRunOnce() [Fact] public void SingleNonNullElement() { - string[] key = { "Tim" }; - Record[] source = { new Record { Name = key[0], Score = 60 } }; + string[] key = ["Tim"]; + Record[] source = [new Record { Name = key[0], Score = 60 }]; AssertGroupingCorrect(key, source, source.GroupBy(e => e.Name).ToArray()); } @@ -507,8 +507,8 @@ public void SingleNonNullElement() [Fact] public void AllElementsSameKey() { - string[] key = { "Tim", "Tim", "Tim", "Tim" }; - int[] scores = { 60, -10, 40, 100 }; + string[] key = ["Tim", "Tim", "Tim", "Tim"]; + int[] scores = [60, -10, 40, 100]; var source = key.Zip(scores, (k, e) => new Record { Name = k, Score = e }); AssertGroupingCorrect(key, source.ToArray(), source.GroupBy(e => e.Name, new AnagramEqualityComparer()).ToArray(), new AnagramEqualityComparer()); @@ -517,8 +517,8 @@ public void AllElementsSameKey() [Fact] public void AllElementsDifferentKeyElementSelectorUsed() { - string[] key = { "Tim", "Chris", "Robert", "Prakash" }; - int[] element = { 60, -10, 40, 100 }; + string[] key = ["Tim", "Chris", "Robert", "Prakash"]; + int[] element = [60, -10, 40, 100]; var source = key.Zip(element, (k, e) => new Record { Name = k, Score = e }); AssertGroupingCorrect(key, element, source.GroupBy(e => e.Name, e => e.Score).ToArray()); @@ -527,8 +527,8 @@ public void AllElementsDifferentKeyElementSelectorUsed() [Fact] public void SomeDuplicateKeys() { - string[] key = { "Tim", "Tim", "Chris", "Chris", "Robert", "Prakash" }; - int[] element = { 55, 25, 49, 24, -100, 9 }; + string[] key = ["Tim", "Tim", "Chris", "Chris", "Robert", "Prakash"]; + int[] element = [55, 25, 49, 24, -100, 9]; var source = key.Zip(element, (k, e) => new Record { Name = k, Score = e }); AssertGroupingCorrect(key, element, source.GroupBy(e => e.Name, e => e.Score).ToArray()); @@ -537,8 +537,8 @@ public void SomeDuplicateKeys() [Fact] public void SomeDuplicateKeysIncludingNulls() { - string[] key = { null, null, "Chris", "Chris", "Prakash", "Prakash" }; - int[] element = { 55, 25, 49, 24, 9, 9 }; + string[] key = [null, null, "Chris", "Chris", "Prakash", "Prakash"]; + int[] element = [55, 25, 49, 24, 9, 9]; var source = key.Zip(element, (k, e) => new Record { Name = k, Score = e }); AssertGroupingCorrect(key, element, source.GroupBy(e => e.Name, e => e.Score).ToArray()); @@ -547,9 +547,9 @@ public void SomeDuplicateKeysIncludingNulls() [Fact] public void SingleElementResultSelectorUsed() { - string[] key = { "Tim" }; - int[] element = { 60 }; - long[] expected = { 180 }; + string[] key = ["Tim"]; + int[] element = [60]; + long[] expected = [180]; var source = key.Zip(element, (k, e) => new Record { Name = k, Score = e }); Assert.Equal(expected, source.GroupBy(e => e.Name, (k, es) => (long)(k ?? " ").Length * es.Sum(e => e.Score))); @@ -574,10 +574,10 @@ public void GroupedResultCorrectSize() [Fact] public void AllElementsDifferentKeyElementSelectorUsedResultSelector() { - string[] key = { "Tim", "Chris", "Robert", "Prakash" }; - int[] element = { 60, -10, 40, 100 }; + string[] key = ["Tim", "Chris", "Robert", "Prakash"]; + int[] element = [60, -10, 40, 100]; var source = key.Zip(element, (k, e) => new Record { Name = k, Score = e }); - long[] expected = { 180, -50, 240, 700 }; + long[] expected = [180, -50, 240, 700]; Assert.Equal(expected, source.GroupBy(e => e.Name, e => e.Score, (k, es) => (long)(k ?? " ").Length * es.Sum())); } @@ -585,14 +585,14 @@ public void AllElementsDifferentKeyElementSelectorUsedResultSelector() [Fact] public void AllElementsSameKeyResultSelectorUsed() { - int[] element = { 60, -10, 40, 100 }; - long[] expected = { 570 }; - Record[] source = { + int[] element = [60, -10, 40, 100]; + long[] expected = [570]; + Record[] source = [ new Record { Name = "Tim", Score = element[0] }, new Record { Name = "Tim", Score = element[1] }, new Record { Name = "miT", Score = element[2] }, new Record { Name = "miT", Score = element[3] } - }; + ]; Assert.Equal(expected, source.GroupBy(e => e.Name, (k, es) => k.Length * es.Sum(e => (long)e.Score), new AnagramEqualityComparer())); } @@ -600,15 +600,15 @@ public void AllElementsSameKeyResultSelectorUsed() [Fact] public void NullComparerResultSelectorUsed() { - int[] element = { 60, -10, 40, 100 }; - Record[] source = { + int[] element = [60, -10, 40, 100]; + Record[] source = [ new Record { Name = "Tim", Score = element[0] }, new Record { Name = "Tim", Score = element[1] }, new Record { Name = "miT", Score = element[2] }, new Record { Name = "miT", Score = element[3] }, - }; + ]; - long[] expected = { 150, 420 }; + long[] expected = [150, 420]; Assert.Equal(expected, source.GroupBy(e => e.Name, (k, es) => k.Length * es.Sum(e => (long)e.Score), null)); } @@ -616,15 +616,15 @@ public void NullComparerResultSelectorUsed() [Fact] public void GroupingToArray() { - Record[] source = new Record[] - { + Record[] source = + [ new Record{ Name = "Tim", Score = 55 }, new Record{ Name = "Chris", Score = 49 }, new Record{ Name = "Robert", Score = -100 }, new Record{ Name = "Chris", Score = 24 }, new Record{ Name = "Prakash", Score = 9 }, new Record{ Name = "Tim", Score = 25 } - }; + ]; IGrouping[] groupedArray = source.GroupBy(r => r.Name).ToArray(); Assert.Equal(4, groupedArray.Length); @@ -634,15 +634,15 @@ public void GroupingToArray() [Fact] public void GroupingWithElementSelectorToArray() { - Record[] source = new Record[] - { + Record[] source = + [ new Record{ Name = "Tim", Score = 55 }, new Record{ Name = "Chris", Score = 49 }, new Record{ Name = "Robert", Score = -100 }, new Record{ Name = "Chris", Score = 24 }, new Record{ Name = "Prakash", Score = 9 }, new Record{ Name = "Tim", Score = 25 } - }; + ]; IGrouping[] groupedArray = source.GroupBy(r => r.Name, e => e.Score).ToArray(); Assert.Equal(4, groupedArray.Length); @@ -652,15 +652,15 @@ public void GroupingWithElementSelectorToArray() [Fact] public void GroupingWithResultsToArray() { - Record[] source = new Record[] - { + Record[] source = + [ new Record{ Name = "Tim", Score = 55 }, new Record{ Name = "Chris", Score = 49 }, new Record{ Name = "Robert", Score = -100 }, new Record{ Name = "Chris", Score = 24 }, new Record{ Name = "Prakash", Score = 9 }, new Record{ Name = "Tim", Score = 25 } - }; + ]; IEnumerable[] groupedArray = source.GroupBy(r => r.Name, (r, e) => e).ToArray(); Assert.Equal(4, groupedArray.Length); @@ -670,15 +670,15 @@ public void GroupingWithResultsToArray() [Fact] public void GroupingWithElementSelectorAndResultsToArray() { - Record[] source = new Record[] - { + Record[] source = + [ new Record{ Name = "Tim", Score = 55 }, new Record{ Name = "Chris", Score = 49 }, new Record{ Name = "Robert", Score = -100 }, new Record{ Name = "Chris", Score = 24 }, new Record{ Name = "Prakash", Score = 9 }, new Record{ Name = "Tim", Score = 25 } - }; + ]; IEnumerable[] groupedArray = source.GroupBy(r => r.Name, e => e, (r, e) => e).ToArray(); Assert.Equal(4, groupedArray.Length); @@ -688,15 +688,15 @@ public void GroupingWithElementSelectorAndResultsToArray() [Fact] public void GroupingToList() { - Record[] source = new Record[] - { + Record[] source = + [ new Record { Name = "Tim", Score = 55 }, new Record { Name = "Chris", Score = 49 }, new Record { Name = "Robert", Score = -100 }, new Record { Name = "Chris", Score = 24 }, new Record { Name = "Prakash", Score = 9 }, new Record { Name = "Tim", Score = 25 } - }; + ]; List> groupedList = source.GroupBy(r => r.Name).ToList(); Assert.Equal(4, groupedList.Count); @@ -706,15 +706,15 @@ public void GroupingToList() [Fact] public void GroupingWithElementSelectorToList() { - Record[] source = new Record[] - { + Record[] source = + [ new Record { Name = "Tim", Score = 55 }, new Record { Name = "Chris", Score = 49 }, new Record { Name = "Robert", Score = -100 }, new Record { Name = "Chris", Score = 24 }, new Record { Name = "Prakash", Score = 9 }, new Record { Name = "Tim", Score = 25 } - }; + ]; List> groupedList = source.GroupBy(r => r.Name, e => e.Score).ToList(); Assert.Equal(4, groupedList.Count); @@ -724,15 +724,15 @@ public void GroupingWithElementSelectorToList() [Fact] public void GroupingWithResultsToList() { - Record[] source = new Record[] - { + Record[] source = + [ new Record { Name = "Tim", Score = 55 }, new Record { Name = "Chris", Score = 49 }, new Record { Name = "Robert", Score = -100 }, new Record { Name = "Chris", Score = 24 }, new Record { Name = "Prakash", Score = 9 }, new Record { Name = "Tim", Score = 25 } - }; + ]; List> groupedList = source.GroupBy(r => r.Name, (r, e) => e).ToList(); Assert.Equal(4, groupedList.Count); @@ -742,15 +742,15 @@ public void GroupingWithResultsToList() [Fact] public void GroupingWithElementSelectorAndResultsToList() { - Record[] source = new Record[] - { + Record[] source = + [ new Record { Name = "Tim", Score = 55 }, new Record { Name = "Chris", Score = 49 }, new Record { Name = "Robert", Score = -100 }, new Record { Name = "Chris", Score = 24 }, new Record { Name = "Prakash", Score = 9 }, new Record { Name = "Tim", Score = 25 } - }; + ]; List> groupedList = source.GroupBy(r => r.Name, e => e, (r, e) => e).ToList(); Assert.Equal(4, groupedList.Count); @@ -760,15 +760,15 @@ public void GroupingWithElementSelectorAndResultsToList() [Fact] public void GroupingCount() { - Record[] source = new Record[] - { + Record[] source = + [ new Record { Name = "Tim", Score = 55 }, new Record { Name = "Chris", Score = 49 }, new Record { Name = "Robert", Score = -100 }, new Record { Name = "Chris", Score = 24 }, new Record { Name = "Prakash", Score = 9 }, new Record { Name = "Tim", Score = 25 } - }; + ]; Assert.Equal(4, source.GroupBy(r => r.Name).Count()); } @@ -776,15 +776,15 @@ public void GroupingCount() [Fact] public void GroupingWithElementSelectorCount() { - Record[] source = new Record[] - { + Record[] source = + [ new Record { Name = "Tim", Score = 55 }, new Record { Name = "Chris", Score = 49 }, new Record { Name = "Robert", Score = -100 }, new Record { Name = "Chris", Score = 24 }, new Record { Name = "Prakash", Score = 9 }, new Record { Name = "Tim", Score = 25 } - }; + ]; Assert.Equal(4, source.GroupBy(r => r.Name, e => e.Score).Count()); } @@ -792,15 +792,15 @@ public void GroupingWithElementSelectorCount() [Fact] public void GroupingWithResultsCount() { - Record[] source = new Record[] - { + Record[] source = + [ new Record { Name = "Tim", Score = 55 }, new Record { Name = "Chris", Score = 49 }, new Record { Name = "Robert", Score = -100 }, new Record { Name = "Chris", Score = 24 }, new Record { Name = "Prakash", Score = 9 }, new Record { Name = "Tim", Score = 25 } - }; + ]; Assert.Equal(4, source.GroupBy(r => r.Name, (r, e) => e).Count()); } @@ -808,15 +808,15 @@ public void GroupingWithResultsCount() [Fact] public void GroupingWithElementSelectorAndResultsCount() { - Record[] source = new Record[] - { + Record[] source = + [ new Record { Name = "Tim", Score = 55 }, new Record { Name = "Chris", Score = 49 }, new Record { Name = "Robert", Score = -100 }, new Record { Name = "Chris", Score = 24 }, new Record { Name = "Prakash", Score = 9 }, new Record { Name = "Tim", Score = 25 } - }; + ]; Assert.Equal(4, source.GroupBy(r => r.Name, e => e, (r, e) => e).Count()); } @@ -862,7 +862,7 @@ public static void GroupingKeyIsPublic() { // Grouping.Key needs to be public (not explicitly implemented) for the sake of WPF. - object[] objs = { "Foo", 1.0M, "Bar", new { X = "X" }, 2.00M }; + object[] objs = ["Foo", 1.0M, "Bar", new { X = "X" }, 2.00M]; object group = objs.GroupBy(x => x.GetType()).First(); Type grouptype = group.GetType(); diff --git a/tests/System.Linq.Tests/Tests/ZLinq/GroupJoinTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/GroupJoinTests.cs index a3c850ee..4c3cf9f7 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/GroupJoinTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/GroupJoinTests.cs @@ -98,35 +98,35 @@ public static JoinRec createJoinRec(CustomerRec cr, IEnumerable arIE [Fact] public void OuterEmptyInnerNonEmpty() { - CustomerRec[] outer = { }; - OrderRec[] inner = new[] - { + CustomerRec[] outer = []; + OrderRec[] inner = + [ new OrderRec{ orderID = 45321, custID = 98022, total = 50 }, new OrderRec{ orderID = 97865, custID = 32103, total = 25 } - }; + ]; Assert.Empty(outer.GroupJoin(inner, e => e.custID, e => e.custID, createJoinRec)); } [Fact] public void CustomComparer() { - CustomerRec[] outer = new[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Tim", custID = 1234 }, new CustomerRec{ name = "Bob", custID = 9865 }, new CustomerRec{ name = "Robert", custID = 9895 } - }; - AnagramRec[] inner = new[] - { + ]; + AnagramRec[] inner = + [ new AnagramRec{ name = "Robert", orderID = 93483, total = 19 }, new AnagramRec{ name = "miT", orderID = 93489, total = 45 } - }; - JoinRec[] expected = new[] - { - new JoinRec{ name = "Tim", orderID = new int?[]{ 93489 }, total = new int?[]{ 45 } }, - new JoinRec{ name = "Bob", orderID = new int?[]{ }, total = new int?[]{ } }, - new JoinRec{ name = "Robert", orderID = new int?[]{ 93483 }, total = new int?[]{ 19 } } - }; + ]; + JoinRec[] expected = + [ + new JoinRec{ name = "Tim", orderID = [93489], total = [45] }, + new JoinRec{ name = "Bob", orderID = [], total = [] }, + new JoinRec{ name = "Robert", orderID = [93483], total = [19] } + ]; Assert.Equal(expected, outer.GroupJoin(inner, e => e.name, e => e.name, createJoinRec, new AnagramEqualityComparer())); } @@ -135,11 +135,11 @@ public void CustomComparer() public void OuterNull() { CustomerRec[] outer = null; - AnagramRec[] inner = new AnagramRec[] - { + AnagramRec[] inner = + [ new AnagramRec{ name = "Robert", orderID = 93483, total = 19 }, new AnagramRec{ name = "miT", orderID = 93489, total = 45 } - }; + ]; AssertExtensions.Throws(() => outer.GroupJoin(inner, e => e.name, e => e.name, createJoinRec, new AnagramEqualityComparer())); } @@ -147,12 +147,12 @@ public void OuterNull() [Fact] public void InnerNull() { - CustomerRec[] outer = new[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Tim", custID = 1234 }, new CustomerRec{ name = "Bob", custID = 9865 }, new CustomerRec{ name = "Robert", custID = 9895 } - }; + ]; AnagramRec[] inner = null; AssertExtensions.Throws("inner", () => outer.GroupJoin(inner, e => e.name, e => e.name, createJoinRec, new AnagramEqualityComparer())); @@ -161,17 +161,17 @@ public void InnerNull() [Fact] public void OuterKeySelectorNull() { - CustomerRec[] outer = new CustomerRec[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Tim", custID = 1234 }, new CustomerRec{ name = "Bob", custID = 9865 }, new CustomerRec{ name = "Robert", custID = 9895 } - }; - AnagramRec[] inner = new AnagramRec[] - { + ]; + AnagramRec[] inner = + [ new AnagramRec{ name = "Robert", orderID = 93483, total = 19 }, new AnagramRec{ name = "miT", orderID = 93489, total = 45 } - }; + ]; AssertExtensions.Throws("outerKeySelector", () => outer.GroupJoin(inner, null, e => e.name, createJoinRec, new AnagramEqualityComparer())); } @@ -179,17 +179,17 @@ public void OuterKeySelectorNull() [Fact] public void InnerKeySelectorNull() { - CustomerRec[] outer = new CustomerRec[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Tim", custID = 1234 }, new CustomerRec{ name = "Bob", custID = 9865 }, new CustomerRec{ name = "Robert", custID = 9895 } - }; - AnagramRec[] inner = new AnagramRec[] - { + ]; + AnagramRec[] inner = + [ new AnagramRec{ name = "Robert", orderID = 93483, total = 19 }, new AnagramRec{ name = "miT", orderID = 93489, total = 45 } - }; + ]; AssertExtensions.Throws("innerKeySelector", () => outer.GroupJoin(inner, e => e.name, null, createJoinRec, new AnagramEqualityComparer())); } @@ -197,17 +197,17 @@ public void InnerKeySelectorNull() [Fact] public void ResultSelectorNull() { - CustomerRec[] outer = new CustomerRec[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Tim", custID = 1234 }, new CustomerRec{ name = "Bob", custID = 9865 }, new CustomerRec{ name = "Robert", custID = 9895 } - }; - AnagramRec[] inner = new AnagramRec[] - { + ]; + AnagramRec[] inner = + [ new AnagramRec{ name = "Robert", orderID = 93483, total = 19 }, new AnagramRec{ name = "miT", orderID = 93489, total = 45 } - }; + ]; AssertExtensions.Throws("resultSelector", () => outer.GroupJoin(inner, e => e.name, e => e.name, (Func, JoinRec>)null, new AnagramEqualityComparer())); } @@ -216,11 +216,11 @@ public void ResultSelectorNull() public void OuterNullNoComparer() { CustomerRec[] outer = null; - AnagramRec[] inner = new AnagramRec[] - { + AnagramRec[] inner = + [ new AnagramRec{ name = "Robert", orderID = 93483, total = 19 }, new AnagramRec{ name = "miT", orderID = 93489, total = 45 } - }; + ]; AssertExtensions.Throws(() => outer.GroupJoin(inner, e => e.name, e => e.name, createJoinRec)); } @@ -228,12 +228,12 @@ public void OuterNullNoComparer() [Fact] public void InnerNullNoComparer() { - CustomerRec[] outer = new[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Tim", custID = 1234 }, new CustomerRec{ name = "Bob", custID = 9865 }, new CustomerRec{ name = "Robert", custID = 9895 } - }; + ]; AnagramRec[] inner = null; AssertExtensions.Throws("inner", () => outer.GroupJoin(inner, e => e.name, e => e.name, createJoinRec)); @@ -242,17 +242,17 @@ public void InnerNullNoComparer() [Fact] public void OuterKeySelectorNullNoComparer() { - CustomerRec[] outer = new CustomerRec[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Tim", custID = 1234 }, new CustomerRec{ name = "Bob", custID = 9865 }, new CustomerRec{ name = "Robert", custID = 9895 } - }; - AnagramRec[] inner = new AnagramRec[] - { + ]; + AnagramRec[] inner = + [ new AnagramRec{ name = "Robert", orderID = 93483, total = 19 }, new AnagramRec{ name = "miT", orderID = 93489, total = 45 } - }; + ]; AssertExtensions.Throws("outerKeySelector", () => outer.GroupJoin(inner, null, e => e.name, createJoinRec)); } @@ -260,17 +260,17 @@ public void OuterKeySelectorNullNoComparer() [Fact] public void InnerKeySelectorNullNoComparer() { - CustomerRec[] outer = new CustomerRec[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Tim", custID = 1234 }, new CustomerRec{ name = "Bob", custID = 9865 }, new CustomerRec{ name = "Robert", custID = 9895 } - }; - AnagramRec[] inner = new AnagramRec[] - { + ]; + AnagramRec[] inner = + [ new AnagramRec{ name = "Robert", orderID = 93483, total = 19 }, new AnagramRec{ name = "miT", orderID = 93489, total = 45 } - }; + ]; AssertExtensions.Throws("innerKeySelector", () => outer.GroupJoin(inner, e => e.name, null, createJoinRec)); } @@ -278,17 +278,17 @@ public void InnerKeySelectorNullNoComparer() [Fact] public void ResultSelectorNullNoComparer() { - CustomerRec[] outer = new CustomerRec[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Tim", custID = 1234 }, new CustomerRec{ name = "Bob", custID = 9865 }, new CustomerRec{ name = "Robert", custID = 9895 } - }; - AnagramRec[] inner = new AnagramRec[] - { + ]; + AnagramRec[] inner = + [ new AnagramRec{ name = "Robert", orderID = 93483, total = 19 }, new AnagramRec{ name = "miT", orderID = 93489, total = 45 } - }; + ]; AssertExtensions.Throws("resultSelector", () => outer.GroupJoin(inner, e => e.name, e => e.name, (Func, JoinRec>)null)); } @@ -296,9 +296,9 @@ public void ResultSelectorNullNoComparer() [Fact] public void OuterInnerBothSingleNullElement() { - string[] outer = new string[] { null }; - string[] inner = new string[] { null }; - string[] expected = new string[] { null }; + string[] outer = [null]; + string[] inner = [null]; + string[] expected = [null]; Assert.Equal(expected, outer.GroupJoin(inner, e => e, e => e, (x, y) => x, EqualityComparer.Default)); } @@ -306,17 +306,17 @@ public void OuterInnerBothSingleNullElement() [Fact] public void OuterNonEmptyInnerEmpty() { - CustomerRec[] outer = new[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Tim", custID = 43434 }, new CustomerRec{ name = "Bob", custID = 34093 } - }; - OrderRec[] inner = { }; - JoinRec[] expected = new[] - { - new JoinRec{ name = "Tim", orderID = new int?[]{ }, total = new int?[]{ } }, - new JoinRec{ name = "Bob", orderID = new int?[]{ }, total = new int?[]{ } } - }; + ]; + OrderRec[] inner = []; + JoinRec[] expected = + [ + new JoinRec{ name = "Tim", orderID = [], total = [] }, + new JoinRec{ name = "Bob", orderID = [], total = [] } + ]; Assert.Equal(expected, outer.GroupJoin(inner, e => e.custID, e => e.custID, createJoinRec)); } @@ -324,9 +324,9 @@ public void OuterNonEmptyInnerEmpty() [Fact] public void SingleElementEachAndMatches() { - CustomerRec[] outer = new[] { new CustomerRec { name = "Tim", custID = 43434 } }; - OrderRec[] inner = new[] { new OrderRec { orderID = 97865, custID = 43434, total = 25 } }; - JoinRec[] expected = new[] { new JoinRec { name = "Tim", orderID = new int?[] { 97865 }, total = new int?[] { 25 } } }; + CustomerRec[] outer = [new CustomerRec { name = "Tim", custID = 43434 }]; + OrderRec[] inner = [new OrderRec { orderID = 97865, custID = 43434, total = 25 }]; + JoinRec[] expected = [new JoinRec { name = "Tim", orderID = [97865], total = [25] }]; Assert.Equal(expected, outer.GroupJoin(inner, e => e.custID, e => e.custID, createJoinRec)); } @@ -334,9 +334,9 @@ public void SingleElementEachAndMatches() [Fact] public void SingleElementEachAndDoesntMatch() { - CustomerRec[] outer = new[] { new CustomerRec { name = "Tim", custID = 43434 } }; - OrderRec[] inner = new[] { new OrderRec { orderID = 97865, custID = 49434, total = 25 } }; - JoinRec[] expected = new JoinRec[] { new JoinRec { name = "Tim", orderID = new int?[] { }, total = new int?[] { } } }; + CustomerRec[] outer = [new CustomerRec { name = "Tim", custID = 43434 }]; + OrderRec[] inner = [new OrderRec { orderID = 97865, custID = 49434, total = 25 }]; + JoinRec[] expected = [new JoinRec { name = "Tim", orderID = [], total = [] }]; Assert.Equal(expected, outer.GroupJoin(inner, e => e.custID, e => e.custID, createJoinRec)); } @@ -344,21 +344,21 @@ public void SingleElementEachAndDoesntMatch() [Fact] public void SelectorsReturnNull() { - CustomerRec[] outer = new[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Tim", custID = null }, new CustomerRec{ name = "Bob", custID = null } - }; - OrderRec[] inner = new[] - { + ]; + OrderRec[] inner = + [ new OrderRec{ orderID = 97865, custID = null, total = 25 }, new OrderRec{ orderID = 34390, custID = null, total = 19 } - }; - JoinRec[] expected = new[] - { - new JoinRec{ name = "Tim", orderID = new int?[]{ }, total = new int?[]{ } }, - new JoinRec{ name = "Bob", orderID = new int?[]{ }, total = new int?[]{ } } - }; + ]; + JoinRec[] expected = + [ + new JoinRec{ name = "Tim", orderID = [], total = [] }, + new JoinRec{ name = "Bob", orderID = [], total = [] } + ]; Assert.Equal(expected, outer.GroupJoin(inner, e => e.custID, e => e.custID, createJoinRec)); } @@ -366,22 +366,22 @@ public void SelectorsReturnNull() [Fact] public void InnerSameKeyMoreThanOneElementAndMatches() { - CustomerRec[] outer = new[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Tim", custID = 1234 }, new CustomerRec{ name = "Bob", custID = 9865 } - }; - OrderRec[] inner = new[] - { + ]; + OrderRec[] inner = + [ new OrderRec{ orderID = 97865, custID = 1234, total = 25 }, new OrderRec{ orderID = 34390, custID = 1234, total = 19 }, new OrderRec{ orderID = 34390, custID = 9865, total = 19 } - }; - JoinRec[] expected = new[] - { - new JoinRec { name = "Tim", orderID = new int?[]{ 97865, 34390 }, total = new int?[] { 25, 19 } }, - new JoinRec { name = "Bob", orderID = new int?[]{ 34390 }, total = new int?[]{ 19 } } - }; + ]; + JoinRec[] expected = + [ + new JoinRec { name = "Tim", orderID = [97865, 34390], total = [25, 19] }, + new JoinRec { name = "Bob", orderID = [34390], total = [19] } + ]; Assert.Equal(expected, outer.GroupJoin(inner, e => e.custID, e => e.custID, createJoinRec)); } @@ -389,22 +389,22 @@ public void InnerSameKeyMoreThanOneElementAndMatches() [Fact] public void InnerSameKeyMoreThanOneElementAndMatchesRunOnce() { - CustomerRec[] outer = new[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Tim", custID = 1234 }, new CustomerRec{ name = "Bob", custID = 9865 } - }; - OrderRec[] inner = new[] - { + ]; + OrderRec[] inner = + [ new OrderRec{ orderID = 97865, custID = 1234, total = 25 }, new OrderRec{ orderID = 34390, custID = 1234, total = 19 }, new OrderRec{ orderID = 34390, custID = 9865, total = 19 } - }; - JoinRec[] expected = new[] - { - new JoinRec { name = "Tim", orderID = new int?[]{ 97865, 34390 }, total = new int?[] { 25, 19 } }, - new JoinRec { name = "Bob", orderID = new int?[]{ 34390 }, total = new int?[]{ 19 } } - }; + ]; + JoinRec[] expected = + [ + new JoinRec { name = "Tim", orderID = [97865, 34390], total = [25, 19] }, + new JoinRec { name = "Bob", orderID = [34390], total = [19] } + ]; Assert.Equal(expected, outer.RunOnce().GroupJoin(inner.RunOnce(), e => e.custID, e => e.custID, createJoinRec)); } @@ -412,23 +412,23 @@ public void InnerSameKeyMoreThanOneElementAndMatchesRunOnce() [Fact] public void OuterSameKeyMoreThanOneElementAndMatches() { - CustomerRec[] outer = new[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Tim", custID = 1234 }, new CustomerRec{ name = "Bob", custID = 9865 }, new CustomerRec{ name = "Robert", custID = 9865 } - }; - OrderRec[] inner = new[] - { + ]; + OrderRec[] inner = + [ new OrderRec{ orderID = 97865, custID = 1234, total = 25 }, new OrderRec{ orderID = 34390, custID = 9865, total = 19 } - }; - JoinRec[] expected = new[] - { - new JoinRec { name = "Tim", orderID = new int?[]{ 97865 }, total = new int?[]{ 25 } }, - new JoinRec { name = "Bob", orderID = new int?[]{ 34390 }, total = new int?[]{ 19 } }, - new JoinRec { name = "Robert", orderID = new int?[]{ 34390 }, total = new int?[]{ 19 } } - }; + ]; + JoinRec[] expected = + [ + new JoinRec { name = "Tim", orderID = [97865], total = [25] }, + new JoinRec { name = "Bob", orderID = [34390], total = [19] }, + new JoinRec { name = "Robert", orderID = [34390], total = [19] } + ]; Assert.Equal(expected, outer.GroupJoin(inner, e => e.custID, e => e.custID, createJoinRec)); } @@ -436,23 +436,23 @@ public void OuterSameKeyMoreThanOneElementAndMatches() [Fact] public void NoMatches() { - CustomerRec[] outer = new[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Tim", custID = 1234 }, new CustomerRec{ name = "Bob", custID = 9865 }, new CustomerRec{ name = "Robert", custID = 9895 } - }; - OrderRec[] inner = new[] - { + ]; + OrderRec[] inner = + [ new OrderRec{ orderID = 97865, custID = 2334, total = 25 }, new OrderRec{ orderID = 34390, custID = 9065, total = 19 } - }; - JoinRec[] expected = new[] - { - new JoinRec{ name = "Tim", orderID = new int?[]{ }, total = new int?[]{ } }, - new JoinRec{ name = "Bob", orderID = new int?[]{ }, total = new int?[]{ } }, - new JoinRec{ name = "Robert", orderID = new int?[]{ }, total = new int?[]{ } } - }; + ]; + JoinRec[] expected = + [ + new JoinRec{ name = "Tim", orderID = [], total = [] }, + new JoinRec{ name = "Bob", orderID = [], total = [] }, + new JoinRec{ name = "Robert", orderID = [], total = [] } + ]; Assert.Equal(expected, outer.GroupJoin(inner, e => e.custID, e => e.custID, createJoinRec)); } @@ -460,23 +460,23 @@ public void NoMatches() [Fact] public void NullComparer() { - CustomerRec[] outer = new[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Tim", custID = 1234 }, new CustomerRec{ name = "Bob", custID = 9865 }, new CustomerRec{ name = "Robert", custID = 9895 } - }; - AnagramRec[] inner = new[] - { + ]; + AnagramRec[] inner = + [ new AnagramRec{ name = "Robert", orderID = 93483, total = 19 }, new AnagramRec{ name = "miT", orderID = 93489, total = 45 } - }; - JoinRec[] expected = new[] - { - new JoinRec{ name = "Tim", orderID = new int?[]{ }, total = new int?[]{ } }, - new JoinRec{ name = "Bob", orderID = new int?[]{ }, total = new int?[]{ } }, - new JoinRec{ name = "Robert", orderID = new int?[]{ 93483 }, total = new int?[]{ 19 } } - }; + ]; + JoinRec[] expected = + [ + new JoinRec{ name = "Tim", orderID = [], total = [] }, + new JoinRec{ name = "Bob", orderID = [], total = [] }, + new JoinRec{ name = "Robert", orderID = [93483], total = [19] } + ]; Assert.Equal(expected, outer.GroupJoin(inner, e => e.name, e => e.name, createJoinRec, null)); } @@ -484,23 +484,23 @@ public void NullComparer() [Fact] public void NullComparerRunOnce() { - CustomerRec[] outer = new[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Tim", custID = 1234 }, new CustomerRec{ name = "Bob", custID = 9865 }, new CustomerRec{ name = "Robert", custID = 9895 } - }; - AnagramRec[] inner = new[] - { + ]; + AnagramRec[] inner = + [ new AnagramRec{ name = "Robert", orderID = 93483, total = 19 }, new AnagramRec{ name = "miT", orderID = 93489, total = 45 } - }; - JoinRec[] expected = new[] - { - new JoinRec{ name = "Tim", orderID = new int?[]{ }, total = new int?[]{ } }, - new JoinRec{ name = "Bob", orderID = new int?[]{ }, total = new int?[]{ } }, - new JoinRec{ name = "Robert", orderID = new int?[]{ 93483 }, total = new int?[]{ 19 } } - }; + ]; + JoinRec[] expected = + [ + new JoinRec{ name = "Tim", orderID = [], total = [] }, + new JoinRec{ name = "Bob", orderID = [], total = [] }, + new JoinRec{ name = "Robert", orderID = [93483], total = [19] } + ]; Assert.Equal(expected, outer.RunOnce().GroupJoin(inner.RunOnce(), e => e.name, e => e.name, createJoinRec, null)); } diff --git a/tests/System.Linq.Tests/Tests/ZLinq/IntersectTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/IntersectTests.cs index c70b8d38..4c81c8a5 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/IntersectTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/IntersectTests.cs @@ -32,10 +32,10 @@ public void SameResultsRepeatCallsStringQuery() public static IEnumerable Int_TestData() { - yield return new object[] { new int[0], new int[0], new int[0] }; - yield return new object[] { new int[] { -5, 3, -2, 6, 9 }, new int[] { 0, 5, 2, 10, 20 }, new int[0] }; - yield return new object[] { new int[] { 1, 2, 2, 3, 4, 3, 5 }, new int[] { 1, 4, 4, 2, 2, 2 }, new int[] { 1, 2, 4 } }; - yield return new object[] { new int[] { 1, 1, 1, 1, 1, 1 }, new int[] { 1, 1, 1, 1, 1 }, new int[] { 1 } }; + yield return [new int[0], new int[0], new int[0]]; + yield return [new int[] { -5, 3, -2, 6, 9 }, new int[] { 0, 5, 2, 10, 20 }, new int[0]]; + yield return [new int[] { 1, 2, 2, 3, 4, 3, 5 }, new int[] { 1, 4, 4, 2, 2, 2 }, new int[] { 1, 2, 4 }]; + yield return [new int[] { 1, 1, 1, 1, 1, 1 }, new int[] { 1, 1, 1, 1, 1 }, new int[] { 1 }]; } [Theory] @@ -49,12 +49,11 @@ public void Int(IEnumerable first, IEnumerable second, int[] expected) public static IEnumerable String_TestData() { IEqualityComparer defaultComparer = EqualityComparer.Default; - yield return new object[] { new string[1], new string[0], defaultComparer, new string[0] }; - yield return new object[] { new string[] { null, null, string.Empty }, new string[2], defaultComparer, new string[] { null } }; - yield return new object[] { new string[2], new string[0], defaultComparer, new string[0] }; - - yield return new object[] { new string[] { "Tim", "Bob", "Mike", "Robert" }, new string[] { "ekiM", "bBo" }, null, new string[0] }; - yield return new object[] { new string[] { "Tim", "Bob", "Mike", "Robert" }, new string[] { "ekiM", "bBo" }, new AnagramEqualityComparer(), new string[] { "Bob", "Mike" } }; + yield return [new string[1], new string[0], defaultComparer, new string[0]]; + yield return [new string[] { null, null, string.Empty }, new string[2], defaultComparer, new string[] { null }]; + yield return [new string[2], new string[0], defaultComparer, new string[0]]; + yield return [new string[] { "Tim", "Bob", "Mike", "Robert" }, new string[] { "ekiM", "bBo" }, null, new string[0]]; + yield return [new string[] { "Tim", "Bob", "Mike", "Robert" }, new string[] { "ekiM", "bBo" }, new AnagramEqualityComparer(), new string[] { "Bob", "Mike" }]; } [Theory] @@ -70,9 +69,9 @@ public void String(IEnumerable first, IEnumerable second, IEqual public static IEnumerable NullableInt_TestData() { - yield return new object[] { new int?[0], new int?[] { -5, 0, null, 1, 2, 9, 2 }, new int?[0] }; - yield return new object[] { new int?[] { -5, 0, 1, 2, null, 9, 2 }, new int?[0], new int?[0] }; - yield return new object[] { new int?[] { 1, 2, null, 3, 4, 5, 6 }, new int?[] { 6, 7, 7, 7, null, 8, 1 }, new int?[] { 1, null, 6 } }; + yield return [new int?[0], new int?[] { -5, 0, null, 1, 2, 9, 2 }, new int?[0]]; + yield return [new int?[] { -5, 0, 1, 2, null, 9, 2 }, new int?[0], new int?[0]]; + yield return [new int?[] { 1, 2, null, 3, 4, 5, 6 }, new int?[] { 6, 7, 7, 7, null, 8, 1 }, new int?[] { 1, null, 6 }]; } [Theory] @@ -94,7 +93,7 @@ public void NullableIntRunOnce(IEnumerable first, IEnumerable second public void FirstNull_ThrowsArgumentNullException() { string[] first = null; - string[] second = { "ekiM", "bBo" }; + string[] second = ["ekiM", "bBo"]; AssertExtensions.Throws(() => first.Intersect(second)); AssertExtensions.Throws(() => first.Intersect(second, new AnagramEqualityComparer())); @@ -103,7 +102,7 @@ public void FirstNull_ThrowsArgumentNullException() [Fact] public void SecondNull_ThrowsArgumentNullException() { - string[] first = { "Tim", "Bob", "Mike", "Robert" }; + string[] first = ["Tim", "Bob", "Mike", "Robert"]; string[] second = null; AssertExtensions.Throws("second", () => first.Intersect(second)); @@ -123,24 +122,24 @@ public void ForcedToEnumeratorDoesntEnumerate() public void HashSetWithBuiltInComparer_HashSetContainsNotUsed() { IEnumerable input1 = new HashSet(StringComparer.OrdinalIgnoreCase) { "a" }; - IEnumerable input2 = new[] { "A" }; + IEnumerable input2 = ["A"]; - Assert.Equal(Enumerable.Empty(), input1.Intersect(input2)); - Assert.Equal(Enumerable.Empty(), input1.Intersect(input2, null)); - Assert.Equal(Enumerable.Empty(), input1.Intersect(input2, EqualityComparer.Default)); - Assert.Equal(new[] { "a" }, input1.Intersect(input2, StringComparer.OrdinalIgnoreCase)); + Assert.Equal([], input1.Intersect(input2)); + Assert.Equal([], input1.Intersect(input2, null)); + Assert.Equal([], input1.Intersect(input2, EqualityComparer.Default)); + Assert.Equal(["a"], input1.Intersect(input2, StringComparer.OrdinalIgnoreCase)); - Assert.Equal(Enumerable.Empty(), input2.Intersect(input1)); - Assert.Equal(Enumerable.Empty(), input2.Intersect(input1, null)); - Assert.Equal(Enumerable.Empty(), input2.Intersect(input1, EqualityComparer.Default)); - Assert.Equal(new[] { "A" }, input2.Intersect(input1, StringComparer.OrdinalIgnoreCase)); + Assert.Equal([], input2.Intersect(input1)); + Assert.Equal([], input2.Intersect(input1, null)); + Assert.Equal([], input2.Intersect(input1, EqualityComparer.Default)); + Assert.Equal(["A"], input2.Intersect(input1, StringComparer.OrdinalIgnoreCase)); } [Fact] public void IntersectBy_FirstNull_ThrowsArgumentNullException() { string[] first = null; - string[] second = { "bBo", "shriC" }; + string[] second = ["bBo", "shriC"]; AssertExtensions.Throws(() => first.IntersectBy(second, x => x)); AssertExtensions.Throws(() => first.IntersectBy(second, x => x, new AnagramEqualityComparer())); @@ -149,7 +148,7 @@ public void IntersectBy_FirstNull_ThrowsArgumentNullException() [Fact] public void IntersectBy_SecondNull_ThrowsArgumentNullException() { - string[] first = { "Bob", "Tim", "Robert", "Chris" }; + string[] first = ["Bob", "Tim", "Robert", "Chris"]; string[] second = null; AssertExtensions.Throws("second", () => first.IntersectBy(second, x => x)); @@ -159,8 +158,8 @@ public void IntersectBy_SecondNull_ThrowsArgumentNullException() [Fact] public void IntersectBy_KeySelectorNull_ThrowsArgumentNullException() { - string[] first = { "Bob", "Tim", "Robert", "Chris" }; - string[] second = { "bBo", "shriC" }; + string[] first = ["Bob", "Tim", "Robert", "Chris"]; + string[] second = ["bBo", "shriC"]; Func keySelector = null; AssertExtensions.Throws("keySelector", () => first.IntersectBy(second, keySelector)); @@ -195,14 +194,14 @@ public static IEnumerable IntersectBy_TestData() second: Enumerable.Range(10, 10), keySelector: x => x, comparer: null, - expected: Enumerable.Empty()); + expected: []); yield return WrapArgs( first: Enumerable.Repeat(5, 20), - second: Enumerable.Empty(), + second: [], keySelector: x => x, comparer: null, - expected: Enumerable.Empty()); + expected: []); yield return WrapArgs( first: Enumerable.Repeat(5, 20), @@ -212,42 +211,42 @@ public static IEnumerable IntersectBy_TestData() expected: Enumerable.Repeat(5, 1)); yield return WrapArgs( - first: new string[] { "Bob", "Tim", "Robert", "Chris" }, - second: new string[] { "bBo", "shriC" }, + first: ["Bob", "Tim", "Robert", "Chris"], + second: ["bBo", "shriC"], keySelector: x => x, null, - expected: Array.Empty()); + expected: []); yield return WrapArgs( - first: new string[] { "Bob", "Tim", "Robert", "Chris" }, - second: new string[] { "bBo", "shriC" }, + first: ["Bob", "Tim", "Robert", "Chris"], + second: ["bBo", "shriC"], keySelector: x => x, new AnagramEqualityComparer(), - expected: new string[] { "Bob", "Chris" }); + expected: ["Bob", "Chris"]); yield return WrapArgs( first: new (string Name, int Age)[] { ("Tom", 20), ("Dick", 30), ("Harry", 40) }, - second: new int[] { 15, 20, 40 }, + second: [15, 20, 40], keySelector: x => x.Age, comparer: null, expected: new (string Name, int Age)[] { ("Tom", 20), ("Harry", 40) }); yield return WrapArgs( first: new (string Name, int Age)[] { ("Tom", 20), ("Dick", 30), ("Harry", 40) }, - second: new string[] { "moT" }, + second: ["moT"], keySelector: x => x.Name, comparer: null, - expected: Array.Empty<(string Name, int Age)>()); + expected: []); yield return WrapArgs( first: new (string Name, int Age)[] { ("Tom", 20), ("Dick", 30), ("Harry", 40) }, - second: new string[] { "moT" }, + second: ["moT"], keySelector: x => x.Name, comparer: new AnagramEqualityComparer(), expected: new (string Name, int Age)[] { ("Tom", 20) }); object[] WrapArgs(IEnumerable first, IEnumerable second, Func keySelector, IEqualityComparer? comparer, IEnumerable expected) - => new object[] { first, second, keySelector, comparer, expected }; + => [first, second, keySelector, comparer, expected]; } } } diff --git a/tests/System.Linq.Tests/Tests/ZLinq/JoinTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/JoinTests.cs index 91636c4b..03c6ee50 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/JoinTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/JoinTests.cs @@ -49,35 +49,35 @@ public static JoinRec createJoinRec(CustomerRec cr, AnagramRec or) [Fact] public void OuterEmptyInnerNonEmpty() { - CustomerRec[] outer = { }; - OrderRec[] inner = new[] - { + CustomerRec[] outer = []; + OrderRec[] inner = + [ new OrderRec{ orderID = 45321, custID = 98022, total = 50 }, new OrderRec{ orderID = 97865, custID = 32103, total = 25 } - }; + ]; Assert.Empty(outer.Join(inner, e => e.custID, e => e.custID, createJoinRec)); } [Fact] public void FirstOuterMatchesLastInnerLastOuterMatchesFirstInnerSameNumberElements() { - CustomerRec[] outer = new[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Prakash", custID = 98022 }, new CustomerRec{ name = "Tim", custID = 99021 }, new CustomerRec{ name = "Robert", custID = 99022 } - }; - OrderRec[] inner = new[] - { + ]; + OrderRec[] inner = + [ new OrderRec{ orderID = 45321, custID = 99022, total = 50 }, new OrderRec{ orderID = 43421, custID = 29022, total = 20 }, new OrderRec{ orderID = 95421, custID = 98022, total = 9 } - }; - JoinRec[] expected = new[] - { + ]; + JoinRec[] expected = + [ new JoinRec{ name = "Prakash", orderID = 95421, total = 9 }, new JoinRec{ name = "Robert", orderID = 45321, total = 50 } - }; + ]; Assert.Equal(expected, outer.Join(inner, e => e.custID, e => e.custID, createJoinRec)); } @@ -85,18 +85,18 @@ public void FirstOuterMatchesLastInnerLastOuterMatchesFirstInnerSameNumberElemen [Fact] public void NullComparer() { - CustomerRec[] outer = new[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Prakash", custID = 98022 }, new CustomerRec{ name = "Tim", custID = 99021 }, new CustomerRec{ name = "Robert", custID = 99022 } - }; - AnagramRec[] inner = new[] - { + ]; + AnagramRec[] inner = + [ new AnagramRec{ name = "miT", orderID = 43455, total = 10 }, new AnagramRec{ name = "Prakash", orderID = 323232, total = 9 } - }; - JoinRec[] expected = new[] { new JoinRec { name = "Prakash", orderID = 323232, total = 9 } }; + ]; + JoinRec[] expected = [new JoinRec { name = "Prakash", orderID = 323232, total = 9 }]; Assert.Equal(expected, outer.Join(inner, e => e.name, e => e.name, createJoinRec, null)); } @@ -104,22 +104,22 @@ public void NullComparer() [Fact] public void CustomComparer() { - CustomerRec[] outer = new[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Prakash", custID = 98022 }, new CustomerRec{ name = "Tim", custID = 99021 }, new CustomerRec{ name = "Robert", custID = 99022 } - }; - AnagramRec[] inner = new[] - { + ]; + AnagramRec[] inner = + [ new AnagramRec{ name = "miT", orderID = 43455, total = 10 }, new AnagramRec{ name = "Prakash", orderID = 323232, total = 9 } - }; - JoinRec[] expected = new[] - { + ]; + JoinRec[] expected = + [ new JoinRec{ name = "Prakash", orderID = 323232, total = 9 }, new JoinRec{ name = "Tim", orderID = 43455, total = 10 } - }; + ]; Assert.Equal(expected, outer.Join(inner, e => e.name, e => e.name, createJoinRec, new AnagramEqualityComparer())); } @@ -128,11 +128,11 @@ public void CustomComparer() public void OuterNull() { CustomerRec[] outer = null; - AnagramRec[] inner = new[] - { + AnagramRec[] inner = + [ new AnagramRec{ name = "miT", orderID = 43455, total = 10 }, new AnagramRec{ name = "Prakash", orderID = 323232, total = 9 } - }; + ]; AssertExtensions.Throws(() => outer.Join(inner, e => e.name, e => e.name, createJoinRec, new AnagramEqualityComparer())); } @@ -140,12 +140,12 @@ public void OuterNull() [Fact] public void InnerNull() { - CustomerRec[] outer = new[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Prakash", custID = 98022 }, new CustomerRec{ name = "Tim", custID = 99021 }, new CustomerRec{ name = "Robert", custID = 99022 } - }; + ]; AnagramRec[] inner = null; AssertExtensions.Throws("inner", () => outer.Join(inner, e => e.name, e => e.name, createJoinRec, new AnagramEqualityComparer())); @@ -154,17 +154,17 @@ public void InnerNull() [Fact] public void OuterKeySelectorNull() { - CustomerRec[] outer = new[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Prakash", custID = 98022 }, new CustomerRec{ name = "Tim", custID = 99021 }, new CustomerRec{ name = "Robert", custID = 99022 } - }; - AnagramRec[] inner = new[] - { + ]; + AnagramRec[] inner = + [ new AnagramRec{ name = "miT", orderID = 43455, total = 10 }, new AnagramRec{ name = "Prakash", orderID = 323232, total = 9 } - }; + ]; AssertExtensions.Throws("outerKeySelector", () => outer.Join(inner, null, e => e.name, createJoinRec, new AnagramEqualityComparer())); } @@ -172,17 +172,17 @@ public void OuterKeySelectorNull() [Fact] public void InnerKeySelectorNull() { - CustomerRec[] outer = new[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Prakash", custID = 98022 }, new CustomerRec{ name = "Tim", custID = 99021 }, new CustomerRec{ name = "Robert", custID = 99022 } - }; - AnagramRec[] inner = new[] - { + ]; + AnagramRec[] inner = + [ new AnagramRec{ name = "miT", orderID = 43455, total = 10 }, new AnagramRec{ name = "Prakash", orderID = 323232, total = 9 } - }; + ]; AssertExtensions.Throws("innerKeySelector", () => outer.Join(inner, e => e.name, null, createJoinRec, new AnagramEqualityComparer())); } @@ -190,17 +190,17 @@ public void InnerKeySelectorNull() [Fact] public void ResultSelectorNull() { - CustomerRec[] outer = new[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Prakash", custID = 98022 }, new CustomerRec{ name = "Tim", custID = 99021 }, new CustomerRec{ name = "Robert", custID = 99022 } - }; - AnagramRec[] inner = new[] - { + ]; + AnagramRec[] inner = + [ new AnagramRec{ name = "miT", orderID = 43455, total = 10 }, new AnagramRec{ name = "Prakash", orderID = 323232, total = 9 } - }; + ]; AssertExtensions.Throws("resultSelector", () => outer.Join(inner, e => e.name, e => e.name, (Func)null, new AnagramEqualityComparer())); } @@ -209,11 +209,11 @@ public void ResultSelectorNull() public void OuterNullNoComparer() { CustomerRec[] outer = null; - AnagramRec[] inner = new[] - { + AnagramRec[] inner = + [ new AnagramRec{ name = "miT", orderID = 43455, total = 10 }, new AnagramRec{ name = "Prakash", orderID = 323232, total = 9 } - }; + ]; AssertExtensions.Throws(() => outer.Join(inner, e => e.name, e => e.name, createJoinRec)); } @@ -221,12 +221,12 @@ public void OuterNullNoComparer() [Fact] public void InnerNullNoComparer() { - CustomerRec[] outer = new[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Prakash", custID = 98022 }, new CustomerRec{ name = "Tim", custID = 99021 }, new CustomerRec{ name = "Robert", custID = 99022 } - }; + ]; AnagramRec[] inner = null; AssertExtensions.Throws("inner", () => outer.Join(inner, e => e.name, e => e.name, createJoinRec)); @@ -235,17 +235,17 @@ public void InnerNullNoComparer() [Fact] public void OuterKeySelectorNullNoComparer() { - CustomerRec[] outer = new[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Prakash", custID = 98022 }, new CustomerRec{ name = "Tim", custID = 99021 }, new CustomerRec{ name = "Robert", custID = 99022 } - }; - AnagramRec[] inner = new[] - { + ]; + AnagramRec[] inner = + [ new AnagramRec{ name = "miT", orderID = 43455, total = 10 }, new AnagramRec{ name = "Prakash", orderID = 323232, total = 9 } - }; + ]; AssertExtensions.Throws("outerKeySelector", () => outer.Join(inner, null, e => e.name, createJoinRec)); } @@ -253,17 +253,17 @@ public void OuterKeySelectorNullNoComparer() [Fact] public void InnerKeySelectorNullNoComparer() { - CustomerRec[] outer = new[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Prakash", custID = 98022 }, new CustomerRec{ name = "Tim", custID = 99021 }, new CustomerRec{ name = "Robert", custID = 99022 } - }; - AnagramRec[] inner = new[] - { + ]; + AnagramRec[] inner = + [ new AnagramRec{ name = "miT", orderID = 43455, total = 10 }, new AnagramRec{ name = "Prakash", orderID = 323232, total = 9 } - }; + ]; AssertExtensions.Throws("innerKeySelector", () => outer.Join(inner, e => e.name, null, createJoinRec)); } @@ -271,17 +271,17 @@ public void InnerKeySelectorNullNoComparer() [Fact] public void ResultSelectorNullNoComparer() { - CustomerRec[] outer = new[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Prakash", custID = 98022 }, new CustomerRec{ name = "Tim", custID = 99021 }, new CustomerRec{ name = "Robert", custID = 99022 } - }; - AnagramRec[] inner = new[] - { + ]; + AnagramRec[] inner = + [ new AnagramRec{ name = "miT", orderID = 43455, total = 10 }, new AnagramRec{ name = "Prakash", orderID = 323232, total = 9 } - }; + ]; AssertExtensions.Throws("resultSelector", () => outer.Join(inner, e => e.name, e => e.name, (Func)null)); } @@ -289,9 +289,9 @@ public void ResultSelectorNullNoComparer() [Fact] public void SkipsNullElements() { - string[] outer = new[] { null, string.Empty }; - string[] inner = new[] { null, string.Empty }; - string[] expected = new[] { string.Empty }; + string[] outer = [null, string.Empty]; + string[] inner = [null, string.Empty]; + string[] expected = [string.Empty]; Assert.Equal(expected, outer.Join(inner, e => e, e => e, (x, y) => y, EqualityComparer.Default)); } @@ -299,21 +299,21 @@ public void SkipsNullElements() [Fact] public void OuterNonEmptyInnerEmpty() { - CustomerRec[] outer = new[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Tim", custID = 43434 }, new CustomerRec{ name = "Bob", custID = 34093 } - }; - OrderRec[] inner = { }; + ]; + OrderRec[] inner = []; Assert.Empty(outer.Join(inner, e => e.custID, e => e.custID, createJoinRec)); } [Fact] public void SingleElementEachAndMatches() { - CustomerRec[] outer = new[] { new CustomerRec { name = "Prakash", custID = 98022 } }; - OrderRec[] inner = new[] { new OrderRec { orderID = 45321, custID = 98022, total = 50 } }; - JoinRec[] expected = new[] { new JoinRec { name = "Prakash", orderID = 45321, total = 50 } }; + CustomerRec[] outer = [new CustomerRec { name = "Prakash", custID = 98022 }]; + OrderRec[] inner = [new OrderRec { orderID = 45321, custID = 98022, total = 50 }]; + JoinRec[] expected = [new JoinRec { name = "Prakash", orderID = 45321, total = 50 }]; Assert.Equal(expected, outer.Join(inner, e => e.custID, e => e.custID, createJoinRec)); } @@ -321,44 +321,44 @@ public void SingleElementEachAndMatches() [Fact] public void SingleElementEachAndDoesntMatch() { - CustomerRec[] outer = new[] { new CustomerRec { name = "Prakash", custID = 98922 } }; - OrderRec[] inner = new[] { new OrderRec { orderID = 45321, custID = 98022, total = 50 } }; + CustomerRec[] outer = [new CustomerRec { name = "Prakash", custID = 98922 }]; + OrderRec[] inner = [new OrderRec { orderID = 45321, custID = 98022, total = 50 }]; Assert.Empty(outer.Join(inner, e => e.custID, e => e.custID, createJoinRec)); } [Fact] public void SelectorsReturnNull() { - int?[] inner = { null, null, null }; - int?[] outer = { null, null }; + int?[] inner = [null, null]; + int?[] outer = [null, null, null]; Assert.Empty(outer.Join(inner, e => e, e => e, (x, y) => x)); } [Fact] public void InnerSameKeyMoreThanOneElementAndMatches() { - CustomerRec[] outer = new[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Prakash", custID = 98022 }, new CustomerRec{ name = "Tim", custID = 99021 }, new CustomerRec{ name = "Robert", custID = 99022 } - }; - OrderRec[] inner = new[] - { + ]; + OrderRec[] inner = + [ new OrderRec{ orderID = 45321, custID = 98022, total = 50 }, new OrderRec{ orderID = 45421, custID = 98022, total = 10 }, new OrderRec{ orderID = 43421, custID = 99022, total = 20 }, new OrderRec{ orderID = 85421, custID = 98022, total = 18 }, new OrderRec{ orderID = 95421, custID = 99021, total = 9 } - }; - JoinRec[] expected = new[] - { + ]; + JoinRec[] expected = + [ new JoinRec{ name = "Prakash", orderID = 45321, total = 50 }, new JoinRec{ name = "Prakash", orderID = 45421, total = 10 }, new JoinRec{ name = "Prakash", orderID = 85421, total = 18 }, new JoinRec{ name = "Tim", orderID = 95421, total = 9 }, new JoinRec{ name = "Robert", orderID = 43421, total = 20 } - }; + ]; Assert.Equal(expected, outer.Join(inner, e => e.custID, e => e.custID, createJoinRec)); } @@ -366,26 +366,26 @@ public void InnerSameKeyMoreThanOneElementAndMatches() [Fact] public void OuterSameKeyMoreThanOneElementAndMatches() { - CustomerRec[] outer = new[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Prakash", custID = 98022 }, new CustomerRec{ name = "Bob", custID = 99022 }, new CustomerRec{ name = "Tim", custID = 99021 }, new CustomerRec{ name = "Robert", custID = 99022 } - }; - OrderRec[] inner = new[] - { + ]; + OrderRec[] inner = + [ new OrderRec{ orderID = 45321, custID = 98022, total = 50 }, new OrderRec{ orderID = 43421, custID = 99022, total = 20 }, new OrderRec{ orderID = 95421, custID = 99021, total = 9 } - }; - JoinRec[] expected = new[] - { + ]; + JoinRec[] expected = + [ new JoinRec{ name = "Prakash", orderID = 45321, total = 50 }, new JoinRec{ name = "Bob", orderID = 43421, total = 20 }, new JoinRec{ name = "Tim", orderID = 95421, total = 9 }, new JoinRec{ name = "Robert", orderID = 43421, total = 20 } - }; + ]; Assert.Equal(expected, outer.Join(inner, e => e.custID, e => e.custID, createJoinRec)); } @@ -393,19 +393,19 @@ public void OuterSameKeyMoreThanOneElementAndMatches() [Fact] public void NoMatches() { - CustomerRec[] outer = new[] - { + CustomerRec[] outer = + [ new CustomerRec{ name = "Prakash", custID = 98022 }, new CustomerRec{ name = "Bob", custID = 99022 }, new CustomerRec{ name = "Tim", custID = 99021 }, new CustomerRec{ name = "Robert", custID = 99022 } - }; - OrderRec[] inner = new[] - { + ]; + OrderRec[] inner = + [ new OrderRec{ orderID = 45321, custID = 18022, total = 50 }, new OrderRec{ orderID = 43421, custID = 29022, total = 20 }, new OrderRec{ orderID = 95421, custID = 39021, total = 9 } - }; + ]; Assert.Empty(outer.Join(inner, e => e.custID, e => e.custID, createJoinRec)); } diff --git a/tests/System.Linq.Tests/Tests/ZLinq/LastOrDefaultTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/LastOrDefaultTests.cs index 6563ae71..ff88fe96 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/LastOrDefaultTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/LastOrDefaultTests.cs @@ -31,7 +31,7 @@ public void SameResultsRepeatCallsStringQuery() private static void TestEmptyIList() { - T[] source = { }; + T[] source = []; T expected = default(T); Assert.IsAssignableFrom>(source); @@ -41,7 +41,7 @@ private static void TestEmptyIList() private static void TestEmptyIListDefault(T defaultValue) { - T[] source = { }; + T[] source = []; Assert.IsAssignableFrom>(source); @@ -68,7 +68,7 @@ public void EmptyIList() [Fact] public void IListTOneElement() { - int[] source = { 5 }; + int[] source = [5]; int expected = 5; Assert.IsAssignableFrom>(source); @@ -79,7 +79,7 @@ public void IListTOneElement() [Fact] public void IListTOneElementDefault() { - int[] source = { 5 }; + int[] source = [5]; int expected = 5; Assert.IsAssignableFrom>(source); @@ -91,7 +91,7 @@ public void IListTOneElementDefault() [Fact] public void IListTManyElementsLastIsDefault() { - int?[] source = { -10, 2, 4, 3, 0, 2, null }; + int?[] source = [-10, 2, 4, 3, 0, 2, null]; int? expected = null; Assert.IsAssignableFrom>(source); @@ -102,7 +102,7 @@ public void IListTManyElementsLastIsDefault() [Fact] public void IListTManyElementsLastIsNotDefault() { - int?[] source = { -10, 2, 4, 3, 0, 2, null, 19 }; + int?[] source = [-10, 2, 4, 3, 0, 2, null, 19]; int? expected = 19; Assert.IsAssignableFrom>(source); @@ -113,7 +113,7 @@ public void IListTManyElementsLastIsNotDefault() [Fact] public void IListTManyElementsLastHasDefault() { - int?[] source = { -10, 2, 4, 3, 0, 2, null }; + int?[] source = [-10, 2, 4, 3, 0, 2, null]; int? expected = null; Assert.IsAssignableFrom>(source); @@ -124,7 +124,7 @@ public void IListTManyElementsLastHasDefault() [Fact] public void IListTManyElementsLastIsHasDefault() { - int?[] source = { -10, 2, 4, 3, 0, 2, null, 19 }; + int?[] source = [-10, 2, 4, 3, 0, 2, null, 19]; int? expected = 19; Assert.IsAssignableFrom>(source); @@ -181,7 +181,7 @@ public void ManyElementsNotIListT() [Fact] public void EmptySourcePredicate() { - int?[] source = { }; + int?[] source = []; Assert.All(CreateSources(source), source => { @@ -193,7 +193,7 @@ public void EmptySourcePredicate() [Fact] public void OneElementTruePredicate() { - int[] source = { 4 }; + int[] source = [4]; Func predicate = IsEven; int expected = 4; @@ -206,7 +206,7 @@ public void OneElementTruePredicate() [Fact] public void OneElementTruePredicateDefault() { - int[] source = { 4 }; + int[] source = [4]; Func predicate = IsEven; int expected = 4; @@ -219,7 +219,7 @@ public void OneElementTruePredicateDefault() [Fact] public void ManyElementsPredicateFalseForAll() { - int[] source = { 9, 5, 1, 3, 17, 21 }; + int[] source = [9, 5, 1, 3, 17, 21]; Func predicate = IsEven; int expected = default(int); @@ -232,7 +232,7 @@ public void ManyElementsPredicateFalseForAll() [Fact] public void ManyElementsPredicateFalseForAllDefault() { - int[] source = { 9, 5, 1, 3, 17, 21 }; + int[] source = [9, 5, 1, 3, 17, 21]; Func predicate = IsEven; int expected = 5; @@ -245,7 +245,7 @@ public void ManyElementsPredicateFalseForAllDefault() [Fact] public void PredicateTrueOnlyForLast() { - int[] source = { 9, 5, 1, 3, 17, 21, 50 }; + int[] source = [9, 5, 1, 3, 17, 21, 50]; Func predicate = IsEven; int expected = 50; @@ -258,7 +258,7 @@ public void PredicateTrueOnlyForLast() [Fact] public void PredicateTrueForSome() { - int[] source = { 3, 7, 10, 7, 9, 2, 11, 18, 13, 9 }; + int[] source = [3, 7, 10, 7, 9, 2, 11, 18, 13, 9]; Func predicate = IsEven; int expected = 18; @@ -271,7 +271,7 @@ public void PredicateTrueForSome() [Fact] public void PredicateTrueForSomeRunOnce() { - int[] source = { 3, 7, 10, 7, 9, 2, 11, 18, 13, 9 }; + int[] source = [3, 7, 10, 7, 9, 2, 11, 18, 13, 9]; Func predicate = IsEven; int expected = 18; diff --git a/tests/System.Linq.Tests/Tests/ZLinq/LastTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/LastTests.cs index 33764797..8e7f83e8 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/LastTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/LastTests.cs @@ -31,7 +31,7 @@ public void SameResultsRepeatCallsStringQuery() private static void TestEmptyIList() { - T[] source = { }; + T[] source = []; Assert.NotNull(source as IList); @@ -50,7 +50,7 @@ public void EmptyIListT() [Fact] public void IListTOneElement() { - int[] source = { 5 }; + int[] source = [5]; int expected = 5; Assert.NotNull(source as IList); @@ -61,7 +61,7 @@ public void IListTOneElement() [Fact] public void IListTManyElementsLastIsDefault() { - int?[] source = { -10, 2, 4, 3, 0, 2, null }; + int?[] source = [-10, 2, 4, 3, 0, 2, null]; int? expected = null; Assert.IsAssignableFrom>(source); @@ -72,7 +72,7 @@ public void IListTManyElementsLastIsDefault() [Fact] public void IListTManyElementsLastIsNotDefault() { - int?[] source = { -10, 2, 4, 3, 0, 2, null, 19 }; + int?[] source = [-10, 2, 4, 3, 0, 2, null, 19]; int? expected = 19; Assert.IsAssignableFrom>(source); @@ -128,7 +128,7 @@ public void ManyElementsNotIListT() [Fact] public void EmptySourcePredicate() { - int[] source = { }; + int[] source = []; Assert.All(CreateSources(source), source => { @@ -140,7 +140,7 @@ public void EmptySourcePredicate() [Fact] public void OneElementTruePredicate() { - int[] source = { 4 }; + int[] source = [4]; Func predicate = IsEven; int expected = 4; @@ -153,7 +153,7 @@ public void OneElementTruePredicate() [Fact] public void ManyElementsPredicateFalseForAll() { - int[] source = { 9, 5, 1, 3, 17, 21 }; + int[] source = [9, 5, 1, 3, 17, 21]; Func predicate = IsEven; Assert.All(CreateSources(source), source => @@ -165,7 +165,7 @@ public void ManyElementsPredicateFalseForAll() [Fact] public void PredicateTrueOnlyForLast() { - int[] source = { 9, 5, 1, 3, 17, 21, 50 }; + int[] source = [9, 5, 1, 3, 17, 21, 50]; Func predicate = IsEven; int expected = 50; @@ -178,7 +178,7 @@ public void PredicateTrueOnlyForLast() [Fact] public void PredicateTrueForSome() { - int[] source = { 3, 7, 10, 7, 9, 2, 11, 18, 13, 9 }; + int[] source = [3, 7, 10, 7, 9, 2, 11, 18, 13, 9]; Func predicate = IsEven; int expected = 18; @@ -191,7 +191,7 @@ public void PredicateTrueForSome() [Fact] public void PredicateTrueForSomeRunOnce() { - int[] source = { 3, 7, 10, 7, 9, 2, 11, 18, 13, 9 }; + int[] source = [3, 7, 10, 7, 9, 2, 11, 18, 13, 9]; Func predicate = IsEven; int expected = 18; diff --git a/tests/System.Linq.Tests/Tests/ZLinq/LongCountTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/LongCountTests.cs index 8b776a23..ac0b4b73 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/LongCountTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/LongCountTests.cs @@ -30,15 +30,15 @@ public void SameResultsRepeatCallsStringQuery() public static IEnumerable LongCount_TestData() { - yield return new object[] { new int[0], null, 0L }; - yield return new object[] { new int[] { 3 }, null, 1L }; + yield return [new int[0], null, 0L]; + yield return [new int[] { 3 }, null, 1L]; Func isEvenFunc = IsEven; - yield return new object[] { new int[0], isEvenFunc, 0L }; - yield return new object[] { new int[] { 4 }, isEvenFunc, 1L }; - yield return new object[] { new int[] { 5 }, isEvenFunc, 0L }; - yield return new object[] { new int[] { 2, 5, 7, 9, 29, 10 }, isEvenFunc, 2L }; - yield return new object[] { new int[] { 2, 20, 22, 100, 50, 10 }, isEvenFunc, 6L }; + yield return [new int[0], isEvenFunc, 0L]; + yield return [new int[] { 4 }, isEvenFunc, 1L]; + yield return [new int[] { 5 }, isEvenFunc, 0L]; + yield return [new int[] { 2, 5, 7, 9, 29, 10 }, isEvenFunc, 2L]; + yield return [new int[] { 2, 20, 22, 100, 50, 10 }, isEvenFunc, 6L]; } [Theory] @@ -72,7 +72,7 @@ public static void LongCountRunOnce(IEnumerable source, Func pre [Fact] public void NullableArray_IncludesNullValues() { - int?[] data = { -10, 4, 9, null, 11 }; + int?[] data = [-10, 4, 9, null, 11]; Assert.Equal(5, data.LongCount()); } diff --git a/tests/System.Linq.Tests/Tests/ZLinq/MaxTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/MaxTests.cs index b89f5cd1..ea37f922 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/MaxTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/MaxTests.cs @@ -68,17 +68,17 @@ public static IEnumerable Max_Int_TestData() { foreach ((int[] array, long expected) in new[] { - (new[] { 42 }, 42), + ([42], 42), (Enumerable.Range(1, 10).ToArray(), 10), - (new int[] { -100, -15, -50, -10 }, -10), - (new int[] { -16, 0, 50, 100, 1000 }, 1000), + ([-100, -15, -50, -10], -10), + ([-16, 0, 50, 100, 1000], 1000), (new int[] { -16, 0, 50, 100, 1000 }.Concat(Enumerable.Repeat(int.MaxValue, 1)).ToArray(), int.MaxValue), - (new[] { 20 }, 20), + ([20], 20), (Enumerable.Repeat(-2, 5).ToArray(), -2), - (new int[] { 16, 9, 10, 7, 8 }, 16), - (new int[] { 6, 9, 10, 0, 50 }, 50), - (new int[] { -6, 0, -9, 0, -10, 0 }, 0), + ([16, 9, 10, 7, 8], 16), + ([6, 9, 10, 0, 50], 50), + ([-6, 0, -9, 0, -10, 0], 0), }) { yield return new object[] { new TestEnumerable(array), expected }; @@ -98,21 +98,21 @@ public static IEnumerable Max_Long_TestData() { foreach ((long[] array, long expected) in new[] { - (new[] { 42L }, 42L), + ([42L], 42L), (Enumerable.Range(1, 10).Select(i => (long)i).ToArray(), 10L), - (new long[] { -100, -15, -50, -10 }, -10L), - (new long[] { -16, 0, 50, 100, 1000 }, 1000L), + ([-100, -15, -50, -10], -10L), + ([-16, 0, 50, 100, 1000], 1000L), (new long[] { -16, 0, 50, 100, 1000 }.Concat(Enumerable.Repeat(long.MaxValue, 1)).ToArray(), long.MaxValue), - (new[] { int.MaxValue + 10L }, int.MaxValue + 10L), + ([int.MaxValue + 10L], int.MaxValue + 10L), (Enumerable.Repeat(500L, 5).ToArray(), 500L), - (new long[] { 250, 49, 130, 47, 28 }, 250L), - (new long[] { 6, 9, 10, 0, int.MaxValue + 50L }, int.MaxValue + 50L), - (new long[] { 6, 50, 9, 50, 10, 50 }, 50L), + ([250, 49, 130, 47, 28], 250L), + ([6, 9, 10, 0, int.MaxValue + 50L], int.MaxValue + 50L), + ([6, 50, 9, 50, 10, 50], 50L), }) { - yield return new object[] { new TestEnumerable(array), expected }; - yield return new object[] { array, expected }; + yield return [new TestEnumerable(array), expected]; + yield return [array, expected]; } } @@ -144,22 +144,22 @@ public static IEnumerable Max_Float_TestData() { foreach ((float[] array, float expected) in new[] { - (new[] { 42f }, 42f), + ([42f], 42f), (Enumerable.Range(1, 10).Select(i => (float)i).ToArray(), 10f), - (new float[] { -100, -15, -50, -10 }, -10f), - (new float[] { -16, 0, 50, 100, 1000 }, 1000f), + ([-100, -15, -50, -10], -10f), + ([-16, 0, 50, 100, 1000], 1000f), (new float[] { -16, 0, 50, 100, 1000 }.Concat(Enumerable.Repeat(float.MaxValue, 1)).ToArray(), float.MaxValue), - (new[] { 5.5f }, 5.5f), - (new float[] { 112.5f, 4.9f, 30f, 4.7f, 28f }, 112.5f), - (new float[] { 6.8f, 9.4f, -10f, 0f, float.NaN, 53.6f }, 53.6f), - (new float[] { -5.5f, float.PositiveInfinity, 9.9f, float.PositiveInfinity }, float.PositiveInfinity), + ([5.5f], 5.5f), + ([112.5f, 4.9f, 30f, 4.7f, 28f], 112.5f), + ([6.8f, 9.4f, -10f, 0f, float.NaN, 53.6f], 53.6f), + ([-5.5f, float.PositiveInfinity, 9.9f, float.PositiveInfinity], float.PositiveInfinity), (Enumerable.Repeat(float.NaN, 5).ToArray(), float.NaN), - (new float[] { float.NaN, 6.8f, 9.4f, 10f, 0, -5.6f }, 10f), - (new float[] { 6.8f, 9.4f, 10f, 0, -5.6f, float.NaN }, 10f), - (new float[] { float.NaN, float.NegativeInfinity }, float.NegativeInfinity), - (new float[] { float.NegativeInfinity, float.NaN }, float.NegativeInfinity), + ([float.NaN, 6.8f, 9.4f, 10f, 0, -5.6f], 10f), + ([6.8f, 9.4f, 10f, 0, -5.6f, float.NaN], 10f), + ([float.NaN, float.NegativeInfinity], float.NegativeInfinity), + ([float.NegativeInfinity, float.NaN], float.NegativeInfinity), // Normally NaN < anything and anything < NaN returns false // However, this leads to some irksome outcomes in Min and Max. @@ -168,12 +168,12 @@ public static IEnumerable Max_Float_TestData() // ordering where NaN is smaller than every value, including // negative infinity. (Enumerable.Range(1, 10).Select(i => (float)i).Concat(Enumerable.Repeat(float.NaN, 1)).ToArray(), 10f), - (new float[] { -1f, -10, float.NaN, 10, 200, 1000 }, 1000f), - (new float[] { float.MinValue, 3000f, 100, 200, float.NaN, 1000 }, 3000f), + ([-1f, -10, float.NaN, 10, 200, 1000], 1000f), + ([float.MinValue, 3000f, 100, 200, float.NaN, 1000], 3000f), }) { - yield return new object[] { new TestEnumerable(array), expected }; - yield return new object[] { array, expected }; + yield return [new TestEnumerable(array), expected]; + yield return [array, expected]; } } @@ -212,14 +212,14 @@ public void Max_Float_SeveralNaNWithSelector() [Fact] public void Max_NullableFloat_SeveralNaNOrNullWithSelector() { - float?[] source = new float?[] { float.NaN, null, float.NaN, null }; + float?[] source = [float.NaN, null, float.NaN, null]; Assert.True(float.IsNaN(source.Max(i => i).Value)); } [Fact] public void Max_Float_NaNAtStartWithSelector() { - float[] source = { float.NaN, 6.8f, 9.4f, 10f, 0, -5.6f }; + float[] source = [float.NaN, 6.8f, 9.4f, 10f, 0, -5.6f]; Assert.Equal(10f, source.Max(i => i)); } @@ -227,21 +227,21 @@ public static IEnumerable Max_Double_TestData() { foreach ((double[] array, double expected) in new[] { - (new[] { 42.0 }, 42.0), + ([42.0], 42.0), (Enumerable.Range(1, 10).Select(i => (double)i).ToArray(), 10.0), - (new double[] { -100, -15, -50, -10 }, -10.0), - (new double[] { -16, 0, 50, 100, 1000 }, 1000.0), + ([-100, -15, -50, -10], -10.0), + ([-16, 0, 50, 100, 1000], 1000.0), (new double[] { -16, 0, 50, 100, 1000 }.Concat(Enumerable.Repeat(double.MaxValue, 1)).ToArray(), double.MaxValue), (Enumerable.Repeat(5.5, 1).ToArray(), 5.5), (Enumerable.Repeat(double.NaN, 5).ToArray(), double.NaN), - (new double[] { 112.5, 4.9, 30, 4.7, 28 }, 112.5), - (new double[] { 6.8, 9.4, -10, 0, double.NaN, 53.6 }, 53.6), - (new double[] { -5.5, double.PositiveInfinity, 9.9, double.PositiveInfinity }, double.PositiveInfinity), - (new double[] { double.NaN, 6.8, 9.4, 10.5, 0, -5.6 }, 10.5), - (new double[] { 6.8, 9.4, 10.5, 0, -5.6, double.NaN }, 10.5), - (new double[] { double.NaN, double.NegativeInfinity }, double.NegativeInfinity), - (new double[] { double.NegativeInfinity, double.NaN }, double.NegativeInfinity), + ([112.5, 4.9, 30, 4.7, 28], 112.5), + ([6.8, 9.4, -10, 0, double.NaN, 53.6], 53.6), + ([-5.5, double.PositiveInfinity, 9.9, double.PositiveInfinity], double.PositiveInfinity), + ([double.NaN, 6.8, 9.4, 10.5, 0, -5.6], 10.5), + ([6.8, 9.4, 10.5, 0, -5.6, double.NaN], 10.5), + ([double.NaN, double.NegativeInfinity], double.NegativeInfinity), + ([double.NegativeInfinity, double.NaN], double.NegativeInfinity), // Normally NaN < anything and anything < NaN returns false // However, this leads to some irksome outcomes in Min and Max. @@ -250,12 +250,12 @@ public static IEnumerable Max_Double_TestData() // ordering where NaN is smaller than every value, including // negative infinity. (Enumerable.Range(1, 10).Select(i => (double)i).Concat(Enumerable.Repeat(double.NaN, 1)).ToArray(), 10.0), - (new double[] { -1F, -10, double.NaN, 10, 200, 1000 }, 1000.0), - (new double[] { double.MinValue, 3000F, 100, 200, double.NaN, 1000 }, 3000.0), + ([-1F, -10, double.NaN, 10, 200, 1000], 1000.0), + ([double.MinValue, 3000F, 100, 200, double.NaN, 1000], 3000.0), }) { - yield return new object[] { new TestEnumerable(array), expected }; - yield return new object[] { array, expected }; + yield return [new TestEnumerable(array), expected]; + yield return [array, expected]; } } @@ -294,14 +294,14 @@ public void Max_Double_AllNaNWithSelector() [Fact] public void Max_Double_SeveralNaNOrNullWithSelector() { - double?[] source = new double?[] { double.NaN, null, double.NaN, null }; + double?[] source = [double.NaN, null, double.NaN, null]; Assert.True(double.IsNaN(source.Max(i => i).Value)); } [Fact] public void Max_Double_NaNThenNegativeInfinityWithSelector() { - double[] source = { double.NaN, double.NegativeInfinity }; + double[] source = [double.NaN, double.NegativeInfinity]; Assert.True(double.IsNegativeInfinity(source.Max(i => i))); } @@ -309,21 +309,21 @@ public static IEnumerable Max_Decimal_TestData() { foreach ((decimal[] array, decimal expected) in new[] { - (new[] { 42m }, 42m), + ([42m], 42m), (Enumerable.Range(1, 10).Select(i => (decimal)i).ToArray(), 10m), - (new decimal[] { -100, -15, -50, -10 }, -10m), - (new decimal[] { -16, 0, 50, 100, 1000 }, 1000m), + ([-100, -15, -50, -10], -10m), + ([-16, 0, 50, 100, 1000], 1000m), (new decimal[] { -16, 0, 50, 100, 1000 }.Concat(Enumerable.Repeat(decimal.MaxValue, 1)).ToArray(), decimal.MaxValue), - (new decimal[] { 5.5m }, 5.5m), + ([5.5m], 5.5m), (Enumerable.Repeat(-3.4m, 5).ToArray(), -3.4m), - (new decimal[] { 122.5m, 4.9m, 10m, 4.7m, 28m }, 122.5m), - (new decimal[] { 6.8m, 9.4m, 10m, 0m, 0m, decimal.MaxValue }, decimal.MaxValue), - (new decimal[] { -5.5m, 0m, 9.9m, -5.5m, 9.9m }, 9.9m), + ([122.5m, 4.9m, 10m, 4.7m, 28m], 122.5m), + ([6.8m, 9.4m, 10m, 0m, 0m, decimal.MaxValue], decimal.MaxValue), + ([-5.5m, 0m, 9.9m, -5.5m, 9.9m], 9.9m), }) { - yield return new object[] { new TestEnumerable(array), expected }; - yield return new object[] { array, expected }; + yield return [new TestEnumerable(array), expected]; + yield return [array, expected]; } } @@ -355,19 +355,19 @@ public void Max_Decimal_EmptySource_ThrowsInvalidOperationException() public static IEnumerable 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)).ToArray(), int.MaxValue }; - yield return new object[] { Enumerable.Repeat(default(int?), 100), null }; + yield return [Enumerable.Repeat((int?)42, 1), 42]; + yield return [Enumerable.Range(1, 10).Select(i => (int?)i).ToArray(), 10]; + yield return [new int?[] { null, -100, -15, -50, -10 }, -10]; + yield return [new int?[] { null, -16, 0, 50, 100, 1000 }, 1000]; + yield return [new int?[] { null, -16, 0, 50, 100, 1000 }.Concat(Enumerable.Repeat((int?)int.MaxValue, 1)).ToArray(), int.MaxValue]; + yield return [Enumerable.Repeat(default(int?), 100), null]; - yield return new object[] { Enumerable.Empty(), 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 }; + yield return [Enumerable.Empty(), null]; + yield return [Enumerable.Repeat((int?)-20, 1), -20]; + yield return [new int?[] { -6, null, -9, -10, null, -17, -18 }, -6]; + yield return [new int?[] { null, null, null, null, null, -5 }, -5]; + yield return [new int?[] { 6, null, null, 100, 9, 100, 10, 100 }, 100]; + yield return [Enumerable.Repeat(default(int?), 5), null]; } [Theory] @@ -394,19 +394,19 @@ public void Max_NullableInt_NullSource_ThrowsArgumentNullException() public static IEnumerable 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)).ToArray(), long.MaxValue }; - yield return new object[] { Enumerable.Repeat(default(long?), 100), null }; + yield return [Enumerable.Repeat((long?)42, 1), 42L]; + yield return [Enumerable.Range(1, 10).Select(i => (long?)i).ToArray(), 10L]; + yield return [new long?[] { null, -100, -15, -50, -10 }, -10L]; + yield return [new long?[] { null, -16, 0, 50, 100, 1000 }, 1000L]; + yield return [new long?[] { null, -16, 0, 50, 100, 1000 }.Concat(Enumerable.Repeat((long?)long.MaxValue, 1)).ToArray(), long.MaxValue]; + yield return [Enumerable.Repeat(default(long?), 100), null]; - yield return new object[] { Enumerable.Empty(), 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 }; + yield return [Enumerable.Empty(), null]; + yield return [Enumerable.Repeat((long?)long.MaxValue, 1), long.MaxValue]; + yield return [Enumerable.Repeat(default(long?), 5), null]; + yield return [new long?[] { long.MaxValue, null, 9, 10, null, 7, 8 }, long.MaxValue]; + yield return [new long?[] { null, null, null, null, null, -long.MaxValue }, -long.MaxValue]; + yield return [new long?[] { -6, null, null, 0, -9, 0, -10, -30 }, 0L]; } [Theory] @@ -426,28 +426,28 @@ public void Max_NullableLong_NullSource_ThrowsArgumentNullException() public static IEnumerable 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)).ToArray(), float.MaxValue }; - yield return new object[] { Enumerable.Repeat(default(float?), 100), null }; - - yield return new object[] { Enumerable.Empty(), 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 }; + yield return [Enumerable.Repeat((float?)42, 1), 42f]; + yield return [Enumerable.Range(1, 10).Select(i => (float?)i).ToArray(), 10f]; + yield return [new float?[] { null, -100, -15, -50, -10 }, -10f]; + yield return [new float?[] { null, -16, 0, 50, 100, 1000 }, 1000f]; + yield return [new float?[] { null, -16, 0, 50, 100, 1000 }.Concat(Enumerable.Repeat((float?)float.MaxValue, 1)).ToArray(), float.MaxValue]; + yield return [Enumerable.Repeat(default(float?), 100), null]; + + yield return [Enumerable.Empty(), null]; + yield return [Enumerable.Repeat((float?)float.MinValue, 1), float.MinValue]; + yield return [Enumerable.Repeat(default(float?), 5), null]; + yield return [new float?[] { 14.50f, null, float.NaN, 10.98f, null, 7.5f, 8.6f }, 14.50f]; + yield return [new float?[] { null, null, null, null, null, 0f }, 0f]; + yield return [new float?[] { -6.4f, null, null, -0.5f, -9.4f, -0.5f, -10.9f, -0.5f }, -0.5f]; + + yield return [new float?[] { float.NaN, 6.8f, 9.4f, 10f, 0, null, -5.6f }, 10f]; + yield return [new float?[] { 6.8f, 9.4f, 10f, 0, null, -5.6f, float.NaN }, 10f]; + yield return [new float?[] { float.NaN, float.NegativeInfinity }, float.NegativeInfinity]; + yield return [new float?[] { float.NegativeInfinity, float.NaN }, float.NegativeInfinity]; + yield return [Enumerable.Repeat((float?)float.NaN, 3), float.NaN]; + yield return [new float?[] { float.NaN, null, null, null }, float.NaN]; + yield return [new float?[] { null, null, null, float.NaN }, float.NaN]; + yield return [new float?[] { null, float.NaN, null }, float.NaN]; } [Theory] @@ -467,28 +467,28 @@ public void Max_NullableFloat_NullSource_ThrowsArgumentNullException() public static IEnumerable 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)).ToArray(), double.MaxValue }; - yield return new object[] { Enumerable.Repeat(default(double?), 100), null }; - - yield return new object[] { Enumerable.Empty(), 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 }; + yield return [Enumerable.Repeat((double?)42, 1), 42.0]; + yield return [Enumerable.Range(1, 10).Select(i => (double?)i).ToArray(), 10.0]; + yield return [new double?[] { null, -100, -15, -50, -10 }, -10.0]; + yield return [new double?[] { null, -16, 0, 50, 100, 1000 }, 1000.0]; + yield return [new double?[] { null, -16, 0, 50, 100, 1000 }.Concat(Enumerable.Repeat((double?)double.MaxValue, 1)).ToArray(), double.MaxValue]; + yield return [Enumerable.Repeat(default(double?), 100), null]; + + yield return [Enumerable.Empty(), null]; + yield return [Enumerable.Repeat((double?)double.MinValue, 1), double.MinValue]; + yield return [Enumerable.Repeat(default(double?), 5), null]; + yield return [new double?[] { 14.50, null, double.NaN, 10.98, null, 7.5, 8.6 }, 14.50]; + yield return [new double?[] { null, null, null, null, null, 0 }, 0.0]; + yield return [new double?[] { -6.4, null, null, -0.5, -9.4, -0.5, -10.9, -0.5 }, -0.5]; + + yield return [new double?[] { double.NaN, 6.8, 9.4, 10.5, 0, null, -5.6 }, 10.5]; + yield return [new double?[] { 6.8, 9.4, 10.8, 0, null, -5.6, double.NaN }, 10.8]; + yield return [new double?[] { double.NaN, double.NegativeInfinity }, double.NegativeInfinity]; + yield return [new double?[] { double.NegativeInfinity, double.NaN }, double.NegativeInfinity]; + yield return [Enumerable.Repeat((double?)double.NaN, 3), double.NaN]; + yield return [new double?[] { double.NaN, null, null, null }, double.NaN]; + yield return [new double?[] { null, null, null, double.NaN }, double.NaN]; + yield return [new double?[] { null, double.NaN, null }, double.NaN]; } [Theory] @@ -508,19 +508,19 @@ public void Max_NullableDouble_NullSource_ThrowsArgumentNullException() public static IEnumerable 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)).ToArray(), decimal.MaxValue }; - yield return new object[] { Enumerable.Repeat(default(decimal?), 100), null }; + yield return [Enumerable.Repeat((decimal?)42, 1), 42m]; + yield return [Enumerable.Range(1, 10).Select(i => (decimal?)i).ToArray(), 10m]; + yield return [new decimal?[] { null, -100M, -15, -50, -10 }, -10m]; + yield return [new decimal?[] { null, -16M, 0, 50, 100, 1000 }, 1000m]; + yield return [new decimal?[] { null, -16M, 0, 50, 100, 1000 }.Concat(Enumerable.Repeat((decimal?)decimal.MaxValue, 1)).ToArray(), decimal.MaxValue]; + yield return [Enumerable.Repeat(default(decimal?), 100), null]; - yield return new object[] { Enumerable.Empty(), 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 }; + yield return [Enumerable.Empty(), null]; + yield return [Enumerable.Repeat((decimal?)decimal.MaxValue, 1), decimal.MaxValue]; + yield return [Enumerable.Repeat(default(decimal?), 5), null]; + yield return [new decimal?[] { 14.50m, null, null, 10.98m, null, 7.5m, 8.6m }, 14.50m]; + yield return [new decimal?[] { null, null, null, null, null, 0m }, 0m]; + yield return [new decimal?[] { 6.4m, null, null, decimal.MaxValue, 9.4m, decimal.MaxValue, 10.9m, decimal.MaxValue }, decimal.MaxValue]; } [Theory] @@ -540,18 +540,18 @@ public void Max_NullableDecimal_NullSource_ThrowsArgumentNullException() public static IEnumerable Max_DateTime_TestData() { - yield return new object[] { Enumerable.Range(1, 10).Select(i => new DateTime(2000, 1, i)).ToArray(), new DateTime(2000, 1, 10) }; - yield return new object[] { new DateTime[] { new DateTime(2000, 12, 1), new DateTime(2000, 12, 31), new DateTime(2000, 1, 12) }, new DateTime(2000, 12, 31) }; + yield return [Enumerable.Range(1, 10).Select(i => new DateTime(2000, 1, i)).ToArray(), new DateTime(2000, 1, 10)]; + yield return [new DateTime[] { new DateTime(2000, 12, 1), new DateTime(2000, 12, 31), new DateTime(2000, 1, 12) }, new DateTime(2000, 12, 31)]; - DateTime[] threeThousand = new DateTime[] - { + DateTime[] threeThousand = + [ new DateTime(3000, 1, 1), new DateTime(100, 1, 1), new DateTime(200, 1, 1), new DateTime(1000, 1, 1) - }; - yield return new object[] { threeThousand, new DateTime(3000, 1, 1) }; - yield return new object[] { threeThousand.Concat(Enumerable.Repeat(DateTime.MaxValue, 1)).ToArray(), DateTime.MaxValue }; + ]; + yield return [threeThousand, new DateTime(3000, 1, 1)]; + yield return [threeThousand.Concat(Enumerable.Repeat(DateTime.MaxValue, 1)).ToArray(), DateTime.MaxValue]; } [Theory] @@ -580,17 +580,17 @@ public void Max_DateTime_EmptySource_ThrowsInvalidOperationException() public static IEnumerable 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 [Enumerable.Range(1, 10).Select(i => i.ToString()).ToArray(), "9"]; + yield return [new string[] { "Alice", "Bob", "Charlie", "Eve", "Mallory", "Victor", "Trent" }, "Victor"]; + yield return [new string[] { null, "Charlie", null, "Victor", "Trent", null, "Eve", "Alice", "Mallory", "Bob" }, "Victor"]; - yield return new object[] { Enumerable.Empty(), 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 }; + yield return [Enumerable.Empty(), null]; + yield return [Enumerable.Repeat("Hello", 1), "Hello"]; + yield return [Enumerable.Repeat("hi", 5), "hi"]; + yield return [new string[] { "zzz", "aaa", "abcd", "bark", "temp", "cat" }, "zzz"]; + yield return [new string[] { null, null, null, null, "aAa" }, "aAa"]; + yield return [new string[] { "ooo", "ccc", "ccc", "ooo", "ooo", "nnn" }, "ooo"]; + yield return [Enumerable.Repeat(default(string), 5), null]; } [Theory] @@ -885,7 +885,7 @@ public static IEnumerable Max_Generic_TestData() expected: null); yield return WrapArgs( - source: Enumerable.Empty(), + source: [], comparer: Comparer.Create((_, _) => 0), expected: null); @@ -905,17 +905,17 @@ public static IEnumerable Max_Generic_TestData() expected: 0); yield return WrapArgs( - source: new string[] { "Aardvark", "Zyzzyva", "Zebra", "Antelope" }, + source: ["Aardvark", "Zyzzyva", "Zebra", "Antelope"], comparer: null, expected: "Zyzzyva"); yield return WrapArgs( - source: new string[] { "Aardvark", "Zyzzyva", "Zebra", "Antelope" }, + source: ["Aardvark", "Zyzzyva", "Zebra", "Antelope"], comparer: Comparer.Create((x, y) => -x.CompareTo(y)), expected: "Aardvark"); object[] WrapArgs(IEnumerable source, IComparer? comparer, TSource? expected) - => new object[] { source, comparer, expected }; + => [source, comparer, expected]; } [Fact] @@ -931,7 +931,7 @@ public static void MaxBy_Generic_NullSource_ThrowsArgumentNullException() [Fact] public static void MaxBy_Generic_NullKeySelector_ThrowsArgumentNullException() { - IEnumerable source = Enumerable.Empty(); + IEnumerable source = []; Func keySelector = null; AssertExtensions.Throws("keySelector", () => source.MaxBy(keySelector)); @@ -1034,13 +1034,13 @@ public static IEnumerable MaxBy_Generic_TestData() expected: 0); yield return WrapArgs( - source: new string[] { "Aardvark", "Zyzzyva", "Zebra", "Antelope" }, + source: ["Aardvark", "Zyzzyva", "Zebra", "Antelope"], keySelector: x => x, comparer: null, expected: "Zyzzyva"); yield return WrapArgs( - source: new string[] { "Aardvark", "Zyzzyva", "Zebra", "Antelope" }, + source: ["Aardvark", "Zyzzyva", "Zebra", "Antelope"], keySelector: x => x, comparer: Comparer.Create((x, y) => -x.CompareTo(y)), expected: "Aardvark"); @@ -1076,7 +1076,7 @@ public static IEnumerable MaxBy_Generic_TestData() expected: (Name: "Harry", Age: 20)); object[] WrapArgs(IEnumerable source, Func keySelector, IComparer? comparer, TSource? expected) - => new object[] { source, keySelector, comparer, expected }; + => [source, keySelector, comparer, expected]; } } } diff --git a/tests/System.Linq.Tests/Tests/ZLinq/MinTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/MinTests.cs index 7e6b7da8..770ab461 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/MinTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/MinTests.cs @@ -50,22 +50,22 @@ public static IEnumerable Min_Int_TestData() { foreach ((int[] array, long expected) in new[] { - (new[] { 42 }, 42), + ([42], 42), (Enumerable.Range(1, 10).ToArray(), 1), - (new int[] { -1, -10, 10, 200, 1000 }, -10), - (new int[] { 3000, 100, 200, 1000 }, 100), + ([-1, -10, 10, 200, 1000], -10), + ([3000, 100, 200, 1000], 100), (new int[] { 3000, 100, 200, 1000 }.Concat(Enumerable.Repeat(int.MinValue, 1)).ToArray(), int.MinValue), - (new[] { 20 }, 20), + ([20], 20), (Enumerable.Repeat(-2, 5).ToArray(), -2), (Enumerable.Range(1, 10).ToArray(), 1), - (new int[] { 6, 9, 10, 7, 8 }, 6), - (new int[] { 6, 9, 10, 0, -5 }, -5), - (new int[] { 6, 0, 9, 0, 10, 0 }, 0), + ([6, 9, 10, 7, 8], 6), + ([6, 9, 10, 0, -5], -5), + ([6, 0, 9, 0, 10, 0], 0), }) { - yield return new object[] { new TestEnumerable(array), expected }; - yield return new object[] { array, expected }; + yield return [new TestEnumerable(array), expected]; + yield return [array, expected]; } } @@ -99,21 +99,21 @@ public static IEnumerable Min_Long_TestData() { foreach ((long[] array, long expected) in new[] { - (new[] { 42L }, 42L), + ([42L], 42L), (Enumerable.Range(1, 10).Select(i => (long)i).ToArray(), 1L), - (new long[] { -1, -10, 10, 200, 1000 }, -10L), - (new long[] { 3000, 100, 200, 1000 }, 100L), + ([-1, -10, 10, 200, 1000], -10L), + ([3000, 100, 200, 1000], 100L), (new long[] { 3000, 100, 200, 1000 }.Concat(Enumerable.Repeat(long.MinValue, 1)).ToArray(), long.MinValue), - (new[] { int.MaxValue + 10L }, int.MaxValue + 10L), + ([int.MaxValue + 10L], int.MaxValue + 10L), (Enumerable.Repeat(500L, 5).ToArray(), 500L), - (new long[] { -250, 49, 130, 47, 28 }, -250L), - (new long[] { 6, 9, 10, 0, -int.MaxValue - 50L }, -int.MaxValue - 50L), - (new long[] { 6, -5, 9, -5, 10, -5 }, -5), + ([-250, 49, 130, 47, 28], -250L), + ([6, 9, 10, 0, -int.MaxValue - 50L], -int.MaxValue - 50L), + ([6, -5, 9, -5, 10, -5], -5), }) { - yield return new object[] { new TestEnumerable(array), expected }; - yield return new object[] { array, expected }; + yield return [new TestEnumerable(array), expected]; + yield return [array, expected]; } } @@ -147,27 +147,39 @@ public static IEnumerable Min_Float_TestData() { foreach ((float[] array, float expected) in new[] { - (new[] { 42f }, 42f), + ([42f], 42f), (Enumerable.Range(1, 10).Select(i => (float)i).ToArray(), 1f), - (new float[] { -1, -10, 10, 200, 1000 }, -10f), - (new float[] { 3000, 100, 200, 1000 }, 100), + ([-1, -10, 10, 200, 1000], -10f), + ([3000, 100, 200, 1000], 100), (new float[] { 3000, 100, 200, 1000 }.Concat(Enumerable.Repeat(float.MinValue, 1)).ToArray(), float.MinValue), - (new[] { 5.5f }, 5.5f), + ([5.5f], 5.5f), (Enumerable.Repeat(float.NaN, 5).ToArray(), float.NaN), - (new float[] { -2.5f, 4.9f, 130f, 4.7f, 28f }, -2.5f), - (new float[] { 6.8f, 9.4f, 10f, 0, -5.6f }, -5.6f), - (new float[] { -5.5f, float.NegativeInfinity, 9.9f, float.NegativeInfinity }, float.NegativeInfinity), - - (new float[] { float.NaN, 6.8f, 9.4f, 10f, 0, -5.6f }, float.NaN), - (new float[] { 6.8f, 9.4f, 10f, 0, -5.6f, float.NaN }, float.NaN), - (new float[] { float.NaN, float.NegativeInfinity }, float.NaN), - (new float[] { float.NegativeInfinity, float.NaN }, float.NaN), + ([-2.5f, 4.9f, 130f, 4.7f, 28f], -2.5f), + ([6.8f, 9.4f, 10f, 0, -5.6f], -5.6f), + ([-5.5f, float.NegativeInfinity, 9.9f, float.NegativeInfinity], float.NegativeInfinity), + + ([float.NaN, 6.8f, 9.4f, 10f, 0, -5.6f], float.NaN), + ([6.8f, 9.4f, 10f, 0, -5.6f, float.NaN], float.NaN), + ([float.NaN, float.NegativeInfinity], float.NaN), + ([float.NegativeInfinity, float.NaN], float.NaN), + + // Normally NaN < anything is false, as is anything < NaN + // However, this leads to some irksome outcomes in Min and Max. + // If we use those semantics then Min(NaN, 5.0) is NaN, but + // Min(5.0, NaN) is 5.0! To fix this, we impose a total + // ordering where NaN is smaller than every value, including + // negative infinity. + (Enumerable.Range(1, 10).Select(i => (float)i).Concat(Enumerable.Repeat(float.NaN, 1)).ToArray(), float.NaN), + ([-1F, -10, float.NaN, 10, 200, 1000], float.NaN), + ([float.MinValue, 3000F, 100, 200, float.NaN, 1000], float.NaN), }) { - yield return new object[] { new TestEnumerable(array), expected }; - yield return new object[] { array, expected }; + yield return [new TestEnumerable(array), expected]; + yield return [array, expected]; } + + yield return [Enumerable.Repeat(float.NaN, 3).ToArray(), float.NaN]; } [Theory] @@ -180,29 +192,12 @@ public void Min_Float(IEnumerable source, float expected) public static IEnumerable Min_Float_Shortcircuit_TestData() { - foreach ((float[] array, float expected) in new[] - { - // Normally NaN < anything is false, as is anything < NaN - // However, this leads to some irksome outcomes in Min and Max. - // If we use those semantics then Min(NaN, 5.0) is NaN, but - // Min(5.0, NaN) is 5.0! To fix this, we impose a total - // ordering where NaN is smaller than every value, including - // negative infinity. - (Enumerable.Range(1, 10).Select(i => (float)i).Concat(Enumerable.Repeat(float.NaN, 1)).ToArray(), float.NaN), - (new float[] { -1F, -10, float.NaN, 10, 200, 1000 }, float.NaN), - (new float[] { float.MinValue, 3000F, 100, 200, float.NaN, 1000 }, float.NaN), - }) - { - yield return new object[] { new TestEnumerable(array), expected }; - yield return new object[] { array, expected }; - } - // In .NET Core, Enumerable.Min shortcircuits if it finds any float.NaN in the array, // as nothing can be less than float.NaN. See https://github.com/dotnet/corefx/pull/2426. // Without this optimization, we would iterate through int.MaxValue elements, which takes // a long time. - yield return new object[] { Enumerable.Repeat(float.NaN, int.MaxValue), float.NaN }; - yield return new object[] { Enumerable.Repeat(float.NaN, 3).ToArray(), float.NaN }; + yield return [Enumerable.Repeat(float.NaN, int.MaxValue), float.NaN]; + yield return [Enumerable.Repeat(float.NaN, 3).ToArray(), float.NaN]; } [Theory(Skip = SkipReason.Issue0092)] @@ -235,25 +230,35 @@ public static IEnumerable Min_Double_TestData() { foreach ((double[] array, double expected) in new[] { - (new[] { 42.0 }, 42.0), + ([42.0], 42.0), (Enumerable.Range(1, 10).Select(i => (double)i).ToArray(), 1.0 ), - (new double[] { -1, -10, 10, 200, 1000 }, -10.0), - (new double[] { 3000, 100, 200, 1000 }, 100.0), + ([-1, -10, 10, 200, 1000], -10.0), + ([3000, 100, 200, 1000], 100.0), (new double[] { 3000, 100, 200, 1000 }.Concat(Enumerable.Repeat(double.MinValue, 1)).ToArray(), double.MinValue), - (new[] { 5.5 }, 5.5), - (new double[] { -2.5, 4.9, 130, 4.7, 28 }, -2.5), - (new double[] { 6.8, 9.4, 10, 0, -5.6 }, -5.6), - (new double[] { -5.5, double.NegativeInfinity, 9.9, double.NegativeInfinity }, double.NegativeInfinity), + ([5.5], 5.5), + ([-2.5, 4.9, 130, 4.7, 28], -2.5), + ([6.8, 9.4, 10, 0, -5.6], -5.6), + ([-5.5, double.NegativeInfinity, 9.9, double.NegativeInfinity], double.NegativeInfinity), + + ([double.NaN, 6.8, 9.4, 10, 0, -5.6], double.NaN), + ([6.8, 9.4, 10, 0, -5.6, double.NaN], double.NaN), + ([double.NaN, double.NegativeInfinity], double.NaN), + ([double.NegativeInfinity, double.NaN], double.NaN), - (new double[] { double.NaN, 6.8, 9.4, 10, 0, -5.6 }, double.NaN), - (new double[] { 6.8, 9.4, 10, 0, -5.6, double.NaN }, double.NaN), - (new double[] { double.NaN, double.NegativeInfinity }, double.NaN), - (new double[] { double.NegativeInfinity, double.NaN }, double.NaN), + // Normally NaN < anything is false, as is anything < NaN + // However, this leads to some irksome outcomes in Min and Max. + // If we use those semantics then Min(NaN, 5.0) is NaN, but + // Min(5.0, NaN) is 5.0! To fix this, we impose a total + // ordering where NaN is smaller than every value, including + // negative infinity. + (Enumerable.Range(1, 10).Select(i => (double)i).Concat(Enumerable.Repeat(double.NaN, 1)).ToArray(), double.NaN), + ([-1, -10, double.NaN, 10, 200, 1000], double.NaN), + ([double.MinValue, 3000F, 100, 200, double.NaN, 1000], double.NaN), }) { - yield return new object[] { new TestEnumerable(array), expected }; - yield return new object[] { array, expected }; + yield return [new TestEnumerable(array), expected]; + yield return [array, expected]; } } @@ -267,23 +272,6 @@ public void Min_Double(IEnumerable source, double expected) public static IEnumerable Min_Double_Shortcircuit_TestData() { - foreach ((double[] array, double expected) in new[] - { - // Normally NaN < anything is false, as is anything < NaN - // However, this leads to some irksome outcomes in Min and Max. - // If we use those semantics then Min(NaN, 5.0) is NaN, but - // Min(5.0, NaN) is 5.0! To fix this, we impose a total - // ordering where NaN is smaller than every value, including - // negative infinity. - (Enumerable.Range(1, 10).Select(i => (double)i).Concat(Enumerable.Repeat(double.NaN, 1)).ToArray(), double.NaN), - (new double[] { -1, -10, double.NaN, 10, 200, 1000 }, double.NaN), - (new double[] { double.MinValue, 3000F, 100, 200, double.NaN, 1000 }, double.NaN), - }) - { - yield return new object[] { new TestEnumerable(array), expected }; - yield return new object[] { array, expected }; - } - // In .NET Core, Enumerable.Min shortcircuits if it finds any double.NaN in the array, // as nothing can be less than double.NaN. See https://github.com/dotnet/corefx/pull/2426. // Without this optimization, we would iterate through int.MaxValue elements, which takes @@ -320,22 +308,22 @@ public void Min_Double_EmptySource_ThrowsInvalidOperationException() public static IEnumerable Min_Decimal_TestData() { foreach ((decimal[] array, decimal expected) in new[] - { - (new[] { 42m }, 42m), + { + ([42m], 42m), (Enumerable.Range(1, 10).Select(i => (decimal)i).ToArray(), 1m), - (new decimal[] { -1, -10, 10, 200, 1000 }, -10m), - (new decimal[] { 3000, 100, 200, 1000 }, 100m), + ([-1, -10, 10, 200, 1000], -10m), + ([3000, 100, 200, 1000], 100m), (new decimal[] { 3000, 100, 200, 1000 }.Concat(Enumerable.Repeat(decimal.MinValue, 1)).ToArray(), decimal.MinValue), - (new[] { 5.5m }, 5.5m), + ([5.5m], 5.5m), (Enumerable.Repeat(-3.4m, 5).ToArray(), -3.4m), - (new decimal[] { -2.5m, 4.9m, 130m, 4.7m, 28m }, -2.5m), - (new decimal[] { 6.8m, 9.4m, 10m, 0m, 0m, decimal.MinValue }, decimal.MinValue), - (new decimal[] { -5.5m, 0m, 9.9m, -5.5m, 5m }, -5.5m), + ([-2.5m, 4.9m, 130m, 4.7m, 28m], -2.5m), + ([6.8m, 9.4m, 10m, 0m, 0m, decimal.MinValue], decimal.MinValue), + ([-5.5m, 0m, 9.9m, -5.5m, 5m], -5.5m), }) { - yield return new object[] { new TestEnumerable(array), expected }; - yield return new object[] { array, expected }; + yield return [new TestEnumerable(array), expected]; + yield return [array, expected]; } } @@ -367,19 +355,19 @@ public void Min_Decimal_NullSource_ThrowsArgumentNullException() public static IEnumerable 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)).ToArray(), int.MinValue }; - yield return new object[] { Enumerable.Repeat(default(int?), 100), null }; - yield return new object[] { Enumerable.Repeat((int?)42, 1), 42 }; + yield return [Enumerable.Range(1, 10).Select(i => (int?)i).ToArray(), 1]; + yield return [new int?[] { null, -1, -10, 10, 200, 1000 }, -10]; + yield return [new int?[] { null, 3000, 100, 200, 1000 }, 100]; + yield return [new int?[] { null, 3000, 100, 200, 1000 }.Concat(Enumerable.Repeat((int?)int.MinValue, 1)).ToArray(), int.MinValue]; + yield return [Enumerable.Repeat(default(int?), 100), null]; + yield return [Enumerable.Repeat((int?)42, 1), 42]; - yield return new object[] { Enumerable.Empty(), 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 }; + yield return [Enumerable.Empty(), null]; + yield return [Enumerable.Repeat((int?)20, 1), 20]; + yield return [Enumerable.Repeat(default(int?), 5), null]; + yield return [new int?[] { 6, null, 9, 10, null, 7, 8 }, 6]; + yield return [new int?[] { null, null, null, null, null, -5 }, -5]; + yield return [new int?[] { 6, null, null, 0, 9, 0, 10, 0 }, 0]; } [Theory] @@ -404,19 +392,19 @@ public void Min_NullableInt_NullSource_ThrowsArgumentNullException() public static IEnumerable Min_NullableLong_TestData() { - yield return new object[] { Enumerable.Range(1, 10).Select(i => (long?)i).ToArray(), 1L }; - yield return new object[] { new long?[] { null, -1, -10, 10, 200, 1000 }, -10L }; - yield return new object[] { new long?[] { null, 3000, 100, 200, 1000 }, 100L }; - yield return new object[] { new long?[] { null, 3000, 100, 200, 1000 }.Concat(Enumerable.Repeat((long?)long.MinValue, 1)).ToArray(), long.MinValue }; - yield return new object[] { Enumerable.Repeat(default(long?), 100), null }; - yield return new object[] { Enumerable.Repeat((long?)42, 1), 42L }; + yield return [Enumerable.Range(1, 10).Select(i => (long?)i).ToArray(), 1L]; + yield return [new long?[] { null, -1, -10, 10, 200, 1000 }, -10L]; + yield return [new long?[] { null, 3000, 100, 200, 1000 }, 100L]; + yield return [new long?[] { null, 3000, 100, 200, 1000 }.Concat(Enumerable.Repeat((long?)long.MinValue, 1)).ToArray(), long.MinValue]; + yield return [Enumerable.Repeat(default(long?), 100), null]; + yield return [Enumerable.Repeat((long?)42, 1), 42L]; - yield return new object[] { Enumerable.Empty(), 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.MinValue, null, 9, 10, null, 7, 8 }, long.MinValue }; - 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, 0 }, 0L }; + yield return [Enumerable.Empty(), null]; + yield return [Enumerable.Repeat((long?)long.MaxValue, 1), long.MaxValue]; + yield return [Enumerable.Repeat(default(long?), 5), null]; + yield return [new long?[] { long.MinValue, null, 9, 10, null, 7, 8 }, long.MinValue]; + yield return [new long?[] { null, null, null, null, null, -long.MaxValue }, -long.MaxValue]; + yield return [new long?[] { 6, null, null, 0, 9, 0, 10, 0 }, 0L]; } [Theory] @@ -435,27 +423,29 @@ public void Min_NullableLong_NullSource_ThrowsArgumentNullException() public static IEnumerable Min_NullableFloat_TestData() { - yield return new object[] { Enumerable.Range(1, 10).Select(i => (float?)i).ToArray(), 1f }; - yield return new object[] { new float?[] { null, -1, -10, 10, 200, 1000 }, -10f }; - yield return new object[] { new float?[] { null, 3000, 100, 200, 1000 }, 100f }; - yield return new object[] { new float?[] { null, 3000, 100, 200, 1000 }.Concat(Enumerable.Repeat((float?)float.MinValue, 1)).ToArray(), float.MinValue }; - yield return new object[] { Enumerable.Repeat(default(float?), 100), null }; - yield return new object[] { Enumerable.Repeat((float?)42, 1), 42f }; - - yield return new object[] { Enumerable.Empty(), null }; - yield return new object[] { Enumerable.Repeat((float?)float.MinValue, 1), float.MinValue }; - yield return new object[] { Enumerable.Repeat(default(float?), 100), null }; - yield return new object[] { new float?[] { -4.50f, null, 10.98f, null, 7.5f, 8.6f }, -4.5f }; - 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 }, float.NaN }; - yield return new object[] { new float?[] { 6.8f, 9.4f, 10f, 0, null, -5.6f, float.NaN }, float.NaN }; - yield return new object[] { new float?[] { float.NaN, float.NegativeInfinity }, float.NaN }; - yield return new object[] { new float?[] { float.NegativeInfinity, float.NaN }, 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 }; + yield return [Enumerable.Range(1, 10).Select(i => (float?)i).ToArray(), 1f]; + yield return [new float?[] { null, -1, -10, 10, 200, 1000 }, -10f]; + yield return [new float?[] { null, 3000, 100, 200, 1000 }, 100f]; + yield return [new float?[] { null, 3000, 100, 200, 1000 }.Concat(Enumerable.Repeat((float?)float.MinValue, 1)).ToArray(), float.MinValue]; + yield return [Enumerable.Repeat(default(float?), 100), null]; + yield return [Enumerable.Repeat((float?)42, 1), 42f]; + + yield return [Enumerable.Empty(), null]; + yield return [Enumerable.Repeat((float?)float.MinValue, 1), float.MinValue]; + yield return [Enumerable.Repeat(default(float?), 100), null]; + yield return [new float?[] { -4.50f, null, 10.98f, null, 7.5f, 8.6f }, -4.5f]; + yield return [new float?[] { null, null, null, null, null, 0f }, 0f]; + yield return [new float?[] { 6.4f, null, null, -0.5f, 9.4f, -0.5f, 10.9f, -0.5f }, -0.5f]; + + yield return [new float?[] { float.NaN, 6.8f, 9.4f, 10f, 0, null, -5.6f }, float.NaN]; + yield return [new float?[] { 6.8f, 9.4f, 10f, 0, null, -5.6f, float.NaN }, float.NaN]; + yield return [new float?[] { float.NaN, float.NegativeInfinity }, float.NaN]; + yield return [new float?[] { float.NegativeInfinity, float.NaN }, float.NaN]; + yield return [new float?[] { float.NaN, null, null, null }, float.NaN]; + yield return [new float?[] { null, null, null, float.NaN }, float.NaN]; + yield return [new float?[] { null, float.NaN, null }, float.NaN]; + + yield return [Enumerable.Repeat((float?)float.NaN, 3), float.NaN]; } [Theory] @@ -473,8 +463,8 @@ public static IEnumerable Min_NullableFloat_Shortcircuit_TestData() // as nothing can be less than float.NaN. See https://github.com/dotnet/corefx/pull/2426. // Without this optimization, we would iterate through int.MaxValue elements, which takes // a long time. - yield return new object[] { Enumerable.Repeat((float?)float.NaN, int.MaxValue), float.NaN }; - yield return new object[] { Enumerable.Repeat((float?)float.NaN, 3), float.NaN }; + yield return [Enumerable.Repeat((float?)float.NaN, int.MaxValue), float.NaN]; + yield return [Enumerable.Repeat((float?)float.NaN, 3), float.NaN]; } [Theory(Skip = SkipReason.Issue0092)] @@ -494,27 +484,29 @@ public void Min_NullableFloat_NullSource_ThrowsArgumentNullException() public static IEnumerable Min_NullableDouble_TestData() { - yield return new object[] { Enumerable.Range(1, 10).Select(i => (double?)i).ToArray(), 1.0 }; - yield return new object[] { new double?[] { null, -1, -10, 10, 200, 1000 }, -10.0 }; - yield return new object[] { new double?[] { null, 3000, 100, 200, 1000 }, 100.0 }; - yield return new object[] { new double?[] { null, 3000, 100, 200, 1000 }.Concat(Enumerable.Repeat((double?)double.MinValue, 1)).ToArray(), double.MinValue }; - yield return new object[] { Enumerable.Repeat(default(double?), 100), null }; - yield return new object[] { Enumerable.Repeat((double?)42, 1), 42.0 }; - - yield return new object[] { Enumerable.Empty(), 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?[] { -4.50, null, 10.98, null, 7.5, 8.6 }, -4.5 }; - 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.0, 0.0, null, -5.6 }, double.NaN }; - yield return new object[] { new double?[] { 6.8, 9.4, 10, 0.0, null, -5.6f, double.NaN }, double.NaN }; - yield return new object[] { new double?[] { double.NaN, double.NegativeInfinity }, double.NaN }; - yield return new object[] { new double?[] { double.NegativeInfinity, double.NaN }, 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 }; + yield return [Enumerable.Range(1, 10).Select(i => (double?)i).ToArray(), 1.0]; + yield return [new double?[] { null, -1, -10, 10, 200, 1000 }, -10.0]; + yield return [new double?[] { null, 3000, 100, 200, 1000 }, 100.0]; + yield return [new double?[] { null, 3000, 100, 200, 1000 }.Concat(Enumerable.Repeat((double?)double.MinValue, 1)).ToArray(), double.MinValue]; + yield return [Enumerable.Repeat(default(double?), 100), null]; + yield return [Enumerable.Repeat((double?)42, 1), 42.0]; + + yield return [Enumerable.Empty(), null]; + yield return [Enumerable.Repeat((double?)double.MinValue, 1), double.MinValue]; + yield return [Enumerable.Repeat(default(double?), 5), null]; + yield return [new double?[] { -4.50, null, 10.98, null, 7.5, 8.6 }, -4.5]; + yield return [new double?[] { null, null, null, null, null, 0 }, 0.0]; + yield return [new double?[] { 6.4, null, null, -0.5, 9.4, -0.5, 10.9, -0.5 }, -0.5]; + + yield return [new double?[] { double.NaN, 6.8, 9.4, 10.0, 0.0, null, -5.6 }, double.NaN]; + yield return [new double?[] { 6.8, 9.4, 10, 0.0, null, -5.6f, double.NaN }, double.NaN]; + yield return [new double?[] { double.NaN, double.NegativeInfinity }, double.NaN]; + yield return [new double?[] { double.NegativeInfinity, double.NaN }, double.NaN]; + yield return [new double?[] { double.NaN, null, null, null }, double.NaN]; + yield return [new double?[] { null, null, null, double.NaN }, double.NaN]; + yield return [new double?[] { null, double.NaN, null }, double.NaN]; + + yield return [Enumerable.Repeat((double?)double.NaN, 3), double.NaN]; } [Theory] @@ -531,8 +523,8 @@ public static IEnumerable Min_NullableDouble_ShortCircuit_TestData() // as nothing can be less than double.NaN. See https://github.com/dotnet/corefx/pull/2426. // Without this optimization, we would iterate through int.MaxValue elements, which takes // a long time. - yield return new object[] { Enumerable.Repeat((double?)double.NaN, int.MaxValue), double.NaN }; - yield return new object[] { Enumerable.Repeat((double?)double.NaN, 3), double.NaN }; + yield return [Enumerable.Repeat((double?)double.NaN, int.MaxValue), double.NaN]; + yield return [Enumerable.Repeat((double?)double.NaN, 3), double.NaN]; } [Theory(Skip = SkipReason.Issue0092)] @@ -551,19 +543,19 @@ public void Min_NullableDouble_NullSource_ThrowsArgumentNullException() public static IEnumerable Min_NullableDecimal_TestData() { - yield return new object[] { Enumerable.Range(1, 10).Select(i => (decimal?)i).ToArray(), 1m }; - yield return new object[] { new decimal?[] { null, -1, -10, 10, 200, 1000 }, -10m }; - yield return new object[] { new decimal?[] { null, 3000, 100, 200, 1000 }, 100m }; - yield return new object[] { new decimal?[] { null, 3000, 100, 200, 1000 }.Concat(Enumerable.Repeat((decimal?)decimal.MinValue, 1)).ToArray(), decimal.MinValue }; - yield return new object[] { Enumerable.Repeat(default(decimal?), 100), null }; - yield return new object[] { Enumerable.Repeat((decimal?)42, 1), 42m }; + yield return [Enumerable.Range(1, 10).Select(i => (decimal?)i).ToArray(), 1m]; + yield return [new decimal?[] { null, -1, -10, 10, 200, 1000 }, -10m]; + yield return [new decimal?[] { null, 3000, 100, 200, 1000 }, 100m]; + yield return [new decimal?[] { null, 3000, 100, 200, 1000 }.Concat(Enumerable.Repeat((decimal?)decimal.MinValue, 1)).ToArray(), decimal.MinValue]; + yield return [Enumerable.Repeat(default(decimal?), 100), null]; + yield return [Enumerable.Repeat((decimal?)42, 1), 42m]; - yield return new object[] { Enumerable.Empty(), 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?[] { -4.50m, null, null, 10.98m, null, 7.5m, 8.6m }, -4.5m }; - yield return new object[] { new decimal?[] { null, null, null, null, null, 0m }, 0m }; - yield return new object[] { new decimal?[] { 6.4m, null, null, decimal.MinValue, 9.4m, decimal.MinValue, 10.9m, decimal.MinValue }, decimal.MinValue }; + yield return [Enumerable.Empty(), null]; + yield return [Enumerable.Repeat((decimal?)decimal.MaxValue, 1), decimal.MaxValue]; + yield return [Enumerable.Repeat(default(decimal?), 5), null]; + yield return [new decimal?[] { -4.50m, null, null, 10.98m, null, 7.5m, 8.6m }, -4.5m]; + yield return [new decimal?[] { null, null, null, null, null, 0m }, 0m]; + yield return [new decimal?[] { 6.4m, null, null, decimal.MinValue, 9.4m, decimal.MinValue, 10.9m, decimal.MinValue }, decimal.MinValue]; } [Theory] @@ -586,13 +578,13 @@ public static IEnumerable Min_DateTime_TestData() yield return new object[] { Enumerable.Range(1, 10).Select(i => new DateTime(2000, 1, i)).ToArray(), new DateTime(2000, 1, 1) }; yield return new object[] { new DateTime[] { new DateTime(2000, 12, 1), new DateTime(2000, 1, 1), new DateTime(2000, 1, 12) }, new DateTime(2000, 1, 1) }; - DateTime[] hundred = new DateTime[] - { + DateTime[] hundred = + [ new DateTime(3000, 1, 1), new DateTime(100, 1, 1), new DateTime(200, 1, 1), new DateTime(1000, 1, 1) - }; + ]; yield return new object[] { hundred, new DateTime(100, 1, 1) }; yield return new object[] { hundred.Concat(Enumerable.Repeat(DateTime.MinValue, 1).ToArray()).ToArray(), DateTime.MinValue }; } @@ -625,17 +617,17 @@ public void Min_DateTime_EmptySource_ThrowsInvalidOperationException() public static IEnumerable Min_String_TestData() { - yield return new object[] { Enumerable.Range(1, 10).Select(i => i.ToString()).ToArray(), "1" }; - yield return new object[] { new string[] { "Alice", "Bob", "Charlie", "Eve", "Mallory", "Trent", "Victor" }, "Alice" }; - yield return new object[] { new string[] { null, "Charlie", null, "Victor", "Trent", null, "Eve", "Alice", "Mallory", "Bob" }, "Alice" }; + yield return [Enumerable.Range(1, 10).Select(i => i.ToString()).ToArray(), "1"]; + yield return [new string[] { "Alice", "Bob", "Charlie", "Eve", "Mallory", "Trent", "Victor" }, "Alice"]; + yield return [new string[] { null, "Charlie", null, "Victor", "Trent", null, "Eve", "Alice", "Mallory", "Bob" }, "Alice"]; - yield return new object[] { Enumerable.Empty(), 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[] { "aaa", "abcd", "bark", "temp", "cat" }, "aaa" }; - yield return new object[] { new string[] { null, null, null, null, "aAa" }, "aAa" }; - yield return new object[] { new string[] { "ooo", "www", "www", "ooo", "ooo", "ppp" }, "ooo" }; - yield return new object[] { Enumerable.Repeat(default(string), 5), null }; + yield return [Enumerable.Empty(), null]; + yield return [Enumerable.Repeat("Hello", 1), "Hello"]; + yield return [Enumerable.Repeat("hi", 5), "hi"]; + yield return [new string[] { "aaa", "abcd", "bark", "temp", "cat" }, "aaa"]; + yield return [new string[] { null, null, null, null, "aAa" }, "aAa"]; + yield return [new string[] { "ooo", "www", "www", "ooo", "ooo", "ppp" }, "ooo"]; + yield return [Enumerable.Repeat(default(string), 5), null]; } [Theory] @@ -925,7 +917,7 @@ public static IEnumerable Min_Generic_TestData() expected: null); yield return WrapArgs( - source: Enumerable.Empty(), + source: [], comparer: Comparer.Create((_, _) => 0), expected: null); @@ -945,17 +937,17 @@ public static IEnumerable Min_Generic_TestData() expected: 0); yield return WrapArgs( - source: new string[] { "Aardvark", "Zyzzyva", "Zebra", "Antelope" }, + source: ["Aardvark", "Zyzzyva", "Zebra", "Antelope"], comparer: null, expected: "Aardvark"); yield return WrapArgs( - source: new string[] { "Aardvark", "Zyzzyva", "Zebra", "Antelope" }, + source: ["Aardvark", "Zyzzyva", "Zebra", "Antelope"], comparer: Comparer.Create((x, y) => -x.CompareTo(y)), expected: "Zyzzyva"); object[] WrapArgs(IEnumerable source, IComparer? comparer, TSource? expected) - => new object[] { source, comparer, expected }; + => [source, comparer, expected]; } [Fact] @@ -971,7 +963,7 @@ public static void MinBy_Generic_NullSource_ThrowsArgumentNullException() [Fact] public static void MinBy_Generic_NullKeySelector_ThrowsArgumentNullException() { - IEnumerable source = Enumerable.Empty(); + IEnumerable source = []; Func keySelector = null; AssertExtensions.Throws("keySelector", () => source.MinBy(keySelector)); @@ -1074,13 +1066,13 @@ public static IEnumerable MinBy_Generic_TestData() expected: 0); yield return WrapArgs( - source: new string[] { "Aardvark", "Zyzzyva", "Zebra", "Antelope" }, + source: ["Aardvark", "Zyzzyva", "Zebra", "Antelope"], keySelector: x => x, comparer: null, expected: "Aardvark"); yield return WrapArgs( - source: new string[] { "Aardvark", "Zyzzyva", "Zebra", "Antelope" }, + source: ["Aardvark", "Zyzzyva", "Zebra", "Antelope"], keySelector: x => x, comparer: Comparer.Create((x, y) => -x.CompareTo(y)), expected: "Zyzzyva"); @@ -1116,7 +1108,7 @@ public static IEnumerable MinBy_Generic_TestData() expected: (Name: "Harry", Age: 20)); object[] WrapArgs(IEnumerable source, Func keySelector, IComparer? comparer, TSource? expected) - => new object[] { source, keySelector, comparer, expected }; + => [source, keySelector, comparer, expected]; } } } diff --git a/tests/System.Linq.Tests/Tests/ZLinq/OfTypeTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/OfTypeTests.cs index 460f4c4e..d3a738f0 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/OfTypeTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/OfTypeTests.cs @@ -31,14 +31,14 @@ where string.IsNullOrEmpty(x) [Fact] public void EmptySource() { - object[] source = { }; + object[] source = []; Assert.Empty(source.OfType()); } [Fact] public void LongSequenceFromIntSource() { - int[] source = { 99, 45, 81 }; + int[] source = [99, 45, 81]; Assert.Empty(source.OfType()); } @@ -46,15 +46,15 @@ public void LongSequenceFromIntSource() [Fact] public void HeterogenousSourceNoAppropriateElements() { - object[] source = { "Hello", 3.5, "Test" }; + object[] source = ["Hello", 3.5, "Test"]; Assert.Empty(source.OfType()); } [Fact] public void HeterogenousSourceOnlyFirstOfType() { - object[] source = { 10, "Hello", 3.5, "Test" }; - int[] expected = { 10 }; + object[] source = [10, "Hello", 3.5, "Test"]; + int[] expected = [10]; Assert.Equal(expected, source.OfType()); } @@ -62,8 +62,8 @@ public void HeterogenousSourceOnlyFirstOfType() [Fact] public void AllElementsOfNullableTypeNullsSkipped() { - object[] source = { 10, -4, null, null, 4, 9 }; - int?[] expected = { 10, -4, 4, 9 }; + object[] source = [10, -4, null, null, 4, 9]; + int?[] expected = [10, -4, 4, 9]; Assert.Equal(expected, source.OfType()); } @@ -71,8 +71,8 @@ public void AllElementsOfNullableTypeNullsSkipped() [Fact] public void HeterogenousSourceSomeOfType() { - object[] source = { 3.5m, -4, "Test", "Check", 4, 8.0, 10.5, 9 }; - int[] expected = { -4, 4, 9 }; + object[] source = [3.5m, -4, "Test", "Check", 4, 8.0, 10.5, 9]; + int[] expected = [-4, 4, 9]; Assert.Equal(expected, source.OfType()); } @@ -80,8 +80,8 @@ public void HeterogenousSourceSomeOfType() [Fact] public void RunOnce() { - object[] source = { 3.5m, -4, "Test", "Check", 4, 8.0, 10.5, 9 }; - int[] expected = { -4, 4, 9 }; + object[] source = [3.5m, -4, "Test", "Check", 4, 8.0, 10.5, 9]; + int[] expected = [-4, 4, 9]; Assert.Equal(expected, source.RunOnce().OfType()); } @@ -89,8 +89,8 @@ public void RunOnce() [Fact] public void IntFromNullableInt() { - int[] source = { -4, 4, 9 }; - int?[] expected = { -4, 4, 9 }; + int[] source = [-4, 4, 9]; + int?[] expected = [-4, 4, 9]; Assert.Equal(expected, source.OfType()); } @@ -98,8 +98,8 @@ public void IntFromNullableInt() [Fact] public void IntFromNullableIntWithNulls() { - int?[] source = { null, -4, 4, null, 9 }; - int[] expected = { -4, 4, 9 }; + int?[] source = [null, -4, 4, null, 9]; + int[] expected = [-4, 4, 9]; Assert.Equal(expected, source.OfType()); } @@ -107,14 +107,14 @@ public void IntFromNullableIntWithNulls() [Fact] public void NullableDecimalFromString() { - string[] source = { "Test1", "Test2", "Test9" }; + string[] source = ["Test1", "Test2", "Test9"]; Assert.Empty(source.OfType()); } [Fact] public void LongFromDouble() { - long[] source = { 99L, 45L, 81L }; + long[] source = [99L, 45L, 81L]; Assert.Empty(source.OfType()); } @@ -161,16 +161,16 @@ public void ReferenceType_ReturnsNewEnumerable() [Fact] public void ToArray() { - IEnumerable source = new object[] { 1, 2, 3, 4, 5 }; - Assert.Equal(new int[] { 1, 2, 3, 4, 5 }, source.OfType().ToArray()); + IEnumerable source = [1, 2, 3, 4, 5]; + Assert.Equal([1, 2, 3, 4, 5], source.OfType().ToArray()); Assert.Empty(source.OfType().ToArray()); } [Fact] public void ToList() { - IEnumerable source = new object[] { 1, 2, 3, 4, 5 }; - Assert.Equal(new int[] { 1, 2, 3, 4, 5 }, source.OfType().ToList()); + IEnumerable source = [1, 2, 3, 4, 5]; + Assert.Equal([1, 2, 3, 4, 5], source.OfType().ToList()); Assert.Empty(source.OfType().ToList()); } @@ -192,7 +192,7 @@ public void Count() [Fact] public void First_Last_ElementAt() { - IEnumerable source = new object[] { 1, 2, 3, 4, 5 }; + IEnumerable source = [1, 2, 3, 4, 5]; Assert.Equal(1, source.OfType().First()); Assert.Equal(0, source.OfType().FirstOrDefault()); @@ -207,10 +207,10 @@ public void First_Last_ElementAt() [Fact] public void OfTypeSelect() { - IEnumerable objects = new object[] { "1", null, "22", null, 3, 4, "55555" }; - Assert.Equal(new int[] { 1, 2, 5 }, objects.OfType().Select(s => s.Length)); + IEnumerable objects = ["1", null, "22", null, 3, 4, "55555"]; + Assert.Equal([1, 2, 5], objects.OfType().Select(s => s.Length)); - Assert.Equal(new int[] { 1, 2, 3, 4, 5 }, new int[] { 1, 2, 3, 4, 5 }.OfType().Select(o => (int)o)); + Assert.Equal([1, 2, 3, 4, 5], new int[] { 1, 2, 3, 4, 5 }.OfType().Select(o => (int)o)); } [Fact] diff --git a/tests/System.Linq.Tests/Tests/ZLinq/OrderByDescendingTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/OrderByDescendingTests.cs index a0d91a1b..56d41914 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/OrderByDescendingTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/OrderByDescendingTests.cs @@ -33,15 +33,15 @@ public void SameResultsRepeatCallsStringQuery() [Fact] public void SourceEmpty() { - int[] source = { }; + int[] source = []; Assert.Empty(source.OrderByDescending(e => e)); } [Fact] public void KeySelectorReturnsNull() { - int?[] source = { null, null, null }; - int?[] expected = { null, null, null }; + int?[] source = [null, null, null]; + int?[] expected = [null, null, null]; Assert.Equal(expected, source.OrderByDescending(e => e)); } @@ -49,8 +49,8 @@ public void KeySelectorReturnsNull() [Fact] public void ElementsAllSameKey() { - int?[] source = { 9, 9, 9, 9, 9, 9 }; - int?[] expected = { 9, 9, 9, 9, 9, 9 }; + int?[] source = [9, 9, 9, 9, 9, 9]; + int?[] expected = [9, 9, 9, 9, 9, 9]; Assert.Equal(expected, source.OrderByDescending(e => e)); } @@ -80,8 +80,8 @@ public void KeySelectorCalled() [Fact] public void FirstAndLastAreDuplicatesCustomComparer() { - string[] source = { "Prakash", "Alpha", "DAN", "dan", "Prakash" }; - string[] expected = { "Prakash", "Prakash", "DAN", "dan", "Alpha" }; + string[] source = ["Prakash", "Alpha", "DAN", "dan", "Prakash"]; + string[] expected = ["Prakash", "Prakash", "DAN", "dan", "Alpha"]; Assert.Equal(expected, source.OrderByDescending(e => e, StringComparer.OrdinalIgnoreCase)); } @@ -89,8 +89,8 @@ public void FirstAndLastAreDuplicatesCustomComparer() [Fact] public void RunOnce() { - string[] source = { "Prakash", "Alpha", "DAN", "dan", "Prakash" }; - string[] expected = { "Prakash", "Prakash", "DAN", "dan", "Alpha" }; + string[] source = ["Prakash", "Alpha", "DAN", "dan", "Prakash"]; + string[] expected = ["Prakash", "Prakash", "DAN", "dan", "Alpha"]; Assert.Equal(expected, source.RunOnce().OrderByDescending(e => e, StringComparer.OrdinalIgnoreCase)); } @@ -98,8 +98,8 @@ public void RunOnce() [Fact] public void FirstAndLastAreDuplicatesNullPassedAsComparer() { - int[] source = { 5, 1, 3, 2, 5 }; - int[] expected = { 5, 5, 3, 2, 1 }; + int[] source = [5, 1, 3, 2, 5]; + int[] expected = [5, 5, 3, 2, 1]; Assert.Equal(expected, source.OrderByDescending(e => e, null)); } @@ -107,8 +107,8 @@ public void FirstAndLastAreDuplicatesNullPassedAsComparer() [Fact] public void SourceReverseOfResultNullPassedAsComparer() { - int[] source = { -75, -50, 0, 5, 9, 30, 100 }; - int[] expected = { 100, 30, 9, 5, 0, -50, -75 }; + int[] source = [-75, -50, 0, 5, 9, 30, 100]; + int[] expected = [100, 30, 9, 5, 0, -50, -75]; Assert.Equal(expected, source.OrderByDescending(e => e, null)); } @@ -155,7 +155,7 @@ public int Compare(int x, int y) [Fact] public void OrderByExtremeComparer() { - int[] outOfOrder = new[] { 7, 1, 0, 9, 3, 5, 4, 2, 8, 6 }; + int[] outOfOrder = [7, 1, 0, 9, 3, 5, 4, 2, 8, 6]; // The .NET Framework has a bug where the input is incorrectly ordered if the comparer // returns int.MaxValue or int.MinValue. See https://github.com/dotnet/corefx/pull/2240. diff --git a/tests/System.Linq.Tests/Tests/ZLinq/OrderByTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/OrderByTests.cs index 065aeb92..fda31a68 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/OrderByTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/OrderByTests.cs @@ -51,7 +51,7 @@ public void SameResultsRepeatCallsStringQuery() [Fact] public void SourceEmpty() { - int[] source = { }; + int[] source = []; Assert.Empty(source.OrderBy(e => e)); } @@ -66,8 +66,8 @@ public void OrderedCount() [Fact] public void SurviveBadComparerAlwaysReturnsNegative() { - int[] source = { 1 }; - int[] expected = { 1 }; + int[] source = [1]; + int[] expected = [1]; Assert.Equal(expected, source.OrderBy(e => e, new BadComparer2())); } @@ -75,8 +75,8 @@ public void SurviveBadComparerAlwaysReturnsNegative() [Fact] public void KeySelectorReturnsNull() { - int?[] source = { null, null, null }; - int?[] expected = { null, null, null }; + int?[] source = [null, null, null]; + int?[] expected = [null, null, null]; Assert.Equal(expected, source.OrderBy(e => e)); } @@ -84,8 +84,8 @@ public void KeySelectorReturnsNull() [Fact] public void ElementsAllSameKey() { - int?[] source = { 9, 9, 9, 9, 9, 9 }; - int?[] expected = { 9, 9, 9, 9, 9, 9 }; + int?[] source = [9, 9, 9, 9, 9, 9]; + int?[] expected = [9, 9, 9, 9, 9, 9]; Assert.Equal(expected, source.OrderBy(e => e)); } @@ -112,8 +112,8 @@ public void KeySelectorCalled() [Fact] public void FirstAndLastAreDuplicatesCustomComparer() { - string[] source = { "Prakash", "Alpha", "dan", "DAN", "Prakash" }; - string[] expected = { "Alpha", "dan", "DAN", "Prakash", "Prakash" }; + string[] source = ["Prakash", "Alpha", "dan", "DAN", "Prakash"]; + string[] expected = ["Alpha", "dan", "DAN", "Prakash", "Prakash"]; Assert.Equal(expected, source.OrderBy(e => e, StringComparer.OrdinalIgnoreCase)); } @@ -121,8 +121,8 @@ public void FirstAndLastAreDuplicatesCustomComparer() [Fact] public void RunOnce() { - string[] source = { "Prakash", "Alpha", "dan", "DAN", "Prakash" }; - string[] expected = { "Alpha", "dan", "DAN", "Prakash", "Prakash" }; + string[] source = ["Prakash", "Alpha", "dan", "DAN", "Prakash"]; + string[] expected = ["Alpha", "dan", "DAN", "Prakash", "Prakash"]; Assert.Equal(expected, source.RunOnce().OrderBy(e => e, StringComparer.OrdinalIgnoreCase)); } @@ -130,8 +130,8 @@ public void RunOnce() [Fact] public void FirstAndLastAreDuplicatesNullPassedAsComparer() { - int[] source = { 5, 1, 3, 2, 5 }; - int[] expected = { 1, 2, 3, 5, 5 }; + int[] source = [5, 1, 3, 2, 5]; + int[] expected = [1, 2, 3, 5, 5]; Assert.Equal(expected, source.OrderBy(e => e, null)); } @@ -139,8 +139,8 @@ public void FirstAndLastAreDuplicatesNullPassedAsComparer() [Fact] public void SourceReverseOfResultNullPassedAsComparer() { - int?[] source = { 100, 30, 9, 5, 0, -50, -75, null }; - int?[] expected = { null, -75, -50, 0, 5, 9, 30, 100 }; + int?[] source = [100, 30, 9, 5, 0, -50, -75, null]; + int?[] expected = [null, -75, -50, 0, 5, 9, 30, 100]; Assert.Equal(expected, source.OrderBy(e => e, null)); } @@ -236,8 +236,8 @@ public void EmptyOrderedToList() [Fact] public void SurviveBadComparerAlwaysReturnsPositive() { - int[] source = { 1 }; - int[] expected = { 1 }; + int[] source = [1]; + int[] expected = [1]; Assert.Equal(expected, source.OrderBy(e => e, new BadComparer1())); } @@ -404,7 +404,7 @@ public void LastOrDefaultOnOrdered() [Fact] public void EnumeratorDoesntContinue() { - var enumerator = NumberRangeGuaranteedNotCollectionType(0, 3).Shuffle().OrderBy(i => i).GetEnumerator(); + using var enumerator = NumberRangeGuaranteedNotCollectionType(0, 3).Shuffle().OrderBy(i => i).GetEnumerator(); while (enumerator.MoveNext()) { } Assert.False(enumerator.MoveNext()); } @@ -517,7 +517,7 @@ public void TakeOne(IEnumerable source) [Fact] public void CultureOrderBy() { - string[] source = new[] { "Apple0", "\uFFFDble0", "Apple1", "\uFFFDble1", "Apple2", "\uFFFDble2" }; + string[] source = ["Apple0", "\uFFFDble0", "Apple1", "\uFFFDble1", "Apple2", "\uFFFDble2"]; CultureInfo dk = new CultureInfo("da-DK"); CultureInfo au = new CultureInfo("en-AU"); @@ -550,7 +550,7 @@ public void CultureOrderBy() using (new ThreadCultureChange(dk)) // "dk" whilst GetEnumerator { - var s = source.OrderBy(x => x).GetEnumerator(); + using var s = source.OrderBy(x => x).GetEnumerator(); using (new ThreadCultureChange(au)) // but "au" whilst accessing... { int idx = 0; @@ -564,7 +564,7 @@ public void CultureOrderBy() using (new ThreadCultureChange(au)) { // "au" whilst GetEnumerator - var s = source.OrderBy(x => x).GetEnumerator(); + using var s = source.OrderBy(x => x).GetEnumerator(); using (new ThreadCultureChange(dk)) { @@ -589,7 +589,7 @@ public void CultureOrderBy() [Fact] public void CultureOrderByElementAt() { - string[] source = new[] { "Apple0", "\uFFFDble0", "Apple1", "\uFFFDble1", "Apple2", "\uFFFDble2" }; + string[] source = ["Apple0", "\uFFFDble0", "Apple1", "\uFFFDble1", "Apple2", "\uFFFDble2"]; CultureInfo dk = new CultureInfo("da-DK"); CultureInfo au = new CultureInfo("en-AU"); diff --git a/tests/System.Linq.Tests/Tests/ZLinq/OrderDescendingTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/OrderDescendingTests.cs index 286af971..9ad83d37 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/OrderDescendingTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/OrderDescendingTests.cs @@ -30,15 +30,15 @@ public void SameResultsRepeatCallsStringQuery() [Fact] public void SourceEmpty() { - int[] source = { }; + int[] source = []; Assert.Empty(source.OrderDescending()); } [Fact] public void KeySelectorReturnsNull() { - int?[] source = { null, null, null }; - int?[] expected = { null, null, null }; + int?[] source = [null, null, null]; + int?[] expected = [null, null, null]; Assert.Equal(expected, source.OrderDescending()); } @@ -46,8 +46,8 @@ public void KeySelectorReturnsNull() [Fact] public void ElementsAllSameKey() { - int?[] source = { 9, 9, 9, 9, 9, 9 }; - int?[] expected = { 9, 9, 9, 9, 9, 9 }; + int?[] source = [9, 9, 9, 9, 9, 9]; + int?[] expected = [9, 9, 9, 9, 9, 9]; Assert.Equal(expected, source.OrderDescending()); } @@ -70,8 +70,8 @@ public void KeySelectorCalled() [Fact] public void FirstAndLastAreDuplicatesCustomComparer() { - string[] source = { "Prakash", "Alpha", "DAN", "dan", "Prakash" }; - string[] expected = { "Prakash", "Prakash", "DAN", "dan", "Alpha" }; + string[] source = ["Prakash", "Alpha", "DAN", "dan", "Prakash"]; + string[] expected = ["Prakash", "Prakash", "DAN", "dan", "Alpha"]; Assert.Equal(expected, source.OrderDescending(StringComparer.OrdinalIgnoreCase)); } @@ -79,8 +79,8 @@ public void FirstAndLastAreDuplicatesCustomComparer() [Fact] public void RunOnce() { - string[] source = { "Prakash", "Alpha", "DAN", "dan", "Prakash" }; - string[] expected = { "Prakash", "Prakash", "DAN", "dan", "Alpha" }; + string[] source = ["Prakash", "Alpha", "DAN", "dan", "Prakash"]; + string[] expected = ["Prakash", "Prakash", "DAN", "dan", "Alpha"]; Assert.Equal(expected, source.RunOnce().OrderDescending(StringComparer.OrdinalIgnoreCase)); } @@ -88,8 +88,8 @@ public void RunOnce() [Fact] public void FirstAndLastAreDuplicatesNullPassedAsComparer() { - int[] source = { 5, 1, 3, 2, 5 }; - int[] expected = { 5, 5, 3, 2, 1 }; + int[] source = [5, 1, 3, 2, 5]; + int[] expected = [5, 5, 3, 2, 1]; Assert.Equal(expected, source.OrderDescending(null)); } @@ -97,8 +97,8 @@ public void FirstAndLastAreDuplicatesNullPassedAsComparer() [Fact] public void SourceReverseOfResultNullPassedAsComparer() { - int[] source = { -75, -50, 0, 5, 9, 30, 100 }; - int[] expected = { 100, 30, 9, 5, 0, -50, -75 }; + int[] source = [-75, -50, 0, 5, 9, 30, 100]; + int[] expected = [100, 30, 9, 5, 0, -50, -75]; Assert.Equal(expected, source.OrderDescending(null)); } @@ -175,7 +175,7 @@ public int Compare(int x, int y) [Fact] public void OrderByExtremeComparer() { - int[] outOfOrder = new[] { 7, 1, 0, 9, 3, 5, 4, 2, 8, 6 }; + int[] outOfOrder = [7, 1, 0, 9, 3, 5, 4, 2, 8, 6]; // The .NET Framework has a bug where the input is incorrectly ordered if the comparer // returns int.MaxValue or int.MinValue. See https://github.com/dotnet/corefx/pull/2240. diff --git a/tests/System.Linq.Tests/Tests/ZLinq/OrderTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/OrderTests.cs index 55db129d..4905318c 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/OrderTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/OrderTests.cs @@ -37,7 +37,7 @@ public void SameResultsRepeatCallsIntQuery() [Fact] public void SourceEmpty() { - int[] source = { }; + int[] source = []; Assert.Empty(source.Order()); } @@ -52,8 +52,8 @@ public void OrderedCount() [Fact] public void SurviveBadComparerAlwaysReturnsNegative() { - int[] source = { 1 }; - int[] expected = { 1 }; + int[] source = [1]; + int[] expected = [1]; Assert.Equal(expected, source.Order(new BadComparer2())); } @@ -61,8 +61,8 @@ public void SurviveBadComparerAlwaysReturnsNegative() [Fact] public void KeySelectorReturnsNull() { - int?[] source = { null, null, null }; - int?[] expected = { null, null, null }; + int?[] source = [null, null, null]; + int?[] expected = [null, null, null]; Assert.Equal(expected, source.Order()); } @@ -70,8 +70,8 @@ public void KeySelectorReturnsNull() [Fact] public void ElementsAllSameKey() { - int?[] source = { 9, 9, 9, 9, 9, 9 }; - int?[] expected = { 9, 9, 9, 9, 9, 9 }; + int?[] source = [9, 9, 9, 9, 9, 9]; + int?[] expected = [9, 9, 9, 9, 9, 9]; Assert.Equal(expected, source.Order()); } @@ -79,8 +79,8 @@ public void ElementsAllSameKey() [Fact] public void FirstAndLastAreDuplicatesCustomComparer() { - string[] source = { "Prakash", "Alpha", "dan", "DAN", "Prakash" }; - string[] expected = { "Alpha", "dan", "DAN", "Prakash", "Prakash" }; + string[] source = ["Prakash", "Alpha", "dan", "DAN", "Prakash"]; + string[] expected = ["Alpha", "dan", "DAN", "Prakash", "Prakash"]; Assert.Equal(expected, source.Order(StringComparer.OrdinalIgnoreCase)); } @@ -88,8 +88,8 @@ public void FirstAndLastAreDuplicatesCustomComparer() [Fact] public void RunOnce() { - string[] source = { "Prakash", "Alpha", "dan", "DAN", "Prakash" }; - string[] expected = { "Alpha", "dan", "DAN", "Prakash", "Prakash" }; + string[] source = ["Prakash", "Alpha", "dan", "DAN", "Prakash"]; + string[] expected = ["Alpha", "dan", "DAN", "Prakash", "Prakash"]; Assert.Equal(expected, source.RunOnce().Order(StringComparer.OrdinalIgnoreCase)); } @@ -97,8 +97,8 @@ public void RunOnce() [Fact] public void FirstAndLastAreDuplicatesNullPassedAsComparer() { - int[] source = { 5, 1, 3, 2, 5 }; - int[] expected = { 1, 2, 3, 5, 5 }; + int[] source = [5, 1, 3, 2, 5]; + int[] expected = [1, 2, 3, 5, 5]; Assert.Equal(expected, source.Order(null)); } @@ -106,8 +106,8 @@ public void FirstAndLastAreDuplicatesNullPassedAsComparer() [Fact] public void SourceReverseOfResultNullPassedAsComparer() { - int?[] source = { 100, 30, 9, 5, 0, -50, -75, null }; - int?[] expected = { null, -75, -50, 0, 5, 9, 30, 100 }; + int?[] source = [100, 30, 9, 5, 0, -50, -75, null]; + int?[] expected = [null, -75, -50, 0, 5, 9, 30, 100]; Assert.Equal(expected, source.Order(null)); } @@ -158,8 +158,8 @@ public void EmptyOrderedToList() [Fact] public void SurviveBadComparerAlwaysReturnsPositive() { - int[] source = { 1 }; - int[] expected = { 1 }; + int[] source = [1]; + int[] expected = [1]; Assert.Equal(expected, source.Order(new BadComparer1())); } @@ -291,7 +291,7 @@ public void LastOnOrdered() [Fact] public void LastOnOrderedMatchingCases() { - object[] boxedInts = new object[] { 0, 1, 2, 9, 1, 2, 3, 9, 4, 5, 7, 8, 9, 0, 1 }; + object[] boxedInts = [0, 1, 2, 9, 1, 2, 3, 9, 4, 5, 7, 8, 9, 0, 1]; Assert.Same(boxedInts[12], boxedInts.Order().Last()); Assert.Same(boxedInts[12], boxedInts.Order().LastOrDefault()); Assert.Same(boxedInts[12], boxedInts.Order().Last(o => (int)o % 2 == 1)); @@ -325,7 +325,7 @@ public void ElementAtOnOrdered() [Fact] public void EnumeratorDoesntContinue() { - var enumerator = NumberRangeGuaranteedNotCollectionType(0, 3).Shuffle().Order().GetEnumerator(); + using var enumerator = NumberRangeGuaranteedNotCollectionType(0, 3).Shuffle().Order().GetEnumerator(); while (enumerator.MoveNext()) { } Assert.False(enumerator.MoveNext()); } @@ -397,7 +397,7 @@ public void TakeOne(IEnumerable source) [Fact] public void CultureOrder() { - string[] source = new[] { "Apple0", "\u00C6ble0", "Apple1", "\u00C6ble1", "Apple2", "\u00C6ble2" }; + string[] source = ["Apple0", "\u00C6ble0", "Apple1", "\u00C6ble1", "Apple2", "\u00C6ble2"]; CultureInfo dk = new CultureInfo("da-DK"); CultureInfo au = new CultureInfo("en-AU"); @@ -430,7 +430,7 @@ public void CultureOrder() using (new ThreadCultureChange(dk)) // "dk" whilst GetEnumerator { - var s = source.Order().GetEnumerator(); + using var s = source.Order().GetEnumerator(); using (new ThreadCultureChange(au)) // but "au" whilst accessing... { int idx = 0; @@ -444,7 +444,7 @@ public void CultureOrder() using (new ThreadCultureChange(au)) { // "au" whilst GetEnumerator - var s = source.Order().GetEnumerator(); + using var s = source.Order().GetEnumerator(); using (new ThreadCultureChange(dk)) { @@ -469,7 +469,7 @@ public void CultureOrder() [Fact] public void CultureOrderElementAt() { - string[] source = new[] { "Apple0", "\u00C6ble0", "Apple1", "\u00C6ble1", "Apple2", "\u00C6ble2" }; + string[] source = ["Apple0", "\u00C6ble0", "Apple1", "\u00C6ble1", "Apple2", "\u00C6ble2"]; CultureInfo dk = new CultureInfo("da-DK"); CultureInfo au = new CultureInfo("en-AU"); @@ -504,7 +504,7 @@ public void CultureOrderElementAt() [Fact] public void StableSort_CustomComparerAlwaysReturns0() { - byte[] values = new byte[] { 0x45, 0x7D, 0x4B, 0x61, 0x27 }; + byte[] values = [0x45, 0x7D, 0x4B, 0x61, 0x27]; byte[] newValues = values.Order(Comparer.Create((a, b) => 0)).ToArray(); AssertExtensions.SequenceEqual(values, newValues); } diff --git a/tests/System.Linq.Tests/Tests/ZLinq/OrderedSubsetting.cs b/tests/System.Linq.Tests/Tests/ZLinq/OrderedSubsetting.cs index 105d44c1..3fad4ca2 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/OrderedSubsetting.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/OrderedSubsetting.cs @@ -107,7 +107,7 @@ public void Take() Assert.Empty(ordered.Take(0)); Assert.Empty(ordered.Take(-1)); Assert.Empty(ordered.Take(int.MinValue)); - Assert.Equal(new int[] { 0 }, ordered.Take(1)); + Assert.Equal([0], ordered.Take(1)); Assert.Equal(Enumerable.Range(0, 100), ordered.Take(101)); Assert.Equal(Enumerable.Range(0, 100), ordered.Take(int.MaxValue)); Assert.Equal(Enumerable.Range(0, 100), ordered.Take(100)); @@ -145,7 +145,7 @@ public void Skip() Assert.Equal(Enumerable.Range(0, 100), ordered.Skip(0)); Assert.Equal(Enumerable.Range(0, 100), ordered.Skip(-1)); Assert.Equal(Enumerable.Range(0, 100), ordered.Skip(int.MinValue)); - Assert.Equal(new int[] { 99 }, ordered.Skip(99)); + Assert.Equal([99], ordered.Skip(99)); Assert.Empty(ordered.Skip(101)); Assert.Empty(ordered.Skip(int.MaxValue)); Assert.Empty(ordered.Skip(100)); @@ -335,10 +335,10 @@ public void Count() [Fact] public void SkipTakesOnlyOne() { - Assert.Equal(new[] { 1 }, Enumerable.Range(1, 10).Shuffle().OrderBy(i => i).Take(1)); - Assert.Equal(new[] { 2 }, Enumerable.Range(1, 10).Shuffle().OrderBy(i => i).Skip(1).Take(1)); - Assert.Equal(new[] { 3 }, Enumerable.Range(1, 10).Shuffle().OrderBy(i => i).Take(3).Skip(2)); - Assert.Equal(new[] { 1 }, Enumerable.Range(1, 10).Shuffle().OrderBy(i => i).Take(3).Take(1)); + Assert.Equal([1], Enumerable.Range(1, 10).Shuffle().OrderBy(i => i).Take(1)); + Assert.Equal([2], Enumerable.Range(1, 10).Shuffle().OrderBy(i => i).Skip(1).Take(1)); + Assert.Equal([3], Enumerable.Range(1, 10).Shuffle().OrderBy(i => i).Take(3).Skip(2)); + Assert.Equal([1], Enumerable.Range(1, 10).Shuffle().OrderBy(i => i).Take(3).Take(1)); } [Fact] @@ -399,7 +399,7 @@ public void SingleElementCount() [Fact] public void EnumeratorDoesntContinue() { - var enumerator = NumberRangeGuaranteedNotCollectionType(0, 3).Shuffle().OrderBy(i => i).Skip(1).GetEnumerator(); + using var enumerator = NumberRangeGuaranteedNotCollectionType(0, 3).Shuffle().OrderBy(i => i).Skip(1).GetEnumerator(); while (enumerator.MoveNext()) { } Assert.False(enumerator.MoveNext()); } @@ -407,7 +407,7 @@ public void EnumeratorDoesntContinue() [Fact] public void Select() { - Assert.Equal(new[] { 0, 2, 4, 6, 8 }, Enumerable.Range(-1, 8).Shuffle().OrderBy(i => i).Skip(1).Take(5).Select(i => i * 2)); + Assert.Equal([0, 2, 4, 6, 8], Enumerable.Range(-1, 8).Shuffle().OrderBy(i => i).Skip(1).Take(5).Select(i => i * 2)); } [Fact(Skip = SkipReason.EnumeratorBehaviorDifference)] @@ -453,14 +453,14 @@ public void SelectLast() public void SelectArray() { var source = Enumerable.Range(0, 9).Shuffle().OrderBy(i => i).Skip(1).Take(5).Select(i => i * 2); - Assert.Equal(new[] { 2, 4, 6, 8, 10 }, source.ToArray()); + Assert.Equal([2, 4, 6, 8, 10], source.ToArray()); } [Fact] public void SelectList() { var source = Enumerable.Range(0, 9).Shuffle().OrderBy(i => i).Skip(1).Take(5).Select(i => i * 2); - Assert.Equal(new[] { 2, 4, 6, 8, 10 }, source.ToList()); + Assert.Equal([2, 4, 6, 8, 10], source.ToList()); } [Fact] diff --git a/tests/System.Linq.Tests/Tests/ZLinq/RangeTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/RangeTests.cs index a8e035a8..bda4b888 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/RangeTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/RangeTests.cs @@ -26,7 +26,7 @@ public static IEnumerable Range_ToArray_ProduceCorrectResult_MemberDat { for (int i = 0; i < 64; i++) { - yield return new object[] { i }; + yield return [i]; } } @@ -78,33 +78,27 @@ public void Range_ThrowExceptionOnOverflow() [Fact] public void Range_NotEnumerateAfterEnd() { - using (var rangeEnum = Enumerable.Range(1, 1).GetEnumerator()) - { - Assert.True(rangeEnum.MoveNext()); - Assert.False(rangeEnum.MoveNext()); - Assert.False(rangeEnum.MoveNext()); - } + using var rangeEnum = Enumerable.Range(1, 1).GetEnumerator(); + Assert.True(rangeEnum.MoveNext()); + Assert.False(rangeEnum.MoveNext()); + Assert.False(rangeEnum.MoveNext()); } [Fact] public void Range_EnumerableAndEnumeratorAreSame() { var rangeEnumerable = Enumerable.Range(1, 1); - using (var rangeEnumerator = rangeEnumerable.GetEnumerator()) - { - Assert.Same(rangeEnumerable, rangeEnumerator); - } + using var rangeEnumerator = rangeEnumerable.GetEnumerator(); + Assert.Same(rangeEnumerable, rangeEnumerator); } [Fact] public void Range_GetEnumeratorReturnUniqueInstances() { var rangeEnumerable = Enumerable.Range(1, 1); - using (var enum1 = rangeEnumerable.GetEnumerator()) - using (var enum2 = rangeEnumerable.GetEnumerator()) - { - Assert.NotSame(enum1, enum2); - } + using var enum1 = rangeEnumerable.GetEnumerator(); + using var enum2 = rangeEnumerable.GetEnumerator(); + Assert.NotSame(enum1, enum2); } [Fact] @@ -116,7 +110,7 @@ public void Range_ToInt32MaxValue() Assert.Equal(count, rangeEnumerable.Count()); - int[] expected = { int.MaxValue - 3, int.MaxValue - 2, int.MaxValue - 1, int.MaxValue }; + int[] expected = [int.MaxValue - 3, int.MaxValue - 2, int.MaxValue - 1, int.MaxValue]; Assert.Equal(expected, rangeEnumerable); } @@ -132,7 +126,7 @@ public void NegativeStart() { int start = -5; int count = 1; - int[] expected = { -5 }; + int[] expected = [-5]; Assert.Equal(expected, Enumerable.Range(start, count)); } @@ -142,7 +136,7 @@ public void ArbitraryStart() { int start = 12; int count = 6; - int[] expected = { 12, 13, 14, 15, 16, 17 }; + int[] expected = [12, 13, 14, 15, 16, 17]; Assert.Equal(expected, Enumerable.Range(start, count)); } @@ -174,10 +168,10 @@ public void SkipExcessive() [Fact] public void SkipTakeCanOnlyBeOne() { - Assert.Equal(new[] { 1 }, Enumerable.Range(1, 10).Take(1)); - Assert.Equal(new[] { 2 }, Enumerable.Range(1, 10).Skip(1).Take(1)); - Assert.Equal(new[] { 3 }, Enumerable.Range(1, 10).Take(3).Skip(2)); - Assert.Equal(new[] { 1 }, Enumerable.Range(1, 10).Take(3).Take(1)); + Assert.Equal([1], Enumerable.Range(1, 10).Take(1)); + Assert.Equal([2], Enumerable.Range(1, 10).Skip(1).Take(1)); + Assert.Equal([3], Enumerable.Range(1, 10).Take(3).Skip(2)); + Assert.Equal([1], Enumerable.Range(1, 10).Take(3).Take(1)); } [Fact] @@ -246,8 +240,8 @@ public void LastOrDefault_ValueEnumerableRange() [Fact] public void IListImplementationIsValid() { - Validate(Enumerable.Range(42, 10), new[] { 42, 43, 44, 45, 46, 47, 48, 49, 50, 51 }); - Validate(Enumerable.Range(42, 10).Skip(3).Take(4).ToArray().AsEnumerable(), new[] { 45, 46, 47, 48 }); + Validate(Enumerable.Range(42, 10), [42, 43, 44, 45, 46, 47, 48, 49, 50, 51]); + Validate(Enumerable.Range(42, 10).Skip(3).Take(4).ToArray().AsEnumerable(), [45, 46, 47, 48]); static void Validate(IEnumerable e, int[] expected) { diff --git a/tests/System.Linq.Tests/Tests/ZLinq/RepeatTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/RepeatTests.cs index 59e1e935..d29ec3fe 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/RepeatTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/RepeatTests.cs @@ -125,7 +125,7 @@ public void SameResultsRepeatCallsStringQuery() [Fact] public void CountOneSingleResult() { - int[] expected = { -15 }; + int[] expected = [-15]; Assert.Equal(expected, Enumerable.Repeat(-15, 1)); } @@ -133,7 +133,7 @@ public void CountOneSingleResult() [Fact] public void RepeatArbitraryCorrectResults() { - int[] expected = { 12, 12, 12, 12, 12, 12, 12, 12 }; + int[] expected = [12, 12, 12, 12, 12, 12, 12, 12]; Assert.Equal(expected, Enumerable.Repeat(12, 8)); } @@ -141,7 +141,7 @@ public void RepeatArbitraryCorrectResults() [Fact] public void RepeatNull() { - int?[] expected = { null, null, null, null }; + int?[] expected = [null, null, null, null]; Assert.Equal(expected, Enumerable.Repeat((int?)null, 4)); } @@ -173,10 +173,10 @@ public void SkipExcessive() [Fact] public void TakeCanOnlyBeOne() { - Assert.Equal(new[] { 1 }, Enumerable.Repeat(1, 10).Take(1)); - Assert.Equal(new[] { 1 }, Enumerable.Repeat(1, 10).Skip(1).Take(1)); - Assert.Equal(new[] { 1 }, Enumerable.Repeat(1, 10).Take(3).Skip(2)); - Assert.Equal(new[] { 1 }, Enumerable.Repeat(1, 10).Take(3).Take(1)); + Assert.Equal([1], Enumerable.Repeat(1, 10).Take(1)); + Assert.Equal([1], Enumerable.Repeat(1, 10).Skip(1).Take(1)); + Assert.Equal([1], Enumerable.Repeat(1, 10).Take(3).Skip(2)); + Assert.Equal([1], Enumerable.Repeat(1, 10).Take(3).Take(1)); } [Fact] @@ -242,8 +242,8 @@ public void Count() [Fact] public void ICollectionImplementationIsValid() { - Validate(Enumerable.Repeat(42, 10), new[] { 42, 42, 42, 42, 42, 42, 42, 42, 42, 42 }); - Validate(Enumerable.Repeat(42, 10).Skip(3).Take(4).ToArray(), new[] { 42, 42, 42, 42 }); + Validate(Enumerable.Repeat(42, 10), [42, 42, 42, 42, 42, 42, 42, 42, 42, 42]); + Validate(Enumerable.Repeat(42, 10).Skip(3).Take(4).ToArray(), [42, 42, 42, 42]); static void Validate(IEnumerable e, int[] expected) { diff --git a/tests/System.Linq.Tests/Tests/ZLinq/ReverseTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/ReverseTests.cs index 3ce3af86..b3734e13 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/ReverseTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/ReverseTests.cs @@ -11,7 +11,8 @@ public class ReverseTests : EnumerableTests [Fact] public void InvalidArguments() { - AssertExtensions.Throws("source", () => Enumerable.Reverse(null)); + AssertExtensions.Throws("source", () => Enumerable.Reverse((IEnumerable)null)); + AssertExtensions.Throws("source", () => Enumerable.Reverse((string[])null)); } [Theory] @@ -48,6 +49,40 @@ public void Reverse(IEnumerable source) Assert.Equal(actual, actual); // Repeat the enumeration against itself. } + [Theory] + [MemberData(nameof(ReverseData))] + public void ReverseArray(IEnumerable source) + { + T[] expected = source.ToArray(); + Array.Reverse(expected); + + var actual = source.ToArray().Reverse(); + + Assert.Equal(expected, actual); + Assert.Equal(expected.Count(), actual.Count()); // Count may be optimized. + Assert.Equal(expected, actual.ToArray()); + Assert.Equal(expected, actual.ToList()); + + Assert.Equal(expected.FirstOrDefault(), actual.FirstOrDefault()); + Assert.Equal(expected.LastOrDefault(), actual.LastOrDefault()); + + for (int i = 0; i < expected.Length; i++) + { + Assert.Equal(expected[i], actual.ElementAt(i)); + + Assert.Equal(expected.Skip(i), actual.Skip(i)); + Assert.Equal(expected.Take(i), actual.Take(i)); + } + + Assert.Equal(default(T), actual.ElementAtOrDefault(-1)); + Assert.Equal(default(T), actual.ElementAtOrDefault(expected.Length)); + + Assert.Equal(expected, actual.Select(_ => _)); + Assert.Equal(expected, actual.Where(_ => true)); + + Assert.Equal(actual, actual); // Repeat the enumeration against itself. + } + [Theory, MemberData(nameof(ReverseData))] public void RunOnce(IEnumerable source) { @@ -64,9 +99,9 @@ public static IEnumerable ReverseData() var integers = new[] { Array.Empty(), // No elements. - new[] { 1 }, // One element. - new[] { 9999, 0, 888, -1, 66, -777, 1, 2, -12345 }, // Distinct elements. - new[] { -10, 0, 5, 0, 9, 100, 9 }, // Some repeating elements. + [1], // One element. + [9999, 0, 888, -1, 66, -777, 1, 2, -12345], // Distinct elements. + [-10, 0, 5, 0, 9, 100, 9], // Some repeating elements. }; return integers diff --git a/tests/System.Linq.Tests/Tests/ZLinq/SelectManyTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/SelectManyTests.cs index 4623f816..49c09b84 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/SelectManyTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/SelectManyTests.cs @@ -36,11 +36,11 @@ public void EmptySourceResultSelectorIndexedSelector() [Fact] public void SingleElement() { - int?[] expected = { 90, 55, null, 43, 89 }; + int?[] expected = [90, 55, null, 43, 89]; StringWithIntArray[] source = - { + [ new StringWithIntArray { name = "Prakash", total = expected } - }; + ]; Assert.Equal(expected, source.SelectMany(e => e.total)); } @@ -48,13 +48,13 @@ public void SingleElement() public void NonEmptySelectingEmpty() { StringWithIntArray[] source = - { - new StringWithIntArray { name="Prakash", total=new int?[0] }, - new StringWithIntArray { name="Bob", total=new int?[0] }, - new StringWithIntArray { name="Chris", total=new int?[0] }, - new StringWithIntArray { name=null, total=new int?[0] }, - new StringWithIntArray { name="Prakash", total=new int?[0] } - }; + [ + new StringWithIntArray { name="Prakash", total=[] }, + new StringWithIntArray { name="Bob", total=[] }, + new StringWithIntArray { name="Chris", total=[] }, + new StringWithIntArray { name=null, total=[] }, + new StringWithIntArray { name="Prakash", total=[] } + ]; Assert.Empty(source.SelectMany(e => e.total)); } @@ -63,13 +63,13 @@ public void NonEmptySelectingEmpty() public void NonEmptySelectingEmptyIndexedSelector() { StringWithIntArray[] source = - { - new StringWithIntArray { name="Prakash", total=new int?[0] }, - new StringWithIntArray { name="Bob", total=new int?[0] }, - new StringWithIntArray { name="Chris", total=new int?[0] }, - new StringWithIntArray { name=null, total=new int?[0] }, - new StringWithIntArray { name="Prakash", total=new int?[0] } - }; + [ + new StringWithIntArray { name="Prakash", total=[] }, + new StringWithIntArray { name="Bob", total=[] }, + new StringWithIntArray { name="Chris", total=[] }, + new StringWithIntArray { name=null, total=[] }, + new StringWithIntArray { name="Prakash", total=[] } + ]; Assert.Empty(source.SelectMany((e, i) => e.total)); } @@ -78,13 +78,13 @@ public void NonEmptySelectingEmptyIndexedSelector() public void NonEmptySelectingEmptyWithResultSelector() { StringWithIntArray[] source = - { - new StringWithIntArray { name="Prakash", total=new int?[0] }, - new StringWithIntArray { name="Bob", total=new int?[0] }, - new StringWithIntArray { name="Chris", total=new int?[0] }, - new StringWithIntArray { name=null, total=new int?[0] }, - new StringWithIntArray { name="Prakash", total=new int?[0] } - }; + [ + new StringWithIntArray { name="Prakash", total=[] }, + new StringWithIntArray { name="Bob", total=[] }, + new StringWithIntArray { name="Chris", total=[] }, + new StringWithIntArray { name=null, total=[] }, + new StringWithIntArray { name="Prakash", total=[] } + ]; Assert.Empty(source.SelectMany(e => e.total, (e, f) => f.ToString())); } @@ -93,13 +93,13 @@ public void NonEmptySelectingEmptyWithResultSelector() public void NonEmptySelectingEmptyIndexedSelectorWithResultSelector() { StringWithIntArray[] source = - { - new StringWithIntArray { name="Prakash", total=new int?[0] }, - new StringWithIntArray { name="Bob", total=new int?[0] }, - new StringWithIntArray { name="Chris", total=new int?[0] }, - new StringWithIntArray { name=null, total=new int?[0] }, - new StringWithIntArray { name="Prakash", total=new int?[0] } - }; + [ + new StringWithIntArray { name="Prakash", total=[] }, + new StringWithIntArray { name="Bob", total=[] }, + new StringWithIntArray { name="Chris", total=[] }, + new StringWithIntArray { name=null, total=[] }, + new StringWithIntArray { name="Prakash", total=[] } + ]; Assert.Empty(source.SelectMany((e, i) => e.total, (e, f) => f.ToString())); } @@ -108,14 +108,14 @@ public void NonEmptySelectingEmptyIndexedSelectorWithResultSelector() public void ResultsSelected() { 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} } - }; - int?[] expected = { 1, 2, 3, 4, 5, 6, 8, 9, -10, 100 }; + [ + new StringWithIntArray { name="Prakash", total= [1, 2, 3, 4] }, + new StringWithIntArray { name="Bob", total= [5, 6] }, + new StringWithIntArray { name="Chris", total=[] }, + new StringWithIntArray { name=null, total= [8, 9] }, + new StringWithIntArray { name="Prakash", total= [-10, 100] } + ]; + int?[] expected = [1, 2, 3, 4, 5, 6, 8, 9, -10, 100]; Assert.Equal(expected, source.SelectMany(e => e.total)); } @@ -123,14 +123,14 @@ public void ResultsSelected() public void RunOnce() { 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} } - }; - int?[] expected = { 1, 2, 3, 4, 5, 6, 8, 9, -10, 100 }; + [ + new StringWithIntArray { name="Prakash", total= [1, 2, 3, 4] }, + new StringWithIntArray { name="Bob", total= [5, 6] }, + new StringWithIntArray { name="Chris", total=[] }, + new StringWithIntArray { name=null, total= [8, 9] }, + new StringWithIntArray { name="Prakash", total= [-10, 100] } + ]; + int?[] expected = [1, 2, 3, 4, 5, 6, 8, 9, -10, 100]; Assert.Equal(expected, source.RunOnce().SelectMany(e => e.total.RunOnce())); } @@ -143,11 +143,11 @@ public void SourceEmptyIndexUsed() [Fact] public void SingleElementIndexUsed() { - int?[] expected = { 90, 55, null, 43, 89 }; + int?[] expected = [90, 55, null, 43, 89]; StringWithIntArray[] source = - { + [ new StringWithIntArray { name = "Prakash", total = expected } - }; + ]; Assert.Equal(expected, source.SelectMany((e, index) => e.total)); } @@ -155,13 +155,13 @@ public void SingleElementIndexUsed() public void NonEmptySelectingEmptyIndexUsed() { StringWithIntArray[] source = - { - new StringWithIntArray { name="Prakash", total= new int?[0] }, - new StringWithIntArray { name="Bob", total=new int?[0] }, - new StringWithIntArray { name="Chris", total=new int?[0] }, - new StringWithIntArray { name=null, total=new int?[0] }, - new StringWithIntArray { name="Prakash", total=new int?[0] } - }; + [ + new StringWithIntArray { name="Prakash", total= [] }, + new StringWithIntArray { name="Bob", total=[] }, + new StringWithIntArray { name="Chris", total=[] }, + new StringWithIntArray { name=null, total=[] }, + new StringWithIntArray { name="Prakash", total=[] } + ]; Assert.Empty(source.SelectMany((e, index) => e.total)); } @@ -169,14 +169,14 @@ public void NonEmptySelectingEmptyIndexUsed() public void ResultsSelectedIndexUsed() { 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} } - }; - int?[] expected = { 1, 2, 3, 4, 5, 6, 8, 9, -10, 100 }; + [ + new StringWithIntArray { name="Prakash", total= [1, 2, 3, 4] }, + new StringWithIntArray { name="Bob", total= [5, 6] }, + new StringWithIntArray { name="Chris", total=[] }, + new StringWithIntArray { name=null, total= [8, 9] }, + new StringWithIntArray { name="Prakash", total= [-10, 100] } + ]; + int?[] expected = [1, 2, 3, 4, 5, 6, 8, 9, -10, 100]; Assert.Equal(expected, source.SelectMany((e, index) => e.total)); } @@ -184,13 +184,13 @@ public void ResultsSelectedIndexUsed() public void IndexCausingFirstToBeSelected() { 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} } - }; + [ + new StringWithIntArray { name="Prakash", total= [1, 2, 3, 4] }, + new StringWithIntArray { name="Bob", total= [5, 6] }, + new StringWithIntArray { name="Chris", total=[] }, + new StringWithIntArray { name=null, total= [8, 9] }, + new StringWithIntArray { name="Robert", total= [-10, 100] } + ]; Assert.Equal(source.First().total, source.SelectMany((e, i) => i == 0 ? e.total : Enumerable.Empty())); } @@ -199,13 +199,13 @@ public void IndexCausingFirstToBeSelected() 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} } - }; + [ + new StringWithIntArray { name="Prakash", total=[1, 2, 3, 4] }, + new StringWithIntArray { name="Bob", total=[5, 6] }, + new StringWithIntArray { name="Chris", total=[] }, + new StringWithIntArray { name=null, total=[8, 9] }, + new StringWithIntArray { name="Robert", total=[-10, 100] } + ]; Assert.Equal(source.Last().total, source.SelectMany((e, i) => i == 4 ? e.total : Enumerable.Empty())); } @@ -227,14 +227,14 @@ public void IndexOverflow() public void ResultSelector() { 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", "5", "6", "8", "9", "-10", "100" }; + [ + new StringWithIntArray { name="Prakash", total= [1, 2, 3, 4] }, + new StringWithIntArray { name="Bob", total= [5, 6] }, + new StringWithIntArray { name="Chris", total=[] }, + new StringWithIntArray { name=null, total= [8, 9] }, + new StringWithIntArray { name="Prakash", total= [-10, 100] } + ]; + string[] expected = ["1", "2", "3", "4", "5", "6", "8", "9", "-10", "100"]; Assert.Equal(expected, source.SelectMany(e => e.total, (e, f) => f.ToString())); } @@ -313,14 +313,14 @@ public void NullIndexedSelector() 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" }; + [ + new StringWithIntArray { name="Prakash", total= [1, 2, 3, 4] }, + new StringWithIntArray { name="Bob", total= [5, 6] }, + new StringWithIntArray { name="Chris", total=[] }, + new StringWithIntArray { name=null, total= [8, 9] }, + new StringWithIntArray { name="Prakash", total= [-10, 100] } + ]; + string[] expected = ["1", "2", "3", "4"]; Assert.Equal(expected, source.SelectMany((e, i) => i == 0 ? e.total : Enumerable.Empty(), (e, f) => f.ToString())); } @@ -328,15 +328,15 @@ public void IndexCausingFirstToBeSelectedWithResultSelector() public void IndexCausingLastToBeSelectedWithResultSelector() { 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} } - }; - - string[] expected = { "-10", "100" }; + [ + new StringWithIntArray { name="Prakash", total= [1, 2, 3, 4] }, + new StringWithIntArray { name="Bob", total= [5, 6] }, + new StringWithIntArray { name="Chris", total=[] }, + new StringWithIntArray { name=null, total= [8, 9] }, + new StringWithIntArray { name="Robert", total= [-10, 100] } + ]; + + string[] expected = ["-10", "100"]; Assert.Equal(expected, source.SelectMany((e, i) => i == 4 ? e.total : Enumerable.Empty(), (e, f) => f.ToString())); } @@ -373,34 +373,23 @@ public void ForcedToEnumeratorDoesntEnumerateIndexedResultSel() Assert.False(en.TryGetNext(out _)); } - [Theory] - [MemberData(nameof(ParameterizedTestsData))] - public void ParameterizedTests(IEnumerable source, Func> selector) + [Fact] + public void ParameterizedTests() { - Assert.All(CreateSources(source), source => + for (int i = 1; i <= 20; i++) { - var expected = source.Select(i => selector(i)).Aggregate((l, r) => l.Concat(r).ToArray()).ToArray(); - var actual = source.SelectMany(selector); - - Assert.Equal(expected, actual); - Assert.Equal(expected.Count(), actual.Count()); // SelectMany may employ an optimized Count implementation. - Assert.Equal(expected.ToArray(), actual.ToArray()); - Assert.Equal(expected.ToList(), actual.ToList()); - }); - } + Assert.All(CreateSources(Enumerable.Range(1, i)), source => + { + Func> selector = n => Enumerable.Range(i, n); - public static IEnumerable ParameterizedTestsData() - { - var items = IdentityTransforms().ToArray(); - Debug.Assert(items.Length == 13); + var expected = source.Select(i => selector(i)).Aggregate((l, r) => l.Concat(r).ToArray()).ToArray(); + var actual = source.SelectMany(selector); - foreach (Func, IEnumerable> transform in IdentityTransforms()) - { - for (int i = 1; i <= 20; i++) - { - Func> selector = n => transform(Enumerable.Range(i, n)); - yield return new object[] { Enumerable.Range(1, i), selector }; - } + Assert.Equal(expected, actual); + Assert.Equal(expected.Length, actual.Count()); // SelectMany may employ an optimized Count implementation. + Assert.Equal(expected, actual.ToArray()); + Assert.Equal(expected, actual.ToList()); + }); } } @@ -470,7 +459,7 @@ public void DisposeAfterEnumeration(int sourceLength, int subLength) public static IEnumerable DisposeAfterEnumerationData() { - int[] lengths = { 1, 2, 3, 5, 8, 13, 21, 34 }; + int[] lengths = [1, 2, 3, 5, 8, 13, 21, 34]; return lengths.SelectMany(l => lengths, (l1, l2) => new object[] { l1, l2 }).ToArray(); } @@ -541,98 +530,97 @@ public void EvaluateSelectorOncePerItem(int count) public static IEnumerable GetToArrayDataSources() { // Marker at the end - yield return new object[] - { + yield return + [ new IEnumerable[] { - new TestEnumerable(new int[] { 0 }), - new TestEnumerable(new int[] { 1 }), - new TestEnumerable(new int[] { 2 }), - new int[] { 3 }, + new TestEnumerable([0]), + new TestEnumerable([1]), + new TestEnumerable([2]), + [3], } - }; + ]; // Marker at beginning - yield return new object[] - { + yield return + [ new IEnumerable[] { - new int[] { 0 }, - new TestEnumerable(new int[] { 1 }), - new TestEnumerable(new int[] { 2 }), - new TestEnumerable(new int[] { 3 }), + [0], + new TestEnumerable([1]), + new TestEnumerable([2]), + new TestEnumerable([3]), } - }; + ]; // Marker in middle - yield return new object[] - { + yield return + [ new IEnumerable[] { - new TestEnumerable(new int[] { 0 }), - new int[] { 1 }, - new TestEnumerable(new int[] { 2 }), + new TestEnumerable([0]), + [1], + new TestEnumerable([2]), } - }; + ]; // Non-marker in middle - yield return new object[] - { + yield return + [ new IEnumerable[] { - new int[] { 0 }, - new TestEnumerable(new int[] { 1 }), - new int[] { 2 }, + [0], + new TestEnumerable([1]), [2], } - }; + ]; // Big arrays (marker in middle) - yield return new object[] - { + yield return + [ new IEnumerable[] { new TestEnumerable(Enumerable.Range(0, 100).ToArray()), Enumerable.Range(100, 100).ToArray(), new TestEnumerable(Enumerable.Range(200, 100).ToArray()), } - }; + ]; // Big arrays (non-marker in middle) - yield return new object[] - { + yield return + [ new IEnumerable[] { Enumerable.Range(0, 100).ToArray(), new TestEnumerable(Enumerable.Range(100, 100).ToArray()), Enumerable.Range(200, 100).ToArray(), } - }; + ]; // Interleaved (first marker) - yield return new object[] - { + yield return + [ new IEnumerable[] { - new int[] { 0 }, - new TestEnumerable(new int[] { 1 }), - new int[] { 2 }, - new TestEnumerable(new int[] { 3 }), - new int[] { 4 }, + [0], + new TestEnumerable([1]), + [2], + new TestEnumerable([3]), + [4], } - }; + ]; // Interleaved (first non-marker) - yield return new object[] - { + yield return + [ new IEnumerable[] { - new TestEnumerable(new int[] { 0 }), - new int[] { 1 }, - new TestEnumerable(new int[] { 2 }), - new int[] { 3 }, - new TestEnumerable(new int[] { 4 }), + new TestEnumerable([0]), + [1], + new TestEnumerable([2]), + [3], + new TestEnumerable([4]), } - }; + ]; } } } diff --git a/tests/System.Linq.Tests/Tests/ZLinq/SelectTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/SelectTests.cs index ca597be8..164a94a9 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/SelectTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/SelectTests.cs @@ -32,7 +32,7 @@ public void SingleElement() { new { name = "Prakash", custID = 98088 } }; - string[] expected = { "Prakash" }; + string[] expected = ["Prakash"]; Assert.Equal(expected, source.Select(e => e.name)); } @@ -47,7 +47,7 @@ public void SelectProperty() new { name=(string)null, custID=30349 }, new { name="Prakash", custID=39030 } }; - string[] expected = { "Prakash", "Bob", "Chris", null, "Prakash" }; + string[] expected = ["Prakash", "Bob", "Chris", null, "Prakash"]; Assert.Equal(expected, source.Select(e => e.name)); } @@ -61,7 +61,7 @@ public void RunOnce() new { name=(string)null, custID=30349 }, new { name="Prakash", custID=39030 } }; - string[] expected = { "Prakash", "Bob", "Chris", null, "Prakash" }; + string[] expected = ["Prakash", "Bob", "Chris", null, "Prakash"]; Assert.Equal(expected, source.RunOnce().Select(e => e.name)); Assert.Equal(expected, source.ToArray().RunOnce().Select(e => e.name)); Assert.Equal(expected, source.ToList().RunOnce().Select(e => e.name)); @@ -70,7 +70,7 @@ public void RunOnce() [Fact] public void EmptyWithIndexedSelector() { - Assert.Equal(Enumerable.Empty(), Enumerable.Empty().Select((s, i) => s.Length + i)); + Assert.Equal([], Enumerable.Empty().Select((s, i) => s.Length + i)); } [Fact(Skip = SkipReason.RefStruct)] @@ -91,7 +91,7 @@ public void SingleElementIndexedSelector() { new { name = "Prakash", custID = 98088 } }; - string[] expected = { "Prakash" }; + string[] expected = ["Prakash"]; Assert.Equal(expected, source.Select((e, index) => e.name)); } @@ -106,7 +106,7 @@ public void SelectPropertyPassingIndex() new { name=(string)null, custID=30349 }, new { name="Prakash", custID=39030 } }; - string[] expected = { "Prakash", "Bob", "Chris", null, "Prakash" }; + string[] expected = ["Prakash", "Bob", "Chris", null, "Prakash"]; Assert.Equal(expected, source.Select((e, i) => e.name)); } @@ -118,7 +118,7 @@ public void SelectPropertyUsingIndex() new { name="Bob", custID=29099 }, new { name="Chris", custID=39033 } }; - string[] expected = { "Prakash", null, null }; + string[] expected = ["Prakash", null, null]; Assert.Equal(expected, source.Select((e, i) => i == 0 ? e.name : null)); } @@ -133,7 +133,7 @@ public void SelectPropertyPassingIndexOnLast() new { name="Allen", custID=39033 }, new { name="Chuck", custID=39033 } }; - string[] expected = { null, null, null, null, null, "Chuck" }; + string[] expected = [null, null, null, null, null, "Chuck"]; Assert.Equal(expected, source.Select((e, i) => i == 5 ? e.name : null)); } @@ -189,7 +189,7 @@ public void Select_SelectorIsNull_ArgumentNullExceptionThrown() public void Select_SourceIsAnArray_ExecutionIsDeferred() { bool funcCalled = false; - Func[] source = new Func[] { () => { funcCalled = true; return 1; } }; + Func[] source = [() => { funcCalled = true; return 1; }]; var query = source.Select(d => d()); Assert.False(funcCalled); @@ -239,7 +239,7 @@ public void Select_SourceIsIEnumerable_ExecutionIsDeferred() public void SelectSelect_SourceIsAnArray_ExecutionIsDeferred() { bool funcCalled = false; - Func[] source = new Func[] { () => { funcCalled = true; return 1; } }; + Func[] source = [() => { funcCalled = true; return 1; }]; var query = source.Select(d => d).Select(d => d()); Assert.False(funcCalled); @@ -288,7 +288,7 @@ public void SelectSelect_SourceIsIEnumerable_ExecutionIsDeferred() [Fact] public void Select_SourceIsAnArray_ReturnsExpectedValues() { - int[] source = new[] { 1, 2, 3, 4, 5 }; + int[] source = [1, 2, 3, 4, 5]; Func selector = i => i + 1; var query = source.Select(selector); @@ -307,7 +307,7 @@ public void Select_SourceIsAnArray_ReturnsExpectedValues() [Fact] public void Select_SourceIsAList_ReturnsExpectedValues() { - List source = new List { 1, 2, 3, 4, 5 }; + List source = [1, 2, 3, 4, 5]; Func selector = i => i + 1; var query = source.Select(selector); @@ -384,7 +384,7 @@ public void Select_SourceIsIEnumerable_ReturnsExpectedValues() [Fact] public void Select_SourceIsAnArray_CurrentIsDefaultOfTAfterEnumeration() { - int[] source = new[] { 1 }; + int[] source = [1]; Func selector = i => i + 1; var query = source.Select(selector); @@ -398,7 +398,7 @@ public void Select_SourceIsAnArray_CurrentIsDefaultOfTAfterEnumeration() [Fact] public void Select_SourceIsAList_CurrentIsDefaultOfTAfterEnumeration() { - List source = new List() { 1 }; + List source = [1]; Func selector = i => i + 1; var query = source.Select(selector); @@ -445,7 +445,7 @@ public void Select_SourceIsIEnumerable_CurrentIsDefaultOfTAfterEnumeration() var query = source.Select(selector); - var enumerator = query.GetEnumerator(); + using var enumerator = query.GetEnumerator(); while (enumerator.MoveNext()) ; Assert.Equal(default(int), enumerator.Current); @@ -455,7 +455,7 @@ public void Select_SourceIsIEnumerable_CurrentIsDefaultOfTAfterEnumeration() public void SelectSelect_SourceIsAnArray_ReturnsExpectedValues() { Func selector = i => i + 1; - int[] source = new[] { 1, 2, 3, 4, 5 }; + int[] source = [1, 2, 3, 4, 5]; var query = source.Select(selector).Select(selector); @@ -550,7 +550,7 @@ public void SelectSelect_SourceIsIEnumerable_ReturnsExpectedValues() [Fact] public void Select_SourceIsEmptyEnumerable_ReturnedCollectionHasNoElements() { - IEnumerable source = Enumerable.Empty(); + IEnumerable source = []; bool wasSelectorCalled = false; var result = source.Select(i => { wasSelectorCalled = true; return i + 1; }); @@ -568,13 +568,13 @@ public void Select_SourceIsEmptyEnumerable_ReturnedCollectionHasNoElements() [Fact] public void Select_ExceptionThrownFromSelector_ExceptionPropagatedToTheCaller() { - int[] source = new[] { 1, 2, 3, 4, 5 }; + int[] source = [1, 2, 3, 4, 5]; Func selector = i => { throw new InvalidOperationException(); }; Assert.Throws(() => { var result = source.Select(selector); - var enumerator = result.GetEnumerator(); + using var enumerator = result.GetEnumerator(); enumerator.MoveNext(); }); } @@ -582,7 +582,7 @@ public void Select_ExceptionThrownFromSelector_ExceptionPropagatedToTheCaller() [Fact] public void Select_ExceptionThrownFromSelector_IteratorCanBeUsedAfterExceptionIsCaught() { - int[] source = new[] { 1, 2, 3, 4, 5 }; + int[] source = [1, 2, 3, 4, 5]; Func selector = i => { if (i == 1) @@ -591,7 +591,7 @@ public void Select_ExceptionThrownFromSelector_IteratorCanBeUsedAfterExceptionIs }; var result = source.Select(selector); - var enumerator = result.GetEnumerator(); + using var enumerator = result.GetEnumerator(); try { @@ -615,7 +615,7 @@ public void Select_ExceptionThrownFromCurrentOfSourceIterator_ExceptionPropagate Assert.Throws(() => { var result = source.Select(selector); - var enumerator = result.GetEnumerator(); + using var enumerator = result.GetEnumerator(); enumerator.MoveNext(); }); } @@ -627,7 +627,7 @@ public void Select_ExceptionThrownFromCurrentOfSourceIterator_IteratorCanBeUsedA Func selector = i => i + 1; var result = source.Select(selector); - var enumerator = result.GetEnumerator(); + using var enumerator = result.GetEnumerator(); try { @@ -667,7 +667,7 @@ public void Select_ExceptionThrownFromMoveNextOfSourceIterator_ExceptionPropagat Assert.Throws(() => { var result = source.Select(selector); - var enumerator = result.GetEnumerator(); + using var enumerator = result.GetEnumerator(); enumerator.MoveNext(); }); } @@ -679,7 +679,7 @@ public void Select_ExceptionThrownFromMoveNextOfSourceIterator_IteratorCanBeUsed Func selector = i => i + 1; var result = source.Select(selector); - var enumerator = result.GetEnumerator(); + using var enumerator = result.GetEnumerator(); try { @@ -702,7 +702,7 @@ public void Select_ExceptionThrownFromGetEnumeratorOnSource_ExceptionPropagatedT Assert.Throws(() => { var result = source.Select(selector); - var enumerator = result.GetEnumerator(); + using var enumerator = result.GetEnumerator(); enumerator.MoveNext(); }); } @@ -714,7 +714,7 @@ public void Select_ExceptionThrownFromGetEnumeratorOnSource_CurrentIsSetToDefaul Func selector = i => i.ToString(); var result = source.Select(selector); - var enumerator = result.GetEnumerator(); + using var enumerator = result.GetEnumerator(); try { @@ -739,7 +739,7 @@ public void Select_SourceListGetsModifiedDuringIteration_ExceptionIsPropagated() Func selector = i => i + 1; var result = source.Select(selector); - var enumerator = result.GetEnumerator(); + using var enumerator = result.GetEnumerator(); Assert.True(enumerator.MoveNext()); Assert.Equal(2 /* 1 + 1 */, enumerator.Current); @@ -758,11 +758,11 @@ public void Select_SourceListGetsModifiedDuringIteration_ExceptionIsPropagated() [Fact(Skip = SkipReason.RefStruct)] public void Select_GetEnumeratorCalledTwice_DifferentInstancesReturned() { - int[] source = new[] { 1, 2, 3, 4, 5 }; + int[] source = [1, 2, 3, 4, 5]; var query = source.Select(i => i + 1); - var enumerator1 = query.GetEnumerator(); - var enumerator2 = query.GetEnumerator(); + using var enumerator1 = query.GetEnumerator(); + using var enumerator2 = query.GetEnumerator(); // ZLinq use ref struct. //Assert.Same(query, enumerator1); @@ -775,13 +775,13 @@ public void Select_GetEnumeratorCalledTwice_DifferentInstancesReturned() [Fact(Skip = SkipReason.EnumeratorReset)] public void Select_ResetCalledOnEnumerator_ThrowsException() { - int[] source = new[] { 1, 2, 3, 4, 5 }; + int[] source = [1, 2, 3, 4, 5]; Func selector = i => i + 1; Assert.Throws(() => { var result = source.Select(selector); - var enumerator = result.GetEnumerator(); + using var enumerator = result.GetEnumerator(); // enumerator.Reset(); // ZLinq don't support Reset() }); } @@ -861,10 +861,10 @@ public void Select_SourceIsAnIList_Count() public void Select_SourceIsArray_Skip() { var source = new[] { 1, 2, 3, 4 }.Select(i => i * 2); - Assert.Equal(new[] { 6, 8 }, source.Skip(2)); - Assert.Equal(new[] { 6, 8 }, source.Skip(2).Skip(-1)); - Assert.Equal(new[] { 6, 8 }, source.Skip(1).Skip(1)); - Assert.Equal(new[] { 2, 4, 6, 8 }, source.Skip(-1)); + Assert.Equal([6, 8], source.Skip(2)); + Assert.Equal([6, 8], source.Skip(2).Skip(-1)); + Assert.Equal([6, 8], source.Skip(1).Skip(1)); + Assert.Equal([2, 4, 6, 8], source.Skip(-1)); Assert.Empty(source.Skip(4)); Assert.Empty(source.Skip(20)); } @@ -873,10 +873,10 @@ public void Select_SourceIsArray_Skip() public void Select_SourceIsList_Skip() { var source = new List { 1, 2, 3, 4 }.Select(i => i * 2); - Assert.Equal(new[] { 6, 8 }, source.Skip(2)); - Assert.Equal(new[] { 6, 8 }, source.Skip(2).Skip(-1)); - Assert.Equal(new[] { 6, 8 }, source.Skip(1).Skip(1)); - Assert.Equal(new[] { 2, 4, 6, 8 }, source.Skip(-1)); + Assert.Equal([6, 8], source.Skip(2)); + Assert.Equal([6, 8], source.Skip(2).Skip(-1)); + Assert.Equal([6, 8], source.Skip(1).Skip(1)); + Assert.Equal([2, 4, 6, 8], source.Skip(-1)); Assert.Empty(source.Skip(4)); Assert.Empty(source.Skip(20)); } @@ -885,10 +885,10 @@ public void Select_SourceIsList_Skip() public void Select_SourceIsIList_Skip() { var source = new List { 1, 2, 3, 4 }.AsReadOnly().Select(i => i * 2); - Assert.Equal(new[] { 6, 8 }, source.Skip(2)); - Assert.Equal(new[] { 6, 8 }, source.Skip(2).Skip(-1)); - Assert.Equal(new[] { 6, 8 }, source.Skip(1).Skip(1)); - Assert.Equal(new[] { 2, 4, 6, 8 }, source.Skip(-1)); + Assert.Equal([6, 8], source.Skip(2)); + Assert.Equal([6, 8], source.Skip(2).Skip(-1)); + Assert.Equal([6, 8], source.Skip(1).Skip(1)); + Assert.Equal([2, 4, 6, 8], source.Skip(-1)); Assert.Empty(source.Skip(4)); Assert.Empty(source.Skip(20)); } @@ -897,45 +897,45 @@ public void Select_SourceIsIList_Skip() public void Select_SourceIsArray_Take() { var source = new[] { 1, 2, 3, 4 }.Select(i => i * 2); - Assert.Equal(new[] { 2, 4 }, source.Take(2)); - Assert.Equal(new[] { 2, 4 }, source.Take(3).Take(2)); + Assert.Equal([2, 4], source.Take(2)); + Assert.Equal([2, 4], source.Take(3).Take(2)); Assert.Empty(source.Take(-1)); - Assert.Equal(new[] { 2, 4, 6, 8 }, source.Take(4)); - Assert.Equal(new[] { 2, 4, 6, 8 }, source.Take(40)); - Assert.Equal(new[] { 2 }, source.Take(1)); - Assert.Equal(new[] { 4 }, source.Skip(1).Take(1)); - Assert.Equal(new[] { 6 }, source.Take(3).Skip(2)); - Assert.Equal(new[] { 2 }, source.Take(3).Take(1)); + Assert.Equal([2, 4, 6, 8], source.Take(4)); + Assert.Equal([2, 4, 6, 8], source.Take(40)); + Assert.Equal([2], source.Take(1)); + Assert.Equal([4], source.Skip(1).Take(1)); + Assert.Equal([6], source.Take(3).Skip(2)); + Assert.Equal([2], source.Take(3).Take(1)); } [Fact] public void Select_SourceIsList_Take() { var source = new List { 1, 2, 3, 4 }.Select(i => i * 2); - Assert.Equal(new[] { 2, 4 }, source.Take(2)); - Assert.Equal(new[] { 2, 4 }, source.Take(3).Take(2)); + Assert.Equal([2, 4], source.Take(2)); + Assert.Equal([2, 4], source.Take(3).Take(2)); Assert.Empty(source.Take(-1)); - Assert.Equal(new[] { 2, 4, 6, 8 }, source.Take(4)); - Assert.Equal(new[] { 2, 4, 6, 8 }, source.Take(40)); - Assert.Equal(new[] { 2 }, source.Take(1)); - Assert.Equal(new[] { 4 }, source.Skip(1).Take(1)); - Assert.Equal(new[] { 6 }, source.Take(3).Skip(2)); - Assert.Equal(new[] { 2 }, source.Take(3).Take(1)); + Assert.Equal([2, 4, 6, 8], source.Take(4)); + Assert.Equal([2, 4, 6, 8], source.Take(40)); + Assert.Equal([2], source.Take(1)); + Assert.Equal([4], source.Skip(1).Take(1)); + Assert.Equal([6], source.Take(3).Skip(2)); + Assert.Equal([2], source.Take(3).Take(1)); } [Fact] public void Select_SourceIsIList_Take() { var source = new List { 1, 2, 3, 4 }.AsReadOnly().Select(i => i * 2); - Assert.Equal(new[] { 2, 4 }, source.Take(2)); - Assert.Equal(new[] { 2, 4 }, source.Take(3).Take(2)); + Assert.Equal([2, 4], source.Take(2)); + Assert.Equal([2, 4], source.Take(3).Take(2)); Assert.Empty(source.Take(-1)); - Assert.Equal(new[] { 2, 4, 6, 8 }, source.Take(4)); - Assert.Equal(new[] { 2, 4, 6, 8 }, source.Take(40)); - Assert.Equal(new[] { 2 }, source.Take(1)); - Assert.Equal(new[] { 4 }, source.Skip(1).Take(1)); - Assert.Equal(new[] { 6 }, source.Take(3).Skip(2)); - Assert.Equal(new[] { 2 }, source.Take(3).Take(1)); + Assert.Equal([2, 4, 6, 8], source.Take(4)); + Assert.Equal([2, 4, 6, 8], source.Take(40)); + Assert.Equal([2], source.Take(1)); + Assert.Equal([4], source.Skip(1).Take(1)); + Assert.Equal([6], source.Take(3).Skip(2)); + Assert.Equal([2], source.Take(3).Take(1)); } [Fact] @@ -1141,22 +1141,22 @@ public void Select_SourceIsArray_SkipRepeatCalls() public void Select_SourceIsArraySkipSelect() { var source = new[] { 1, 2, 3, 4 }.Select(i => i * 2).Skip(1).Select(i => i + 1); - Assert.Equal(new[] { 5, 7, 9 }, source); + Assert.Equal([5, 7, 9], source); } [Fact] public void Select_SourceIsArrayTakeTake() { var source = new[] { 1, 2, 3, 4 }.Select(i => i * 2).Take(2).Take(1); - Assert.Equal(new[] { 2 }, source); - Assert.Equal(new[] { 2 }, source.Take(10)); + Assert.Equal([2], source); + Assert.Equal([2], source.Take(10)); } [Fact] public void Select_SourceIsIPartitionToArray() { - Assert.Equal(Array.Empty(), new List().Order().Select(i => i * 2).ToArray()); - Assert.Equal(new[] { 2, 4, 6, 8 }, new List { 1, 2, 3, 4 }.Order().Select(i => i * 2).ToArray()); + Assert.Equal([], new List().Order().Select(i => i * 2).ToArray()); + Assert.Equal([2, 4, 6, 8], new List { 1, 2, 3, 4 }.Order().Select(i => i * 2).ToArray()); } [Fact] @@ -1171,26 +1171,26 @@ public void Select_SourceIsListSkipTakeCount() [Fact] public void Select_SourceIsListSkipTakeToArray() { - Assert.Equal(new[] { 2, 4, 6 }, new List { 1, 2, 3, 4 }.Select(i => i * 2).Take(3).ToArray()); - Assert.Equal(new[] { 2, 4, 6, 8 }, new List { 1, 2, 3, 4 }.Select(i => i * 2).Take(9).ToArray()); - Assert.Equal(new[] { 6, 8 }, new List { 1, 2, 3, 4 }.Select(i => i * 2).Skip(2).ToArray()); + Assert.Equal([2, 4, 6], new List { 1, 2, 3, 4 }.Select(i => i * 2).Take(3).ToArray()); + Assert.Equal([2, 4, 6, 8], new List { 1, 2, 3, 4 }.Select(i => i * 2).Take(9).ToArray()); + Assert.Equal([6, 8], new List { 1, 2, 3, 4 }.Select(i => i * 2).Skip(2).ToArray()); Assert.Empty(new List { 1, 2, 3, 4 }.Select(i => i * 2).Skip(8).ToArray()); } [Fact] public void Select_SourceIsListSkipTakeToList() { - Assert.Equal(new[] { 2, 4, 6 }, new List { 1, 2, 3, 4 }.Select(i => i * 2).Take(3).ToList()); - Assert.Equal(new[] { 2, 4, 6, 8 }, new List { 1, 2, 3, 4 }.Select(i => i * 2).Take(9).ToList()); - Assert.Equal(new[] { 6, 8 }, new List { 1, 2, 3, 4 }.Select(i => i * 2).Skip(2).ToList()); + Assert.Equal([2, 4, 6], new List { 1, 2, 3, 4 }.Select(i => i * 2).Take(3).ToList()); + Assert.Equal([2, 4, 6, 8], new List { 1, 2, 3, 4 }.Select(i => i * 2).Take(9).ToList()); + Assert.Equal([6, 8], new List { 1, 2, 3, 4 }.Select(i => i * 2).Skip(2).ToList()); Assert.Empty(new List { 1, 2, 3, 4 }.Select(i => i * 2).Skip(8).ToList()); } [Fact] public void Select_SourceIsIPartitionToList() { - Assert.Equal(Array.Empty(), new List().Order().Select(i => i * 2).ToList()); - Assert.Equal(new[] { 2, 4, 6, 8 }, new List { 1, 2, 3, 4 }.Order().Select(i => i * 2).ToList()); + Assert.Equal([], new List().Order().Select(i => i * 2).ToList()); + Assert.Equal([2, 4, 6, 8], new List { 1, 2, 3, 4 }.Order().Select(i => i * 2).ToList()); } [Theory] @@ -1212,19 +1212,19 @@ public void MoveNextAfterDispose(IEnumerable source) foreach (IEnumerable equivalentSource in identityTransforms.Select(t => t(source))) { var result = equivalentSource.Select(i => i); - using (var e = result.GetEnumerator()) - { - while (e.MoveNext()) ; // Loop until we reach the end of the iterator, @ which pt it gets disposed. - Assert.False(e.MoveNext()); // MoveNext should not throw an exception after Dispose. - } + using var e = result.GetEnumerator(); + + while (e.MoveNext()) ; // Loop until we reach the end of the iterator, @ which pt it gets disposed. + Assert.False(e.MoveNext()); // MoveNext should not throw an exception after Dispose. + } } public static IEnumerable MoveNextAfterDisposeData() { - yield return new object[] { Array.Empty() }; - yield return new object[] { new int[1] }; - yield return new object[] { Enumerable.Range(1, 30) }; + yield return [Array.Empty()]; + yield return [new int[1]]; + yield return [Enumerable.Range(1, 30)]; } [Theory(Skip = SkipReason.Issue0082)] @@ -1265,7 +1265,7 @@ public static IEnumerable RunSelectorDuringCountData() foreach (var transform in transforms) { - yield return new object[] { transform(enumerable) }; + yield return [transform(enumerable)]; } } } diff --git a/tests/System.Linq.Tests/Tests/ZLinq/SequenceEqualTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/SequenceEqualTests.cs index 7b0cd5bf..fbdbb85d 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/SequenceEqualTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/SequenceEqualTests.cs @@ -33,8 +33,8 @@ public void SameResultsRepeatCallsStringQuery() [Fact] public void BothEmpty() { - int[] first = { }; - int[] second = { }; + int[] first = []; + int[] second = []; Assert.True(first.SequenceEqual(second)); Assert.True(FlipIsCollection(first).SequenceEqual(second)); @@ -45,8 +45,8 @@ public void BothEmpty() [Fact] public void MismatchInMiddle() { - int?[] first = { 1, 2, 3, 4 }; - int?[] second = { 1, 2, 6, 4 }; + int?[] first = [1, 2, 3, 4]; + int?[] second = [1, 2, 6, 4]; Assert.False(first.SequenceEqual(second)); Assert.False(FlipIsCollection(first).SequenceEqual(second)); @@ -57,8 +57,8 @@ public void MismatchInMiddle() [Fact] public void NullComparer() { - string[] first = { "Bob", "Tim", "Chris" }; - string[] second = { "Bbo", "mTi", "rishC" }; + string[] first = ["Bob", "Tim", "Chris"]; + string[] second = ["Bbo", "mTi", "rishC"]; Assert.False(first.SequenceEqual(second, null)); Assert.False(FlipIsCollection(first).SequenceEqual(second, null)); @@ -69,8 +69,8 @@ public void NullComparer() [Fact] public void CustomComparer() { - string[] first = { "Bob", "Tim", "Chris" }; - string[] second = { "Bbo", "mTi", "rishC" }; + string[] first = ["Bob", "Tim", "Chris"]; + string[] second = ["Bbo", "mTi", "rishC"]; Assert.True(first.SequenceEqual(second, new AnagramEqualityComparer())); Assert.True(FlipIsCollection(first).SequenceEqual(second, new AnagramEqualityComparer())); @@ -81,8 +81,8 @@ public void CustomComparer() [Fact] public void RunOnce() { - string[] first = { "Bob", "Tim", "Chris" }; - string[] second = { "Bbo", "mTi", "rishC" }; + string[] first = ["Bob", "Tim", "Chris"]; + string[] second = ["Bbo", "mTi", "rishC"]; Assert.True(first.RunOnce().SequenceEqual(second.RunOnce(), new AnagramEqualityComparer())); } @@ -90,8 +90,8 @@ public void RunOnce() [Fact] public void BothSingleNullExplicitComparer() { - string[] first = { null }; - string[] second = { null }; + string[] first = [null]; + string[] second = [null]; Assert.True(first.SequenceEqual(second, StringComparer.Ordinal)); Assert.True(FlipIsCollection(first).SequenceEqual(second, StringComparer.Ordinal)); @@ -102,8 +102,8 @@ public void BothSingleNullExplicitComparer() [Fact] public void BothMatchIncludingNullElements() { - int?[] first = { -6, null, 0, -4, 9, 10, 20 }; - int?[] second = { -6, null, 0, -4, 9, 10, 20 }; + int?[] first = [-6, null, 0, -4, 9, 10, 20]; + int?[] second = [-6, null, 0, -4, 9, 10, 20]; Assert.True(first.SequenceEqual(second)); Assert.True(FlipIsCollection(first).SequenceEqual(second)); @@ -114,8 +114,8 @@ public void BothMatchIncludingNullElements() [Fact] public void EmptyWithNonEmpty() { - int?[] first = { }; - int?[] second = { 2, 3, 4 }; + int?[] first = []; + int?[] second = [2, 3, 4]; Assert.False(first.SequenceEqual(second)); Assert.False(FlipIsCollection(first).SequenceEqual(second)); @@ -126,8 +126,8 @@ public void EmptyWithNonEmpty() [Fact] public void NonEmptyWithEmpty() { - int?[] first = { 2, 3, 4 }; - int?[] second = { }; + int?[] first = [2, 3, 4]; + int?[] second = []; Assert.False(first.SequenceEqual(second)); Assert.False(FlipIsCollection(first).SequenceEqual(second)); @@ -138,8 +138,8 @@ public void NonEmptyWithEmpty() [Fact] public void MismatchingSingletons() { - int?[] first = { 2 }; - int?[] second = { 4 }; + int?[] first = [2]; + int?[] second = [4]; Assert.False(first.SequenceEqual(second)); Assert.False(FlipIsCollection(first).SequenceEqual(second)); @@ -150,8 +150,8 @@ public void MismatchingSingletons() [Fact] public void MismatchOnFirst() { - int?[] first = { 1, 2, 3, 4, 5 }; - int?[] second = { 2, 2, 3, 4, 5 }; + int?[] first = [1, 2, 3, 4, 5]; + int?[] second = [2, 2, 3, 4, 5]; Assert.False(first.SequenceEqual(second)); Assert.False(FlipIsCollection(first).SequenceEqual(second)); @@ -163,8 +163,8 @@ public void MismatchOnFirst() [Fact] public void MismatchOnLast() { - int?[] first = { 1, 2, 3, 4, 4 }; - int?[] second = { 1, 2, 3, 4, 5 }; + int?[] first = [1, 2, 3, 4, 4]; + int?[] second = [1, 2, 3, 4, 5]; Assert.False(first.SequenceEqual(second)); Assert.False(FlipIsCollection(first).SequenceEqual(second)); @@ -175,8 +175,8 @@ public void MismatchOnLast() [Fact] public void SecondLargerThanFirst() { - int?[] first = { 1, 2, 3, 4 }; - int?[] second = { 1, 2, 3, 4, 4 }; + int?[] first = [1, 2, 3, 4]; + int?[] second = [1, 2, 3, 4, 4]; Assert.False(first.SequenceEqual(second)); Assert.False(FlipIsCollection(first).SequenceEqual(second)); @@ -187,8 +187,8 @@ public void SecondLargerThanFirst() [Fact] public void FirstLargerThanSecond() { - int?[] first = { 1, 2, 3, 4, 4 }; - int?[] second = { 1, 2, 3, 4 }; + int?[] first = [1, 2, 3, 4, 4]; + int?[] second = [1, 2, 3, 4]; Assert.False(first.SequenceEqual(second)); Assert.False(FlipIsCollection(first).SequenceEqual(second)); @@ -200,7 +200,7 @@ public void FirstLargerThanSecond() public void FirstSourceNull() { int[] first = null; - int[] second = { }; + int[] second = []; AssertExtensions.Throws(() => first.SequenceEqual(second)); } @@ -208,7 +208,7 @@ public void FirstSourceNull() [Fact] public void SecondSourceNull() { - int[] first = { }; + int[] first = []; int[] second = null; AssertExtensions.Throws("second", () => first.SequenceEqual(second)); @@ -220,7 +220,7 @@ public void ByteArrays_SpecialCasedButExpectedBehavior() AssertExtensions.Throws(() => ((byte[])null).SequenceEqual(new byte[1])); AssertExtensions.Throws("second", () => new byte[1].SequenceEqual(null)); - Assert.False(new byte[1].SequenceEqual(new byte[0])); + Assert.False(new byte[1].SequenceEqual([])); Assert.False(new byte[0].SequenceEqual(new byte[1])); var r = new Random(); diff --git a/tests/System.Linq.Tests/Tests/ZLinq/SingleOrDefaultTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/SingleOrDefaultTests.cs index 5c0894c5..fc8430aa 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/SingleOrDefaultTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/SingleOrDefaultTests.cs @@ -27,87 +27,39 @@ public void SameResultsRepeatCallsStringQuery() } [Fact] - public void EmptyIList() + public void Empty() { - int?[] source = { }; - int? expected = null; - - Assert.Equal(expected, source.SingleOrDefault()); - } - - [Fact] - public void EmptyIListDefault() - { - int?[] source = { }; - int expected = 5; - - Assert.Equal(expected, source.SingleOrDefault(5)); - } - - [Fact] - public void SingleElementIList() - { - int[] source = { 4 }; - int expected = 4; - - Assert.Equal(expected, source.SingleOrDefault()); + foreach (IEnumerable source in CreateSources([])) + { + Assert.Null(source.SingleOrDefault()); + Assert.Equal(5, source.SingleOrDefault(5)); + } } [Fact] - public void SingleElementIListDefault() + public void SingleElement() { - int[] source = { 4 }; - int expected = 4; - - Assert.Equal(expected, source.SingleOrDefault(5)); + foreach (IEnumerable source in CreateSources([4])) + { + Assert.Equal(4, source.SingleOrDefault()); + Assert.Equal(4, source.SingleOrDefault(5)); + } } [Fact] public void ManyElementIList() { - int[] source = { 4, 4, 4, 4, 4 }; - - Assert.Throws(() => source.SingleOrDefault()); - } - - [Fact] - public void ManyElementIListDefault() - { - int[] source = { 4, 4, 4, 4, 4 }; - - Assert.Throws(() => source.SingleOrDefault(5)); - } - - [Fact] - public void EmptyNotIList() - { - IEnumerable source = RepeatedNumberGuaranteedNotCollectionType(0, 0); - int expected = default(int); - - Assert.Equal(expected, source.SingleOrDefault()); - } - - [Fact] - public void SingleElementNotIList() - { - IEnumerable source = RepeatedNumberGuaranteedNotCollectionType(-5, 1); - int expected = -5; - - Assert.Equal(expected, source.SingleOrDefault()); - } - - [Fact] - public void ManyElementNotIList() - { - IEnumerable source = RepeatedNumberGuaranteedNotCollectionType(3, 5); - - Assert.Throws(() => source.SingleOrDefault()); + foreach (IEnumerable source in CreateSources([4, 4, 4, 4, 4])) + { + Assert.Throws(() => source.SingleOrDefault()); + Assert.Throws(() => source.SingleOrDefault(4)); + } } [Fact] public void EmptySourceWithPredicate() { - int[] source = { }; + int[] source = []; int expected = default(int); Assert.All(CreateSources(source), source => @@ -119,7 +71,7 @@ public void EmptySourceWithPredicate() [Fact] public void EmptySourceWithPredicateDefault() { - int[] source = { }; + int[] source = []; int expected = 5; Assert.All(CreateSources(source), source => @@ -131,7 +83,7 @@ public void EmptySourceWithPredicateDefault() [Fact] public void SingleElementPredicateTrue() { - int[] source = { 4 }; + int[] source = [4]; int expected = 4; Assert.All(CreateSources(source), source => @@ -143,7 +95,7 @@ public void SingleElementPredicateTrue() [Fact] public void SingleElementPredicateTrueDefault() { - int[] source = { 4 }; + int[] source = [4]; int expected = 4; Assert.All(CreateSources(source), source => @@ -155,7 +107,7 @@ public void SingleElementPredicateTrueDefault() [Fact] public void SingleElementPredicateFalse() { - int[] source = { 3 }; + int[] source = [3]; int expected = default(int); Assert.All(CreateSources(source), source => @@ -167,7 +119,7 @@ public void SingleElementPredicateFalse() [Fact] public void SingleElementPredicateFalseDefault() { - int[] source = { 3 }; + int[] source = [3]; int expected = 5; Assert.All(CreateSources(source), source => @@ -179,7 +131,7 @@ public void SingleElementPredicateFalseDefault() [Fact] public void ManyElementsPredicateFalseForAll() { - int[] source = { 3, 1, 7, 9, 13, 19 }; + int[] source = [3, 1, 7, 9, 13, 19]; int expected = default(int); Assert.All(CreateSources(source), source => @@ -191,7 +143,7 @@ public void ManyElementsPredicateFalseForAll() [Fact] public void ManyElementsPredicateFalseForAllDefault() { - int[] source = { 3, 1, 7, 9, 13, 19 }; + int[] source = [3, 1, 7, 9, 13, 19]; int expected = 5; Assert.All(CreateSources(source), source => @@ -203,7 +155,7 @@ public void ManyElementsPredicateFalseForAllDefault() [Fact] public void ManyElementsPredicateTrueForLast() { - int[] source = { 3, 1, 7, 9, 13, 19, 20 }; + int[] source = [3, 1, 7, 9, 13, 19, 20]; int expected = 20; Assert.All(CreateSources(source), source => @@ -215,7 +167,7 @@ public void ManyElementsPredicateTrueForLast() [Fact] public void ManyElementsPredicateTrueForLastDefault() { - int[] source = { 3, 1, 7, 9, 13, 19, 20 }; + int[] source = [3, 1, 7, 9, 13, 19, 20]; int expected = 20; Assert.All(CreateSources(source), source => @@ -227,7 +179,7 @@ public void ManyElementsPredicateTrueForLastDefault() [Fact] public void ManyElementsPredicateTrueForFirstAndFifth() { - int[] source = { 2, 3, 1, 7, 10, 13, 19, 9 }; + int[] source = [2, 3, 1, 7, 10, 13, 19, 9]; Assert.All(CreateSources(source), source => { @@ -238,7 +190,7 @@ public void ManyElementsPredicateTrueForFirstAndFifth() [Fact] public void ManyElementsPredicateTrueForFirstAndFifthDefault() { - int[] source = { 2, 3, 1, 7, 10, 13, 19, 9 }; + int[] source = [2, 3, 1, 7, 10, 13, 19, 9]; Assert.All(CreateSources(source), source => { @@ -287,7 +239,7 @@ public void ThrowsOnNullSourceDefault() [Fact] public void ThrowsOnNullPredicate() { - int[] source = { }; + int[] source = []; Func nullPredicate = null; AssertExtensions.Throws("predicate", () => source.SingleOrDefault(nullPredicate)); } @@ -295,7 +247,7 @@ public void ThrowsOnNullPredicate() [Fact] public void ThrowsOnNullPredicateDefault() { - int[] source = { }; + int[] source = []; Func nullPredicate = null; AssertExtensions.Throws("predicate", () => source.SingleOrDefault(nullPredicate, 5)); } diff --git a/tests/System.Linq.Tests/Tests/ZLinq/SingleTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/SingleTests.cs index 94c55212..2752f5be 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/SingleTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/SingleTests.cs @@ -37,59 +37,37 @@ public void SameResultsRepeatCallsIntQueryWithZero() } [Fact] - public void EmptyIList() + public void Empty() { - int[] source = { }; - - Assert.Throws(() => source.Single()); + foreach (IEnumerable source in CreateSources([])) + { + Assert.Throws(() => source.Single()); + } } [Fact] - public void SingleElementIList() + public void SingleElement() { - int[] source = { 4 }; int expected = 4; - - Assert.Equal(expected, source.Single()); - } - - [Fact] - public void ManyElementIList() - { - int[] source = { 4, 4, 4, 4, 4 }; - - Assert.Throws(() => source.Single()); - } - - [Fact] - public void EmptyNotIList() - { - IEnumerable source = RepeatedNumberGuaranteedNotCollectionType(0, 0); - - Assert.Throws(() => source.Single()); - } - - [Fact] - public void SingleElementNotIList() - { - IEnumerable source = RepeatedNumberGuaranteedNotCollectionType(-5, 1); - int expected = -5; - - Assert.Equal(expected, source.Single()); + foreach (IEnumerable source in CreateSources([4])) + { + Assert.Equal(expected, source.Single()); + } } [Fact] - public void ManyElementNotIList() + public void ManyElement() { - IEnumerable source = RepeatedNumberGuaranteedNotCollectionType(3, 5); - - Assert.Throws(() => source.Single()); + foreach (IEnumerable source in CreateSources([4, 4, 4, 4, 4])) + { + Assert.Throws(() => source.Single()); + } } [Fact] public void EmptySourceWithPredicate() { - int[] source = { }; + int[] source = []; Assert.All(CreateSources(source), source => { @@ -100,7 +78,7 @@ public void EmptySourceWithPredicate() [Fact] public void SingleElementPredicateTrue() { - int[] source = { 4 }; + int[] source = [4]; int expected = 4; Assert.All(CreateSources(source), source => @@ -112,7 +90,7 @@ public void SingleElementPredicateTrue() [Fact] public void SingleElementPredicateFalse() { - int[] source = { 3 }; + int[] source = [3]; Assert.All(CreateSources(source), source => { @@ -123,7 +101,7 @@ public void SingleElementPredicateFalse() [Fact] public void ManyElementsPredicateFalseForAll() { - int[] source = { 3, 1, 7, 9, 13, 19 }; + int[] source = [3, 1, 7, 9, 13, 19]; Assert.All(CreateSources(source), source => { @@ -134,7 +112,7 @@ public void ManyElementsPredicateFalseForAll() [Fact] public void ManyElementsPredicateTrueForLast() { - int[] source = { 3, 1, 7, 9, 13, 19, 20 }; + int[] source = [3, 1, 7, 9, 13, 19, 20]; int expected = 20; Assert.All(CreateSources(source), source => @@ -146,7 +124,7 @@ public void ManyElementsPredicateTrueForLast() [Fact] public void ManyElementsPredicateTrueForFirstAndLast() { - int[] source = { 2, 3, 1, 7, 9, 13, 19, 10 }; + int[] source = [2, 3, 1, 7, 9, 13, 19, 10]; Assert.All(CreateSources(source), source => { @@ -187,7 +165,7 @@ public void ThrowsOnNullSource() [Fact] public void ThrowsOnNullPredicate() { - int[] source = { }; + int[] source = []; Func nullPredicate = null; AssertExtensions.Throws("predicate", () => source.Single(nullPredicate)); } diff --git a/tests/System.Linq.Tests/Tests/ZLinq/SkipLastTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/SkipLastTests.cs index 26453927..320de6a1 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/SkipLastTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/SkipLastTests.cs @@ -2,7 +2,9 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Collections.Generic; +using System.Linq.Tests; using Xunit; +using static System.Linq.Tests.SkipTakeData; namespace ZLinq.Tests { @@ -15,28 +17,29 @@ public void SkipLastThrowsOnNull() } [Theory] - [MemberData(nameof(SkipTakeData.EnumerableData), MemberType = typeof(SkipTakeData))] + [MemberData(nameof(EnumerableData), MemberType = typeof(SkipTakeData))] public void SkipLast(IEnumerable source, int count) { - Assert.All(IdentityTransforms(), transform => - { - IEnumerable equivalent = transform(source); + int[] expected = source.Reverse().Skip(count).Reverse().ToArray(); - var expected = equivalent.Reverse().Skip(count).Reverse().ToArray(); - var actual = equivalent.SkipLast(count).ToArray(); + Assert.All(CreateSources(source), source => + { + var actual = source.SkipLast(count); Assert.Equal(expected, actual); - Assert.Equal(expected.Count(), actual.Count()); + + Assert.Equal(expected.Length, actual.Count()); Assert.Equal(expected, actual.ToArray()); Assert.Equal(expected, actual.ToList()); - Assert.Equal(expected.FirstOrDefault(), actual.FirstOrDefault()); Assert.Equal(expected.LastOrDefault(), actual.LastOrDefault()); - Assert.All(Enumerable.Range(0, expected.Count()), index => + if (expected.Length > 0) { - Assert.Equal(expected.ElementAt(index), actual.ElementAt(index)); - }); + Assert.Equal(expected[0], actual.ElementAt(0)); + Assert.Equal(expected[^1], actual.ElementAt(expected.Length - 1)); + Assert.Equal(expected[expected.Length / 2], actual.ElementAt(expected.Length / 2)); + } Assert.Equal(0, actual.ElementAtOrDefault(-1)); Assert.Equal(0, actual.ElementAtOrDefault(actual.Count())); @@ -44,7 +47,7 @@ public void SkipLast(IEnumerable source, int count) } [Theory] - [MemberData(nameof(SkipTakeData.EvaluationBehaviorData), MemberType = typeof(SkipTakeData))] + [MemberData(nameof(EvaluationBehaviorData), MemberType = typeof(SkipTakeData))] public void EvaluationBehavior(int count) { // We want to make sure no more than `count` items are ever evaluated ahead of the current position. @@ -58,26 +61,25 @@ public void EvaluationBehavior(int count) current: () => index, // Yield from 1 up to the limit, inclusive. dispose: () => index ^= int.MinValue); - var iterator = source.SkipLast(count).GetEnumerator(); + using (var iterator = source.SkipLast(count).GetEnumerator()) + { + Assert.Equal(0, index); // Nothing should be done before MoveNext is called. - Assert.Equal(0, index); // Nothing should be done before MoveNext is called. + for (int i = 1; i <= count; i++) + { + Assert.True(iterator.MoveNext()); + Assert.Equal(i, iterator.Current); + Assert.Equal(count + i, index); + } - for (int i = 1; i <= count; i++) - { - Assert.True(iterator.MoveNext()); - Assert.Equal(i, iterator.Current); - Assert.Equal(count + i, index); + Assert.False(iterator.MoveNext()); } - Assert.False(iterator.MoveNext()); - Assert.Equal(0, iterator.Current); - - iterator.Dispose(); Assert.Equal(int.MinValue, index & int.MinValue); } [Theory] - [MemberData(nameof(SkipTakeData.EnumerableData), MemberType = typeof(SkipTakeData))] + [MemberData(nameof(EnumerableData), MemberType = typeof(SkipTakeData))] public void RunOnce(IEnumerable source, int count) { var expected = source.SkipLast(count); @@ -94,7 +96,7 @@ public void List_ChangesAfterSkipLast_ChangesReflectedInResults() list.RemoveAt(4); list.RemoveAt(3); - Assert.Equal(new[] { 1 }, e.ToArray()); + Assert.Equal([1], e.ToArray()); } [Fact] @@ -106,7 +108,7 @@ public void List_Skip_ChangesAfterSkipLast_ChangesReflectedInResults() list.RemoveAt(4); - Assert.Equal(new[] { 2 }, e.ToArray()); + Assert.Equal([2], e.ToArray()); } } } diff --git a/tests/System.Linq.Tests/Tests/ZLinq/SkipTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/SkipTests.cs index 185f780f..97974cda 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/SkipTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/SkipTests.cs @@ -49,13 +49,13 @@ public void SkipNoneIList() [Fact] public void SkipExcessive() { - Assert.Equal(Enumerable.Empty(), NumberRangeGuaranteedNotCollectionType(0, 20).Skip(42)); + Assert.Equal([], NumberRangeGuaranteedNotCollectionType(0, 20).Skip(42)); } [Fact] public void SkipExcessiveIList() { - Assert.Equal(Enumerable.Empty(), NumberRangeGuaranteedNotCollectionType(0, 20).ToList().Skip(42)); + Assert.Equal([], NumberRangeGuaranteedNotCollectionType(0, 20).ToList().Skip(42)); } [Fact] @@ -86,9 +86,9 @@ public void SkipThrowsOnNullIList() [Fact] public void SkipOnEmpty() { - 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)); + Assert.Equal([], GuaranteeNotIList(Enumerable.Empty()).Skip(0)); + Assert.Equal([], GuaranteeNotIList(Enumerable.Empty()).Skip(-1)); + Assert.Equal([], GuaranteeNotIList(Enumerable.Empty()).Skip(1)); } [Fact] @@ -96,9 +96,9 @@ 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)); + Assert.Equal([], Enumerable.Empty().ToList().Skip(0)); + Assert.Equal([], Enumerable.Empty().ToList().Skip(-1)); + Assert.Equal([], Enumerable.Empty().ToList().Skip(1)); } [Fact] @@ -156,8 +156,8 @@ public void SameResultsRepeatCallsStringQueryIList() [Fact] public void SkipOne() { - int?[] source = { 3, 100, 4, null, 10 }; - int?[] expected = { 100, 4, null, 10 }; + int?[] source = [3, 100, 4, null, 10]; + int?[] expected = [100, 4, null, 10]; Assert.Equal(expected, source.Skip(1)); } @@ -165,8 +165,8 @@ public void SkipOne() [Fact] public void SkipOneNotIList() { - int?[] source = { 3, 100, 4, null, 10 }; - int?[] expected = { 100, 4, null, 10 }; + int?[] source = [3, 100, 4, null, 10]; + int?[] expected = [100, 4, null, 10]; Assert.Equal(expected, GuaranteeNotIList(source).Skip(1)); } @@ -174,8 +174,8 @@ public void SkipOneNotIList() [Fact] public void SkipAllButOne() { - int?[] source = { 3, 100, null, 4, 10 }; - int?[] expected = { 10 }; + int?[] source = [3, 100, null, 4, 10]; + int?[] expected = [10]; Assert.Equal(expected, source.Skip(source.Length - 1)); } @@ -183,8 +183,8 @@ public void SkipAllButOne() [Fact] public void SkipAllButOneNotIList() { - int?[] source = { 3, 100, null, 4, 10 }; - int?[] expected = { 10 }; + int?[] source = [3, 100, null, 4, 10]; + int?[] expected = [10]; Assert.Equal(expected, GuaranteeNotIList(source.Skip(source.Length - 1).ToArray())); } @@ -192,14 +192,14 @@ public void SkipAllButOneNotIList() [Fact] public void SkipOneMoreThanAll() { - int[] source = { 3, 100, 4, 10 }; + int[] source = [3, 100, 4, 10]; Assert.Empty(source.Skip(source.Length + 1)); } [Fact] public void SkipOneMoreThanAllNotIList() { - int[] source = { 3, 100, 4, 10 }; + int[] source = [3, 100, 4, 10]; Assert.Empty(GuaranteeNotIList(source).Skip(source.Length + 1)); } @@ -290,7 +290,7 @@ public void ElementAt() [Fact] public void ElementAtNotIList() { - var source = GuaranteeNotIList(new[] { 1, 2, 3, 4, 5, 6 }); + var source = GuaranteeNotIList([1, 2, 3, 4, 5, 6]); var remaining = source.Skip(2); Assert.Equal(3, remaining.ElementAt(0)); Assert.Equal(4, remaining.ElementAt(1)); @@ -314,7 +314,7 @@ public void ElementAtOrDefault() [Fact] public void ElementAtOrDefaultNotIList() { - var source = GuaranteeNotIList(new[] { 1, 2, 3, 4, 5, 6 }); + var source = GuaranteeNotIList([1, 2, 3, 4, 5, 6]); var remaining = source.Skip(2); Assert.Equal(3, remaining.ElementAtOrDefault(0)); Assert.Equal(4, remaining.ElementAtOrDefault(1)); @@ -336,7 +336,7 @@ public void First() [Fact] public void FirstNotIList() { - var source = GuaranteeNotIList(new[] { 1, 2, 3, 4, 5 }); + var source = GuaranteeNotIList([1, 2, 3, 4, 5]); Assert.Equal(1, source.Skip(0).First()); Assert.Equal(3, source.Skip(2).First()); Assert.Equal(5, source.Skip(4).First()); @@ -356,7 +356,7 @@ public void FirstOrDefault() [Fact] public void FirstOrDefaultNotIList() { - var source = GuaranteeNotIList(new[] { 1, 2, 3, 4, 5 }); + var source = GuaranteeNotIList([1, 2, 3, 4, 5]); Assert.Equal(1, source.Skip(0).FirstOrDefault()); Assert.Equal(3, source.Skip(2).FirstOrDefault()); Assert.Equal(5, source.Skip(4).FirstOrDefault()); @@ -376,7 +376,7 @@ public void Last() [Fact] public void LastNotList() { - var source = GuaranteeNotIList(new[] { 1, 2, 3, 4, 5 }); + var source = GuaranteeNotIList([1, 2, 3, 4, 5]); Assert.Equal(5, source.Skip(0).Last()); Assert.Equal(5, source.Skip(1).Last()); Assert.Equal(5, source.Skip(4).Last()); @@ -396,7 +396,7 @@ public void LastOrDefault() [Fact] public void LastOrDefaultNotList() { - var source = GuaranteeNotIList(new[] { 1, 2, 3, 4, 5 }); + var source = GuaranteeNotIList([1, 2, 3, 4, 5]); Assert.Equal(5, source.Skip(0).LastOrDefault()); Assert.Equal(5, source.Skip(1).LastOrDefault()); Assert.Equal(5, source.Skip(4).LastOrDefault()); @@ -407,8 +407,8 @@ public void LastOrDefaultNotList() public void ToArray() { var source = new[] { 1, 2, 3, 4, 5 }; - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Skip(0).ToArray()); - Assert.Equal(new[] { 2, 3, 4, 5 }, source.Skip(1).ToArray()); + Assert.Equal([1, 2, 3, 4, 5], source.Skip(0).ToArray()); + Assert.Equal([2, 3, 4, 5], source.Skip(1).ToArray()); Assert.Equal(5, source.Skip(4).ToArray().Single()); Assert.Empty(source.Skip(5).ToArray()); Assert.Empty(source.Skip(40).ToArray()); @@ -417,9 +417,9 @@ public void ToArray() [Fact] public void ToArrayNotList() { - var source = GuaranteeNotIList(new[] { 1, 2, 3, 4, 5 }); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Skip(0).ToArray()); - Assert.Equal(new[] { 2, 3, 4, 5 }, source.Skip(1).ToArray()); + var source = GuaranteeNotIList([1, 2, 3, 4, 5]); + Assert.Equal([1, 2, 3, 4, 5], source.Skip(0).ToArray()); + Assert.Equal([2, 3, 4, 5], source.Skip(1).ToArray()); Assert.Equal(5, source.Skip(4).ToArray().Single()); Assert.Empty(source.Skip(5).ToArray()); Assert.Empty(source.Skip(40).ToArray()); @@ -429,8 +429,8 @@ public void ToArrayNotList() public void ToList() { var source = new[] { 1, 2, 3, 4, 5 }; - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Skip(0).ToList()); - Assert.Equal(new[] { 2, 3, 4, 5 }, source.Skip(1).ToList()); + Assert.Equal([1, 2, 3, 4, 5], source.Skip(0).ToList()); + Assert.Equal([2, 3, 4, 5], source.Skip(1).ToList()); Assert.Equal(5, source.Skip(4).ToList().Single()); Assert.Empty(source.Skip(5).ToList()); Assert.Empty(source.Skip(40).ToList()); @@ -439,9 +439,9 @@ public void ToList() [Fact] public void ToListNotList() { - var source = GuaranteeNotIList(new[] { 1, 2, 3, 4, 5 }); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Skip(0).ToList()); - Assert.Equal(new[] { 2, 3, 4, 5 }, source.Skip(1).ToList()); + var source = GuaranteeNotIList([1, 2, 3, 4, 5]); + Assert.Equal([1, 2, 3, 4, 5], source.Skip(0).ToList()); + Assert.Equal([2, 3, 4, 5], source.Skip(1).ToList()); Assert.Equal(5, source.Skip(4).ToList().Single()); Assert.Empty(source.Skip(5).ToList()); Assert.Empty(source.Skip(40).ToList()); @@ -458,7 +458,7 @@ public void RepeatEnumerating() [Fact] public void RepeatEnumeratingNotList() { - var source = GuaranteeNotIList(new[] { 1, 2, 3, 4, 5 }); + var source = GuaranteeNotIList([1, 2, 3, 4, 5]); var remaining = source.Skip(1); Assert.Equal(remaining, remaining); } diff --git a/tests/System.Linq.Tests/Tests/ZLinq/SkipWhileTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/SkipWhileTests.cs index 86ef8fd1..2d0e3992 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/SkipWhileTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/SkipWhileTests.cs @@ -11,15 +11,15 @@ public class SkipWhileTests : EnumerableTests [Fact] public void Empty() { - Assert.Equal(Enumerable.Empty(), Enumerable.Empty().SkipWhile(i => i < 40)); - Assert.Equal(Enumerable.Empty(), Enumerable.Empty().SkipWhile((i, index) => i < 40)); + Assert.Equal([], Enumerable.Empty().SkipWhile(i => i < 40)); + Assert.Equal([], Enumerable.Empty().SkipWhile((i, index) => i < 40)); } [Fact] public void SkipWhileAllTrue() { - Assert.Equal(Enumerable.Empty(), Enumerable.Range(0, 20).SkipWhile(i => i < 40)); - Assert.Equal(Enumerable.Empty(), Enumerable.Range(0, 20).SkipWhile((i, idx) => i == idx)); + Assert.Equal([], Enumerable.Range(0, 20).SkipWhile(i => i < 40)); + Assert.Equal([], Enumerable.Range(0, 20).SkipWhile((i, idx) => i == idx)); } [Fact] @@ -97,8 +97,8 @@ public void SameResultsRepeatCallsStringQuery() [Fact] public void PredicateManyFalseOnSecond() { - int[] source = { 8, 3, 12, 4, 6, 10 }; - int[] expected = { 3, 12, 4, 6, 10 }; + int[] source = [8, 3, 12, 4, 6, 10]; + int[] expected = [3, 12, 4, 6, 10]; Assert.Equal(expected, source.SkipWhile(e => e % 2 == 0)); } @@ -106,8 +106,8 @@ public void PredicateManyFalseOnSecond() [Fact] public void PredicateManyFalseOnSecondIndex() { - int[] source = { 8, 3, 12, 4, 6, 10 }; - int[] expected = { 3, 12, 4, 6, 10 }; + int[] source = [8, 3, 12, 4, 6, 10]; + int[] expected = [3, 12, 4, 6, 10]; Assert.Equal(expected, source.SkipWhile((e, i) => e % 2 == 0)); } @@ -115,8 +115,8 @@ public void PredicateManyFalseOnSecondIndex() [Fact] public void PredicateTrueOnSecondFalseOnFirstAndOthers() { - int[] source = { 3, 2, 4, 12, 6 }; - int[] expected = { 3, 2, 4, 12, 6 }; + int[] source = [3, 2, 4, 12, 6]; + int[] expected = [3, 2, 4, 12, 6]; Assert.Equal(expected, source.SkipWhile(e => e % 2 == 0)); } @@ -124,8 +124,8 @@ public void PredicateTrueOnSecondFalseOnFirstAndOthers() [Fact] public void PredicateTrueOnSecondFalseOnFirstAndOthersIndex() { - int[] source = { 3, 2, 4, 12, 6 }; - int[] expected = { 3, 2, 4, 12, 6 }; + int[] source = [3, 2, 4, 12, 6]; + int[] expected = [3, 2, 4, 12, 6]; Assert.Equal(expected, source.SkipWhile((e, i) => e % 2 == 0)); } @@ -133,8 +133,8 @@ public void PredicateTrueOnSecondFalseOnFirstAndOthersIndex() [Fact] public void FirstExcludedByIndex() { - int[] source = { 6, 2, 5, 3, 8 }; - int[] expected = { 2, 5, 3, 8 }; + int[] source = [6, 2, 5, 3, 8]; + int[] expected = [2, 5, 3, 8]; Assert.Equal(expected, source.SkipWhile((element, index) => index == 0)); } @@ -142,8 +142,8 @@ public void FirstExcludedByIndex() [Fact] public void AllButLastExcludedByIndex() { - int[] source = { 6, 2, 5, 3, 8 }; - int[] expected = { 8 }; + int[] source = [6, 2, 5, 3, 8]; + int[] expected = [8]; Assert.Equal(expected, source.SkipWhile((element, index) => index < source.Length - 1)); } diff --git a/tests/System.Linq.Tests/Tests/ZLinq/SumTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/SumTests.cs index 488ee786..f3853c65 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/SumTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/SumTests.cs @@ -98,7 +98,7 @@ public void SumOfNullableOfDecimal_SourceIsNull_ArgumentNullExceptionThrown() [Fact] public void SumOfInt_SelectorIsNull_ArgumentNullExceptionThrown() { - IEnumerable sourceInt = Enumerable.Empty(); + IEnumerable sourceInt = []; Func selector = null; AssertExtensions.Throws("selector", () => sourceInt.Sum(selector)); } @@ -107,7 +107,7 @@ public void SumOfInt_SelectorIsNull_ArgumentNullExceptionThrown() public void SumOfNullableOfInt_SelectorIsNull_ArgumentNullExceptionThrown() { - IEnumerable sourceNullableInt = Enumerable.Empty(); + IEnumerable sourceNullableInt = []; Func selector = null; AssertExtensions.Throws("selector", () => sourceNullableInt.Sum(selector)); } @@ -115,7 +115,7 @@ public void SumOfNullableOfInt_SelectorIsNull_ArgumentNullExceptionThrown() [Fact] public void SumOfLong_SelectorIsNull_ArgumentNullExceptionThrown() { - IEnumerable sourceLong = Enumerable.Empty(); + IEnumerable sourceLong = []; Func selector = null; AssertExtensions.Throws("selector", () => sourceLong.Sum(selector)); } @@ -124,7 +124,7 @@ public void SumOfLong_SelectorIsNull_ArgumentNullExceptionThrown() public void SumOfNullableOfLong_SelectorIsNull_ArgumentNullExceptionThrown() { - IEnumerable sourceNullableLong = Enumerable.Empty(); + IEnumerable sourceNullableLong = []; Func selector = null; AssertExtensions.Throws("selector", () => sourceNullableLong.Sum(selector)); } @@ -132,7 +132,7 @@ public void SumOfNullableOfLong_SelectorIsNull_ArgumentNullExceptionThrown() [Fact] public void SumOfFloat_SelectorIsNull_ArgumentNullExceptionThrown() { - IEnumerable sourceFloat = Enumerable.Empty(); + IEnumerable sourceFloat = []; Func selector = null; AssertExtensions.Throws("selector", () => sourceFloat.Sum(selector)); } @@ -141,7 +141,7 @@ public void SumOfFloat_SelectorIsNull_ArgumentNullExceptionThrown() public void SumOfNullableOfFloat_SelectorIsNull_ArgumentNullExceptionThrown() { - IEnumerable sourceNullableFloat = Enumerable.Empty(); + IEnumerable sourceNullableFloat = []; Func selector = null; AssertExtensions.Throws("selector", () => sourceNullableFloat.Sum(selector)); } @@ -149,7 +149,7 @@ public void SumOfNullableOfFloat_SelectorIsNull_ArgumentNullExceptionThrown() [Fact] public void SumOfDouble_SelectorIsNull_ArgumentNullExceptionThrown() { - IEnumerable sourceDouble = Enumerable.Empty(); + IEnumerable sourceDouble = []; Func selector = null; AssertExtensions.Throws("selector", () => sourceDouble.Sum(selector)); } @@ -158,7 +158,7 @@ public void SumOfDouble_SelectorIsNull_ArgumentNullExceptionThrown() public void SumOfNullableOfDouble_SelectorIsNull_ArgumentNullExceptionThrown() { - IEnumerable sourceNullableDouble = Enumerable.Empty(); + IEnumerable sourceNullableDouble = []; Func selector = null; AssertExtensions.Throws("selector", () => sourceNullableDouble.Sum(selector)); } @@ -166,7 +166,7 @@ public void SumOfNullableOfDouble_SelectorIsNull_ArgumentNullExceptionThrown() [Fact] public void SumOfDecimal_SelectorIsNull_ArgumentNullExceptionThrown() { - IEnumerable sourceDecimal = Enumerable.Empty(); + IEnumerable sourceDecimal = []; Func selector = null; AssertExtensions.Throws("selector", () => sourceDecimal.Sum(selector)); } @@ -175,7 +175,7 @@ public void SumOfDecimal_SelectorIsNull_ArgumentNullExceptionThrown() public void SumOfNullableOfDecimal_SelectorIsNull_ArgumentNullExceptionThrown() { - IEnumerable sourceNullableDecimal = Enumerable.Empty(); + IEnumerable sourceNullableDecimal = []; Func selector = null; AssertExtensions.Throws("selector", () => sourceNullableDecimal.Sum(selector)); } @@ -187,7 +187,7 @@ public void SumOfNullableOfDecimal_SelectorIsNull_ArgumentNullExceptionThrown() [Fact] public void SumOfInt_SourceIsEmptyCollection_ZeroReturned() { - IEnumerable sourceInt = Enumerable.Empty(); + IEnumerable sourceInt = []; Assert.Equal(0, sourceInt.Sum()); Assert.Equal(0, sourceInt.Sum(x => x)); } @@ -196,7 +196,7 @@ public void SumOfInt_SourceIsEmptyCollection_ZeroReturned() public void SumOfNullableOfInt_SourceIsEmptyCollection_ZeroReturned() { - IEnumerable sourceNullableInt = Enumerable.Empty(); + IEnumerable sourceNullableInt = []; Assert.Equal(0, sourceNullableInt.Sum()); Assert.Equal(0, sourceNullableInt.Sum(x => x)); } @@ -204,7 +204,7 @@ public void SumOfNullableOfInt_SourceIsEmptyCollection_ZeroReturned() [Fact] public void SumOfLong_SourceIsEmptyCollection_ZeroReturned() { - IEnumerable sourceLong = Enumerable.Empty(); + IEnumerable sourceLong = []; Assert.Equal(0L, sourceLong.Sum()); Assert.Equal(0L, sourceLong.Sum(x => x)); } @@ -213,7 +213,7 @@ public void SumOfLong_SourceIsEmptyCollection_ZeroReturned() public void SumOfNullableOfLong_SourceIsEmptyCollection_ZeroReturned() { - IEnumerable sourceNullableLong = Enumerable.Empty(); + IEnumerable sourceNullableLong = []; Assert.Equal(0L, sourceNullableLong.Sum()); Assert.Equal(0L, sourceNullableLong.Sum(x => x)); } @@ -221,7 +221,7 @@ public void SumOfNullableOfLong_SourceIsEmptyCollection_ZeroReturned() [Fact] public void SumOfFloat_SourceIsEmptyCollection_ZeroReturned() { - IEnumerable sourceFloat = Enumerable.Empty(); + IEnumerable sourceFloat = []; Assert.Equal(0f, sourceFloat.Sum()); Assert.Equal(0f, sourceFloat.Sum(x => x)); } @@ -230,7 +230,7 @@ public void SumOfFloat_SourceIsEmptyCollection_ZeroReturned() public void SumOfNullableOfFloat_SourceIsEmptyCollection_ZeroReturned() { - IEnumerable sourceNullableFloat = Enumerable.Empty(); + IEnumerable sourceNullableFloat = []; Assert.Equal(0f, sourceNullableFloat.Sum()); Assert.Equal(0f, sourceNullableFloat.Sum(x => x)); } @@ -238,7 +238,7 @@ public void SumOfNullableOfFloat_SourceIsEmptyCollection_ZeroReturned() [Fact] public void SumOfDouble_SourceIsEmptyCollection_ZeroReturned() { - IEnumerable sourceDouble = Enumerable.Empty(); + IEnumerable sourceDouble = []; Assert.Equal(0d, sourceDouble.Sum()); Assert.Equal(0d, sourceDouble.Sum(x => x)); } @@ -247,7 +247,7 @@ public void SumOfDouble_SourceIsEmptyCollection_ZeroReturned() public void SumOfNullableOfDouble_SourceIsEmptyCollection_ZeroReturned() { - IEnumerable sourceNullableDouble = Enumerable.Empty(); + IEnumerable sourceNullableDouble = []; Assert.Equal(0d, sourceNullableDouble.Sum()); Assert.Equal(0d, sourceNullableDouble.Sum(x => x)); } @@ -255,7 +255,7 @@ public void SumOfNullableOfDouble_SourceIsEmptyCollection_ZeroReturned() [Fact] public void SumOfDecimal_SourceIsEmptyCollection_ZeroReturned() { - IEnumerable sourceDecimal = Enumerable.Empty(); + IEnumerable sourceDecimal = []; Assert.Equal(0m, sourceDecimal.Sum()); Assert.Equal(0m, sourceDecimal.Sum(x => x)); } @@ -264,7 +264,7 @@ public void SumOfDecimal_SourceIsEmptyCollection_ZeroReturned() public void SumOfNullableOfDecimal_SourceIsEmptyCollection_ZeroReturned() { - IEnumerable sourceNullableDecimal = Enumerable.Empty(); + IEnumerable sourceNullableDecimal = []; Assert.Equal(0m, sourceNullableDecimal.Sum()); Assert.Equal(0m, sourceNullableDecimal.Sum(x => x)); } @@ -276,7 +276,7 @@ public void SumOfNullableOfDecimal_SourceIsEmptyCollection_ZeroReturned() [Fact] public void SumOfInt_SourceIsNotEmpty_ProperSumReturned() { - IEnumerable sourceInt = new int[] { 1, -2, 3, -4 }; + IEnumerable sourceInt = [1, -2, 3, -4]; Assert.Equal(-2, sourceInt.Sum()); Assert.Equal(-2, sourceInt.Sum(x => x)); } @@ -285,7 +285,7 @@ public void SumOfInt_SourceIsNotEmpty_ProperSumReturned() public void SumOfNullableOfInt_SourceIsNotEmpty_ProperSumReturned() { - IEnumerable sourceNullableInt = new int?[] { 1, -2, null, 3, -4, null }; + IEnumerable sourceNullableInt = [1, -2, null, 3, -4, null]; Assert.Equal(-2, sourceNullableInt.Sum()); Assert.Equal(-2, sourceNullableInt.Sum(x => x)); } @@ -293,7 +293,7 @@ public void SumOfNullableOfInt_SourceIsNotEmpty_ProperSumReturned() [Fact] public void SumOfLong_SourceIsNotEmpty_ProperSumReturned() { - IEnumerable sourceLong = new long[] { 1L, -2L, 3L, -4L }; + IEnumerable sourceLong = [1L, -2L, 3L, -4L]; Assert.Equal(-2L, sourceLong.Sum()); Assert.Equal(-2L, sourceLong.Sum(x => x)); } @@ -302,7 +302,7 @@ public void SumOfLong_SourceIsNotEmpty_ProperSumReturned() public void SumOfNullableOfLong_SourceIsNotEmpty_ProperSumReturned() { - IEnumerable sourceNullableLong = new long?[] { 1L, -2L, null, 3L, -4L, null }; + IEnumerable sourceNullableLong = [1L, -2L, null, 3L, -4L, null]; Assert.Equal(-2L, sourceNullableLong.Sum()); Assert.Equal(-2L, sourceNullableLong.Sum(x => x)); } @@ -310,7 +310,7 @@ public void SumOfNullableOfLong_SourceIsNotEmpty_ProperSumReturned() [Fact] public void SumOfFloat_SourceIsNotEmpty_ProperSumReturned() { - IEnumerable sourceFloat = new float[] { 1f, 0.5f, -1f, 0.5f }; + IEnumerable sourceFloat = [1f, 0.5f, -1f, 0.5f]; Assert.Equal(1f, sourceFloat.Sum()); Assert.Equal(1f, sourceFloat.Sum(x => x)); } @@ -319,7 +319,7 @@ public void SumOfFloat_SourceIsNotEmpty_ProperSumReturned() public void SumOfNullableOfFloat_SourceIsNotEmpty_ProperSumReturned() { - IEnumerable sourceNullableFloat = new float?[] { 1f, 0.5f, null, -1f, 0.5f, null }; + IEnumerable sourceNullableFloat = [1f, 0.5f, null, -1f, 0.5f, null]; Assert.Equal(1f, sourceNullableFloat.Sum()); Assert.Equal(1f, sourceNullableFloat.Sum(x => x)); } @@ -327,7 +327,7 @@ public void SumOfNullableOfFloat_SourceIsNotEmpty_ProperSumReturned() [Fact] public void SumOfDouble_SourceIsNotEmpty_ProperSumReturned() { - IEnumerable sourceDouble = new double[] { 1d, 0.5d, -1d, 0.5d }; + IEnumerable sourceDouble = [1d, 0.5d, -1d, 0.5d]; Assert.Equal(1d, sourceDouble.Sum()); Assert.Equal(1d, sourceDouble.Sum(x => x)); } @@ -336,7 +336,7 @@ public void SumOfDouble_SourceIsNotEmpty_ProperSumReturned() public void SumOfNullableOfDouble_SourceIsNotEmpty_ProperSumReturned() { - IEnumerable sourceNullableDouble = new double?[] { 1d, 0.5d, null, -1d, 0.5d, null }; + IEnumerable sourceNullableDouble = [1d, 0.5d, null, -1d, 0.5d, null]; Assert.Equal(1d, sourceNullableDouble.Sum()); Assert.Equal(1d, sourceNullableDouble.Sum(x => x)); } @@ -344,7 +344,7 @@ public void SumOfNullableOfDouble_SourceIsNotEmpty_ProperSumReturned() [Fact] public void SumOfDecimal_SourceIsNotEmpty_ProperSumReturned() { - IEnumerable sourceDecimal = new decimal[] { 1m, 0.5m, -1m, 0.5m }; + IEnumerable sourceDecimal = [1m, 0.5m, -1m, 0.5m]; Assert.Equal(1m, sourceDecimal.Sum()); Assert.Equal(1m, sourceDecimal.Sum(x => x)); } @@ -353,7 +353,7 @@ public void SumOfDecimal_SourceIsNotEmpty_ProperSumReturned() public void SumOfNullableOfDecimal_SourceIsNotEmpty_ProperSumReturned() { - IEnumerable sourceNullableDecimal = new decimal?[] { 1m, 0.5m, null, -1m, 0.5m, null }; + IEnumerable sourceNullableDecimal = [1m, 0.5m, null, -1m, 0.5m, null]; Assert.Equal(1m, sourceNullableDecimal.Sum()); Assert.Equal(1m, sourceNullableDecimal.Sum(x => x)); } @@ -372,7 +372,7 @@ public static IEnumerable SumOverflowsVerticalVectorLanes() { for (int verticalOffset = 1; verticalOffset < 6; verticalOffset++) { - yield return new object[] { element, verticalOffset }; + yield return [element, verticalOffset]; } } } @@ -380,7 +380,7 @@ public static IEnumerable SumOverflowsVerticalVectorLanes() [Fact] public void SumOfInt_SourceSumsToOverflow_OverflowExceptionThrown() { - IEnumerable sourceInt = new int[] { int.MaxValue, 1 }; + IEnumerable sourceInt = [int.MaxValue, 1]; Assert.Throws(() => sourceInt.Sum()); Assert.Throws(() => sourceInt.Sum(x => x)); } @@ -419,7 +419,7 @@ public void SumOfInt_SourceSumsToOverflowVectorVertically_OverflowExceptionThrow [Fact] public void SumOfNullableOfInt_SourceSumsToOverflow_OverflowExceptionThrown() { - IEnumerable sourceNullableInt = new int?[] { int.MaxValue, null, 1 }; + IEnumerable sourceNullableInt = [int.MaxValue, null, 1]; Assert.Throws(() => sourceNullableInt.Sum()); Assert.Throws(() => sourceNullableInt.Sum(x => x)); } @@ -427,7 +427,7 @@ public void SumOfNullableOfInt_SourceSumsToOverflow_OverflowExceptionThrown() [Fact] public void SumOfLong_SourceSumsToOverflow_OverflowExceptionThrown() { - IEnumerable sourceLong = new long[] { long.MaxValue, 1L }; + IEnumerable sourceLong = [long.MaxValue, 1L]; Assert.Throws(() => sourceLong.Sum()); Assert.Throws(() => sourceLong.Sum(x => x)); } @@ -466,7 +466,7 @@ public void SumOfLong_SourceSumsToOverflowVectorVertically_OverflowExceptionThro [Fact] public void SumOfNullableOfLong_SourceSumsToOverflow_OverflowExceptionThrown() { - IEnumerable sourceNullableLong = new long?[] { long.MaxValue, null, 1 }; + IEnumerable sourceNullableLong = [long.MaxValue, null, 1]; Assert.Throws(() => sourceNullableLong.Sum()); Assert.Throws(() => sourceNullableLong.Sum(x => x)); } @@ -474,7 +474,7 @@ public void SumOfNullableOfLong_SourceSumsToOverflow_OverflowExceptionThrown() [Fact] public void SumOfFloat_SourceSumsToOverflow_InfinityReturned() { - IEnumerable sourceFloat = new float[] { float.MaxValue, float.MaxValue }; + IEnumerable sourceFloat = [float.MaxValue, float.MaxValue]; Assert.True(float.IsPositiveInfinity(sourceFloat.Sum())); Assert.True(float.IsPositiveInfinity(sourceFloat.Sum(x => x))); } @@ -482,7 +482,7 @@ public void SumOfFloat_SourceSumsToOverflow_InfinityReturned() [Fact] public void SumOfNullableOfFloat_SourceSumsToOverflow_InfinityReturned() { - IEnumerable sourceNullableFloat = new float?[] { float.MaxValue, null, float.MaxValue }; + IEnumerable sourceNullableFloat = [float.MaxValue, null, float.MaxValue]; Assert.True(float.IsPositiveInfinity(sourceNullableFloat.Sum().Value)); Assert.True(float.IsPositiveInfinity(sourceNullableFloat.Sum(x => x).Value)); } @@ -490,7 +490,7 @@ public void SumOfNullableOfFloat_SourceSumsToOverflow_InfinityReturned() [Fact] public void SumOfDouble_SourceSumsToOverflow_InfinityReturned() { - IEnumerable sourceDouble = new double[] { double.MaxValue, double.MaxValue }; + IEnumerable sourceDouble = [double.MaxValue, double.MaxValue]; Assert.True(double.IsPositiveInfinity(sourceDouble.Sum())); Assert.True(double.IsPositiveInfinity(sourceDouble.Sum(x => x))); } @@ -498,7 +498,7 @@ public void SumOfDouble_SourceSumsToOverflow_InfinityReturned() [Fact] public void SumOfNullableOfDouble_SourceSumsToOverflow_InfinityReturned() { - IEnumerable sourceNullableDouble = new double?[] { double.MaxValue, null, double.MaxValue }; + IEnumerable sourceNullableDouble = [double.MaxValue, null, double.MaxValue]; Assert.True(double.IsPositiveInfinity(sourceNullableDouble.Sum().Value)); Assert.True(double.IsPositiveInfinity(sourceNullableDouble.Sum(x => x).Value)); } @@ -506,7 +506,7 @@ public void SumOfNullableOfDouble_SourceSumsToOverflow_InfinityReturned() [Fact] public void SumOfDecimal_SourceSumsToOverflow_OverflowExceptionThrown() { - IEnumerable sourceDecimal = new decimal[] { decimal.MaxValue, 1m }; + IEnumerable sourceDecimal = [decimal.MaxValue, 1m]; Assert.Throws(() => sourceDecimal.Sum()); Assert.Throws(() => sourceDecimal.Sum(x => x)); } @@ -514,7 +514,7 @@ public void SumOfDecimal_SourceSumsToOverflow_OverflowExceptionThrown() [Fact] public void SumOfNullableOfDecimal_SourceSumsToOverflow_OverflowExceptionThrown() { - IEnumerable sourceNullableDecimal = new decimal?[] { decimal.MaxValue, null, 1m }; + IEnumerable sourceNullableDecimal = [decimal.MaxValue, null, 1m]; Assert.Throws(() => sourceNullableDecimal.Sum()); Assert.Throws(() => sourceNullableDecimal.Sum(x => x)); } @@ -532,14 +532,14 @@ where x > int.MinValue [Fact] public void SolitaryNullableSingle() { - float?[] source = { 20.51f }; + float?[] source = [20.51f]; Assert.Equal(source.FirstOrDefault(), source.Sum()); } [Fact] public void NaNFromSingles() { - float?[] source = { 20.45f, 0f, -10.55f, float.NaN }; + float?[] source = [20.45f, 0f, -10.55f, float.NaN]; Assert.True(float.IsNaN(source.Sum().Value)); } @@ -552,7 +552,7 @@ public void NullableSingleAllNull() [Fact] public void NullableSingleToNegativeInfinity() { - float?[] source = { -float.MaxValue, -float.MaxValue }; + float?[] source = [-float.MaxValue, -float.MaxValue]; Assert.True(float.IsNegativeInfinity(source.Sum().Value)); } @@ -570,14 +570,14 @@ public void NullableSingleFromSelector() [Fact] public void SolitaryInt32() { - int[] source = { 20 }; + int[] source = [20]; Assert.Equal(source.FirstOrDefault(), source.Sum()); } [Fact] public void OverflowInt32Negative() { - int[] source = { -int.MaxValue, 0, -5, -20 }; + int[] source = [-int.MaxValue, 0, -5, -20]; Assert.Throws(() => source.Sum()); } @@ -596,7 +596,7 @@ public void Int32FromSelector() [Fact] public void SolitaryNullableInt32() { - int?[] source = { -9 }; + int?[] source = [-9]; Assert.Equal(source.FirstOrDefault(), source.Sum()); } @@ -609,7 +609,7 @@ public void NullableInt32AllNull() [Fact] public void NullableInt32NegativeOverflow() { - int?[] source = { -int.MaxValue, 0, -5, null, null, -20 }; + int?[] source = [-int.MaxValue, 0, -5, null, null, -20]; Assert.Throws(() => source.Sum()); } @@ -640,14 +640,14 @@ public void RunOnce() [Fact] public void SolitaryInt64() { - long[] source = { int.MaxValue + 20L }; + long[] source = [int.MaxValue + 20L]; Assert.Equal(source.FirstOrDefault(), source.Sum()); } [Fact] public void NullableInt64NegativeOverflow() { - long[] source = { -long.MaxValue, 0, -5, 20, -16 }; + long[] source = [-long.MaxValue, 0, -5, 20, -16]; Assert.Throws(() => source.Sum()); } @@ -666,7 +666,7 @@ public void Int64FromSelector() [Fact] public void SolitaryNullableInt64() { - long?[] source = { -int.MaxValue - 20L }; + long?[] source = [-int.MaxValue - 20L]; Assert.Equal(source.FirstOrDefault(), source.Sum()); } @@ -679,7 +679,7 @@ public void NullableInt64AllNull() [Fact] public void Int64NegativeOverflow() { - long?[] source = { -long.MaxValue, 0, -5, -20, null, null }; + long?[] source = [-long.MaxValue, 0, -5, -20, null, null]; Assert.Throws(() => source.Sum()); } @@ -698,21 +698,21 @@ public void NullableInt64FromSelector() [Fact] public void SolitaryDouble() { - double[] source = { 20.51 }; + double[] source = [20.51]; Assert.Equal(source.FirstOrDefault(), source.Sum()); } [Fact] public void DoubleWithNaN() { - double[] source = { 20.45, 0, -10.55, double.NaN }; + double[] source = [20.45, 0, -10.55, double.NaN]; Assert.True(double.IsNaN(source.Sum())); } [Fact] public void DoubleToNegativeInfinity() { - double[] source = { -double.MaxValue, -double.MaxValue }; + double[] source = [-double.MaxValue, -double.MaxValue]; Assert.True(double.IsNegativeInfinity(source.Sum())); } @@ -732,7 +732,7 @@ public void DoubleFromSelector() [Fact] public void SolitaryNullableDouble() { - double?[] source = { 20.51 }; + double?[] source = [20.51]; Assert.Equal(source.FirstOrDefault(), source.Sum()); } @@ -745,7 +745,7 @@ public void NullableDoubleAllNull() [Fact] public void NullableDoubleToNegativeInfinity() { - double?[] source = { -double.MaxValue, -double.MaxValue }; + double?[] source = [-double.MaxValue, -double.MaxValue]; Assert.True(double.IsNegativeInfinity(source.Sum().Value)); } @@ -764,14 +764,14 @@ public void NullableDoubleFromSelector() [Fact] public void SolitaryDecimal() { - decimal[] source = { 20.51m }; + decimal[] source = [20.51m]; Assert.Equal(source.FirstOrDefault(), source.Sum()); } [Fact] public void DecimalNegativeOverflow() { - decimal[] source = { -decimal.MaxValue, -decimal.MaxValue }; + decimal[] source = [-decimal.MaxValue, -decimal.MaxValue]; Assert.Throws(() => source.Sum()); } @@ -790,7 +790,7 @@ public void DecimalFromSelector() [Fact] public void SolitaryNullableDecimal() { - decimal?[] source = { 20.51m }; + decimal?[] source = [20.51m]; Assert.Equal(source.FirstOrDefault(), source.Sum()); } @@ -803,7 +803,7 @@ public void NullableDecimalAllNull() [Fact] public void NullableDecimalNegativeOverflow() { - decimal?[] source = { -decimal.MaxValue, -decimal.MaxValue }; + decimal?[] source = [-decimal.MaxValue, -decimal.MaxValue]; Assert.Throws(() => source.Sum()); } @@ -822,14 +822,14 @@ public void NullableDecimalFromSelector() [Fact] public void SolitarySingle() { - float[] source = { 20.51f }; + float[] source = [20.51f]; Assert.Equal(source.FirstOrDefault(), source.Sum()); } [Fact] public void SingleToNegativeInfinity() { - float[] source = { -float.MaxValue, -float.MaxValue }; + float[] source = [-float.MaxValue, -float.MaxValue]; Assert.True(float.IsNegativeInfinity(source.Sum())); } diff --git a/tests/System.Linq.Tests/Tests/ZLinq/TakeLastTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/TakeLastTests.cs index 93cc3e2d..d7ce079c 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/TakeLastTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/TakeLastTests.cs @@ -2,7 +2,9 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Collections.Generic; +using System.Linq.Tests; using Xunit; +using static System.Linq.Tests.SkipTakeData; namespace ZLinq.Tests { @@ -15,28 +17,29 @@ public void SkipLastThrowsOnNull() } [Theory] - [MemberData(nameof(SkipTakeData.EnumerableData), MemberType = typeof(SkipTakeData))] + [MemberData(nameof(EnumerableData), MemberType = typeof(SkipTakeData))] public void TakeLast(IEnumerable source, int count) { - Assert.All(IdentityTransforms(), transform => - { - IEnumerable equivalent = transform(source); + int[] expected = source.Reverse().Take(count).Reverse().ToArray(); - var expected = equivalent.Reverse().Take(count).Reverse().ToArray(); - var actual = equivalent.TakeLast(count).ToArray(); + Assert.All(CreateSources(source), source => + { + var actual = source.TakeLast(count); Assert.Equal(expected, actual); - Assert.Equal(expected.Count(), actual.Count()); + + Assert.Equal(expected.Length, actual.Count()); Assert.Equal(expected, actual.ToArray()); Assert.Equal(expected, actual.ToList()); - Assert.Equal(expected.FirstOrDefault(), actual.FirstOrDefault()); Assert.Equal(expected.LastOrDefault(), actual.LastOrDefault()); - Assert.All(Enumerable.Range(0, expected.Count()), index => + if (expected.Length > 0) { - Assert.Equal(expected.ElementAt(index), actual.ElementAt(index)); - }); + Assert.Equal(expected[0], actual.ElementAt(0)); + Assert.Equal(expected[^1], actual.ElementAt(expected.Length - 1)); + Assert.Equal(expected[expected.Length / 2], actual.ElementAt(expected.Length / 2)); + } Assert.Equal(0, actual.ElementAtOrDefault(-1)); Assert.Equal(0, actual.ElementAtOrDefault(actual.Count())); @@ -44,7 +47,7 @@ public void TakeLast(IEnumerable source, int count) } [Theory(Skip = SkipReason.Issue0081)] - [MemberData(nameof(SkipTakeData.EvaluationBehaviorData), MemberType = typeof(SkipTakeData))] + [MemberData(nameof(EvaluationBehaviorData), MemberType = typeof(SkipTakeData))] public void EvaluationBehavior(int count) { int index = 0; @@ -55,33 +58,33 @@ public void EvaluationBehavior(int count) current: () => index, // Yield from 1 up to the limit, inclusive. dispose: () => index ^= int.MinValue); - var iterator = source.TakeLast(count).GetEnumerator(); - Assert.Equal(0, index); // Nothing should be done before MoveNext is called. - - for (int i = 1; i <= count; i++) + using (var iterator = source.TakeLast(count).GetEnumerator()) { - Assert.True(iterator.MoveNext()); - Assert.Equal(count + i, iterator.Current); + Assert.Equal(0, index); // Nothing should be done before MoveNext is called. - // After the first MoveNext call to the enumerator, everything should be evaluated and the enumerator - // should be disposed. - Assert.Equal(int.MinValue, index & int.MinValue); - Assert.Equal(limit + 1, index & int.MaxValue); - } + for (int i = 1; i <= count; i++) + { + Assert.True(iterator.MoveNext()); + Assert.Equal(count + i, iterator.Current); - Assert.False(iterator.MoveNext()); - Assert.Equal(0, iterator.Current); + // After the first MoveNext call to the enumerator, everything should be evaluated and the enumerator + // should be disposed. + Assert.Equal(int.MinValue, index & int.MinValue); + Assert.Equal(limit + 1, index & int.MaxValue); + } + + Assert.False(iterator.MoveNext()); + } // Unlike SkipLast, TakeLast can tell straightaway that it can return a sequence with no elements if count <= 0. // The enumerable it returns is a specialized empty iterator that has no connections to the source. Hence, // after MoveNext returns false under those circumstances, it won't invoke Dispose on our enumerator. int expected = count <= 0 ? 0 : int.MinValue; - iterator.Dispose(); Assert.Equal(expected, index & int.MinValue); } [Theory] - [MemberData(nameof(SkipTakeData.EnumerableData), MemberType = typeof(SkipTakeData))] + [MemberData(nameof(EnumerableData), MemberType = typeof(SkipTakeData))] public void RunOnce(IEnumerable source, int count) { var expected = source.TakeLast(count); @@ -98,7 +101,7 @@ public void List_ChangesAfterTakeLast_ChangesReflectedInResults() list.RemoveAt(0); list.RemoveAt(0); - Assert.Equal(new[] { 3, 4, 5 }, e.ToArray()); + Assert.Equal([3, 4, 5], e.ToArray()); } [Fact] @@ -110,7 +113,7 @@ public void List_Skip_ChangesAfterTakeLast_ChangesReflectedInResults() list.RemoveAt(0); - Assert.Equal(new[] { 3, 4, 5 }, e.ToArray()); + Assert.Equal([3, 4, 5], e.ToArray()); } } } diff --git a/tests/System.Linq.Tests/Tests/ZLinq/TakeTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/TakeTests.cs index 31225175..122909d4 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/TakeTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/TakeTests.cs @@ -72,156 +72,92 @@ public void SameResultsRepeatCallsStringQueryIList() [Fact] public void SourceEmptyCountPositive() { - var source = new int[] { }; - Assert.Empty(source.Take(5)); - - Assert.Empty(source.Take(0..5)); - Assert.Empty(source.Take(^5..5)); - Assert.Empty(source.Take(0..^0)); - Assert.Empty(source.Take(^5..^0)); - } - - [Fact] - public void SourceEmptyCountPositiveNotIList() - { - var source = NumberRangeGuaranteedNotCollectionType(0, 0); - Assert.Empty(source.Take(5)); + foreach (IEnumerable source in CreateSources([])) + { + Assert.Empty(source.Take(5)); - Assert.Empty(source.Take(0..5)); - Assert.Empty(source.Take(^5..5)); - Assert.Empty(source.Take(0..^0)); - Assert.Empty(source.Take(^5..^0)); + Assert.Empty(source.Take(0..5)); + Assert.Empty(source.Take(^5..5)); + Assert.Empty(source.Take(0..^0)); + Assert.Empty(source.Take(^5..^0)); + } } [Fact] public void SourceNonEmptyCountNegative() { - var source = new[] { 2, 5, 9, 1 }; - Assert.Empty(source.Take(-5)); - - Assert.Empty(source.Take(^9..0)); - } - - [Fact] - public void SourceNonEmptyCountNegativeNotIList() - { - var source = ForceNotCollection(new[] { 2, 5, 9, 1 }); - Assert.Empty(source.Take(-5)); - - Assert.Empty(source.Take(^9..0)); + foreach (IEnumerable source in CreateSources([2, 5, 9, 1])) + { + Assert.Empty(source.Take(-5)); + Assert.Empty(source.Take(^9..0)); + } } [Fact] public void SourceNonEmptyCountZero() { - var source = new[] { 2, 5, 9, 1 }; - Assert.Empty(source.Take(0)); - - Assert.Empty(source.Take(0..0)); - Assert.Empty(source.Take(^4..0)); - Assert.Empty(source.Take(0..^4)); - Assert.Empty(source.Take(^4..^4)); - } - - [Fact] - public void SourceNonEmptyCountZeroNotIList() - { - var source = ForceNotCollection(new[] { 2, 5, 9, 1 }); - Assert.Empty(source.Take(0)); + foreach (IEnumerable source in CreateSources([2, 5, 9, 1])) + { + Assert.Empty(source.Take(0)); - Assert.Empty(source.Take(0..0)); - Assert.Empty(source.Take(^4..0)); - Assert.Empty(source.Take(0..^4)); - Assert.Empty(source.Take(^4..^4)); + Assert.Empty(source.Take(0..0)); + Assert.Empty(source.Take(^4..0)); + Assert.Empty(source.Take(0..^4)); + Assert.Empty(source.Take(^4..^4)); + } } [Fact] public void SourceNonEmptyCountOne() { - var source = new[] { 2, 5, 9, 1 }; - int[] expected = { 2 }; - - Assert.Equal(expected, source.Take(1)); + int[] expected = [2]; - Assert.Equal(expected, source.Take(0..1)); - Assert.Equal(expected, source.Take(^4..1)); - Assert.Equal(expected, source.Take(0..^3)); - Assert.Equal(expected, source.Take(^4..^3)); - } - - [Fact] - public void SourceNonEmptyCountOneNotIList() - { - var source = ForceNotCollection(new[] { 2, 5, 9, 1 }); - int[] expected = { 2 }; - - Assert.Equal(expected, source.Take(1)); + foreach (IEnumerable source in CreateSources([2, 5, 9, 1])) + { + Assert.Equal(expected, source.Take(1)); - Assert.Equal(expected, source.Take(0..1)); - Assert.Equal(expected, source.Take(^4..1)); - Assert.Equal(expected, source.Take(0..^3)); - Assert.Equal(expected, source.Take(^4..^3)); + Assert.Equal(expected, source.Take(0..1)); + Assert.Equal(expected, source.Take(^4..1)); + Assert.Equal(expected, source.Take(0..^3)); + Assert.Equal(expected, source.Take(^4..^3)); + } } [Fact] public void SourceNonEmptyTakeAllExactly() { - var source = new[] { 2, 5, 9, 1 }; - - Assert.Equal(source, source.Take(source.Length)); - - Assert.Equal(source, source.Take(0..source.Length)); - Assert.Equal(source, source.Take(^source.Length..source.Length)); - Assert.Equal(source, source.Take(0..^0)); - Assert.Equal(source, source.Take(^source.Length..^0)); - } - - [Fact] - public void SourceNonEmptyTakeAllExactlyNotIList() - { - var source = ForceNotCollection(new[] { 2, 5, 9, 1 }); - - Assert.Equal(source, source.Take(source.Count())); + foreach (IEnumerable source in CreateSources([2, 5, 9, 1])) + { + Assert.Equal(source, source.Take(4)); - Assert.Equal(source, source.Take(0..source.Count())); - Assert.Equal(source, source.Take(^source.Count()..source.Count())); - Assert.Equal(source, source.Take(0..^0)); - Assert.Equal(source, source.Take(^source.Count()..^0)); + Assert.Equal(source, source.Take(0..4)); + Assert.Equal(source, source.Take(^4..4)); + Assert.Equal(source, source.Take(0..^0)); + Assert.Equal(source, source.Take(^4..^0)); + } } [Fact] public void SourceNonEmptyTakeAllButOne() { - var source = new[] { 2, 5, 9, 1 }; - int[] expected = { 2, 5, 9 }; + int[] expected = [2, 5, 9]; - Assert.Equal(expected, source.Take(3)); + foreach (IEnumerable source in CreateSources([2, 5, 9, 1])) + { + Assert.Equal(expected, source.Take(3)); - Assert.Equal(expected, source.Take(0..3)); - Assert.Equal(expected, source.Take(^4..3)); - Assert.Equal(expected, source.Take(0..^1)); - Assert.Equal(expected, source.Take(^4..^1)); + Assert.Equal(expected, source.Take(0..3)); + Assert.Equal(expected, source.Take(^4..3)); + Assert.Equal(expected, source.Take(0..^1)); + Assert.Equal(expected, source.Take(^4..^1)); + } } [Fact] public void RunOnce() { var source = new[] { 2, 5, 9, 1 }; - int[] expected = { 2, 5, 9 }; - - Assert.Equal(expected, source.RunOnce().Take(3)); - - Assert.Equal(expected, source.RunOnce().Take(0..3)); - Assert.Equal(expected, source.RunOnce().Take(^4..3)); - Assert.Equal(expected, source.RunOnce().Take(0..^1)); - Assert.Equal(expected, source.RunOnce().Take(^4..^1)); - } - - [Fact] - public void SourceNonEmptyTakeAllButOneNotIList() - { - var source = ForceNotCollection(new[] { 2, 5, 9, 1 }); - int[] expected = { 2, 5, 9 }; + int[] expected = [2, 5, 9]; Assert.Equal(expected, source.RunOnce().Take(3)); @@ -234,23 +170,13 @@ public void SourceNonEmptyTakeAllButOneNotIList() [Fact] public void SourceNonEmptyTakeExcessive() { - var source = new int?[] { 2, 5, null, 9, 1 }; - - Assert.Equal(source, source.Take(source.Length + 1)); - - Assert.Equal(source, source.Take(0..(source.Length + 1))); - Assert.Equal(source, source.Take(^(source.Length + 1)..(source.Length + 1))); - } - - [Fact] - public void SourceNonEmptyTakeExcessiveNotIList() - { - var source = ForceNotCollection(new int?[] { 2, 5, null, 9, 1 }); - - Assert.Equal(source, source.Take(source.Count() + 1)); + foreach (IEnumerable source in CreateSources([2, 5, null, 9, 1])) + { + Assert.Equal(source, source.Take(5)); - Assert.Equal(source, source.Take(0..(source.Count() + 1))); - Assert.Equal(source, source.Take(^(source.Count() + 1)..(source.Count() + 1))); + Assert.Equal(source, source.Take(0..5)); + Assert.Equal(source, source.Take(^6..6)); + } } [Fact] @@ -314,81 +240,36 @@ public void Count() Assert.Empty(NumberRangeGuaranteedNotCollectionType(0, 3).Take(^3..^3)); } - [Fact(Skip = SkipReason.EnumeratorBehaviorDifference)] - public void ForcedToEnumeratorDoesntEnumerateIList() - { - var valueEnumerable1 = NumberRangeGuaranteedNotCollectionType(0, 3).ToList().Take(2); - // Don't insist on this behaviour, but check it's correct if it happens - using var en1 = valueEnumerable1.Enumerator; - Assert.False(en1.TryGetNext(out _)); - - var valueEnumerable2 = NumberRangeGuaranteedNotCollectionType(0, 3).ToList().Take(0..2); - using var en2 = valueEnumerable2.Enumerator; - Assert.False(en2.TryGetNext(out _)); - - var valueEnumerable3 = NumberRangeGuaranteedNotCollectionType(0, 3).ToList().Take(^3..2); - using var en3 = valueEnumerable3.Enumerator; - Assert.False(en3.TryGetNext(out _)); - - var valueEnumerable4 = NumberRangeGuaranteedNotCollectionType(0, 3).ToList().Take(0..^1); - using var en4 = valueEnumerable4.Enumerator; - Assert.False(en4.TryGetNext(out _)); - - var valueEnumerable5 = NumberRangeGuaranteedNotCollectionType(0, 3).ToList().Take(^3..^1); - using var en5 = valueEnumerable5.Enumerator; - Assert.False(en5.TryGetNext(out _)); - } - [Fact] public void FollowWithTake() { - var source = new[] { 5, 6, 7, 8 }; var expected = new[] { 5, 6 }; - Assert.Equal(expected, source.Take(5).Take(3).Take(2).Take(40)); - Assert.Equal(expected, source.Take(0..5).Take(0..3).Take(0..2).Take(0..40)); - Assert.Equal(expected, source.Take(^4..5).Take(^4..3).Take(^3..2).Take(^2..40)); - Assert.Equal(expected, source.Take(0..^0).Take(0..^1).Take(0..^1).Take(0..^0)); - Assert.Equal(expected, source.Take(^4..^0).Take(^4..^1).Take(^3..^1).Take(^2..^0)); - } - - [Fact] - public void FollowWithTakeNotIList() - { - var source = NumberRangeGuaranteedNotCollectionType(5, 4); - var expected = new[] { 5, 6 }; - Assert.Equal(expected, source.Take(5).Take(3).Take(2)); + foreach (IEnumerable source in CreateSources([5, 6, 7, 8])) + { + Assert.Equal(expected, source.Take(5).Take(3).Take(2).Take(40)); - Assert.Equal(expected, source.Take(0..5).Take(0..3).Take(0..2)); - Assert.Equal(expected, source.Take(^4..5).Take(^4..3).Take(^3..2)); - Assert.Equal(expected, source.Take(0..^0).Take(0..^1).Take(0..^1)); - Assert.Equal(expected, source.Take(^4..^0).Take(^4..^1).Take(^3..^1)); + Assert.Equal(expected, source.Take(0..5).Take(0..3).Take(0..2).Take(0..40)); + Assert.Equal(expected, source.Take(^4..5).Take(^4..3).Take(^3..2).Take(^2..40)); + Assert.Equal(expected, source.Take(0..^0).Take(0..^1).Take(0..^1).Take(0..^0)); + Assert.Equal(expected, source.Take(^4..^0).Take(^4..^1).Take(^3..^1).Take(^2..^0)); + } } [Fact] public void FollowWithSkip() { - var source = new[] { 1, 2, 3, 4, 5, 6 }; var expected = new[] { 3, 4, 5 }; - Assert.Equal(expected, source.Take(5).Skip(2).Skip(-4)); - - Assert.Equal(expected, source.Take(0..5).Skip(2).Skip(-4)); - Assert.Equal(expected, source.Take(^6..5).Skip(2).Skip(-4)); - Assert.Equal(expected, source.Take(0..^1).Skip(2).Skip(-4)); - Assert.Equal(expected, source.Take(^6..^1).Skip(2).Skip(-4)); - } - [Fact] - public void FollowWithSkipNotIList() - { - var source = NumberRangeGuaranteedNotCollectionType(1, 6); - var expected = new[] { 3, 4, 5 }; - Assert.Equal(expected, source.Take(5).Skip(2).Skip(-4)); + foreach (IEnumerable source in CreateSources([1, 2, 3, 4, 5, 6])) + { + Assert.Equal(expected, source.Take(5).Skip(2).Skip(-4)); - Assert.Equal(expected, source.Take(0..5).Skip(2).Skip(-4)); - Assert.Equal(expected, source.Take(^6..5).Skip(2).Skip(-4)); - Assert.Equal(expected, source.Take(0..^1).Skip(2).Skip(-4)); - Assert.Equal(expected, source.Take(^6..^1).Skip(2).Skip(-4)); + Assert.Equal(expected, source.Take(0..5).Skip(2).Skip(-4)); + Assert.Equal(expected, source.Take(^6..5).Skip(2).Skip(-4)); + Assert.Equal(expected, source.Take(0..^1).Skip(2).Skip(-4)); + Assert.Equal(expected, source.Take(^6..^1).Skip(2).Skip(-4)); + } } [Fact] @@ -426,41 +307,6 @@ public void ElementAt() Assert.Throws(() => source.Take(^6..^3).ElementAt(3)); } - [Fact] - public void ElementAtNotIList() - { - var source = ForceNotCollection(new[] { 1, 2, 3, 4, 5, 6 }); - var taken0 = source.Take(3); - Assert.Equal(1, taken0.ElementAt(0)); - Assert.Equal(3, taken0.ElementAt(2)); - Assert.Throws(() => source.Take(3).ElementAt(-1)); - Assert.Throws(() => source.Take(3).ElementAt(3)); - - var taken1 = source.Take(0..3); - Assert.Equal(1, taken1.ElementAt(0)); - Assert.Equal(3, taken1.ElementAt(2)); - Assert.Throws(() => source.Take(0..3).ElementAt(-1)); - Assert.Throws(() => source.Take(0..3).ElementAt(3)); - - var taken2 = source.Take(^6..3); - Assert.Equal(1, taken2.ElementAt(0)); - Assert.Equal(3, taken2.ElementAt(2)); - Assert.Throws(() => source.Take(^6..3).ElementAt(-1)); - Assert.Throws(() => source.Take(^6..3).ElementAt(3)); - - var taken3 = source.Take(0..^3); - Assert.Equal(1, taken3.ElementAt(0)); - Assert.Equal(3, taken3.ElementAt(2)); - Assert.Throws(() => source.Take(0..^3).ElementAt(-1)); - Assert.Throws(() => source.Take(0..^3).ElementAt(3)); - - var taken4 = source.Take(^6..^3); - Assert.Equal(1, taken4.ElementAt(0)); - Assert.Equal(3, taken4.ElementAt(2)); - Assert.Throws(() => source.Take(^6..^3).ElementAt(-1)); - Assert.Throws(() => source.Take(^6..^3).ElementAt(3)); - } - [Fact] public void ElementAtOrDefault() { @@ -496,590 +342,304 @@ public void ElementAtOrDefault() Assert.Equal(0, taken4.ElementAtOrDefault(3)); } - [Fact] - public void ElementAtOrDefaultNotIList() - { - var source = ForceNotCollection(new[] { 1, 2, 3, 4, 5, 6 }); - var taken0 = source.Take(3); - Assert.Equal(1, taken0.ElementAtOrDefault(0)); - Assert.Equal(3, taken0.ElementAtOrDefault(2)); - Assert.Equal(0, taken0.ElementAtOrDefault(-1)); - Assert.Equal(0, taken0.ElementAtOrDefault(3)); - - var taken1 = source.Take(0..3); - Assert.Equal(1, taken1.ElementAtOrDefault(0)); - Assert.Equal(3, taken1.ElementAtOrDefault(2)); - Assert.Equal(0, taken1.ElementAtOrDefault(-1)); - Assert.Equal(0, taken1.ElementAtOrDefault(3)); - - var taken2 = source.Take(^6..3); - Assert.Equal(1, taken2.ElementAtOrDefault(0)); - Assert.Equal(3, taken2.ElementAtOrDefault(2)); - Assert.Equal(0, taken2.ElementAtOrDefault(-1)); - Assert.Equal(0, taken2.ElementAtOrDefault(3)); - - var taken3 = source.Take(0..^3); - Assert.Equal(1, taken3.ElementAtOrDefault(0)); - Assert.Equal(3, taken3.ElementAtOrDefault(2)); - Assert.Equal(0, taken3.ElementAtOrDefault(-1)); - Assert.Equal(0, taken3.ElementAtOrDefault(3)); - - var taken4 = source.Take(^6..^3); - Assert.Equal(1, taken4.ElementAtOrDefault(0)); - Assert.Equal(3, taken4.ElementAtOrDefault(2)); - Assert.Equal(0, taken4.ElementAtOrDefault(-1)); - Assert.Equal(0, taken4.ElementAtOrDefault(3)); - } - [Fact] public void First() { - var source = new[] { 1, 2, 3, 4, 5 }; - Assert.Equal(1, source.Take(1).First()); - Assert.Equal(1, source.Take(4).First()); - Assert.Equal(1, source.Take(40).First()); - Assert.Throws(() => source.Take(0).First()); - Assert.Throws(() => source.Skip(5).Take(10).First()); - - Assert.Equal(1, source.Take(0..1).First()); - Assert.Equal(1, source.Take(0..4).First()); - Assert.Equal(1, source.Take(0..40).First()); - Assert.Throws(() => source.Take(0..0).First()); - Assert.Throws(() => source.Skip(5).Take(0..10).First()); - - Assert.Equal(1, source.Take(^5..1).First()); - Assert.Equal(1, source.Take(^5..4).First()); - Assert.Equal(1, source.Take(^5..40).First()); - Assert.Throws(() => source.Take(^5..0).First()); - Assert.Throws(() => source.Skip(5).Take(^5..10).First()); - - Assert.Equal(1, source.Take(0..^4).First()); - Assert.Equal(1, source.Take(0..^1).First()); - Assert.Equal(1, source.Take(0..^0).First()); - Assert.Throws(() => source.Take(0..^5).First()); - Assert.Throws(() => source.Skip(5).Take(0..^5).First()); - - Assert.Equal(1, source.Take(^5..^4).First()); - Assert.Equal(1, source.Take(^5..^1).First()); - Assert.Equal(1, source.Take(^5..^0).First()); - Assert.Throws(() => source.Take(^5..^5).First()); - Assert.Throws(() => source.Skip(5).Take(^10..^0).First()); - } - - [Fact] - public void FirstNotIList() - { - var source = ForceNotCollection(new[] { 1, 2, 3, 4, 5 }); - Assert.Equal(1, source.Take(1).First()); - Assert.Equal(1, source.Take(4).First()); - Assert.Equal(1, source.Take(40).First()); - Assert.Throws(() => source.Take(0).First()); - Assert.Throws(() => source.Skip(5).Take(10).First()); - - Assert.Equal(1, source.Take(0..1).First()); - Assert.Equal(1, source.Take(0..4).First()); - Assert.Equal(1, source.Take(0..40).First()); - Assert.Throws(() => source.Take(0..0).First()); - Assert.Throws(() => source.Skip(5).Take(0..10).First()); - - Assert.Equal(1, source.Take(^5..1).First()); - Assert.Equal(1, source.Take(^5..4).First()); - Assert.Equal(1, source.Take(^5..40).First()); - Assert.Throws(() => source.Take(^5..0).First()); - Assert.Throws(() => source.Skip(5).Take(^5..10).First()); - - Assert.Equal(1, source.Take(0..^4).First()); - Assert.Equal(1, source.Take(0..^1).First()); - Assert.Equal(1, source.Take(0..^0).First()); - Assert.Throws(() => source.Take(0..^5).First()); - Assert.Throws(() => source.Skip(5).Take(0..^5).First()); - - Assert.Equal(1, source.Take(^5..^4).First()); - Assert.Equal(1, source.Take(^5..^1).First()); - Assert.Equal(1, source.Take(^5..^0).First()); - Assert.Throws(() => source.Take(^5..^5).First()); - Assert.Throws(() => source.Skip(5).Take(^10..^0).First()); + foreach (IEnumerable source in CreateSources([1, 2, 3, 4, 5])) + { + Assert.Equal(1, source.Take(1).First()); + Assert.Equal(1, source.Take(4).First()); + Assert.Equal(1, source.Take(40).First()); + Assert.Throws(() => source.Take(0).First()); + Assert.Throws(() => source.Skip(5).Take(10).First()); + + Assert.Equal(1, source.Take(0..1).First()); + Assert.Equal(1, source.Take(0..4).First()); + Assert.Equal(1, source.Take(0..40).First()); + Assert.Throws(() => source.Take(0..0).First()); + Assert.Throws(() => source.Skip(5).Take(0..10).First()); + + Assert.Equal(1, source.Take(^5..1).First()); + Assert.Equal(1, source.Take(^5..4).First()); + Assert.Equal(1, source.Take(^5..40).First()); + Assert.Throws(() => source.Take(^5..0).First()); + Assert.Throws(() => source.Skip(5).Take(^5..10).First()); + + Assert.Equal(1, source.Take(0..^4).First()); + Assert.Equal(1, source.Take(0..^1).First()); + Assert.Equal(1, source.Take(0..^0).First()); + Assert.Throws(() => source.Take(0..^5).First()); + Assert.Throws(() => source.Skip(5).Take(0..^5).First()); + + Assert.Equal(1, source.Take(^5..^4).First()); + Assert.Equal(1, source.Take(^5..^1).First()); + Assert.Equal(1, source.Take(^5..^0).First()); + Assert.Throws(() => source.Take(^5..^5).First()); + Assert.Throws(() => source.Skip(5).Take(^10..^0).First()); + } } [Fact] public void FirstOrDefault() { - var source = new[] { 1, 2, 3, 4, 5 }; - Assert.Equal(1, source.Take(1).FirstOrDefault()); - Assert.Equal(1, source.Take(4).FirstOrDefault()); - Assert.Equal(1, source.Take(40).FirstOrDefault()); - Assert.Equal(0, source.Take(0).FirstOrDefault()); - Assert.Equal(0, source.Skip(5).Take(10).FirstOrDefault()); - - Assert.Equal(1, source.Take(0..1).FirstOrDefault()); - Assert.Equal(1, source.Take(0..4).FirstOrDefault()); - Assert.Equal(1, source.Take(0..40).FirstOrDefault()); - Assert.Equal(0, source.Take(0..0).FirstOrDefault()); - Assert.Equal(0, source.Skip(5).Take(0..10).FirstOrDefault()); - - Assert.Equal(1, source.Take(^5..1).FirstOrDefault()); - Assert.Equal(1, source.Take(^5..4).FirstOrDefault()); - Assert.Equal(1, source.Take(^5..40).FirstOrDefault()); - Assert.Equal(0, source.Take(^5..0).FirstOrDefault()); - Assert.Equal(0, source.Skip(5).Take(^10..10).FirstOrDefault()); - - Assert.Equal(1, source.Take(0..^4).FirstOrDefault()); - Assert.Equal(1, source.Take(0..^1).FirstOrDefault()); - Assert.Equal(1, source.Take(0..^0).FirstOrDefault()); - Assert.Equal(0, source.Take(0..^5).FirstOrDefault()); - Assert.Equal(0, source.Skip(5).Take(0..^10).FirstOrDefault()); - - Assert.Equal(1, source.Take(^5..^4).FirstOrDefault()); - Assert.Equal(1, source.Take(^5..^1).FirstOrDefault()); - Assert.Equal(1, source.Take(^5..^0).FirstOrDefault()); - Assert.Equal(0, source.Take(^5..^5).FirstOrDefault()); - Assert.Equal(0, source.Skip(5).Take(^10..^0).FirstOrDefault()); - } - - [Fact] - public void FirstOrDefaultNotIList() - { - var source = ForceNotCollection(new[] { 1, 2, 3, 4, 5 }); - Assert.Equal(1, source.Take(1).FirstOrDefault()); - Assert.Equal(1, source.Take(4).FirstOrDefault()); - Assert.Equal(1, source.Take(40).FirstOrDefault()); - Assert.Equal(0, source.Take(0).FirstOrDefault()); - Assert.Equal(0, source.Skip(5).Take(10).FirstOrDefault()); - - Assert.Equal(1, source.Take(0..1).FirstOrDefault()); - Assert.Equal(1, source.Take(0..4).FirstOrDefault()); - Assert.Equal(1, source.Take(0..40).FirstOrDefault()); - Assert.Equal(0, source.Take(0..0).FirstOrDefault()); - Assert.Equal(0, source.Skip(5).Take(0..10).FirstOrDefault()); - - Assert.Equal(1, source.Take(^5..1).FirstOrDefault()); - Assert.Equal(1, source.Take(^5..4).FirstOrDefault()); - Assert.Equal(1, source.Take(^5..40).FirstOrDefault()); - Assert.Equal(0, source.Take(^5..0).FirstOrDefault()); - Assert.Equal(0, source.Skip(5).Take(^10..10).FirstOrDefault()); - - Assert.Equal(1, source.Take(0..^4).FirstOrDefault()); - Assert.Equal(1, source.Take(0..^1).FirstOrDefault()); - Assert.Equal(1, source.Take(0..^0).FirstOrDefault()); - Assert.Equal(0, source.Take(0..^5).FirstOrDefault()); - Assert.Equal(0, source.Skip(5).Take(0..^10).FirstOrDefault()); - - Assert.Equal(1, source.Take(^5..^4).FirstOrDefault()); - Assert.Equal(1, source.Take(^5..^1).FirstOrDefault()); - Assert.Equal(1, source.Take(^5..^0).FirstOrDefault()); - Assert.Equal(0, source.Take(^5..^5).FirstOrDefault()); - Assert.Equal(0, source.Skip(5).Take(^10..^0).FirstOrDefault()); + foreach (IEnumerable source in CreateSources([1, 2, 3, 4, 5])) + { + Assert.Equal(1, source.Take(1).FirstOrDefault()); + Assert.Equal(1, source.Take(4).FirstOrDefault()); + Assert.Equal(1, source.Take(40).FirstOrDefault()); + Assert.Equal(0, source.Take(0).FirstOrDefault()); + Assert.Equal(0, source.Skip(5).Take(10).FirstOrDefault()); + + Assert.Equal(1, source.Take(0..1).FirstOrDefault()); + Assert.Equal(1, source.Take(0..4).FirstOrDefault()); + Assert.Equal(1, source.Take(0..40).FirstOrDefault()); + Assert.Equal(0, source.Take(0..0).FirstOrDefault()); + Assert.Equal(0, source.Skip(5).Take(0..10).FirstOrDefault()); + + Assert.Equal(1, source.Take(^5..1).FirstOrDefault()); + Assert.Equal(1, source.Take(^5..4).FirstOrDefault()); + Assert.Equal(1, source.Take(^5..40).FirstOrDefault()); + Assert.Equal(0, source.Take(^5..0).FirstOrDefault()); + Assert.Equal(0, source.Skip(5).Take(^10..10).FirstOrDefault()); + + Assert.Equal(1, source.Take(0..^4).FirstOrDefault()); + Assert.Equal(1, source.Take(0..^1).FirstOrDefault()); + Assert.Equal(1, source.Take(0..^0).FirstOrDefault()); + Assert.Equal(0, source.Take(0..^5).FirstOrDefault()); + Assert.Equal(0, source.Skip(5).Take(0..^10).FirstOrDefault()); + + Assert.Equal(1, source.Take(^5..^4).FirstOrDefault()); + Assert.Equal(1, source.Take(^5..^1).FirstOrDefault()); + Assert.Equal(1, source.Take(^5..^0).FirstOrDefault()); + Assert.Equal(0, source.Take(^5..^5).FirstOrDefault()); + Assert.Equal(0, source.Skip(5).Take(^10..^0).FirstOrDefault()); + } } [Fact] public void Last() { - var source = new[] { 1, 2, 3, 4, 5 }; - Assert.Equal(1, source.Take(1).Last()); - Assert.Equal(5, source.Take(5).Last()); - Assert.Equal(5, source.Take(40).Last()); - Assert.Throws(() => source.Take(0).Last()); - Assert.Throws(() => Array.Empty().Take(40).Last()); - - Assert.Equal(1, source.Take(0..1).Last()); - Assert.Equal(5, source.Take(0..5).Last()); - Assert.Equal(5, source.Take(0..40).Last()); - Assert.Throws(() => source.Take(0..0).Last()); - Assert.Throws(() => Array.Empty().Take(0..40).Last()); - - Assert.Equal(1, source.Take(^5..1).Last()); - Assert.Equal(5, source.Take(^5..5).Last()); - Assert.Equal(5, source.Take(^5..40).Last()); - Assert.Throws(() => source.Take(^5..0).Last()); - Assert.Throws(() => Array.Empty().Take(^5..40).Last()); - - Assert.Equal(1, source.Take(0..^4).Last()); - Assert.Equal(5, source.Take(0..^0).Last()); - Assert.Equal(5, source.Take(3..^0).Last()); - Assert.Throws(() => source.Take(0..^5).Last()); - Assert.Throws(() => Array.Empty().Take(0..^0).Last()); - - Assert.Equal(1, source.Take(^5..^4).Last()); - Assert.Equal(5, source.Take(^5..^0).Last()); - Assert.Equal(5, source.Take(^5..^0).Last()); - Assert.Throws(() => source.Take(^5..^5).Last()); - Assert.Throws(() => Array.Empty().Take(^40..^0).Last()); - } - - [Fact] - public void LastNotIList() - { - var source = ForceNotCollection(new[] { 1, 2, 3, 4, 5 }); - Assert.Equal(1, source.Take(1).Last()); - Assert.Equal(5, source.Take(5).Last()); - Assert.Equal(5, source.Take(40).Last()); - Assert.Throws(() => source.Take(0).Last()); - Assert.Throws(() => ForceNotCollection(Array.Empty()).Take(40).Last()); - - Assert.Equal(1, source.Take(0..1).Last()); - Assert.Equal(5, source.Take(0..5).Last()); - Assert.Equal(5, source.Take(0..40).Last()); - Assert.Throws(() => source.Take(0..0).Last()); - Assert.Throws(() => ForceNotCollection(Array.Empty()).Take(0..40).Last()); - - Assert.Equal(1, source.Take(^5..1).Last()); - Assert.Equal(5, source.Take(^5..5).Last()); - Assert.Equal(5, source.Take(^5..40).Last()); - Assert.Throws(() => source.Take(^5..0).Last()); - Assert.Throws(() => ForceNotCollection(Array.Empty()).Take(^5..40).Last()); - - Assert.Equal(1, source.Take(0..^4).Last()); - Assert.Equal(5, source.Take(0..^0).Last()); - Assert.Equal(5, source.Take(3..^0).Last()); - Assert.Throws(() => source.Take(0..^5).Last()); - Assert.Throws(() => ForceNotCollection(Array.Empty()).Take(0..^0).Last()); - - Assert.Equal(1, source.Take(^5..^4).Last()); - Assert.Equal(5, source.Take(^5..^0).Last()); - Assert.Equal(5, source.Take(^5..^0).Last()); - Assert.Throws(() => source.Take(^5..^5).Last()); - Assert.Throws(() => ForceNotCollection(Array.Empty()).Take(^40..^0).Last()); + foreach (var source in CreateSources([1, 2, 3, 4, 5])) + { + Assert.Equal(1, source.Take(1).Last()); + Assert.Equal(2, source.Take(2).Last()); + Assert.Equal(3, source.Take(3).Last()); + Assert.Equal(4, source.Take(4).Last()); + Assert.Equal(5, source.Take(5).Last()); + Assert.Equal(5, source.Take(6).Last()); + Assert.Equal(5, source.Take(40).Last()); + Assert.Throws(() => source.Take(0).Last()); + Assert.Throws(() => Array.Empty().Take(40).Last()); + + Assert.Equal(1, source.Take(0..1).Last()); + Assert.Equal(5, source.Take(0..5).Last()); + Assert.Equal(5, source.Take(0..40).Last()); + Assert.Throws(() => source.Take(0..0).Last()); + Assert.Throws(() => Array.Empty().Take(0..40).Last()); + + Assert.Equal(1, source.Take(^5..1).Last()); + Assert.Equal(5, source.Take(^5..5).Last()); + Assert.Equal(5, source.Take(^5..40).Last()); + Assert.Throws(() => source.Take(^5..0).Last()); + Assert.Throws(() => Array.Empty().Take(^5..40).Last()); + + Assert.Equal(1, source.Take(0..^4).Last()); + Assert.Equal(5, source.Take(0..^0).Last()); + Assert.Equal(5, source.Take(3..^0).Last()); + Assert.Throws(() => source.Take(0..^5).Last()); + Assert.Throws(() => Array.Empty().Take(0..^0).Last()); + + Assert.Equal(1, source.Take(^5..^4).Last()); + Assert.Equal(5, source.Take(^5..^0).Last()); + Assert.Equal(5, source.Take(^5..^0).Last()); + Assert.Throws(() => source.Take(^5..^5).Last()); + Assert.Throws(() => Array.Empty().Take(^40..^0).Last()); + } } [Fact] public void LastOrDefault() { - var source = new[] { 1, 2, 3, 4, 5 }; - Assert.Equal(1, source.Take(1).LastOrDefault()); - Assert.Equal(5, source.Take(5).LastOrDefault()); - Assert.Equal(5, source.Take(40).LastOrDefault()); - Assert.Equal(0, source.Take(0).LastOrDefault()); - Assert.Equal(0, Array.Empty().Take(40).LastOrDefault()); - - Assert.Equal(1, source.Take(0..1).LastOrDefault()); - Assert.Equal(5, source.Take(0..5).LastOrDefault()); - Assert.Equal(5, source.Take(0..40).LastOrDefault()); - Assert.Equal(0, source.Take(0..0).LastOrDefault()); - Assert.Equal(0, Array.Empty().Take(0..40).LastOrDefault()); - - Assert.Equal(1, source.Take(^5..1).LastOrDefault()); - Assert.Equal(5, source.Take(^5..5).LastOrDefault()); - Assert.Equal(5, source.Take(^5..40).LastOrDefault()); - Assert.Equal(0, source.Take(^5..0).LastOrDefault()); - Assert.Equal(0, Array.Empty().Take(^5..40).LastOrDefault()); - - Assert.Equal(1, source.Take(0..^4).LastOrDefault()); - Assert.Equal(5, source.Take(0..^0).LastOrDefault()); - Assert.Equal(5, source.Take(3..^0).LastOrDefault()); - Assert.Equal(0, source.Take(0..^5).LastOrDefault()); - Assert.Equal(0, Array.Empty().Take(0..^0).LastOrDefault()); - - Assert.Equal(1, source.Take(^5..^4).LastOrDefault()); - Assert.Equal(5, source.Take(^5..^0).LastOrDefault()); - Assert.Equal(5, source.Take(^40..^0).LastOrDefault()); - Assert.Equal(0, source.Take(^5..^5).LastOrDefault()); - Assert.Equal(0, Array.Empty().Take(^40..^0).LastOrDefault()); - } - - [Fact] - public void LastOrDefaultNotIList() - { - var source = ForceNotCollection(new[] { 1, 2, 3, 4, 5 }); - Assert.Equal(1, source.Take(1).LastOrDefault()); - Assert.Equal(5, source.Take(5).LastOrDefault()); - Assert.Equal(5, source.Take(40).LastOrDefault()); - Assert.Equal(0, source.Take(0).LastOrDefault()); - Assert.Equal(0, ForceNotCollection(Array.Empty()).Take(40).LastOrDefault()); - - Assert.Equal(1, source.Take(0..1).LastOrDefault()); - Assert.Equal(5, source.Take(0..5).LastOrDefault()); - Assert.Equal(5, source.Take(0..40).LastOrDefault()); - Assert.Equal(0, source.Take(0..0).LastOrDefault()); - Assert.Equal(0, ForceNotCollection(Array.Empty()).Take(0..40).LastOrDefault()); - - Assert.Equal(1, source.Take(^5..1).LastOrDefault()); - Assert.Equal(5, source.Take(^5..5).LastOrDefault()); - Assert.Equal(5, source.Take(^5..40).LastOrDefault()); - Assert.Equal(0, source.Take(^5..0).LastOrDefault()); - Assert.Equal(0, ForceNotCollection(Array.Empty()).Take(^5..40).LastOrDefault()); - - Assert.Equal(1, source.Take(0..^4).LastOrDefault()); - Assert.Equal(5, source.Take(0..^0).LastOrDefault()); - Assert.Equal(5, source.Take(3..^0).LastOrDefault()); - Assert.Equal(0, source.Take(0..^5).LastOrDefault()); - Assert.Equal(0, ForceNotCollection(Array.Empty()).Take(0..^0).LastOrDefault()); - - Assert.Equal(1, source.Take(^5..^4).LastOrDefault()); - Assert.Equal(5, source.Take(^5..^0).LastOrDefault()); - Assert.Equal(5, source.Take(^40..^0).LastOrDefault()); - Assert.Equal(0, source.Take(^5..^5).LastOrDefault()); - Assert.Equal(0, ForceNotCollection(Array.Empty()).Take(^40..^0).LastOrDefault()); + foreach (var source in CreateSources([1, 2, 3, 4, 5])) + { + Assert.Equal(1, source.Take(1).LastOrDefault()); + Assert.Equal(2, source.Take(2).LastOrDefault()); + Assert.Equal(3, source.Take(3).LastOrDefault()); + Assert.Equal(4, source.Take(4).LastOrDefault()); + Assert.Equal(5, source.Take(5).LastOrDefault()); + Assert.Equal(5, source.Take(6).LastOrDefault()); + Assert.Equal(5, source.Take(40).LastOrDefault()); + Assert.Equal(0, source.Take(0).LastOrDefault()); + Assert.Equal(0, Array.Empty().Take(40).LastOrDefault()); + + Assert.Equal(1, source.Take(0..1).LastOrDefault()); + Assert.Equal(5, source.Take(0..5).LastOrDefault()); + Assert.Equal(5, source.Take(0..40).LastOrDefault()); + Assert.Equal(0, source.Take(0..0).LastOrDefault()); + Assert.Equal(0, Array.Empty().Take(0..40).LastOrDefault()); + + Assert.Equal(1, source.Take(^5..1).LastOrDefault()); + Assert.Equal(5, source.Take(^5..5).LastOrDefault()); + Assert.Equal(5, source.Take(^5..40).LastOrDefault()); + Assert.Equal(0, source.Take(^5..0).LastOrDefault()); + Assert.Equal(0, Array.Empty().Take(^5..40).LastOrDefault()); + + Assert.Equal(1, source.Take(0..^4).LastOrDefault()); + Assert.Equal(5, source.Take(0..^0).LastOrDefault()); + Assert.Equal(5, source.Take(3..^0).LastOrDefault()); + Assert.Equal(0, source.Take(0..^5).LastOrDefault()); + Assert.Equal(0, Array.Empty().Take(0..^0).LastOrDefault()); + + Assert.Equal(1, source.Take(^5..^4).LastOrDefault()); + Assert.Equal(5, source.Take(^5..^0).LastOrDefault()); + Assert.Equal(5, source.Take(^40..^0).LastOrDefault()); + Assert.Equal(0, source.Take(^5..^5).LastOrDefault()); + Assert.Equal(0, Array.Empty().Take(^40..^0).LastOrDefault()); + } } [Fact] public void ToArray() { - var source = new[] { 1, 2, 3, 4, 5 }; - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(5).ToArray()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(6).ToArray()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(40).ToArray()); - Assert.Equal(new[] { 1, 2, 3, 4 }, source.Take(4).ToArray()); - Assert.Equal(1, source.Take(1).ToArray().Single()); - Assert.Empty(source.Take(0).ToArray()); - Assert.Empty(source.Take(-10).ToArray()); - - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(0..5).ToArray()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(0..6).ToArray()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(0..40).ToArray()); - Assert.Equal(new[] { 1, 2, 3, 4 }, source.Take(0..4).ToArray()); - Assert.Equal(1, source.Take(0..1).ToArray().Single()); - Assert.Empty(source.Take(0..0).ToArray()); - - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^5..5).ToArray()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^5..6).ToArray()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^5..40).ToArray()); - Assert.Equal(new[] { 1, 2, 3, 4 }, source.Take(^5..4).ToArray()); - Assert.Equal(1, source.Take(^5..1).ToArray().Single()); - Assert.Empty(source.Take(^5..0).ToArray()); - Assert.Empty(source.Take(^15..0).ToArray()); - - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(0..^0).ToArray()); - Assert.Equal(new[] { 1, 2, 3, 4 }, source.Take(0..^1).ToArray()); - Assert.Equal(1, source.Take(0..^4).ToArray().Single()); - Assert.Empty(source.Take(0..^5).ToArray()); - Assert.Empty(source.Take(0..^15).ToArray()); - - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^5..^0).ToArray()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^6..^0).ToArray()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^45..^0).ToArray()); - Assert.Equal(new[] { 1, 2, 3, 4 }, source.Take(^5..^1).ToArray()); - Assert.Equal(1, source.Take(^5..^4).ToArray().Single()); - Assert.Empty(source.Take(^5..^5).ToArray()); - Assert.Empty(source.Take(^15..^5).ToArray()); - } - - [Fact] - public void ToArrayNotList() - { - var source = ForceNotCollection(new[] { 1, 2, 3, 4, 5 }); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(5).ToArray()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(6).ToArray()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(40).ToArray()); - Assert.Equal(new[] { 1, 2, 3, 4 }, source.Take(4).ToArray()); - Assert.Equal(1, source.Take(1).ToArray().Single()); - Assert.Empty(source.Take(0).ToArray()); - Assert.Empty(source.Take(-10).ToArray()); - - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(0..5).ToArray()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(0..6).ToArray()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(0..40).ToArray()); - Assert.Equal(new[] { 1, 2, 3, 4 }, source.Take(0..4).ToArray()); - Assert.Equal(1, source.Take(0..1).ToArray().Single()); - Assert.Empty(source.Take(0..0).ToArray()); - - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^5..5).ToArray()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^5..6).ToArray()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^5..40).ToArray()); - Assert.Equal(new[] { 1, 2, 3, 4 }, source.Take(^5..4).ToArray()); - Assert.Equal(1, source.Take(^5..1).ToArray().Single()); - Assert.Empty(source.Take(^5..0).ToArray()); - Assert.Empty(source.Take(^15..0).ToArray()); - - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(0..^0).ToArray()); - Assert.Equal(new[] { 1, 2, 3, 4 }, source.Take(0..^1).ToArray()); - Assert.Equal(1, source.Take(0..^4).ToArray().Single()); - Assert.Empty(source.Take(0..^5).ToArray()); - Assert.Empty(source.Take(0..^15).ToArray()); - - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^5..^0).ToArray()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^6..^0).ToArray()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^45..^0).ToArray()); - Assert.Equal(new[] { 1, 2, 3, 4 }, source.Take(^5..^1).ToArray()); - Assert.Equal(1, source.Take(^5..^4).ToArray().Single()); - Assert.Empty(source.Take(^5..^5).ToArray()); - Assert.Empty(source.Take(^15..^5).ToArray()); + foreach (var source in CreateSources([1, 2, 3, 4, 5])) + { + Assert.Equal([1, 2, 3, 4, 5], source.Take(5).ToArray()); + Assert.Equal([1, 2, 3, 4, 5], source.Take(6).ToArray()); + Assert.Equal([1, 2, 3, 4, 5], source.Take(40).ToArray()); + Assert.Equal([1, 2, 3, 4], source.Take(4).ToArray()); + Assert.Equal(1, source.Take(1).ToArray().Single()); + Assert.Empty(source.Take(0).ToArray()); + Assert.Empty(source.Take(-10).ToArray()); + + Assert.Equal([1, 2, 3, 4, 5], source.Take(0..5).ToArray()); + Assert.Equal([1, 2, 3, 4, 5], source.Take(0..6).ToArray()); + Assert.Equal([1, 2, 3, 4, 5], source.Take(0..40).ToArray()); + Assert.Equal([1, 2, 3, 4], source.Take(0..4).ToArray()); + Assert.Equal(1, source.Take(0..1).ToArray().Single()); + Assert.Empty(source.Take(0..0).ToArray()); + + Assert.Equal([1, 2, 3, 4, 5], source.Take(^5..5).ToArray()); + Assert.Equal([1, 2, 3, 4, 5], source.Take(^5..6).ToArray()); + Assert.Equal([1, 2, 3, 4, 5], source.Take(^5..40).ToArray()); + Assert.Equal([1, 2, 3, 4], source.Take(^5..4).ToArray()); + Assert.Equal(1, source.Take(^5..1).ToArray().Single()); + Assert.Empty(source.Take(^5..0).ToArray()); + Assert.Empty(source.Take(^15..0).ToArray()); + + Assert.Equal([1, 2, 3, 4, 5], source.Take(0..^0).ToArray()); + Assert.Equal([1, 2, 3, 4], source.Take(0..^1).ToArray()); + Assert.Equal(1, source.Take(0..^4).ToArray().Single()); + Assert.Empty(source.Take(0..^5).ToArray()); + Assert.Empty(source.Take(0..^15).ToArray()); + + Assert.Equal([1, 2, 3, 4, 5], source.Take(^5..^0).ToArray()); + Assert.Equal([1, 2, 3, 4, 5], source.Take(^6..^0).ToArray()); + Assert.Equal([1, 2, 3, 4, 5], source.Take(^45..^0).ToArray()); + Assert.Equal([1, 2, 3, 4], source.Take(^5..^1).ToArray()); + Assert.Equal(1, source.Take(^5..^4).ToArray().Single()); + Assert.Empty(source.Take(^5..^5).ToArray()); + Assert.Empty(source.Take(^15..^5).ToArray()); + } } [Fact] public void ToList() { - var source = new[] { 1, 2, 3, 4, 5 }; - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(5).ToList()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(6).ToList()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(40).ToList()); - Assert.Equal(new[] { 1, 2, 3, 4 }, source.Take(4).ToList()); - Assert.Equal(1, source.Take(1).ToList().Single()); - Assert.Empty(source.Take(0).ToList()); - Assert.Empty(source.Take(-10).ToList()); - - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(0..5).ToList()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(0..6).ToList()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(0..40).ToList()); - Assert.Equal(new[] { 1, 2, 3, 4 }, source.Take(0..4).ToList()); - Assert.Equal(1, source.Take(0..1).ToList().Single()); - Assert.Empty(source.Take(0..0).ToList()); - - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^5..5).ToList()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^5..6).ToList()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^5..40).ToList()); - Assert.Equal(new[] { 1, 2, 3, 4 }, source.Take(^5..4).ToList()); - Assert.Equal(1, source.Take(^5..1).ToList().Single()); - Assert.Empty(source.Take(^5..0).ToList()); - Assert.Empty(source.Take(^15..0).ToList()); - - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(0..^0).ToList()); - Assert.Equal(new[] { 1, 2, 3, 4 }, source.Take(0..^1).ToList()); - Assert.Equal(1, source.Take(0..^4).ToList().Single()); - Assert.Empty(source.Take(0..^5).ToList()); - Assert.Empty(source.Take(0..^15).ToList()); - - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^5..^0).ToList()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^6..^0).ToList()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^45..^0).ToList()); - Assert.Equal(new[] { 1, 2, 3, 4 }, source.Take(^5..^1).ToList()); - Assert.Equal(1, source.Take(^5..^4).ToList().Single()); - Assert.Empty(source.Take(^5..^5).ToList()); - Assert.Empty(source.Take(^15..^5).ToList()); - } - - [Fact] - public void ToListNotList() - { - var source = ForceNotCollection(new[] { 1, 2, 3, 4, 5 }); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(5).ToList()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(6).ToList()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(40).ToList()); - Assert.Equal(new[] { 1, 2, 3, 4 }, source.Take(4).ToList()); - Assert.Equal(1, source.Take(1).ToList().Single()); - Assert.Empty(source.Take(0).ToList()); - Assert.Empty(source.Take(-10).ToList()); - - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(0..5).ToList()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(0..6).ToList()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(0..40).ToList()); - Assert.Equal(new[] { 1, 2, 3, 4 }, source.Take(0..4).ToList()); - Assert.Equal(1, source.Take(0..1).ToList().Single()); - Assert.Empty(source.Take(0..0).ToList()); - - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^5..5).ToList()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^5..6).ToList()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^5..40).ToList()); - Assert.Equal(new[] { 1, 2, 3, 4 }, source.Take(^5..4).ToList()); - Assert.Equal(1, source.Take(^5..1).ToList().Single()); - Assert.Empty(source.Take(^5..0).ToList()); - Assert.Empty(source.Take(^15..0).ToList()); - - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(0..^0).ToList()); - Assert.Equal(new[] { 1, 2, 3, 4 }, source.Take(0..^1).ToList()); - Assert.Equal(1, source.Take(0..^4).ToList().Single()); - Assert.Empty(source.Take(0..^5).ToList()); - Assert.Empty(source.Take(0..^15).ToList()); - - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^5..^0).ToList()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^6..^0).ToList()); - Assert.Equal(new[] { 1, 2, 3, 4, 5 }, source.Take(^45..^0).ToList()); - Assert.Equal(new[] { 1, 2, 3, 4 }, source.Take(^5..^1).ToList()); - Assert.Equal(1, source.Take(^5..^4).ToList().Single()); - Assert.Empty(source.Take(^5..^5).ToList()); - Assert.Empty(source.Take(^15..^5).ToList()); + foreach (var source in CreateSources([1, 2, 3, 4, 5])) + { + Assert.Equal([1, 2, 3, 4, 5], source.Take(5).ToList()); + Assert.Equal([1, 2, 3, 4, 5], source.Take(6).ToList()); + Assert.Equal([1, 2, 3, 4, 5], source.Take(40).ToList()); + Assert.Equal([1, 2, 3, 4], source.Take(4).ToList()); + Assert.Equal(1, source.Take(1).ToList().Single()); + Assert.Empty(source.Take(0).ToList()); + Assert.Empty(source.Take(-10).ToList()); + + Assert.Equal([1, 2, 3, 4, 5], source.Take(0..5).ToList()); + Assert.Equal([1, 2, 3, 4, 5], source.Take(0..6).ToList()); + Assert.Equal([1, 2, 3, 4, 5], source.Take(0..40).ToList()); + Assert.Equal([1, 2, 3, 4], source.Take(0..4).ToList()); + Assert.Equal(1, source.Take(0..1).ToList().Single()); + Assert.Empty(source.Take(0..0).ToList()); + + Assert.Equal([1, 2, 3, 4, 5], source.Take(^5..5).ToList()); + Assert.Equal([1, 2, 3, 4, 5], source.Take(^5..6).ToList()); + Assert.Equal([1, 2, 3, 4, 5], source.Take(^5..40).ToList()); + Assert.Equal([1, 2, 3, 4], source.Take(^5..4).ToList()); + Assert.Equal(1, source.Take(^5..1).ToList().Single()); + Assert.Empty(source.Take(^5..0).ToList()); + Assert.Empty(source.Take(^15..0).ToList()); + + Assert.Equal([1, 2, 3, 4, 5], source.Take(0..^0).ToList()); + Assert.Equal([1, 2, 3, 4], source.Take(0..^1).ToList()); + Assert.Equal(1, source.Take(0..^4).ToList().Single()); + Assert.Empty(source.Take(0..^5).ToList()); + Assert.Empty(source.Take(0..^15).ToList()); + + Assert.Equal([1, 2, 3, 4, 5], source.Take(^5..^0).ToList()); + Assert.Equal([1, 2, 3, 4, 5], source.Take(^6..^0).ToList()); + Assert.Equal([1, 2, 3, 4, 5], source.Take(^45..^0).ToList()); + Assert.Equal([1, 2, 3, 4], source.Take(^5..^1).ToList()); + Assert.Equal(1, source.Take(^5..^4).ToList().Single()); + Assert.Empty(source.Take(^5..^5).ToList()); + Assert.Empty(source.Take(^15..^5).ToList()); + } } [Fact] public void TakeCanOnlyBeOneList() { - var source = new[] { 2, 4, 6, 8, 10 }; - Assert.Equal(new[] { 2 }, source.Take(1)); - Assert.Equal(new[] { 4 }, source.Skip(1).Take(1)); - Assert.Equal(new[] { 6 }, source.Take(3).Skip(2)); - Assert.Equal(new[] { 2 }, source.Take(3).Take(1)); - - Assert.Equal(new[] { 2 }, source.Take(0..1)); - Assert.Equal(new[] { 4 }, source.Skip(1).Take(0..1)); - Assert.Equal(new[] { 6 }, source.Take(0..3).Skip(2)); - Assert.Equal(new[] { 2 }, source.Take(0..3).Take(0..1)); - - Assert.Equal(new[] { 2 }, source.Take(^5..1)); - Assert.Equal(new[] { 4 }, source.Skip(1).Take(^4..1)); - Assert.Equal(new[] { 6 }, source.Take(^5..3).Skip(2)); - Assert.Equal(new[] { 2 }, source.Take(^5..3).Take(^4..1)); - - Assert.Equal(new[] { 2 }, source.Take(0..^4)); - Assert.Equal(new[] { 4 }, source.Skip(1).Take(0..^3)); - Assert.Equal(new[] { 6 }, source.Take(0..^2).Skip(2)); - Assert.Equal(new[] { 2 }, source.Take(0..^2).Take(0..^2)); - - Assert.Equal(new[] { 2 }, source.Take(^5..^4)); - Assert.Equal(new[] { 4 }, source.Skip(1).Take(^4..^3)); - Assert.Equal(new[] { 6 }, source.Take(^5..^2).Skip(2)); - Assert.Equal(new[] { 2 }, source.Take(^5..^2).Take(^4..^2)); - } - - [Fact] - public void TakeCanOnlyBeOneNotList() - { - var source = ForceNotCollection(new[] { 2, 4, 6, 8, 10 }); - Assert.Equal(new[] { 2 }, source.Take(1)); - Assert.Equal(new[] { 4 }, source.Skip(1).Take(1)); - Assert.Equal(new[] { 6 }, source.Take(3).Skip(2)); - Assert.Equal(new[] { 2 }, source.Take(3).Take(1)); - - Assert.Equal(new[] { 2 }, source.Take(0..1)); - Assert.Equal(new[] { 4 }, source.Skip(1).Take(0..1)); - Assert.Equal(new[] { 6 }, source.Take(0..3).Skip(2)); - Assert.Equal(new[] { 2 }, source.Take(0..3).Take(0..1)); - - Assert.Equal(new[] { 2 }, source.Take(^5..1)); - Assert.Equal(new[] { 4 }, source.Skip(1).Take(^4..1)); - Assert.Equal(new[] { 6 }, source.Take(^5..3).Skip(2)); - Assert.Equal(new[] { 2 }, source.Take(^5..3).Take(^4..1)); - - Assert.Equal(new[] { 2 }, source.Take(0..^4)); - Assert.Equal(new[] { 4 }, source.Skip(1).Take(0..^3)); - Assert.Equal(new[] { 6 }, source.Take(0..^2).Skip(2)); - Assert.Equal(new[] { 2 }, source.Take(0..^2).Take(0..^2)); - - Assert.Equal(new[] { 2 }, source.Take(^5..^4)); - Assert.Equal(new[] { 4 }, source.Skip(1).Take(^4..^3)); - Assert.Equal(new[] { 6 }, source.Take(^5..^2).Skip(2)); - Assert.Equal(new[] { 2 }, source.Take(^5..^2).Take(^4..^2)); + foreach (var source in CreateSources([2, 4, 6, 8, 10])) + { + Assert.Equal([2], source.Take(1)); + Assert.Equal([4], source.Skip(1).Take(1)); + Assert.Equal([6], source.Take(3).Skip(2)); + Assert.Equal([2], source.Take(3).Take(1)); + + Assert.Equal([2], source.Take(0..1)); + Assert.Equal([4], source.Skip(1).Take(0..1)); + Assert.Equal([6], source.Take(0..3).Skip(2)); + Assert.Equal([2], source.Take(0..3).Take(0..1)); + + Assert.Equal([2], source.Take(^5..1)); + Assert.Equal([4], source.Skip(1).Take(^4..1)); + Assert.Equal([6], source.Take(^5..3).Skip(2)); + Assert.Equal([2], source.Take(^5..3).Take(^4..1)); + + Assert.Equal([2], source.Take(0..^4)); + Assert.Equal([4], source.Skip(1).Take(0..^3)); + Assert.Equal([6], source.Take(0..^2).Skip(2)); + Assert.Equal([2], source.Take(0..^2).Take(0..^2)); + + Assert.Equal([2], source.Take(^5..^4)); + Assert.Equal([4], source.Skip(1).Take(^4..^3)); + Assert.Equal([6], source.Take(^5..^2).Skip(2)); + Assert.Equal([2], source.Take(^5..^2).Take(^4..^2)); + } } [Fact] public void RepeatEnumerating() { - var source = new[] { 1, 2, 3, 4, 5 }; - var taken1 = source.Take(3); - Assert.Equal(taken1, taken1); - - var taken2 = source.Take(0..3); - Assert.Equal(taken2, taken2); - - var taken3 = source.Take(^5..3); - Assert.Equal(taken3, taken3); - - var taken4 = source.Take(0..^2); - Assert.Equal(taken4, taken4); - - var taken5 = source.Take(^5..^2); - Assert.Equal(taken5, taken5); - } - - [Fact] - public void RepeatEnumeratingNotList() - { - var source = ForceNotCollection(new[] { 1, 2, 3, 4, 5 }); - var taken1 = source.Take(3); - Assert.Equal(taken1, taken1); + foreach (var source in CreateSources([1, 2, 3, 4, 5])) + { + var taken1 = source.Take(3); + Assert.Equal(taken1, taken1); - var taken2 = source.Take(0..3); - Assert.Equal(taken2, taken2); + var taken2 = source.Take(0..3); + Assert.Equal(taken2, taken2); - var taken3 = source.Take(^5..3); - Assert.Equal(taken3, taken3); + var taken3 = source.Take(^5..3); + Assert.Equal(taken3, taken3); - var taken4 = source.Take(0..^2); - Assert.Equal(taken4, taken4); + var taken4 = source.Take(0..^2); + Assert.Equal(taken4, taken4); - var taken5 = source.Take(^5..^2); - Assert.Equal(taken5, taken5); + var taken5 = source.Take(^5..^2); + Assert.Equal(taken5, taken5); + } } - [Theory] [InlineData(1000)] [InlineData(1000000)] @@ -1408,13 +968,13 @@ public void DisposeSource_EndIndexFromEnd_ShouldDisposeOnCompletedEnumeration() [Fact] public void OutOfBoundNoException() { - Func source = () => new[] { 1, 2, 3, 4, 5 }; + Func source = () => [1, 2, 3, 4, 5]; Assert.Equal(source(), source().Take(0..6)); Assert.Equal(source(), source().Take(0..int.MaxValue)); - Assert.Equal(new int[] { 1, 2, 3, 4 }, source().Take(^10..4)); - Assert.Equal(new int[] { 1, 2, 3, 4 }, source().Take(^int.MaxValue..4)); + Assert.Equal([1, 2, 3, 4], source().Take(^10..4)); + Assert.Equal([1, 2, 3, 4], source().Take(^int.MaxValue..4)); Assert.Equal(source(), source().Take(^10..6)); Assert.Equal(source(), source().Take(^int.MaxValue..6)); Assert.Equal(source(), source().Take(^10..int.MaxValue)); @@ -1429,8 +989,8 @@ public void OutOfBoundNoException() Assert.Empty(source().Take(int.MaxValue..^6)); Assert.Empty(source().Take(int.MaxValue..^int.MaxValue)); - Assert.Equal(new int[] { 1, 2, 3, 4 }, source().Take(^10..^1)); - Assert.Equal(new int[] { 1, 2, 3, 4 }, source().Take(^int.MaxValue..^1)); + Assert.Equal([1, 2, 3, 4], source().Take(^10..^1)); + Assert.Equal([1, 2, 3, 4], source().Take(^int.MaxValue..^1)); Assert.Empty(source().Take(^0..^6)); Assert.Empty(source().Take(^1..^6)); Assert.Empty(source().Take(^6..^6)); @@ -1450,8 +1010,8 @@ public void OutOfBoundNoExceptionNotList() Assert.Equal(source, ForceNotCollection(source).Take(0..6)); Assert.Equal(source, ForceNotCollection(source).Take(0..int.MaxValue)); - Assert.Equal(new int[] { 1, 2, 3, 4 }, ForceNotCollection(source).Take(^10..4)); - Assert.Equal(new int[] { 1, 2, 3, 4 }, ForceNotCollection(source).Take(^int.MaxValue..4)); + Assert.Equal([1, 2, 3, 4], ForceNotCollection(source).Take(^10..4)); + Assert.Equal([1, 2, 3, 4], ForceNotCollection(source).Take(^int.MaxValue..4)); Assert.Equal(source, ForceNotCollection(source).Take(^10..6)); Assert.Equal(source, ForceNotCollection(source).Take(^int.MaxValue..6)); Assert.Equal(source, ForceNotCollection(source).Take(^10..int.MaxValue)); @@ -1466,8 +1026,8 @@ public void OutOfBoundNoExceptionNotList() Assert.Empty(ForceNotCollection(source).Take(int.MaxValue..^6)); Assert.Empty(ForceNotCollection(source).Take(int.MaxValue..^int.MaxValue)); - Assert.Equal(new int[] { 1, 2, 3, 4 }, ForceNotCollection(source).Take(^10..^1)); - Assert.Equal(new int[] { 1, 2, 3, 4 }, ForceNotCollection(source).Take(^int.MaxValue..^1)); + Assert.Equal([1, 2, 3, 4], ForceNotCollection(source).Take(^10..^1)); + Assert.Equal([1, 2, 3, 4], ForceNotCollection(source).Take(^int.MaxValue..^1)); Assert.Empty(ForceNotCollection(source).Take(^0..^6)); Assert.Empty(ForceNotCollection(source).Take(^1..^6)); Assert.Empty(ForceNotCollection(source).Take(^6..^6)); @@ -1487,8 +1047,8 @@ public void OutOfBoundNoExceptionListPartition() Assert.Equal(source, ListPartitionOrEmpty(source).Take(0..6)); Assert.Equal(source, ListPartitionOrEmpty(source).Take(0..int.MaxValue)); - Assert.Equal(new int[] { 1, 2, 3, 4 }, ListPartitionOrEmpty(source).Take(^10..4)); - Assert.Equal(new int[] { 1, 2, 3, 4 }, ListPartitionOrEmpty(source).Take(^int.MaxValue..4)); + Assert.Equal([1, 2, 3, 4], ListPartitionOrEmpty(source).Take(^10..4)); + Assert.Equal([1, 2, 3, 4], ListPartitionOrEmpty(source).Take(^int.MaxValue..4)); Assert.Equal(source, ListPartitionOrEmpty(source).Take(^10..6)); Assert.Equal(source, ListPartitionOrEmpty(source).Take(^int.MaxValue..6)); Assert.Equal(source, ListPartitionOrEmpty(source).Take(^10..int.MaxValue)); @@ -1503,8 +1063,8 @@ public void OutOfBoundNoExceptionListPartition() Assert.Empty(ListPartitionOrEmpty(source).Take(int.MaxValue..^6)); Assert.Empty(ListPartitionOrEmpty(source).Take(int.MaxValue..^int.MaxValue)); - Assert.Equal(new int[] { 1, 2, 3, 4 }, ListPartitionOrEmpty(source).Take(^10..^1)); - Assert.Equal(new int[] { 1, 2, 3, 4 }, ListPartitionOrEmpty(source).Take(^int.MaxValue..^1)); + Assert.Equal([1, 2, 3, 4], ListPartitionOrEmpty(source).Take(^10..^1)); + Assert.Equal([1, 2, 3, 4], ListPartitionOrEmpty(source).Take(^int.MaxValue..^1)); Assert.Empty(ListPartitionOrEmpty(source).Take(^0..^6)); Assert.Empty(ListPartitionOrEmpty(source).Take(^1..^6)); Assert.Empty(ListPartitionOrEmpty(source).Take(^6..^6)); @@ -1524,8 +1084,8 @@ public void OutOfBoundNoExceptionEnumerablePartition() Assert.Equal(source, EnumerablePartitionOrEmpty(source).Take(0..6)); Assert.Equal(source, EnumerablePartitionOrEmpty(source).Take(0..int.MaxValue)); - Assert.Equal(new int[] { 1, 2, 3, 4 }, EnumerablePartitionOrEmpty(source).Take(^10..4)); - Assert.Equal(new int[] { 1, 2, 3, 4 }, EnumerablePartitionOrEmpty(source).Take(^int.MaxValue..4)); + Assert.Equal([1, 2, 3, 4], EnumerablePartitionOrEmpty(source).Take(^10..4)); + Assert.Equal([1, 2, 3, 4], EnumerablePartitionOrEmpty(source).Take(^int.MaxValue..4)); Assert.Equal(source, EnumerablePartitionOrEmpty(source).Take(^10..6)); Assert.Equal(source, EnumerablePartitionOrEmpty(source).Take(^int.MaxValue..6)); Assert.Equal(source, EnumerablePartitionOrEmpty(source).Take(^10..int.MaxValue)); @@ -1540,8 +1100,8 @@ public void OutOfBoundNoExceptionEnumerablePartition() Assert.Empty(EnumerablePartitionOrEmpty(source).Take(int.MaxValue..^6)); Assert.Empty(EnumerablePartitionOrEmpty(source).Take(int.MaxValue..^int.MaxValue)); - Assert.Equal(new int[] { 1, 2, 3, 4 }, EnumerablePartitionOrEmpty(source).Take(^10..^1)); - Assert.Equal(new int[] { 1, 2, 3, 4 }, EnumerablePartitionOrEmpty(source).Take(^int.MaxValue..^1)); + Assert.Equal([1, 2, 3, 4], EnumerablePartitionOrEmpty(source).Take(^10..^1)); + Assert.Equal([1, 2, 3, 4], EnumerablePartitionOrEmpty(source).Take(^int.MaxValue..^1)); Assert.Empty(EnumerablePartitionOrEmpty(source).Take(^0..^6)); Assert.Empty(EnumerablePartitionOrEmpty(source).Take(^1..^6)); Assert.Empty(EnumerablePartitionOrEmpty(source).Take(^6..^6)); @@ -1559,26 +1119,26 @@ public void MutableSource() var source1 = new List() { 0, 1, 2, 3, 4 }; var query1 = source1.Take(3); source1.RemoveAt(0); - source1.InsertRange(2, new[] { -1, -2 }); - Assert.Equal(new[] { 1, 2, -1 }, query1); + source1.InsertRange(2, [-1, -2]); + Assert.Equal([1, 2, -1], query1); var source2 = new List() { 0, 1, 2, 3, 4 }; var query2 = source2.Take(0..3); source2.RemoveAt(0); - source2.InsertRange(2, new[] { -1, -2 }); - Assert.Equal(new[] { 1, 2, -1 }, query2); + source2.InsertRange(2, [-1, -2]); + Assert.Equal([1, 2, -1], query2); var source3 = new List() { 0, 1, 2, 3, 4 }; var query3 = source3.Take(^6..3); source3.RemoveAt(0); - source3.InsertRange(2, new[] { -1, -2 }); - Assert.Equal(new[] { 1, 2, -1 }, query3); + source3.InsertRange(2, [-1, -2]); + Assert.Equal([1, 2, -1], query3); var source4 = new List() { 0, 1, 2, 3, 4 }; var query4 = source4.Take(^6..^3); source4.RemoveAt(0); - source4.InsertRange(2, new[] { -1, -2 }); - Assert.Equal(new[] { 1, 2, -1 }, query4); + source4.InsertRange(2, [-1, -2]); + Assert.Equal([1, 2, -1], query4); } [Fact] @@ -1587,32 +1147,32 @@ public void MutableSourceNotList() var source1 = new List() { 0, 1, 2, 3, 4 }; var query1 = ForceNotCollection(source1).Select(i => i).Take(3); source1.RemoveAt(0); - source1.InsertRange(2, new[] { -1, -2 }); - Assert.Equal(new[] { 1, 2, -1 }, query1); + source1.InsertRange(2, [-1, -2]); + Assert.Equal([1, 2, -1], query1); var source2 = new List() { 0, 1, 2, 3, 4 }; var query2 = ForceNotCollection(source2).Select(i => i).Take(0..3); source2.RemoveAt(0); - source2.InsertRange(2, new[] { -1, -2 }); - Assert.Equal(new[] { 1, 2, -1 }, query2); + source2.InsertRange(2, [-1, -2]); + Assert.Equal([1, 2, -1], query2); var source3 = new List() { 0, 1, 2, 3, 4 }; var query3 = ForceNotCollection(source3).Select(i => i).Take(^6..3); source3.RemoveAt(0); - source3.InsertRange(2, new[] { -1, -2 }); - Assert.Equal(new[] { 1, 2, -1 }, query3); + source3.InsertRange(2, [-1, -2]); + Assert.Equal([1, 2, -1], query3); var source4 = new List() { 0, 1, 2, 3, 4 }; var query4 = ForceNotCollection(source4).Select(i => i).Take(^6..^3); source4.RemoveAt(0); - source4.InsertRange(2, new[] { -1, -2 }); - Assert.Equal(new[] { 1, 2, -1 }, query4); + source4.InsertRange(2, [-1, -2]); + Assert.Equal([1, 2, -1], query4); } [Fact] public void NonEmptySource_ConsistencyWithCountable() { - Func source = () => new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + Func source = () => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; // Multiple elements in the middle. Assert.Equal(source()[^9..5], source().Take(^9..5)); @@ -1658,7 +1218,7 @@ public void NonEmptySource_ConsistencyWithCountable() [Fact] public void NonEmptySource_ConsistencyWithCountable_NotList() { - int[] source = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + int[] source = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; // Multiple elements in the middle. Assert.Equal(source[^9..5], ForceNotCollection(source).Take(^9..5)); @@ -1704,7 +1264,7 @@ public void NonEmptySource_ConsistencyWithCountable_NotList() [Fact] public void NonEmptySource_ConsistencyWithCountable_ListPartition() { - int[] source = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + int[] source = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; // Multiple elements in the middle. Assert.Equal(source[^9..5], ListPartitionOrEmpty(source).Take(^9..5)); @@ -1750,7 +1310,7 @@ public void NonEmptySource_ConsistencyWithCountable_ListPartition() [Fact] public void NonEmptySource_ConsistencyWithCountable_EnumerablePartition() { - int[] source = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + int[] source = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; // Multiple elements in the middle. Assert.Equal(source[^9..5], EnumerablePartitionOrEmpty(source).Take(^9..5)); @@ -1796,7 +1356,7 @@ public void NonEmptySource_ConsistencyWithCountable_EnumerablePartition() [Fact] public void NonEmptySource_DoNotThrowException() { - Func source = () => new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + Func source = () => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; Assert.Empty(source().Take(3..2)); Assert.Empty(source().Take(6..^5)); @@ -1807,7 +1367,7 @@ public void NonEmptySource_DoNotThrowException() [Fact] public void NonEmptySource_DoNotThrowException_NotList() { - int[] source = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + int[] source = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; Assert.Empty(ForceNotCollection(source).Take(3..2)); Assert.Empty(ForceNotCollection(source).Take(6..^5)); @@ -1818,7 +1378,7 @@ public void NonEmptySource_DoNotThrowException_NotList() [Fact] public void NonEmptySource_DoNotThrowException_ListPartition() { - int[] source = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + int[] source = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; Assert.Empty(ListPartitionOrEmpty(source).Take(3..2)); Assert.Empty(ListPartitionOrEmpty(source).Take(6..^5)); @@ -1829,7 +1389,7 @@ public void NonEmptySource_DoNotThrowException_ListPartition() [Fact] public void NonEmptySource_DoNotThrowException_EnumerablePartition() { - int[] source = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + int[] source = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; Assert.Empty(EnumerablePartitionOrEmpty(source).Take(3..2)); Assert.Empty(EnumerablePartitionOrEmpty(source).Take(6..^5)); @@ -1840,7 +1400,7 @@ public void NonEmptySource_DoNotThrowException_EnumerablePartition() [Fact] public void EmptySource_DoNotThrowException() { - Func source = () => new int[] { }; + Func source = () => []; // Multiple elements in the middle. Assert.Empty(source().Take(^9..5)); @@ -1892,7 +1452,7 @@ public void EmptySource_DoNotThrowException() [Fact] public void EmptySource_DoNotThrowException_NotList() { - int[] source = { }; + int[] source = []; // Multiple elements in the middle. Assert.Empty(ForceNotCollection(source).Take(^9..5)); @@ -1944,7 +1504,7 @@ public void EmptySource_DoNotThrowException_NotList() [Fact] public void EmptySource_DoNotThrowException_ListPartition() { - int[] source = { }; + int[] source = []; // Multiple elements in the middle. Assert.Empty(ListPartitionOrEmpty(source).Take(^9..5)); @@ -1996,7 +1556,7 @@ public void EmptySource_DoNotThrowException_ListPartition() [Fact] public void EmptySource_DoNotThrowException_EnumerablePartition() { - int[] source = { }; + int[] source = []; // Multiple elements in the middle. Assert.Empty(EnumerablePartitionOrEmpty(source).Take(^9..5)); diff --git a/tests/System.Linq.Tests/Tests/ZLinq/TakeWhileTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/TakeWhileTests.cs index 0979b0a2..73b2df39 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/TakeWhileTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/TakeWhileTests.cs @@ -11,8 +11,8 @@ public class TakeWhileTests : EnumerableTests [Fact] public void Empty() { - Assert.Equal(Enumerable.Empty(), Enumerable.Empty().TakeWhile(i => i < 40)); - Assert.Equal(Enumerable.Empty(), Enumerable.Empty().TakeWhile((i, index) => i < 40)); + Assert.Equal([], Enumerable.Empty().TakeWhile(i => i < 40)); + Assert.Equal([], Enumerable.Empty().TakeWhile((i, index) => i < 40)); } [Fact] @@ -50,22 +50,22 @@ public void SourceEmptyIndexed() [Fact] public void SourceNonEmptyPredicateFalseForAll() { - int[] source = { 9, 7, 15, 3, 11 }; + int[] source = [9, 7, 15, 3, 11]; Assert.Empty(source.TakeWhile(x => x % 2 == 0)); } [Fact] public void SourceNonEmptyPredicateFalseForAllWithIndex() { - int[] source = { 9, 7, 15, 3, 11 }; + int[] source = [9, 7, 15, 3, 11]; Assert.Empty(source.TakeWhile((x, i) => x % 2 == 0)); } [Fact] public void SourceNonEmptyPredicateTrueSomeFalseSecond() { - int[] source = { 8, 3, 12, 4, 6, 10 }; - int[] expected = { 8 }; + int[] source = [8, 3, 12, 4, 6, 10]; + int[] expected = [8]; Assert.Equal(expected, source.TakeWhile(x => x % 2 == 0)); } @@ -73,8 +73,8 @@ public void SourceNonEmptyPredicateTrueSomeFalseSecond() [Fact] public void SourceNonEmptyPredicateTrueSomeFalseSecondWithIndex() { - int[] source = { 8, 3, 12, 4, 6, 10 }; - int[] expected = { 8 }; + int[] source = [8, 3, 12, 4, 6, 10]; + int[] expected = [8]; Assert.Equal(expected, source.TakeWhile((x, i) => x % 2 == 0)); } @@ -82,22 +82,22 @@ public void SourceNonEmptyPredicateTrueSomeFalseSecondWithIndex() [Fact] public void SourceNonEmptyPredicateTrueSomeFalseFirst() { - int[] source = { 3, 2, 4, 12, 6 }; + int[] source = [3, 2, 4, 12, 6]; Assert.Empty(source.TakeWhile(x => x % 2 == 0)); } [Fact] public void SourceNonEmptyPredicateTrueSomeFalseFirstWithIndex() { - int[] source = { 3, 2, 4, 12, 6 }; + int[] source = [3, 2, 4, 12, 6]; Assert.Empty(source.TakeWhile((x, i) => x % 2 == 0)); } [Fact] public void FirstTakenByIndex() { - int[] source = { 6, 2, 5, 3, 8 }; - int[] expected = { 6 }; + int[] source = [6, 2, 5, 3, 8]; + int[] expected = [6]; Assert.Equal(expected, source.TakeWhile((element, index) => index == 0)); } @@ -105,8 +105,8 @@ public void FirstTakenByIndex() [Fact] public void AllButLastTakenByIndex() { - int[] source = { 6, 2, 5, 3, 8 }; - int[] expected = { 6, 2, 5, 3 }; + int[] source = [6, 2, 5, 3, 8]; + int[] expected = [6, 2, 5, 3]; Assert.Equal(expected, source.TakeWhile((element, index) => index < source.Length - 1)); } @@ -114,11 +114,11 @@ public void AllButLastTakenByIndex() [Fact] public void RunOnce() { - int[] source = { 8, 3, 12, 4, 6, 10 }; - int[] expected = { 8 }; + int[] source = [8, 3, 12, 4, 6, 10]; + int[] expected = [8]; Assert.Equal(expected, source.RunOnce().TakeWhile(x => x % 2 == 0)); - source = new[] { 6, 2, 5, 3, 8 }; - expected = new[] { 6, 2, 5, 3 }; + source = [6, 2, 5, 3, 8]; + expected = [6, 2, 5, 3]; Assert.Equal(expected, source.RunOnce().TakeWhile((element, index) => index < source.Length - 1)); } @@ -145,7 +145,7 @@ public void ThrowsOnNullSource() [Fact] public void ThrowsOnNullPredicate() { - int[] source = { 1, 2, 3 }; + int[] source = [1, 2, 3]; Func nullPredicate = null; AssertExtensions.Throws("predicate", () => source.TakeWhile(nullPredicate)); @@ -161,7 +161,7 @@ public void ThrowsOnNullSourceIndexed() [Fact] public void ThrowsOnNullPredicateIndexed() { - int[] source = { 1, 2, 3 }; + int[] source = [1, 2, 3]; Func nullPredicate = null; AssertExtensions.Throws("predicate", () => source.TakeWhile(nullPredicate)); diff --git a/tests/System.Linq.Tests/Tests/ZLinq/ThenByDescendingTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/ThenByDescendingTests.cs index 76d2720c..3194cc4a 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/ThenByDescendingTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/ThenByDescendingTests.cs @@ -39,7 +39,7 @@ public void SameResultsRepeatCallsStringQuery() [Fact] public void SourceEmpty() { - int[] source = { }; + int[] source = []; Assert.Empty(source.OrderBy(e => e).ThenByDescending(e => e)); } @@ -95,7 +95,7 @@ public void OrderIsStable() var source = @"Because I could not stop for Death - He kindly stopped for me - The Carriage held but just Ourselves - -And Immortality.".Split(new[] { ' ', '\n', '\r', '-' }, StringSplitOptions.RemoveEmptyEntries); +And Immortality.".Split([' ', '\n', '\r', '-'], StringSplitOptions.RemoveEmptyEntries); var expected = new[] { "stopped", "kindly", "could", "stop", "held", "just", "not", "for", "for", "but", "me", @@ -111,7 +111,7 @@ public void OrderIsStableCustomComparer() var source = @"Because I could not stop for Death - He kindly stopped for me - The Carriage held but just Ourselves - -And Immortality.".Split(new[] { ' ', '\n', '\r', '-' }, StringSplitOptions.RemoveEmptyEntries); +And Immortality.".Split([' ', '\n', '\r', '-'], StringSplitOptions.RemoveEmptyEntries); var expected = new[] { "me", "not", "for", "for", "but", "stop", "held", "just", "could", "kindly", "stopped", @@ -127,7 +127,7 @@ public void RunOnce() var source = @"Because I could not stop for Death - He kindly stopped for me - The Carriage held but just Ourselves - -And Immortality.".Split(new[] { ' ', '\n', '\r', '-' }, StringSplitOptions.RemoveEmptyEntries); +And Immortality.".Split([' ', '\n', '\r', '-'], StringSplitOptions.RemoveEmptyEntries); var expected = new[] { "me", "not", "for", "for", "but", "stop", "held", "just", "could", "kindly", "stopped", diff --git a/tests/System.Linq.Tests/Tests/ZLinq/ThenByTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/ThenByTests.cs index 02049899..67fd3723 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/ThenByTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/ThenByTests.cs @@ -40,7 +40,7 @@ public void SameResultsRepeatCallsStringQuery() [Fact] public void SourceEmpty() { - int[] source = { }; + int[] source = []; Assert.Empty(source.OrderBy(e => e).ThenBy(e => e)); } @@ -116,7 +116,7 @@ public void OrderIsStable() var source = @"Because I could not stop for Death - He kindly stopped for me - The Carriage held but just Ourselves - -And Immortality.".Split(new[] { ' ', '\n', '\r', '-' }, StringSplitOptions.RemoveEmptyEntries); +And Immortality.".Split([' ', '\n', '\r', '-'], StringSplitOptions.RemoveEmptyEntries); var expected = new[] { "me", "not", "for", "for", "but", "stop", "held", "just", "could", "kindly", "stopped", @@ -132,7 +132,7 @@ public void RunOnce() var source = @"Because I could not stop for Death - He kindly stopped for me - The Carriage held but just Ourselves - -And Immortality.".Split(new[] { ' ', '\n', '\r', '-' }, StringSplitOptions.RemoveEmptyEntries); +And Immortality.".Split([' ', '\n', '\r', '-'], StringSplitOptions.RemoveEmptyEntries); var expected = new[] { "me", "not", "for", "for", "but", "stop", "held", "just", "could", "kindly", "stopped", diff --git a/tests/System.Linq.Tests/Tests/ZLinq/ToArrayTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/ToArrayTests.cs index 7aeee147..0256b5a4 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/ToArrayTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/ToArrayTests.cs @@ -18,7 +18,7 @@ public class ToArrayTests : EnumerableTests [Fact] public void ToArray_CreateACopyWhenNotEmpty() { - int[] sourceArray = new int[] { 1, 2, 3, 4, 5 }; + int[] sourceArray = [1, 2, 3, 4, 5]; int[] resultArray = sourceArray.ToArray(); Assert.NotSame(sourceArray, resultArray); @@ -28,7 +28,7 @@ public void ToArray_CreateACopyWhenNotEmpty() [Fact] public void ToArray_UseArrayEmptyWhenEmpty() { - int[] emptySourceArray = Array.Empty(); + int[] emptySourceArray = []; Assert.Same(emptySourceArray.ToArray(), emptySourceArray.ToArray()); @@ -70,7 +70,7 @@ public void ToArray_WorkWithEmptyCollection() [Fact] public void ToArray_ProduceCorrectArray() { - int[] sourceArray = new int[] { 1, 2, 3, 4, 5, 6, 7 }; + int[] sourceArray = [1, 2, 3, 4, 5, 6, 7]; RunToArrayOnAllCollectionTypes(sourceArray, resultArray => { @@ -79,7 +79,7 @@ public void ToArray_ProduceCorrectArray() }); - string[] sourceStringArray = new string[] { "1", "2", "3", "4", "5", "6", "7", "8" }; + string[] sourceStringArray = ["1", "2", "3", "4", "5", "6", "7", "8"]; RunToArrayOnAllCollectionTypes(sourceStringArray, resultStringArray => { @@ -92,16 +92,16 @@ public void ToArray_ProduceCorrectArray() [Fact] public void RunOnce() { - Assert.Equal(new int[] { 1, 2, 3, 4, 5, 6, 7 }, Enumerable.Range(1, 7).RunOnce().ToArray()); + Assert.Equal([1, 2, 3, 4, 5, 6, 7], Enumerable.Range(1, 7).RunOnce().ToArray()); Assert.Equal( - new string[] { "1", "2", "3", "4", "5", "6", "7", "8" }, + ["1", "2", "3", "4", "5", "6", "7", "8"], Enumerable.Range(1, 8).Select(i => i.ToString()).RunOnce().ToArray()); } [Fact] public void ToArray_TouchCountWithICollection() { - TestCollection source = new TestCollection(new int[] { 1, 2, 3, 4 }); + TestCollection source = new TestCollection([1, 2, 3, 4]); var resultArray = source.ToArray(); Assert.Equal(source, resultArray); @@ -120,7 +120,7 @@ public void ToArray_ThrowArgumentNullExceptionWhenSourceIsNull() [Fact(Skip = SkipReason.ICollectionCopyTo)] public void ToArray_UseCopyToWithICollection() { - TestCollection source = new TestCollection(new int[] { 1, 2, 3, 4 }); + TestCollection source = new TestCollection([1, 2, 3, 4]); var resultArray = source.ToArray(); Assert.Equal(source, resultArray); @@ -150,13 +150,13 @@ public void ToArray_ArrayWhereSelect(int[] sourceIntegers, string[] convertedStr Assert.Equal(convertedStrings, sourceIntegers.Select(i => i.ToString()).ToArray()); Assert.Equal(sourceIntegers, sourceIntegers.Where(i => true).ToArray()); - Assert.Equal(Array.Empty(), sourceIntegers.Where(i => false).ToArray()); + Assert.Equal([], sourceIntegers.Where(i => false).ToArray()); Assert.Equal(convertedStrings, sourceIntegers.Where(i => true).Select(i => i.ToString()).ToArray()); - Assert.Equal(Array.Empty(), sourceIntegers.Where(i => false).Select(i => i.ToString()).ToArray()); + Assert.Equal([], sourceIntegers.Where(i => false).Select(i => i.ToString()).ToArray()); Assert.Equal(convertedStrings, sourceIntegers.Select(i => i.ToString()).Where(s => s is not null).ToArray()); - Assert.Equal(Array.Empty(), sourceIntegers.Select(i => i.ToString()).Where(s => s is null).ToArray()); + Assert.Equal([], sourceIntegers.Select(i => i.ToString()).Where(s => s is null).ToArray()); } [Theory] @@ -170,13 +170,13 @@ public void ToArray_ListWhereSelect(int[] sourceIntegers, string[] convertedStri Assert.Equal(convertedStrings, sourceList.Select(i => i.ToString()).ToArray()); Assert.Equal(sourceList, sourceList.Where(i => true).ToArray()); - Assert.Equal(Array.Empty(), sourceList.Where(i => false).ToArray()); + Assert.Equal([], sourceList.Where(i => false).ToArray()); Assert.Equal(convertedStrings, sourceList.Where(i => true).Select(i => i.ToString()).ToArray()); - Assert.Equal(Array.Empty(), sourceList.Where(i => false).Select(i => i.ToString()).ToArray()); + Assert.Equal([], sourceList.Where(i => false).Select(i => i.ToString()).ToArray()); Assert.Equal(convertedStrings, sourceList.Select(i => i.ToString()).Where(s => s is not null).ToArray()); - Assert.Equal(Array.Empty(), sourceList.Select(i => i.ToString()).Where(s => s is null).ToArray()); + Assert.Equal([], sourceList.Select(i => i.ToString()).Where(s => s is null).ToArray()); } [Fact] @@ -228,7 +228,7 @@ public void EmptyArraysSameObject() [Fact] public void SourceIsEmptyICollectionT() { - int[] source = { }; + int[] source = []; ICollection collection = source as ICollection; @@ -239,8 +239,8 @@ public void SourceIsEmptyICollectionT() [Fact] public void SourceIsICollectionTWithFewElements() { - int?[] source = { -5, null, 0, 10, 3, -1, null, 4, 9 }; - int?[] expected = { -5, null, 0, 10, 3, -1, null, 4, 9 }; + int?[] source = [-5, null, 0, 10, 3, -1, null, 4, 9]; + int?[] expected = [-5, null, 0, 10, 3, -1, null, 4, 9]; ICollection collection = source as ICollection; @@ -262,7 +262,7 @@ public void SourceNotICollectionAndIsEmpty() public void SourceNotICollectionAndHasElements() { IEnumerable source = NumberRangeGuaranteedNotCollectionType(-4, 10); - int[] expected = { -4, -3, -2, -1, 0, 1, 2, 3, 4, 5 }; + int[] expected = [-4, -3, -2, -1, 0, 1, 2, 3, 4, 5]; Assert.Null(source as ICollection); @@ -273,7 +273,7 @@ public void SourceNotICollectionAndHasElements() public void SourceNotICollectionAndAllNull() { IEnumerable source = RepeatedNullableNumberGuaranteedNotCollectionType(null, 5); - int?[] expected = { null, null, null, null, null }; + int?[] expected = [null, null, null, null, null]; Assert.Null(source as ICollection); @@ -284,14 +284,14 @@ public void SourceNotICollectionAndAllNull() public void ConstantTimeCountPartitionSelectSameTypeToArray() { var source = Enumerable.Range(0, 100).Select(i => i * 2).Skip(1).Take(5); - Assert.Equal(new[] { 2, 4, 6, 8, 10 }, source.ToArray()); + Assert.Equal([2, 4, 6, 8, 10], source.ToArray()); } [Fact] public void ConstantTimeCountPartitionSelectDiffTypeToArray() { var source = Enumerable.Range(0, 100).Select(i => i.ToString()).Skip(1).Take(5); - Assert.Equal(new[] { "1", "2", "3", "4", "5" }, source.ToArray()); + Assert.Equal(["1", "2", "3", "4", "5"], source.ToArray()); } [Fact] @@ -312,14 +312,14 @@ public void ConstantTimeCountEmptyPartitionSelectDiffTypeToArray() public void NonConstantTimeCountPartitionSelectSameTypeToArray() { var source = NumberRangeGuaranteedNotCollectionType(0, 100).OrderBy(i => i).Select(i => i * 2).Skip(1).Take(5); - Assert.Equal(new[] { 2, 4, 6, 8, 10 }, source.ToArray()); + Assert.Equal([2, 4, 6, 8, 10], source.ToArray()); } [Fact] public void NonConstantTimeCountPartitionSelectDiffTypeToArray() { var source = NumberRangeGuaranteedNotCollectionType(0, 100).OrderBy(i => i).Select(i => i.ToString()).Skip(1).Take(5); - Assert.Equal(new[] { "1", "2", "3", "4", "5" }, source.ToArray()); + Assert.Equal(["1", "2", "3", "4", "5"], source.ToArray()); } [Fact] @@ -366,25 +366,25 @@ private enum Enum1 [Fact] public void ToArray_Cast() { - Enum0[] source = { Enum0.First, Enum0.Second, Enum0.Third }; + Enum0[] source = [Enum0.First, Enum0.Second, Enum0.Third]; var cast = source.Cast(); // Assert.IsType(cast); // ZLinq don't support implicit cast to IEnumerable var castArray = cast.ToArray(); Assert.IsType(castArray); - Assert.Equal(new[] { Enum1.First, Enum1.Second, Enum1.Third }, castArray); + Assert.Equal([Enum1.First, Enum1.Second, Enum1.Third], castArray); } public static IEnumerable ToArrayShouldWorkWithSpecialLengthLazyEnumerables_MemberData() { // Return array sizes that should be small enough not to OOM int MaxPower = PlatformDetection.IsBrowser ? 15 : 18; - yield return new object[] { 1 }; - yield return new object[] { 2 }; + yield return [1]; + yield return [2]; for (int i = 2; i <= MaxPower; i++) { - yield return new object[] { (i << i) - 1 }; - yield return new object[] { (i << i) }; - yield return new object[] { (i << i) + 1 }; + yield return [(i << i) - 1]; + yield return [(i << i)]; + yield return [(i << i) + 1]; } } } diff --git a/tests/System.Linq.Tests/Tests/ZLinq/ToDictionaryTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/ToDictionaryTests.cs index 49ba3651..8a10800b 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/ToDictionaryTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/ToDictionaryTests.cs @@ -81,7 +81,7 @@ public void ToDictionaryFromKv_WorkWithEmptyCollection() [Fact] public void ToDictionary_ProduceCorrectDictionary() { - int[] sourceArray = new int[] { 1, 2, 3, 4, 5, 6, 7 }; + int[] sourceArray = [1, 2, 3, 4, 5, 6, 7]; RunToDictionaryOnAllCollectionTypes(sourceArray, resultDictionary => { @@ -90,7 +90,7 @@ public void ToDictionary_ProduceCorrectDictionary() Assert.Equal(sourceArray, resultDictionary.Values); }); - string[] sourceStringArray = new string[] { "1", "2", "3", "4", "5", "6", "7", "8" }; + string[] sourceStringArray = ["1", "2", "3", "4", "5", "6", "7", "8"]; RunToDictionaryOnAllCollectionTypes(sourceStringArray, resultDictionary => { @@ -103,7 +103,7 @@ public void ToDictionary_ProduceCorrectDictionary() [Fact] public void ToDictionaryFromKv_ProduceCorrectDictionary() { - int[] sourceArray = new[] { 1, 2, 3, 4, 5, 6, 7 }; + int[] sourceArray = [1, 2, 3, 4, 5, 6, 7]; RunToDictionaryFromKvOnAllCollectionTypes(sourceArray, resultDictionary => { @@ -112,7 +112,7 @@ public void ToDictionaryFromKv_ProduceCorrectDictionary() Assert.Equal(sourceArray, resultDictionary.Values); }); - string[] sourceStringArray = new[] { "1", "2", "3", "4", "5", "6", "7", "8" }; + string[] sourceStringArray = ["1", "2", "3", "4", "5", "6", "7", "8"]; RunToDictionaryFromKvOnAllCollectionTypes(sourceStringArray, resultDictionary => { @@ -144,7 +144,7 @@ public void RunOnce() public void ToDictionary_PassCustomComparer() { EqualityComparer comparer = EqualityComparer.Create((x, y) => x == y, x => x); - TestCollection collection = new TestCollection(new int[] { 1, 2, 3, 4, 5, 6 }); + TestCollection collection = new TestCollection([1, 2, 3, 4, 5, 6]); Dictionary result1 = collection.ToDictionary(key => key, comparer); Assert.Same(comparer, result1.Comparer); @@ -162,7 +162,7 @@ public void ToDictionary_PassCustomComparer() [Fact] public void ToDictionary_UseDefaultComparerOnNull() { - TestCollection collection = new TestCollection(new int[] { 1, 2, 3, 4, 5, 6 }); + TestCollection collection = new TestCollection([1, 2, 3, 4, 5, 6]); Dictionary result1 = collection.ToDictionary(key => key, comparer: null); Assert.Same(EqualityComparer.Default, result1.Comparer); @@ -174,7 +174,7 @@ public void ToDictionary_UseDefaultComparerOnNull() [Fact] public void ToDictionary_UseDefaultComparer() { - TestCollection collection = new TestCollection(new[] { 1, 2, 3, 4, 5, 6 }); + TestCollection collection = new TestCollection([1, 2, 3, 4, 5, 6]); Dictionary result1 = collection.ToDictionary(key => key); Assert.Same(EqualityComparer.Default, result1.Comparer); @@ -192,7 +192,7 @@ public void ToDictionary_UseDefaultComparer() [Fact] public void ToDictionary_KeyValueSelectorsWork() { - TestCollection collection = new TestCollection(new int[] { 1, 2, 3, 4, 5, 6 }); + TestCollection collection = new TestCollection([1, 2, 3, 4, 5, 6]); Dictionary result = collection.ToDictionary(key => key + 10, val => val + 100); @@ -213,7 +213,7 @@ public void ToDictionary_ThrowArgumentNullExceptionWhenSourceIsNull() [Fact] public void ToDictionary_ThrowArgumentNullExceptionWhenKeySelectorIsNull() { - int[] source = new int[0]; + int[] source = []; Func keySelector = null; AssertExtensions.Throws("keySelector", () => source.ToDictionary(keySelector)); } @@ -221,7 +221,7 @@ public void ToDictionary_ThrowArgumentNullExceptionWhenKeySelectorIsNull() [Fact] public void ToDictionary_ThrowArgumentNullExceptionWhenValueSelectorIsNull() { - int[] source = new int[0]; + int[] source = []; Func keySelector = key => key; Func valueSelector = null; AssertExtensions.Throws("elementSelector", () => source.ToDictionary(keySelector, valueSelector)); @@ -238,7 +238,7 @@ public void ToDictionary_ThrowArgumentNullExceptionWhenSourceIsNullElementSelect [Fact] public void ToDictionary_ThrowArgumentNullExceptionWhenKeySelectorIsNullElementSelector() { - int[] source = new int[0]; + int[] source = []; Func keySelector = null; AssertExtensions.Throws("keySelector", () => source.ToDictionary(keySelector, e => e)); } @@ -246,7 +246,7 @@ public void ToDictionary_ThrowArgumentNullExceptionWhenKeySelectorIsNullElementS [Fact] public void ToDictionary_KeySelectorThrowException() { - int[] source = new int[] { 1, 2, 3 }; + int[] source = [1, 2, 3]; Func keySelector = key => { if (key == 1) @@ -261,7 +261,7 @@ public void ToDictionary_KeySelectorThrowException() [Fact] public void ToDictionary_ThrowWhenKeySelectorReturnNull() { - int[] source = new int[] { 1, 2, 3 }; + int[] source = [1, 2, 3]; Func keySelector = key => null; AssertExtensions.Throws("key", () => source.ToDictionary(keySelector)); @@ -270,7 +270,7 @@ public void ToDictionary_ThrowWhenKeySelectorReturnNull() [Fact] public void ToDictionary_ThrowWhenKeySelectorReturnSameValueTwice() { - int[] source = new int[] { 1, 2, 3 }; + int[] source = [1, 2, 3]; Func keySelector = key => 1; AssertExtensions.Throws(null, () => source.ToDictionary(keySelector)); @@ -279,7 +279,7 @@ public void ToDictionary_ThrowWhenKeySelectorReturnSameValueTwice() [Fact] public void ToDictionary_ValueSelectorThrowException() { - int[] source = new int[] { 1, 2, 3 }; + int[] source = [1, 2, 3]; Func keySelector = key => key; Func valueSelector = value => { @@ -303,12 +303,12 @@ public void ThrowsOnNullKey() source.ToDictionary(e => e.Name); // Doesn't throw; - source = new[] - { + source = + [ new { Name = "Chris", Score = 50 }, new { Name = "Bob", Score = 95 }, new { Name = default(string), Score = 55 } - }; + ]; AssertExtensions.Throws("key", () => source.ToDictionary(e => e.Name)); @@ -343,12 +343,12 @@ public void ThrowsOnNullKeyCustomComparer() source.ToDictionary(e => e.Name, new AnagramEqualityComparer()); // Doesn't throw; - source = new[] - { + source = + [ new { Name = "Chris", Score = 50 }, new { Name = "Bob", Score = 95 }, new { Name = default(string), Score = 55 } - }; + ]; AssertExtensions.Throws("key", () => source.ToDictionary(e => e.Name, new AnagramEqualityComparer())); } @@ -365,12 +365,12 @@ public void ThrowsOnNullKeyValueSelector() source.ToDictionary(e => e.Name, e => e); // Doesn't throw; - source = new[] - { + source = + [ new { Name = "Chris", Score = 50 }, new { Name = "Bob", Score = 95 }, new { Name = default(string), Score = 55 } - }; + ]; AssertExtensions.Throws("key", () => source.ToDictionary(e => e.Name, e => e)); } @@ -387,12 +387,12 @@ public void ThrowsOnNullKeyCustomComparerValueSelector() source.ToDictionary(e => e.Name, e => e, new AnagramEqualityComparer()); // Doesn't throw; - source = new[] - { + source = + [ new { Name = "Chris", Score = 50 }, new { Name = "Bob", Score = 95 }, new { Name = default(string), Score = 55 } - }; + ]; AssertExtensions.Throws("key", () => source.ToDictionary(e => e.Name, e => e, new AnagramEqualityComparer())); } @@ -433,27 +433,26 @@ private static void AssertMatches(IEnumerable keys, IEnumerable valu Assert.NotNull(dict); Assert.NotNull(keys); Assert.NotNull(values); - using (var ke = keys.GetEnumerator()) + + using var ke = keys.GetEnumerator(); + foreach (var value in values) { - foreach (var value in values) - { - Assert.True(ke.MoveNext()); - var key = ke.Current; - E dictValue; - Assert.True(dict.TryGetValue(key, out dictValue)); - Assert.Equal(value, dictValue); - dict.Remove(key); - } - Assert.False(ke.MoveNext()); - Assert.Empty(dict); + Assert.True(ke.MoveNext()); + var key = ke.Current; + E dictValue; + Assert.True(dict.TryGetValue(key, out dictValue)); + Assert.Equal(value, dictValue); + dict.Remove(key); } + Assert.False(ke.MoveNext()); + Assert.Empty(dict); } [Fact] public void EmptySource() { - int[] elements = new int[] { }; - string[] keys = new string[] { }; + int[] elements = []; + string[] keys = []; var source = keys.Zip(elements, (k, e) => new { Name = k, Score = e }); AssertMatches(keys, elements, source.ToDictionary(e => e.Name, e => e.Score, new AnagramEqualityComparer())); @@ -462,8 +461,8 @@ public void EmptySource() [Fact] public void OneElementNullComparer() { - int[] elements = new int[] { 5 }; - string[] keys = new string[] { "Bob" }; + int[] elements = [5]; + string[] keys = ["Bob"]; var source = new[] { new { Name = keys[0], Score = elements[0] } }; AssertMatches(keys, elements, source.ToDictionary(e => e.Name, e => e.Score, null)); @@ -472,7 +471,7 @@ public void OneElementNullComparer() [Fact] public void SeveralElementsCustomComparerer() { - string[] keys = new string[] { "Bob", "Zen", "Prakash", "Chris", "Sachin" }; + string[] keys = ["Bob", "Zen", "Prakash", "Chris", "Sachin"]; var source = new[] { new { Name = "Bbo", Score = 95 }, @@ -488,9 +487,9 @@ public void SeveralElementsCustomComparerer() [Fact] public void NullCoalescedKeySelector() { - string[] elements = new string[] { null }; - string[] keys = new string[] { string.Empty }; - string[] source = new string[] { null }; + string[] elements = [null]; + string[] keys = [string.Empty]; + string[] source = [null]; AssertMatches(keys, elements, source.ToDictionary(e => e ?? string.Empty, e => e, EqualityComparer.Default)); diff --git a/tests/System.Linq.Tests/Tests/ZLinq/ToListTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/ToListTests.cs index 8a2571db..a6d14379 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/ToListTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/ToListTests.cs @@ -47,7 +47,7 @@ public void ToList_WorkWithEmptyCollection() [Fact] public void ToList_ProduceCorrectList() { - int[] sourceArray = new int[] { 1, 2, 3, 4, 5, 6, 7 }; + int[] sourceArray = [1, 2, 3, 4, 5, 6, 7]; RunToListOnAllCollectionTypes(sourceArray, resultList => { @@ -55,7 +55,7 @@ public void ToList_ProduceCorrectList() Assert.Equal(sourceArray, resultList); }); - string[] sourceStringArray = new string[] { "1", "2", "3", "4", "5", "6", "7", "8" }; + string[] sourceStringArray = ["1", "2", "3", "4", "5", "6", "7", "8"]; RunToListOnAllCollectionTypes(sourceStringArray, resultStringList => { @@ -74,7 +74,7 @@ public void RunOnce() [Fact] public void ToList_TouchCountWithICollection() { - TestCollection source = new TestCollection(new int[] { 1, 2, 3, 4 }); + TestCollection source = new TestCollection([1, 2, 3, 4]); var resultList = source.ToList(); Assert.Equal(source, resultList); @@ -93,7 +93,7 @@ public void ToList_ThrowArgumentNullExceptionWhenSourceIsNull() [Fact(Skip = SkipReason.ICollectionCopyTo)] public void ToList_UseCopyToWithICollection() { - TestCollection source = new TestCollection(new int[] { 1, 2, 3, 4 }); + TestCollection source = new TestCollection([1, 2, 3, 4]); var resultList = source.ToList(); Assert.Equal(source, resultList); @@ -160,13 +160,13 @@ public void ToList_IListWhereSelect(int[] sourceIntegers, string[] convertedStri Assert.Equal(convertedList, sourceList.Select(i => i.ToString()).ToList()); Assert.Equal(sourceList, sourceList.Where(i => true).ToList()); - Assert.Equal(ReadOnlyCollection.Empty, sourceList.Where(i => false).ToList()); + Assert.Equal([], sourceList.Where(i => false).ToList()); Assert.Equal(convertedList, sourceList.Where(i => true).Select(i => i.ToString()).ToList()); - Assert.Equal(ReadOnlyCollection.Empty, sourceList.Where(i => false).Select(i => i.ToString()).ToList()); + Assert.Equal([], sourceList.Where(i => false).Select(i => i.ToString()).ToList()); Assert.Equal(convertedList, sourceList.Select(i => i.ToString()).Where(s => s is not null).ToList()); - Assert.Equal(ReadOnlyCollection.Empty, sourceList.Select(i => i.ToString()).Where(s => s is null).ToList()); + Assert.Equal([], sourceList.Select(i => i.ToString()).Where(s => s is null).ToList()); } [Fact] @@ -192,7 +192,7 @@ public void SameResultsRepeatCallsFromWhereOnStringQuery() [Fact] public void SourceIsEmptyICollectionT() { - int[] source = { }; + int[] source = []; ICollection collection = source as ICollection; @@ -203,8 +203,8 @@ public void SourceIsEmptyICollectionT() [Fact] public void SourceIsICollectionTWithFewElements() { - int?[] source = { -5, null, 0, 10, 3, -1, null, 4, 9 }; - int?[] expected = { -5, null, 0, 10, 3, -1, null, 4, 9 }; + int?[] source = [-5, null, 0, 10, 3, -1, null, 4, 9]; + int?[] expected = [-5, null, 0, 10, 3, -1, null, 4, 9]; ICollection collection = source as ICollection; @@ -223,7 +223,7 @@ public void SourceNotICollectionAndIsEmpty() public void SourceNotICollectionAndHasElements() { IEnumerable source = NumberRangeGuaranteedNotCollectionType(-4, 10); - int[] expected = { -4, -3, -2, -1, 0, 1, 2, 3, 4, 5 }; + int[] expected = [-4, -3, -2, -1, 0, 1, 2, 3, 4, 5]; Assert.Null(source as ICollection); @@ -234,7 +234,7 @@ public void SourceNotICollectionAndHasElements() public void SourceNotICollectionAndAllNull() { IEnumerable source = RepeatedNullableNumberGuaranteedNotCollectionType(null, 5); - int?[] expected = { null, null, null, null, null }; + int?[] expected = [null, null, null, null, null]; Assert.Null(source as ICollection); @@ -245,14 +245,14 @@ public void SourceNotICollectionAndAllNull() public void ConstantTimeCountPartitionSelectSameTypeToList() { var source = Enumerable.Range(0, 100).Select(i => i * 2).Skip(1).Take(5); - Assert.Equal(new[] { 2, 4, 6, 8, 10 }, source.ToList()); + Assert.Equal([2, 4, 6, 8, 10], source.ToList()); } [Fact] public void ConstantTimeCountPartitionSelectDiffTypeToList() { var source = Enumerable.Range(0, 100).Select(i => i.ToString()).Skip(1).Take(5); - Assert.Equal(new[] { "1", "2", "3", "4", "5" }, source.ToList()); + Assert.Equal(["1", "2", "3", "4", "5"], source.ToList()); } [Fact] @@ -273,14 +273,14 @@ public void ConstantTimeCountEmptyPartitionSelectDiffTypeToList() public void NonConstantTimeCountPartitionSelectSameTypeToList() { var source = NumberRangeGuaranteedNotCollectionType(0, 100).OrderBy(i => i).Select(i => i * 2).Skip(1).Take(5); - Assert.Equal(new[] { 2, 4, 6, 8, 10 }, source.ToList()); + Assert.Equal([2, 4, 6, 8, 10], source.ToList()); } [Fact] public void NonConstantTimeCountPartitionSelectDiffTypeToList() { var source = NumberRangeGuaranteedNotCollectionType(0, 100).OrderBy(i => i).Select(i => i.ToString()).Skip(1).Take(5); - Assert.Equal(new[] { "1", "2", "3", "4", "5" }, source.ToList()); + Assert.Equal(["1", "2", "3", "4", "5"], source.ToList()); } [Fact] diff --git a/tests/System.Linq.Tests/Tests/ZLinq/ToLookupTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/ToLookupTests.cs index 8046b97d..fd7b3009 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/ToLookupTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/ToLookupTests.cs @@ -52,7 +52,7 @@ public void SameResultsRepeatCall() [Fact] public void Empty() { - AssertMatches(Enumerable.Empty(), Enumerable.Empty(), Enumerable.Empty().ToLookup(i => i)); + AssertMatches([], [], Enumerable.Empty().ToLookup(i => i)); Assert.False(Enumerable.Empty().ToLookup(i => i).Contains(0)); Assert.Empty(Enumerable.Empty().ToLookup(i => i)[0]); } @@ -60,8 +60,8 @@ public void Empty() [Fact] public void NullKeyIncluded() { - string[] key = { "Chris", "Bob", null, "Tim" }; - int[] element = { 50, 95, 55, 90 }; + string[] key = ["Chris", "Bob", null, "Tim"]; + int[] element = [50, 95, 55, 90]; var source = key.Zip(element, (k, e) => new { Name = k, Score = e }); AssertMatches(key, source.ToArray(), source.ToLookup(e => e.Name)); @@ -70,8 +70,8 @@ public void NullKeyIncluded() [Fact] public void OneElementCustomComparer() { - string[] key = { "Chris" }; - int[] element = { 50 }; + string[] key = ["Chris"]; + int[] element = [50]; var source = new[] { new { Name = "risCh", Score = 50 } }; AssertMatches(key, source, source.ToLookup(e => e.Name, new AnagramEqualityComparer())); @@ -80,8 +80,8 @@ public void OneElementCustomComparer() [Fact] public void UniqueElementsElementSelector() { - string[] key = { "Chris", "Prakash", "Tim", "Robert", "Brian" }; - int[] element = { 50, 100, 95, 60, 80 }; + string[] key = ["Chris", "Prakash", "Tim", "Robert", "Brian"]; + int[] element = [50, 100, 95, 60, 80]; var source = new[] { new { Name = key[0], Score = element[0] }, @@ -97,8 +97,8 @@ public void UniqueElementsElementSelector() [Fact] public void DuplicateKeys() { - string[] key = { "Chris", "Prakash", "Robert" }; - int[] element = { 50, 80, 100, 95, 99, 56 }; + string[] key = ["Chris", "Prakash", "Robert"]; + int[] element = [50, 80, 100, 95, 99, 56]; var source = new[] { new { Name = key[0], Score = element[0] }, @@ -115,8 +115,8 @@ public void DuplicateKeys() [Fact] public void RunOnce() { - string[] key = { "Chris", "Prakash", "Robert" }; - int[] element = { 50, 80, 100, 95, 99, 56 }; + string[] key = ["Chris", "Prakash", "Robert"]; + int[] element = [50, 80, 100, 95, 99, 56]; var source = new[] { new { Name = key[0], Score = element[0] }, @@ -133,8 +133,8 @@ public void RunOnce() [Fact] public void Count() { - string[] key = { "Chris", "Prakash", "Robert" }; - int[] element = { 50, 80, 100, 95, 99, 56 }; + string[] key = ["Chris", "Prakash", "Robert"]; + int[] element = [50, 80, 100, 95, 99, 56]; var source = new[] { new { Name = key[0], Score = element[0] }, @@ -151,8 +151,8 @@ public void Count() [Fact] public void EmptySource() { - string[] key = { }; - int[] element = { }; + string[] key = []; + int[] element = []; var source = key.Zip(element, (k, e) => new { Name = k, Score = e }); AssertMatches(key, element, source.ToLookup(e => e.Name, e => e.Score, new AnagramEqualityComparer())); @@ -161,9 +161,9 @@ public void EmptySource() [Fact] public void SingleNullKeyAndElement() { - string[] key = { null }; - string[] element = { null }; - string[] source = new string[] { null }; + string[] key = [null]; + string[] element = [null]; + string[] source = [null]; AssertMatches(key, element, source.ToLookup(e => e, e => e, EqualityComparer.Default)); } @@ -344,7 +344,7 @@ public void LookupImplementsICollection(int count) private sealed class NopGrouping : IGrouping { public string Key => ""; - public IEnumerator GetEnumerator() => ((IList)Array.Empty()).GetEnumerator(); + public IEnumerator GetEnumerator() => ((IList)[]).GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); } diff --git a/tests/System.Linq.Tests/Tests/ZLinq/UnionTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/UnionTests.cs index 3989362a..a44998be 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/UnionTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/UnionTests.cs @@ -74,27 +74,27 @@ public void SameResultsRepeatCallsMultipleUnions() [Fact] public void BothEmpty() { - int[] first = { }; - int[] second = { }; + int[] first = []; + int[] second = []; Assert.Empty(first.Union(second)); } [Fact] public void ManyEmpty() { - int[] first = { }; - int[] second = { }; - int[] third = { }; - int[] fourth = { }; + int[] first = []; + int[] second = []; + int[] third = []; + int[] fourth = []; Assert.Empty(first.Union(second).Union(third).Union(fourth)); } [Fact] public void CustomComparer() { - string[] first = { "Bob", "Robert", "Tim", "Matt", "miT" }; - string[] second = { "ttaM", "Charlie", "Bbo" }; - string[] expected = { "Bob", "Robert", "Tim", "Matt", "Charlie" }; + string[] first = ["Bob", "Robert", "Tim", "Matt", "miT"]; + string[] second = ["ttaM", "Charlie", "Bbo"]; + string[] expected = ["Bob", "Robert", "Tim", "Matt", "Charlie"]; var comparer = new AnagramEqualityComparer(); @@ -104,9 +104,9 @@ public void CustomComparer() [Fact] public void RunOnce() { - string[] first = { "Bob", "Robert", "Tim", "Matt", "miT" }; - string[] second = { "ttaM", "Charlie", "Bbo" }; - string[] expected = { "Bob", "Robert", "Tim", "Matt", "Charlie" }; + string[] first = ["Bob", "Robert", "Tim", "Matt", "miT"]; + string[] second = ["ttaM", "Charlie", "Bbo"]; + string[] expected = ["Bob", "Robert", "Tim", "Matt", "Charlie"]; var comparer = new AnagramEqualityComparer(); Assert.Equal(expected, first.RunOnce().Union(second.RunOnce(), comparer), comparer); @@ -116,7 +116,7 @@ public void RunOnce() public void FirstNullCustomComparer() { string[] first = null; - string[] second = { "ttaM", "Charlie", "Bbo" }; + string[] second = ["ttaM", "Charlie", "Bbo"]; var ane = AssertExtensions.Throws(() => first.Union(second, new AnagramEqualityComparer())); } @@ -124,7 +124,7 @@ public void FirstNullCustomComparer() [Fact] public void SecondNullCustomComparer() { - string[] first = { "Bob", "Robert", "Tim", "Matt", "miT" }; + string[] first = ["Bob", "Robert", "Tim", "Matt", "miT"]; string[] second = null; var ane = AssertExtensions.Throws("second", () => first.Union(second, new AnagramEqualityComparer())); @@ -134,7 +134,7 @@ public void SecondNullCustomComparer() public void FirstNullNoComparer() { string[] first = null; - string[] second = { "ttaM", "Charlie", "Bbo" }; + string[] second = ["ttaM", "Charlie", "Bbo"]; var ane = AssertExtensions.Throws(() => first.Union(second)); } @@ -142,7 +142,7 @@ public void FirstNullNoComparer() [Fact] public void SecondNullNoComparer() { - string[] first = { "Bob", "Robert", "Tim", "Matt", "miT" }; + string[] first = ["Bob", "Robert", "Tim", "Matt", "miT"]; string[] second = null; var ane = AssertExtensions.Throws("second", () => first.Union(second)); @@ -151,9 +151,9 @@ public void SecondNullNoComparer() [Fact] public void SingleNullWithEmpty() { - string[] first = { null }; - string[] second = new string[0]; - string[] expected = { null }; + string[] first = [null]; + string[] second = []; + string[] expected = [null]; Assert.Equal(expected, first.Union(second, EqualityComparer.Default)); } @@ -161,9 +161,9 @@ public void SingleNullWithEmpty() [Fact] public void NullEmptyStringMix() { - string[] first = { null, null, string.Empty }; - string[] second = { null, null }; - string[] expected = { null, string.Empty }; + string[] first = [null, null, string.Empty]; + string[] second = [null, null]; + string[] expected = [null, string.Empty]; Assert.Equal(expected, first.Union(second, EqualityComparer.Default)); } @@ -171,9 +171,9 @@ public void NullEmptyStringMix() [Fact] public void DoubleNullWithEmpty() { - string[] first = { null, null }; - string[] second = new string[0]; - string[] expected = { null }; + string[] first = [null, null]; + string[] second = []; + string[] expected = [null]; Assert.Equal(expected, first.Union(second, EqualityComparer.Default)); } @@ -181,9 +181,9 @@ public void DoubleNullWithEmpty() [Fact] public void EmptyWithNonEmpty() { - int[] first = { }; - int[] second = { 2, 4, 5, 3, 2, 3, 9 }; - int[] expected = { 2, 4, 5, 3, 9 }; + int[] first = []; + int[] second = [2, 4, 5, 3, 2, 3, 9]; + int[] expected = [2, 4, 5, 3, 9]; Assert.Equal(expected, first.Union(second)); } @@ -191,9 +191,9 @@ public void EmptyWithNonEmpty() [Fact] public void NonEmptyWithEmpty() { - int[] first = { 2, 4, 5, 3, 2, 3, 9 }; - int[] second = { }; - int[] expected = { 2, 4, 5, 3, 9 }; + int[] first = [2, 4, 5, 3, 2, 3, 9]; + int[] second = []; + int[] expected = [2, 4, 5, 3, 9]; Assert.Equal(expected, first.Union(second)); } @@ -201,9 +201,9 @@ public void NonEmptyWithEmpty() [Fact] public void CommonElementsShared() { - int[] first = { 1, 2, 3, 4, 5, 6 }; - int[] second = { 6, 7, 7, 7, 8, 1 }; - int[] expected = { 1, 2, 3, 4, 5, 6, 7, 8 }; + int[] first = [1, 2, 3, 4, 5, 6]; + int[] second = [6, 7, 7, 7, 8, 1]; + int[] expected = [1, 2, 3, 4, 5, 6, 7, 8]; Assert.Equal(expected, first.Union(second)); } @@ -211,9 +211,9 @@ public void CommonElementsShared() [Fact] public void SameElementRepeated() { - int[] first = { 1, 1, 1, 1, 1, 1 }; - int[] second = { 1, 1, 1, 1, 1, 1 }; - int[] expected = { 1 }; + int[] first = [1, 1, 1, 1, 1, 1]; + int[] second = [1, 1, 1, 1, 1, 1]; + int[] expected = [1]; Assert.Equal(expected, first.Union(second)); } @@ -221,9 +221,9 @@ public void SameElementRepeated() [Fact] public void RepeatedElementsWithSingleElement() { - int[] first = { 1, 2, 3, 5, 3, 6 }; - int[] second = { 7 }; - int[] expected = { 1, 2, 3, 5, 6, 7 }; + int[] first = [1, 2, 3, 5, 3, 6]; + int[] second = [7]; + int[] expected = [1, 2, 3, 5, 6, 7]; Assert.Equal(expected, first.Union(second)); } @@ -231,9 +231,9 @@ public void RepeatedElementsWithSingleElement() [Fact] public void SingleWithAllUnique() { - int?[] first = { 2 }; - int?[] second = { 3, null, 4, 5 }; - int?[] expected = { 2, 3, null, 4, 5 }; + int?[] first = [2]; + int?[] second = [3, null, 4, 5]; + int?[] expected = [2, 3, null, 4, 5]; Assert.Equal(expected, first.Union(second)); } @@ -241,9 +241,9 @@ public void SingleWithAllUnique() [Fact] public void EachHasRepeatsBetweenAndAmongstThemselves() { - int?[] first = { 1, 2, 3, 4, null, 5, 1 }; - int?[] second = { 6, 2, 3, 4, 5, 6 }; - int?[] expected = { 1, 2, 3, 4, null, 5, 6 }; + int?[] first = [1, 2, 3, 4, null, 5, 1]; + int?[] second = [6, 2, 3, 4, 5, 6]; + int?[] expected = [1, 2, 3, 4, null, 5, 6]; Assert.Equal(expected, first.Union(second)); } @@ -251,11 +251,11 @@ public void EachHasRepeatsBetweenAndAmongstThemselves() [Fact] public void EachHasRepeatsBetweenAndAmongstThemselvesMultipleUnions() { - int?[] first = { 1, 2, 3, 4, null, 5, 1 }; - int?[] second = { 6, 2, 3, 4, 5, 6 }; - int?[] third = { 2, 8, 2, 3, 2, 8 }; - int?[] fourth = { null, 1, 7, 2, 7 }; - int?[] expected = { 1, 2, 3, 4, null, 5, 6, 8, 7 }; + int?[] first = [1, 2, 3, 4, null, 5, 1]; + int?[] second = [6, 2, 3, 4, 5, 6]; + int?[] third = [2, 8, 2, 3, 2, 8]; + int?[] fourth = [null, 1, 7, 2, 7]; + int?[] expected = [1, 2, 3, 4, null, 5, 6, 8, 7]; Assert.Equal(expected, first.Union(second).Union(third).Union(fourth)); } @@ -263,11 +263,11 @@ public void EachHasRepeatsBetweenAndAmongstThemselvesMultipleUnions() [Fact] public void MultipleUnionsCustomComparer() { - int?[] first = { 1, 102, 903, 204, null, 5, 601 }; - int?[] second = { 6, 202, 903, 204, 5, 106 }; - int?[] third = { 2, 308, 2, 103, 802, 308 }; - int?[] fourth = { null, 101, 207, 202, 207 }; - int?[] expected = { 1, 102, 903, 204, null, 5, 6, 308, 207 }; + int?[] first = [1, 102, 903, 204, null, 5, 601]; + int?[] second = [6, 202, 903, 204, 5, 106]; + int?[] third = [2, 308, 2, 103, 802, 308]; + int?[] fourth = [null, 101, 207, 202, 207]; + int?[] expected = [1, 102, 903, 204, null, 5, 6, 308, 207]; Assert.Equal(expected, first.Union(second, new Modulo100EqualityComparer()).Union(third, new Modulo100EqualityComparer()).Union(fourth, new Modulo100EqualityComparer())); } @@ -275,12 +275,12 @@ public void MultipleUnionsCustomComparer() [Fact] public void MultipleUnionsDifferentComparers() { - string[] first = { "Alpha", "Bravo", "Charlie", "Bravo", "Delta", "atleD", "ovarB" }; - string[] second = { "Charlie", "Delta", "Echo", "Foxtrot", "Foxtrot", "choE" }; - string[] third = { "trotFox", "Golf", "Alpha", "choE", "Tango" }; + string[] first = ["Alpha", "Bravo", "Charlie", "Bravo", "Delta", "atleD", "ovarB"]; + string[] second = ["Charlie", "Delta", "Echo", "Foxtrot", "Foxtrot", "choE"]; + string[] third = ["trotFox", "Golf", "Alpha", "choE", "Tango"]; - string[] plainThenAnagram = { "Alpha", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "Golf", "Tango" }; - string[] anagramThenPlain = { "Alpha", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "trotFox", "Golf", "choE", "Tango" }; + string[] plainThenAnagram = ["Alpha", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "Golf", "Tango"]; + string[] anagramThenPlain = ["Alpha", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "trotFox", "Golf", "choE", "Tango"]; Assert.Equal(plainThenAnagram, first.Union(second).Union(third, new AnagramEqualityComparer())); Assert.Equal(anagramThenPlain, first.Union(second, new AnagramEqualityComparer()).Union(third)); @@ -289,9 +289,9 @@ public void MultipleUnionsDifferentComparers() [Fact] public void NullEqualityComparer() { - string[] first = { "Bob", "Robert", "Tim", "Matt", "miT" }; - string[] second = { "ttaM", "Charlie", "Bbo" }; - string[] expected = { "Bob", "Robert", "Tim", "Matt", "miT", "ttaM", "Charlie", "Bbo" }; + string[] first = ["Bob", "Robert", "Tim", "Matt", "miT"]; + string[] second = ["ttaM", "Charlie", "Bbo"]; + string[] expected = ["Bob", "Robert", "Tim", "Matt", "miT", "ttaM", "Charlie", "Bbo"]; Assert.Equal(expected, first.Union(second, null)); } @@ -308,7 +308,11 @@ public void ForcedToEnumeratorDoesntEnumerate() [Fact(Skip = SkipReason.EnumeratorBehaviorDifference)] public void ForcedToEnumeratorDoesntEnumerateMultipleUnions() { - var valueEnumerable = NumberRangeGuaranteedNotCollectionType(0, 3).Union(Enumerable.Range(0, 3)).Union(Enumerable.Range(2, 4)).Union(new[] { 9, 2, 4 }); + var valueEnumerable = NumberRangeGuaranteedNotCollectionType(0, 3) + .Union(Enumerable.Range(0, 3)) + .Union(Enumerable.Range(2, 4)) + .Union([9, 2, 4]); + // Don't insist on this behaviour, but check it's correct if it happens using var en = valueEnumerable.Enumerator; Assert.False(en.TryGetNext(out _)); @@ -317,9 +321,9 @@ public void ForcedToEnumeratorDoesntEnumerateMultipleUnions() [Fact] public void ToArray() { - string[] first = { "Bob", "Robert", "Tim", "Matt", "miT" }; - string[] second = { "ttaM", "Charlie", "Bbo" }; - string[] expected = { "Bob", "Robert", "Tim", "Matt", "miT", "ttaM", "Charlie", "Bbo" }; + string[] first = ["Bob", "Robert", "Tim", "Matt", "miT"]; + string[] second = ["ttaM", "Charlie", "Bbo"]; + string[] expected = ["Bob", "Robert", "Tim", "Matt", "miT", "ttaM", "Charlie", "Bbo"]; Assert.Equal(expected, first.Union(second).ToArray()); } @@ -327,10 +331,10 @@ public void ToArray() [Fact] public void ToArrayMultipleUnion() { - string[] first = { "Bob", "Robert", "Tim", "Matt", "miT" }; - string[] second = { "ttaM", "Charlie", "Bbo" }; - string[] third = { "Bob", "Albert", "Tim" }; - string[] expected = { "Bob", "Robert", "Tim", "Matt", "miT", "ttaM", "Charlie", "Bbo", "Albert" }; + string[] first = ["Bob", "Robert", "Tim", "Matt", "miT"]; + string[] second = ["ttaM", "Charlie", "Bbo"]; + string[] third = ["Bob", "Albert", "Tim"]; + string[] expected = ["Bob", "Robert", "Tim", "Matt", "miT", "ttaM", "Charlie", "Bbo", "Albert"]; Assert.Equal(expected, first.Union(second).Union(third).ToArray()); } @@ -338,9 +342,9 @@ public void ToArrayMultipleUnion() [Fact] public void ToList() { - string[] first = { "Bob", "Robert", "Tim", "Matt", "miT" }; - string[] second = { "ttaM", "Charlie", "Bbo" }; - string[] expected = { "Bob", "Robert", "Tim", "Matt", "miT", "ttaM", "Charlie", "Bbo" }; + string[] first = ["Bob", "Robert", "Tim", "Matt", "miT"]; + string[] second = ["ttaM", "Charlie", "Bbo"]; + string[] expected = ["Bob", "Robert", "Tim", "Matt", "miT", "ttaM", "Charlie", "Bbo"]; Assert.Equal(expected, first.Union(second).ToList()); } @@ -348,10 +352,10 @@ public void ToList() [Fact] public void ToListMultipleUnion() { - string[] first = { "Bob", "Robert", "Tim", "Matt", "miT" }; - string[] second = { "ttaM", "Charlie", "Bbo" }; - string[] third = { "Bob", "Albert", "Tim" }; - string[] expected = { "Bob", "Robert", "Tim", "Matt", "miT", "ttaM", "Charlie", "Bbo", "Albert" }; + string[] first = ["Bob", "Robert", "Tim", "Matt", "miT"]; + string[] second = ["ttaM", "Charlie", "Bbo"]; + string[] third = ["Bob", "Albert", "Tim"]; + string[] expected = ["Bob", "Robert", "Tim", "Matt", "miT", "ttaM", "Charlie", "Bbo", "Albert"]; Assert.Equal(expected, first.Union(second).Union(third).ToList()); } @@ -359,17 +363,17 @@ public void ToListMultipleUnion() [Fact] public void Count() { - string[] first = { "Bob", "Robert", "Tim", "Matt", "miT" }; - string[] second = { "ttaM", "Charlie", "Bbo" }; + string[] first = ["Bob", "Robert", "Tim", "Matt", "miT"]; + string[] second = ["ttaM", "Charlie", "Bbo"]; Assert.Equal(8, first.Union(second).Count()); } [Fact] public void CountMultipleUnion() { - string[] first = { "Bob", "Robert", "Tim", "Matt", "miT" }; - string[] second = { "ttaM", "Charlie", "Bbo" }; - string[] third = { "Bob", "Albert", "Tim" }; + string[] first = ["Bob", "Robert", "Tim", "Matt", "miT"]; + string[] second = ["ttaM", "Charlie", "Bbo"]; + string[] third = ["Bob", "Albert", "Tim"]; Assert.Equal(9, first.Union(second).Union(third).Count()); } @@ -377,8 +381,8 @@ public void CountMultipleUnion() [Fact] public void RepeatEnumerating() { - string[] first = { "Bob", "Robert", "Tim", "Matt", "miT" }; - string[] second = { "ttaM", "Charlie", "Bbo" }; + string[] first = ["Bob", "Robert", "Tim", "Matt", "miT"]; + string[] second = ["ttaM", "Charlie", "Bbo"]; var result = first.Union(second); @@ -388,9 +392,9 @@ public void RepeatEnumerating() [Fact] public void RepeatEnumeratingMultipleUnions() { - string[] first = { "Bob", "Robert", "Tim", "Matt", "miT" }; - string[] second = { "ttaM", "Charlie", "Bbo" }; - string[] third = { "Matt", "Albert", "Ichabod" }; + string[] first = ["Bob", "Robert", "Tim", "Matt", "miT"]; + string[] second = ["ttaM", "Charlie", "Bbo"]; + string[] third = ["Matt", "Albert", "Ichabod"]; var result = first.Union(second).Union(third); @@ -401,24 +405,24 @@ public void RepeatEnumeratingMultipleUnions() public void HashSetWithBuiltInComparer_HashSetContainsNotUsed() { IEnumerable input1 = new HashSet(StringComparer.OrdinalIgnoreCase) { "a" }; - IEnumerable input2 = new[] { "A" }; + IEnumerable input2 = ["A"]; - Assert.Equal(new[] { "a", "A" }, input1.Union(input2)); - Assert.Equal(new[] { "a", "A" }, input1.Union(input2, null)); - Assert.Equal(new[] { "a", "A" }, input1.Union(input2, EqualityComparer.Default)); - Assert.Equal(new[] { "a" }, input1.Union(input2, StringComparer.OrdinalIgnoreCase)); + Assert.Equal(["a", "A"], input1.Union(input2)); + Assert.Equal(["a", "A"], input1.Union(input2, null)); + Assert.Equal(["a", "A"], input1.Union(input2, EqualityComparer.Default)); + Assert.Equal(["a"], input1.Union(input2, StringComparer.OrdinalIgnoreCase)); - Assert.Equal(new[] { "A", "a" }, input2.Union(input1)); - Assert.Equal(new[] { "A", "a" }, input2.Union(input1, null)); - Assert.Equal(new[] { "A", "a" }, input2.Union(input1, EqualityComparer.Default)); - Assert.Equal(new[] { "A" }, input2.Union(input1, StringComparer.OrdinalIgnoreCase)); + Assert.Equal(["A", "a"], input2.Union(input1)); + Assert.Equal(["A", "a"], input2.Union(input1, null)); + Assert.Equal(["A", "a"], input2.Union(input1, EqualityComparer.Default)); + Assert.Equal(["A"], input2.Union(input1, StringComparer.OrdinalIgnoreCase)); } [Fact] public void UnionBy_FirstNull_ThrowsArgumentNullException() { string[] first = null; - string[] second = { "bBo", "shriC" }; + string[] second = ["bBo", "shriC"]; AssertExtensions.Throws(() => first.UnionBy(second, x => x)); AssertExtensions.Throws(() => first.UnionBy(second, x => x, new AnagramEqualityComparer())); @@ -427,7 +431,7 @@ public void UnionBy_FirstNull_ThrowsArgumentNullException() [Fact] public void UnionBy_SecondNull_ThrowsArgumentNullException() { - string[] first = { "Bob", "Tim", "Robert", "Chris" }; + string[] first = ["Bob", "Tim", "Robert", "Chris"]; string[] second = null; AssertExtensions.Throws("second", () => first.UnionBy(second, x => x)); @@ -437,8 +441,8 @@ public void UnionBy_SecondNull_ThrowsArgumentNullException() [Fact] public void UnionBy_KeySelectorNull_ThrowsArgumentNullException() { - string[] first = { "Bob", "Tim", "Robert", "Chris" }; - string[] second = { "bBo", "shriC" }; + string[] first = ["Bob", "Tim", "Robert", "Chris"]; + string[] second = ["bBo", "shriC"]; Func keySelector = null; AssertExtensions.Throws("keySelector", () => first.UnionBy(second, keySelector)); @@ -476,7 +480,7 @@ public static IEnumerable UnionBy_TestData() expected: Enumerable.Range(0, 20)); yield return WrapArgs( - first: Enumerable.Empty(), + first: [], second: Enumerable.Range(0, 5), keySelector: x => x, comparer: null, @@ -484,7 +488,7 @@ public static IEnumerable UnionBy_TestData() yield return WrapArgs( first: Enumerable.Repeat(5, 20), - second: Enumerable.Empty(), + second: [], keySelector: x => x, comparer: null, expected: Enumerable.Repeat(5, 1)); @@ -497,18 +501,18 @@ public static IEnumerable UnionBy_TestData() expected: Enumerable.Repeat(5, 1)); yield return WrapArgs( - first: new string[] { "Bob", "Tim", "Robert", "Chris" }, - second: new string[] { "bBo", "shriC" }, + first: ["Bob", "Tim", "Robert", "Chris"], + second: ["bBo", "shriC"], keySelector: x => x, null, - expected: new string[] { "Bob", "Tim", "Robert", "Chris", "bBo", "shriC" }); + expected: ["Bob", "Tim", "Robert", "Chris", "bBo", "shriC"]); yield return WrapArgs( - first: new string[] { "Bob", "Tim", "Robert", "Chris" }, - second: new string[] { "bBo", "shriC" }, + first: ["Bob", "Tim", "Robert", "Chris"], + second: ["bBo", "shriC"], keySelector: x => x, new AnagramEqualityComparer(), - expected: new string[] { "Bob", "Tim", "Robert", "Chris" }); + expected: ["Bob", "Tim", "Robert", "Chris"]); yield return WrapArgs( first: new (string Name, int Age)[] { ("Tom", 20), ("Dick", 30), ("Harry", 40), ("Martin", 20) }, @@ -532,7 +536,7 @@ public static IEnumerable UnionBy_TestData() expected: new (string Name, int Age)[] { ("Tom", 20), ("Dick", 30), ("Harry", 40), ("Martin", 20), ("Toby", 33) }); object[] WrapArgs(IEnumerable first, IEnumerable second, Func keySelector, IEqualityComparer? comparer, IEnumerable expected) - => new object[] { first, second, keySelector, comparer, expected }; + => [first, second, keySelector, comparer, expected]; } } } diff --git a/tests/System.Linq.Tests/Tests/ZLinq/WhereTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/WhereTests.cs index 915d0a38..28264691 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/WhereTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/WhereTests.cs @@ -42,32 +42,33 @@ public void Where_PredicateIsNull_ArgumentNullExceptionThrown() public void Where_Array_ExecutionIsDeferred() { bool funcCalled = false; - Func[] source = { () => { funcCalled = true; return true; } }; + Func[] source = [() => { funcCalled = true; return true; }]; - { - var query = source.Where(value => value()); - Assert.False(funcCalled); - } - { - var query = source.Where((value, index) => value()); - Assert.False(funcCalled); - } + var query = source.Where(value => value()); + Assert.False(funcCalled); + + var query2 = source.Where((value, index) => value()); + Assert.False(funcCalled); } [Fact] public void Where_List_ExecutionIsDeferred() { bool funcCalled = false; - List> source = new List>() { () => { funcCalled = true; return true; } }; + List> source = + [ + () => + { + funcCalled = true; + return true; + } + ]; - { - var query = source.Where(value => value()); - Assert.False(funcCalled); - } - { - var query = source.Where((value, index) => value()); - Assert.False(funcCalled); - } + var query = source.Where(value => value()); + Assert.False(funcCalled); + + var query2 = source.Where((value, index) => value()); + Assert.False(funcCalled); } [Fact] @@ -76,14 +77,11 @@ public void Where_IReadOnlyCollection_ExecutionIsDeferred() bool funcCalled = false; IReadOnlyCollection> source = new ReadOnlyCollection>(new List>() { () => { funcCalled = true; return true; } }); - { - var query = source.Where(value => value()); - Assert.False(funcCalled); - } - { - var query = source.Where((value, index) => value()); - Assert.False(funcCalled); - } + var query = source.Where(value => value()); + Assert.False(funcCalled); + + var query2 = source.Where((value, index) => value()); + Assert.False(funcCalled); } [Fact] @@ -92,14 +90,11 @@ public void Where_ICollection_ExecutionIsDeferred() bool funcCalled = false; ICollection> source = new LinkedList>(new List>() { () => { funcCalled = true; return true; } }); - { - var query = source.Where(value => value()); - Assert.False(funcCalled); - } - { - var query = source.Where((value, index) => value()); - Assert.False(funcCalled); - } + var query = source.Where(value => value()); + Assert.False(funcCalled); + + var query2 = source.Where((value, index) => value()); + Assert.False(funcCalled); } [Fact] @@ -108,46 +103,44 @@ public void Where_IEnumerable_ExecutionIsDeferred() bool funcCalled = false; IEnumerable> source = Enumerable.Repeat((Func)(() => { funcCalled = true; return true; }), 1); - { - var query = source.Where(value => value()); - Assert.False(funcCalled); - } - { - var query = source.Where((value, index) => value()); - Assert.False(funcCalled); - } + var query = source.Where(value => value()); + Assert.False(funcCalled); + + var query2 = source.Where((value, index) => value()); + Assert.False(funcCalled); } [Fact] public void WhereWhere_Array_ExecutionIsDeferred() { bool funcCalled = false; - Func[] source = new Func[] { () => { funcCalled = true; return true; } }; + Func[] source = [() => { funcCalled = true; return true; }]; - { - var query = source.Where(value => value()).Where(value => value()); - Assert.False(funcCalled); - } - { - var query = source.Where((value, index) => value()); - Assert.False(funcCalled); - } + var query = source.Where(value => value()).Where(value => value()); + Assert.False(funcCalled); + + var query2 = source.Where((value, index) => value()); + Assert.False(funcCalled); } [Fact] public void WhereWhere_List_ExecutionIsDeferred() { bool funcCalled = false; - List> source = new List>() { () => { funcCalled = true; return true; } }; + List> source = + [ + () => + { + funcCalled = true; + return true; + } + ]; - { - var query = source.Where(value => value()).Where(value => value()); - Assert.False(funcCalled); - } - { - var query = source.Where((value, index) => value()); - Assert.False(funcCalled); - } + var query = source.Where(value => value()).Where(value => value()); + Assert.False(funcCalled); + + var query2 = source.Where((value, index) => value()); + Assert.False(funcCalled); } [Fact] @@ -156,14 +149,11 @@ public void WhereWhere_IReadOnlyCollection_ExecutionIsDeferred() bool funcCalled = false; IReadOnlyCollection> source = new ReadOnlyCollection>(new List>() { () => { funcCalled = true; return true; } }); - { - var query = source.Where(value => value()).Where(value => value()); - Assert.False(funcCalled); - } - { - var query = source.Where((value, index) => value()); - Assert.False(funcCalled); - } + var query = source.Where(value => value()).Where(value => value()); + Assert.False(funcCalled); + + var query2 = source.Where((value, index) => value()); + Assert.False(funcCalled); } [Fact] @@ -172,14 +162,11 @@ public void WhereWhere_ICollection_ExecutionIsDeferred() bool funcCalled = false; ICollection> source = new LinkedList>(new List>() { () => { funcCalled = true; return true; } }); - { - var query = source.Where(value => value()).Where(value => value()); - Assert.False(funcCalled); - } - { - var query = source.Where((value, index) => value()); - Assert.False(funcCalled); - } + var query = source.Where(value => value()).Where(value => value()); + Assert.False(funcCalled); + + var query2 = source.Where((value, index) => value()); + Assert.False(funcCalled); } [Fact] @@ -188,14 +175,11 @@ public void WhereWhere_IEnumerable_ExecutionIsDeferred() bool funcCalled = false; IEnumerable> source = Enumerable.Repeat((Func)(() => { funcCalled = true; return true; }), 1); - { - var query = source.Where(value => value()).Where(value => value()); - Assert.False(funcCalled); - } - { - var query = source.Where((value, index) => value()); - Assert.False(funcCalled); - } + var query = source.Where(value => value()).Where(value => value()); + Assert.False(funcCalled); + + var query2 = source.Where((value, index) => value()); + Assert.False(funcCalled); } #endregion @@ -205,7 +189,7 @@ public void WhereWhere_IEnumerable_ExecutionIsDeferred() [Fact] public void Where_Array_ReturnsExpectedValues_True() { - int[] source = new[] { 1, 2, 3, 4, 5 }; + int[] source = [1, 2, 3, 4, 5]; Func truePredicate = (value) => true; var result = source.Where(truePredicate); @@ -220,7 +204,7 @@ public void Where_Array_ReturnsExpectedValues_True() [Fact] public void Where_Array_ReturnsExpectedValues_False() { - int[] source = new[] { 1, 2, 3, 4, 5 }; + int[] source = [1, 2, 3, 4, 5]; Func falsePredicate = (value) => false; var result = source.Where(falsePredicate); @@ -231,7 +215,7 @@ public void Where_Array_ReturnsExpectedValues_False() [Fact] public void Where_Array_ReturnsExpectedValues_Complex() { - int[] source = new[] { 2, 1, 3, 5, 4 }; + int[] source = [2, 1, 3, 5, 4]; Func complexPredicate = (value, index) => { return (value == index); }; var result = source.Where(complexPredicate); @@ -244,7 +228,7 @@ public void Where_Array_ReturnsExpectedValues_Complex() [Fact] public void Where_List_ReturnsExpectedValues_True() { - List source = new List { 1, 2, 3, 4, 5 }; + List source = [1, 2, 3, 4, 5]; Func truePredicate = (value) => true; var result = source.Where(truePredicate); @@ -259,7 +243,7 @@ public void Where_List_ReturnsExpectedValues_True() [Fact] public void Where_List_ReturnsExpectedValues_False() { - List source = new List { 1, 2, 3, 4, 5 }; + List source = [1, 2, 3, 4, 5]; Func falsePredicate = (value) => false; var result = source.Where(falsePredicate); @@ -270,7 +254,7 @@ public void Where_List_ReturnsExpectedValues_False() [Fact] public void Where_List_ReturnsExpectedValues_Complex() { - List source = new List { 2, 1, 3, 5, 4 }; + List source = [2, 1, 3, 5, 4]; Func complexPredicate = (value, index) => { return (value == index); }; var result = source.Where(complexPredicate); @@ -400,7 +384,7 @@ public void Where_IEnumerable_ReturnsExpectedValues_Complex() [Fact] public void Where_EmptyEnumerable_ReturnsNoElements() { - IEnumerable source = Enumerable.Empty(); + IEnumerable source = []; bool wasSelectorCalled = false; var result = source.Where(value => { wasSelectorCalled = true; return true; }); @@ -418,10 +402,10 @@ public void Where_EmptyEnumerable_ReturnsNoElementsWithIndex() [Fact] public void Where_Array_CurrentIsDefaultOfTAfterEnumeration() { - int[] source = new[] { 1 }; + int[] source = [1]; Func truePredicate = (value) => true; - var enumerator = source.Where(truePredicate).GetEnumerator(); + using var enumerator = source.Where(truePredicate).GetEnumerator(); while (enumerator.MoveNext()) ; Assert.Equal(default(int), enumerator.Current); @@ -430,10 +414,10 @@ public void Where_Array_CurrentIsDefaultOfTAfterEnumeration() [Fact] public void Where_List_CurrentIsDefaultOfTAfterEnumeration() { - List source = new List() { 1 }; + List source = [1]; Func truePredicate = (value) => true; - var enumerator = source.Where(truePredicate).GetEnumerator(); + using var enumerator = source.Where(truePredicate).GetEnumerator(); while (enumerator.MoveNext()) ; Assert.Equal(default(int), enumerator.Current); @@ -445,7 +429,7 @@ public void Where_IReadOnlyCollection_CurrentIsDefaultOfTAfterEnumeration() IReadOnlyCollection source = new ReadOnlyCollection(new List() { 1 }); Func truePredicate = (value) => true; - var enumerator = source.Where(truePredicate).GetEnumerator(); + using var enumerator = source.Where(truePredicate).GetEnumerator(); while (enumerator.MoveNext()) ; Assert.Equal(default(int), enumerator.Current); @@ -469,7 +453,7 @@ public void Where_IEnumerable_CurrentIsDefaultOfTAfterEnumeration() IEnumerable source = Enumerable.Repeat(1, 1); Func truePredicate = (value) => true; - var enumerator = source.Where(truePredicate).GetEnumerator(); + using var enumerator = source.Where(truePredicate).GetEnumerator(); while (enumerator.MoveNext()) ; Assert.Equal(default(int), enumerator.Current); @@ -478,7 +462,7 @@ public void Where_IEnumerable_CurrentIsDefaultOfTAfterEnumeration() [Fact] public void WhereWhere_Array_ReturnsExpectedValues() { - int[] source = new[] { 1, 2, 3, 4, 5 }; + int[] source = [1, 2, 3, 4, 5]; Func evenPredicate = (value) => value % 2 == 0; var result = source.Where(evenPredicate).Where(evenPredicate); @@ -491,7 +475,7 @@ public void WhereWhere_Array_ReturnsExpectedValues() [Fact] public void WhereWhere_List_ReturnsExpectedValues() { - List source = new List { 1, 2, 3, 4, 5 }; + List source = [1, 2, 3, 4, 5]; Func evenPredicate = (value) => value % 2 == 0; var result = source.Where(evenPredicate).Where(evenPredicate); @@ -543,7 +527,7 @@ public void WhereWhere_IEnumerable_ReturnsExpectedValues() [Fact] public void WhereSelect_Array_ReturnsExpectedValues() { - int[] source = new[] { 1, 2, 3, 4, 5 }; + int[] source = [1, 2, 3, 4, 5]; Func evenPredicate = (value) => value % 2 == 0; Func addSelector = (value) => value + 1; @@ -557,7 +541,7 @@ public void WhereSelect_Array_ReturnsExpectedValues() [Fact] public void WhereSelectSelect_Array_ReturnsExpectedValues() { - int[] source = new[] { 1, 2, 3, 4, 5 }; + int[] source = [1, 2, 3, 4, 5]; Func evenPredicate = (value) => value % 2 == 0; Func addSelector = (value) => value + 1; @@ -571,7 +555,7 @@ public void WhereSelectSelect_Array_ReturnsExpectedValues() [Fact] public void WhereSelect_List_ReturnsExpectedValues() { - List source = new List { 1, 2, 3, 4, 5 }; + List source = [1, 2, 3, 4, 5]; Func evenPredicate = (value) => value % 2 == 0; Func addSelector = (value) => value + 1; @@ -585,7 +569,7 @@ public void WhereSelect_List_ReturnsExpectedValues() [Fact] public void WhereSelectSelect_List_ReturnsExpectedValues() { - List source = new List { 1, 2, 3, 4, 5 }; + List source = [1, 2, 3, 4, 5]; Func evenPredicate = (value) => value % 2 == 0; Func addSelector = (value) => value + 1; @@ -683,7 +667,7 @@ public void WhereSelectSelect_IEnumerable_ReturnsExpectedValues() [Fact] public void SelectWhere_Array_ReturnsExpectedValues() { - int[] source = new[] { 1, 2, 3, 4, 5 }; + int[] source = [1, 2, 3, 4, 5]; Func evenPredicate = (value) => value % 2 == 0; Func addSelector = (value) => value + 1; @@ -698,7 +682,7 @@ public void SelectWhere_Array_ReturnsExpectedValues() [Fact] public void SelectWhere_List_ReturnsExpectedValues() { - List source = new List { 1, 2, 3, 4, 5 }; + List source = [1, 2, 3, 4, 5]; Func evenPredicate = (value) => value % 2 == 0; Func addSelector = (value) => value + 1; @@ -762,7 +746,7 @@ public void SelectWhere_IEnumerable_ReturnsExpectedValues() [Fact] public void Where_PredicateThrowsException() { - int[] source = new[] { 1, 2, 3, 4, 5 }; + int[] source = [1, 2, 3, 4, 5]; Func predicate = value => { if (value == 1) @@ -816,6 +800,7 @@ public void Where_SourceThrowsOnCurrent() Assert.Equal(2, enumerator.Current); } + [Fact] public void Where_SourceThrowsOnMoveNext() { @@ -873,7 +858,7 @@ public void Where_SourceThrowsOnGetEnumerator() [Fact(Skip = SkipReason.EnumeratorReset)] public void Select_ResetEnumerator_ThrowsException() { - int[] source = new[] { 1, 2, 3, 4, 5 }; + int[] source = [1, 2, 3, 4, 5]; var enumerator = source.Where(value => true).GetEnumerator(); // The .NET Framework throws a NotImplementedException. @@ -910,7 +895,7 @@ public void Where_SourceThrowsOnConcurrentModification() [Fact(Skip = SkipReason.RefStruct)] public void Where_GetEnumeratorReturnsUniqueInstances() { - int[] source = new[] { 1, 2, 3, 4, 5 }; + int[] source = [1, 2, 3, 4, 5]; var result = source.Where(value => true); @@ -947,51 +932,51 @@ public void SameResultsRepeatCallsStringQuery() [Fact] public void SingleElementPredicateFalse() { - int[] source = { 3 }; + int[] source = [3]; Assert.DoesNotContain(source, IsEven); } [Fact] public void PredicateFalseForAll() { - int[] source = { 9, 7, 15, 3, 27 }; + int[] source = [9, 7, 15, 3, 27]; Assert.DoesNotContain(source, IsEven); } [Fact] public void PredicateTrueFirstOnly() { - int[] source = { 10, 9, 7, 15, 3, 27 }; + int[] source = [10, 9, 7, 15, 3, 27]; Assert.Equal(source.Take(1), source.Where(IsEven)); } [Fact] public void PredicateTrueLastOnly() { - int[] source = { 9, 7, 15, 3, 27, 20 }; + int[] source = [9, 7, 15, 3, 27, 20]; Assert.Equal(source.Skip(source.Length - 1), source.Where(IsEven)); } [Fact] public void PredicateTrueFirstThirdSixth() { - int[] source = { 20, 7, 18, 9, 7, 10, 21 }; - int[] expected = { 20, 18, 10 }; + int[] source = [20, 7, 18, 9, 7, 10, 21]; + int[] expected = [20, 18, 10]; Assert.Equal(expected, source.Where(IsEven)); } [Fact] public void RunOnce() { - int[] source = { 20, 7, 18, 9, 7, 10, 21 }; - int[] expected = { 20, 18, 10 }; + int[] source = [20, 7, 18, 9, 7, 10, 21]; + int[] expected = [20, 18, 10]; Assert.Equal(expected, source.RunOnce().Where(IsEven)); } [Fact] public void SourceAllNullsPredicateTrue() { - int?[] source = { null, null, null, null }; + int?[] source = [null, null, null, null]; Assert.Equal(source, source.Where(num => true)); } @@ -1004,64 +989,64 @@ public void SourceEmptyIndexedPredicate() [Fact] public void SingleElementIndexedPredicateTrue() { - int[] source = { 2 }; + int[] source = [2]; Assert.Equal(source, source.Where((e, i) => e % 2 == 0)); } [Fact] public void SingleElementIndexedPredicateFalse() { - int[] source = { 3 }; + int[] source = [3]; Assert.Empty(source.Where((e, i) => e % 2 == 0)); } [Fact] public void IndexedPredicateFalseForAll() { - int[] source = { 9, 7, 15, 3, 27 }; + int[] source = [9, 7, 15, 3, 27]; Assert.Empty(source.Where((e, i) => e % 2 == 0)); } [Fact] public void IndexedPredicateTrueFirstOnly() { - int[] source = { 10, 9, 7, 15, 3, 27 }; + int[] source = [10, 9, 7, 15, 3, 27]; Assert.Equal(source.Take(1), source.Where((e, i) => e % 2 == 0)); } [Fact] public void IndexedPredicateTrueLastOnly() { - int[] source = { 9, 7, 15, 3, 27, 20 }; + int[] source = [9, 7, 15, 3, 27, 20]; Assert.Equal(source.Skip(source.Length - 1), source.Where((e, i) => e % 2 == 0)); } [Fact] public void IndexedPredicateTrueFirstThirdSixth() { - int[] source = { 20, 7, 18, 9, 7, 10, 21 }; - int[] expected = { 20, 18, 10 }; + int[] source = [20, 7, 18, 9, 7, 10, 21]; + int[] expected = [20, 18, 10]; Assert.Equal(expected, source.Where((e, i) => e % 2 == 0)); } [Fact] public void SourceAllNullsIndexedPredicateTrue() { - int?[] source = { null, null, null, null }; + int?[] source = [null, null, null, null]; Assert.Equal(source, source.Where((num, index) => true)); } [Fact] public void PredicateSelectsFirst() { - int[] source = { -40, 20, 100, 5, 4, 9 }; + int[] source = [-40, 20, 100, 5, 4, 9]; Assert.Equal(source.Take(1), source.Where((e, i) => i == 0)); } [Fact] public void PredicateSelectsLast() { - int[] source = { -40, 20, 100, 5, 4, 9 }; + int[] source = [-40, 20, 100, 5, 4, 9]; Assert.Equal(source.Skip(source.Length - 1), source.Where((e, i) => i == source.Length - 1)); } @@ -1148,19 +1133,18 @@ public void ToCollection(IEnumerable source) Assert.Equal(source, equivalent.ToList()); Assert.Equal(source.Count(), equivalent.Count()); // Count may be optimized. The above asserts do not imply this will pass. - using (var en = equivalent.GetEnumerator()) + using var en = equivalent.GetEnumerator(); + + for (int i = 0; i < equivalent.Count(); i++) { - for (int i = 0; i < equivalent.Count(); i++) - { - Assert.True(en.MoveNext()); - } + Assert.True(en.MoveNext()); + } - Assert.False(en.MoveNext()); // No more items, this should dispose. - Assert.Equal(0, en.Current); // Reset to default value + Assert.False(en.MoveNext()); // No more items, this should dispose. + Assert.Equal(0, en.Current); // Reset to default value - Assert.False(en.MoveNext()); // Want to be sure MoveNext after disposing still works. - Assert.Equal(0, en.Current); - } + Assert.False(en.MoveNext()); // Want to be sure MoveNext after disposing still works. + Assert.Equal(0, en.Current); } { var equivalent = source.Where(s => true).Select(s => s); @@ -1169,63 +1153,58 @@ public void ToCollection(IEnumerable source) Assert.Equal(source, equivalent.ToList()); Assert.Equal(source.Count(), equivalent.Count()); // Count may be optimized. The above asserts do not imply this will pass. - using (var en = equivalent.GetEnumerator()) + using var en = equivalent.GetEnumerator(); + + for (int i = 0; i < equivalent.Count(); i++) { - for (int i = 0; i < equivalent.Count(); i++) - { - Assert.True(en.MoveNext()); - } + Assert.True(en.MoveNext()); + } - Assert.False(en.MoveNext()); // No more items, this should dispose. - Assert.Equal(0, en.Current); // Reset to default value + Assert.False(en.MoveNext()); // No more items, this should dispose. + Assert.Equal(0, en.Current); // Reset to default value - Assert.False(en.MoveNext()); // Want to be sure MoveNext after disposing still works. - Assert.Equal(0, en.Current); - } + Assert.False(en.MoveNext()); // Want to be sure MoveNext after disposing still works. + Assert.Equal(0, en.Current); } } [Fact] public void WhereFirstLast() { - Assert.All(IdentityTransforms(), transform => + Assert.All(CreateSources(Enumerable.Range(0, 10)), source => { - IEnumerable data = transform(Enumerable.Range(0, 10)); + Assert.Equal(3, source.Where(i => i == 3).First()); + Assert.Equal(0, source.Where(i => i % 2 == 0).First()); - Assert.Equal(3, data.Where(i => i == 3).First()); - Assert.Equal(0, data.Where(i => i % 2 == 0).First()); + Assert.Equal(3, source.Where(i => i == 3).Last()); + Assert.Equal(8, source.Where(i => i % 2 == 0).Last()); - Assert.Equal(3, data.Where(i => i == 3).Last()); - Assert.Equal(8, data.Where(i => i % 2 == 0).Last()); + Assert.Equal(3, source.Where(i => i == 3).ElementAt(0)); + Assert.Equal(8, source.Where(i => i % 2 == 0).ElementAt(4)); - Assert.Equal(3, data.Where(i => i == 3).ElementAt(0)); - Assert.Equal(8, data.Where(i => i % 2 == 0).ElementAt(4)); - - Assert.Throws(() => data.Where(i => i == 10).First()); - Assert.Throws(() => data.Where(i => i == 10).Last()); - Assert.Throws(() => data.Where(i => i == 10).ElementAt(0)); + Assert.Throws(() => source.Where(i => i == 10).First()); + Assert.Throws(() => source.Where(i => i == 10).Last()); + Assert.Throws(() => source.Where(i => i == 10).ElementAt(0)); }); } [Fact] public void WhereSelectFirstLast() { - Assert.All(IdentityTransforms(), transform => + Assert.All(CreateSources(Enumerable.Range(0, 10)), source => { - IEnumerable data = transform(Enumerable.Range(0, 10)); - - Assert.Equal(6, data.Where(i => i == 3).Select(i => i * 2).First()); - Assert.Equal(0, data.Where(i => i % 2 == 0).Select(i => i * 2).First()); + Assert.Equal(6, source.Where(i => i == 3).Select(i => i * 2).First()); + Assert.Equal(0, source.Where(i => i % 2 == 0).Select(i => i * 2).First()); - Assert.Equal(6, data.Where(i => i == 3).Select(i => i * 2).Last()); - Assert.Equal(16, data.Where(i => i % 2 == 0).Select(i => i * 2).Last()); + Assert.Equal(6, source.Where(i => i == 3).Select(i => i * 2).Last()); + Assert.Equal(16, source.Where(i => i % 2 == 0).Select(i => i * 2).Last()); - Assert.Equal(6, data.Where(i => i == 3).Select(i => i * 2).ElementAt(0)); - Assert.Equal(16, data.Where(i => i % 2 == 0).Select(i => i * 2).ElementAt(4)); + Assert.Equal(6, source.Where(i => i == 3).Select(i => i * 2).ElementAt(0)); + Assert.Equal(16, source.Where(i => i % 2 == 0).Select(i => i * 2).ElementAt(4)); - Assert.Throws(() => data.Where(i => i == 10).Select(i => i * 2).First()); - Assert.Throws(() => data.Where(i => i == 10).Select(i => i * 2).Last()); - Assert.Throws(() => data.Where(i => i == 10).Select(i => i * 2).ElementAt(0)); + Assert.Throws(() => source.Where(i => i == 10).Select(i => i * 2).First()); + Assert.Throws(() => source.Where(i => i == 10).Select(i => i * 2).Last()); + Assert.Throws(() => source.Where(i => i == 10).Select(i => i * 2).ElementAt(0)); }); } @@ -1233,9 +1212,9 @@ public static IEnumerable ToCollectionData() { IEnumerable seq = GenerateRandomSequnce(seed: 0xdeadbeef, count: 10); - foreach (var seq2 in IdentityTransforms().Select(t => t(seq)).ToArray()) + foreach (IEnumerable seq2 in CreateSources(seq)) { - yield return new object[] { seq2 }; + yield return [seq2]; } } diff --git a/tests/System.Linq.Tests/Tests/ZLinq/ZipTests.cs b/tests/System.Linq.Tests/Tests/ZLinq/ZipTests.cs index 2a4f061a..9b482ba5 100644 --- a/tests/System.Linq.Tests/Tests/ZLinq/ZipTests.cs +++ b/tests/System.Linq.Tests/Tests/ZLinq/ZipTests.cs @@ -11,9 +11,9 @@ public class ZipTests : EnumerableTests [Fact] public void ImplicitTypeParameters() { - IEnumerable first = new int[] { 1, 2, 3 }; - IEnumerable second = new int[] { 2, 5, 9 }; - IEnumerable expected = new int[] { 3, 7, 12 }; + IEnumerable first = [1, 2, 3]; + IEnumerable second = [2, 5, 9]; + IEnumerable expected = [3, 7, 12]; Assert.Equal(expected, first.Zip(second, (x, y) => x + y)); } @@ -21,9 +21,9 @@ public void ImplicitTypeParameters() [Fact] public void ExplicitTypeParameters() { - IEnumerable first = new int[] { 1, 2, 3 }; - IEnumerable second = new int[] { 2, 5, 9 }; - IEnumerable expected = new int[] { 3, 7, 12 }; + IEnumerable first = [1, 2, 3]; + IEnumerable second = [2, 5, 9]; + IEnumerable expected = [3, 7, 12]; Assert.Equal(expected, first.Zip(second, (x, y) => x + y)); } @@ -32,7 +32,7 @@ public void ExplicitTypeParameters() public void FirstIsNull() { IEnumerable first = null; - IEnumerable second = new int[] { 2, 5, 9 }; + IEnumerable second = [2, 5, 9]; AssertExtensions.Throws(() => first.Zip(second, (x, y) => x + y)); } @@ -40,7 +40,7 @@ public void FirstIsNull() [Fact] public void SecondIsNull() { - IEnumerable first = new int[] { 1, 2, 3 }; + IEnumerable first = [1, 2, 3]; IEnumerable second = null; AssertExtensions.Throws("second", () => first.Zip(second, (x, y) => x + y)); @@ -49,8 +49,8 @@ public void SecondIsNull() [Fact] public void FuncIsNull() { - IEnumerable first = new int[] { 1, 2, 3 }; - IEnumerable second = new int[] { 2, 4, 6 }; + IEnumerable first = [1, 2, 3]; + IEnumerable second = [2, 4, 6]; Func func = null; AssertExtensions.Throws("resultSelector", () => first.Zip(second, func)); @@ -59,14 +59,14 @@ public void FuncIsNull() [Fact] public void ExceptionThrownFromFirstsEnumerator() { - ThrowsOnMatchEnumerable first = new ThrowsOnMatchEnumerable(new int[] { 1, 3, 3 }, 2); - IEnumerable second = new int[] { 2, 4, 6 }; + ThrowsOnMatchEnumerable first = new ThrowsOnMatchEnumerable([1, 3, 3], 2); + IEnumerable second = [2, 4, 6]; Func func = (x, y) => x + y; - IEnumerable expected = new int[] { 3, 7, 9 }; + IEnumerable expected = [3, 7, 9]; Assert.Equal(expected, first.Zip(second, func)); - first = new ThrowsOnMatchEnumerable(new int[] { 1, 2, 3 }, 2); + first = new ThrowsOnMatchEnumerable([1, 2, 3], 2); Assert.Throws(() => { @@ -78,14 +78,14 @@ public void ExceptionThrownFromFirstsEnumerator() [Fact] public void ExceptionThrownFromSecondsEnumerator() { - ThrowsOnMatchEnumerable second = new ThrowsOnMatchEnumerable(new int[] { 1, 3, 3 }, 2); - IEnumerable first = new int[] { 2, 4, 6 }; + ThrowsOnMatchEnumerable second = new ThrowsOnMatchEnumerable([1, 3, 3], 2); + IEnumerable first = [2, 4, 6]; Func func = (x, y) => x + y; - IEnumerable expected = new int[] { 3, 7, 9 }; + IEnumerable expected = [3, 7, 9]; Assert.Equal(expected, first.Zip(second, func)); - second = new ThrowsOnMatchEnumerable(new int[] { 1, 2, 3 }, 2); + second = new ThrowsOnMatchEnumerable([1, 2, 3], 2); Assert.Throws(() => { @@ -97,10 +97,10 @@ public void ExceptionThrownFromSecondsEnumerator() [Fact] public void FirstAndSecondEmpty() { - IEnumerable first = new int[] { }; - IEnumerable second = new int[] { }; + IEnumerable first = []; + IEnumerable second = []; Func func = (x, y) => x + y; - IEnumerable expected = new int[] { }; + IEnumerable expected = []; Assert.Equal(expected, first.Zip(second, func)); } @@ -109,10 +109,10 @@ public void FirstAndSecondEmpty() [Fact] public void FirstEmptySecondSingle() { - IEnumerable first = new int[] { }; - IEnumerable second = new int[] { 2 }; + IEnumerable first = []; + IEnumerable second = [2]; Func func = (x, y) => x + y; - IEnumerable expected = new int[] { }; + IEnumerable expected = []; Assert.Equal(expected, first.Zip(second, func)); } @@ -120,10 +120,10 @@ public void FirstEmptySecondSingle() [Fact] public void FirstEmptySecondMany() { - IEnumerable first = new int[] { }; - IEnumerable second = new int[] { 2, 4, 8 }; + IEnumerable first = []; + IEnumerable second = [2, 4, 8]; Func func = (x, y) => x + y; - IEnumerable expected = new int[] { }; + IEnumerable expected = []; Assert.Equal(expected, first.Zip(second, func)); } @@ -132,10 +132,10 @@ public void FirstEmptySecondMany() [Fact] public void SecondEmptyFirstSingle() { - IEnumerable first = new int[] { 1 }; - IEnumerable second = new int[] { }; + IEnumerable first = [1]; + IEnumerable second = []; Func func = (x, y) => x + y; - IEnumerable expected = new int[] { }; + IEnumerable expected = []; Assert.Equal(expected, first.Zip(second, func)); } @@ -143,10 +143,10 @@ public void SecondEmptyFirstSingle() [Fact] public void SecondEmptyFirstMany() { - IEnumerable first = new int[] { 1, 2, 3 }; - IEnumerable second = new int[] { }; + IEnumerable first = [1, 2, 3]; + IEnumerable second = []; Func func = (x, y) => x + y; - IEnumerable expected = new int[] { }; + IEnumerable expected = []; Assert.Equal(expected, first.Zip(second, func)); } @@ -154,10 +154,10 @@ public void SecondEmptyFirstMany() [Fact] public void FirstAndSecondSingle() { - IEnumerable first = new int[] { 1 }; - IEnumerable second = new int[] { 2 }; + IEnumerable first = [1]; + IEnumerable second = [2]; Func func = (x, y) => x + y; - IEnumerable expected = new int[] { 3 }; + IEnumerable expected = [3]; Assert.Equal(expected, first.Zip(second, func)); } @@ -165,10 +165,10 @@ public void FirstAndSecondSingle() [Fact] public void FirstAndSecondEqualSize() { - IEnumerable first = new int[] { 1, 2, 3 }; - IEnumerable second = new int[] { 2, 3, 4 }; + IEnumerable first = [1, 2, 3]; + IEnumerable second = [2, 3, 4]; Func func = (x, y) => x + y; - IEnumerable expected = new int[] { 3, 5, 7 }; + IEnumerable expected = [3, 5, 7]; Assert.Equal(expected, first.Zip(second, func)); } @@ -176,10 +176,10 @@ public void FirstAndSecondEqualSize() [Fact] public void SecondOneMoreThanFirst() { - IEnumerable first = new int[] { 1, 2 }; - IEnumerable second = new int[] { 2, 4, 8 }; + IEnumerable first = [1, 2]; + IEnumerable second = [2, 4, 8]; Func func = (x, y) => x + y; - IEnumerable expected = new int[] { 3, 6 }; + IEnumerable expected = [3, 6]; Assert.Equal(expected, first.Zip(second, func)); } @@ -188,10 +188,10 @@ public void SecondOneMoreThanFirst() [Fact] public void SecondManyMoreThanFirst() { - IEnumerable first = new int[] { 1, 2 }; - IEnumerable second = new int[] { 2, 4, 8, 16 }; + IEnumerable first = [1, 2]; + IEnumerable second = [2, 4, 8, 16]; Func func = (x, y) => x + y; - IEnumerable expected = new int[] { 3, 6 }; + IEnumerable expected = [3, 6]; Assert.Equal(expected, first.Zip(second, func)); } @@ -199,10 +199,10 @@ public void SecondManyMoreThanFirst() [Fact] public void FirstOneMoreThanSecond() { - IEnumerable first = new int[] { 1, 2, 3 }; - IEnumerable second = new int[] { 2, 4 }; + IEnumerable first = [1, 2, 3]; + IEnumerable second = [2, 4]; Func func = (x, y) => x + y; - IEnumerable expected = new int[] { 3, 6 }; + IEnumerable expected = [3, 6]; Assert.Equal(expected, first.Zip(second, func)); } @@ -211,10 +211,10 @@ public void FirstOneMoreThanSecond() [Fact] public void FirstManyMoreThanSecond() { - IEnumerable first = new int[] { 1, 2, 3, 4 }; - IEnumerable second = new int[] { 2, 4 }; + IEnumerable first = [1, 2, 3, 4]; + IEnumerable second = [2, 4]; Func func = (x, y) => x + y; - IEnumerable expected = new int[] { 3, 6 }; + IEnumerable expected = [3, 6]; Assert.Equal(expected, first.Zip(second, func)); } @@ -223,15 +223,15 @@ public void FirstManyMoreThanSecond() [Fact] public void DelegateFuncChanged() { - IEnumerable first = new int[] { 1, 2, 3, 4 }; - IEnumerable second = new int[] { 2, 4, 8 }; + IEnumerable first = [1, 2, 3, 4]; + IEnumerable second = [2, 4, 8]; Func func = (x, y) => x + y; - IEnumerable expected = new int[] { 3, 6, 11 }; + IEnumerable expected = [3, 6, 11]; Assert.Equal(expected, first.Zip(second, func)); func = (x, y) => x - y; - expected = new int[] { -1, -2, -5 }; + expected = [-1, -2, -5]; Assert.Equal(expected, first.Zip(second, func)); } @@ -239,13 +239,13 @@ public void DelegateFuncChanged() [Fact] public void LambdaFuncChanged() { - IEnumerable first = new int[] { 1, 2, 3, 4 }; - IEnumerable second = new int[] { 2, 4, 8 }; - IEnumerable expected = new int[] { 3, 6, 11 }; + IEnumerable first = [1, 2, 3, 4]; + IEnumerable second = [2, 4, 8]; + IEnumerable expected = [3, 6, 11]; Assert.Equal(expected, first.Zip(second, (x, y) => x + y)); - expected = new int[] { -1, -2, -5 }; + expected = [-1, -2, -5]; Assert.Equal(expected, first.Zip(second, (x, y) => x - y)); } @@ -253,10 +253,10 @@ public void LambdaFuncChanged() [Fact] public void FirstHasFirstElementNull() { - IEnumerable first = new[] { (int?)null, 2, 3, 4 }; - IEnumerable second = new int[] { 2, 4, 8 }; + IEnumerable first = [(int?)null, 2, 3, 4]; + IEnumerable second = [2, 4, 8]; Func func = (x, y) => x + y; - IEnumerable expected = new int?[] { null, 6, 11 }; + IEnumerable expected = [null, 6, 11]; Assert.Equal(expected, first.Zip(second, func)); } @@ -264,10 +264,10 @@ public void FirstHasFirstElementNull() [Fact] public void FirstHasLastElementNull() { - IEnumerable first = new[] { 1, 2, (int?)null }; - IEnumerable second = new int[] { 2, 4, 6, 8 }; + IEnumerable first = [1, 2, (int?)null]; + IEnumerable second = [2, 4, 6, 8]; Func func = (x, y) => x + y; - IEnumerable expected = new int?[] { 3, 6, null }; + IEnumerable expected = [3, 6, null]; Assert.Equal(expected, first.Zip(second, func)); } @@ -275,10 +275,10 @@ public void FirstHasLastElementNull() [Fact] public void FirstHasMiddleNullValue() { - IEnumerable first = new[] { 1, (int?)null, 3 }; - IEnumerable second = new int[] { 2, 4, 6, 8 }; + IEnumerable first = [1, (int?)null, 3]; + IEnumerable second = [2, 4, 6, 8]; Func func = (x, y) => x + y; - IEnumerable expected = new int?[] { 3, null, 9 }; + IEnumerable expected = [3, null, 9]; Assert.Equal(expected, first.Zip(second, func)); } @@ -286,10 +286,10 @@ public void FirstHasMiddleNullValue() [Fact] public void FirstAllElementsNull() { - IEnumerable first = new int?[] { null, null, null }; - IEnumerable second = new int[] { 2, 4, 6, 8 }; + IEnumerable first = [null, null, null]; + IEnumerable second = [2, 4, 6, 8]; Func func = (x, y) => x + y; - IEnumerable expected = new int?[] { null, null, null }; + IEnumerable expected = [null, null, null]; Assert.Equal(expected, first.Zip(second, func)); } @@ -297,10 +297,10 @@ public void FirstAllElementsNull() [Fact] public void SecondHasFirstElementNull() { - IEnumerable first = new int[] { 1, 2, 3, 4 }; - IEnumerable second = new int?[] { null, 4, 6 }; + IEnumerable first = [1, 2, 3, 4]; + IEnumerable second = [null, 4, 6]; Func func = (x, y) => x + y; - IEnumerable expected = new int?[] { null, 6, 9 }; + IEnumerable expected = [null, 6, 9]; Assert.Equal(expected, first.Zip(second, func)); } @@ -308,10 +308,10 @@ public void SecondHasFirstElementNull() [Fact] public void SecondHasLastElementNull() { - IEnumerable first = new int[] { 1, 2, 3, 4 }; - IEnumerable second = new int?[] { 2, 4, null }; + IEnumerable first = [1, 2, 3, 4]; + IEnumerable second = [2, 4, null]; Func func = (x, y) => x + y; - IEnumerable expected = new int?[] { 3, 6, null }; + IEnumerable expected = [3, 6, null]; Assert.Equal(expected, first.Zip(second, func)); } @@ -319,10 +319,10 @@ public void SecondHasLastElementNull() [Fact] public void SecondHasMiddleElementNull() { - IEnumerable first = new int[] { 1, 2, 3, 4 }; - IEnumerable second = new int?[] { 2, null, 6 }; + IEnumerable first = [1, 2, 3, 4]; + IEnumerable second = [2, null, 6]; Func func = (x, y) => x + y; - IEnumerable expected = new int?[] { 3, null, 9 }; + IEnumerable expected = [3, null, 9]; Assert.Equal(expected, first.Zip(second, func)); } @@ -330,10 +330,10 @@ public void SecondHasMiddleElementNull() [Fact] public void SecondHasAllElementsNull() { - IEnumerable first = new int[] { 1, 2, 3, 4 }; - IEnumerable second = new int?[] { null, null, null }; + IEnumerable first = [1, 2, 3, 4]; + IEnumerable second = [null, null, null]; Func func = (x, y) => x + y; - IEnumerable expected = new int?[] { null, null, null }; + IEnumerable expected = [null, null, null]; Assert.Equal(expected, first.Zip(second, func)); } @@ -341,10 +341,10 @@ public void SecondHasAllElementsNull() [Fact] public void SecondLargerFirstAllNull() { - IEnumerable first = new int?[] { null, null, null, null }; - IEnumerable second = new int?[] { null, null, null }; + IEnumerable first = [null, null, null, null]; + IEnumerable second = [null, null, null]; Func func = (x, y) => x + y; - IEnumerable expected = new int?[] { null, null, null }; + IEnumerable expected = [null, null, null]; Assert.Equal(expected, first.Zip(second, func)); } @@ -353,10 +353,10 @@ public void SecondLargerFirstAllNull() [Fact] public void FirstSameSizeSecondAllNull() { - IEnumerable first = new int?[] { null, null, null }; - IEnumerable second = new int?[] { null, null, null }; + IEnumerable first = [null, null, null]; + IEnumerable second = [null, null, null]; Func func = (x, y) => x + y; - IEnumerable expected = new int?[] { null, null, null }; + IEnumerable expected = [null, null, null]; Assert.Equal(expected, first.Zip(second, func)); } @@ -364,10 +364,10 @@ public void FirstSameSizeSecondAllNull() [Fact] public void FirstSmallerSecondAllNull() { - IEnumerable first = new int?[] { null, null, null }; - IEnumerable second = new int?[] { null, null, null, null }; + IEnumerable first = [null, null, null]; + IEnumerable second = [null, null, null, null]; Func func = (x, y) => x + y; - IEnumerable expected = new int?[] { null, null, null }; + IEnumerable expected = [null, null, null]; Assert.Equal(expected, first.Zip(second, func)); } @@ -384,10 +384,10 @@ public void ForcedToEnumeratorDoesntEnumerate() [Fact] public void RunOnce() { - IEnumerable first = new[] { 1, (int?)null, 3 }; - IEnumerable second = new[] { 2, 4, 6, 8 }; + IEnumerable first = [1, (int?)null, 3]; + IEnumerable second = [2, 4, 6, 8]; Func func = (x, y) => x + y; - IEnumerable expected = new int?[] { 3, null, 9 }; + IEnumerable expected = [3, null, 9]; Assert.Equal(expected, first.RunOnce().Zip(second.RunOnce(), func)); } @@ -395,9 +395,9 @@ public void RunOnce() [Fact] public void Zip2_ImplicitTypeParameters() { - IEnumerable first = new int[] { 1, 2, 3 }; - IEnumerable second = new int[] { 2, 5, 9 }; - IEnumerable<(int, int)> expected = new (int, int)[] { (1, 2), (2, 5), (3, 9) }; + IEnumerable first = [1, 2, 3]; + IEnumerable second = [2, 5, 9]; + IEnumerable<(int, int)> expected = [(1, 2), (2, 5), (3, 9)]; Assert.Equal(expected, first.Zip(second)); } @@ -405,9 +405,9 @@ public void Zip2_ImplicitTypeParameters() [Fact] public void Zip2_ExplicitTypeParameters() { - IEnumerable first = new int[] { 1, 2, 3 }; - IEnumerable second = new int[] { 2, 5, 9 }; - IEnumerable<(int, int)> expected = new (int, int)[] { (1, 2), (2, 5), (3, 9) }; + IEnumerable first = [1, 2, 3]; + IEnumerable second = [2, 5, 9]; + IEnumerable<(int, int)> expected = [(1, 2), (2, 5), (3, 9)]; Assert.Equal(expected, first.Zip(second)); } @@ -416,7 +416,7 @@ public void Zip2_ExplicitTypeParameters() public void Zip2_FirstIsNull() { IEnumerable first = null; - IEnumerable second = new int[] { 2, 5, 9 }; + IEnumerable second = [2, 5, 9]; AssertExtensions.Throws(() => first.Zip(second)); } @@ -424,7 +424,7 @@ public void Zip2_FirstIsNull() [Fact] public void Zip2_SecondIsNull() { - IEnumerable first = new int[] { 1, 2, 3 }; + IEnumerable first = [1, 2, 3]; IEnumerable second = null; AssertExtensions.Throws("second", () => first.Zip(second)); @@ -433,13 +433,13 @@ public void Zip2_SecondIsNull() [Fact] public void Zip2_ExceptionThrownFromFirstsEnumerator() { - ThrowsOnMatchEnumerable first = new ThrowsOnMatchEnumerable(new int[] { 1, 3, 3 }, 2); - IEnumerable second = new int[] { 2, 4, 6 }; - IEnumerable<(int, int)> expected = new (int, int)[] { (1, 2), (3, 4), (3, 6) }; + ThrowsOnMatchEnumerable first = new ThrowsOnMatchEnumerable([1, 3, 3], 2); + IEnumerable second = [2, 4, 6]; + IEnumerable<(int, int)> expected = [(1, 2), (3, 4), (3, 6)]; Assert.Equal(expected, first.Zip(second)); - first = new ThrowsOnMatchEnumerable(new int[] { 1, 2, 3 }, 2); + first = new ThrowsOnMatchEnumerable([1, 2, 3], 2); Assert.Throws(() => { @@ -451,13 +451,13 @@ public void Zip2_ExceptionThrownFromFirstsEnumerator() [Fact] public void Zip2_ExceptionThrownFromSecondsEnumerator() { - ThrowsOnMatchEnumerable second = new ThrowsOnMatchEnumerable(new int[] { 1, 3, 3 }, 2); - IEnumerable first = new int[] { 2, 4, 6 }; - IEnumerable<(int, int)> expected = new (int, int)[] { (2, 1), (4, 3), (6, 3) }; + ThrowsOnMatchEnumerable second = new ThrowsOnMatchEnumerable([1, 3, 3], 2); + IEnumerable first = [2, 4, 6]; + IEnumerable<(int, int)> expected = [(2, 1), (4, 3), (6, 3)]; Assert.Equal(expected, first.Zip(second)); - second = new ThrowsOnMatchEnumerable(new int[] { 1, 2, 3 }, 2); + second = new ThrowsOnMatchEnumerable([1, 2, 3], 2); Assert.Throws(() => { @@ -469,9 +469,9 @@ public void Zip2_ExceptionThrownFromSecondsEnumerator() [Fact] public void Zip2_FirstAndSecondEmpty() { - IEnumerable first = new int[] { }; - IEnumerable second = new int[] { }; - IEnumerable<(int, int)> expected = new (int, int)[] { }; + IEnumerable first = []; + IEnumerable second = []; + IEnumerable<(int, int)> expected = []; Assert.Equal(expected, first.Zip(second)); } @@ -479,9 +479,9 @@ public void Zip2_FirstAndSecondEmpty() [Fact] public void Zip2_FirstEmptySecondSingle() { - IEnumerable first = new int[] { }; - IEnumerable second = new int[] { 2 }; - IEnumerable<(int, int)> expected = new (int, int)[] { }; + IEnumerable first = []; + IEnumerable second = [2]; + IEnumerable<(int, int)> expected = []; Assert.Equal(expected, first.Zip(second)); } @@ -489,9 +489,9 @@ public void Zip2_FirstEmptySecondSingle() [Fact] public void Zip2_FirstEmptySecondMany() { - IEnumerable first = new int[] { }; - IEnumerable second = new int[] { 2, 4, 8 }; - IEnumerable<(int, int)> expected = new (int, int)[] { }; + IEnumerable first = []; + IEnumerable second = [2, 4, 8]; + IEnumerable<(int, int)> expected = []; Assert.Equal(expected, first.Zip(second)); } @@ -499,9 +499,9 @@ public void Zip2_FirstEmptySecondMany() [Fact] public void Zip2_SecondEmptyFirstSingle() { - IEnumerable first = new int[] { 1 }; - IEnumerable second = new int[] { }; - IEnumerable<(int, int)> expected = new (int, int)[] { }; + IEnumerable first = [1]; + IEnumerable second = []; + IEnumerable<(int, int)> expected = []; Assert.Equal(expected, first.Zip(second)); } @@ -509,9 +509,9 @@ public void Zip2_SecondEmptyFirstSingle() [Fact] public void Zip2_SecondEmptyFirstMany() { - IEnumerable first = new int[] { 1, 2, 3 }; - IEnumerable second = new int[] { }; - IEnumerable<(int, int)> expected = new (int, int)[] { }; + IEnumerable first = [1, 2, 3]; + IEnumerable second = []; + IEnumerable<(int, int)> expected = []; Assert.Equal(expected, first.Zip(second)); } @@ -519,9 +519,9 @@ public void Zip2_SecondEmptyFirstMany() [Fact] public void Zip2_FirstAndSecondSingle() { - IEnumerable first = new int[] { 1 }; - IEnumerable second = new int[] { 2 }; - IEnumerable<(int, int)> expected = new (int, int)[] { (1, 2) }; + IEnumerable first = [1]; + IEnumerable second = [2]; + IEnumerable<(int, int)> expected = [(1, 2)]; Assert.Equal(expected, first.Zip(second)); } @@ -529,9 +529,9 @@ public void Zip2_FirstAndSecondSingle() [Fact] public void Zip2_FirstAndSecondEqualSize() { - IEnumerable first = new int[] { 1, 2, 3 }; - IEnumerable second = new int[] { 2, 3, 4 }; - IEnumerable<(int, int)> expected = new (int, int)[] { (1, 2), (2, 3), (3, 4) }; + IEnumerable first = [1, 2, 3]; + IEnumerable second = [2, 3, 4]; + IEnumerable<(int, int)> expected = [(1, 2), (2, 3), (3, 4)]; Assert.Equal(expected, first.Zip(second)); } @@ -539,9 +539,9 @@ public void Zip2_FirstAndSecondEqualSize() [Fact] public void Zip2_SecondOneMoreThanFirst() { - IEnumerable first = new int[] { 1, 2 }; - IEnumerable second = new int[] { 2, 4, 8 }; - IEnumerable<(int, int)> expected = new (int, int)[] { (1, 2), (2, 4) }; + IEnumerable first = [1, 2]; + IEnumerable second = [2, 4, 8]; + IEnumerable<(int, int)> expected = [(1, 2), (2, 4)]; Assert.Equal(expected, first.Zip(second)); } @@ -550,9 +550,9 @@ public void Zip2_SecondOneMoreThanFirst() [Fact] public void Zip2_SecondManyMoreThanFirst() { - IEnumerable first = new int[] { 1, 2 }; - IEnumerable second = new int[] { 2, 4, 8, 16 }; - IEnumerable<(int, int)> expected = new (int, int)[] { (1, 2), (2, 4) }; + IEnumerable first = [1, 2]; + IEnumerable second = [2, 4, 8, 16]; + IEnumerable<(int, int)> expected = [(1, 2), (2, 4)]; Assert.Equal(expected, first.Zip(second)); } @@ -560,9 +560,9 @@ public void Zip2_SecondManyMoreThanFirst() [Fact] public void Zip2_FirstOneMoreThanSecond() { - IEnumerable first = new int[] { 1, 2, 3 }; - IEnumerable second = new int[] { 2, 4 }; - IEnumerable<(int, int)> expected = new (int, int)[] { (1, 2), (2, 4) }; + IEnumerable first = [1, 2, 3]; + IEnumerable second = [2, 4]; + IEnumerable<(int, int)> expected = [(1, 2), (2, 4)]; Assert.Equal(expected, first.Zip(second)); } @@ -570,9 +570,9 @@ public void Zip2_FirstOneMoreThanSecond() [Fact] public void Zip2_FirstManyMoreThanSecond() { - IEnumerable first = new int[] { 1, 2, 3, 4 }; - IEnumerable second = new int[] { 2, 4 }; - IEnumerable<(int, int)> expected = new (int, int)[] { (1, 2), (2, 4) }; + IEnumerable first = [1, 2, 3, 4]; + IEnumerable second = [2, 4]; + IEnumerable<(int, int)> expected = [(1, 2), (2, 4)]; Assert.Equal(expected, first.Zip(second)); } @@ -580,9 +580,9 @@ public void Zip2_FirstManyMoreThanSecond() [Fact] public void Zip2_RunOnce() { - IEnumerable first = new[] { 1, (int?)null, 3 }; - IEnumerable second = new[] { 2, 4, 6, 8 }; - IEnumerable<(int?, int)> expected = new (int?, int)[] { (1, 2), (null, 4), (3, 6) }; + IEnumerable first = [1, (int?)null, 3]; + IEnumerable second = [2, 4, 6, 8]; + IEnumerable<(int?, int)> expected = [(1, 2), (null, 4), (3, 6)]; Assert.Equal(expected, first.RunOnce().Zip(second.RunOnce())); } @@ -590,22 +590,22 @@ public void Zip2_RunOnce() [Fact] public void Zip2_NestedTuple() { - IEnumerable first = new[] { 1, 3, 5 }; - IEnumerable second = new[] { 2, 4, 6 }; - IEnumerable<(int, int)> third = new[] { (1, 2), (3, 4), (5, 6) }; + IEnumerable first = [1, 3, 5]; + IEnumerable second = [2, 4, 6]; + IEnumerable<(int, int)> third = [(1, 2), (3, 4), (5, 6)]; Assert.Equal(third, first.Zip(second)); - IEnumerable fourth = new[] { "one", "two", "three" }; + IEnumerable fourth = ["one", "two", "three"]; - IEnumerable<((int, int), string)> final = new[] { ((1, 2), "one"), ((3, 4), "two"), ((5, 6), "three") }; + IEnumerable<((int, int), string)> final = [((1, 2), "one"), ((3, 4), "two"), ((5, 6), "three")]; Assert.Equal(final, third.Zip(fourth)); } [Fact] public void Zip2_TupleNames() { - var t = new[] { 1, 2, 3 }.Zip(new[] { 2, 4, 6 }).First(); + var t = new[] { 1, 2, 3 }.Zip([2, 4, 6]).First(); Assert.Equal(t.Item1, t.First); Assert.Equal(t.Item2, t.Second); } @@ -614,8 +614,8 @@ public void Zip2_TupleNames() public void Zip3_FirstIsNull() { IEnumerable first = null; - IEnumerable second = new[] { 4, 5, 6 }; - IEnumerable third = new[] { 7, 8, 9 }; + IEnumerable second = [4, 5, 6]; + IEnumerable third = [7, 8, 9]; AssertExtensions.Throws(() => first.Zip(second, third)); } @@ -623,9 +623,9 @@ public void Zip3_FirstIsNull() [Fact] public void Zip3_SecondIsNull() { - IEnumerable first = new[] { 1, 2, 3 }; + IEnumerable first = [1, 2, 3]; IEnumerable second = null; - IEnumerable third = new[] { 4, 5, 6 }; + IEnumerable third = [4, 5, 6]; AssertExtensions.Throws("second", () => first.Zip(second, third)); } @@ -633,8 +633,8 @@ public void Zip3_SecondIsNull() [Fact] public void Zip3_ThirdIsNull() { - IEnumerable first = new[] { 1, 2, 3 }; - IEnumerable second = new[] { 4, 5, 6 }; + IEnumerable first = [1, 2, 3]; + IEnumerable second = [4, 5, 6]; IEnumerable third = null; AssertExtensions.Throws("third", () => first.Zip(second, third)); @@ -643,10 +643,10 @@ public void Zip3_ThirdIsNull() [Fact] public void Zip3_ThirdEmpty() { - IEnumerable first = new[] { 1, 2, 3 }; - IEnumerable second = new[] { 4, 5, 6 }; - IEnumerable third = new int[] { }; - IEnumerable<(int, int, int)> expected = new (int, int, int)[] { }; + IEnumerable first = [1, 2, 3]; + IEnumerable second = [4, 5, 6]; + IEnumerable third = []; + IEnumerable<(int, int, int)> expected = []; Assert.Equal(expected, first.Zip(second, third)); } @@ -654,10 +654,10 @@ public void Zip3_ThirdEmpty() [Fact] public void Zip3_ImplicitTypeParameters() { - IEnumerable first = new[] { 1, 2 }; - IEnumerable second = new[] { 3, 4 }; - IEnumerable third = new[] { 5, 6 }; - IEnumerable<(int, int, int)> expected = new[] { (1, 3, 5), (2, 4, 6) }; + IEnumerable first = [1, 2]; + IEnumerable second = [3, 4]; + IEnumerable third = [5, 6]; + IEnumerable<(int, int, int)> expected = [(1, 3, 5), (2, 4, 6)]; Assert.Equal(expected, first.Zip(second, third)); } @@ -665,10 +665,10 @@ public void Zip3_ImplicitTypeParameters() [Fact] public void Zip3_ExplicitTypeParameters() { - IEnumerable first = new[] { 1, 2 }; - IEnumerable second = new[] { 3, 4 }; - IEnumerable third = new[] { 5, 6 }; - IEnumerable<(int, int, int)> expected = new[] { (1, 3, 5), (2, 4, 6) }; + IEnumerable first = [1, 2]; + IEnumerable second = [3, 4]; + IEnumerable third = [5, 6]; + IEnumerable<(int, int, int)> expected = [(1, 3, 5), (2, 4, 6)]; Assert.Equal(expected, first.Zip(second, third)); } @@ -676,10 +676,10 @@ public void Zip3_ExplicitTypeParameters() [Fact] public void Zip3_ThirdOneMore() { - IEnumerable first = new[] { 1, 2 }; - IEnumerable second = new[] { 3, 4 }; - IEnumerable third = new[] { 5, 6, 7 }; - IEnumerable<(int, int, int)> expected = new[] { (1, 3, 5), (2, 4, 6) }; + IEnumerable first = [1, 2]; + IEnumerable second = [3, 4]; + IEnumerable third = [5, 6, 7]; + IEnumerable<(int, int, int)> expected = [(1, 3, 5), (2, 4, 6)]; Assert.Equal(expected, first.Zip(second, third)); } @@ -687,10 +687,10 @@ public void Zip3_ThirdOneMore() [Fact] public void Zip3_ThirdManyMore() { - IEnumerable first = new[] { 1, 2 }; - IEnumerable second = new[] { 3, 4 }; - IEnumerable third = new[] { 5, 6, 7, 8 }; - IEnumerable<(int, int, int)> expected = new[] { (1, 3, 5), (2, 4, 6) }; + IEnumerable first = [1, 2]; + IEnumerable second = [3, 4]; + IEnumerable third = [5, 6, 7, 8]; + IEnumerable<(int, int, int)> expected = [(1, 3, 5), (2, 4, 6)]; Assert.Equal(expected, first.Zip(second, third)); } @@ -698,10 +698,10 @@ public void Zip3_ThirdManyMore() [Fact] public void Zip3_ThirdOneLess() { - IEnumerable first = new[] { 1, 2 }; - IEnumerable second = new[] { 3, 4 }; - IEnumerable third = new[] { 5 }; - IEnumerable<(int, int, int)> expected = new[] { (1, 3, 5) }; + IEnumerable first = [1, 2]; + IEnumerable second = [3, 4]; + IEnumerable third = [5]; + IEnumerable<(int, int, int)> expected = [(1, 3, 5)]; Assert.Equal(expected, first.Zip(second, third)); } @@ -709,10 +709,10 @@ public void Zip3_ThirdOneLess() [Fact] public void Zip3_ThirdManyLess() { - IEnumerable first = new[] { 1, 2, 3 }; - IEnumerable second = new[] { 3, 4, 5 }; - IEnumerable third = new[] { 5 }; - IEnumerable<(int, int, int)> expected = new[] { (1, 3, 5) }; + IEnumerable first = [1, 2, 3]; + IEnumerable second = [3, 4, 5]; + IEnumerable third = [5]; + IEnumerable<(int, int, int)> expected = [(1, 3, 5)]; Assert.Equal(expected, first.Zip(second, third)); } @@ -720,10 +720,10 @@ public void Zip3_ThirdManyLess() [Fact] public void Zip3_RunOnce() { - IEnumerable first = new[] { 1, 2 }; - IEnumerable second = new[] { 3, 4 }; - IEnumerable third = new[] { 5, 6 }; - IEnumerable<(int, int, int)> expected = new[] { (1, 3, 5), (2, 4, 6) }; + IEnumerable first = [1, 2]; + IEnumerable second = [3, 4]; + IEnumerable third = [5, 6]; + IEnumerable<(int, int, int)> expected = [(1, 3, 5), (2, 4, 6)]; Assert.Equal(expected, first.RunOnce().Zip(second.RunOnce(), third.RunOnce())); }