[ci skip] Reduce chance of Shuffle tests failing

A list containing 5 elements has a 1/(5!) (1/120) chance of being shuffled to the same order. This was causing unit tests to fail locally by pure fluke. Tests now generate a list from 1-52, which has a 1/(52!) chance of shuffling to the same order, so the chance is basically 0. If these tests ran once per second, it would take 1.9x10^50 * age of the universe to hit a collision.

This still can happen. Randomness does not guarantee we won't hit the same order. The odds are just so astronomically small that it's not worth considering. And if the CI run fails due to these tests failing, just re-run them anyway and call it a day
This commit is contained in:
Oliver Booth 2022-04-23 11:25:09 +01:00
parent 224e465915
commit 68d1ea6f4d
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
2 changed files with 3 additions and 25 deletions

View File

@ -9,14 +9,7 @@ public class EnumerableTests
[TestMethod] [TestMethod]
public void EnumerableShuffledShouldBeDifferentOrder() public void EnumerableShuffledShouldBeDifferentOrder()
{ {
var list = new List<int> var list = new List<int>(Enumerable.Range(1, 52)); // 52! chance of being shuffled to the same order
{
1,
2,
3,
4,
5
};
var shuffled = new List<int>(list); var shuffled = new List<int>(list);
CollectionAssert.AreEqual(list, shuffled); CollectionAssert.AreEqual(list, shuffled);

View File

@ -9,14 +9,7 @@ public class ListTests
[TestMethod] [TestMethod]
public void ListShuffleShouldReorder() public void ListShuffleShouldReorder()
{ {
var list = new List<int> var list = new List<int>(Enumerable.Range(1, 52)); // 52! chance of being shuffled to the same order
{
1,
2,
3,
4,
5
};
var shuffled = new List<int>(list); var shuffled = new List<int>(list);
CollectionAssert.AreEqual(list, shuffled); CollectionAssert.AreEqual(list, shuffled);
@ -29,15 +22,7 @@ public class ListTests
[TestMethod] [TestMethod]
public void ListRandomShouldReturnExistingObject() public void ListRandomShouldReturnExistingObject()
{ {
var list = new List<int> var list = new List<int>(Enumerable.Range(1, 52)); // 52! chance of being shuffled to the same order
{
1,
2,
3,
4,
5
};
int random = list.Random(); int random = list.Random();
Assert.IsTrue(list.Contains(random)); Assert.IsTrue(list.Contains(random));