|
7 | 7 | import org.slf4j.LoggerFactory; |
8 | 8 |
|
9 | 9 | import io.github.mikeddavydov.jpwise.core.Combination; |
| 10 | +import io.github.mikeddavydov.jpwise.core.CombinationTable; |
10 | 11 | import io.github.mikeddavydov.jpwise.core.EquivalencePartition; |
11 | 12 | import io.github.mikeddavydov.jpwise.core.GenerationAlgorithm; |
12 | | -import io.github.mikeddavydov.jpwise.core.TestGenerator; |
| 13 | +import io.github.mikeddavydov.jpwise.core.TestInput; |
13 | 14 | import io.github.mikeddavydov.jpwise.core.TestParameter; |
14 | 15 |
|
15 | 16 | /** |
16 | | - * Implements a full combinatorial test case generation algorithm. This |
17 | | - * algorithm generates all |
18 | | - * possible combinations of parameter values while respecting compatibility |
19 | | - * rules between values. |
20 | | - * |
| 17 | + * Algorithm that generates all possible combinations of parameter values. |
21 | 18 | * <p> |
22 | | - * Unlike the pairwise algorithm that only covers pairs of values, this |
23 | | - * algorithm generates every |
24 | | - * possible combination, which provides complete coverage but results in a much |
25 | | - * larger number of |
26 | | - * test cases. |
27 | | - * |
| 19 | + * This algorithm generates the complete set of possible combinations by |
| 20 | + * taking the cartesian product of all parameter values. It then filters |
| 21 | + * out combinations that violate compatibility rules. |
28 | 22 | * <p> |
29 | | - * The number of test cases grows exponentially with the number of parameters |
30 | | - * and their values. |
31 | | - * For example, with 3 parameters having 3 values each, this could generate up |
32 | | - * to 27 test cases |
33 | | - * (3^3). |
34 | | - * |
35 | | - * <p> |
36 | | - * Example usage: |
37 | | - * |
38 | | - * <pre> |
39 | | - * TestInput input = new TestInput(); |
40 | | - * input.add(new TestParameter("browser", browserValues)); |
41 | | - * input.add(new TestParameter("os", osValues)); |
42 | | - * |
43 | | - * TestGenerator generator = new TestGenerator(input); |
44 | | - * generator.generate(new CombinatorialAlgorithm(), 99); // 99 indicates full coverage |
45 | | - * CombinationTable results = generator.result(); |
46 | | - * </pre> |
47 | | - * |
48 | | - * <p> |
49 | | - * Use this algorithm when: |
50 | | - * |
51 | | - * <ul> |
52 | | - * <li>Complete test coverage is required |
53 | | - * <li>The number of parameters and values is small |
54 | | - * <li>You need to verify all possible interactions |
55 | | - * </ul> |
56 | | - * |
57 | | - * @author DavydovMD |
58 | | - * @see GenerationAlgorithm |
59 | | - * @see TestGenerator |
| 23 | + * Note that this algorithm can generate a very large number of combinations |
| 24 | + * if there are many parameters or values per parameter. Use with caution. |
60 | 25 | */ |
61 | 26 | public class CombinatorialAlgorithm extends GenerationAlgorithm { |
62 | | - private static final Logger logger = LoggerFactory.getLogger(CombinatorialAlgorithm.class); |
| 27 | + private static final Logger logger = LoggerFactory.getLogger( |
| 28 | + CombinatorialAlgorithm.class); |
| 29 | + private int limit = Integer.MAX_VALUE; |
63 | 30 |
|
64 | | - /** Creates a new combinatorial algorithm instance. */ |
65 | 31 | public CombinatorialAlgorithm() { |
66 | 32 | super(); |
67 | | - logger.debug("Created new CombinatorialAlgorithm instance"); |
68 | 33 | } |
69 | 34 |
|
70 | 35 | /** |
71 | | - * Generates all possible combinations of parameter values. The algorithm builds |
72 | | - * combinations |
73 | | - * recursively, checking compatibility rules at each step to avoid generating |
74 | | - * invalid |
75 | | - * combinations. |
76 | | - * |
77 | | - * @param testGenerator The test generator containing input parameters |
78 | | - * @param limit The maximum number of combinations to generate |
| 36 | + * Constructs a CombinatorialAlgorithm with a specific limit. |
| 37 | + * This limit can be overridden by the one passed to the generate method. |
| 38 | + * |
| 39 | + * @param limit The maximum number of combinations to generate. |
79 | 40 | */ |
80 | | - @Override |
81 | | - public void generate(TestGenerator testGenerator, int limit) { |
82 | | - logger.info("Starting combinatorial test generation with limit {}", limit); |
83 | | - |
84 | | - List<TestParameter> parameters = testGenerator.input().getTestParameters(); |
85 | | - if (parameters.isEmpty()) { |
86 | | - logger.warn("No parameters provided for test generation"); |
87 | | - return; |
88 | | - } |
89 | | - |
90 | | - List<Combination> results = new ArrayList<>(); |
91 | | - Combination current = new Combination(parameters.size()); |
92 | | - |
93 | | - generateCombinationsRecursive(parameters, 0, current, results, limit); |
94 | | - |
95 | | - // Add all valid combinations to the result table |
96 | | - for (Combination combination : results) { |
97 | | - if (combination.isFilled()) { |
98 | | - testGenerator.result().add(combination); |
99 | | - } |
| 41 | + public CombinatorialAlgorithm(int limit) { |
| 42 | + super(); |
| 43 | + if (limit <= 0) { |
| 44 | + throw new IllegalArgumentException("Limit must be positive."); |
100 | 45 | } |
101 | | - |
102 | | - logger.info( |
103 | | - "Generated {} valid combinations out of {} possible combinations", |
104 | | - results.size(), |
105 | | - calculateTotalCombinations(parameters)); |
| 46 | + this.limit = limit; |
106 | 47 | } |
107 | 48 |
|
108 | | - /** |
109 | | - * Thoroughly checks if all values in a combination are compatible with each |
110 | | - * other. |
111 | | - * This method checks all possible combinations of values, not just pairs. |
112 | | - * It is slower but more accurate for complex compatibility rules. |
113 | | - * |
114 | | - * @param combination The combination to check |
115 | | - * @return true if all values are compatible, false otherwise |
116 | | - */ |
117 | | - private boolean checkCombinationThoroughly(Combination combination) { |
118 | | - if (combination == null) { |
119 | | - logger.warn("Combination is null, skipping compatibility check"); |
120 | | - return true; |
121 | | - } |
122 | | - |
123 | | - EquivalencePartition[] values = combination.getValues(); |
124 | | - if (values == null || values.length < 2) { |
125 | | - return true; |
126 | | - } |
127 | | - |
128 | | - // Check all possible combinations of values |
129 | | - for (int i = 0; i < values.length; i++) { |
130 | | - if (values[i] == null) { |
131 | | - continue; |
132 | | - } |
| 49 | + @Override |
| 50 | + public CombinationTable generate(TestInput input, int nWiseOrLimit) { |
| 51 | + // The nWiseOrLimit parameter for CombinatorialAlgorithm is the actual limit. |
| 52 | + final int effectiveLimit = (nWiseOrLimit > 0) ? nWiseOrLimit : this.limit; |
| 53 | + logger.info("Generating all possible combinations for {} parameters, limit: {}", |
| 54 | + input.getTestParameters().size(), effectiveLimit); |
133 | 55 |
|
134 | | - // Check compatibility with all other values |
135 | | - for (int j = 0; j < values.length; j++) { |
136 | | - if (i == j || values[j] == null) { |
137 | | - continue; |
138 | | - } |
| 56 | + List<Combination> combinations = new ArrayList<>(); |
| 57 | + List<TestParameter> parameters = input.getTestParameters(); |
139 | 58 |
|
140 | | - // Check compatibility in both directions |
141 | | - if (!values[i].isCompatibleWith(values[j]) || !values[j].isCompatibleWith(values[i])) { |
142 | | - logger.debug("Found incompatible values: {} and {}", values[i], values[j]); |
143 | | - return false; |
144 | | - } |
145 | | - } |
146 | | - } |
| 59 | + Combination current = new Combination(parameters); |
| 60 | + // Pass effectiveLimit to the recursive helper |
| 61 | + generateCombinationsRecursive(combinations, current, parameters, 0, effectiveLimit); |
147 | 62 |
|
148 | | - return true; |
| 63 | + logger.info("Generated {} combinations (limit was {})", combinations.size(), effectiveLimit); |
| 64 | + return new CombinationTable(combinations); |
149 | 65 | } |
150 | 66 |
|
151 | | - private void generateCombinationsRecursive( |
152 | | - List<TestParameter> parameters, |
153 | | - int currentIndex, |
154 | | - Combination current, |
155 | | - List<Combination> results, |
156 | | - int limit) { |
157 | | - if (currentIndex >= parameters.size()) { |
158 | | - if (current.isFilled() && checkCombinationThoroughly(current)) { |
159 | | - results.add(new Combination(current)); |
160 | | - if (results.size() >= limit) { |
161 | | - return; |
162 | | - } |
| 67 | + private void generateCombinationsRecursive(List<Combination> combinations, Combination current, |
| 68 | + List<TestParameter> parameters, int index, final int effectiveLimit) { |
| 69 | + if (index == parameters.size()) { |
| 70 | + if (combinations.size() >= effectiveLimit) { |
| 71 | + return; |
| 72 | + } |
| 73 | + if (isValidCombination(current)) { |
| 74 | + combinations.add(new Combination(current)); |
163 | 75 | } |
164 | 76 | return; |
165 | 77 | } |
166 | 78 |
|
167 | | - TestParameter parameter = parameters.get(currentIndex); |
168 | | - if (parameter == null || parameter.getPartitions().isEmpty()) { |
169 | | - logger.warn("Parameter at index {} is null or has no partitions", currentIndex); |
| 79 | + if (combinations.size() >= effectiveLimit) { |
170 | 80 | return; |
171 | 81 | } |
172 | 82 |
|
| 83 | + TestParameter parameter = parameters.get(index); |
173 | 84 | for (EquivalencePartition partition : parameter.getPartitions()) { |
174 | | - if (partition == null) { |
175 | | - logger.warn("Null partition found in parameter {}", parameter.getName()); |
176 | | - continue; |
177 | | - } |
178 | | - |
179 | | - current.setValue(currentIndex, partition); |
180 | | - if (checkCombinationThoroughly(current)) { |
181 | | - generateCombinationsRecursive(parameters, currentIndex + 1, current, results, limit); |
182 | | - if (results.size() >= limit) { |
183 | | - return; |
184 | | - } |
| 85 | + current.setValue(index, partition); |
| 86 | + generateCombinationsRecursive(combinations, current, parameters, index + 1, effectiveLimit); |
| 87 | + if (combinations.size() >= effectiveLimit) { |
| 88 | + return; |
185 | 89 | } |
186 | 90 | } |
187 | 91 | } |
188 | | - |
189 | | - private long calculateTotalCombinations(List<TestParameter> parameters) { |
190 | | - long total = 1; |
191 | | - for (TestParameter parameter : parameters) { |
192 | | - total *= parameter.getPartitions().size(); |
193 | | - } |
194 | | - return total; |
195 | | - } |
196 | 92 | } |
0 commit comments