import junit.framework.TestCase; /** * A JUnit test case class for OpenList * Every method starting with the word "test" will be called when running * the test with JUnit. * * Each test should have only one "assert" statement. * Sometimes you may want assertTrue, while other times you might want assertEquals. * Also note that sometimes you might want to compare your results to another OpenList * which you create "by hand". Early tests use strings for comparison, to test the constructor. * * IMPORTANT: Do not use assertEquals to compare two OpenLists until you are sure that * your equals method is working. * * The "setUp" function is called before each of the tests. * */ public class OpenListTester extends TestCase { private OpenList greetingL; private OpenList megagreeting; private OpenList numericStrings; private OpenList DB; protected void setUp() { greetingL = new OpenList( "Hello", OpenList.emptyList ); megagreeting = new OpenList( "Hey", greetingL ); numericStrings = OpenList.emptyList; for (int i=0 ; i<10 ; ++i) { numericStrings = new OpenList( i , numericStrings ); } // Set up the DB for the assoc tests /* * creation of an association list * This would be much easier if we had written cons already... */ OpenList sublist1 = new OpenList( "jan", new OpenList( 31, OpenList.emptyList )); OpenList sublist2 = new OpenList( "feb", new OpenList( 28, OpenList.emptyList )); OpenList sublist3 = new OpenList( "mar", new OpenList( 31, OpenList.emptyList )); DB = new OpenList( sublist3, OpenList.emptyList ); DB = new OpenList( sublist2, DB ); DB = new OpenList( sublist1, DB ); } /** * Basic constructor test. */ public void testConstructor1() { assertEquals("Constructor test 1", greetingL.toString(), "[Hello]"); } /** * Test for creating an openList from another openList */ public void testConstructor2(){ assertEquals("Constructor test 2", megagreeting.toString(), "[Hey, Hello]"); } /* * Test creating a longer list. * */ public void testConstructor3() { assertEquals( "Constructor test 3", numericStrings.toString(), "[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]"); } // Tests for the static and non-static assoc functions follow // Note: This test will fail until the equals method is written public void testStaticAssoc1() { OpenList answer = new OpenList( "feb", new OpenList( 28, OpenList.emptyList ) ); assertEquals( "Will fail if equals is not written", answer, OpenList.assoc("feb", DB)); } public void testStaticAssoc2() { assertEquals( OpenList.emptyList, OpenList.assoc("sep", DB)); } // Note: This test will fail until the equals method is written public void testNonStaticAssoc1() { OpenList answer = new OpenList( "feb", new OpenList( 28, OpenList.emptyList ) ); assertEquals( "Will fail if equals is not written", answer, DB.assoc("feb")); } public void testNonStaticAssoc2() { assertEquals( OpenList.emptyList, DB.assoc("sep")); } // Add your tests below. }