Skip to content

Commit

Permalink
Added more edge and corner cases around converting floating point val…
Browse files Browse the repository at this point in the history
…ues to byte.
  • Loading branch information
jdereg committed Feb 4, 2024
1 parent 5fbf693 commit 34f9483
Showing 1 changed file with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,25 @@ class ConverterEverythingTest
});
TEST_FACTORY.put(pair(Float.class, Byte.class), new Object[][] {
{ -1f, (byte)-1 },
{ -1.99f, (byte)-1 },
{ -1.1f, (byte)-1 },
{ 0f, (byte) 0 },
{ 1f, (byte) 1 },
{ 1.1f, (byte) 1 },
{ 1.999f, (byte) 1 },
{ -128f, Byte.MIN_VALUE },
{ 127f, Byte.MAX_VALUE },
{ -129f, (byte) 127 }, // verify wrap around
{ 128f, (byte) -128 } // verify wrap around
});
TEST_FACTORY.put(pair(Double.class, Byte.class), new Object[][] {
{ -1d, (byte) -1 },
{ -1.99d, (byte)-1 },
{ -1.1d, (byte)-1 },
{ 0d, (byte) 0 },
{ 1d, (byte) 1 },
{ 1.1d, (byte) 1 },
{ 1.999d, (byte) 1 },
{ -128d, Byte.MIN_VALUE },
{ 127d, Byte.MAX_VALUE },
{-129d, (byte) 127 }, // verify wrap around
Expand Down Expand Up @@ -148,8 +156,12 @@ class ConverterEverythingTest
});
TEST_FACTORY.put(pair(BigDecimal.class, Byte.class), new Object[][] {
{ new BigDecimal("-1"), (byte) -1 },
{ new BigDecimal("-1.1"), (byte) -1 },
{ new BigDecimal("-1.9"), (byte) -1 },
{ new BigDecimal("0"), (byte) 0 },
{ new BigDecimal("1"), (byte) 1 },
{ new BigDecimal("1.1"), (byte) 1 },
{ new BigDecimal("1.9"), (byte) 1 },
{ new BigDecimal("-128"), Byte.MIN_VALUE },
{ new BigDecimal("127"), Byte.MAX_VALUE },
{ new BigDecimal("-129"), (byte)127 },
Expand All @@ -172,7 +184,7 @@ class ConverterEverythingTest
{ mapOf("_v", "1"), (byte) 1 },
{ mapOf("_v", 1), (byte) 1 },
{ mapOf("value", "1"), (byte) 1 },
{ mapOf("value", 1L), (byte) 1 },
{ mapOf("value", 1d), (byte) 1 },

{ mapOf("_v","-128"), Byte.MIN_VALUE },
{ mapOf("_v",-128), Byte.MIN_VALUE },
Expand All @@ -195,11 +207,17 @@ class ConverterEverythingTest
{ mapOf("value", 128L), (byte) -128 },
});
TEST_FACTORY.put(pair(String.class, Byte.class), new Object[][] {
{ "-1", (byte)-1 },
{ "0", (byte)0 },
{ "1", (byte)1 },
{ "-1", (byte) -1 },
{ "-1.1", (byte) -1 },
{ "-1.9", (byte) -1 },
{ "0", (byte) 0 },
{ "1", (byte) 1 },
{ "1.1", (byte) 1 },
{ "1.9", (byte) 1 },
{ "-128", (byte)-128 },
{ "127", (byte)127 },
{ "", (byte)0 },
{ "crapola", new IllegalArgumentException("Value: crapola not parseable as a byte value or outside -128 to 127")},
{ "-129", new IllegalArgumentException("-129 not parseable as a byte value or outside -128 to 127") },
{ "128", new IllegalArgumentException("128 not parseable as a byte value or outside -128 to 127") },
});
Expand Down

0 comments on commit 34f9483

Please sign in to comment.