Skip to content

Commit

Permalink
Accept that scalable can be empty + throw exception if a percentage i…
Browse files Browse the repository at this point in the history
…s undefined in ProportionalScalable (#2026)

Signed-off-by: VEDELAGO MIORA <miora.ralambotiana@rte-france.com>
  • Loading branch information
miovd authored Mar 22, 2022
1 parent 43db044 commit 632c9b9
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ private static void checkPercentages(List<Float> percentages, List<Scalable> sca
if (scalables.size() != percentages.size()) {
throw new IllegalArgumentException("percentage and scalable list must have the same size");
}
if (scalables.isEmpty()) {
return;
}
if (percentages.stream().anyMatch(p -> Float.isNaN(p))) {
throw new IllegalArgumentException("There is at least one undefined percentage");
}
double sum = percentages.stream().mapToDouble(Double::valueOf).sum();
if (Math.abs(100 - sum) > EPSILON) {
throw new IllegalArgumentException(String.format("Sum of percentages must be equals to 100 (%.2f)", sum));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ public void testInitialValue() {
assertEquals(0., Scalable.proportional(Arrays.asList(70.f, 30.f), Arrays.asList(g1, l1)).initialValue(network), 1e-3);

testInvalidProportionalScalable(Collections.singletonList(100.0f), Collections.emptyList());
testInvalidProportionalScalable(Collections.emptyList(), Collections.emptyList());
testInvalidProportionalScalable(Arrays.asList(70.f, 20.f), Arrays.asList(g1, l1));
}

Expand Down

0 comments on commit 632c9b9

Please sign in to comment.