import org.junit.* ; import static org.junit.Assert.* ; import Examples.* ; public class TestBinarySearchTree { private BinarySearchTree empty() { return new BinarySearchTree() ; } private BinarySearchTree singleton() { BinarySearchTree t = new BinarySearchTree() ; t.insert(0); return t ; } private BinarySearchTree t1() { BinarySearchTree t = new BinarySearchTree() ; t.insert(0); t.insert(-10); t.insert(10); return t ; } @Test public void find() { assertTrue(singleton().find(0).equals(0)) ; assertTrue(t1().find(10).equals(10)) ; assertTrue(t1().find(-10).equals(-10)) ; } @Test public void findBO() { assertNull(empty().find(0)) ; assertNull(singleton().find(10)) ; assertNull(t1().find(99)) ; } @Test public void findMax() { // surpressed negative-result test: assertNull(empty().findMax()) ; assertTrue(singleton().findMax().equals(0)) ; assertTrue(t1().findMax().equals(10)) ; } @Test public void findMin() { assertTrue(singleton().findMin().equals(0)) ; assertTrue(t1().findMin() .equals(-10)) ; } @Test public void findMinBO() { assertNull(empty().findMin()) ; } @Test public void isEmpty() { assertTrue(empty().isEmpty()) ; } @Test public void isEmptyBO() { assertFalse(singleton().isEmpty()) ; assertFalse(t1().isEmpty()) ; } @Test public void makeEmpty() { BinarySearchTree t = empty() ; t.makeEmpty() ; assertTrue(t.isEmpty()) ; t = singleton() ; t.makeEmpty() ; assertTrue(t.isEmpty()) ; t = t1() ; t.makeEmpty() ; assertTrue(t.isEmpty()) ; } @Test public void insert() { BinarySearchTree t ; t = empty(); t.insert(0) ; assertTrue(t.findMax().equals(0)) ; t = singleton() ; t.insert(0) ; t.remove(0) ; assertTrue(t.isEmpty()); t = singleton() ; t.insert(10) ; assertTrue(t.findMax().equals(10)) ; t = t1() ; t.insert(-5) ; assertTrue(t.findMin().equals(-10)) ; assertTrue(t.findMax().equals(10)) ; assertTrue(t.find(-5).equals(-5)) ; t = t1() ; t.insert(-15) ; assertTrue(t.findMin().equals(-15)) ; assertTrue(t.findMax().equals(10)) ; t = t1() ; t.insert(15) ; assertTrue(t.findMin().equals(-10)) ; assertTrue(t.findMax().equals(15)) ; } @Test public void insertBO() { BinarySearchTree t ; // test on inserts that suposed have no effect: t = t1() ; t.insert(10) ; assertTrue(t.findMax().equals(10)) ; t = t1() ; t.insert(-10) ; assertTrue(t.findMin().equals(-10)) ; } @Test public void remove() { BinarySearchTree t ; t = t1() ; t.remove(10) ; assertNull(t.find(10)) ; t = t1() ; t.remove(-10) ; assertNull(t.find(-10)) ; } @Test public void removeBO() { BinarySearchTree t ; // testing non-effectfull remove: t = empty() ; t.remove(0) ; assertTrue(t.isEmpty()) ; t = singleton() ; t.remove(0) ; assertTrue(t.isEmpty()) ; t = singleton() ; t.remove(10) ; assertFalse(t.isEmpty()) ; t = t1() ; t.remove(-5) ; assertNull(t.find(-5)) ; } }