From 68d1ea6f4d1ea82f2c3e9752d8e0e06a16d82eed Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Sat, 23 Apr 2022 11:25:09 +0100 Subject: [PATCH] [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 --- X10D.Tests/src/Collections/EnumerableTests.cs | 9 +-------- X10D.Tests/src/Collections/ListTests.cs | 19 ++----------------- 2 files changed, 3 insertions(+), 25 deletions(-) diff --git a/X10D.Tests/src/Collections/EnumerableTests.cs b/X10D.Tests/src/Collections/EnumerableTests.cs index 5cd9dc0..40cb35e 100644 --- a/X10D.Tests/src/Collections/EnumerableTests.cs +++ b/X10D.Tests/src/Collections/EnumerableTests.cs @@ -9,14 +9,7 @@ public class EnumerableTests [TestMethod] public void EnumerableShuffledShouldBeDifferentOrder() { - var list = new List - { - 1, - 2, - 3, - 4, - 5 - }; + var list = new List(Enumerable.Range(1, 52)); // 52! chance of being shuffled to the same order var shuffled = new List(list); CollectionAssert.AreEqual(list, shuffled); diff --git a/X10D.Tests/src/Collections/ListTests.cs b/X10D.Tests/src/Collections/ListTests.cs index 135a485..53d5906 100644 --- a/X10D.Tests/src/Collections/ListTests.cs +++ b/X10D.Tests/src/Collections/ListTests.cs @@ -9,14 +9,7 @@ public class ListTests [TestMethod] public void ListShuffleShouldReorder() { - var list = new List - { - 1, - 2, - 3, - 4, - 5 - }; + var list = new List(Enumerable.Range(1, 52)); // 52! chance of being shuffled to the same order var shuffled = new List(list); CollectionAssert.AreEqual(list, shuffled); @@ -29,15 +22,7 @@ public class ListTests [TestMethod] public void ListRandomShouldReturnExistingObject() { - var list = new List - { - 1, - 2, - 3, - 4, - 5 - }; - + var list = new List(Enumerable.Range(1, 52)); // 52! chance of being shuffled to the same order int random = list.Random(); Assert.IsTrue(list.Contains(random));