/* * StringArrayListTest.java - Tests for StringArrayList class * Starter code for homework assignment 1. * Hal Perkins, CSE 143, Summer 2011 * Updated 6/27: new code in test2 to check ensure capacity bug */ public class StringArrayListTest { // Declare a new exception type for failed tests. This is beyond the scope // of CSE143, but it is a reasonable why to handle the situation. public static class TestFailedException extends java.lang.RuntimeException { // constructors public TestFailedException() { super("test failed"); } public TestFailedException(String reason) { super(reason); } } // Generate an TestFailedException public static void fail() { throw new TestFailedException(); } public static void fail(String reason) { throw new TestFailedException(reason); } // Report an error if condition is false public static void check(boolean condition) { if (!condition) { fail(); } } public static void check(boolean condition, String reason) { if (!condition) { fail(reason); } } // Verify that the given StringArrayList contains the same number of // strings in the same order as in the array expected. Fail if not. public static void checkList(StringArrayList s, String[] expected) { check(s.size() == expected.length, "list size " + s.size() + " not expected value " + expected.length); for (int k = 0; k < expected.length; k++) { check(s.get(k).equals(expected[k]), "elements mismatch at position " + k); } } // StringArrayList tests // test empty and single-element StringArrayList public static void test1() { // create empty StringArrayList StringArrayList s = new StringArrayList(); check(s != null); check(s.size() == 0); check(s.isEmpty()); // add one element to stringlist s.add("foo"); check(s.size() == 1); check(!s.isEmpty()); check(s.get(0).equals("foo")); check(s.indexOf("foo") == 0); check(s.contains("foo")); check(s.indexOf("bar") == -1); check(!s.contains("bar")); } // setup and other methods used by later tests // Return a new StringArrayList containing "foo", "bar", "baz" public static StringArrayList setup1() { StringArrayList s = new StringArrayList(); s.add("foo"); s.add("bar"); s.add("baz"); return s; } // Basic tests for multi-element StringArrayList public static void test2() { StringArrayList s = setup1(); check(s.size() == 3); check(!s.isEmpty()); check(s.contains("foo")); check(s.contains("bar")); check(s.contains("baz")); check(!s.contains("blat")); check(s.indexOf("foo") == 0); check(s.indexOf("baz") == 2); check(s.get(1).equals("bar")); // check that indexOf returns first location if there are duplicate copies s.add("bar"); check(s.size() == 4); check(s.indexOf("bar") == 1); // verify it is possible to fill a list exactly to capacity StringArrayList t = new StringArrayList(3); t.add("abc"); t.add("pqr"); t.add("xyz"); } // check toString public static void test3() { StringArrayList s = new StringArrayList(); check(s.toString().equals("[]")); s.add("hi"); check(s.toString().equals("[hi]")); s = setup1(); check(s.toString().equals("[foo, bar, baz]")); } // Test insert by position public static void test4() { StringArrayList s = setup1(); // check insert in middle of list s.add(1, "bam"); String[] expected1 = {"foo", "bam", "bar", "baz"}; checkList(s, expected1); // check insert at beginning and end s.add(0, "beginning"); s.add(s.size(), "end"); String[] expected2 = {"beginning", "foo", "bam", "bar", "baz", "end"}; checkList(s, expected2); } // Test delete and clear public static void test5() { StringArrayList s = setup1(); s.add("bam"); s.remove(1); String[] expected = {"foo", "baz", "bam"}; checkList(s, expected); s.clear(); check(s.size() == 0); check(s.isEmpty()); check(!s.contains("foo")); } // main program - run tests public static void main(String[] args) { test1(); test2(); test3(); test4(); test5(); } }