public class A5Tests { /** The pass/fail status of the tests. */ private boolean allTestsPassed = true;
Question:
public class A5Tests {
/** The pass/fail status of the tests. */
private boolean allTestsPassed = true;
/** The List used to test the outcomes. */
private LinkedList movieList;
/**
The starting point for the test program.
@param args command line arguments - ignored in this program
*/
public static void main(String[] args) {
new A5Tests().runTests();
}
/**
Runs the tests of the various LinkedList methods.
*/
private void runTests() {
setup();
// Display the original list
System.out.println("movieList : " + movieList);
// Test all methods
testInsert(); // test various add methods
testReverse(); // test reverse method
testRemove(); // test different remove options
if (allTestsPassed) {
System.out.println("All tests passed!");
}
}
/**
Initializes the LinkedList that will be used in the tests.
*/
private void setup() {
movieList = new LinkedList();
// Add few movies to the LinkedList
movieList.add("Frozen");
movieList.add("The Lion King");
movieList.add("Moana");
movieList.add("Onward");
movieList.add("Toy Story");
movieList.add("Aladdin");
movieList.add("Your Name");
}
/**
Test various add methods.
*/
public void testInsert() {
// Add at the begining of the list
movieList.add(0, "Spider-man");
if (!movieList.get(0).equals("Spider-man")) {
allTestsPassed = false;
System.out.println("Test add at index 0 failed!");
}
// Add movie at the end of the list
movieList.add("Zootopia");
if (!movieList.get(movieList.getSize() - 1).equals("Zootopia")) {
allTestsPassed = false;
System.out.println("Test add at end of list failed!");
}
// Add a movie at the k index
movieList.add(4, "Inside Out");
if (!movieList.get(4).equals("Inside Out")) {
allTestsPassed = false;
System.out.println("Test add at index k failed!");
}
}
/**
Test reverse method.
*/
public void testReverse() {
// Create a reverse of the original list
List reverseList = movieList.duplicateReversed();
// Loop to check if reverse is correct
for (int i = 0; i < movieList.getSize(); i++) {
if (!movieList.get(i).equals(reverseList.get(reverseList.getSize() - (i + 1)))) {
allTestsPassed = false;
System.out.println("Test of the duplicateReversed() method failed!");
}
}
}
/**
Test remove methods.
*/
public void testRemove() {
// Create a duplicate list
List duplicateList = movieList.duplicate();
// Remove from the begining of the list
duplicateList.remove(0);
if (!duplicateList.get(0).equals(movieList.get(1))) {
allTestsPassed = false;
System.out.println("Test remove at index 0 failed!");
}
// Remove movie at the end of the list
duplicateList.remove(duplicateList.getSize() - 1);
if (!duplicateList.get(duplicateList.getSize() - 1).equals(movieList.get(movieList.getSize() - 2))) {
allTestsPassed = false;
System.out.println("Test remove at end of the list failed!");
}
// Remove movie based on movie title
duplicateList.remove("Inside Out");
if (duplicateList.indexOf("Inside Out") != -1) {
allTestsPassed = false;
System.out.println("Test remove based on title failed!");
}
}
}
***************************************************************************************************
public interface List {
/**
Return true if the list is empty; false otherwise.
@return true if the list is empty; false otherwise
*/
boolean isEmpty();
/**
Returns the number of items in the list.
@return the number of items in the list
*/
int getSize();
/**
Adds an item to the end of the list.
precondition: none
postcondition: item is added at the end of the list
@param item the element to add to the end of the list
*/
void add(E item);
/**
Adds an item to the list at the given index.
precondition: none
postcondition: item is added at the given index
@param index the position at which to add the item
@param item the element to add to the list
@throws IndexOutOfBoundsException if index is out of bounds
*/
void add(int index, E item);
/**
Removes the item from the list at the given index.
precondition: none
postcondition: removes the item from the list at the given index
@param index the position from which to remove an element
@throws NoSuchElementException if the Linked List is empty
@throws IndexOutOfBoundsException if index is out of bounds
*/
void remove(int index);
/**
Removes an item from the list.
precondition: none
postcondition: removes the first element in the list whose equal method
matches that of the given item
@param item the element to remove
@throws NoSuchElementException if the Linked List is empty
*/
void remove(E item);
/**
Returns the item at the specified index.
precondition: none
postcondition: returns the element at specified index
@param index the position from which to return an element
@return the item at the specified index
@throws IndexOutOfBoundsException if index is out of bounds
*/
E get(int index);
/**
Returns the index of the first item in the list whose equal method
matches that of the given item, or -1 if no match is found.
precondition: none
postcondition: returns the index of specified item
@param item the element to search for
@return the index of the first item that matches given item or -1 if no match is found
*/
int indexOf(E item);
/**
Creates a duplicate of the list.
precondition: none
postcondition: returns a copy of the linked list
@return a copy of the linked list
*/
List duplicate();
/**
Creates a duplicate of the list with the nodes in reverse order.
precondition: none
postcondition: returns a copy of the linked list with the nodes in
reverse order
@return a copy of the linked list in reverse order
*/
List duplicateReversed();
}
Introduction to Algorithms
ISBN: 978-0262033848
3rd edition
Authors: Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest