diff --git a/src/test/java/com/search/DepthFirstSearchTest.java b/src/test/java/com/search/DepthFirstSearchTest.java new file mode 100644 index 00000000..859abfb1 --- /dev/null +++ b/src/test/java/com/search/DepthFirstSearchTest.java @@ -0,0 +1,36 @@ +package src.test.java.com.search; + +import org.junit.Assert; +import org.junit.Test; +import src.main.java.com.search.DepthFirstSearch; +import src.main.java.com.search.BinaryTree; + +public class DepthFirstSearchTest { + @Test + public void testDepthFirstSearch() { + + BinaryTree tree1 = new BinaryTree(); + tree1.add(1,1); + tree1.add(2,2); + tree1.add(3,3); + tree1.add(4,4); + Assert.assertEquals("Incorrect index", 3, DepthFirstSearch.find(3, tree1)); + Assert.assertEquals("Incorrect index", 1, DepthFirstSearch.find(1, tree1)); + Assert.assertEquals("Incorrect index", null, DepthFirstSearch.find(0, tree1)); + Assert.assertEquals("Incorrect index", null, DepthFirstSearch.find(8, tree1)); + Assert.assertEquals("Incorrect index", null, DepthFirstSearch.find(-2, tree1)); + + BinaryTree tree2 = new BinaryTree(); + tree2.add("1","A"); + tree2.add("2","B"); + tree2.add("3","C"); + tree2.add("4","D"); + + Assert.assertEquals("Incorrect index", "C", LinearSearch.findIndex(tree2,"3")); + Assert.assertEquals("Incorrect index", "B", LinearSearch.findIndex(tree2,"2")); + Assert.assertEquals("Incorrect index", null, LinearSearch.findIndex(tree2,"F")); + + BinaryTree tree3 = new BinaryTree(); + Assert.assertEquals("Incorrect index", null, LinearSearch.findIndex(tree3, "")); + } +}