1212import java .io .ByteArrayInputStream ;
1313import java .io .ByteArrayOutputStream ;
1414import java .io .PrintStream ;
15- import java .lang .reflect .InvocationTargetException ;
1615import java .lang .reflect .Method ;
1716import java .util .ArrayList ;
1817import java .util .stream .Stream ;
@@ -61,7 +60,7 @@ void setup() {
6160@ ParameterizedTest
6261@ MethodSource ("getInput" )
6362@ DisplayName ("Should return correct common ancestor for any two nodes in the tree" )
64- void shouldReturnCorrectLCA2 (String simulatedInput ,String expectedParent ) {
63+ void shouldReturnCorrectLCAThroughMain (String simulatedInput ,String expectedParent ) {
6564System .setIn (new ByteArrayInputStream (simulatedInput .getBytes ()));
6665
6766ByteArrayOutputStream outContent =new ByteArrayOutputStream ();
@@ -88,11 +87,11 @@ public static Stream<Arguments> getInput() {
8887 );
8988 }
9089
90+ @ SuppressWarnings ("RFI_SET_ACCESSIBLE" )
9191@ ParameterizedTest
9292@ CsvSource ({"9,4,2" ,"5,6,5" ,"5,4,0" ,"3,8,3" ,"6,3,0" ,"3,3,3" })
9393@ DisplayName ("Should return correct common ancestor for any two nodes in the tree" )
94- void shouldReturnCorrectLCA (int v1 ,int v2 ,int expectedParent ) {
95- try {
94+ void shouldReturnCorrectLCA (int v1 ,int v2 ,int expectedParent )throws Exception {
9695Method dfs =LCA .class .getDeclaredMethod ("dfs" ,ArrayList .class ,int .class ,int .class ,int [].class ,int [].class );
9796Method getLCA =LCA .class .getDeclaredMethod ("getLCA" ,int .class ,int .class ,int [].class ,int [].class );
9897dfs .setAccessible (true );
@@ -101,35 +100,26 @@ void shouldReturnCorrectLCA(int v1, int v2, int expectedParent) {
101100dfs .invoke (null ,adj ,0 , -1 ,parent ,depth );
102101
103102assertEquals (expectedParent ,getLCA .invoke (null ,v1 ,v2 ,depth ,parent ));
104- }catch (NoSuchMethodException |InvocationTargetException |IllegalAccessException e ) {
105- throw new RuntimeException (e );
106- }
107103 }
108104
105+ @ SuppressWarnings ("RFI_SET_ACCESSIBLE" )
109106@ Test
110- void shouldReturnCorrectDepthsForArray () {
111- try {
112- Method dfs =LCA .class .getDeclaredMethod ("dfs" ,ArrayList .class ,int .class ,int .class ,int [].class ,int [].class );
113- dfs .setAccessible (true );
107+ void shouldReturnCorrectDepthsForArray ()throws Exception {
108+ Method dfs =LCA .class .getDeclaredMethod ("dfs" ,ArrayList .class ,int .class ,int .class ,int [].class ,int [].class );
109+ dfs .setAccessible (true );
114110
115- dfs .invoke (null ,adj ,0 , -1 ,parent ,depth );
116- }catch (NoSuchMethodException |InvocationTargetException |IllegalAccessException e ) {
117- throw new RuntimeException (e );
118- }
111+ dfs .invoke (null ,adj ,0 , -1 ,parent ,depth );
119112
120113assertArrayEquals (new int [] {0 ,1 ,1 ,2 ,2 ,2 ,3 ,3 ,4 ,4 },depth );
121114 }
122115
116+ @ SuppressWarnings ("RFI_SET_ACCESSIBLE" )
123117@ Test
124- void shouldReturnCorrectParentsForArray () {
125- try {
126- Method dfs =LCA .class .getDeclaredMethod ("dfs" ,ArrayList .class ,int .class ,int .class ,int [].class ,int [].class );
127- dfs .setAccessible (true );
118+ void shouldReturnCorrectParentsForArray ()throws Exception {
119+ Method dfs =LCA .class .getDeclaredMethod ("dfs" ,ArrayList .class ,int .class ,int .class ,int [].class ,int [].class );
120+ dfs .setAccessible (true );
128121
129- dfs .invoke (null ,adj ,0 , -1 ,parent ,depth );
130- }catch (NoSuchMethodException |InvocationTargetException |IllegalAccessException e ) {
131- throw new RuntimeException (e );
132- }
122+ dfs .invoke (null ,adj ,0 , -1 ,parent ,depth );
133123
134124assertArrayEquals (new int [] {0 ,0 ,0 ,2 ,2 ,1 ,5 ,3 ,7 ,7 },parent );
135125 }