From 268fce8775e36d45ee00e151f4876dec6303cb10 Mon Sep 17 00:00:00 2001 From: Krzysztof Jafra Date: Thu, 12 Jul 2018 11:58:30 +0200 Subject: [PATCH 1/3] List Shuffle extension --- .../IList[T].Shuffle.cs | 33 ++++++++++++++ .../IList[T].Swap.cs | 2 +- src/Z.Collections/Z.Collections.csproj | 1 + .../IList[T].Shuffle.cs | 43 +++++++++++++++++++ .../Z.Collections.Test.csproj | 1 + 5 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 src/Z.Collections/System.Collections.Generic.IList[T]/IList[T].Shuffle.cs create mode 100644 test/Z.Collections.Test/System.Collections.Generic.IList[T]/IList[T].Shuffle.cs diff --git a/src/Z.Collections/System.Collections.Generic.IList[T]/IList[T].Shuffle.cs b/src/Z.Collections/System.Collections.Generic.IList[T]/IList[T].Shuffle.cs new file mode 100644 index 00000000..5feb60a5 --- /dev/null +++ b/src/Z.Collections/System.Collections.Generic.IList[T]/IList[T].Shuffle.cs @@ -0,0 +1,33 @@ +// Description: C# Extension Methods Library to enhances the .NET Framework by adding hundreds of new methods. It drastically increases developers productivity and code readability. Support C# and VB.NET +// Website & Documentation: https://github.com/zzzprojects/Z.ExtensionMethods +// Forum: https://github.com/zzzprojects/Z.ExtensionMethods/issues +// License: https://github.com/zzzprojects/Z.ExtensionMethods/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using System; +using System.Collections.Generic; +using System.Linq; + +public static partial class Extensions +{ + static Random _random = new Random(); + + /// + /// An IList<T> extension method that shuffles items in a collection. + /// + /// Generic type parameter. + /// The @this to act on. + public static void Shuffle(this IList @this) + { + + int length = @this.Count; + for (int i = 0; i < length; i++) + { + int randomIndex = i + _random.Next(length - i); + T item = @this.ElementAt(randomIndex); + @this[randomIndex] = @this[i]; + @this[i] = item; + } + } +} \ No newline at end of file diff --git a/src/Z.Collections/System.Collections.Generic.IList[T]/IList[T].Swap.cs b/src/Z.Collections/System.Collections.Generic.IList[T]/IList[T].Swap.cs index 0cd404ba..84bb255b 100644 --- a/src/Z.Collections/System.Collections.Generic.IList[T]/IList[T].Swap.cs +++ b/src/Z.Collections/System.Collections.Generic.IList[T]/IList[T].Swap.cs @@ -10,7 +10,7 @@ public static partial class Extensions { /// - /// An ICollection<T> extension method that swaps item only when it exists in a collection. + /// An IList<T> extension method that swaps item only when it exists in a collection. /// /// Generic type parameter. /// The @this to act on. diff --git a/src/Z.Collections/Z.Collections.csproj b/src/Z.Collections/Z.Collections.csproj index ccd0c9d0..32f462e0 100644 --- a/src/Z.Collections/Z.Collections.csproj +++ b/src/Z.Collections/Z.Collections.csproj @@ -63,6 +63,7 @@ + diff --git a/test/Z.Collections.Test/System.Collections.Generic.IList[T]/IList[T].Shuffle.cs b/test/Z.Collections.Test/System.Collections.Generic.IList[T]/IList[T].Shuffle.cs new file mode 100644 index 00000000..d05e6336 --- /dev/null +++ b/test/Z.Collections.Test/System.Collections.Generic.IList[T]/IList[T].Shuffle.cs @@ -0,0 +1,43 @@ +// Description: C# Extension Methods Library to enhances the .NET Framework by adding hundreds of new methods. It drastically increases developers productivity and code readability. Support C# and VB.NET +// Website & Documentation: https://github.com/zzzprojects/Z.ExtensionMethods +// Forum: https://github.com/zzzprojects/Z.ExtensionMethods/issues +// License: https://github.com/zzzprojects/Z.ExtensionMethods/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. +using System.Collections.Generic; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Z.Collections.Test +{ + [TestClass] + public class System_Collections_Generic_IList_T_Shuffle + { + [TestMethod] + public void Shuffle() + { + // Type + var intList = new List() { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 }; + + // Examples + var example1 = new List(intList); + var example2 = new List(intList); + var example3 = new List(intList); + var example4 = new List(intList); + var example5 = new List(intList); + + //Shuffle and pack results + example1.Shuffle(); + example2.Shuffle(); + example3.Shuffle(); + example4.Shuffle(); + example5.Shuffle(); + + var shuffledResults = new List> {example1, example2, example3, example4, example5}; + + // Unit Test + Assert.IsTrue(!shuffledResults.Contains(intList), "The collection should be shuffled"); + Assert.IsTrue(shuffledResults.Distinct().Count() == shuffledResults.Count, "The shuffled results should be unique"); + } + } +} \ No newline at end of file diff --git a/test/Z.Collections.Test/Z.Collections.Test.csproj b/test/Z.Collections.Test/Z.Collections.Test.csproj index 5cdf1ca3..78b91e22 100644 --- a/test/Z.Collections.Test/Z.Collections.Test.csproj +++ b/test/Z.Collections.Test/Z.Collections.Test.csproj @@ -78,6 +78,7 @@ + From 5e70161817f5a4a2432e77a7c20367b3cfefb9c6 Mon Sep 17 00:00:00 2001 From: Krzysztof Jafra Date: Thu, 12 Jul 2018 12:02:28 +0200 Subject: [PATCH 2/3] small fixes --- .../System.Collections.Generic.IList[T]/IList[T].Shuffle.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Z.Collections/System.Collections.Generic.IList[T]/IList[T].Shuffle.cs b/src/Z.Collections/System.Collections.Generic.IList[T]/IList[T].Shuffle.cs index 5feb60a5..52f22889 100644 --- a/src/Z.Collections/System.Collections.Generic.IList[T]/IList[T].Shuffle.cs +++ b/src/Z.Collections/System.Collections.Generic.IList[T]/IList[T].Shuffle.cs @@ -11,7 +11,7 @@ public static partial class Extensions { - static Random _random = new Random(); + private static readonly Random _random = new Random(); /// /// An IList<T> extension method that shuffles items in a collection. @@ -20,7 +20,6 @@ public static partial class Extensions /// The @this to act on. public static void Shuffle(this IList @this) { - int length = @this.Count; for (int i = 0; i < length; i++) { From f97666b3fede430dfa9a789c116d25b0081c0cb2 Mon Sep 17 00:00:00 2001 From: Krzysztof Jafra Date: Thu, 12 Jul 2018 13:10:53 +0200 Subject: [PATCH 3/3] Add test case --- .../System.Collections.Generic.IList[T]/IList[T].Shuffle.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/Z.Collections.Test/System.Collections.Generic.IList[T]/IList[T].Shuffle.cs b/test/Z.Collections.Test/System.Collections.Generic.IList[T]/IList[T].Shuffle.cs index d05e6336..71cf5225 100644 --- a/test/Z.Collections.Test/System.Collections.Generic.IList[T]/IList[T].Shuffle.cs +++ b/test/Z.Collections.Test/System.Collections.Generic.IList[T]/IList[T].Shuffle.cs @@ -16,7 +16,7 @@ public class System_Collections_Generic_IList_T_Shuffle [TestMethod] public void Shuffle() { - // Type + // Input var intList = new List() { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 }; // Examples @@ -36,7 +36,8 @@ public void Shuffle() var shuffledResults = new List> {example1, example2, example3, example4, example5}; // Unit Test - Assert.IsTrue(!shuffledResults.Contains(intList), "The collection should be shuffled"); + Assert.IsTrue(shuffledResults.All(e=> e.Count() == 20), "The shuffle should't change the collection length"); + Assert.IsFalse(shuffledResults.Contains(intList), "The input collection should not be contained in shuffled results"); Assert.IsTrue(shuffledResults.Distinct().Count() == shuffledResults.Count, "The shuffled results should be unique"); } }