public static char TestRemove() { char result = 'q'; string strRemoved = ""; string test = "Remove() Test"; string testDescription = "This test randomly selects a number from the list and calls Remove(). The method will remove the first occurence of the selected number by replacing it with the last number and shortening the list."; UnorderedArrayList list = new UnorderedArrayList(); list.InsertValues(initializeList()); int num = GetRandomNumber(); int valueSelected = list.GetValueAtLocation(num); int locationOfSelected = list.GetFirstPositionOfValue(valueSelected); int[] allLocationsOfSelected = { locationOfSelected }; int valueAtEnd = list.GetValueAtLocation(list.GetLength() - 1); strRemoved = locationOfSelected.ToString(); list.Remove(valueSelected); ReportRemoveTestsToConsole(test, testDescription, valueSelected.ToString(), allLocationsOfSelected, list); Console.Write("\nPress 'r' to Repeat, 'c' to Continue, or 'q' to Quit: "); result = Convert.ToChar(Console.ReadKey().KeyChar); if (result == 'r') { TestRemove(); } return(result); }
public static void ReportMinMaxTestsToConsole(string nameOfTest, string description, int maxValueReported) { WriteTestReportHeader(nameOfTest, description); UnorderedArrayList l = new UnorderedArrayList(); l.InsertValues(initializeList()); l.Print(); Console.WriteLine(); Console.WriteLine("{0} Reported: {1}", nameOfTest, maxValueReported.ToString()); Console.Write("Sorted List: "); l.sort(); l.Print(); int v = 0; if (nameOfTest == "Max() Test") { v = l.GetValueAtLocation(l.GetLength() - 1); } else { v = l.GetValueAtLocation(v); } Console.WriteLine(); Console.WriteLine("{0} Reported {1}. This matches {2} from the sorted list above.", nameOfTest, maxValueReported.ToString(), v.ToString()); }
public static void ReportRemoveTestsToConsole(string nameOfTest, string description, string valSelected, int[] locSelected, UnorderedArrayList resultList) { WriteTestReportHeader(nameOfTest, description); UnorderedArrayList list = new UnorderedArrayList(); list.InsertValues(initializeList()); list.Print(); Console.WriteLine("\nThe value {0} was randomly selected.", valSelected); string strSelections = ""; int strCounter = 0; foreach (int s in locSelected) { if (strCounter == locSelected.Length - 1) { strSelections += (s + 1).ToString(); strCounter++; break; } else { strSelections += (s + 1).ToString() + ", "; strCounter++; } } Console.WriteLine("The value {0} was found at position(s): {1} ", valSelected, strSelections); int countRemoval = 0; for (int i = 0; i < locSelected.Length; i++) { if (locSelected[i].Equals(list.GetLength() - 1)) { Console.WriteLine("The value {0} is at the end of the list and cannot be moved", valSelected); } else { Console.WriteLine("The method will replace the value {0} at the {1} position with {2} from the end.", valSelected, (locSelected[i] + 1).ToString(), list.GetValueAtLocation(list.GetLength() - (i + 1)).ToString()); } countRemoval = i + 1; } Console.WriteLine(); Console.WriteLine("The method peformed a total of {0} replacement(s).\n", countRemoval.ToString()); Console.WriteLine(); Console.Write("Original List:\t "); list.Print(); Console.Write("Updated List:\t "); resultList.Print(); }