|
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.assertNull; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
5 | 5 |
|
| 6 | +import com.thealgorithms.maths.BinomialCoefficient; |
| 7 | +import java.util.ArrayList; |
6 | 8 | import java.util.List;
|
7 |
| -import java.util.TreeSet; |
8 |
| -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; |
9 | 13 |
|
10 | 14 | public class ArrayCombinationTest {
|
11 |
| - |
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); |
18 |
| - } |
19 |
| - |
20 |
| - @Test |
21 |
| - void testNoLengthElement() { |
22 |
| - List<TreeSet<Integer>> result = ArrayCombination.combination(2, 0); |
23 |
| - assertNull(result); |
| 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)); |
24 | 20 | }
|
25 | 21 |
|
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()); |
| 22 | + @ParameterizedTest |
| 23 | + @MethodSource("wrongInputs") |
| 24 | + void testCombinationThrows(int n, int k) { |
| 25 | + assertThrows(IllegalArgumentException.class, () -> ArrayCombination.combination(n, k)); |
32 | 26 | }
|
33 | 27 |
|
34 |
| - @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]); |
| 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)))); |
41 | 31 | }
|
42 | 32 |
|
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]); |
| 33 | + private static Stream<Arguments> wrongInputs() { |
| 34 | + return Stream.of(Arguments.of(-1, 0), Arguments.of(0, -1), Arguments.of(2, 100)); |
50 | 35 | }
|
51 | 36 | }
|
0 commit comments