|
| 1 | +packagesrc.test.java.com.search; |
| 2 | + |
| 3 | +importorg.junit.Assert; |
| 4 | +importorg.junit.Test; |
| 5 | +importsrc.main.java.com.search.DepthFirstSearch; |
| 6 | +importsrc.main.java.com.search.BinaryTree; |
| 7 | + |
| 8 | +publicclassDepthFirstSearchTest { |
| 9 | +@Test |
| 10 | +publicvoidtestDepthFirstSearch() { |
| 11 | + |
| 12 | +BinaryTree<Integer>tree1 =newBinaryTree<Integer>(); |
| 13 | +tree1.add(1,1); |
| 14 | +tree1.add(2,2); |
| 15 | +tree1.add(3,3); |
| 16 | +tree1.add(4,4); |
| 17 | +Assert.assertEquals("Incorrect index",3,DepthFirstSearch.find(3,tree1)); |
| 18 | +Assert.assertEquals("Incorrect index",1,DepthFirstSearch.find(1,tree1)); |
| 19 | +Assert.assertEquals("Incorrect index",null,DepthFirstSearch.find(0,tree1)); |
| 20 | +Assert.assertEquals("Incorrect index",null,DepthFirstSearch.find(8,tree1)); |
| 21 | +Assert.assertEquals("Incorrect index",null,DepthFirstSearch.find(-2,tree1)); |
| 22 | + |
| 23 | +BinaryTree<String>tree2 =newBinaryTree<String>(); |
| 24 | +tree2.add("1","A"); |
| 25 | +tree2.add("2","B"); |
| 26 | +tree2.add("3","C"); |
| 27 | +tree2.add("4","D"); |
| 28 | + |
| 29 | +Assert.assertEquals("Incorrect index","C",LinearSearch.findIndex(tree2,"3")); |
| 30 | +Assert.assertEquals("Incorrect index","B",LinearSearch.findIndex(tree2,"2")); |
| 31 | +Assert.assertEquals("Incorrect index",null,LinearSearch.findIndex(tree2,"F")); |
| 32 | + |
| 33 | +BinaryTree<String>tree3 =newBinaryTree<String>(); |
| 34 | +Assert.assertEquals("Incorrect index",null,LinearSearch.findIndex(tree3,"")); |
| 35 | + } |
| 36 | +} |