/** * Test program for some basic functions of the SortedStringList class. * This tests the add and search methods, but does not test any of the code * to eliminate duplicate entries or allow them again. * CSE 143 homework 1 starter code. * @author Hal Perkins * @version 0.2, 3/30/06 */ public class TestSortedStringList { /** * Complain loudly if condition is false * @param condition any boolean value - throw a runtime exception if this is false. */ public static void check(boolean condition) { if (!condition) { throw new RuntimeException("test failed"); } } /** * Verify that a SortedStringList contains an expected list of strings * in the correct order and that it has the right size. * @param values Strings that should appear in the list, in order * @param list the SortedStringList the values should appear in */ public static void checkStrings(String[] values, SortedStringList list) { check(list.size() == values.length); for (int k = 0; k < values.length; k++) { check(list.get(k).equals(values[k])); } } /** * Add the strings in an array to a SortedStringList * @param values Strings to be added to the list * @param list list that strings should be added to */ public static void addStrings(String[] values, SortedStringList list) { for (int k = 0; k < values.length; k++) { list.add(values[k]); } } /** * Create a SortedStringList and try some of its operations. * This is not a comprehensive test, but gives some idea * how to create an object and use some of its methods. */ public static void main(String[] args) { SortedStringList s1 = new SortedStringList(); check(s1.isEmpty()); check(s1.size() == 0); s1.add("foo"); check(!s1.isEmpty()); check(s1.size() == 1); check(s1.get(0).equals("foo")); String[] vals = {"pear", "banana", "apple", "orange"}; SortedStringList s2 = new SortedStringList(); addStrings(vals, s2); String[] expected1 = {"apple", "banana", "orange", "pear"}; checkStrings(expected1, s2); s2.add("strawberry"); String[] expected2 = {"apple", "banana", "orange", "pear", "strawberry"}; checkStrings(expected2, s2); s2.add("chocolate"); String[] expected3 = {"apple", "banana", "chocolate", "orange", "pear", "strawberry"}; checkStrings(expected3, s2); s2.remove(3); String[] expected4 = {"apple", "banana", "chocolate", "pear", "strawberry"}; checkStrings(expected4, s2); check(s2.indexOf("apple") == 0); check(s2.indexOf("banana") == 1); check(s2.indexOf("strawberry") == 4); check(s2.indexOf("orange") == -1); check(s2.indexOf("vanilla") == -1); check(s2.indexOf("aaarrg!") == -1); } }