commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pste...@apache.org
Subject svn commit: r411647 - in /jakarta/commons/proper/math/trunk: src/java/org/apache/commons/math/fraction/ProperFractionFormat.java src/test/org/apache/commons/math/fraction/FractionFormatTest.java xdocs/changes.xml
Date Mon, 05 Jun 2006 03:06:52 GMT
Author: psteitz
Date: Sun Jun  4 20:06:52 2006
New Revision: 411647

URL: http://svn.apache.org/viewvc?rev=411647&view=rev
Log:
Modified ProperFractionFormat to reject embedded minus signs.
JIRA: MATH-60
Reported by Nhung Nnguyen

Modified:
    jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/fraction/ProperFractionFormat.java
    jakarta/commons/proper/math/trunk/src/test/org/apache/commons/math/fraction/FractionFormatTest.java
    jakarta/commons/proper/math/trunk/xdocs/changes.xml

Modified: jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/fraction/ProperFractionFormat.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/fraction/ProperFractionFormat.java?rev=411647&r1=411646&r2=411647&view=diff
==============================================================================
--- jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/fraction/ProperFractionFormat.java
(original)
+++ jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/fraction/ProperFractionFormat.java
Sun Jun  4 20:06:52 2006
@@ -24,6 +24,10 @@
 /**
  * Formats a Fraction number in proper format.  The number format for each of
  * the whole number, numerator and, denominator can be configured.
+ * <p>
+ * Minus signs are only allowed in the whole number part - i.e.,
+ * "-3 1/2" is legitimate and denotes -7/2, but "-3 -1/2" is invalid and
+ * will result in a <code>ParseException</code>.
  * 
  * @since 1.1
  * @version $Revision$ $Date$
@@ -114,6 +118,11 @@
     /**
      * Parses a string to produce a {@link Fraction} object.  This method
      * expects the string to be formatted as a proper fraction.
+     * <p>
+     * Minus signs are only allowed in the whole number part - i.e.,
+     * "-3 1/2" is legitimate and denotes -7/2, but "-3 -1/2" is invalid and
+     * will result in a <code>ParseException</code>.
+     * 
      * @param source the string to parse
      * @param pos input/ouput parsing parameter.
      * @return the parsed {@link Fraction} object.
@@ -152,6 +161,12 @@
             pos.setIndex(initialIndex);
             return null;
         }
+        
+        if (num.intValue() < 0) {
+            // minus signs should be leading, invalid expression
+            pos.setIndex(initialIndex);
+            return null;
+        }
 
         // parse '/'
         int startIndex = pos.getIndex();
@@ -182,6 +197,12 @@
             // invalid integer number
             // set index back to initial, error index should already be set
             // character examined.
+            pos.setIndex(initialIndex);
+            return null;
+        }
+        
+        if (den.intValue() < 0) {
+            // minus signs must be leading, invalid
             pos.setIndex(initialIndex);
             return null;
         }

Modified: jakarta/commons/proper/math/trunk/src/test/org/apache/commons/math/fraction/FractionFormatTest.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/math/trunk/src/test/org/apache/commons/math/fraction/FractionFormatTest.java?rev=411647&r1=411646&r2=411647&view=diff
==============================================================================
--- jakarta/commons/proper/math/trunk/src/test/org/apache/commons/math/fraction/FractionFormatTest.java
(original)
+++ jakarta/commons/proper/math/trunk/src/test/org/apache/commons/math/fraction/FractionFormatTest.java
Sun Jun  4 20:06:52 2006
@@ -229,6 +229,23 @@
         }
     }
     
+    public void testParseProperInvalidMinus() {
+        String source = "2 -2 / 3";
+        try {
+            Fraction c = properFormat.parse(source);
+            fail("invalid minus in improper fraction.");
+        } catch (ParseException ex) {
+            // expected
+        }
+        source = "2 2 / -3";
+        try {
+            Fraction c = properFormat.parse(source);
+            fail("invalid minus in improper fraction.");
+        } catch (ParseException ex) {
+            // expected
+        }
+    }
+    
     public void testNumeratorFormat() {
     	NumberFormat old = properFormat.getNumeratorFormat();
         NumberFormat nf = NumberFormat.getInstance();

Modified: jakarta/commons/proper/math/trunk/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/math/trunk/xdocs/changes.xml?rev=411647&r1=411646&r2=411647&view=diff
==============================================================================
--- jakarta/commons/proper/math/trunk/xdocs/changes.xml (original)
+++ jakarta/commons/proper/math/trunk/xdocs/changes.xml Sun Jun  4 20:06:52 2006
@@ -49,6 +49,9 @@
       <action dev="psteitz" type="update" issue="MATH-148" due-to="Joni Salonen">
         Added QR Decomposition.
       </action>
+      <action dev="psteitz" type="fix" issue="MATH-60" due-to="Nhung Nnguyen">
+        Modified ProperFractionFormat to reject embedded minus signs.
+      </action>
     </release>
     <release version="1.1" date="2005-12-17"  
  description="This is a maintenance release containing bug fixes and enhancements.



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message