|
1 | 1 | package com.thealgorithms.backtracking;
|
2 | 2 |
|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
| 4 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
4 | 5 |
|
| 6 | +import com.thealgorithms.maths.BinomialCoefficient; |
5 | 7 | import java.util.ArrayList;
|
6 | 8 | import java.util.List;
|
7 |
| -import org.junit.jupiter.api.Test; |
| 9 | +import java.util.stream.Stream; |
| 10 | +import org.junit.jupiter.params.ParameterizedTest; |
| 11 | +import org.junit.jupiter.params.provider.Arguments; |
| 12 | +import org.junit.jupiter.params.provider.MethodSource; |
8 | 13 |
|
9 | 14 | public class ArrayCombinationTest {
|
10 |
| - |
11 |
| - @Test |
12 |
| - public void testCombination() { |
13 |
| - // Test case 1: n = 4, k = 2 |
14 |
| - List<List<Integer>> expected = new ArrayList<>(); |
15 |
| - expected.add(List.of(1, 2)); |
16 |
| - expected.add(List.of(1, 3)); |
17 |
| - expected.add(List.of(1, 4)); |
18 |
| - expected.add(List.of(2, 3)); |
19 |
| - expected.add(List.of(2, 4)); |
20 |
| - expected.add(List.of(3, 4)); |
21 |
| - |
22 |
| - List<List<Integer>> actual = ArrayCombination.combination(4, 2); |
23 |
| - |
24 |
| - assertEquals(expected, actual); |
| 15 | + @ParameterizedTest |
| 16 | + @MethodSource("regularInputs") |
| 17 | + void testCombination(int n, int k, List<List<Integer>> expected) { |
| 18 | + assertEquals(expected.size(), BinomialCoefficient.binomialCoefficient(n, k)); |
| 19 | + assertEquals(expected, ArrayCombination.combination(n, k)); |
25 | 20 | }
|
26 | 21 |
|
27 |
| - @Test |
28 |
| - public void testEmptyCombination() { |
29 |
| - // Test case 2: n = 4, k = 0 (invalid input) |
30 |
| - List<List<Integer>> expected = new ArrayList<>(); |
31 |
| - List<List<Integer>> actual = ArrayCombination.combination(4, 0); |
32 |
| - |
33 |
| - assertEquals(expected, actual); |
| 22 | + @ParameterizedTest |
| 23 | + @MethodSource("wrongInputs") |
| 24 | + void testCombinationThrows(int n, int k) { |
| 25 | + assertThrows(IllegalArgumentException.class, () -> ArrayCombination.combination(n, k)); |
34 | 26 | }
|
35 | 27 |
|
36 |
| - @Test |
37 |
| - public void testSingleElementCombinations() { |
38 |
| - // Test case 3: n = 3, k = 1 |
39 |
| - List<List<Integer>> expected = new ArrayList<>(); |
40 |
| - expected.add(List.of(1)); |
41 |
| - expected.add(List.of(2)); |
42 |
| - expected.add(List.of(3)); |
43 |
| - |
44 |
| - List<List<Integer>> actual = ArrayCombination.combination(3, 1); |
| 28 | + private static Stream<Arguments> regularInputs() { |
| 29 | + return Stream.of(Arguments.of(0, 0, List.of(new ArrayList<Integer>())), Arguments.of(1, 0, List.of(new ArrayList<Integer>())), Arguments.of(1, 1, List.of(List.of(0))), Arguments.of(3, 0, List.of(new ArrayList<Integer>())), Arguments.of(3, 1, List.of(List.of(0), List.of(1), List.of(2))), |
| 30 | + Arguments.of(4, 2, List.of(List.of(0, 1), List.of(0, 2), List.of(0, 3), List.of(1, 2), List.of(1, 3), List.of(2, 3)))); |
| 31 | + } |
45 | 32 |
|
46 |
| - assertEquals(expected, actual); |
| 33 | + private static Stream<Arguments> wrongInputs() { |
| 34 | + return Stream.of(Arguments.of(-1, 0), Arguments.of(0, -1), Arguments.of(2, 100)); |
47 | 35 | }
|
48 | 36 | }
|
0 commit comments