فهرست منبع

fix: metric: BigDecimal > 64bit

Ranides Atterwim 10 سال پیش
والد
کامیت
6c319bcaca

+ 3 - 5
assira/src/main/java/net/ranides/assira/text/FormatNumber.java

@@ -189,10 +189,12 @@ public final class FormatNumber {
     static class UnitPrefix {
         
         private final double v;
+        private final BigDecimal dv;
         private final String u;
 
         UnitPrefix(double v, String u) {
             this.v = v;
+            this.dv = BigDecimal.valueOf(v);
             this.u = u;
         }
 
@@ -200,12 +202,8 @@ public final class FormatNumber {
             return v;
         }
         
-        public BigInteger intValue() {
-            return BigInteger.valueOf((long)v);
-        }
-        
         public BigDecimal decimalValue() {
-            return BigDecimal.valueOf(v);
+            return dv;
         }
 
         public String text() {

+ 2 - 17
assira/src/main/java/net/ranides/assira/text/ResolveDialect.java

@@ -429,7 +429,7 @@ public final class ResolveDialect {
                 if(value.bitLength()<64) {
                     vappend(target, value.longValue());
                 } else {
-                    vappend(target, value);
+                    vappend(target, new BigDecimal(value));
                 }
                 return;
             }
@@ -483,22 +483,7 @@ public final class ResolveDialect {
             }
             target.append(nf.format(value/c.doubleValue())).append(c.text());
         }
-        
-        private void vappend(Appendable target, BigInteger value) throws IOException {
-            if(value.signum() < 0) {
-                target.append('-');
-                value = value.negate();
-            }
-            UnitPrefix c = units[0];
-            for (UnitPrefix u : units) {
-                c = u;
-                if( value.compareTo(c.intValue()) > 0) {
-                    break;
-                }
-            }
-            target.append(nf.format(value.divide(c.intValue()).doubleValue())).append(c.text());
-        }
-        
+
         private void vappend(Appendable target, BigDecimal value) throws IOException {
             if(value.signum() < 0) {
                 target.append('-');

+ 59 - 23
assira/src/test/java/net/ranides/assira/text/ResolveFormatTest.java

@@ -17,13 +17,56 @@ import java.util.regex.Pattern;
 import net.ranides.assira.generic.HashUtils;
 import net.ranides.assira.generic.SerializationUtils;
 import org.junit.Test;
-import static org.junit.Assert.*;
+import static net.ranides.assira.junit.NewAssert.*;
 
 /**
  *
  * @author Ranides Atterwim <ranides@gmail.com>
  */
+@SuppressWarnings("PMD.AvoidDuplicateLiterals")
 public class ResolveFormatTest {
+    
+    @Test
+    public void testSerialization() throws IOException {
+        ResolveFormat a = ResolveFormat.compile("{x} {y} {z}");
+        ResolveFormat b = SerializationUtils.copy(a);
+        
+        assertNotSame(a, b);
+        assertEquals(a, b);
+        
+        assertEquals("5 6 7", b.format(new A(5,6,7)));
+    }
+    
+    @Test
+    public void testBasics() throws IOException {
+        ResolveFormat a = ResolveFormat.compile("{x} {y} {z}");
+        ResolveFormat b = ResolveFormat.compile("US","{x} {y} {z}");
+        
+        assertEquals("{x} {y} {z}", a.pattern());
+        assertEquals("{x} {y} {z}", b.pattern());
+        
+        assertEquals(ResolveDialect.getDefault(), a.dialect());
+        assertEquals(ResolveDialect.getInstance("US"), b.dialect());
+    }
+    
+    @Test
+    public void testEquals() {
+        ResolveFormat a = ResolveFormat.compile("PL", "{x} {y} {z}");
+        ResolveFormat b = ResolveFormat.compile("PL", "{x} {y} {z}");
+        ResolveFormat c = ResolveFormat.compile("PL", "{x} {y} {r}");
+        ResolveFormat d = ResolveFormat.compile("US", "{x} {y} {z}");
+        ResolveFormat e = ResolveFormat.compile("US", "{x} {y} {r}");
+        String f = "Hello";
+        
+        assertSymNotEquals(a, null);
+        assertSymNotEquals(a, c);
+        assertSymNotEquals(a, d);
+        assertSymNotEquals(a, e);
+        assertSymNotEquals(a, f);
+        
+        assertSymEquals(a, a);
+        assertSymEquals(a, b);
+    }
 
     @Test
     public void testFormat_Number() {
@@ -158,6 +201,17 @@ public class ResolveFormatTest {
         assertEquals("-1.5E3", ResolveFormat.compile("{bdvalue,metric/e,#.0}").format(ITEM));
     }
     
+    @Test
+    public void testFormat_Metric_BigInteger_128() {
+        assertEquals("-1.542Y", ResolveFormat.compile("{value128,metric}").format(ITEM));
+        assertEquals("-1.542Y", ResolveFormat.compile("{value128,metric/si}").format(ITEM));
+        assertEquals("-1.542E24", ResolveFormat.compile("{value128,metric/e}").format(ITEM));
+        
+        assertEquals("-1.5Y", ResolveFormat.compile("{value128,metric,#.0}").format(ITEM));
+        assertEquals("-1.5Y", ResolveFormat.compile("{value128,metric/si,#.0}").format(ITEM));
+        assertEquals("-1.5E24", ResolveFormat.compile("{value128,metric/e,#.0}").format(ITEM));
+    }
+    
     @Test
     public void testFormat_Metric_BigDecimal() {
         assertEquals("-154.2k", ResolveFormat.compile("{bivalue,metric}").format(ITEM));
@@ -221,17 +275,7 @@ public class ResolveFormatTest {
         assertEquals("-150.6KK", ResolveFormat.compile("{bivalue,binary/knuth,#.0}").format(ITEM));
         assertEquals("-150.6B3", ResolveFormat.compile("{bivalue,binary/e,#.0}").format(ITEM));
     }
-    
-    @Test
-    public void testFormat_Pattern_Array() {
-        String r1 = ResolveUtils.format("Hello {1} world {2}{3}!", new int[]{17, 19, 47, 33});
-        String r2 = ResolveUtils.vformat("Hello {1} world {2}{3}!", 17, 19, 47, 33);
-        String r3 = ResolveUtils.vformat("Hello {0} world!", 17);
-        assertEquals("Hello 19 world 4733!", r1);
-        assertEquals("Hello 19 world 4733!", r2);
-        assertEquals("Hello 17 world!", r3);
-    }
-    
+        
     @Test
     public void testFormat_Pattern_Matcher() {
         String source ="AH-ello";
@@ -256,17 +300,6 @@ public class ResolveFormatTest {
         assertEquals("Hello 77 t world ?", r2);
     }
     
-    @Test
-    public void testSerialization() throws IOException {
-        ResolveFormat a = ResolveFormat.compile("{x} {y} {z}");
-        ResolveFormat b = SerializationUtils.copy(a);
-        
-        assertNotSame(a, b);
-        assertEquals(a, b);
-        
-        assertEquals("5 6 7", b.format(new A(5,6,7)));
-    }
-    
     private static final class Wrapper {
         public Object[] items;
 
@@ -343,5 +376,8 @@ public class ResolveFormatTest {
         
         public BigDecimal bdvalue = BigDecimal.valueOf(-1542.50);
         
+        public BigInteger value128 = new BigInteger("-1542000000000000000000000");
+        
     };
+
 }

+ 44 - 0
assira/src/test/java/net/ranides/assira/text/ResolveUtilsTest.java

@@ -0,0 +1,44 @@
+/*
+ * @author Ranides Atterwim <ranides@gmail.com>
+ * @copyright Ranides Atterwim
+ * @license WTFPL
+ * @url http://ranides.net/projects/assira
+ */
+package net.ranides.assira.text;
+
+import java.io.IOException;
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+/**
+ *
+ * @author Ranides Atterwim <ranides@gmail.com>
+ */
+public class ResolveUtilsTest {
+    
+    @Test
+    public void testFormat_Pattern_Array() {
+        String r1 = ResolveUtils.format("Hello {1} world {2}{3}!", new int[]{17, 19, 47, 33});
+        String r2 = ResolveUtils.vformat("Hello {1} world {2}{3}!", 17, 19, 47, 33);
+        String r3 = ResolveUtils.vformat("Hello {0} world!", 17);
+        
+        assertEquals("Hello 19 world 4733!", r1);
+        assertEquals("Hello 19 world 4733!", r2);
+        assertEquals("Hello 17 world!", r3);
+    }
+    
+    @Test
+    public void testFormat_Pattern_Array_Append() throws IOException {
+        StringBuilder r1 = new StringBuilder().append("r1 = ");
+        StringBuilder r2 = new StringBuilder().append("r2 = ");
+        StringBuilder r3 = new StringBuilder().append("r3 = ");
+        ResolveUtils.format(r1, "Hello {1} world {2}{3}!", new int[]{17, 19, 47, 33});
+        ResolveUtils.vformat(r2, "Hello {1} world {2}{3}!", 17, 19, 47, 33);
+        ResolveUtils.vformat(r3, "Hello {0} world!", 17);
+        
+        assertEquals("r1 = Hello 19 world 4733!", r1.toString());
+        assertEquals("r2 = Hello 19 world 4733!", r2.toString());
+        assertEquals("r3 = Hello 17 world!", r3.toString());
+    }
+    
+}