Skip to content

Commit 80a3aa8

Browse files
authored
Add tests for CombinationSum algorithm
This test class covers various scenarios for the CombinationSum algorithm, including edge cases and multiple combinations.
1 parent 4e30321 commit 80a3aa8

File tree

1 file changed

+145
-0
lines changed

1 file changed

+145
-0
lines changed
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package com.thealgorithms.recursion;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import java.util.Arrays;
6+
import java.util.List;
7+
import org.junit.jupiter.api.Test;
8+
9+
/**
10+
* Comprehensive test class for CombinationSum algorithm
11+
* Tests various scenarios including edge cases
12+
*/
13+
class CombinationSumTest {
14+
15+
@Test
16+
void testBasicCase() {
17+
CombinationSum cs = new CombinationSum();
18+
int[] candidates = {2, 3, 6, 7};
19+
int target = 7;
20+
List<List<Integer>> result = cs.combinationSum(candidates, target);
21+
22+
assertTrue(result.contains(Arrays.asList(2, 2, 3)));
23+
assertTrue(result.contains(Arrays.asList(7)));
24+
assertEquals(2, result.size());
25+
}
26+
27+
@Test
28+
void testMultipleCombinations() {
29+
CombinationSum cs = new CombinationSum();
30+
int[] candidates = {2, 3, 5};
31+
int target = 8;
32+
List<List<Integer>> result = cs.combinationSum(candidates, target);
33+
34+
assertTrue(result.contains(Arrays.asList(2, 2, 2, 2)));
35+
assertTrue(result.contains(Arrays.asList(2, 3, 3)));
36+
assertTrue(result.contains(Arrays.asList(3, 5)));
37+
assertEquals(3, result.size());
38+
}
39+
40+
@Test
41+
void testNoSolution() {
42+
CombinationSum cs = new CombinationSum();
43+
int[] candidates = {2};
44+
int target = 1;
45+
List<List<Integer>> result = cs.combinationSum(candidates, target);
46+
47+
assertTrue(result.isEmpty());
48+
}
49+
50+
@Test
51+
void testSingleElement() {
52+
CombinationSum cs = new CombinationSum();
53+
int[] candidates = {1};
54+
int target = 1;
55+
List<List<Integer>> result = cs.combinationSum(candidates, target);
56+
57+
assertEquals(1, result.size());
58+
assertTrue(result.contains(Arrays.asList(1)));
59+
}
60+
61+
@Test
62+
void testSingleElementRepeated() {
63+
CombinationSum cs = new CombinationSum();
64+
int[] candidates = {2};
65+
int target = 8;
66+
List<List<Integer>> result = cs.combinationSum(candidates, target);
67+
68+
assertEquals(1, result.size());
69+
assertTrue(result.contains(Arrays.asList(2, 2, 2, 2)));
70+
}
71+
72+
@Test
73+
void testLargerNumbers() {
74+
CombinationSum cs = new CombinationSum();
75+
int[] candidates = {10, 1, 2, 7, 6, 1, 5};
76+
int target = 8;
77+
List<List<Integer>> result = cs.combinationSum(candidates, target);
78+
79+
assertFalse(result.isEmpty());
80+
// Verify all combinations sum to target
81+
for (List<Integer> combination : result) {
82+
int sum = combination.stream().mapToInt(Integer::intValue).sum();
83+
assertEquals(target, sum);
84+
}
85+
}
86+
87+
@Test
88+
void testTargetZero() {
89+
CombinationSum cs = new CombinationSum();
90+
int[] candidates = {1, 2, 3};
91+
int target = 0;
92+
List<List<Integer>> result = cs.combinationSum(candidates, target);
93+
94+
// Should return empty list in the combination
95+
assertEquals(1, result.size());
96+
assertTrue(result.get(0).isEmpty());
97+
}
98+
99+
@Test
100+
void testEmptyCandidates() {
101+
CombinationSum cs = new CombinationSum();
102+
int[] candidates = {};
103+
int target = 5;
104+
List<List<Integer>> result = cs.combinationSum(candidates, target);
105+
106+
assertTrue(result.isEmpty());
107+
}
108+
109+
@Test
110+
void testLargeTarget() {
111+
CombinationSum cs = new CombinationSum();
112+
int[] candidates = {3, 5, 8};
113+
int target = 11;
114+
List<List<Integer>> result = cs.combinationSum(candidates, target);
115+
116+
assertTrue(result.contains(Arrays.asList(3, 3, 5)));
117+
assertTrue(result.contains(Arrays.asList(3, 8)));
118+
119+
// Verify all combinations sum to target
120+
for (List<Integer> combination : result) {
121+
int sum = combination.stream().mapToInt(Integer::intValue).sum();
122+
assertEquals(target, sum);
123+
}
124+
}
125+
126+
@Test
127+
void testAllCombinationsValid() {
128+
CombinationSum cs = new CombinationSum();
129+
int[] candidates = {2, 3, 6, 7};
130+
int target = 7;
131+
List<List<Integer>> result = cs.combinationSum(candidates, target);
132+
133+
// Verify each combination sums to target
134+
for (List<Integer> combination : result) {
135+
int sum = 0;
136+
for (int num : combination) {
137+
sum += num;
138+
}
139+
assertEquals(target, sum, "Each combination should sum to target");
140+
}
141+
142+
// Verify no duplicates in result
143+
assertEquals(result.size(), result.stream().distinct().count());
144+
}
145+
}

0 commit comments

Comments
 (0)