|
3 | 3 | import static org.junit.Assert.assertEquals;
|
4 | 4 |
|
5 | 5 | import org.junit.Before;
|
| 6 | +import org.junit.Rule; |
6 | 7 | import org.junit.Test;
|
| 8 | +import org.junit.rules.ExpectedException; |
| 9 | + |
7 | 10 | import com.github.miniyk2012.coding2017.basic.BinaryTreeNode;
|
8 | 11 |
|
9 | 12 | public class BinaryTreeNodeTest {
|
10 | 13 |
|
11 | 14 | private BinaryTreeNode<Integer> binaryTreeNode;
|
12 | 15 |
|
| 16 | + /** |
| 17 | + // 4 |
| 18 | + // 1 5 |
| 19 | + // 2 3 |
| 20 | + */ |
13 | 21 | @Before
|
14 | 22 | public void setUpBinaryTreeNode() {
|
15 | 23 | binaryTreeNode = new BinaryTreeNode<Integer>(4);
|
16 |
| - } |
17 |
| - |
18 |
| - @Test |
19 |
| - public void testBinaryTreeNodeFunctional() { |
20 | 24 | binaryTreeNode.insert(1);
|
21 | 25 | binaryTreeNode.insert(3);
|
22 | 26 | binaryTreeNode.insert(5);
|
23 | 27 | binaryTreeNode.insert(2);
|
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + public void testBinaryTreeNodeFunctional1() { |
24 | 32 | assertEquals(new Integer(4), binaryTreeNode.getData());
|
25 | 33 | assertEquals(new Integer(1), binaryTreeNode.getLeft().getData());
|
26 | 34 | assertEquals(new Integer(5), binaryTreeNode.getRight().getData());
|
27 | 35 | assertEquals(new Integer(3), binaryTreeNode.getLeft().getRight().getData());
|
28 | 36 | assertEquals(new Integer(2), binaryTreeNode.getLeft().getRight().getLeft().getData());
|
29 | 37 | }
|
30 |
| - |
| 38 | + |
| 39 | + @Test |
| 40 | + public void testBinaryTreeFunctional2() { |
| 41 | + BinaryTreeNode<Integer> node1 = binaryTreeNode.getLeft(); |
| 42 | + assertEquals(new Integer(1), node1.getData()); |
| 43 | + assertEquals(new Integer(3), node1.getRight().getData()); |
| 44 | + assertEquals(new Integer(5), binaryTreeNode.getRight().getData()); |
| 45 | + } |
| 46 | + |
| 47 | + @Rule |
| 48 | + public ExpectedException expectedEx = ExpectedException.none(); |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testBinaryTreeFunctional3() |
| 52 | + { |
| 53 | + BinaryTreeNode<Integer> treeNode = new BinaryTreeNode<Integer>(100); |
| 54 | + treeNode.insert(10); |
| 55 | + binaryTreeNode.setRight(treeNode); |
| 56 | + // 4 |
| 57 | + // 1 100 |
| 58 | + // 2 3 10 |
| 59 | + assertEquals(new Integer(4), binaryTreeNode.getData()); |
| 60 | + assertEquals(new Integer(1), binaryTreeNode.getLeft().getData()); |
| 61 | + assertEquals(new Integer(3), binaryTreeNode.getLeft().getRight().getData()); |
| 62 | + assertEquals(new Integer(2), binaryTreeNode.getLeft().getRight().getLeft().getData()); |
| 63 | + assertEquals(new Integer(100), binaryTreeNode.getRight().getData()); |
| 64 | + assertEquals(new Integer(10), binaryTreeNode.getRight().getLeft().getData()); |
| 65 | + |
| 66 | + expectedEx.expect(Exception.class); |
| 67 | + binaryTreeNode.getRight().getRight().getRight(); // null exception |
| 68 | + binaryTreeNode.getRight().getRight().getLeft(); // null exception |
| 69 | + } |
31 | 70 | }
|
0 commit comments