Skip to content

Commit 177f115

Browse files
committed
Made style changes
According to Clang again...
1 parent 96b539d commit 177f115

File tree

4 files changed

+85
-39
lines changed

4 files changed

+85
-39
lines changed

src/test/java/com/thealgorithms/datastructures/trees/AVLSimpleTest.java

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.thealgorithms.datastructures.trees;
22

3+
import static org.junit.jupiter.api.Assertions.assertAll;
4+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
35
import static org.junit.jupiter.api.Assertions.assertEquals;
46

57
import com.thealgorithms.devutils.ConsoleInterceptor;
@@ -20,11 +22,13 @@ public class AVLSimpleTest {
2022
Setup/TearDown
2123
======================== */
2224

25+
/** Starts capturing System.in. */
2326
@BeforeEach
2427
void setup() {
2528
interceptor.captureOutput();
2629
}
2730

31+
/** Cleans up after each test by closing the interceptor. */
2832
@AfterEach
2933
void tearDown() {
3034
interceptor.close();
@@ -34,10 +38,29 @@ void tearDown() {
3438
Helper methods
3539
======================== */
3640

41+
/**
42+
* Returns a string representation of the expected tree after inserting
43+
* the values 10, 20, and 30 in any order.
44+
* <p>
45+
* The returned string follows the same format as the tree's display method,
46+
* where each node is represented in the form "left=>value<=right" and "END"
47+
* denotes an empty (null) child.
48+
* @return a string representing the expected tree structure
49+
*/
3750
String getExpectedTree() {
3851
return "10=>20<=30END=>10<=ENDEND=>30<=END2";
3952
}
4053

54+
/**
55+
* Returns the actual string representation of the tree as printed
56+
* by the display method.
57+
*
58+
* <p>This method captures the console output via the interceptor and
59+
* removes all newline characters, returning a single-line string
60+
* suitable for comparison with the expected tree string.
61+
*
62+
* @return the actual tree structure as a single-line string
63+
*/
4164
String getActualTree() {
4265
return interceptor.getAndClearConsoleOutput().replaceAll("[\\r\\n]", "");
4366
}
@@ -47,7 +70,7 @@ String getActualTree() {
4770
======================== */
4871

4972
@Test
50-
@DisplayName("A longer generic AVL tree creation that should pass")
73+
@DisplayName("Creates a longer simple AVL tree that matches the expected layout")
5174
void testTreeCreation() {
5275
tree.insert(25);
5376
tree.insert(30);
@@ -77,52 +100,51 @@ void testEmptyTree() {
77100
}
78101

79102
@ParameterizedTest
80-
@MethodSource("getTreeNodesInput")
103+
@MethodSource("getTreeNodesValues")
81104
@DisplayName("Test to ensure all rotation paths are covered")
82-
void testAllRotations(int node1, int node2, int node3) {
83-
tree.insert(node1);
84-
tree.insert(node2);
85-
tree.insert(node3);
105+
void testAllRotations(int node1, int node2, int node3, String errorMessage) {
106+
assertAll(() -> assertDoesNotThrow(() -> tree.insert(node1), "inserting: " + node1), () -> assertDoesNotThrow(() -> tree.insert(node2), "inserting: " + node2), () -> assertDoesNotThrow(() -> tree.insert(node3), "inserting: " + node3));
86107

87108
tree.display();
88109

89-
assertEquals(getExpectedTree(), getActualTree());
110+
assertEquals(getExpectedTree(), getActualTree(), errorMessage);
90111
}
91112

92-
public static Stream<Arguments> getTreeNodesInput() {
93-
return Stream.of(Arguments.of(30, 20, 10), Arguments.of(30, 10, 20), Arguments.of(10, 20, 30), Arguments.of(10, 30, 20));
113+
public static Stream<Arguments> getTreeNodesValues() {
114+
return Stream.of(Arguments.of(30, 20, 10, "LL rotation failed"), Arguments.of(30, 10, 20, "LR rotation failed"), Arguments.of(10, 20, 30, "RR rotation failed"), Arguments.of(10, 30, 20, "RL rotation failed"));
94115
}
95116

96117
@ParameterizedTest
97118
@MethodSource("getTreeNodesInputForBFEqualsOneRotations")
98-
@DisplayName("Rotation not triggered when balance factor equals threshold")
99-
void testRotatesNotTriggeredWhenBFEqualsOne(int node, String expectedTree) {
100-
tree.insert(30);
119+
@DisplayName("Checks rotation isn't triggered when balance factor equals threshold")
120+
void testRotationsNotTriggeredWhenBFEqualsOne(int boundaryNode, String expectedTree, String errorMessage) {
101121
tree.insert(20);
122+
tree.insert(30);
102123
tree.insert(10);
103-
tree.insert(node);
124+
tree.insert(boundaryNode);
104125

105126
tree.display();
106127

107-
assertEquals(expectedTree, getActualTree());
128+
assertEquals(expectedTree, getActualTree(), errorMessage);
108129
}
109130

110131
public static Stream<Arguments> getTreeNodesInputForBFEqualsOneRotations() {
111-
return Stream.of(Arguments.of(5, "10=>20<=305=>10<=ENDEND=>5<=ENDEND=>30<=END3"), Arguments.of(35, "10=>20<=30END=>10<=ENDEND=>30<=35END=>35<=END3"));
132+
return Stream.of(Arguments.of(5, "10=>20<=305=>10<=ENDEND=>5<=ENDEND=>30<=END3", "Insertion of 5 should not trigger rotation"), Arguments.of(15, "10=>20<=30END=>10<=15END=>15<=ENDEND=>30<=END3", "Insertion of 15 should not trigger rotation"),
133+
Arguments.of(25, "10=>20<=30END=>10<=END25=>30<=ENDEND=>25<=END3", "Insertion of 25 should not trigger rotation"), Arguments.of(35, "10=>20<=30END=>10<=ENDEND=>30<=35END=>35<=END3", "Insertion of 35 should not trigger rotation"));
112134
}
113135

114136
@Test
115-
@DisplayName("Should return true for a tree that don't account for duplicates")
116-
void testDuplicatesInTreeCreationDoNotStick() {
137+
@DisplayName("Ignores duplicate insertions and create a tree with depth one")
138+
void testDuplicateInsertionsIgnored() {
117139
int duplicate = 20;
118-
tree.insert(30);
119140
tree.insert(duplicate);
120-
tree.insert(20);
121141
tree.insert(duplicate);
122-
tree.insert(10);
142+
tree.insert(duplicate);
123143

124144
tree.display();
125145

126-
assertEquals(getExpectedTree(), getActualTree());
146+
String actualTree = getActualTree();
147+
148+
assertEquals('1', actualTree.charAt(actualTree.length() - 1));
127149
}
128150
}

src/test/java/com/thealgorithms/datastructures/trees/GenericTreeTest.java

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.junit.jupiter.api.AfterEach;
66
import org.junit.jupiter.api.Assertions;
77
import org.junit.jupiter.api.BeforeEach;
8+
import org.junit.jupiter.api.DisplayName;
89
import org.junit.jupiter.api.Test;
910
import org.junit.jupiter.params.ParameterizedTest;
1011
import org.junit.jupiter.params.provider.ValueSource;
@@ -17,17 +18,25 @@ public class GenericTreeTest {
1718
Setup/TearDown
1819
======================== */
1920

21+
/**
22+
* Sets up the test environment before each test:
23+
* <ol>
24+
* <li>Makes the interceptor provide a predefined sequence of values for the tree.</li>
25+
* <li>Initializes a new GenericTree instance.</li>
26+
* <li>Starts capturing console output via the interceptor.</li>
27+
* </ol>
28+
*/
2029
@BeforeEach
2130
void setup() {
2231
String treeValues = "40\n5\n34\n0\n1\n0\n32\n1\n123\n0\n342\n2\n98\n0\n35\n0\n12\n0";
2332
interceptor.mockInput(treeValues);
24-
interceptor.captureOutput();
2533

2634
tree = new GenericTree();
2735

28-
interceptor.clearConsoleOutput();
36+
interceptor.captureOutput();
2937
}
3038

39+
/** Cleans up after each test by closing the interceptor. */
3140
@AfterEach
3241
void tearDown() {
3342
interceptor.close();
@@ -38,7 +47,8 @@ void tearDown() {
3847
======================== */
3948

4049
/**
41-
* Creates a generic tree that looks something like this:
50+
* Returns the string representation of the generic tree that's created in the {@link #setup()} method.
51+
* It looks something like this:
4252
*
4353
* <pre>
4454
* 40
@@ -54,6 +64,15 @@ String getExpectedTree() {
5464
return "40=>34 1 32 342 12 .34=>.1=>.32=>123 .123=>.342=>98 35 .98=>.35=>.12=>.";
5565
}
5666

67+
/**
68+
* Returns the actual string representation of the tree as printed
69+
* by the display method.
70+
* <p>
71+
* This method captures the console output via the interceptor and
72+
* removes all newline characters, returning a single-line string
73+
* suitable for comparison with the expected tree string.
74+
* @return the actual tree structure as a single-line string
75+
*/
5776
String getConsoleOutput() {
5877
return interceptor.getAndClearConsoleOutput().replaceAll("[\\r\\n]", "");
5978
}
@@ -63,20 +82,15 @@ String getConsoleOutput() {
6382
======================== */
6483

6584
@Test
66-
void testCreateValidTree() {
85+
@DisplayName("Asserts the created tree have the expected structure")
86+
void testValidTree() {
6787
tree.display();
6888

6989
Assertions.assertEquals(getExpectedTree(), getConsoleOutput());
7090
}
7191

7292
@Test
73-
void testCreateValidTreeIsNotEmpty() {
74-
tree.display();
75-
76-
Assertions.assertDoesNotThrow(interceptor::getAndClearConsoleOutput);
77-
}
78-
79-
@Test
93+
@DisplayName("Creating GenericTree with invalid input throws InputMismatchException")
8094
void testCreateInValidTree() {
8195
String invalidTreeValues = "a\nb\nc\n";
8296
interceptor.mockInput(invalidTreeValues);
@@ -85,33 +99,39 @@ void testCreateInValidTree() {
8599
}
86100

87101
@Test
102+
@DisplayName("Gets the correct number of nodes in the tree")
88103
void testGettingCorrectSizeOfTree() {
89-
Assertions.assertEquals(9, tree.size2call());
104+
Assertions.assertEquals(3, tree.size2call());
90105
}
91106

92107
@Test
108+
@DisplayName("Gets the highest value among the trees nodes")
93109
void testGettingTheMaximalValueOfTreesNodes() {
94110
Assertions.assertEquals(342, tree.maxcall());
95111
}
96112

97113
@Test
114+
@DisplayName("Returns the correct height of the tree")
98115
void testGettingTheHeightOfTree() {
99116
Assertions.assertEquals(2, tree.heightcall());
100117
}
101118

102119
@ParameterizedTest
103120
@ValueSource(ints = {40, 34, 1, 32, 342, 123, 98, 35})
121+
@DisplayName("Returns true when searching for values that is in the tree")
104122
void testFindingAllPresentValuesInTree(int value) {
105-
Assertions.assertTrue(tree.findcall(value));
123+
Assertions.assertTrue(tree.findcall(value), "The expected value " + value + " wasn't in the tree");
106124
}
107125

108126
@ParameterizedTest
109127
@ValueSource(ints = {41, 31, 2, 52, 542, 223, 92, 38})
128+
@DisplayName("Returns false when searching for values that isn't in the tree")
110129
void testFindingAbsentValuesInTree(int value) {
111-
Assertions.assertFalse(tree.findcall(value));
130+
Assertions.assertFalse(tree.findcall(value), "The value " + value + " was unexpectedly in the tree");
112131
}
113132

114133
@Test
134+
@DisplayName("Outputs all nodes at each tree level from left to right")
115135
void testFindingAllNodesOfCertainDepthInTree() {
116136
int height = tree.heightcall();
117137

@@ -123,24 +143,28 @@ void testFindingAllNodesOfCertainDepthInTree() {
123143
}
124144

125145
@Test
146+
@DisplayName("Prints all node values in pre order")
126147
void testPreOrderPrintsAsExpected() {
127148
tree.preordercall();
128149
Assertions.assertEquals("40 34 1 32 123 342 98 35 12 .", getConsoleOutput());
129150
}
130151

131152
@Test
153+
@DisplayName("Prints all node values in post order")
132154
void testPostOrderPrintsAsExpected() {
133155
tree.postordercall();
134156
Assertions.assertEquals("34 1 123 32 98 35 342 12 40 .", getConsoleOutput());
135157
}
136158

137159
@Test
160+
@DisplayName("Prints all node values in level order")
138161
void testLevelOrderPrintsAsExpected() {
139162
tree.levelorder();
140163
Assertions.assertEquals("40 34 1 32 342 12 123 98 35 .", getConsoleOutput());
141164
}
142165

143166
@Test
167+
@DisplayName("Removes all leaves from the tree")
144168
void testLeavesAreRemoved() {
145169
tree.removeleavescall();
146170
tree.display();

src/test/java/com/thealgorithms/datastructures/trees/LCATest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class LCATest {
3131
======================== */
3232

3333
@ParameterizedTest
34-
@MethodSource("getInput")
34+
@MethodSource("getSimulatedInputAndExpectedParent")
3535
@DisplayName("Should return correct common ancestor for any two nodes in the tree")
3636
void shouldReturnCorrectLCAThroughMain(String simulatedInput, String expectedParent) {
3737
try (ConsoleInterceptor interceptor = new ConsoleInterceptor()) {
@@ -42,11 +42,11 @@ void shouldReturnCorrectLCAThroughMain(String simulatedInput, String expectedPar
4242

4343
String actualParent = interceptor.getAndClearConsoleOutput().replaceAll("[\\r\\n]", "");
4444

45-
assertEquals(expectedParent, actualParent);
45+
assertEquals(expectedParent, actualParent, "The two nodes Lowest Common Ancestor wasn't the expected one.");
4646
}
4747
}
4848

49-
public static Stream<Arguments> getInput() {
49+
public static Stream<Arguments> getSimulatedInputAndExpectedParent() {
5050
return Stream.of(Arguments.of(TREE + "9\n4\n", "2"), Arguments.of(TREE + "5\n6\n", "5"), Arguments.of(TREE + "5\n4\n", "0"), Arguments.of(TREE + "3\n8\n", "3"), Arguments.of(TREE + "6\n3\n", "0"), Arguments.of(TREE + "3\n3\n", "3"));
5151
}
5252
}

src/test/java/com/thealgorithms/devutils/ConsoleInterceptor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void mockInput(String mockedInput) {
5151

5252
/**
5353
* Start capturing System.out by replacing stdout with a custom PrintStream.
54-
* All printed data will be stored in outContent for later retrieval.
54+
* All printed data will be stored in outContent - available via {@link #getAndClearConsoleOutput}, for later retrieval.
5555
*/
5656
public void captureOutput() {
5757
if (!isCapturing) {

0 commit comments

Comments
 (0)