import junit.framework.*; public class ListTest extends TestCase { public ListTest(String name) { super(name); } public void testEmptyToString() { assertEquals("empty toString", "[ ]", Empty.ONLY.toString()); } public void testConsToString() { List list = new Cons(3, new Cons(4, Empty.ONLY)); assertEquals("cons toString", "[ 3 4 ]", list.toString()); } public void testEmptyToStringHelp() { assertEquals("empty help", "", Empty.ONLY.toStringHelp()); } public void testConsToStringHelp() { List list = new Cons(3, new Cons(1, Empty.ONLY)); assertEquals("cons help", "3 1 ", list.toStringHelp()); } } abstract class List { public String toString() { return "[ " + toStringHelp() + "]"; } abstract String toStringHelp(); } class Empty extends List { public static final Empty ONLY = new Empty(); private Empty() {} public String toStringHelp() { return ""; } } class Cons extends List { int first; List rest; Cons(int f, List r) { first = f; rest = r; } public String toStringHelp() { return first + " " + rest.toStringHelp(); } }