11package org .dojo .algorithms ;
22
3+ import org .apache .commons .lang3 .tuple .Pair ;
34import org .junit .jupiter .api .BeforeEach ;
45import org .junit .jupiter .params .ParameterizedTest ;
56import org .junit .jupiter .params .provider .Arguments ;
67import org .junit .jupiter .params .provider .MethodSource ;
78
89import java .util .Arrays ;
10+ import java .util .List ;
911import java .util .stream .Stream ;
1012
13+ import static org .assertj .core .api .Assertions .assertThat ;
1114import static org .junit .jupiter .api .Assertions .assertArrayEquals ;
1215
1316class HashTablesTests {
@@ -32,7 +35,7 @@ void countFrequencies(int[] input, int[] expected) {
3235 assertArrayEquals (expected , actual );
3336 }
3437
35- private static Stream <Arguments > testData () {
38+ private static Stream <Arguments > testTwoSumData () {
3639 return Stream .of (
3740 Arguments .of (new int [] {2 , 7 , 11 , 15 }, 9 , new int [] {0 , 1 }),
3841 Arguments .of (new int [] {3 , 2 , 4 }, 6 , new int [] {1 , 2 }),
@@ -46,11 +49,36 @@ private static Stream<Arguments> testData() {
4649 }
4750
4851 @ ParameterizedTest
49- @ MethodSource ("testData " )
52+ @ MethodSource ("testTwoSumData " )
5053 public void testTwoSum (int [] inputArray , int inputTarget , int [] expected ) {
5154 int [] actual = sut .twoSum (inputArray , inputTarget );
5255 System .out .printf ("%s%n" , Arrays .toString (expected ));
5356 System .out .printf ("%s%n" , Arrays .toString (actual ));
5457 assertArrayEquals (expected , actual );
5558 }
59+
60+ private static Stream <Arguments > testTwoSumAlt2Data () {
61+ return Stream .of (
62+ Arguments .of (new int [] {2 , 7 , 11 , 15 }, 9 , List .of (Pair .of (0 , 1 ))),
63+ Arguments .of (new int [] {2 , 7 , 4 , 5 , 11 , 15 }, 9 , List .of (Pair .of (0 , 1 ), Pair .of (2 , 3 ))),
64+ Arguments .of (new int [] {3 , 2 , 4 }, 6 , List .of (Pair .of (1 , 2 ))),
65+ Arguments .of (new int [] {3 , 3 }, 6 , List .of (Pair .of (0 , 1 ))),
66+ Arguments .of (new int [] {3 , 2 , 3 }, 6 , List .of (Pair .of (0 , 2 ))),
67+ Arguments .of (new int [] {0 , 4 , 3 , 0 }, 0 , List .of (Pair .of (0 , 3 ))),
68+ Arguments .of (new int [] {-3 , 4 , 3 , 90 }, 0 , List .of (Pair .of (0 , 2 ))),
69+ Arguments .of (new int [] {-1 , -2 , -3 , -4 , -5 }, -8 , List .of (Pair .of (2 , 4 ))),
70+ Arguments .of (new int [] {-10 , -1 , -18 , -19 }, -19 , List .of (Pair .of (1 , 2 ))),
71+ Arguments .of (new int [] {1 , 2 , 3 , 4 , 5 , 6 }, 7 , List .of (Pair .of (0 , 5 ), Pair .of (1 , 4 ), Pair .of (2 , 3 ))),
72+ Arguments .of (new int [] {1 , 2 , 3 }, 100 , List .of ())
73+ );
74+ }
75+
76+ @ ParameterizedTest
77+ @ MethodSource ("testTwoSumAlt2Data" )
78+ public void testTwoSumAlt2 (int [] inputArray , int inputTarget , List <Pair <Integer , Integer >> expected ) {
79+ List <Pair <Integer , Integer >> actual = sut .twoSumAlt2 (inputArray , inputTarget );
80+ assertThat (expected )
81+ .withFailMessage (() -> String .format ("Expected pairs: %s, but got: %s" , expected , actual ))
82+ .containsAll (actual );
83+ }
5684}
0 commit comments