Repository: commons-numbers
Updated Branches:
  refs/heads/multimodule 69d3b6231 -> 763ec4fea


http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/aaef6f7a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/FractionFormatTest.java
----------------------------------------------------------------------
diff --git 
a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/FractionFormatTest.java
 
b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/FractionFormatTest.java
new file mode 100644
index 0000000..2c9a398
--- /dev/null
+++ 
b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/FractionFormatTest.java
@@ -0,0 +1,349 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.numbers.fraction;
+
+import java.text.NumberFormat;
+import java.util.Locale;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+
+public class FractionFormatTest {
+
+    FractionFormat properFormat = null;
+    FractionFormat improperFormat = null;
+
+    protected Locale getLocale() {
+        return Locale.getDefault();
+    }
+
+    @Before
+    public void setUp() {
+        properFormat = FractionFormat.getProperInstance(getLocale());
+        improperFormat = FractionFormat.getImproperInstance(getLocale());
+    }
+
+    @Test
+    public void testFormat() {
+        Fraction c = new Fraction(1, 2);
+        String expected = "1 / 2";
+
+        String actual = properFormat.format(c);
+        Assert.assertEquals(expected, actual);
+
+        actual = improperFormat.format(c);
+        Assert.assertEquals(expected, actual);
+    }
+
+    @Test
+    public void testFormatNegative() {
+        Fraction c = new Fraction(-1, 2);
+        String expected = "-1 / 2";
+
+        String actual = properFormat.format(c);
+        Assert.assertEquals(expected, actual);
+
+        actual = improperFormat.format(c);
+        Assert.assertEquals(expected, actual);
+    }
+
+    @Test
+    public void testFormatZero() {
+        Fraction c = new Fraction(0, 1);
+        String expected = "0 / 1";
+
+        String actual = properFormat.format(c);
+        Assert.assertEquals(expected, actual);
+
+        actual = improperFormat.format(c);
+        Assert.assertEquals(expected, actual);
+    }
+
+    @Test
+    public void testFormatImproper() {
+        Fraction c = new Fraction(5, 3);
+
+        String actual = properFormat.format(c);
+        Assert.assertEquals("1 2 / 3", actual);
+
+        actual = improperFormat.format(c);
+        Assert.assertEquals("5 / 3", actual);
+    }
+
+    @Test
+    public void testFormatImproperNegative() {
+        Fraction c = new Fraction(-5, 3);
+
+        String actual = properFormat.format(c);
+        Assert.assertEquals("-1 2 / 3", actual);
+
+        actual = improperFormat.format(c);
+        Assert.assertEquals("-5 / 3", actual);
+    }
+
+    @Test
+    public void testParse() {
+        String source = "1 / 2";
+
+        try {
+            Fraction c = properFormat.parse(source);
+            Assert.assertNotNull(c);
+            Assert.assertEquals(1, c.getNumerator());
+            Assert.assertEquals(2, c.getDenominator());
+
+            c = improperFormat.parse(source);
+            Assert.assertNotNull(c);
+            Assert.assertEquals(1, c.getNumerator());
+            Assert.assertEquals(2, c.getDenominator());
+        } catch (FractionParseException ex) {
+            Assert.fail(ex.getMessage());
+        }
+    }
+
+    @Test
+    public void testParseInteger() {
+        String source = "10";
+        {
+            Fraction c = properFormat.parse(source);
+            Assert.assertNotNull(c);
+            Assert.assertEquals(10, c.getNumerator());
+            Assert.assertEquals(1, c.getDenominator());
+        }
+        {
+            Fraction c = improperFormat.parse(source);
+            Assert.assertNotNull(c);
+            Assert.assertEquals(10, c.getNumerator());
+            Assert.assertEquals(1, c.getDenominator());
+        }
+    }
+
+    @Test
+    public void testParseOne1() {
+        String source = "1 / 1";
+        Fraction c = properFormat.parse(source);
+        Assert.assertNotNull(c);
+        Assert.assertEquals(1, c.getNumerator());
+        Assert.assertEquals(1, c.getDenominator());
+    }
+
+    @Test
+    public void testParseOne2() {
+        String source = "10 / 10";
+        Fraction c = properFormat.parse(source);
+        Assert.assertNotNull(c);
+        Assert.assertEquals(1, c.getNumerator());
+        Assert.assertEquals(1, c.getDenominator());
+    }
+
+    @Test
+    public void testParseZero1() {
+        String source = "0 / 1";
+        Fraction c = properFormat.parse(source);
+        Assert.assertNotNull(c);
+        Assert.assertEquals(0, c.getNumerator());
+        Assert.assertEquals(1, c.getDenominator());
+    }
+
+    @Test
+    public void testParseZero2() {
+        String source = "-0 / 1";
+        Fraction c = properFormat.parse(source);
+        Assert.assertNotNull(c);
+        Assert.assertEquals(0, c.getNumerator());
+        Assert.assertEquals(1, c.getDenominator());
+        // This test shows that the sign is not preserved.
+        Assert.assertEquals(Double.POSITIVE_INFINITY, 1d / c.doubleValue(), 0);
+    }
+
+    @Test
+    public void testParseInvalid() {
+        String source = "a";
+        String msg = "should not be able to parse '10 / a'.";
+        try {
+            properFormat.parse(source);
+            Assert.fail(msg);
+        } catch (FractionParseException ex) {
+            // success
+        }
+        try {
+            improperFormat.parse(source);
+            Assert.fail(msg);
+        } catch (FractionParseException ex) {
+            // success
+        }
+    }
+
+    @Test
+    public void testParseInvalidDenominator() {
+        String source = "10 / a";
+        String msg = "should not be able to parse '10 / a'.";
+        try {
+            properFormat.parse(source);
+            Assert.fail(msg);
+        } catch (FractionParseException ex) {
+            // success
+        }
+        try {
+            improperFormat.parse(source);
+            Assert.fail(msg);
+        } catch (FractionParseException ex) {
+            // success
+        }
+    }
+
+    @Test
+    public void testParseNegative() {
+
+        {
+            String source = "-1 / 2";
+            Fraction c = properFormat.parse(source);
+            Assert.assertNotNull(c);
+            Assert.assertEquals(-1, c.getNumerator());
+            Assert.assertEquals(2, c.getDenominator());
+
+            c = improperFormat.parse(source);
+            Assert.assertNotNull(c);
+            Assert.assertEquals(-1, c.getNumerator());
+            Assert.assertEquals(2, c.getDenominator());
+
+            source = "1 / -2";
+            c = properFormat.parse(source);
+            Assert.assertNotNull(c);
+            Assert.assertEquals(-1, c.getNumerator());
+            Assert.assertEquals(2, c.getDenominator());
+
+            c = improperFormat.parse(source);
+            Assert.assertNotNull(c);
+            Assert.assertEquals(-1, c.getNumerator());
+            Assert.assertEquals(2, c.getDenominator());
+        }
+    }
+
+    @Test
+    public void testParseProper() {
+        String source = "1 2 / 3";
+
+        {
+            Fraction c = properFormat.parse(source);
+            Assert.assertNotNull(c);
+            Assert.assertEquals(5, c.getNumerator());
+            Assert.assertEquals(3, c.getDenominator());
+        }
+
+        try {
+            improperFormat.parse(source);
+            Assert.fail("invalid improper fraction.");
+        } catch (FractionParseException ex) {
+            // success
+        }
+    }
+
+    @Test
+    public void testParseProperNegative() {
+        String source = "-1 2 / 3";
+        {
+            Fraction c = properFormat.parse(source);
+            Assert.assertNotNull(c);
+            Assert.assertEquals(-5, c.getNumerator());
+            Assert.assertEquals(3, c.getDenominator());
+        }
+
+        try {
+            improperFormat.parse(source);
+            Assert.fail("invalid improper fraction.");
+        } catch (FractionParseException ex) {
+            // success
+        }
+    }
+
+    @Test
+    public void testParseProperInvalidMinus() {
+        String source = "2 -2 / 3";
+        try {
+            properFormat.parse(source);
+            Assert.fail("invalid minus in improper fraction.");
+        } catch (FractionParseException ex) {
+            // expected
+        }
+        source = "2 2 / -3";
+        try {
+            properFormat.parse(source);
+            Assert.fail("invalid minus in improper fraction.");
+        } catch (FractionParseException ex) {
+            // expected
+        }
+    }
+
+    @Test
+    public void testNumeratorFormat() {
+        NumberFormat old = properFormat.getNumeratorFormat();
+        NumberFormat nf = NumberFormat.getInstance();
+        nf.setParseIntegerOnly(true);
+        properFormat.setNumeratorFormat(nf);
+        Assert.assertEquals(nf, properFormat.getNumeratorFormat());
+        properFormat.setNumeratorFormat(old);
+
+        old = improperFormat.getNumeratorFormat();
+        nf = NumberFormat.getInstance();
+        nf.setParseIntegerOnly(true);
+        improperFormat.setNumeratorFormat(nf);
+        Assert.assertEquals(nf, improperFormat.getNumeratorFormat());
+        improperFormat.setNumeratorFormat(old);
+    }
+
+    @Test
+    public void testDenominatorFormat() {
+        NumberFormat old = properFormat.getDenominatorFormat();
+        NumberFormat nf = NumberFormat.getInstance();
+        nf.setParseIntegerOnly(true);
+        properFormat.setDenominatorFormat(nf);
+        Assert.assertEquals(nf, properFormat.getDenominatorFormat());
+        properFormat.setDenominatorFormat(old);
+
+        old = improperFormat.getDenominatorFormat();
+        nf = NumberFormat.getInstance();
+        nf.setParseIntegerOnly(true);
+        improperFormat.setDenominatorFormat(nf);
+        Assert.assertEquals(nf, improperFormat.getDenominatorFormat());
+        improperFormat.setDenominatorFormat(old);
+    }
+
+    @Test
+    public void testWholeFormat() {
+        ProperFractionFormat format = (ProperFractionFormat)properFormat;
+
+        NumberFormat old = format.getWholeFormat();
+        NumberFormat nf = NumberFormat.getInstance();
+        nf.setParseIntegerOnly(true);
+        format.setWholeFormat(nf);
+        Assert.assertEquals(nf, format.getWholeFormat());
+        format.setWholeFormat(old);
+    }
+
+    @Test
+    public void testLongFormat() {
+        Assert.assertEquals("10 / 1", improperFormat.format(10l));
+    }
+
+    @Test
+    public void testDoubleFormat() {
+        Assert.assertEquals("355 / 113", improperFormat.format(Math.PI));
+    }
+}

