Browse Source

Drop the public Shuffle extension and add RandomHelper tests

pull/21149/head
maliming 2 months ago
parent
commit
ab084fd950
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 22
      framework/src/Volo.Abp.Core/System/Collections/Generic/AbpEnumerableExtensions.cs
  2. 21
      framework/src/Volo.Abp.Core/Volo/Abp/RandomHelper.cs
  3. 113
      framework/test/Volo.Abp.Core.Tests/Volo/Abp/RandomHelper_Tests.cs

22
framework/src/Volo.Abp.Core/System/Collections/Generic/AbpEnumerableExtensions.cs

@ -2,7 +2,7 @@
namespace System.Collections.Generic;
/// <summary>
/// <summary>
/// Extension methods for <see cref="IEnumerable{T}"/>.
/// </summary>
public static class AbpEnumerableExtensions
@ -59,24 +59,4 @@ public static class AbpEnumerableExtensions
? source.Where(predicate)
: source;
}
/// <summary>
/// Shuffles the given <paramref name="source"/> using Fisher-Yates algorithm.
/// </summary>
/// <param name="source">The source to shuffle</param>
/// <param name="rng">Random number generator</param>
/// <returns>Shuffled enumerable</returns>
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source, Random rng)
{
var elements = source.ToArray();
for (var i = elements.Length - 1; i >= 0; i--)
{
// Swap element "i" with a random earlier element it (or itself)
// ... except we don't really need to swap it fully, as we can
// return it immediately, and afterwards it's irrelevant.
var swapIndex = rng.Next(i + 1);
yield return elements[swapIndex];
elements[swapIndex] = elements[i];
}
}
}

21
framework/src/Volo.Abp.Core/Volo/Abp/RandomHelper.cs

@ -11,7 +11,9 @@ namespace Volo.Abp;
/// </summary>
public static class RandomHelper
{
private readonly static Random Rnd = new Random();
#if NETSTANDARD2_0 || NETSTANDARD2_1
private static readonly Random Rnd = new Random();
#endif
/// <summary>
/// Returns a random number within a specified range.
@ -19,8 +21,8 @@ public static class RandomHelper
/// <param name="minValue">The inclusive lower bound of the random number returned.</param>
/// <param name="maxValue">The exclusive upper bound of the random number returned. maxValue must be greater than or equal to minValue.</param>
/// <returns>
/// A 32-bit signed integer greater than or equal to minValue and less than maxValue;
/// that is, the range of return values includes minValue but not maxValue.
/// A 32-bit signed integer greater than or equal to minValue and less than maxValue;
/// that is, the range of return values includes minValue but not maxValue.
/// If minValue equals maxValue, minValue is returned.
/// </returns>
public static int GetRandom(int minValue, int maxValue)
@ -40,8 +42,8 @@ public static class RandomHelper
/// </summary>
/// <param name="maxValue">The exclusive upper bound of the random number to be generated. maxValue must be greater than or equal to zero.</param>
/// <returns>
/// A 32-bit signed integer greater than or equal to zero, and less than maxValue;
/// that is, the range of return values ordinarily includes zero but not maxValue.
/// A 32-bit signed integer greater than or equal to zero, and less than maxValue;
/// that is, the range of return values ordinarily includes zero but not maxValue.
/// However, if maxValue equals zero, maxValue is returned.
/// </returns>
public static int GetRandom(int maxValue)
@ -103,15 +105,18 @@ public static class RandomHelper
/// <param name="items">items</param>
public static List<T> GenerateRandomizedList<T>([NotNull] IEnumerable<T> items)
{
Check.NotNull(items, nameof(items));
var array = items.ToArray();
#if NETSTANDARD2_0 || NETSTANDARD2_1
lock (Rnd)
for (var i = array.Length - 1; i > 0; i--)
{
return array.Shuffle(Rnd).ToList();
var j = GetRandom(i + 1);
(array[i], array[j]) = (array[j], array[i]);
}
#else
Random.Shared.Shuffle(array);
return array.ToList();
#endif
return array.ToList();
}
}

113
framework/test/Volo.Abp.Core.Tests/Volo/Abp/RandomHelper_Tests.cs

@ -0,0 +1,113 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Shouldly;
using Xunit;
namespace Volo.Abp;
public class RandomHelper_Tests
{
[Fact]
public void GetRandom_Should_Return_Value_In_The_Given_Range()
{
for (var i = 0; i < 100; i++)
{
var value = RandomHelper.GetRandom(10, 20);
value.ShouldBeGreaterThanOrEqualTo(10);
value.ShouldBeLessThan(20);
}
}
[Fact]
public void GetRandom_Should_Handle_Empty_Range()
{
RandomHelper.GetRandom(0).ShouldBe(0);
RandomHelper.GetRandom(5, 5).ShouldBe(5);
}
[Fact]
public void GetRandom_Should_Throw_Exception_For_Invalid_Range()
{
Assert.Throws<ArgumentOutOfRangeException>(() => RandomHelper.GetRandom(-1)).ParamName.ShouldBe("maxValue");
Assert.Throws<ArgumentOutOfRangeException>(() => RandomHelper.GetRandom(10, 5)).ParamName.ShouldBe("minValue");
}
[Fact]
public void GenerateRandomizedList_Should_Keep_All_Items()
{
var items = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8 };
var randomizedList = RandomHelper.GenerateRandomizedList(items);
randomizedList.OrderBy(x => x).ShouldBe(items);
}
[Fact]
public void GenerateRandomizedList_Should_Keep_Duplicate_Items()
{
var items = new List<int> { 1, 1, 2, 2, 2, 3 };
var randomizedList = RandomHelper.GenerateRandomizedList(items);
randomizedList.OrderBy(x => x).ShouldBe(items);
}
[Fact]
public void GenerateRandomizedList_Should_Work_With_Empty_Items()
{
RandomHelper.GenerateRandomizedList(new List<int>()).ShouldBeEmpty();
}
[Fact]
public void GenerateRandomizedList_Should_Work_With_Single_Item()
{
RandomHelper.GenerateRandomizedList(new List<int> { 42 }).ShouldBe(new List<int> { 42 });
}
[Fact]
public void GenerateRandomizedList_Should_Not_Change_The_Given_Items()
{
var items = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8 };
RandomHelper.GenerateRandomizedList(items);
items.ShouldBe(new List<int> { 1, 2, 3, 4, 5, 6, 7, 8 });
}
[Fact]
public void GenerateRandomizedList_Should_Enumerate_The_Given_Items_Once()
{
var enumerationCount = 0;
IEnumerable<int> GetItems()
{
enumerationCount++;
yield return 1;
yield return 2;
yield return 3;
}
RandomHelper.GenerateRandomizedList(GetItems());
enumerationCount.ShouldBe(1);
}
[Fact]
public void GenerateRandomizedList_Should_Change_The_Order_Of_Items()
{
var items = Enumerable.Range(0, 32).ToList();
var randomizedList = RandomHelper.GenerateRandomizedList(items);
randomizedList.SequenceEqual(items).ShouldBeFalse();
}
[Fact]
public void GenerateRandomizedList_Should_Throw_Exception_For_Null_Items()
{
Assert
.Throws<ArgumentNullException>(() => RandomHelper.GenerateRandomizedList<int>(null!))
.ParamName.ShouldBe("items");
}
}
Loading…
Cancel
Save