Skip to content

Commit 74d40dd

Browse files
committed
Added collection shuffle extension method
1 parent 5466ed3 commit 74d40dd

2 files changed

Lines changed: 67 additions & 15 deletions

File tree

src/MADE.Collections/CollectionExtensions.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,5 +350,16 @@ public static int PotentialIndexOf<T>(this IList<T> source, T value, Func<T, T,
350350

351351
return result;
352352
}
353+
354+
/// <summary>
355+
/// Shuffles the elements of a sequence randomly.
356+
/// </summary>
357+
/// <param name="source">The collection to shuffle.</param>
358+
/// <typeparam name="T">The type of item in the collection.</typeparam>
359+
/// <returns>The shuffled collection of items.</returns>
360+
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source)
361+
{
362+
return source.OrderBy(x => Guid.NewGuid());
363+
}
353364
}
354365
}

tests/MADE.Collections.Tests/Tests/CollectionExtensionsTests.cs

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,57 @@ namespace MADE.Collections.Tests.Tests
77
using System.Linq;
88
using MADE.Collections.Tests.Fakes;
99
using MADE.Testing;
10-
1110
using NUnit.Framework;
12-
1311
using Shouldly;
1412
using CollectionExtensions = Collections.CollectionExtensions;
1513

1614
[ExcludeFromCodeCoverage]
1715
[TestFixture]
1816
public class CollectionExtensionsTests
1917
{
18+
public class WhenShufflingItems
19+
{
20+
[Test]
21+
public void ShouldShuffleItemOrderRandomly()
22+
{
23+
// Arrange
24+
var items = new List<int>
25+
{
26+
1,
27+
2,
28+
3,
29+
4,
30+
5
31+
};
32+
33+
// Act
34+
var shuffledItems = items.Shuffle();
35+
36+
// Assert
37+
shuffledItems.ShouldNotBeSameAs(items);
38+
}
39+
40+
[Test]
41+
public void ShouldContainSameItemsAfterShuffle()
42+
{
43+
// Arrange
44+
var items = new List<int>
45+
{
46+
1,
47+
2,
48+
3,
49+
4,
50+
5
51+
};
52+
53+
// Act
54+
var shuffledItems = items.Shuffle();
55+
56+
// Assert
57+
shuffledItems.ShouldBeEquivalentTo(items);
58+
}
59+
}
60+
2061
public class WhenUpdatingACollectionItem
2162
{
2263
[Test]
@@ -34,7 +75,7 @@ public void ShouldThrowArgumentNullExceptionIfNullCollection()
3475
public void ShouldThrowArgumentNullExceptionIfNullItem()
3576
{
3677
// Arrange
37-
var list = new List<string> { "Hello" };
78+
var list = new List<string> {"Hello"};
3879
string item = null;
3980

4081
// Act & Assert
@@ -48,7 +89,7 @@ public void ShouldReturnTrueIfItemUpdated()
4889
TestObject objectToAdd = TestObjectFaker.Create().Generate();
4990
TestObject objectToUpdateWith = TestObjectFaker.Create().Generate();
5091

51-
var list = new List<TestObject> { objectToAdd };
92+
var list = new List<TestObject> {objectToAdd};
5293

5394
// Act
5495
bool updated = list.Update(objectToUpdateWith, (s, i) => s.Name == objectToAdd.Name);
@@ -64,7 +105,7 @@ public void ShouldReturnFalseIfItemToUpdateDoesNotExist()
64105
TestObject objectToAdd = TestObjectFaker.Create().Generate();
65106
TestObject objectToUpdateWith = TestObjectFaker.Create().Generate();
66107

67-
var list = new List<TestObject> { objectToAdd };
108+
var list = new List<TestObject> {objectToAdd};
68109

69110
// Act
70111
bool updated = list.Update(objectToUpdateWith, (s, i) => s.Name == objectToUpdateWith.Name);
@@ -90,7 +131,7 @@ public void ShouldThrowArgumentNullExceptionIfNullCollection()
90131
public void ShouldThrowArgumentNullExceptionIfNullSource()
91132
{
92133
// Arrange
93-
var list = new List<string> { "Hello" };
134+
var list = new List<string> {"Hello"};
94135

95136
// Act & Assert
96137
Assert.Throws<ArgumentNullException>(() => list.MakeEqualTo(null));
@@ -100,8 +141,8 @@ public void ShouldThrowArgumentNullExceptionIfNullSource()
100141
public void ShouldUpdateCollectionToBeEqualOther()
101142
{
102143
// Arrange
103-
var list = new List<string> { "Hello" };
104-
var update = new List<string> { "New", "List" };
144+
var list = new List<string> {"Hello"};
145+
var update = new List<string> {"New", "List"};
105146

106147
// Act
107148
list.MakeEqualTo(update);
@@ -170,17 +211,17 @@ public void ShouldReturnFalseForInvalidCases(Collection<int> expected, Collectio
170211

171212
private static object[] ValidCases =
172213
{
173-
new object[] { (Collection<int>)null, (Collection<int>)null },
174-
new object[] { new ObservableCollection<int> { 1, 2, 3 }, new ObservableCollection<int> { 1, 2, 3 } },
175-
new object[] { new ObservableCollection<int> { 1, 2, 3 }, new ObservableCollection<int> { 3, 2, 1 } },
214+
new object[] {(Collection<int>)null, (Collection<int>)null},
215+
new object[] {new ObservableCollection<int> {1, 2, 3}, new ObservableCollection<int> {1, 2, 3}},
216+
new object[] {new ObservableCollection<int> {1, 2, 3}, new ObservableCollection<int> {3, 2, 1}},
176217
};
177218

178219
private static object[] InvalidCases =
179220
{
180-
new object[] { (Collection<int>)null, new ObservableCollection<int>() },
181-
new object[] { new ObservableCollection<int>(), (Collection<int>)null },
182-
new object[] { new ObservableCollection<int> { 1, 2, 3 }, new ObservableCollection<int> { 4, 5, 6 } },
183-
new object[] { new ObservableCollection<int> { 1, 2, 3 }, new ObservableCollection<int> { 1, 2, 3, 4 } },
221+
new object[] {(Collection<int>)null, new ObservableCollection<int>()},
222+
new object[] {new ObservableCollection<int>(), (Collection<int>)null},
223+
new object[] {new ObservableCollection<int> {1, 2, 3}, new ObservableCollection<int> {4, 5, 6}},
224+
new object[] {new ObservableCollection<int> {1, 2, 3}, new ObservableCollection<int> {1, 2, 3, 4}},
184225
};
185226
}
186227
}

0 commit comments

Comments
 (0)