1
1
package com .baeldung .algorithms .breadthfirstsearch ;
2
2
3
- import org .junit .jupiter .api .BeforeEach ;
4
3
import org .junit .jupiter .api .Test ;
5
4
6
5
import static org .assertj .core .api .Assertions .assertThat ;
@@ -13,73 +12,75 @@ class BreadthFirstSearchAlgorithmUnitTest {
13
12
private Tree <Integer > rootSecondChild ;
14
13
15
14
private Node <Integer > start ;
16
- private Node <Integer > firstNeighbour ;
17
- private Node <Integer > firstNeighbourNeighbour ;
18
- private Node <Integer > secondNeighbour ;
19
-
20
- @ BeforeEach
21
- void beforeEach () {
22
- initTree ();
23
- initNode ();
24
- }
25
-
26
- private void initTree () {
27
- root = Tree .of (10 );
28
- rootFirstChild = root .addChild (2 );
29
- depthMostChild = rootFirstChild .addChild (3 );
30
- rootSecondChild = root .addChild (4 );
31
- }
32
-
33
- private void initNode () {
34
- start = new Node <>(10 );
35
- firstNeighbour = new Node <>(2 );
36
- start .connect (firstNeighbour );
37
-
38
- firstNeighbourNeighbour = new Node <>(3 );
39
- firstNeighbour .connect (firstNeighbourNeighbour );
40
- firstNeighbourNeighbour .connect (start );
41
-
42
- secondNeighbour = new Node <>(4 );
43
- start .connect (secondNeighbour );
44
- }
15
+ private Node <Integer > firstNeighbor ;
16
+ private Node <Integer > firstNeighborNeighbor ;
17
+ private Node <Integer > secondNeighbor ;
45
18
46
19
@ Test
47
20
void givenTree_whenSearchTen_thenRoot () {
21
+ initTree ();
48
22
assertThat (BreadthFirstSearchAlgorithm .search (10 , root )).isPresent ().contains (root );
49
23
}
50
24
51
25
@ Test
52
26
void givenTree_whenSearchThree_thenDepthMostValue () {
27
+ initTree ();
53
28
assertThat (BreadthFirstSearchAlgorithm .search (3 , root )).isPresent ().contains (depthMostChild );
54
29
}
55
30
56
31
@ Test
57
32
void givenTree_whenSearchFour_thenRootSecondChild () {
33
+ initTree ();
58
34
assertThat (BreadthFirstSearchAlgorithm .search (4 , root )).isPresent ().contains (rootSecondChild );
59
35
}
60
36
61
37
@ Test
62
38
void givenTree_whenSearchFive_thenNotFound () {
39
+ initTree ();
63
40
assertThat (BreadthFirstSearchAlgorithm .search (5 , root )).isEmpty ();
64
41
}
65
42
43
+ private void initTree () {
44
+ root = Tree .of (10 );
45
+ rootFirstChild = root .addChild (2 );
46
+ depthMostChild = rootFirstChild .addChild (3 );
47
+ rootSecondChild = root .addChild (4 );
48
+ }
49
+
66
50
@ Test
67
51
void givenNode_whenSearchTen_thenStart () {
68
- assertThat (BreadthFirstSearchAlgorithm .search (10 , firstNeighbourNeighbour )).isPresent ().contains (start );
52
+ initNode ();
53
+ assertThat (BreadthFirstSearchAlgorithm .search (10 , firstNeighborNeighbor )).isPresent ().contains (start );
69
54
}
70
55
71
56
@ Test
72
- void givenNode_whenSearchThree_thenNeighbourNeighbour () {
73
- assertThat (BreadthFirstSearchAlgorithm .search (3 , firstNeighbourNeighbour )).isPresent ().contains (firstNeighbourNeighbour );
57
+ void givenNode_whenSearchThree_thenNeighborNeighbor () {
58
+ initNode ();
59
+ assertThat (BreadthFirstSearchAlgorithm .search (3 , firstNeighborNeighbor )).isPresent ().contains (firstNeighborNeighbor );
74
60
}
75
61
76
62
@ Test
77
- void givenNode_whenSearchFour_thenSecondNeighbour () {
78
- assertThat (BreadthFirstSearchAlgorithm .search (4 , firstNeighbourNeighbour )).isPresent ().contains (secondNeighbour );
63
+ void givenNode_whenSearchFour_thenSecondNeighbor () {
64
+ initNode ();
65
+ assertThat (BreadthFirstSearchAlgorithm .search (4 , firstNeighborNeighbor )).isPresent ().contains (secondNeighbor );
79
66
}
80
67
81
68
@ Test
82
69
void givenNode_whenSearchFive_thenNotFound () {
83
- assertThat (BreadthFirstSearchAlgorithm .search (5 , firstNeighbourNeighbour )).isEmpty ();
70
+ initNode ();
71
+ assertThat (BreadthFirstSearchAlgorithm .search (5 , firstNeighborNeighbor )).isEmpty ();
72
+ }
73
+
74
+ private void initNode () {
75
+ start = new Node <>(10 );
76
+ firstNeighbor = new Node <>(2 );
77
+ start .connect (firstNeighbor );
78
+
79
+ firstNeighborNeighbor = new Node <>(3 );
80
+ firstNeighbor .connect (firstNeighborNeighbor );
81
+ firstNeighborNeighbor .connect (start );
82
+
83
+ secondNeighbor = new Node <>(4 );
84
+ start .connect (secondNeighbor );
84
85
}
85
86
}
0 commit comments