public void InsertionSortNawArray_InsertionSortGeinverteerd_SortedArray_ShouldNotSetAnyNewIndexes() { // Arrange NAW[] testSet; var expectedLength = 10; var array = ArrayExtensions.InitializeTestSubject(new NawArrayUnordered(expectedLength), expectedLength, out testSet, orderAscending: true); var newNaw = RandomNawGenerator.New(); // Act Alg1_Practicum_Utils.Globals.Alg1NawArrayMethodCalled = false; array.InsertionSortInverted(); // Assert Assert.IsFalse(Alg1_Practicum_Utils.Globals.Alg1NawArrayMethodCalled, "\n\nEr wordt een methode van Alg1NawArray gebruikt!\n\n"); Assert.IsTrue(array.CheckIsGesorteerd()); Assert.IsTrue(Logger.Instance.LogItems.Where(li => li.ArrayAction == ArrayAction.GET).Count() >= expectedLength); Assert.AreEqual(0, Logger.Instance.LogItems.Where(li => li.ArrayAction == ArrayAction.SET && li.NewNaw1 != li.OldNaw1).Count()); }
public void NawArrayUnordered_SelectionSort_NineOnFront_WholeArrayShifted() { // Arrange NAW[] testSet; var expectedLength = 10; var array = ArrayExtensions.InitializeTestSubject(new NawArrayUnordered(expectedLength), expectedLength, out testSet); var newNaw = RandomNawGenerator.New(); testSet[0].Woonplaats = "9"; testSet[1].Woonplaats = "0"; testSet[2].Woonplaats = "1"; testSet[3].Woonplaats = "2"; testSet[4].Woonplaats = "3"; testSet[5].Woonplaats = "4"; testSet[6].Woonplaats = "5"; testSet[7].Woonplaats = "6"; testSet[8].Woonplaats = "7"; testSet[9].Woonplaats = "8"; // Act array.SelectionSort(); // Assert var realSwaps = Logger.Instance.RealSwaps.ToList(); var logItems = Logger.Instance.LogItems.ToList(); Assert.IsTrue(array.CheckIsGesorteerd(), "De array is niet gesorteerd na de SelectionSort"); Assert.AreEqual(expectedLength - 1, realSwaps.Count, "Het laatste item staat vooraan, de rest staat juist. We verwachten dat je het totale aantal -1 nodig hebt om de rest voor de laatste te zetten."); for (int i = 0; i < expectedLength - 1; i++) { Assert.AreEqual(i, realSwaps[i].Index1); Assert.AreEqual(i + 1, realSwaps[i].Index2); var indexOfSwap = logItems.IndexOf(realSwaps[i]); // We assert to not touch previous touched items. Assert.IsFalse(logItems.Skip(indexOfSwap + 1).Any(li => li.Index1 <= realSwaps[i].Index1), "Wanneer je een deel van je lijst gesorteerd hebt, moet je de rest laten staan."); } }
public void SelectionSortNawArray_SelectionSort_NineOnFront_WholeArrayShifted() { // Arrange NAW[] testSet; var expectedLength = 10; var array = ArrayExtensions.InitializeTestSubject(new NawArrayUnordered(expectedLength), expectedLength, out testSet); var newNaw = RandomNawGenerator.New(); testSet[0].Woonplaats = "9"; testSet[1].Woonplaats = "0"; testSet[2].Woonplaats = "1"; testSet[3].Woonplaats = "2"; testSet[4].Woonplaats = "3"; testSet[5].Woonplaats = "4"; testSet[6].Woonplaats = "5"; testSet[7].Woonplaats = "6"; testSet[8].Woonplaats = "7"; testSet[9].Woonplaats = "8"; // Act array.SelectionSort(); // Assert var realSwaps = Logger.Instance.RealSwaps.ToList(); var logItems = Logger.Instance.LogItems.ToList(); Assert.IsTrue(array.CheckIsGesorteerd()); Assert.AreEqual(expectedLength - 1, realSwaps.Count); for (int i = 0; i < expectedLength - 1; i++) { Assert.AreEqual(i, realSwaps[i].Index1); Assert.AreEqual(i + 1, realSwaps[i].Index2); var indexOfSwap = logItems.IndexOf(realSwaps[i]); // We assert to not touch previous touched items. Assert.IsFalse(logItems.Skip(indexOfSwap + 1).Any(li => li.Index1 <= realSwaps[i].Index1)); } }
public void NawArrayUnordered_SelectionSort_ShouldStartWithFirst() { // Arrange NAW[] testSet; var expectedLength = 10; var array = ArrayExtensions.InitializeTestSubject(new NawArrayUnordered(expectedLength), expectedLength, out testSet); var newNaw = RandomNawGenerator.New(); testSet[0].Woonplaats = "9"; testSet[1].Woonplaats = "0"; testSet[2].Woonplaats = "1"; testSet[3].Woonplaats = "2"; testSet[4].Woonplaats = "3"; testSet[5].Woonplaats = "4"; testSet[6].Woonplaats = "5"; testSet[7].Woonplaats = "6"; testSet[8].Woonplaats = "7"; testSet[9].Woonplaats = "8"; // Act array.SelectionSort(); // Assert Assert.IsTrue(array.CheckIsGesorteerd(), "De array is niet gesorteerd na de SelectionSort"); var logItems = Logger.Instance.LogItems.ToList(); var realSwaps = Logger.Instance.RealSwaps.ToList(); Assert.AreEqual(expectedLength - 1, realSwaps.Count, "We verwachten dat je alle items voor de 9 moet gaan zetten, daarom heb je 8 swaps nodig."); for (int i = 0; i < realSwaps.Count; i++) { var indexOf = logItems.IndexOf(realSwaps[i]); var range = logItems.Take(indexOf); Assert.AreEqual(i, range.Min(li => li.Index1), "Je hebt niet de kleinste uit de overgebleven set gepakt."); Assert.AreEqual(expectedLength - 1, range.Max(li => li.Index1), "Je hebt niet de kleinste uti de overgebleven set gepakt."); logItems = logItems.Skip(indexOf + 1).ToList(); } }
public void SelectionSortNawArray_SelectionSortGeinverteerd_ShouldStartWithLast() { // Arrange NAW[] testSet; var expectedLength = 10; var array = ArrayExtensions.InitializeTestSubject(new NawArrayUnordered(expectedLength), expectedLength, out testSet); var newNaw = RandomNawGenerator.New(); testSet[0].Woonplaats = "9"; testSet[1].Woonplaats = "0"; testSet[2].Woonplaats = "1"; testSet[3].Woonplaats = "2"; testSet[4].Woonplaats = "3"; testSet[5].Woonplaats = "4"; testSet[6].Woonplaats = "5"; testSet[7].Woonplaats = "6"; testSet[8].Woonplaats = "7"; testSet[9].Woonplaats = "8"; // Act array.SelectionSortInverted(); // Assert Assert.IsTrue(array.CheckIsGesorteerd()); var logItems = Logger.Instance.LogItems.ToList(); var realSwaps = Logger.Instance.RealSwaps.ToList(); Assert.AreEqual(expectedLength - 1, realSwaps.Count); for (int i = 0; i < realSwaps.Count; i++) { var indexOf = logItems.IndexOf(realSwaps[i]); var range = logItems.Take(indexOf); Assert.AreEqual(0, range.Min(li => li.Index1)); Assert.AreEqual(expectedLength - 1 - i, range.Max(li => li.Index1)); logItems = logItems.Skip(indexOf).ToList(); } }
public void NawArrayUnordered_Add_Valid_ShouldAddAtTheEnd() { // Arrange NAW[] testSet; var expectedLength = 5; var array = InitializeTestsubject(10, expectedLength, out testSet); var newNaw = RandomNawGenerator.New(); // Act array.Add(newNaw); // Assert Assert.AreEqual(expectedLength + 1, array.Count); // Niets is overschreven for (var i = 0; i < testSet.Length; i++) { Assert.AreEqual(testSet[i], array.Array[i], "\n\nNawArrayUnordered.Add(): Bij het toevoegen aan de array is item {0} onterecht overschreven.", i); } // Aan het einde toegevoegd Assert.AreEqual(newNaw, array.Array[expectedLength], "\n\nNawArrayUnordered.Add(): Bij het toevoegen aan de array is de nieuwe item niet aan het einde ingevoegd."); }
public void NawArrayUnordered_BubbleSort_SortedArrayDescending_ShouldSetAllItemsButOne() { // Arrange NAW[] testSet; var expectedLength = 100; var array = ArrayExtensions.InitializeTestSubject(new NawArrayUnordered(expectedLength), expectedLength, out testSet, orderDescending: true); var newNaw = RandomNawGenerator.New(); // Act Alg1_Practicum_Utils.Globals.Alg1NawArrayMethodCalled = false; array.BubbleSort(); // Assert Assert.IsFalse(Alg1_Practicum_Utils.Globals.Alg1NawArrayMethodCalled, "\n\nEr wordt een methode van Alg1NawArray gebruikt!\n\n"); Assert.IsTrue(array.CheckIsGesorteerd(), "De array is na de bubbleSort niet geordend. De items staan niet op de juiste volgorde."); var swaps = Logger.Instance.RealSwaps; Assert.AreEqual(expectedLength.SumAllSmallerIncSelf() - expectedLength, swaps.Count(), "Het aantal swaps zou gelijk moeten zijn aan een O(Log(n)), dit is niet het geval."); }
public void NawArrayUnordered_Add_OneTooMany_ShouldThrowException() { // Arrange NAW[] testSet; var array = InitializeTestsubject(3, 3, out testSet); var oneTooMany = RandomNawGenerator.New(); // Act Alg1_Practicum_Utils.Globals.Alg1NawArrayMethodCalled = false; try { array.Add(oneTooMany); } catch (Exception ex) { // Assert Assert.IsInstanceOfType(ex, typeof(NawArrayUnorderedOutOfBoundsException), "\n\nNawArrayUnordered.Add(): Toevoegen van 11e element aan array met omvang van 10 geeft geen exceptie. Controleer je wel of het array nog ruimte heeft ?"); } Assert.IsFalse(Alg1_Practicum_Utils.Globals.Alg1NawArrayMethodCalled, "\n\nEr wordt een methode van Alg1NawArray gebruikt!\n\n"); }
public void NawArrayUnordered_BubbleSort_AllTheSameWoonplaats_IsInOrder() { // Arrange NAW[] testSet; var expectedLength = 10; var array = ArrayExtensions.InitializeTestSubject(new NawArrayUnordered(expectedLength), expectedLength, out testSet); var newNaw = RandomNawGenerator.New(); foreach (var item in testSet) { item.Woonplaats = "Allemaal dezelfde woonplaats"; } // Act Alg1_Practicum_Utils.Globals.Alg1NawArrayMethodCalled = false; array.BubbleSort(); // Assert Assert.IsFalse(Alg1_Practicum_Utils.Globals.Alg1NawArrayMethodCalled, "\n\nEr wordt een methode van Alg1NawArray gebruikt!\n\n"); Assert.IsTrue(array.CheckIsGesorteerd(), "De array is na de bubbleSort niet geordend. De items staan niet op de juiste volgorde."); }
public void InsertionSortableNawArrayUnordered_InsertionSort_AllTheSameWoonplaats_IsInOrder() { // Arrange NAW[] testSet; var expectedLength = 10; var array = ArrayExtensions.InitializeTestSubject(New(expectedLength), expectedLength, out testSet); var newNaw = RandomNawGenerator.New(); foreach (var item in testSet) { item.Woonplaats = "Allemaal dezelfde woonplaats"; } // Act Alg1.Practica.Utils.Globals.Alg1NawArrayMethodCalled = false; array.InsertionSort(); // Assert Assert.IsFalse(Alg1.Practica.Utils.Globals.Alg1NawArrayMethodCalled, "\n\nEr wordt een methode van Alg1NawArray gebruikt!\n\n"); Assert.IsTrue(array.CheckIsGesorteerd(), "De array is niet gesorteerd na de InsertionSort."); }
public void InsertionSortNawArray_InsertionSortGeinverteerd_AllTheSameWoonplaatsAndNaam_IsInOrder() { // Arrange NAW[] testSet; var expectedLength = 10; var array = ArrayExtensions.InitializeTestSubject(new NawArrayUnordered(expectedLength), expectedLength, out testSet); var newNaw = RandomNawGenerator.New(); foreach (var item in testSet) { item.Woonplaats = "Allemaal dezelfde woonplaats"; item.Naam = "Allemaal dezelfde naam"; } // Act Alg1_Practicum_Utils.Globals.Alg1NawArrayMethodCalled = false; array.InsertionSortInverted(); // Assert Assert.IsFalse(Alg1_Practicum_Utils.Globals.Alg1NawArrayMethodCalled, "\n\nEr wordt een methode van Alg1NawArray gebruikt!\n\n"); Assert.IsTrue(array.CheckIsGesorteerd()); }
public void BinarySearchNawArray_BinarySearchPerformance() { // Arrange NAW[] testSet; var expectedLength = 100; var runs = 1000000; var array = ArrayExtensions.InitializeTestSubject(new NawArrayOrdered(expectedLength), expectedLength, out testSet, orderAscending: true); var random = new Random(); // generate NAW that is not in the testset NAW nonExistingNaw; do { nonExistingNaw = RandomNawGenerator.New(); } while (testSet.Contains(nonExistingNaw)); Assert.IsTrue(array.FindBinary(nonExistingNaw) == -1); Stopwatch stopwatch_BR100 = new Stopwatch(); Stopwatch stopwatch_BW100 = new Stopwatch(); // Act stopwatch_BR100.Start(); for (int i = 0; i < runs; i++) { var expectedIndex = random.Next(0, expectedLength); var actualIndex = array.FindBinary(testSet[expectedIndex]); } stopwatch_BR100.Stop(); stopwatch_BW100.Start(); for (int i = 0; i < runs; i++) { var actualIndex = array.FindBinary(nonExistingNaw); } stopwatch_BW100.Stop(); // Re-Arrange var expectedLength2 = 1000; var array2 = ArrayExtensions.InitializeTestSubject(new NawArrayOrdered(expectedLength2), expectedLength2, out testSet, orderAscending: true); // generate NAW that is not in the testset do { nonExistingNaw = RandomNawGenerator.New(); } while (testSet.Contains(nonExistingNaw)); Assert.IsTrue(array.FindBinary(nonExistingNaw) == -1); Stopwatch stopwatch_BR1000 = new Stopwatch(); Stopwatch stopwatch_BW1000 = new Stopwatch(); // Act stopwatch_BR1000.Start(); for (int i = 0; i < runs; i++) { var expectedIndex = random.Next(0, expectedLength2); var actualIndex = array2.FindBinary(testSet[expectedIndex]); } stopwatch_BR1000.Stop(); stopwatch_BW1000.Start(); for (int i = 0; i < runs; i++) { var actualIndex = array2.FindBinary(nonExistingNaw); } stopwatch_BW1000.Stop(); // Re-Arrange var expectedLength3 = 10000; var array3 = ArrayExtensions.InitializeTestSubject(new NawArrayOrdered(expectedLength3), expectedLength3, out testSet, orderAscending: true); // generate NAW that is not in the testset do { nonExistingNaw = RandomNawGenerator.New(); } while (testSet.Contains(nonExistingNaw)); Assert.IsTrue(array3.FindBinary(nonExistingNaw) == -1); Stopwatch stopwatch_BR10000 = new Stopwatch(); Stopwatch stopwatch_BW10000 = new Stopwatch(); // Act stopwatch_BR10000.Start(); for (int i = 0; i < runs; i++) { var expectedIndex = random.Next(0, expectedLength3); var actualIndex = array3.FindBinary(testSet[expectedIndex]); } stopwatch_BR10000.Stop(); stopwatch_BW10000.Start(); for (int i = 0; i < runs; i++) { var actualIndex = array3.FindBinary(nonExistingNaw); } stopwatch_BW10000.Stop(); // Assert // Assert.AreEqual(expectedIndex, actualIndex); Console.WriteLine("\nPerformed {0} random Binary searches in Array with length {1}", runs, expectedLength); PrintOutput(expectedLength, stopwatch_BR100); Console.WriteLine("\nPerformed {0} random Binary searches in Array with length {1}", runs, expectedLength2); PrintOutput(expectedLength2, stopwatch_BR1000); Console.WriteLine("\nPerformed {0} random Binary searches in Array with length {1}", runs, expectedLength3); PrintOutput(expectedLength3, stopwatch_BR10000); Console.WriteLine("\nPerformed {0} Binary searches on non existing NAW in Array with length {1}", runs, expectedLength); PrintOutput(expectedLength, stopwatch_BW100); Console.WriteLine("\nPerformed {0} Binary searches on non existing NAW in Array with length {1}", runs, expectedLength2); PrintOutput(expectedLength2, stopwatch_BW1000); Console.WriteLine("\nPerformed {0} Binary searches on non existing NAW in Array with length {1}", runs, expectedLength3); PrintOutput(expectedLength3, stopwatch_BW10000); }
public void NawArrayUnordered_InsertionSort_EightAndFourSwapped_ShouldHaveRightBounds() { // Arrange NAW[] testSet; var expectedLength = 10; var array = ArrayExtensions.InitializeTestSubject(new NawArrayUnordered(expectedLength), expectedLength, out testSet); var newNaw = RandomNawGenerator.New(); testSet[0].Woonplaats = "0"; testSet[1].Woonplaats = "1"; testSet[2].Woonplaats = "2"; testSet[3].Woonplaats = "3"; testSet[4].Woonplaats = "8"; testSet[5].Woonplaats = "5"; testSet[6].Woonplaats = "6"; testSet[7].Woonplaats = "7"; testSet[8].Woonplaats = "4"; testSet[9].Woonplaats = "9"; // Act Alg1_Practicum_Utils.Globals.Alg1NawArrayMethodCalled = false; array.InsertionSort(); // Assert Assert.IsFalse(Alg1_Practicum_Utils.Globals.Alg1NawArrayMethodCalled, "\n\nEr wordt een methode van Alg1NawArray gebruikt!\n\n"); // Logger.Instance.Print(); Assert.IsTrue(array.CheckIsGesorteerd(), "De array is niet gesorteerd na de InsertionSort."); int lowerBound = 4, upperBound = 8; NAW toMoveUp = testSet[lowerBound]; var toMoveUpSetters = (from li in Logger.Instance.LogItems where li.NewNaw1 == toMoveUp && li.ArrayAction == ArrayAction.SET && li.NewNaw1 != li.OldNaw1 // This will be checked in another test orderby li.Index1 select li).ToArray(); // This one bubbles up slowly. for (int i = lowerBound + 1; i <= upperBound; i++) { Assert.IsTrue(toMoveUpSetters[i - lowerBound - 1].Index1 == i, "Er zouden 4 items verplaatst moeten zijn om ruimte te maken in het geordende deel."); } NAW toBeInsertedDown = testSet[upperBound]; var toBeInsertedDownSetters = (from li in Logger.Instance.LogItems where li.NewNaw1 == toBeInsertedDown && li.ArrayAction == ArrayAction.SET && li.NewNaw1 != li.OldNaw1 // This will be checked in another test orderby li.Index1 select li).ToArray(); // This one moves down in an insertion way. Assert.AreEqual(1, toBeInsertedDownSetters.Count(), "Meerdere items zijn geïnsert in de sortering, dit had er 1 moeten zijn."); Assert.AreEqual(lowerBound, toBeInsertedDownSetters.First().Index1, "Item met woonplaats 4 zou op de 4e index geïnsert moeten zijn."); // All setters must be in between the bounds. Assert.IsTrue(Logger.Instance.LogItems .Where(li => li.ArrayAction == ArrayAction.SET && li.NewNaw1 != li.OldNaw1) .All(li => li.Index1 >= lowerBound && li.Index1 <= upperBound), "Je hebt items verplaatst die niet verplaatst hadden hoeven worden omdat ze al goed stonden."); }
public void InsertionSortNawArray_InsertionSortGeinverteerd_EightAndFourSwapped_ShouldNotSetWhenNotSwapped() { // Arrange NAW[] testSet; var expectedLength = 10; var array = ArrayExtensions.InitializeTestSubject(new NawArrayUnordered(expectedLength), expectedLength, out testSet); var newNaw = RandomNawGenerator.New(); testSet[0].Woonplaats = "0"; testSet[1].Woonplaats = "1"; testSet[2].Woonplaats = "2"; testSet[3].Woonplaats = "3"; testSet[4].Woonplaats = "8"; testSet[5].Woonplaats = "5"; testSet[6].Woonplaats = "6"; testSet[7].Woonplaats = "7"; testSet[8].Woonplaats = "4"; testSet[9].Woonplaats = "9"; // Act Alg1_Practicum_Utils.Globals.Alg1NawArrayMethodCalled = false; array.InsertionSortInverted(); Logger.Instance.Print(); // Assert Assert.IsFalse(Alg1_Practicum_Utils.Globals.Alg1NawArrayMethodCalled, "\n\nEr wordt een methode van Alg1NawArray gebruikt!\n\n"); Assert.IsTrue(array.CheckIsGesorteerd()); int lowerBound = 4, upperBound = 8; NAW toMoveDown = testSet[upperBound]; var toMoveDownSetters = (from li in Logger.Instance.LogItems where li.NewNaw1 == toMoveDown && li.ArrayAction == ArrayAction.SET orderby li.Index1 descending select li).ToArray(); // This one bubbles Down slowly. for (int i = upperBound - 1; i >= lowerBound; i--) { Assert.IsTrue(toMoveDownSetters[upperBound - i - 1].Index1 == i); } NAW toBeInsertedUp = testSet[lowerBound]; var toBeInsertedUpSetters = (from li in Logger.Instance.LogItems where li.NewNaw1 == toBeInsertedUp && li.ArrayAction == ArrayAction.SET && li.NewNaw1 != li.OldNaw1 // This will be checked in another test orderby li.Index1 select li).ToArray(); // This one moves Up in an insertion way. Assert.AreEqual(1, toBeInsertedUpSetters.Count()); Assert.AreEqual(upperBound, toBeInsertedUpSetters.First().Index1); // All setters must be in between the bounds. Assert.IsTrue(Logger.Instance.LogItems .Where(li => li.ArrayAction == ArrayAction.SET && li.NewNaw1 != li.OldNaw1) .All(li => li.Index1 >= lowerBound && li.Index1 <= upperBound)); }