http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/aaef6f7a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/FractionTest.java
----------------------------------------------------------------------
diff --git 
a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/FractionTest.java
 
b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/FractionTest.java
new file mode 100644
index 0000000..3b2c4cb
--- /dev/null
+++ 
b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/FractionTest.java
@@ -0,0 +1,625 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.numbers.fraction;
+
+import org.apache.commons.numbers.core.NumbersArithmeticException;
+import org.apache.commons.numbers.core.TestUtils;
+import org.junit.Assert;
+import org.junit.Test;
+
+
+/**
+ */
+public class FractionTest {
+
+    private void assertFraction(int expectedNumerator, int 
expectedDenominator, Fraction actual) {
+        Assert.assertEquals(expectedNumerator, actual.getNumerator());
+        Assert.assertEquals(expectedDenominator, actual.getDenominator());
+    }
+
+    @Test
+    public void testConstructor() {
+        assertFraction(0, 1, new Fraction(0, 1));
+        assertFraction(0, 1, new Fraction(0, 2));
+        assertFraction(0, 1, new Fraction(0, -1));
+        assertFraction(1, 2, new Fraction(1, 2));
+        assertFraction(1, 2, new Fraction(2, 4));
+        assertFraction(-1, 2, new Fraction(-1, 2));
+        assertFraction(-1, 2, new Fraction(1, -2));
+        assertFraction(-1, 2, new Fraction(-2, 4));
+        assertFraction(-1, 2, new Fraction(2, -4));
+
+        // overflow
+        try {
+            new Fraction(Integer.MIN_VALUE, -1);
+            Assert.fail();
+        } catch (NumbersArithmeticException ex) {
+            // success
+        }
+        try {
+            new Fraction(1, Integer.MIN_VALUE);
+            Assert.fail();
+        } catch (NumbersArithmeticException ex) {
+            // success
+        }
+
+        assertFraction(0, 1, new Fraction(0.00000000000001));
+        assertFraction(2, 5, new Fraction(0.40000000000001));
+        assertFraction(15, 1, new Fraction(15.0000000000001));
+    }
+
+    @Test(expected=FractionConversionException.class)
+    public void testGoldenRatio() {
+        // the golden ratio is notoriously a difficult number for continuous 
fraction
+        new Fraction((1 + Math.sqrt(5)) / 2, 1.0e-12, 25);
+    }
+
+    // MATH-179
+    @Test
+    public void testDoubleConstructor() throws Exception  {
+        assertFraction(1, 2, new Fraction((double)1 / (double)2));
+        assertFraction(1, 3, new Fraction((double)1 / (double)3));
+        assertFraction(2, 3, new Fraction((double)2 / (double)3));
+        assertFraction(1, 4, new Fraction((double)1 / (double)4));
+        assertFraction(3, 4, new Fraction((double)3 / (double)4));
+        assertFraction(1, 5, new Fraction((double)1 / (double)5));
+        assertFraction(2, 5, new Fraction((double)2 / (double)5));
+        assertFraction(3, 5, new Fraction((double)3 / (double)5));
+        assertFraction(4, 5, new Fraction((double)4 / (double)5));
+        assertFraction(1, 6, new Fraction((double)1 / (double)6));
+        assertFraction(5, 6, new Fraction((double)5 / (double)6));
+        assertFraction(1, 7, new Fraction((double)1 / (double)7));
+        assertFraction(2, 7, new Fraction((double)2 / (double)7));
+        assertFraction(3, 7, new Fraction((double)3 / (double)7));
+        assertFraction(4, 7, new Fraction((double)4 / (double)7));
+        assertFraction(5, 7, new Fraction((double)5 / (double)7));
+        assertFraction(6, 7, new Fraction((double)6 / (double)7));
+        assertFraction(1, 8, new Fraction((double)1 / (double)8));
+        assertFraction(3, 8, new Fraction((double)3 / (double)8));
+        assertFraction(5, 8, new Fraction((double)5 / (double)8));
+        assertFraction(7, 8, new Fraction((double)7 / (double)8));
+        assertFraction(1, 9, new Fraction((double)1 / (double)9));
+        assertFraction(2, 9, new Fraction((double)2 / (double)9));
+        assertFraction(4, 9, new Fraction((double)4 / (double)9));
+        assertFraction(5, 9, new Fraction((double)5 / (double)9));
+        assertFraction(7, 9, new Fraction((double)7 / (double)9));
+        assertFraction(8, 9, new Fraction((double)8 / (double)9));
+        assertFraction(1, 10, new Fraction((double)1 / (double)10));
+        assertFraction(3, 10, new Fraction((double)3 / (double)10));
+        assertFraction(7, 10, new Fraction((double)7 / (double)10));
+        assertFraction(9, 10, new Fraction((double)9 / (double)10));
+        assertFraction(1, 11, new Fraction((double)1 / (double)11));
+        assertFraction(2, 11, new Fraction((double)2 / (double)11));
+        assertFraction(3, 11, new Fraction((double)3 / (double)11));
+        assertFraction(4, 11, new Fraction((double)4 / (double)11));
+        assertFraction(5, 11, new Fraction((double)5 / (double)11));
+        assertFraction(6, 11, new Fraction((double)6 / (double)11));
+        assertFraction(7, 11, new Fraction((double)7 / (double)11));
+        assertFraction(8, 11, new Fraction((double)8 / (double)11));
+        assertFraction(9, 11, new Fraction((double)9 / (double)11));
+        assertFraction(10, 11, new Fraction((double)10 / (double)11));
+    }
+
+    // MATH-181
+    @Test
+    public void testDigitLimitConstructor() throws Exception  {
+        assertFraction(2, 5, new Fraction(0.4,   9));
+        assertFraction(2, 5, new Fraction(0.4,  99));
+        assertFraction(2, 5, new Fraction(0.4, 999));
+
+        assertFraction(3, 5,      new Fraction(0.6152,    9));
+        assertFraction(8, 13,     new Fraction(0.6152,   99));
+        assertFraction(510, 829,  new Fraction(0.6152,  999));
+        assertFraction(769, 1250, new Fraction(0.6152, 9999));
+
+        // MATH-996
+        assertFraction(1, 2, new Fraction(0.5000000001, 10));
+    }
+
+    @Test
+    public void testIntegerOverflow() {
+        checkIntegerOverflow(0.75000000001455192);
+        checkIntegerOverflow(1.0e10);
+        checkIntegerOverflow(-1.0e10);
+        checkIntegerOverflow(-43979.60679604749);
+    }
+
+    private void checkIntegerOverflow(double a) {
+        try {
+            @SuppressWarnings("unused")
+            Fraction f = new Fraction(a, 1.0e-12, 1000);
+            //System.out.println(f.getNumerator() + "/" + f.getDenominator());
+            Assert.fail("an exception should have been thrown");
+        } catch (FractionConversionException ce) {
+            // expected behavior
+        }
+    }
+
+    @Test
+    public void testEpsilonLimitConstructor() throws Exception  {
+        assertFraction(2, 5, new Fraction(0.4, 1.0e-5, 100));
+
+        assertFraction(3, 5,      new Fraction(0.6152, 0.02, 100));
+        assertFraction(8, 13,     new Fraction(0.6152, 1.0e-3, 100));
+        assertFraction(251, 408,  new Fraction(0.6152, 1.0e-4, 100));
+        assertFraction(251, 408,  new Fraction(0.6152, 1.0e-5, 100));
+        assertFraction(510, 829,  new Fraction(0.6152, 1.0e-6, 100));
+        assertFraction(769, 1250, new Fraction(0.6152, 1.0e-7, 100));
+    }
+
+    @Test
+    public void testCompareTo() {
+        Fraction first = new Fraction(1, 2);
+        Fraction second = new Fraction(1, 3);
+        Fraction third = new Fraction(1, 2);
+
+        Assert.assertEquals(0, first.compareTo(first));
+        Assert.assertEquals(0, first.compareTo(third));
+        Assert.assertEquals(1, first.compareTo(second));
+        Assert.assertEquals(-1, second.compareTo(first));
+
+        // these two values are different approximations of PI
+        // the first  one is approximately PI - 3.07e-18
+        // the second one is approximately PI + 1.936e-17
+        Fraction pi1 = new Fraction(1068966896, 340262731);
+        Fraction pi2 = new Fraction( 411557987, 131002976);
+        Assert.assertEquals(-1, pi1.compareTo(pi2));
+        Assert.assertEquals( 1, pi2.compareTo(pi1));
+        Assert.assertEquals(0.0, pi1.doubleValue() - pi2.doubleValue(), 
1.0e-20);
+    }
+
+    @Test
+    public void testDoubleValue() {
+        Fraction first = new Fraction(1, 2);
+        Fraction second = new Fraction(1, 3);
+
+        Assert.assertEquals(0.5, first.doubleValue(), 0.0);
+        Assert.assertEquals(1.0 / 3.0, second.doubleValue(), 0.0);
+    }
+
+    @Test
+    public void testFloatValue() {
+        Fraction first = new Fraction(1, 2);
+        Fraction second = new Fraction(1, 3);
+
+        Assert.assertEquals(0.5f, first.floatValue(), 0.0f);
+        Assert.assertEquals((float)(1.0 / 3.0), second.floatValue(), 0.0f);
+    }
+
+    @Test
+    public void testIntValue() {
+        Fraction first = new Fraction(1, 2);
+        Fraction second = new Fraction(3, 2);
+
+        Assert.assertEquals(0, first.intValue());
+        Assert.assertEquals(1, second.intValue());
+    }
+
+    @Test
+    public void testLongValue() {
+        Fraction first = new Fraction(1, 2);
+        Fraction second = new Fraction(3, 2);
+
+        Assert.assertEquals(0L, first.longValue());
+        Assert.assertEquals(1L, second.longValue());
+    }
+
+    @Test
+    public void testConstructorDouble() {
+        assertFraction(1, 2, new Fraction(0.5));
+        assertFraction(1, 3, new Fraction(1.0 / 3.0));
+        assertFraction(17, 100, new Fraction(17.0 / 100.0));
+        assertFraction(317, 100, new Fraction(317.0 / 100.0));
+        assertFraction(-1, 2, new Fraction(-0.5));
+        assertFraction(-1, 3, new Fraction(-1.0 / 3.0));
+        assertFraction(-17, 100, new Fraction(17.0 / -100.0));
+        assertFraction(-317, 100, new Fraction(-317.0 / 100.0));
+    }
+
+    @Test
+    public void testAbs() {
+        Fraction a = new Fraction(10, 21);
+        Fraction b = new Fraction(-10, 21);
+        Fraction c = new Fraction(10, -21);
+
+        assertFraction(10, 21, a.abs());
+        assertFraction(10, 21, b.abs());
+        assertFraction(10, 21, c.abs());
+    }
+
+    @Test
+    public void testPercentage() {
+        Assert.assertEquals(50.0, new Fraction(1, 2).percentageValue(), 
1.0e-15);
+    }
+
+    @Test
+    public void testMath835() {
+        final int numer = Integer.MAX_VALUE / 99;
+        final int denom = 1;
+        final double percentage = 100 * ((double) numer) / denom;
+        final Fraction frac = new Fraction(numer, denom);
+        // With the implementation that preceded the fix suggested in MATH-835,
+        // this test was failing, due to overflow.
+        Assert.assertEquals(percentage, frac.percentageValue(), 
Math.ulp(percentage));
+    }
+
+    @Test
+    public void testMath1261() {
+        final Fraction a = new Fraction(Integer.MAX_VALUE, 2);
+        final Fraction b = a.multiply(2);
+        Assert.assertTrue(b.equals(new Fraction(Integer.MAX_VALUE)));
+
+        final Fraction c = new Fraction(2, Integer.MAX_VALUE);
+        final Fraction d = c.divide(2);
+        Assert.assertTrue(d.equals(new Fraction(1, Integer.MAX_VALUE)));
+    }
+
+    @Test
+    public void testReciprocal() {
+        Fraction f = null;
+
+        f = new Fraction(50, 75);
+        f = f.reciprocal();
+        Assert.assertEquals(3, f.getNumerator());
+        Assert.assertEquals(2, f.getDenominator());
+
+        f = new Fraction(4, 3);
+        f = f.reciprocal();
+        Assert.assertEquals(3, f.getNumerator());
+        Assert.assertEquals(4, f.getDenominator());
+
+        f = new Fraction(-15, 47);
+        f = f.reciprocal();
+        Assert.assertEquals(-47, f.getNumerator());
+        Assert.assertEquals(15, f.getDenominator());
+
+        f = new Fraction(0, 3);
+        try {
+            f = f.reciprocal();
+            Assert.fail("expecting ZeroDenominatorException");
+        } catch (ZeroDenominatorException ex) {}
+
+        // large values
+        f = new Fraction(Integer.MAX_VALUE, 1);
+        f = f.reciprocal();
+        Assert.assertEquals(1, f.getNumerator());
+        Assert.assertEquals(Integer.MAX_VALUE, f.getDenominator());
+    }
+
+    @Test
+    public void testNegate() {
+        Fraction f = null;
+
+        f = new Fraction(50, 75);
+        f = f.negate();
+        Assert.assertEquals(-2, f.getNumerator());
+        Assert.assertEquals(3, f.getDenominator());
+
+        f = new Fraction(-50, 75);
+        f = f.negate();
+        Assert.assertEquals(2, f.getNumerator());
+        Assert.assertEquals(3, f.getDenominator());
+
+        // large values
+        f = new Fraction(Integer.MAX_VALUE-1, Integer.MAX_VALUE);
+        f = f.negate();
+        Assert.assertEquals(Integer.MIN_VALUE+2, f.getNumerator());
+        Assert.assertEquals(Integer.MAX_VALUE, f.getDenominator());
+
+        f = new Fraction(Integer.MIN_VALUE, 1);
+        try {
+            f = f.negate();
+            Assert.fail("expecting NumbersArithmeticException");
+        } catch (NumbersArithmeticException ex) {}
+    }
+
+    @Test
+    public void testAdd() {
+        Fraction a = new Fraction(1, 2);
+        Fraction b = new Fraction(2, 3);
+
+        assertFraction(1, 1, a.add(a));
+        assertFraction(7, 6, a.add(b));
+        assertFraction(7, 6, b.add(a));
+        assertFraction(4, 3, b.add(b));
+
+        Fraction f1 = new Fraction(Integer.MAX_VALUE - 1, 1);
+        Fraction f2 = Fraction.ONE;
+        Fraction f = f1.add(f2);
+        Assert.assertEquals(Integer.MAX_VALUE, f.getNumerator());
+        Assert.assertEquals(1, f.getDenominator());
+        f = f1.add(1);
+        Assert.assertEquals(Integer.MAX_VALUE, f.getNumerator());
+        Assert.assertEquals(1, f.getDenominator());
+
+        f1 = new Fraction(-1, 13*13*2*2);
+        f2 = new Fraction(-2, 13*17*2);
+        f = f1.add(f2);
+        Assert.assertEquals(13*13*17*2*2, f.getDenominator());
+        Assert.assertEquals(-17 - 2*13*2, f.getNumerator());
+
+        try {
+            f.add(null);
+            Assert.fail("expecting NullArgumentException");
+        } catch (NullPointerException ex) {}
+
+        // if this fraction is added naively, it will overflow.
+        // check that it doesn't.
+        f1 = new Fraction(1,32768*3);
+        f2 = new Fraction(1,59049);
+        f = f1.add(f2);
+        Assert.assertEquals(52451, f.getNumerator());
+        Assert.assertEquals(1934917632, f.getDenominator());
+
+        f1 = new Fraction(Integer.MIN_VALUE, 3);
+        f2 = new Fraction(1,3);
+        f = f1.add(f2);
+        Assert.assertEquals(Integer.MIN_VALUE+1, f.getNumerator());
+        Assert.assertEquals(3, f.getDenominator());
+
+        f1 = new Fraction(Integer.MAX_VALUE - 1, 1);
+        f2 = Fraction.ONE;
+        f = f1.add(f2);
+        Assert.assertEquals(Integer.MAX_VALUE, f.getNumerator());
+        Assert.assertEquals(1, f.getDenominator());
+
+        try {
+            f = f.add(Fraction.ONE); // should overflow
+            Assert.fail("expecting NumbersArithmeticException but got: " + 
f.toString());
+        } catch (NumbersArithmeticException ex) {}
+
+        // denominator should not be a multiple of 2 or 3 to trigger overflow
+        f1 = new Fraction(Integer.MIN_VALUE, 5);
+        f2 = new Fraction(-1,5);
+        try {
+            f = f1.add(f2); // should overflow
+            Assert.fail("expecting NumbersArithmeticException but got: " + 
f.toString());
+        } catch (FractionOverflowException ex) {}
+
+        try {
+            f= new Fraction(-Integer.MAX_VALUE, 1);
+            f = f.add(f);
+            Assert.fail("expecting NumbersArithmeticException");
+        } catch (NumbersArithmeticException ex) {}
+
+        try {
+            f= new Fraction(-Integer.MAX_VALUE, 1);
+            f = f.add(f);
+            Assert.fail("expecting NumbersArithmeticException");
+        } catch (NumbersArithmeticException ex) {}
+
+        f1 = new Fraction(3,327680);
+        f2 = new Fraction(2,59049);
+        try {
+            f = f1.add(f2); // should overflow
+            Assert.fail("expecting NumbersArithmeticException but got: " + 
f.toString());
+        } catch (NumbersArithmeticException ex) {}
+    }
+
+    @Test
+    public void testDivide() {
+        Fraction a = new Fraction(1, 2);
+        Fraction b = new Fraction(2, 3);
+
+        assertFraction(1, 1, a.divide(a));
+        assertFraction(3, 4, a.divide(b));
+        assertFraction(4, 3, b.divide(a));
+        assertFraction(1, 1, b.divide(b));
+
+        Fraction f1 = new Fraction(3, 5);
+        Fraction f2 = Fraction.ZERO;
+        try {
+            f1.divide(f2);
+            Assert.fail("expecting FractionException");
+        } catch (FractionException ex) {}
+
+        f1 = new Fraction(0, 5);
+        f2 = new Fraction(2, 7);
+        Fraction f = f1.divide(f2);
+        Assert.assertSame(Fraction.ZERO, f);
+
+        f1 = new Fraction(2, 7);
+        f2 = Fraction.ONE;
+        f = f1.divide(f2);
+        Assert.assertEquals(2, f.getNumerator());
+        Assert.assertEquals(7, f.getDenominator());
+
+        f1 = new Fraction(1, Integer.MAX_VALUE);
+        f = f1.divide(f1);
+        Assert.assertEquals(1, f.getNumerator());
+        Assert.assertEquals(1, f.getDenominator());
+
+        f1 = new Fraction(Integer.MIN_VALUE, Integer.MAX_VALUE);
+        f2 = new Fraction(1, Integer.MAX_VALUE);
+        f = f1.divide(f2);
+        Assert.assertEquals(Integer.MIN_VALUE, f.getNumerator());
+        Assert.assertEquals(1, f.getDenominator());
+
+        try {
+            f.divide(null);
+            Assert.fail("NullArgumentException");
+        } catch (NullPointerException ex) {}
+
+        try {
+            f1 = new Fraction(1, Integer.MAX_VALUE);
+            f = f1.divide(f1.reciprocal());  // should overflow
+            Assert.fail("expecting NumbersArithmeticException");
+        } catch (NumbersArithmeticException ex) {}
+        try {
+            f1 = new Fraction(1, -Integer.MAX_VALUE);
+            f = f1.divide(f1.reciprocal());  // should overflow
+            Assert.fail("expecting NumbersArithmeticException");
+        } catch (NumbersArithmeticException ex) {}
+
+        f1 = new Fraction(6, 35);
+        f  = f1.divide(15);
+        Assert.assertEquals(2, f.getNumerator());
+        Assert.assertEquals(175, f.getDenominator());
+
+    }
+
+    @Test
+    public void testMultiply() {
+        Fraction a = new Fraction(1, 2);
+        Fraction b = new Fraction(2, 3);
+
+        assertFraction(1, 4, a.multiply(a));
+        assertFraction(1, 3, a.multiply(b));
+        assertFraction(1, 3, b.multiply(a));
+        assertFraction(4, 9, b.multiply(b));
+
+        Fraction f1 = new Fraction(Integer.MAX_VALUE, 1);
+        Fraction f2 = new Fraction(Integer.MIN_VALUE, Integer.MAX_VALUE);
+        Fraction f = f1.multiply(f2);
+        Assert.assertEquals(Integer.MIN_VALUE, f.getNumerator());
+        Assert.assertEquals(1, f.getDenominator());
+
+        try {
+            f.multiply(null);
+            Assert.fail("expecting NullArgumentException");
+        } catch (NullPointerException ex) {}
+
+        f1 = new Fraction(6, 35);
+        f  = f1.multiply(15);
+        Assert.assertEquals(18, f.getNumerator());
+        Assert.assertEquals(7, f.getDenominator());
+    }
+
+    @Test
+    public void testSubtract() {
+        Fraction a = new Fraction(1, 2);
+        Fraction b = new Fraction(2, 3);
+
+        assertFraction(0, 1, a.subtract(a));
+        assertFraction(-1, 6, a.subtract(b));
+        assertFraction(1, 6, b.subtract(a));
+        assertFraction(0, 1, b.subtract(b));
+
+        Fraction f = new Fraction(1,1);
+        try {
+            f.subtract(null);
+            Assert.fail("expecting NullArgumentException");
+        } catch (NullPointerException ex) {}
+
+        // if this fraction is subtracted naively, it will overflow.
+        // check that it doesn't.
+        Fraction f1 = new Fraction(1,32768*3);
+        Fraction f2 = new Fraction(1,59049);
+        f = f1.subtract(f2);
+        Assert.assertEquals(-13085, f.getNumerator());
+        Assert.assertEquals(1934917632, f.getDenominator());
+
+        f1 = new Fraction(Integer.MIN_VALUE, 3);
+        f2 = new Fraction(1,3).negate();
+        f = f1.subtract(f2);
+        Assert.assertEquals(Integer.MIN_VALUE+1, f.getNumerator());
+        Assert.assertEquals(3, f.getDenominator());
+
+        f1 = new Fraction(Integer.MAX_VALUE, 1);
+        f2 = Fraction.ONE;
+        f = f1.subtract(f2);
+        Assert.assertEquals(Integer.MAX_VALUE-1, f.getNumerator());
+        Assert.assertEquals(1, f.getDenominator());
+        f = f1.subtract(1);
+        Assert.assertEquals(Integer.MAX_VALUE-1, f.getNumerator());
+        Assert.assertEquals(1, f.getDenominator());
+
+        try {
+            f1 = new Fraction(1, Integer.MAX_VALUE);
+            f2 = new Fraction(1, Integer.MAX_VALUE - 1);
+            f = f1.subtract(f2);
+            Assert.fail("expecting NumbersArithmeticException");  //should 
overflow
+        } catch (NumbersArithmeticException ex) {}
+
+        // denominator should not be a multiple of 2 or 3 to trigger overflow
+        f1 = new Fraction(Integer.MIN_VALUE, 5);
+        f2 = new Fraction(1,5);
+        try {
+            f = f1.subtract(f2); // should overflow
+            Assert.fail("expecting FractionOverflowException but got: " + 
f.toString());
+        } catch (FractionOverflowException ex) {}
+
+        try {
+            f= new Fraction(Integer.MIN_VALUE, 1);
+            f = f.subtract(Fraction.ONE);
+            Assert.fail("expecting NumbersArithmeticException");
+        } catch (NumbersArithmeticException ex) {}
+
+        try {
+            f= new Fraction(Integer.MAX_VALUE, 1);
+            f = f.subtract(Fraction.ONE.negate());
+            Assert.fail("expecting NumbersArithmeticException");
+        } catch (NumbersArithmeticException ex) {}
+
+        f1 = new Fraction(3,327680);
+        f2 = new Fraction(2,59049);
+        try {
+            f = f1.subtract(f2); // should overflow
+            Assert.fail("expecting NumbersArithmeticException but got: " + 
f.toString());
+        } catch (NumbersArithmeticException ex) {}
+    }
+
+    @Test
+    public void testEqualsAndHashCode() {
+        Fraction zero  = new Fraction(0,1);
+        Fraction nullFraction = null;
+        Assert.assertTrue( zero.equals(zero));
+        Assert.assertFalse(zero.equals(nullFraction));
+        Assert.assertFalse(zero.equals(Double.valueOf(0)));
+        Fraction zero2 = new Fraction(0,2);
+        Assert.assertTrue(zero.equals(zero2));
+        Assert.assertEquals(zero.hashCode(), zero2.hashCode());
+        Fraction one = new Fraction(1,1);
+        Assert.assertFalse((one.equals(zero) ||zero.equals(one)));
+    }
+
+    @Test
+    public void testGetReducedFraction() {
+        Fraction threeFourths = new Fraction(3, 4);
+        Assert.assertTrue(threeFourths.equals(Fraction.getReducedFraction(6, 
8)));
+        Assert.assertTrue(Fraction.ZERO.equals(Fraction.getReducedFraction(0, 
-1)));
+        try {
+            Fraction.getReducedFraction(1, 0);
+            Assert.fail("expecting ZeroDenominatorException");
+        } catch (ZeroDenominatorException ex) {
+            // expected
+        }
+        Assert.assertEquals(Fraction.getReducedFraction
+                (2, Integer.MIN_VALUE).getNumerator(),-1);
+        Assert.assertEquals(Fraction.getReducedFraction
+                (1, -1).getNumerator(), -1);
+    }
+
+    @Test
+    public void testToString() {
+        Assert.assertEquals("0", new Fraction(0, 3).toString());
+        Assert.assertEquals("3", new Fraction(6, 2).toString());
+        Assert.assertEquals("2 / 3", new Fraction(18, 27).toString());
+    }
+
+    @Test
+    public void testSerial() throws FractionConversionException {
+        Fraction[] fractions = {
+            new Fraction(3, 4), Fraction.ONE, Fraction.ZERO,
+            new Fraction(17), new Fraction(Math.PI, 1000),
+            new Fraction(-5, 2)
+        };
+        for (Fraction fraction : fractions) {
+            Assert.assertEquals(fraction, 
TestUtils.serializeAndRecover(fraction));
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/aaef6f7a/commons-numbers-quaternion/pom.xml
----------------------------------------------------------------------
diff --git a/commons-numbers-quaternion/pom.xml 
b/commons-numbers-quaternion/pom.xml
index 76a59e3..1817528 100644
--- a/commons-numbers-quaternion/pom.xml
+++ b/commons-numbers-quaternion/pom.xml
@@ -46,7 +46,6 @@
     <dependency>
       <groupId>org.apache.commons</groupId>
       <artifactId>commons-numbers-core</artifactId>
-      <version>1.0-SNAPSHOT</version>
     </dependency>
   </dependencies>
 

http://git-wip-us.apache.org/repos/asf/commons-numbers/blob/aaef6f7a/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index b1e4336..770d615 100644
--- a/pom.xml
+++ b/pom.xml
@@ -68,9 +68,27 @@
   <contributors>
     <contributor>
       <name>Raymond DeCampo</name>
+      <email>ray at decampo dot org</email>
     </contributor>
   </contributors>
 
+  <dependencyManagement>
+    <dependencies>
+    <dependency>
+      <groupId>org.apache.commons</groupId>
+      <artifactId>commons-numbers-core</artifactId>
+      <version>${project.version}</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.commons</groupId>
+      <artifactId>commons-numbers-core</artifactId>
+      <version>${project.version}</version>
+      <type>test-jar</type>
+      <scope>test</scope>
+    </dependency>
+    </dependencies>
+  </dependencyManagement>
+
   <dependencies>
     <dependency>
       <groupId>junit</groupId>
@@ -543,7 +561,7 @@
     <module>commons-numbers-core</module>
     <module>commons-numbers-complex</module>
     <module>commons-numbers-quaternion</module>
-    <!-- <module>commons-numbers-fraction</module> -->
+    <module>commons-numbers-fraction</module>
     <!-- <module>commons-numbers-continuedfraction</module> -->
   </modules>
 

Reply via email to