From ab084fd9502955b74f0329ef39bbf2ce888f4867 Mon Sep 17 00:00:00 2001 From: maliming Date: Thu, 17 Sep 2026 09:43:34 +0800 Subject: [PATCH] Drop the public Shuffle extension and add RandomHelper tests --- .../Generic/AbpEnumerableExtensions.cs | 22 +--- .../Volo.Abp.Core/Volo/Abp/RandomHelper.cs | 21 ++-- .../Volo/Abp/RandomHelper_Tests.cs | 113 ++++++++++++++++++ 3 files changed, 127 insertions(+), 29 deletions(-) create mode 100644 framework/test/Volo.Abp.Core.Tests/Volo/Abp/RandomHelper_Tests.cs diff --git a/framework/src/Volo.Abp.Core/System/Collections/Generic/AbpEnumerableExtensions.cs b/framework/src/Volo.Abp.Core/System/Collections/Generic/AbpEnumerableExtensions.cs index 7796989bcf..dc177afbd1 100644 --- a/framework/src/Volo.Abp.Core/System/Collections/Generic/AbpEnumerableExtensions.cs +++ b/framework/src/Volo.Abp.Core/System/Collections/Generic/AbpEnumerableExtensions.cs @@ -2,7 +2,7 @@ namespace System.Collections.Generic; -/// +/// /// Extension methods for . /// public static class AbpEnumerableExtensions @@ -59,24 +59,4 @@ public static class AbpEnumerableExtensions ? source.Where(predicate) : source; } - - /// - /// Shuffles the given using Fisher-Yates algorithm. - /// - /// The source to shuffle - /// Random number generator - /// Shuffled enumerable - public static IEnumerable Shuffle(this IEnumerable 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]; - } - } } diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/RandomHelper.cs b/framework/src/Volo.Abp.Core/Volo/Abp/RandomHelper.cs index 86100d5d3c..01fc02346c 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/RandomHelper.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/RandomHelper.cs @@ -11,7 +11,9 @@ namespace Volo.Abp; /// public static class RandomHelper { - private readonly static Random Rnd = new Random(); +#if NETSTANDARD2_0 || NETSTANDARD2_1 + private static readonly Random Rnd = new Random(); +#endif /// /// Returns a random number within a specified range. @@ -19,8 +21,8 @@ public static class RandomHelper /// The inclusive lower bound of the random number returned. /// The exclusive upper bound of the random number returned. maxValue must be greater than or equal to minValue. /// - /// 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. /// public static int GetRandom(int minValue, int maxValue) @@ -40,8 +42,8 @@ public static class RandomHelper /// /// The exclusive upper bound of the random number to be generated. maxValue must be greater than or equal to zero. /// - /// 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. /// public static int GetRandom(int maxValue) @@ -103,15 +105,18 @@ public static class RandomHelper /// items public static List GenerateRandomizedList([NotNull] IEnumerable 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(); } } diff --git a/framework/test/Volo.Abp.Core.Tests/Volo/Abp/RandomHelper_Tests.cs b/framework/test/Volo.Abp.Core.Tests/Volo/Abp/RandomHelper_Tests.cs new file mode 100644 index 0000000000..b6e7bd94c2 --- /dev/null +++ b/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(() => RandomHelper.GetRandom(-1)).ParamName.ShouldBe("maxValue"); + Assert.Throws(() => RandomHelper.GetRandom(10, 5)).ParamName.ShouldBe("minValue"); + } + + [Fact] + public void GenerateRandomizedList_Should_Keep_All_Items() + { + var items = new List { 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 { 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()).ShouldBeEmpty(); + } + + [Fact] + public void GenerateRandomizedList_Should_Work_With_Single_Item() + { + RandomHelper.GenerateRandomizedList(new List { 42 }).ShouldBe(new List { 42 }); + } + + [Fact] + public void GenerateRandomizedList_Should_Not_Change_The_Given_Items() + { + var items = new List { 1, 2, 3, 4, 5, 6, 7, 8 }; + + RandomHelper.GenerateRandomizedList(items); + + items.ShouldBe(new List { 1, 2, 3, 4, 5, 6, 7, 8 }); + } + + [Fact] + public void GenerateRandomizedList_Should_Enumerate_The_Given_Items_Once() + { + var enumerationCount = 0; + + IEnumerable 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(() => RandomHelper.GenerateRandomizedList(null!)) + .ParamName.ShouldBe("items"); + } +}