diff --git a/src/libraries/System.Linq.AsyncEnumerable/ref/System.Linq.AsyncEnumerable.cs b/src/libraries/System.Linq.AsyncEnumerable/ref/System.Linq.AsyncEnumerable.cs index 6153d0bd56a07c..881b679a19f3ae 100644 --- a/src/libraries/System.Linq.AsyncEnumerable/ref/System.Linq.AsyncEnumerable.cs +++ b/src/libraries/System.Linq.AsyncEnumerable/ref/System.Linq.AsyncEnumerable.cs @@ -132,6 +132,7 @@ public static partial class AsyncEnumerable public static System.Collections.Generic.IAsyncEnumerable Select(this System.Collections.Generic.IAsyncEnumerable source, System.Func> selector) { throw null; } public static System.Collections.Generic.IAsyncEnumerable Select(this System.Collections.Generic.IAsyncEnumerable source, System.Func selector) { throw null; } public static System.Threading.Tasks.ValueTask SequenceEqualAsync(this System.Collections.Generic.IAsyncEnumerable first, System.Collections.Generic.IAsyncEnumerable second, System.Collections.Generic.IEqualityComparer? comparer = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } + public static System.Collections.Generic.IAsyncEnumerable Shuffle(this System.Collections.Generic.IAsyncEnumerable source) { throw null; } public static System.Threading.Tasks.ValueTask SingleAsync(this System.Collections.Generic.IAsyncEnumerable source, System.Func predicate, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } public static System.Threading.Tasks.ValueTask SingleAsync(this System.Collections.Generic.IAsyncEnumerable source, System.Func> predicate, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } public static System.Threading.Tasks.ValueTask SingleAsync(this System.Collections.Generic.IAsyncEnumerable source, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } diff --git a/src/libraries/System.Linq.AsyncEnumerable/src/System.Linq.AsyncEnumerable.csproj b/src/libraries/System.Linq.AsyncEnumerable/src/System.Linq.AsyncEnumerable.csproj index 65d7a694bb6a54..3c5a3aa8f2257d 100644 --- a/src/libraries/System.Linq.AsyncEnumerable/src/System.Linq.AsyncEnumerable.csproj +++ b/src/libraries/System.Linq.AsyncEnumerable/src/System.Linq.AsyncEnumerable.csproj @@ -12,6 +12,7 @@ + diff --git a/src/libraries/System.Linq.AsyncEnumerable/src/System/Linq/Shuffle.cs b/src/libraries/System.Linq.AsyncEnumerable/src/System/Linq/Shuffle.cs new file mode 100644 index 00000000000000..a798976a5aef84 --- /dev/null +++ b/src/libraries/System.Linq.AsyncEnumerable/src/System/Linq/Shuffle.cs @@ -0,0 +1,59 @@ +// 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 System.Runtime.CompilerServices; +using System.Threading; + +namespace System.Linq +{ + public static partial class AsyncEnumerable + { +#if !NET + [ThreadStatic] + private static Random? t_random; +#endif + + /// Shuffles the order of the elements of a sequence. + /// The type of the elements of . + /// A sequence of values to shuffle. + /// A sequence whose elements correspond to those of the input sequence in randomized order. + /// Randomization is performed using a non-cryptographically-secure random number generator. + public static IAsyncEnumerable Shuffle( + this IAsyncEnumerable source) + { + ThrowHelper.ThrowIfNull(source); + + return Impl(source, default); + + static async IAsyncEnumerable Impl( + IAsyncEnumerable source, + [EnumeratorCancellation] CancellationToken cancellationToken) + { + TSource[] array = await source.ToArrayAsync(cancellationToken).ConfigureAwait(false); + +#if NET + Random.Shared.Shuffle(array); +#else + Random random = t_random ??= new Random(Environment.TickCount ^ Environment.CurrentManagedThreadId); + int n = array.Length; + for (int i = 0; i < n - 1; i++) + { + int j = random.Next(i, n); + if (j != i) + { + TSource temp = array[i]; + array[i] = array[j]; + array[j] = temp; + } + } +#endif + + for (int i = 0; i < array.Length; i++) + { + yield return array[i]; + } + } + } + } +} diff --git a/src/libraries/System.Linq.AsyncEnumerable/tests/ShuffleTests.cs b/src/libraries/System.Linq.AsyncEnumerable/tests/ShuffleTests.cs new file mode 100644 index 00000000000000..39130f280099df --- /dev/null +++ b/src/libraries/System.Linq.AsyncEnumerable/tests/ShuffleTests.cs @@ -0,0 +1,69 @@ +// 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 System.Threading; +using System.Threading.Tasks; +using Xunit; + +namespace System.Linq.Tests +{ + public class ShuffleTests : AsyncEnumerableTests + { + [Fact] + public void InvalidInputs_Throws() + { + AssertExtensions.Throws("source", () => AsyncEnumerable.Shuffle(null)); + } + + [Theory] + [InlineData(new int[0])] + [InlineData(new int[] { 1 })] + [InlineData(new int[] { 2, 4, 8 })] + [InlineData(new int[] { -1, 2, 5, 6, 7, 8 })] + public async Task VariousValues_ContainsAllInputValues(int[] values) + { + foreach (IAsyncEnumerable source in CreateSources(values)) + { + int[] shuffled = await source.Shuffle().ToArrayAsync(); + Array.Sort(shuffled); + Assert.Equal(values, shuffled); + } + } + + [Fact] + public async Task ToArrayAsync_ElementsAreRandomized() + { + // The chance that shuffling a thousand elements produces the same order twice is infinitesimal. + int length = 1000; + foreach (IAsyncEnumerable source in CreateSources(Enumerable.Range(0, length).ToArray())) + { + int[] first = await source.Shuffle().ToArrayAsync(); + int[] second = await source.Shuffle().ToArrayAsync(); + Assert.Equal(length, first.Length); + Assert.Equal(length, second.Length); + Assert.NotEqual(first, second); + } + } + + [Fact] + public async Task Cancellation_Cancels() + { + IAsyncEnumerable source = CreateSource(2, 4, 8, 16); + await Assert.ThrowsAsync(async () => + { + await ConsumeAsync(source.Shuffle().WithCancellation(new CancellationToken(true))); + }); + } + + [Fact] + public async Task InterfaceCalls_ExpectedCounts() + { + TrackingAsyncEnumerable source = CreateSource(2, 4, 8, 16).Track(); + await ConsumeAsync(source.Shuffle()); + Assert.Equal(5, source.MoveNextAsyncCount); + Assert.Equal(4, source.CurrentCount); + Assert.Equal(1, source.DisposeAsyncCount); + } + } +} diff --git a/src/libraries/System.Linq.AsyncEnumerable/tests/System.Linq.AsyncEnumerable.Tests.csproj b/src/libraries/System.Linq.AsyncEnumerable/tests/System.Linq.AsyncEnumerable.Tests.csproj index 1a819d87b173b9..164e0e77aa690c 100644 --- a/src/libraries/System.Linq.AsyncEnumerable/tests/System.Linq.AsyncEnumerable.Tests.csproj +++ b/src/libraries/System.Linq.AsyncEnumerable/tests/System.Linq.AsyncEnumerable.Tests.csproj @@ -10,6 +10,7 @@ + diff --git a/src/libraries/System.Linq.Queryable/ref/System.Linq.Queryable.cs b/src/libraries/System.Linq.Queryable/ref/System.Linq.Queryable.cs index 2db91584c129d9..38bff9dcfbf048 100644 --- a/src/libraries/System.Linq.Queryable/ref/System.Linq.Queryable.cs +++ b/src/libraries/System.Linq.Queryable/ref/System.Linq.Queryable.cs @@ -158,6 +158,7 @@ public static partial class Queryable public static System.Linq.IQueryable Select(this System.Linq.IQueryable source, System.Linq.Expressions.Expression> selector) { throw null; } public static bool SequenceEqual(this System.Linq.IQueryable source1, System.Collections.Generic.IEnumerable source2) { throw null; } public static bool SequenceEqual(this System.Linq.IQueryable source1, System.Collections.Generic.IEnumerable source2, System.Collections.Generic.IEqualityComparer? comparer) { throw null; } + public static System.Linq.IQueryable Shuffle(this System.Linq.IQueryable source) { throw null; } public static TSource? SingleOrDefault(this System.Linq.IQueryable source) { throw null; } public static TSource? SingleOrDefault(this System.Linq.IQueryable source, System.Linq.Expressions.Expression> predicate) { throw null; } public static TSource SingleOrDefault(this System.Linq.IQueryable source, System.Linq.Expressions.Expression> predicate, TSource defaultValue) { throw null; } diff --git a/src/libraries/System.Linq.Queryable/src/System/Linq/Queryable.cs b/src/libraries/System.Linq.Queryable/src/System/Linq/Queryable.cs index 0bfe55333b9f1d..802ef47e0ee711 100644 --- a/src/libraries/System.Linq.Queryable/src/System/Linq/Queryable.cs +++ b/src/libraries/System.Linq.Queryable/src/System/Linq/Queryable.cs @@ -1587,6 +1587,18 @@ public static bool SequenceEqual(this IQueryable source1, IEnu Expression.Constant(comparer, typeof(IEqualityComparer)))); } + [DynamicDependency("Shuffle`1", typeof(Enumerable))] + public static IQueryable Shuffle(this IQueryable source) + { + ArgumentNullException.ThrowIfNull(source); + + return source.Provider.CreateQuery( + Expression.Call( + null, + new Func, IQueryable>(Shuffle).Method, + source.Expression)); + } + [DynamicDependency("Any`1", typeof(Enumerable))] public static bool Any(this IQueryable source) { diff --git a/src/libraries/System.Linq.Queryable/tests/ShuffleTests.cs b/src/libraries/System.Linq.Queryable/tests/ShuffleTests.cs new file mode 100644 index 00000000000000..6d9656a80c46e5 --- /dev/null +++ b/src/libraries/System.Linq.Queryable/tests/ShuffleTests.cs @@ -0,0 +1,37 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using Xunit; + +namespace System.Linq.Tests +{ + public class ShuffleTests : EnumerableBasedTests + { + [Fact] + public void InvalidArguments() + { + AssertExtensions.Throws("source", () => ((IQueryable)null).Shuffle()); + } + + [Fact] + public void ProducesAllElements() + { + int[] shuffled = Enumerable.Range(0, 1000).AsQueryable().Shuffle().ToArray(); + Array.Sort(shuffled); + Assert.Equal(Enumerable.Range(0, shuffled.Length), shuffled); + } + + [Fact] + public void ElementsAreRandomized() + { + // The chance that shuffling a thousand elements produces the same order twice is infinitesimal. + const int Length = 1000; + IQueryable source = Enumerable.Range(0, Length).AsQueryable().Shuffle(); + int[] first = source.ToArray(); + int[] second = source.ToArray(); + Assert.Equal(Length, first.Length); + Assert.Equal(Length, second.Length); + Assert.NotEqual(first, second); + } + } +} diff --git a/src/libraries/System.Linq.Queryable/tests/System.Linq.Queryable.Tests.csproj b/src/libraries/System.Linq.Queryable/tests/System.Linq.Queryable.Tests.csproj index 5d35929945edfd..9fcf767d3e81b0 100644 --- a/src/libraries/System.Linq.Queryable/tests/System.Linq.Queryable.Tests.csproj +++ b/src/libraries/System.Linq.Queryable/tests/System.Linq.Queryable.Tests.csproj @@ -31,6 +31,7 @@ + @@ -62,7 +63,6 @@ - + diff --git a/src/libraries/System.Linq/ref/System.Linq.cs b/src/libraries/System.Linq/ref/System.Linq.cs index 20682db0deb76f..50657cf628e7d9 100644 --- a/src/libraries/System.Linq/ref/System.Linq.cs +++ b/src/libraries/System.Linq/ref/System.Linq.cs @@ -4,6 +4,8 @@ // Changes to this file must follow the https://aka.ms/api-review process. // ------------------------------------------------------------------------------ +using System.Collections.Generic; + namespace System.Linq { public static partial class Enumerable @@ -172,6 +174,7 @@ public static System.Collections.Generic.IEnumerable< public static System.Collections.Generic.IEnumerable Select(this System.Collections.Generic.IEnumerable source, System.Func selector) { throw null; } public static bool SequenceEqual(this System.Collections.Generic.IEnumerable first, System.Collections.Generic.IEnumerable second) { throw null; } public static bool SequenceEqual(this System.Collections.Generic.IEnumerable first, System.Collections.Generic.IEnumerable second, System.Collections.Generic.IEqualityComparer? comparer) { throw null; } + public static System.Collections.Generic.IEnumerable Shuffle(this System.Collections.Generic.IEnumerable source) { throw null; } public static TSource? SingleOrDefault(this System.Collections.Generic.IEnumerable source) { throw null; } public static TSource SingleOrDefault(this System.Collections.Generic.IEnumerable source, TSource defaultValue) { throw null; } public static TSource? SingleOrDefault(this System.Collections.Generic.IEnumerable source, System.Func predicate) { throw null; } diff --git a/src/libraries/System.Linq/src/System.Linq.csproj b/src/libraries/System.Linq/src/System.Linq.csproj index cbbec2028cf8d7..7d5854ad4dd983 100644 --- a/src/libraries/System.Linq/src/System.Linq.csproj +++ b/src/libraries/System.Linq/src/System.Linq.csproj @@ -54,6 +54,8 @@ + + diff --git a/src/libraries/System.Linq/src/System/Linq/Shuffle.SpeedOpt.cs b/src/libraries/System.Linq/src/System/Linq/Shuffle.SpeedOpt.cs new file mode 100644 index 00000000000000..1b4d343e890228 --- /dev/null +++ b/src/libraries/System.Linq/src/System/Linq/Shuffle.SpeedOpt.cs @@ -0,0 +1,280 @@ +// 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 System.Diagnostics; +using System.Runtime.InteropServices; + +namespace System.Linq +{ + public static partial class Enumerable + { + private sealed partial class ShuffleIterator + { + public override TSource[] ToArray() + { + TSource[] array = _source.ToArray(); + Random.Shared.Shuffle(array); + return array; + } + + public override List ToList() + { + List list = _source.ToList(); + Random.Shared.Shuffle(CollectionsMarshal.AsSpan(list)); + return list; + } + + public override int GetCount(bool onlyIfCheap) => + !onlyIfCheap ? _source.Count() : + TryGetNonEnumeratedCount(_source, out int count) ? count : + -1; + + public override TSource? TryGetFirst(out bool found) => + TryGetElementAt(0, out found); + + public override TSource? TryGetLast(out bool found) => + TryGetElementAt(0, out found); + + public override TSource? TryGetElementAt(int index, out bool found) + { + if (_source is Iterator iterator && + iterator.GetCount(onlyIfCheap: true) is int iteratorCount && + iteratorCount >= 0) + { + if ((uint)index < (uint)iteratorCount) + { + return iterator.TryGetElementAt(Random.Shared.Next(0, iteratorCount), out found); + } + } + else if (_source is IList list) + { + int listCount = list.Count; + if ((uint)index < (uint)listCount) + { + found = true; + return list[Random.Shared.Next(0, listCount)]; + } + } + else if (index >= 0) + { + List? sample = ShuffleTakeIterator.SampleToList(_source, 1, out long totalElementCount); + if (sample is not null && index < totalElementCount) + { + found = true; + return sample[0]; + } + } + + found = false; + return default; + } + + public override Iterator? Take(int count) + { + // If the source is known to have fewer elements than count, we're best off just using the default implementation. + if (_source.TryGetNonEnumeratedCount(out int sourceCount) && sourceCount <= count) + { + return base.Take(count); + } + + // Otherwise, we either don't know how many elements are in the source, or we know it's more than count. + // Try to optimize by using reservoir sampling to get a random sample of count elements. + return new ShuffleTakeIterator(_source, count); + } + } + + private sealed partial class ShuffleTakeIterator : Iterator + { + private readonly IEnumerable _source; + private readonly int _takeCount; + private List? _buffer; + + public ShuffleTakeIterator(IEnumerable source, int takeCount) + { + Debug.Assert(source is not null); + Debug.Assert(takeCount > 0); + + _source = source; + _takeCount = takeCount; + } + + private protected override Iterator Clone() => new ShuffleTakeIterator(_source, _takeCount); + + public override bool MoveNext() + { + int state = _state; + + Initialized: + if (state > 1) + { + List? buffer = _buffer; + Debug.Assert(buffer is not null); + + int i = state - 2; + if (i < buffer.Count) + { + _current = buffer[i]; + _state++; + return true; + } + } + else if (state == 1) + { + List? buffer = SampleToList(_source, _takeCount, out _); + if (buffer is not null) + { + _buffer = buffer; + _state = state = 2; + goto Initialized; + } + } + + Dispose(); + return false; + } + + public override void Dispose() + { + _buffer = null; + base.Dispose(); + } + + public override TSource[] ToArray() => SampleToList(_source, _takeCount, out _)?.ToArray() ?? []; + + public override List ToList() => SampleToList(_source, _takeCount, out _) ?? []; + + public override int GetCount(bool onlyIfCheap) => + TryGetNonEnumeratedCount(_source, out int count) ? Math.Min(_takeCount, count) : + !onlyIfCheap ? Math.Min(_takeCount, _source.Take(_takeCount).Count()) : + -1; + + public override TSource? TryGetFirst(out bool found) => + TryGetElementAt(0, out found); + + public override TSource? TryGetLast(out bool found) => + TryGetElementAt(0, out found); + + public override TSource? TryGetElementAt(int index, out bool found) + { + if (_source is Iterator iterator && + iterator.GetCount(onlyIfCheap: true) is int iteratorCount && + iteratorCount >= 0) + { + if ((uint)index < (uint)Math.Min(_takeCount, iteratorCount)) + { + return iterator.TryGetElementAt(Random.Shared.Next(0, iteratorCount), out found); + } + } + else if (_source is IList list) + { + int count = list.Count; + if ((uint)index < (uint)Math.Min(_takeCount, count)) + { + found = true; + return list[Random.Shared.Next(0, count)]; + } + } + else if (index >= 0) + { + List? sample = SampleToList(_source, 1, out long totalElementCount); + if (sample is not null && index < Math.Min(_takeCount, totalElementCount)) + { + found = true; + return sample[0]; + } + } + + found = false; + return default; + } + + public override Iterator? Take(int count) => + _takeCount <= count ? this : new ShuffleTakeIterator(_source, count); + + /// Uses reservoir sampling to randomly select elements from . + internal static List? SampleToList(IEnumerable source, int takeCount, out long totalElementCount) + { + List? reservoir = null; + + if (source is IList list) + { + int listCount = list.Count; + Debug.Assert(listCount > takeCount, "Known listCount <= takeCount should have been handled by Iterator.Take override"); + + reservoir = new(takeCount); + + // Fill the reservoir with the first takeCount elements from the source. + for (int i = 0; i < takeCount; i++) + { + reservoir.Add(list[i]); + } + + // For each subsequent element in the source, randomly replace an element in the + // reservoir with a decreasing probability. + for (int i = takeCount; i < listCount; i++) + { + int r = Random.Shared.Next(i + 1); + if (r < takeCount) + { + reservoir[r] = list[i]; + } + } + + totalElementCount = listCount; + } + else + { + using IEnumerator e = source.GetEnumerator(); + if (e.MoveNext()) + { + // Fill the reservoir with the first takeCount elements from the source. + // If we can't fill it, just return what we get. + reservoir = new List(Math.Min(takeCount, 4)) { e.Current }; + while (reservoir.Count < takeCount) + { + if (!e.MoveNext()) + { + totalElementCount = reservoir.Count; + goto ReturnReservoir; + } + + reservoir.Add(e.Current); + } + + // For each subsequent element in the source, randomly replace an element in the + // reservoir with a decreasing probability. + long i = takeCount; + while (e.MoveNext()) + { + i++; + long r = Random.Shared.NextInt64(i); + if (r < takeCount) + { + reservoir[(int)r] = e.Current; + } + } + + totalElementCount = i; + } + else + { + totalElementCount = 0; + } + } + + ReturnReservoir: + if (reservoir is not null) + { + // Ensure that elements in the reservoir are in random order. The sampling helped + // to ensure we got a uniform distribution from the source into the reservoir, but + // it didn't randomize the order of the reservoir itself; this is especially relevant + // to the elements initially added into the reservoir. + Random.Shared.Shuffle(CollectionsMarshal.AsSpan(reservoir)); + } + + return reservoir; + } + } + } +} diff --git a/src/libraries/System.Linq/src/System/Linq/Shuffle.cs b/src/libraries/System.Linq/src/System/Linq/Shuffle.cs new file mode 100644 index 00000000000000..43d115d480dd36 --- /dev/null +++ b/src/libraries/System.Linq/src/System/Linq/Shuffle.cs @@ -0,0 +1,85 @@ +// 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 System.Diagnostics; + +namespace System.Linq +{ + public static partial class Enumerable + { + /// Shuffles the order of the elements of a sequence. + /// The type of the elements of . + /// A sequence of values to shuffle. + /// A sequence whose elements correspond to those of the input sequence in randomized order. + /// Randomization is performed using a non-cryptographically-secure random number generator. + public static IEnumerable Shuffle(this IEnumerable source) + { + if (source is null) + { + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source); + } + + if (IsEmptyArray(source)) + { + return []; + } + + return new ShuffleIterator(source); + } + + private sealed partial class ShuffleIterator : Iterator + { + private readonly IEnumerable _source; + private TSource[]? _buffer; + + public ShuffleIterator(IEnumerable source) + { + Debug.Assert(source is not null); + _source = source; + } + + private protected override Iterator Clone() => new ShuffleIterator(_source); + + public override bool MoveNext() + { + int state = _state; + + Initialized: + if (state > 1) + { + TSource[]? buffer = _buffer; + Debug.Assert(buffer is not null); + + int i = state - 2; + if ((uint)i < (uint)buffer.Length) + { + _current = buffer[i]; + _state++; + return true; + } + } + else if (state == 1) + { + TSource[] buffer = _source.ToArray(); + if (buffer.Length != 0) + { + Random.Shared.Shuffle(buffer); + _buffer = buffer; + _state = state = 2; + goto Initialized; + } + } + + Dispose(); + return false; + } + + public override void Dispose() + { + _buffer = null; + base.Dispose(); + } + } + } +} diff --git a/src/libraries/System.Linq/tests/AverageTests.cs b/src/libraries/System.Linq/tests/AverageTests.cs index 888d6e8043c1c4..6c3920d65c70eb 100644 --- a/src/libraries/System.Linq/tests/AverageTests.cs +++ b/src/libraries/System.Linq/tests/AverageTests.cs @@ -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 [Shuffler.Shuffle(Enumerable.Range(1, i)), expected]; - yield return [Shuffler.Shuffle(Enumerable.Range(1, i).ToArray()), expected]; + yield return [Enumerable.Range(1, i).Shuffle(), expected]; } } @@ -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 [Shuffler.Shuffle(Enumerable.Range(1, i).Select(i => (long)i)), expected]; - yield return [Shuffler.Shuffle(Enumerable.Range(1, i).Select(i => (long)i).ToArray()), expected]; + yield return [Enumerable.Range(1, i).Select(i => (long)i).Shuffle(), expected]; } } @@ -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 [Shuffler.Shuffle(Enumerable.Range(1, i).Select(i => (double)i)), expected]; - yield return [Shuffler.Shuffle(Enumerable.Range(1, i).Select(i => (double)i).ToArray()), expected]; + yield return [Enumerable.Range(1, i).Select(i => (double)i).Shuffle(), expected]; } } @@ -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 [Shuffler.Shuffle(Enumerable.Range(1, i).Select(i => (decimal)i)), expected]; - yield return [Shuffler.Shuffle(Enumerable.Range(1, i).Select(i => (decimal)i).ToArray()), expected]; + yield return [Enumerable.Range(1, i).Select(i => (decimal)i).Shuffle(), expected]; } } @@ -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 [Shuffler.Shuffle(Enumerable.Range(1, i).Select(i => (float)i)), expected]; - yield return [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/src/libraries/System.Linq/tests/MaxTests.cs b/src/libraries/System.Linq/tests/MaxTests.cs index ad9ff571c8c5d4..94baed9a7ef520 100644 --- a/src/libraries/System.Linq/tests/MaxTests.cs +++ b/src/libraries/System.Linq/tests/MaxTests.cs @@ -13,56 +13,29 @@ public static IEnumerable Max_AllTypes_TestData() { for (int length = 2; length < 65; length++) { - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (byte)i)), (byte)(length + length - 1)]; - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (byte)i).ToArray()), (byte)(length + length - 1)]; + yield return [Enumerable.Range(length, length).Select(i => (byte)i).Shuffle(), (byte)(length + length - 1)]; + yield return [Enumerable.Range(length, length).Select(i => (byte)i).ToArray().Shuffle(), (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 [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (sbyte)i)), (sbyte)(length + length - 1)]; - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (sbyte)i).ToArray()), (sbyte)(length + length - 1)]; + yield return [Enumerable.Range(length, length).Select(i => (sbyte)i).Shuffle(), (sbyte)(length + length - 1)]; + yield return [Enumerable.Range(length, length).Select(i => (sbyte)i).ToArray().Shuffle(), (sbyte)(length + length - 1)]; } - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (ushort)i)), (ushort)(length + length - 1)]; - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (ushort)i).ToArray()), (ushort)(length + length - 1)]; - - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (short)i)), (short)(length + length - 1)]; - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (short)i).ToArray()), (short)(length + length - 1)]; - - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (char)i)), (char)(length + length - 1)]; - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (char)i).ToArray()), (char)(length + length - 1)]; - - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (uint)i)), (uint)(length + length - 1)]; - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (uint)i).ToArray()), (uint)(length + length - 1)]; - - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (int)i)), (int)(length + length - 1)]; - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (int)i).ToArray()), (int)(length + length - 1)]; - - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (ulong)i)), (ulong)(length + length - 1)]; - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (ulong)i).ToArray()), (ulong)(length + length - 1)]; - - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (long)i)), (long)(length + length - 1)]; - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (long)i).ToArray()), (long)(length + length - 1)]; - - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (float)i)), (float)(length + length - 1)]; - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (float)i).ToArray()), (float)(length + length - 1)]; - - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (double)i)), (double)(length + length - 1)]; - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (double)i).ToArray()), (double)(length + length - 1)]; - - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (decimal)i)), (decimal)(length + length - 1)]; - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (decimal)i).ToArray()), (decimal)(length + length - 1)]; - - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (nuint)i)), (nuint)(length + length - 1)]; - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (nuint)i).ToArray()), (nuint)(length + length - 1)]; - - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (nint)i)), (nint)(length + length - 1)]; - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (nint)i).ToArray()), (nint)(length + length - 1)]; - - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (Int128)i)), (Int128)(length + length - 1)]; - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (Int128)i).ToArray()), (Int128)(length + length - 1)]; - - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (UInt128)i)), (UInt128)(length + length - 1)]; - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (UInt128)i).ToArray()), (UInt128)(length + length - 1)]; + yield return [Enumerable.Range(length, length).Select(i => (ushort)i).Shuffle(), (ushort)(length + length - 1)]; + yield return [Enumerable.Range(length, length).Select(i => (short)i).Shuffle(), (short)(length + length - 1)]; + yield return [Enumerable.Range(length, length).Select(i => (char)i).Shuffle(), (char)(length + length - 1)]; + yield return [Enumerable.Range(length, length).Select(i => (uint)i).Shuffle(), (uint)(length + length - 1)]; + yield return [Enumerable.Range(length, length).Select(i => (int)i).Shuffle(), (int)(length + length - 1)]; + yield return [Enumerable.Range(length, length).Select(i => (ulong)i).Shuffle(), (ulong)(length + length - 1)]; + yield return [Enumerable.Range(length, length).Select(i => (long)i).Shuffle(), (long)(length + length - 1)]; + yield return [Enumerable.Range(length, length).Select(i => (float)i).Shuffle(), (float)(length + length - 1)]; + yield return [Enumerable.Range(length, length).Select(i => (double)i).Shuffle(), (double)(length + length - 1)]; + yield return [Enumerable.Range(length, length).Select(i => (decimal)i).Shuffle(), (decimal)(length + length - 1)]; + yield return [Enumerable.Range(length, length).Select(i => (nuint)i).Shuffle(), (nuint)(length + length - 1)]; + yield return [Enumerable.Range(length, length).Select(i => (nint)i).Shuffle(), (nint)(length + length - 1)]; + yield return [Enumerable.Range(length, length).Select(i => (Int128)i).Shuffle(), (Int128)(length + length - 1)]; + yield return [Enumerable.Range(length, length).Select(i => (UInt128)i).Shuffle(), (UInt128)(length + length - 1)]; } } diff --git a/src/libraries/System.Linq/tests/MinTests.cs b/src/libraries/System.Linq/tests/MinTests.cs index 1f4c6ed1097619..8774777bfa744e 100644 --- a/src/libraries/System.Linq/tests/MinTests.cs +++ b/src/libraries/System.Linq/tests/MinTests.cs @@ -13,48 +13,24 @@ public static IEnumerable Min_AllTypes_TestData() { for (int length = 2; length < 65; length++) { - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (byte)i)), (byte)length]; - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (byte)i).ToArray()), (byte)length - ]; + yield return [Enumerable.Range(length, length).Select(i => (byte)i).Shuffle(), (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 [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (sbyte)i)), (sbyte)length]; - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (sbyte)i).ToArray()), (sbyte)length]; + yield return [Enumerable.Range(length, length).Select(i => (sbyte)i).Shuffle(), (sbyte)length]; } - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (ushort)i)), (ushort)length]; - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (ushort)i).ToArray()), (ushort)length]; - - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (short)i)), (short)length]; - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (short)i).ToArray()), (short)length]; - - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (uint)i)), (uint)length]; - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (uint)i).ToArray()), (uint)length]; - - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (int)i)), (int)length]; - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (int)i).ToArray()), (int)length]; - - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (ulong)i)), (ulong)length]; - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (ulong)i).ToArray()), (ulong)length]; - - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (long)i)), (long)length]; - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (long)i).ToArray()), (long)length]; - - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (float)i)), (float)length]; - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (float)i).ToArray()), (float)length]; - - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (double)i)), (double)length]; - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (double)i).ToArray()), (double)length]; - - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (decimal)i)), (decimal)length]; - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (decimal)i).ToArray()), (decimal)length]; - - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (nuint)i)), (nuint)length]; - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (nuint)i).ToArray()), (nuint)length]; - - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (nint)i)), (nint)length]; - yield return [Shuffler.Shuffle(Enumerable.Range(length, length).Select(i => (nint)i).ToArray()), (nint)length]; + yield return [Enumerable.Range(length, length).Select(i => (ushort)i).Shuffle(), (ushort)length]; + yield return [Enumerable.Range(length, length).Select(i => (short)i).Shuffle(), (short)length]; + yield return [Enumerable.Range(length, length).Select(i => (uint)i).Shuffle(), (uint)length]; + yield return [Enumerable.Range(length, length).Select(i => (int)i).Shuffle(), (int)length]; + yield return [Enumerable.Range(length, length).Select(i => (ulong)i).Shuffle(), (ulong)length]; + yield return [Enumerable.Range(length, length).Select(i => (long)i).Shuffle(), (long)length]; + yield return [Enumerable.Range(length, length).Select(i => (float)i).Shuffle(), (float)length]; + yield return [Enumerable.Range(length, length).Select(i => (double)i).Shuffle(), (double)length]; + yield return [Enumerable.Range(length, length).Select(i => (decimal)i).Shuffle(), (decimal)length]; + yield return [Enumerable.Range(length, length).Select(i => (nuint)i).Shuffle(), (nuint)length]; + yield return [Enumerable.Range(length, length).Select(i => (nint)i).Shuffle(), (nint)length]; } } diff --git a/src/libraries/System.Linq/tests/ShuffleTests.cs b/src/libraries/System.Linq/tests/ShuffleTests.cs new file mode 100644 index 00000000000000..ade04ecee7a984 --- /dev/null +++ b/src/libraries/System.Linq/tests/ShuffleTests.cs @@ -0,0 +1,387 @@ +// 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 System.Runtime.InteropServices; +using Xunit; + +namespace System.Linq.Tests +{ + public class ShuffleTests : EnumerableTests + { + [Fact] + public void InvalidArguments() + { + AssertExtensions.Throws("source", () => Enumerable.Shuffle(null)); + } + + [Theory] + [InlineData(0)] + [InlineData(1)] + [InlineData(2)] + [InlineData(3)] + [InlineData(100)] + public void Count_ExpectedCountReturned(int length) + { + Assert.All(CreateSources(Enumerable.Range(0, length)), source => + { + Assert.Equal(length, source.Shuffle().Count()); + + IEnumerable shuffled = source.Shuffle(); + Assert.Equal(length, shuffled.Count()); + Assert.Equal(length, shuffled.Count()); + + Assert.Equal(length, source.Shuffle().Take(length).Count()); + Assert.Equal(Math.Min(length, 1), source.Shuffle().Take(1).Count()); + Assert.Equal(Math.Min(length, 1), source.Shuffle().Take(2).Take(1).Count()); + Assert.Equal(Math.Min(length, 1), source.Shuffle().Take(1).Take(2).Count()); + }); + } + + [Theory] + [InlineData(0)] + [InlineData(1)] + [InlineData(2)] + [InlineData(3)] + [InlineData(100)] + public void Enumeration_AllElementsReturned(int length) + { + Assert.All(CreateSources(Enumerable.Range(0, length)), source => + { + List shuffled; + + shuffled = []; + foreach (int i in source.Shuffle()) shuffled.Add(i); + Assert.Equal(length, shuffled.Count); + shuffled.Sort(); + Assert.Equal(Enumerable.Range(0, length), shuffled); + + shuffled = []; + foreach (int i in source.Shuffle().Take(length)) shuffled.Add(i); + Assert.Equal(length, shuffled.Count); + shuffled.Sort(); + Assert.Equal(Enumerable.Range(0, length), shuffled); + }); + } + + [Theory] + [InlineData(0)] + [InlineData(1)] + [InlineData(2)] + [InlineData(3)] + [InlineData(100)] + public void ToArray_AllElementsReturned(int length) + { + Assert.All(CreateSources(Enumerable.Range(0, length)), source => + { + int[] shuffled; + + shuffled = source.Shuffle().ToArray(); + Assert.Equal(length, shuffled.Length); + Array.Sort(shuffled); + Assert.Equal(Enumerable.Range(0, length), shuffled); + + shuffled = source.Shuffle().Take(length).ToArray(); + Assert.Equal(length, shuffled.Length); + Array.Sort(shuffled); + Assert.Equal(Enumerable.Range(0, length), shuffled); + }); + } + + [Theory] + [InlineData(0)] + [InlineData(1)] + [InlineData(2)] + [InlineData(3)] + [InlineData(100)] + public void ToList_AllElementsReturned(int length) + { + Assert.All(CreateSources(Enumerable.Range(0, length)), source => + { + List shuffled; + + shuffled = source.Shuffle().ToList(); + Assert.Equal(length, shuffled.Count); + shuffled.Sort(); + Assert.Equal(Enumerable.Range(0, length), shuffled); + + shuffled = source.Shuffle().Take(length).ToList(); + Assert.Equal(length, shuffled.Count); + shuffled.Sort(); + Assert.Equal(Enumerable.Range(0, length), shuffled); + }); + } + + [Fact] + public void Enumeration_ElementsAreRandomized() + { + // The chance that shuffling a thousand elements produces the same order twice is infinitesimal. + int length = 1000; + Assert.All(CreateSources(Enumerable.Range(0, length)), source => + { + List first, second; + + first = []; + second = []; + foreach (int i in source.Shuffle()) first.Add(i); + foreach (int i in source.Shuffle()) second.Add(i); + Assert.Equal(length, first.Count); + Assert.Equal(length, second.Count); + Assert.Equal(length, first.Distinct().Count()); + Assert.Equal(length, second.Distinct().Count()); + Assert.NotEqual(first, second); + + foreach (int takeCount in new[] { length - 1, length + 1 }) + { + first = []; + second = []; + foreach (int i in source.Shuffle().Take(takeCount)) first.Add(i); + foreach (int i in source.Shuffle().Take(takeCount)) second.Add(i); + Assert.Equal(Math.Min(takeCount, length), first.Count); + Assert.Equal(Math.Min(takeCount, length), second.Count); + Assert.Equal(Math.Min(takeCount, length), first.Distinct().Count()); + Assert.Equal(Math.Min(takeCount, length), second.Distinct().Count()); + Assert.NotEqual(first, second); + + first = []; + second = []; + foreach (int i in source.Shuffle().Take(takeCount + 1).Take(takeCount)) first.Add(i); + foreach (int i in source.Shuffle().Take(takeCount + 1).Take(takeCount)) second.Add(i); + Assert.Equal(Math.Min(takeCount, length), first.Count); + Assert.Equal(Math.Min(takeCount, length), second.Count); + Assert.Equal(Math.Min(takeCount, length), first.Distinct().Count()); + Assert.Equal(Math.Min(takeCount, length), second.Distinct().Count()); + Assert.NotEqual(first, second); + } + }); + } + + [Fact] + public void ToArray_ElementsAreRandomized() + { + // The chance that shuffling a thousand elements produces the same order twice is infinitesimal. + int length = 1000; + Assert.All(CreateSources(Enumerable.Range(0, length)), source => + { + try + { + int[] first, second; + + first = source.Shuffle().ToArray(); + second = source.Shuffle().ToArray(); + Assert.Equal(length, first.Length); + Assert.Equal(length, second.Length); + Assert.Equal(length, first.Distinct().Count()); + Assert.Equal(length, second.Distinct().Count()); + Assert.NotEqual(first, second); + + foreach (int takeCount in new[] { length - 1, length + 1 }) + { + first = source.Shuffle().Take(takeCount).ToArray(); + second = source.Shuffle().Take(takeCount).ToArray(); + Assert.Equal(Math.Min(takeCount, length), first.Length); + Assert.Equal(Math.Min(takeCount, length), second.Length); + Assert.Equal(Math.Min(takeCount, length), first.Distinct().Count()); + Assert.Equal(Math.Min(takeCount, length), second.Distinct().Count()); + Assert.NotEqual(first, second); + + first = source.Shuffle().Take(takeCount + 1).Take(takeCount).ToArray(); + second = source.Shuffle().Take(takeCount + 1).Take(takeCount).ToArray(); + Assert.Equal(Math.Min(takeCount, length), first.Length); + Assert.Equal(Math.Min(takeCount, length), second.Length); + Assert.Equal(Math.Min(takeCount, length), first.Distinct().Count()); + Assert.Equal(Math.Min(takeCount, length), second.Distinct().Count()); + Assert.NotEqual(first, second); + } + } + catch (Exception e) + { + throw new Exception(source.GetType().ToString(), e); + } + }); + } + + [Fact] + public void ToList_ElementsAreRandomized() + { + // The chance that shuffling a thousand elements produces the same order twice is infinitesimal. + int length = 1000; + Assert.All(CreateSources(Enumerable.Range(0, length)), source => + { + List first, second; + + first = source.Shuffle().ToList(); + second = source.Shuffle().ToList(); + Assert.Equal(length, first.Count); + Assert.Equal(length, second.Count); + Assert.Equal(length, first.Distinct().Count()); + Assert.Equal(length, second.Distinct().Count()); + Assert.NotEqual(first, second); + + foreach (int takeCount in new[] { length - 1, length + 1 }) + { + first = source.Shuffle().Take(takeCount).ToList(); + second = source.Shuffle().Take(takeCount).ToList(); + Assert.Equal(Math.Min(takeCount, length), first.Count); + Assert.Equal(Math.Min(takeCount, length), second.Count); + Assert.Equal(Math.Min(takeCount, length), first.Distinct().Count()); + Assert.Equal(Math.Min(takeCount, length), second.Distinct().Count()); + Assert.NotEqual(first, second); + + first = source.Shuffle().Take(takeCount + 1).Take(takeCount).ToList(); + second = source.Shuffle().Take(takeCount + 1).Take(takeCount).ToList(); + Assert.Equal(Math.Min(takeCount, length), first.Count); + Assert.Equal(Math.Min(takeCount, length), second.Count); + Assert.Equal(Math.Min(takeCount, length), first.Distinct().Count()); + Assert.Equal(Math.Min(takeCount, length), second.Distinct().Count()); + Assert.NotEqual(first, second); + } + }); + } + + [Fact] + public void ForcedToEnumeratorDoesntEnumerate() + { + // Don't insist on this behaviour, but check it's correct if it happens + + var iterator = NumberRangeGuaranteedNotCollectionType(0, 3).Shuffle(); + var en = iterator as IEnumerator; + Assert.False(en is not null && en.MoveNext()); + + iterator = NumberRangeGuaranteedNotCollectionType(0, 3).Shuffle().Take(1); + en = iterator as IEnumerator; + Assert.False(en is not null && en.MoveNext()); + + iterator = NumberRangeGuaranteedNotCollectionType(0, 3).Shuffle().Take(2).Take(1); + en = iterator as IEnumerator; + Assert.False(en is not null && en.MoveNext()); + } + + [Fact] + public void First_Last_GetElement_Invalid_ExpectedExceptions() + { + Assert.All(CreateSources(Enumerable.Empty()), source => + { + Assert.Throws(() => source.Shuffle().First()); + Assert.Throws(() => source.Shuffle().Last()); + Assert.Throws(() => source.Shuffle().Take(1).First()); + Assert.Throws(() => source.Shuffle().Take(1).Last()); + + Assert.Null(source.Shuffle().ElementAtOrDefault(1)); + Assert.Null(source.Shuffle().ElementAtOrDefault(-1)); + Assert.Null(source.Shuffle().FirstOrDefault()); + Assert.Null(source.Shuffle().LastOrDefault()); + + Assert.Null(source.Shuffle().Take(1).ElementAtOrDefault(1)); + Assert.Null(source.Shuffle().Take(1).ElementAtOrDefault(-1)); + Assert.Null(source.Shuffle().Take(1).FirstOrDefault()); + Assert.Null(source.Shuffle().Take(1).LastOrDefault()); + + Assert.Null(source.Shuffle().Take(3).Take(1).ElementAtOrDefault(1)); + Assert.Null(source.Shuffle().Take(3).Take(1).ElementAtOrDefault(-1)); + Assert.Null(source.Shuffle().Take(3).Take(1).FirstOrDefault()); + Assert.Null(source.Shuffle().Take(3).Take(1).LastOrDefault()); + }); + } + + [Fact] + public void First_Last_GetElement_Valid_ExpectedElements() + { + Assert.All(CreateSources(Enumerable.Range(0, 10)), source => + { + Assert.InRange(source.Shuffle().First(), 0, 9); + Assert.InRange(source.Shuffle().Last(), 0, 9); + Assert.InRange(source.Shuffle().ElementAt(5), 0, 9); + + Assert.InRange(source.Shuffle().Take(1).First(), 0, 9); + Assert.InRange(source.Shuffle().Take(1).Last(), 0, 9); + Assert.InRange(source.Shuffle().Take(8).ElementAt(5), 0, 9); + + Assert.InRange(source.Shuffle().Take(3).Take(2).First(), 0, 9); + Assert.InRange(source.Shuffle().Take(3).Take(2).Last(), 0, 9); + Assert.InRange(source.Shuffle().Take(8).Take(7).ElementAt(5), 0, 9); + }); + } + + [Fact] + public void First_Last_GetElement_ProduceRandomElements() + { + Assert.All(CreateSources(Enumerable.Range(0, 100)), source => + { + AssertRetry(() => source.Shuffle().First() != source.Shuffle().First()); + AssertRetry(() => source.Shuffle().Last() != source.Shuffle().Last()); + AssertRetry(() => source.Shuffle().ElementAt(5) != source.Shuffle().ElementAt(5)); + + AssertRetry(() => source.Shuffle().Take(10).First() != source.Shuffle().Take(10).First()); + AssertRetry(() => source.Shuffle().Take(10).Last() != source.Shuffle().Take(10).Last()); + AssertRetry(() => source.Shuffle().Take(10).ElementAt(5) != source.Shuffle().Take(10).ElementAt(5)); + + AssertRetry(() => source.Shuffle().Take(10).Take(5).First() != source.Shuffle().Take(10).Take(5).First()); + AssertRetry(() => source.Shuffle().Take(10).Take(5).Last() != source.Shuffle().Take(10).Take(5).Last()); + AssertRetry(() => source.Shuffle().Take(10).Take(5).ElementAt(3) != source.Shuffle().Take(10).Take(5).ElementAt(3)); + }); + + static void AssertRetry(Func predicate) + { + for (int i = 0; i < 10; i++) + { + if (predicate()) return; + } + + Assert.Fail("Predicate was true for 10 iterations"); + } + } + + [OuterLoop] + [Theory] + [InlineData(0)] + [InlineData(1)] + [InlineData(2)] + public void ValidateShuffleTakeRandomDistribution(int mode) + { + const int InputLength = 10; + const int Iterations = 100_000; + const double Expected = Iterations / (double)InputLength; + + IEnumerable ints = Enumerable.Range(0, InputLength); + IEnumerable[] sources = [ints, ints.ToList(), ForceNotCollection(ints)]; + + foreach (IEnumerable source in sources) + { + IEnumerable selected = source.Shuffle().Take(1); + + Dictionary counts = new(); + for (int i = 0; i < Iterations; i++) + { + int value = 0; + switch (mode) + { + case 0: + using (IEnumerator e = selected.GetEnumerator()) + { + e.MoveNext(); + value = e.Current; + } + break; + + case 1: + value = selected.First(); + break; + + case 2: + value = selected.ToArray()[0]; + break; + + default: + Assert.Fail("Invalid mode"); + break; + } + + CollectionsMarshal.GetValueRefOrAddDefault(counts, value, out _)++; + } + + Assert.All(counts, kvp => Assert.InRange(kvp.Value, Expected * 0.85, Expected * 1.15)); + } + } + } +} diff --git a/src/libraries/System.Linq/tests/Shuffler.cs b/src/libraries/System.Linq/tests/Shuffler.cs deleted file mode 100644 index 6339fe8037a3e8..00000000000000 --- a/src/libraries/System.Linq/tests/Shuffler.cs +++ /dev/null @@ -1,19 +0,0 @@ -// 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; - -namespace System.Linq.Tests -{ - public static class Shuffler - { - public static T[] Shuffle(T[] array) - { - var r = new Random(42); - r.Shuffle(array); - return array; - } - - public static IEnumerable Shuffle(this IEnumerable source) => Shuffle(source.ToArray()); - } -} diff --git a/src/libraries/System.Linq/tests/System.Linq.Tests.csproj b/src/libraries/System.Linq/tests/System.Linq.Tests.csproj index 2e1ad5368b276b..10dc8ed05e76c8 100644 --- a/src/libraries/System.Linq/tests/System.Linq.Tests.csproj +++ b/src/libraries/System.Linq/tests/System.Linq.Tests.csproj @@ -1,4 +1,4 @@ - + $(NetCoreAppCurrent) true @@ -58,7 +58,7 @@ - + @@ -79,10 +79,8 @@ - - + + diff --git a/src/libraries/System.Net.Http/tests/UnitTests/Headers/HttpHeadersTest.cs b/src/libraries/System.Net.Http/tests/UnitTests/Headers/HttpHeadersTest.cs index ce306a2e4cd48e..411a6d9f70b3b1 100644 --- a/src/libraries/System.Net.Http/tests/UnitTests/Headers/HttpHeadersTest.cs +++ b/src/libraries/System.Net.Http/tests/UnitTests/Headers/HttpHeadersTest.cs @@ -2494,7 +2494,7 @@ static HttpRequestHeaders CreateHeaders() static headers => headers.TryAddWithoutValidation("h", new[] { "i", "j" }), }; - foreach (Action action in actions.OrderBy(_ => Random.Shared.Next())) + foreach (Action action in actions.Shuffle()) { action(headers); } diff --git a/src/libraries/System.Private.Xml.Linq/tests/axes/AxisOrderValidation.cs b/src/libraries/System.Private.Xml.Linq/tests/axes/AxisOrderValidation.cs index a3a68755381f89..deb1bbdd5a4e16 100644 --- a/src/libraries/System.Private.Xml.Linq/tests/axes/AxisOrderValidation.cs +++ b/src/libraries/System.Private.Xml.Linq/tests/axes/AxisOrderValidation.cs @@ -336,8 +336,7 @@ public static void IEnumerableInDocumentOrderVariationTwo() public static void ReorderToDocumentOrder() { XDocument xDoc = TestData.GetDocumentWithContacts(); - Random rnd = new Random(); - var randomOrderedElements = xDoc.Root.DescendantNodesAndSelf().OrderBy(n => rnd.Next(0, int.MaxValue)); + var randomOrderedElements = xDoc.Root.DescendantNodesAndSelf().Shuffle(); using (var en = randomOrderedElements.InDocumentOrder().GetEnumerator()) { en.MoveNext();