Skip to content

Commit

Permalink
Merge pull request #647 from KingOfSquares/range-check
Browse files Browse the repository at this point in the history
util: Add a range check for HSVLike
  • Loading branch information
kezz committed Dec 21, 2021
2 parents 2bc0e73 + ecb224a commit 6d6e083
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
10 changes: 10 additions & 0 deletions api/src/main/java/net/kyori/adventure/util/HSVLikeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ final class HSVLikeImpl implements HSVLike {
private final float v;

HSVLikeImpl(final float h, final float s, final float v) {
requireInsideRange(h, "h");
requireInsideRange(s, "s");
requireInsideRange(v, "v");
this.h = h;
this.s = s;
this.v = v;
Expand All @@ -53,6 +56,13 @@ public float v() {
return this.v;
}

private static void requireInsideRange(final float number, final String name) throws IllegalArgumentException {
if (number < 0 || 1 < number) {
throw new IllegalArgumentException(
name + " (" + number + ")" + " is not inside the required range: [0,1]");
}
}

@Override
public boolean equals(final @Nullable Object other) {
if (this == other) return true;
Expand Down
14 changes: 14 additions & 0 deletions api/src/test/java/net/kyori/adventure/util/HSVLikeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

class HSVLikeTest {
@Test
Expand Down Expand Up @@ -64,4 +66,16 @@ private static float[] roundFloats(final float @NotNull [] floats) {
}
return result;
}

@Test
void testRange() {
assertThrows(IllegalArgumentException.class, () -> HSVLike.of(2, 1, 1));
assertThrows(IllegalArgumentException.class, () -> HSVLike.of(1, -1, 1));
assertThrows(IllegalArgumentException.class, () -> HSVLike.of(1, 1, 2));
assertDoesNotThrow(() -> {
HSVLike.of(1, 1, 1);
HSVLike.of(0, 0, 0);
HSVLike.of(0.5f, 0.5f, 0.5f);
});
}
}

0 comments on commit 6d6e083

Please sign in to comment.