Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,36 +1,40 @@
package com.thealgorithms.recursion;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertIterableEquals;

import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

public final class GenerateSubsetsTest {

@Test
void subsetRecursionTestOne() {
String str = "abc";
String[] expected = new String[] {"abc", "ab", "ac", "a", "bc", "b", "c", ""};

List<String> ans = GenerateSubsets.subsetRecursion(str);
assertArrayEquals(ans.toArray(), expected);
@DisplayName("Subsets of 'abc'")
void testSubsetsOfABC() {
assertSubsets("abc", Arrays.asList("abc", "ab", "ac", "a", "bc", "b", "c", ""));
}

@Test
void subsetRecursionTestTwo() {
String str = "cbf";
String[] expected = new String[] {"cbf", "cb", "cf", "c", "bf", "b", "f", ""};
@DisplayName("Subsets of 'cbf'")
void testSubsetsOfCBF() {
assertSubsets("cbf", Arrays.asList("cbf", "cb", "cf", "c", "bf", "b", "f", ""));
}

List<String> ans = GenerateSubsets.subsetRecursion(str);
assertArrayEquals(ans.toArray(), expected);
@Test
@DisplayName("Subsets of 'aba' with duplicates")
void testSubsetsWithDuplicateChars() {
assertSubsets("aba", Arrays.asList("aba", "ab", "aa", "a", "ba", "b", "a", ""));
}

@Test
void subsetRecursionTestThree() {
String str = "aba";
String[] expected = new String[] {"aba", "ab", "aa", "a", "ba", "b", "a", ""};
@DisplayName("Subsets of empty string")
void testEmptyInput() {
assertSubsets("", List.of(""));
}

List<String> ans = GenerateSubsets.subsetRecursion(str);
assertArrayEquals(ans.toArray(), expected);
private void assertSubsets(String input, Iterable<String> expected) {
List<String> actual = GenerateSubsets.subsetRecursion(input);
assertIterableEquals(expected, actual, "Subsets do not match for input: " + input);
}
}