package hw4; import junit.framework.*; public class BinarySearchTreeTest extends TestCase { public BinarySearchTreeTest(String pName) { super(pName); } public static void main(String[] args) { junit.textui.TestRunner.run(BinarySearchTreeTest.class); } public static Test suite() { return new TestSuite(BinarySearchTreeTest.class); } /** * Executed before each testX(). * Resets static fields of each user class (except this). */ protected void setUp() { //my setUp() code goes here.. } /** * Executed after each testX() */ protected void tearDown() { //my tearDown() code goes here.. } static int size = 0; /********** Generated testcases **********/ public void testConstructor() { BinarySearchTree t = new BinarySearchTree(); assertTrue(t.size() == 0); assertTrue(t.toString().equals("{}")); } /****** the following tests are for testing add ******/ public void testAddWhenSizeZero() { BinarySearchTree t = new BinarySearchTree(); t.add(new Integer(1)); //System.out.println(t.toString()); assertTrue(t.toString().equals("{ {1} }"));//1 is added into A } public void testAddWhenSizeOne1() { BinarySearchTree t = new BinarySearchTree(); t.add(new Integer(8)); t.add(new Integer(5)); assertTrue(t.toString().equals("{ { {5} 8} }"));//1 is added into A } public void testaddWhenSizeOne2() { BinarySearchTree t = new BinarySearchTree(); t.add(new Integer(8)); t.add(new Integer(16)); assertTrue(t.toString().equals("{ {8 {16} } }"));//1 is added into A } public void testaddWhenSizeOne3() { BinarySearchTree t = new BinarySearchTree(); t.add(new Integer(8)); t.add(new Integer(8)); assertTrue(t.toString().equals("{ {8} }"));//1 is added into A } //the following tests are for the shape of "{}" public void testaddWhenSizeTwo1() { BinarySearchTree t = new BinarySearchTree(); t.add(new Integer(8)); t.add(new Integer(5)); t.add(new Integer(4)); assertTrue(t.toString().equals("{ { { {4} 5} 8} }"));//1 is added into A } public void testaddWhenSizeTwo2() { BinarySearchTree t = new BinarySearchTree(); t.add(new Integer(8)); t.add(new Integer(5)); t.add(new Integer(5)); assertTrue(t.toString().equals("{ { {5} 8} }"));//1 is added into A } public void testaddWhenSizeTwo3() { BinarySearchTree t = new BinarySearchTree(); t.add(new Integer(8)); t.add(new Integer(5)); t.add(new Integer(6)); assertTrue(t.toString().equals("{ { {5 {6} } 8} }"));//1 is added into A } public void testaddWhenSizeTwo4() { BinarySearchTree t = new BinarySearchTree(); t.add(new Integer(8)); t.add(new Integer(5)); t.add(new Integer(8)); assertTrue(t.toString().equals("{ { {5} 8} }"));//1 is added into A } public void testaddWhenSizeTwo5() { BinarySearchTree t = new BinarySearchTree(); t.add(new Integer(8)); t.add(new Integer(5)); t.add(new Integer(12)); assertTrue(t.toString().equals("{ { {5} 8 {12} } }"));//1 is added into A } //the following tests are for the shape of "{}" public void testaddWhenSizeTwoSecond1() { BinarySearchTree t = new BinarySearchTree(); t.add(new Integer(8)); t.add(new Integer(16)); t.add(new Integer(4)); assertTrue(t.toString().equals("{ { {4} 8 {16} } }"));//1 is added into A } public void testaddWhenSizeTwoSecond2() { BinarySearchTree t = new BinarySearchTree(); t.add(new Integer(8)); t.add(new Integer(16)); t.add(new Integer(8)); assertTrue(t.toString().equals("{ {8 {16} } }"));//1 is added into A } public void testaddWhenSizeTwoSecond3() { BinarySearchTree t = new BinarySearchTree(); t.add(new Integer(8)); t.add(new Integer(16)); t.add(new Integer(12)); assertTrue(t.toString().equals("{ {8 { {12} 16} } }"));//1 is added into A } public void testaddWhenSizeTwoSecond4() { BinarySearchTree t = new BinarySearchTree(); t.add(new Integer(8)); t.add(new Integer(16)); t.add(new Integer(16)); assertTrue(t.toString().equals("{ {8 {16} } }"));//1 is added into A } public void testaddWhenSizeTwoSecond5() { BinarySearchTree t = new BinarySearchTree(); t.add(new Integer(8)); t.add(new Integer(16)); t.add(new Integer(20)); assertTrue(t.toString().equals("{ {8 {16 {20} } } }"));//1 is added into A } public void testEquals1() { BinarySearchTree t = new BinarySearchTree(); assertTrue(!t.equals(new Integer(1))); } public void testEquals2() { BinarySearchTree t = new BinarySearchTree(); t.add(new Integer(1)); t.add(new Integer(2)); t.add(new Integer(4)); BinarySearchTree t2 = new BinarySearchTree(); t2.add(new Integer(1)); t2.add(new Integer(2)); t2.add(new Integer(4)); assertTrue(t.equals(t2)); } public void testEquals3() { BinarySearchTree t = new BinarySearchTree(); t.add(new Integer(1)); t.add(new Integer(2)); t.add(new Integer(4)); BinarySearchTree t2 = new BinarySearchTree(); t2.add(new Integer(1)); t2.add(new Integer(2)); t2.add(new Integer(5)); assertTrue(!t.equals(t2)); } public void testEquals4() { BinarySearchTree t = new BinarySearchTree(); BinarySearchTree t2 = new BinarySearchTree(); t2.add(new Integer(1)); t2.add(new Integer(2)); t2.add(new Integer(5)); assertTrue(!t.equals(t2)); assertTrue(!t2.equals(t)); } public void testEquals5() { BinarySearchTree t = new BinarySearchTree(); BinarySearchTree t2 = new BinarySearchTree(); assertTrue(t.equals(t2)); } }