-
Notifications
You must be signed in to change notification settings - Fork 19.7k
/
Copy pathCombinationTest.java
51 lines (43 loc) · 1.69 KB
/
CombinationTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com.thealgorithms.backtracking;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import org.junit.jupiter.api.Test;
public class CombinationTest {
@Test
void testNegativeElement() {
Integer[] array = {1, 2};
assertThrows(IllegalArgumentException.class, () -> { Combination.combination(array, -1); });
}
@Test
void testNoElement() {
List<TreeSet<Integer>> result = Combination.combination(new Integer[] {1, 2}, 0);
assertNotNull(result);
assertEquals(0, result.size());
}
@Test
void testLengthOne() {
List<TreeSet<Integer>> result = Combination.combination(new Integer[] {1, 2}, 1);
assertTrue(result.get(0).iterator().next() == 1);
assertTrue(result.get(1).iterator().next() == 2);
}
@Test
void testLengthTwo() {
List<TreeSet<Integer>> result = Combination.combination(new Integer[] {1, 2}, 2);
Integer[] arr = result.get(0).toArray(new Integer[2]);
assertTrue(arr[0] == 1);
assertTrue(arr[1] == 2);
}
@Test
void testCombinationsWithStrings() {
List<TreeSet<String>> result = Combination.combination(new String[] {"a", "b", "c"}, 2);
assertEquals(3, result.size());
assertTrue(result.contains(new TreeSet<>(Set.of("a", "b"))));
assertTrue(result.contains(new TreeSet<>(Set.of("a", "c"))));
assertTrue(result.contains(new TreeSet<>(Set.of("b", "c"))));
}
}