forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDepthFirstSearchTest.java
93 lines (72 loc) · 2.56 KB
/
DepthFirstSearchTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package com.thealgorithms.searches;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.thealgorithms.datastructures.Node;
import java.util.List;
import java.util.Optional;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class DepthFirstSearchTest {
private Node<Integer> root;
private DepthFirstSearch<Integer> dfs;
//
// Tree structure:
// 1
// / | \
// 2 3 4
// / \
// 5 6
@BeforeEach
public void setUp() {
// nodes declaration
root = new Node<>(1);
var nodeB = new Node<>(2);
var nodeC = new Node<>(3);
var nodeD = new Node<>(4);
var nodeE = new Node<>(5);
var nodeF = new Node<>(6);
// tree initialization
root.addChild(nodeB);
root.addChild(nodeC);
root.addChild(nodeD);
nodeB.addChild(nodeE);
nodeB.addChild(nodeF);
// create an instance to monitor the visited nodes
dfs = new DepthFirstSearch<>();
}
@Test
public void testSearchRoot() {
Integer expectedValue = 1;
List<Integer> expectedPath = List.of(1);
// check value
Optional<Node<Integer>> value = dfs.recursiveSearch(root, expectedValue);
assertEquals(expectedValue, value.orElseGet(() -> new Node<>(null)).getValue());
// check path
assertArrayEquals(expectedPath.toArray(), dfs.getVisited().toArray());
}
@Test
public void testSearch4() {
Integer expectedValue = 4;
List<Integer> expectedPath = List.of(1, 2, 5, 6, 3, 4);
// check value
Optional<Node<Integer>> value = dfs.recursiveSearch(root, expectedValue);
assertEquals(expectedValue, value.orElseGet(() -> new Node<>(null)).getValue());
// check path
assertArrayEquals(expectedPath.toArray(), dfs.getVisited().toArray());
}
@Test
void testNullRoot() {
var value = dfs.recursiveSearch(null, 4);
assertTrue(value.isEmpty());
}
@Test
void testSearchValueThatNotExists() {
List<Integer> expectedPath = List.of(1, 2, 5, 6, 3, 4);
var value = dfs.recursiveSearch(root, 10);
// check that the value is empty because it's not exists in the tree
assertTrue(value.isEmpty());
// check path is the whole list
assertArrayEquals(expectedPath.toArray(), dfs.getVisited().toArray());
}
}