1
1
package com .thealgorithms .backtracking ;
2
2
3
- import static org .junit .jupiter .api .Assertions .assertEquals ;
4
- import static org .junit .jupiter .api .Assertions .assertNull ;
5
3
4
+ import java .util .ArrayList ;
6
5
import java .util .List ;
7
- import java .util .TreeSet ;
8
- import org .junit .jupiter .api .Test ;
6
+
7
+ import org .junit .jupiter .api .Test ;
8
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
9
9
10
10
public class ArrayCombinationTest {
11
11
12
12
@ Test
13
- void testNBeingZeroOrLess () {
14
- List <TreeSet <Integer >> zeroResult = ArrayCombination .combination (0 , 1 );
15
- List <TreeSet <Integer >> negativeResult = ArrayCombination .combination (-1 , 1 );
16
- assertNull (zeroResult );
17
- assertNull (negativeResult );
13
+ public void testCombination () {
14
+ // Test case 1: n = 4, k = 2
15
+ List <List <Integer >> expected = new ArrayList <>();
16
+ expected .add (List .of (1 , 2 ));
17
+ expected .add (List .of (1 , 3 ));
18
+ expected .add (List .of (1 , 4 ));
19
+ expected .add (List .of (2 , 3 ));
20
+ expected .add (List .of (2 , 4 ));
21
+ expected .add (List .of (3 , 4 ));
22
+
23
+ List <List <Integer >> actual = ArrayCombination .combination (4 , 2 );
24
+
25
+ assertEquals (expected , actual );
18
26
}
19
27
20
28
@ Test
21
- void testNoLengthElement () {
22
- List < TreeSet < Integer >> result = ArrayCombination . combination ( 2 , 0 );
23
- assertNull ( result );
24
- }
29
+ public void testEmptyCombination () {
30
+ // Test case 2: n = 4, k = 0 (invalid input)
31
+ List < List < Integer >> expected = new ArrayList <>( );
32
+ List < List < Integer >> actual = ArrayCombination . combination ( 4 , 0 );
25
33
26
- @ Test
27
- void testLengthOne () {
28
- List <TreeSet <Integer >> result = ArrayCombination .combination (2 , 1 );
29
- assert result != null ;
30
- assertEquals (1 , result .get (0 ).iterator ().next ());
31
- assertEquals (2 , result .get (1 ).iterator ().next ());
34
+ assertEquals (expected , actual );
32
35
}
33
36
34
37
@ Test
35
- void testLengthTwo () {
36
- List <TreeSet <Integer >> result = ArrayCombination .combination (2 , 2 );
37
- assert result != null ;
38
- Integer [] arr = result .get (0 ).toArray (new Integer [2 ]);
39
- assertEquals (1 , arr [0 ]);
40
- assertEquals (2 , arr [1 ]);
41
- }
38
+ public void testSingleElementCombinations () {
39
+ // Test case 3: n = 3, k = 1
40
+ List <List <Integer >> expected = new ArrayList <>();
41
+ expected .add (List .of (1 ));
42
+ expected .add (List .of (2 ));
43
+ expected .add (List .of (3 ));
42
44
43
- @ Test
44
- void testLengthFive () {
45
- List <TreeSet <Integer >> result = ArrayCombination .combination (10 , 5 );
46
- assert result != null ;
47
- Integer [] arr = result .get (0 ).toArray (new Integer [5 ]);
48
- assertEquals (1 , arr [0 ]);
49
- assertEquals (5 , arr [4 ]);
45
+ List <List <Integer >> actual = ArrayCombination .combination (3 , 1 );
46
+
47
+ assertEquals (expected , actual );
50
48
}
51
- }
49
+ }
0 commit comments