/** * Test program for some basic functions of the unordered StringList class. * CSE 143 demonstration program. * @author Hal Perkins * @version 0.4, 4/7/06 */ public class TestStringList { /** * 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"); } } /** * Create a StringList 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) { StringList s1 = new StringList(); //System.out.println(s1); check(s1.isEmpty()); check(s1.size() == 0); s1.add("foo"); //System.out.println(s1); check(!s1.isEmpty()); check(s1.size() == 1); check(s1.get(0).equals("foo")); s1.add("bar"); s1.add("baz"); //System.out.println(s1); check(s1.size() == 3); check(s1.get(0).equals("foo")); check(s1.get(2).equals("baz")); s1.set(1,"blah"); //System.out.println(s1); check(s1.size() == 3); check(s1.get(0).equals("foo")); check(s1.get(1).equals("blah")); check(s1.get(2).equals("baz")); s1.add(1,"boo"); //System.out.println(s1); check(s1.size() == 4); check(s1.get(0).equals("foo")); check(s1.get(1).equals("boo")); check(s1.get(2).equals("blah")); check(s1.get(3).equals("baz")); // test dynamic expansion StringList s2 = new StringList(); for (int k = 1; k <= 1000; k++) { s2.add("stuff"); } check(s2.size() == 1000); for (int k = 0; k < s2.size(); k++) { check(s2.get(k).equals("stuff")); } // iterator test StringList s3 = new StringList(); s3.add("apple"); s3.add("banana"); s3.add("cherry"); // Since StringListIterator is a nested class, we need its full name // here. We'll see a better way to do this when we get to interfaces. StringList.StringListIterator it = s3.iterator(); check(it.hasNext()); check(it.next().equals("apple")); check(it.hasNext()); check(it.next().equals("banana")); check(it.hasNext()); check(it.next().equals("cherry")); check(!it.hasNext()); } }