Adding class for generating all subsequences from a given List (#5194)

* Adding class for generating all subsequences from a given List

* Fix test data format

* Fix braces wrong placement

* Fix "Utility classes should not have a public or default constructor."

* Fix checkstyle " Class Subsequence should be declared as final."

* Renaming class Subsequence to SubsequenceFinder. Refactored test to Parametrized test. Fixed input parameter as final.

* Fix formatting

* Fix formatting

* Fix formatting

* Fix import ordering

* Renaming method generate all.
Renaming test method.
Adding duplication test.
Renaming TestData to TestCase.

* Fix formatting

* style: add assertion to avoid potential infinite loop

---------

Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com>
This commit is contained in:
Alex K
2024-05-30 21:43:15 +03:00
committed by GitHub
parent a6e873deef
commit 2568b96784
2 changed files with 82 additions and 0 deletions

View File

@ -0,0 +1,28 @@
package com.thealgorithms.backtracking;
import static org.junit.jupiter.api.Assertions.assertIterableEquals;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
public class SubsequenceFinderTest {
@ParameterizedTest
@MethodSource("getTestCases")
void testGenerateAll(TestCase testData) {
final var actual = SubsequenceFinder.generateAll(testData.input());
assertIterableEquals(testData.expected(), actual);
}
static Stream<TestCase> getTestCases() {
return Stream.of(new TestCase(new ArrayList<>(), List.of(List.of())), new TestCase(List.of(1, 2), List.of(List.of(), List.of(2), List.of(1), List.of(1, 2))),
new TestCase(List.of("A", "B", "C"), List.of(List.of(), List.of("C"), List.of("B"), List.of("B", "C"), List.of("A"), List.of("A", "C"), List.of("A", "B"), List.of("A", "B", "C"))),
new TestCase(List.of(1, 2, 3), List.of(List.of(), List.of(3), List.of(2), List.of(2, 3), List.of(1), List.of(1, 3), List.of(1, 2), List.of(1, 2, 3))), new TestCase(List.of(2, 2), List.of(List.of(), List.of(2), List.of(2), List.of(2, 2))));
}
record TestCase(List<Object> input, List<List<Object>> expected) {
}
}