|
1 | 1 | package com.thealgorithms.datastructures.trees; |
2 | 2 |
|
3 | | -import static org.junit.jupiter.api.Assertions.assertEquals; |
4 | | -import static org.junit.jupiter.api.Assertions.fail; |
5 | | - |
| 3 | +import org.junit.jupiter.api.Assertions; |
6 | 4 | import org.junit.jupiter.api.Test; |
7 | 5 |
|
| 6 | +/** |
| 7 | + * Unit tests for the BinaryTree class. |
| 8 | + */ |
8 | 9 | public class BinaryTreeTest { |
9 | 10 |
|
10 | | - // checks that adding populating the tree and searching for data |
11 | | - // retrieves the expected data |
12 | 11 | @Test |
13 | | - void test1() { |
14 | | - BinaryTree t = new BinaryTree(); |
15 | | - t.put(3); |
16 | | - t.put(5); |
17 | | - t.put(7); |
18 | | - t.put(9); |
19 | | - t.put(12); |
| 12 | + public void testInsertAndFind() { |
| 13 | + BinaryTree tree = new BinaryTree(); |
| 14 | + tree.put(3); |
| 15 | + tree.put(5); |
| 16 | + tree.put(7); |
| 17 | + tree.put(9); |
| 18 | + tree.put(12); |
20 | 19 |
|
21 | | - assertEquals(t.find(5).data, 5); |
22 | | - assertEquals(t.find(7).data, 7); |
| 20 | + Assertions.assertNotNull(tree.find(5), "Node with value 5 should exist"); |
| 21 | + Assertions.assertEquals(5, tree.find(5).data, "Value of the found node should be 5"); |
| 22 | + Assertions.assertEquals(7, tree.find(7).data, "Value of the found node should be 7"); |
23 | 23 | } |
24 | 24 |
|
25 | | - // checks that removing data from the tree |
26 | | - // properly removes and makes the new root the expected new root |
27 | 25 | @Test |
28 | | - void test2() { |
29 | | - BinaryTree t = new BinaryTree(); |
30 | | - t.put(3); |
31 | | - t.put(5); |
32 | | - t.put(7); |
33 | | - t.put(9); |
34 | | - t.put(12); |
35 | | - t.remove(3); |
36 | | - t.remove(5); |
37 | | - t.remove(7); |
| 26 | + public void testRemove() { |
| 27 | + BinaryTree tree = new BinaryTree(); |
| 28 | + tree.put(3); |
| 29 | + tree.put(5); |
| 30 | + tree.put(7); |
| 31 | + tree.put(9); |
| 32 | + tree.put(12); |
| 33 | + tree.remove(3); |
| 34 | + tree.remove(5); |
| 35 | + tree.remove(7); |
38 | 36 |
|
39 | | - // Checks whether the root is null before accessing date |
40 | | - if (t.getRoot() != null) { |
41 | | - assertEquals(t.getRoot().data, 9); |
| 37 | + Assertions.assertNotNull(tree.getRoot(), "Root should not be null after removals"); |
| 38 | + if (tree.getRoot() != null) { |
| 39 | + Assertions.assertEquals(9, tree.getRoot().data, "Root value should be 9 after removals"); |
42 | 40 | } else { |
43 | | - fail("The root node is null after removal."); |
| 41 | + Assertions.fail("Root should not be null after removals, but it is."); |
44 | 42 | } |
45 | 43 | } |
46 | 44 |
|
47 | | - // checks that removing an unexistend node returns false |
48 | | - // as specified by the documentation of the function |
49 | 45 | @Test |
50 | | - void test3() { |
51 | | - BinaryTree t = new BinaryTree(); |
52 | | - t.put(3); |
53 | | - t.put(5); |
54 | | - t.put(7); |
55 | | - t.put(9); |
56 | | - t.put(12); |
| 46 | + public void testRemoveReturnValue() { |
| 47 | + BinaryTree tree = new BinaryTree(); |
| 48 | + tree.put(3); |
| 49 | + tree.put(5); |
| 50 | + tree.put(7); |
| 51 | + tree.put(9); |
| 52 | + tree.put(12); |
57 | 53 |
|
58 | | - assertEquals(t.remove(9), true); |
59 | | - assertEquals(t.remove(398745987), false); |
| 54 | + Assertions.assertTrue(tree.remove(9), "Removing existing node 9 should return true"); |
| 55 | + Assertions.assertFalse(tree.remove(398745987), "Removing non-existing node should return false"); |
60 | 56 | } |
61 | 57 |
|
62 | | - // check if the bfs, inOrder, preOrder and postOrder functions |
63 | | - // worg as expected, also increases the coverage measures in |
64 | | - // JaCoCo |
65 | 58 | @Test |
66 | | - void test4() { |
67 | | - BinaryTree t = new BinaryTree(); |
68 | | - t.put(3); |
69 | | - t.put(5); |
70 | | - t.put(7); |
71 | | - t.put(9); |
72 | | - t.put(12); |
| 59 | + public void testTraversalMethods() { |
| 60 | + BinaryTree tree = new BinaryTree(); |
| 61 | + tree.put(3); |
| 62 | + tree.put(5); |
| 63 | + tree.put(7); |
| 64 | + tree.put(9); |
| 65 | + tree.put(12); |
| 66 | + |
| 67 | + // Testing traversal methods |
| 68 | + tree.bfs(tree.getRoot()); |
| 69 | + tree.inOrder(tree.getRoot()); |
| 70 | + tree.preOrder(tree.getRoot()); |
| 71 | + tree.postOrder(tree.getRoot()); |
73 | 72 |
|
74 | | - t.bfs(t.find(12)); |
75 | | - t.inOrder(t.getRoot()); |
76 | | - t.preOrder(t.getRoot()); |
77 | | - t.postOrder(t.getRoot()); |
| 73 | + Assertions.assertTrue(tree.remove(9), "Removing existing node 9 should return true"); |
| 74 | + Assertions.assertFalse(tree.remove(398745987), "Removing non-existing node should return false"); |
78 | 75 |
|
79 | | - assertEquals(t.remove(9), true); |
80 | | - assertEquals(t.remove(398745987), false); |
| 76 | + Assertions.assertNotNull(tree.getRoot(), "Root should not be null after operations"); |
81 | 77 | } |
82 | 78 | } |
0 commit comments