Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2296d5e

Browse files
committed
Generic tree done
1 parent80d99a9 commit2296d5e

File tree

4 files changed

+133
-65
lines changed

4 files changed

+133
-65
lines changed

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

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
packagecom.thealgorithms.datastructures.trees;
22

3+
importcom.thealgorithms.utils.ConsoleInterceptor;
34
importorg.junit.jupiter.api.AfterEach;
45
importorg.junit.jupiter.api.BeforeEach;
56
importorg.junit.jupiter.api.DisplayName;
@@ -8,31 +9,26 @@
89
importorg.junit.jupiter.params.provider.Arguments;
910
importorg.junit.jupiter.params.provider.MethodSource;
1011

11-
importjava.io.ByteArrayOutputStream;
12-
importjava.io.PrintStream;
1312
importjava.util.stream.Stream;
1413

1514
importstaticorg.junit.jupiter.api.Assertions.assertEquals;
1615

1716
publicclassAVLSimpleTest {
1817
AVLSimpletree =newAVLSimple();
19-
ByteArrayOutputStreamoutContent;
20-
PrintStreamoriginalOut;
18+
ConsoleInterceptorsystemOut =newConsoleInterceptor();
2119

2220
/* ========================
2321
Setup/TearDown
2422
======================== */
2523

2624
@BeforeEach
2725
voidsetup() {
28-
outContent =newByteArrayOutputStream();
29-
originalOut =System.out;
30-
System.setOut(newPrintStream(outContent));
26+
systemOut.captureOutput();
3127
}
3228

3329
@AfterEach
3430
voidtearDown() {
35-
System.setOut(originalOut);
31+
systemOut.restoreOutput();
3632
}
3733

3834
/* ========================
@@ -49,10 +45,7 @@ String getExpectedTree() {
4945
}
5046

5147
StringgetActualTree() {
52-
returnoutContent
53-
.toString()
54-
.trim()
55-
.replace("\n","");
48+
returnsystemOut.getConsoleOutput();
5649
}
5750

5851
/* ========================
@@ -92,7 +85,7 @@ void testTreeCreation() {
9285

9386
@ParameterizedTest
9487
@MethodSource("getTreeNodesInput")
95-
@DisplayName("Testto ensure all rotation paths are covered")
88+
@DisplayName("Test to ensure all rotation paths are covered")
9689
voidtestAllRotations(intnode1,intnode2,intnode3) {
9790
tree.insert(node1);
9891
tree.insert(node2);
@@ -159,9 +152,6 @@ void testDuplicatesInTreeCreationDoNotStick() {
159152

160153
tree.display();
161154

162-
StringexpectedTree =getExpectedTree();
163-
StringactualTree =getActualTree();
164-
165-
assertEquals(expectedTree,actualTree);
155+
assertEquals(getExpectedTree(),getActualTree());
166156
}
167157
}
Lines changed: 66 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,81 @@
11
packagecom.thealgorithms.datastructures.trees;
22

3+
importcom.thealgorithms.utils.ConsoleInterceptor;
34
importorg.junit.jupiter.api.AfterEach;
45
importorg.junit.jupiter.api.BeforeEach;
56
importorg.junit.jupiter.api.Test;
67
importorg.junit.jupiter.params.ParameterizedTest;
78
importorg.junit.jupiter.params.provider.ValueSource;
89

9-
importjava.io.ByteArrayInputStream;
10-
importjava.io.ByteArrayOutputStream;
11-
importjava.io.PrintStream;
10+
importjava.util.InputMismatchException;
1211

1312
importstaticorg.junit.jupiter.api.Assertions.assertEquals;
1413
importstaticorg.junit.jupiter.api.Assertions.assertFalse;
14+
importstaticorg.junit.jupiter.api.Assertions.assertThrows;
1515
importstaticorg.junit.jupiter.api.Assertions.assertTrue;
1616

1717
publicclassGenericTreeTest {
1818
GenericTreetree;
19-
ByteArrayOutputStreamoutContent;
20-
PrintStreamoriginalOut;
19+
ConsoleInterceptorsystemInOut =newConsoleInterceptor();
2120

2221
/* ========================
2322
Setup/TearDown
2423
======================== */
2524

2625
@BeforeEach
2726
voidsetup() {
28-
StringsimulatedScannerInput ="40\n4\n34\n0\n1\n0\n32\n1\n123\n0\n342\n2\n98\n0\n35\n0\n";
29-
System.setIn(newByteArrayInputStream(simulatedScannerInput.getBytes()));
27+
StringtreeValues ="40\n5\n34\n0\n1\n0\n32\n1\n123\n0\n342\n2\n98\n0\n35\n0\n12\n0";
28+
systemInOut.mockInput(treeValues);
3029

3130
tree =newGenericTree();
3231

33-
outContent =newByteArrayOutputStream();
34-
originalOut =System.out;
35-
System.setOut(newPrintStream(outContent));
32+
systemInOut.captureOutput();
3633
}
3734

3835
@AfterEach
3936
voidtearDown() {
40-
System.setOut(originalOut);
41-
System.setIn(System.in);
42-
}
43-
44-
/* ========================
45-
Helper methods
46-
======================== */
47-
48-
StringgetActualTree() {
49-
returnoutContent
50-
.toString()
51-
.trim()
52-
.replace("\n","");
37+
systemInOut.restoreInAndOutput();
5338
}
5439

5540
/**
5641
* Creates a generic tree that looks something like this:
5742
*
5843
* <pre>
59-
* 40
60-
* / / \ \
61-
* / / \ \
62-
* 34 132 324
63-
*|/ \
64-
*123 98 35
44+
*40
45+
* / /|\ \
46+
* / /| \ \
47+
* 34 1 32 324 12
48+
* || \
49+
* 123 98 35
6550
* </pre>
6651
* @return The tree in a string format mimicking what the display() method gives.
6752
*/
6853
StringgetExpectedTree() {
69-
return"40=>34 1 32 342 .34=>.1=>.32=>123 .123=>.342=>98 35 .98=>.35=>.";
54+
return"40=>34 1 32 34212.34=>.1=>.32=>123 .123=>.342=>98 35 .98=>.35=>.12=>.";
7055
}
7156

7257
/* ========================
7358
Tests
7459
======================== */
7560

7661
@Test
77-
voidtestCreateTree() {
62+
voidtestCreateValidTree() {
7863
tree.display();
7964

80-
assertEquals(getExpectedTree(),getActualTree());
65+
assertEquals(getExpectedTree(),systemInOut.getConsoleOutput());
66+
}
67+
68+
@Test
69+
voidtestCreateInValidTree() {
70+
StringinvalidTreeValues ="a\nb\nc\n";
71+
systemInOut.mockInput(invalidTreeValues);
72+
73+
assertThrows(InputMismatchException.class,GenericTree::new);
8174
}
8275

8376
@Test
8477
voidtestGettingCorrectSizeOfTree() {
85-
assertEquals(8,tree.size2call());
78+
assertEquals(9,tree.size2call());
8679
}
8780

8881
@Test
@@ -102,8 +95,45 @@ void testFindingAllPresentValuesInTree(int value) {
10295
}
10396

10497
@ParameterizedTest
105-
@ValueSource(ints = {41,35,2,52,542,223,92,38})
106-
voidtestFindingAbsentValuesInTree() {
107-
assertFalse(tree.findcall(666));
98+
@ValueSource(ints = {41,31,2,52,542,223,92,38})
99+
voidtestFindingAbsentValuesInTree(intvalue) {
100+
assertFalse(tree.findcall(value));
101+
}
102+
103+
@Test
104+
voidtestFindingAllNodesOfCertainDepthInTree() {
105+
intheight =tree.heightcall();
106+
107+
for (inti =0;i <=height;i++) {
108+
tree.depthcaller(i);
109+
}
110+
111+
assertEquals("4034132342121239835",systemInOut.getConsoleOutput());
112+
}
113+
114+
@Test
115+
voidtestPreOrderPrintsAsExpected() {
116+
tree.preordercall();
117+
assertEquals("40 34 1 32 123 342 98 35 12 .",systemInOut.getConsoleOutput());
118+
}
119+
120+
@Test
121+
voidtestPostOrderPrintsAsExpected() {
122+
tree.postordercall();
123+
assertEquals("34 1 123 32 98 35 342 12 40 .",systemInOut.getConsoleOutput());
124+
}
125+
126+
@Test
127+
voidtestLevelOrderPrintsAsExpected() {
128+
tree.levelorder();
129+
assertEquals("40 34 1 32 342 12 123 98 35 .",systemInOut.getConsoleOutput());
130+
}
131+
132+
@Test
133+
voidtestLeavesAreRemoved() {
134+
tree.removeleavescall();
135+
tree.display();
136+
137+
assertEquals("40=>32 342 .32=>.342=>.",systemInOut.getConsoleOutput());
108138
}
109139
}
Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
packagecom.thealgorithms.datastructures.trees;
22

3+
importcom.thealgorithms.utils.ConsoleInterceptor;
34
importorg.junit.jupiter.api.BeforeEach;
45
importorg.junit.jupiter.api.Disabled;
56
importorg.junit.jupiter.api.DisplayName;
@@ -9,17 +10,14 @@
910
importorg.junit.jupiter.params.provider.CsvSource;
1011
importorg.junit.jupiter.params.provider.MethodSource;
1112

12-
importjava.io.ByteArrayInputStream;
13-
importjava.io.ByteArrayOutputStream;
14-
importjava.io.PrintStream;
1513
importjava.lang.reflect.Method;
1614
importjava.util.ArrayList;
1715
importjava.util.stream.Stream;
1816

1917
importstaticorg.junit.jupiter.api.Assertions.assertArrayEquals;
2018
importstaticorg.junit.jupiter.api.Assertions.assertEquals;
2119

22-
publicclassLowestCommonAncestorTest {
20+
publicclassLCATest {
2321
/**
2422
* This input creates the following tree:
2523
* <pre>
@@ -61,18 +59,14 @@ void setup() {
6159
@MethodSource("getInput")
6260
@DisplayName("Should return correct common ancestor for any two nodes in the tree")
6361
voidshouldReturnCorrectLCAThroughMain(StringsimulatedInput,StringexpectedParent) {
64-
System.setIn(newByteArrayInputStream(simulatedInput.getBytes()));
65-
66-
ByteArrayOutputStreamoutContent =newByteArrayOutputStream();
67-
PrintStreamoriginalOut =System.out;
68-
System.setOut(newPrintStream(outContent));
62+
ConsoleInterceptorsystemInOut =newConsoleInterceptor();
63+
systemInOut.mockInputAndCaptureOutput(simulatedInput);
6964

7065
LCA.main(newString[0]);
7166

72-
System.setOut(originalOut);
73-
System.setIn(System.in);
67+
systemInOut.restoreInAndOutput();
7468

75-
StringactualParent =outContent.toString().trim();
69+
StringactualParent =systemInOut.getConsoleOutput();
7670
assertEquals(expectedParent,actualParent);
7771
}
7872

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
packagecom.thealgorithms.utils;
2+
3+
importjava.io.ByteArrayInputStream;
4+
importjava.io.ByteArrayOutputStream;
5+
importjava.io.PrintStream;
6+
7+
publicclassConsoleInterceptor {
8+
ByteArrayOutputStreamoutContent;
9+
PrintStreamoriginalOut;
10+
11+
/* ===========================
12+
Input And Output
13+
=========================== */
14+
15+
publicvoidmockInputAndCaptureOutput(StringmockedInput) {
16+
mockInput(mockedInput);
17+
captureOutput();
18+
}
19+
20+
publicvoidrestoreInAndOutput() {
21+
restoreInput();
22+
restoreOutput();
23+
}
24+
25+
/* ===========================
26+
Input
27+
=========================== */
28+
29+
publicvoidmockInput(StringmockedInput) {
30+
System.setIn(newByteArrayInputStream(mockedInput.getBytes()));
31+
}
32+
33+
publicvoidrestoreInput() {
34+
System.setIn(System.in);
35+
}
36+
37+
/* ===========================
38+
Output
39+
=========================== */
40+
41+
publicvoidcaptureOutput() {
42+
outContent =newByteArrayOutputStream();
43+
originalOut =System.out;
44+
System.setOut(newPrintStream(outContent));
45+
}
46+
47+
publicvoidrestoreOutput() {
48+
System.setOut(originalOut);
49+
}
50+
51+
publicStringgetConsoleOutput() {
52+
returnoutContent.toString().trim().replace("\n","");
53+
}
54+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp