test: add some more

This commit is contained in:
Moritz Böhme 2024-07-02 11:08:28 +02:00
parent fa48438183
commit 6f2b3842cd
2 changed files with 80 additions and 10 deletions

View file

@ -1,12 +1,47 @@
package WardrobeBuilder;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class WardrobeBuilder {
public static List<List<Integer>> allCombinations(Set<Integer> elememts, int target) {
List<Integer> wardrobe = List.of(50);
return List.of(wardrobe);
Set<Integer> elements;
int target;
public WardrobeBuilder(Set<Integer> elements, int target){
this.elements = elements;
this.target = target;
}
public Set<List<Integer>> allCombinations() {
Set<List<Integer>> combinations = completeCombination(new ArrayList<>());
Set<List<Integer>> newCombinations;
for (Integer element : elements) {
newCombinations = new HashSet<>();
for (List<Integer> combination : combinations) {
newCombinations.addAll(completeCombination(combination));
}
combinations = newCombinations;
}
return combinations;
}
private Set<List<Integer>> completeCombination(List<Integer> combination) {
Integer currentSum = combination.stream().reduce(0, Integer::sum);
if (currentSum == target) {
return Set.of(combination);
}
Set<List<Integer>> combinations = new HashSet<>();
for (Integer element : elements) {
if (currentSum + element <= target) {
List<Integer> newCombination = new ArrayList<>(combination);
newCombination.add(element);
newCombination.sort(Integer::compareTo);
combinations.add(newCombination);
}
}
return combinations;
}
}

View file

@ -10,17 +10,52 @@ class MainTest {
@org.junit.jupiter.api.Test
void onlyOneWardrobeFromOneElement() {
Set<Integer> elememts = Set.of(50);
List<List<Integer>> wardrobes = WardrobeBuilder.allCombinations(elememts, 50);
WardrobeBuilder wardrobeBuilder = new WardrobeBuilder(elememts, 50);
Set<List<Integer>> wardrobes = wardrobeBuilder.allCombinations();
assertEquals(1, wardrobes.size());
}
@org.junit.jupiter.api.Test
void sameHeightFromOneElement() {
Set<Integer> elememts = Set.of(50);
List<List<Integer>> wardrobes = WardrobeBuilder.allCombinations(elememts, 50);
List<Integer> wardrobe = wardrobes.getFirst();
void targetHeightFromOneElement() {
Integer target = 75;
Set<Integer> elememts = Set.of(target);
WardrobeBuilder wardrobeBuilder = new WardrobeBuilder(elememts, target);
Set<List<Integer>> wardrobes = wardrobeBuilder.allCombinations();
List<Integer> wardrobe = wardrobes.iterator().next();
Integer sum = wardrobe.stream().reduce(0, Integer::sum);
assertEquals(target, sum);
}
@org.junit.jupiter.api.Test
void targetHeightSumFromTwoElements() {
Set<Integer> elememts = Set.of(50, 75);
Integer target = 125;
WardrobeBuilder wardrobeBuilder = new WardrobeBuilder(elememts, target);
Set<List<Integer>> wardrobes = wardrobeBuilder.allCombinations();
List<Integer> wardrobe = wardrobes.iterator().next();
assertEquals(2, wardrobe.size());
}
@org.junit.jupiter.api.Test
void noDuplicatesForTwoElements() {
Set<Integer> elememts = Set.of(50, 75);
Integer target = 125;
WardrobeBuilder wardrobeBuilder = new WardrobeBuilder(elememts, target);
Set<List<Integer>> wardrobes = wardrobeBuilder.allCombinations();
assertEquals(1, wardrobes.size());
Integer elememt = wardrobe.getFirst();
assertEquals(50, elememt);
}
@org.junit.jupiter.api.Test
void targetHeightPartialFromTwoElements() {
Set<Integer> elememts = Set.of(50, 75);
Integer target = 50;
WardrobeBuilder wardrobeBuilder = new WardrobeBuilder(elememts, target);
Set<List<Integer>> wardrobes = wardrobeBuilder.allCombinations();
List<Integer> wardrobe = wardrobes.iterator().next();
Integer sum = wardrobe.stream().reduce(0, Integer::sum);
assertEquals(target, sum);
}
}