//[MethodName_StateUnderTest_ExpectedBehavior] public void Sort_TimeTable_IsNotStable() { IEnumerable <TimeTable> sortedSample = TimeTable.GetTestSample(); TimeTable[] stableBySelectionSortSample = TimeTable.GetTestSample().ToArray(); //rearrange an array of objects in uniformly random order Knuth.Shuffle <TimeTable>(stableBySelectionSortSample); //sort by TIME Selection.Sort(stableBySelectionSortSample, new TimeTableComparerByTime()); //City: Chicago, Time:09:00:00 //City: Phoenix, Time:09:00:03 //City: Houston, Time:09:00:13 //City: Chicago, Time:09:00:59 //City: Houston, Time:09:01:10 //City: Chicago, Time:09:03:13 //City: Seattle, Time:09:10:11 //City: Seattle, Time:09:10:25 //City: Phoenix, Time:09:14:25 //City: Chicago, Time:09:19:32 //City: Chicago, Time:09:19:46 //City: Chicago, Time:09:21:05 //City: Seattle, Time:09:22:43 //City: Seattle, Time:09:22:54 //City: Chicago, Time:09:25:52 //City: Chicago, Time:09:35:21 //City: Seattle, Time:09:36:14 //City: Phoenix, Time:09:37:44 //sort by CITY Selection.Sort(stableBySelectionSortSample, new TimeTableComparerByCity()); //algorithms is STABLE if sorted by time is preserved for the same city //City: Chicago, Time:09:00:00 //City: Chicago, Time:09:00:59 //City: Chicago, Time:09:03:13 //City: Chicago, Time:09:19:32 //City: Chicago, Time:09:19:46 //City: Chicago, Time:09:21:05 //City: Chicago, Time:09:25:52 //City: Chicago, Time:09:35:21 //City: Houston, Time:09:00:13 //City: Houston, Time:09:01:10 //City: Phoenix, Time:09:00:03 //City: Phoenix, Time:09:14:25 //City: Phoenix, Time:09:37:44 //City: Seattle, Time:09:10:11 //City: Seattle, Time:09:10:25 //City: Seattle, Time:09:22:43 //City: Seattle, Time:09:22:54 //City: Seattle, Time:09:36:14 CollectionAssert.AreNotEqual(sortedSample, stableBySelectionSortSample); CollectionAssert.AreEquivalent(sortedSample, stableBySelectionSortSample); }
public void Random() { var notExpected = new int[5] { 1, 2, 3, 4, 5 }; for (int i = 0; i < 1000; i++) { var a = new int[5] { 1, 2, 3, 4, 5 }; Knuth.Shuffle(a); CollectionAssert.AreNotEqual(a, notExpected); } }
public void Run() { Console.WriteLine("Choose file:"); // Prompt Console.WriteLine("1 - cards.txt"); // Prompt Console.WriteLine("2 - cardsUnicode.txt"); // Prompt Console.WriteLine("or quit"); // Prompt var fileNumber = Console.ReadLine(); var fieName = string.Empty; switch (fileNumber) { case "1": fieName = "cards.txt"; break; case "2": fieName = "cardsUnicode.txt"; break; case "quit": return; default: return; } var @in = new In(string.Format("Files\\Shuffle\\{0}", fieName)); var cards = @in.ReadAllStrings(); // shuffle the array Knuth.Shuffle(cards.Cast <object>().ToArray()); // print results. foreach (var card in cards) { Console.WriteLine(card); } Console.ReadLine(); }