Fix Decima64.valueOf(String) 65/97465/1
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 12 Sep 2021 17:31:32 +0000 (19:31 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 13 Sep 2021 03:03:29 +0000 (05:03 +0200)
When we have a maximum-length string we end up reporting running out of
fraction limit. Fix this by correctly accounting for the period.

JIRA: YANGTOOLS-1321
Change-Id: I40b70400004d39a923b536db8b47784d3659bd4a
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit 5d46adb7935241534c69b00a13593f3e8d4ec7d6)

yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/Decimal64.java
yang/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/Decimal64Test.java

index 78354f391594807493c9ff3d191ca9265c75b13c..c649f015c854b612adb4e342fcc18e75a9a25483 100644 (file)
@@ -112,7 +112,7 @@ public class Decimal64 extends Number implements CanonicalValue<Decimal64> {
                 limit--;
             }
 
-            final int fracLimit = MAX_FRACTION_DIGITS - intLen;
+            final int fracLimit = MAX_FRACTION_DIGITS - intLen + 1;
             byte fracLen = 0;
             long fracPart = 0;
             for (; idx <= limit; idx++, fracLen++) {
index a4963125777ac7a5d164fcaace3555cfad61e791..e2b01871d7909a840348c9a68692a094f84f20c9 100644 (file)
@@ -96,6 +96,15 @@ public class Decimal64Test {
         Decimal64.valueOf("0.12345678901234568");
     }
 
+    @Test
+    public void testFractionLimits() {
+        Decimal64.valueOf("922337203685477580.7");
+        Decimal64.valueOf("9.223372036854775807");
+
+        assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("922337203685477580.71"));
+        assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("9.2233720368547758071"));
+    }
+
     @Test
     public void testParseTooLongString() {
         assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("1234567890123456789"));
@@ -103,7 +112,7 @@ public class Decimal64Test {
 
     @Test
     public void testParseTooLongDecimal() {
-        assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("0.123456789012345689"));
+        assertThrows(NumberFormatException.class, () -> Decimal64.valueOf("0.1234567890123456789"));
     }
 
     @Test