Modified:
jakarta/commons/proper/math/trunk/src/test/org/apache/commons/math/fraction/FractionTest.java
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/math/trunk/src/test/org/apache/commons/math/fraction/FractionTest.java?view=diff&rev=506713&r1=506712&r2=506713
==============================================================================
---
jakarta/commons/proper/math/trunk/src/test/org/apache/commons/math/fraction/FractionTest.java
(original)
+++
jakarta/commons/proper/math/trunk/src/test/org/apache/commons/math/fraction/FractionTest.java
Mon Feb 12 14:35:08 2007
@@ -1,446 +1,446 @@
-/*
- * 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.math.fraction;
-
-import org.apache.commons.math.ConvergenceException;
-
-import junit.framework.TestCase;
-
-/**
- * @version $Revision$ $Date$
- */
-public class FractionTest extends TestCase {
-
- private void assertFraction(int expectedNumerator, int
expectedDenominator, Fraction actual) {
- assertEquals(expectedNumerator, actual.getNumerator());
- assertEquals(expectedDenominator, actual.getDenominator());
- }
-
- 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);
- fail();
- } catch (ArithmeticException ex) {
- // success
- }
- try {
- new Fraction(1, Integer.MIN_VALUE);
- fail();
- } catch (ArithmeticException ex) {
- // success
- }
- try {
- assertFraction(0, 1, new Fraction(0.00000000000001));
- assertFraction(2, 5, new Fraction(0.40000000000001));
- assertFraction(15, 1, new Fraction(15.0000000000001));
-
- } catch (ConvergenceException ex) {
- fail(ex.getMessage());
- }
- }
-
- public void testCompareTo() {
- Fraction first = new Fraction(1, 2);
- Fraction second = new Fraction(1, 3);
- Fraction third = new Fraction(1, 2);
-
- assertEquals(0, first.compareTo(first));
- assertEquals(0, first.compareTo(third));
- assertEquals(1, first.compareTo(second));
- assertEquals(-1, second.compareTo(first));
- }
-
- public void testDoubleValue() {
- Fraction first = new Fraction(1, 2);
- Fraction second = new Fraction(1, 3);
-
- assertEquals(0.5, first.doubleValue(), 0.0);
- assertEquals(1.0 / 3.0, second.doubleValue(), 0.0);
- }
-
- public void testFloatValue() {
- Fraction first = new Fraction(1, 2);
- Fraction second = new Fraction(1, 3);
-
- assertEquals(0.5f, first.floatValue(), 0.0f);
- assertEquals((float)(1.0 / 3.0), second.floatValue(), 0.0f);
- }
-
- public void testIntValue() {
- Fraction first = new Fraction(1, 2);
- Fraction second = new Fraction(3, 2);
-
- assertEquals(0, first.intValue());
- assertEquals(1, second.intValue());
- }
-
- public void testLongValue() {
- Fraction first = new Fraction(1, 2);
- Fraction second = new Fraction(3, 2);
-
- assertEquals(0L, first.longValue());
- assertEquals(1L, second.longValue());
- }
-
- public void testConstructorDouble() {
- try {
- 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));
- } catch (ConvergenceException ex) {
- fail(ex.getMessage());
- }
- }
-
- 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());
- }
-
- public void testReciprocal() {
- Fraction f = null;
-
- f = new Fraction(50, 75);
- f = f.reciprocal();
- assertEquals(3, f.getNumerator());
- assertEquals(2, f.getDenominator());
-
- f = new Fraction(4, 3);
- f = f.reciprocal();
- assertEquals(3, f.getNumerator());
- assertEquals(4, f.getDenominator());
-
- f = new Fraction(-15, 47);
- f = f.reciprocal();
- assertEquals(-47, f.getNumerator());
- assertEquals(15, f.getDenominator());
-
- f = new Fraction(0, 3);
- try {
- f = f.reciprocal();
- fail("expecting ArithmeticException");
- } catch (ArithmeticException ex) {}
-
- // large values
- f = new Fraction(Integer.MAX_VALUE, 1);
- f = f.reciprocal();
- assertEquals(1, f.getNumerator());
- assertEquals(Integer.MAX_VALUE, f.getDenominator());
- }
-
- public void testNegate() {
- Fraction f = null;
-
- f = new Fraction(50, 75);
- f = f.negate();
- assertEquals(-2, f.getNumerator());
- assertEquals(3, f.getDenominator());
-
- f = new Fraction(-50, 75);
- f = f.negate();
- assertEquals(2, f.getNumerator());
- assertEquals(3, f.getDenominator());
-
- // large values
- f = new Fraction(Integer.MAX_VALUE-1, Integer.MAX_VALUE);
- f = f.negate();
- assertEquals(Integer.MIN_VALUE+2, f.getNumerator());
- assertEquals(Integer.MAX_VALUE, f.getDenominator());
-
- f = new Fraction(Integer.MIN_VALUE, 1);
- try {
- f = f.negate();
- fail("expecting ArithmeticException");
- } catch (ArithmeticException ex) {}
- }
-
- 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);
- assertEquals(Integer.MAX_VALUE, f.getNumerator());
- assertEquals(1, f.getDenominator());
-
- f1 = new Fraction(-1, 13*13*2*2);
- f2 = new Fraction(-2, 13*17*2);
- f = f1.add(f2);
- assertEquals(13*13*17*2*2, f.getDenominator());
- assertEquals(-17 - 2*13*2, f.getNumerator());
-
- try {
- f.add(null);
- fail("expecting IllegalArgumentException");
- } catch (IllegalArgumentException 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);
- assertEquals(52451, f.getNumerator());
- assertEquals(1934917632, f.getDenominator());
-
- f1 = new Fraction(Integer.MIN_VALUE, 3);
- f2 = new Fraction(1,3);
- f = f1.add(f2);
- assertEquals(Integer.MIN_VALUE+1, f.getNumerator());
- assertEquals(3, f.getDenominator());
-
- f1 = new Fraction(Integer.MAX_VALUE - 1, 1);
- f2 = Fraction.ONE;
- f = f1.add(f2);
- assertEquals(Integer.MAX_VALUE, f.getNumerator());
- assertEquals(1, f.getDenominator());
-
- try {
- f = f.add(Fraction.ONE); // should overflow
- fail("expecting ArithmeticException but got: " + f.toString());
- } catch (ArithmeticException 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
- fail("expecting ArithmeticException but got: " + f.toString());
- } catch (ArithmeticException ex) {}
-
- try {
- f= new Fraction(-Integer.MAX_VALUE, 1);
- f = f.add(f);
- fail("expecting ArithmeticException");
- } catch (ArithmeticException ex) {}
-
- try {
- f= new Fraction(-Integer.MAX_VALUE, 1);
- f = f.add(f);
- fail("expecting ArithmeticException");
- } catch (ArithmeticException ex) {}
-
- f1 = new Fraction(3,327680);
- f2 = new Fraction(2,59049);
- try {
- f = f1.add(f2); // should overflow
- fail("expecting ArithmeticException but got: " + f.toString());
- } catch (ArithmeticException ex) {}
- }
-
- 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 {
- Fraction f = f1.divide(f2);
- fail("expecting ArithmeticException");
- } catch (ArithmeticException ex) {}
-
- f1 = new Fraction(0, 5);
- f2 = new Fraction(2, 7);
- Fraction f = f1.divide(f2);
- assertSame(Fraction.ZERO, f);
-
- f1 = new Fraction(2, 7);
- f2 = Fraction.ONE;
- f = f1.divide(f2);
- assertEquals(2, f.getNumerator());
- assertEquals(7, f.getDenominator());
-
- f1 = new Fraction(1, Integer.MAX_VALUE);
- f = f1.divide(f1);
- assertEquals(1, f.getNumerator());
- assertEquals(1, f.getDenominator());
-
- f1 = new Fraction(Integer.MIN_VALUE, Integer.MAX_VALUE);
- f2 = new Fraction(1, Integer.MAX_VALUE);
- f = f1.divide(f2);
- assertEquals(Integer.MIN_VALUE, f.getNumerator());
- assertEquals(1, f.getDenominator());
-
- try {
- f.divide(null);
- fail("IllegalArgumentException");
- } catch (IllegalArgumentException ex) {}
-
- try {
- f1 = new Fraction(1, Integer.MAX_VALUE);
- f = f1.divide(f1.reciprocal()); // should overflow
- fail("expecting ArithmeticException");
- } catch (ArithmeticException ex) {}
- try {
- f1 = new Fraction(1, -Integer.MAX_VALUE);
- f = f1.divide(f1.reciprocal()); // should overflow
- fail("expecting ArithmeticException");
- } catch (ArithmeticException ex) {}
- }
-
- 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);
- assertEquals(Integer.MIN_VALUE, f.getNumerator());
- assertEquals(1, f.getDenominator());
-
- try {
- f.multiply(null);
- fail("expecting IllegalArgumentException");
- } catch (IllegalArgumentException ex) {}
- }
-
- 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);
- fail("expecting IllegalArgumentException");
- } catch (IllegalArgumentException 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);
- assertEquals(-13085, f.getNumerator());
- assertEquals(1934917632, f.getDenominator());
-
- f1 = new Fraction(Integer.MIN_VALUE, 3);
- f2 = new Fraction(1,3).negate();
- f = f1.subtract(f2);
- assertEquals(Integer.MIN_VALUE+1, f.getNumerator());
- assertEquals(3, f.getDenominator());
-
- f1 = new Fraction(Integer.MAX_VALUE, 1);
- f2 = Fraction.ONE;
- f = f1.subtract(f2);
- assertEquals(Integer.MAX_VALUE-1, f.getNumerator());
- assertEquals(1, f.getDenominator());
-
- try {
- f1 = new Fraction(1, Integer.MAX_VALUE);
- f2 = new Fraction(1, Integer.MAX_VALUE - 1);
- f = f1.subtract(f2);
- fail("expecting ArithmeticException"); //should overflow
- } catch (ArithmeticException 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
- fail("expecting ArithmeticException but got: " + f.toString());
- } catch (ArithmeticException ex) {}
-
- try {
- f= new Fraction(Integer.MIN_VALUE, 1);
- f = f.subtract(Fraction.ONE);
- fail("expecting ArithmeticException");
- } catch (ArithmeticException ex) {}
-
- try {
- f= new Fraction(Integer.MAX_VALUE, 1);
- f = f.subtract(Fraction.ONE.negate());
- fail("expecting ArithmeticException");
- } catch (ArithmeticException ex) {}
-
- f1 = new Fraction(3,327680);
- f2 = new Fraction(2,59049);
- try {
- f = f1.subtract(f2); // should overflow
- fail("expecting ArithmeticException but got: " + f.toString());
- } catch (ArithmeticException ex) {}
- }
-
- public void testEqualsAndHashCode() {
- Fraction zero = new Fraction(0,1);
- Fraction nullFraction = null;
- int zeroHash = zero.hashCode();
- assertTrue( zero.equals(zero));
- assertFalse(zero.equals(nullFraction));
- assertFalse(zero.equals(new Double(0)));
- Fraction zero2 = new Fraction(0,2);
- assertTrue(zero.equals(zero2));
- assertEquals(zero.hashCode(), zero2.hashCode());
- Fraction one = new Fraction(1,1);
- assertFalse((one.equals(zero) ||zero.equals(one)));
- }
-
- public void testGetReducedFraction() {
- Fraction threeFourths = new Fraction(3, 4);
- assertTrue(threeFourths.equals(Fraction.getReducedFraction(6, 8)));
- assertTrue(Fraction.ZERO.equals(Fraction.getReducedFraction(0, -1)));
- try {
- Fraction f = Fraction.getReducedFraction(1, 0);
- fail("expecting ArithmeticException");
- } catch (ArithmeticException ex) {
- // expected
- }
- assertEquals(Fraction.getReducedFraction
- (2, Integer.MIN_VALUE).getNumerator(),-1);
- assertEquals(Fraction.getReducedFraction
- (1, -1).getNumerator(), -1);
- }
-}
+/*
+ * 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.math.fraction;
+
+import org.apache.commons.math.ConvergenceException;
+
+import junit.framework.TestCase;
+
+/**
+ * @version $Revision$ $Date$
+ */
+public class FractionTest extends TestCase {
+
+ private void assertFraction(int expectedNumerator, int
expectedDenominator, Fraction actual) {
+ assertEquals(expectedNumerator, actual.getNumerator());
+ assertEquals(expectedDenominator, actual.getDenominator());
+ }
+
+ 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);
+ fail();
+ } catch (ArithmeticException ex) {
+ // success
+ }
+ try {
+ new Fraction(1, Integer.MIN_VALUE);
+ fail();
+ } catch (ArithmeticException ex) {
+ // success
+ }
+ try {
+ assertFraction(0, 1, new Fraction(0.00000000000001));
+ assertFraction(2, 5, new Fraction(0.40000000000001));
+ assertFraction(15, 1, new Fraction(15.0000000000001));
+
+ } catch (ConvergenceException ex) {
+ fail(ex.getMessage());
+ }
+ }
+
+ public void testCompareTo() {
+ Fraction first = new Fraction(1, 2);
+ Fraction second = new Fraction(1, 3);
+ Fraction third = new Fraction(1, 2);
+
+ assertEquals(0, first.compareTo(first));
+ assertEquals(0, first.compareTo(third));
+ assertEquals(1, first.compareTo(second));
+ assertEquals(-1, second.compareTo(first));
+ }
+
+ public void testDoubleValue() {
+ Fraction first = new Fraction(1, 2);
+ Fraction second = new Fraction(1, 3);
+
+ assertEquals(0.5, first.doubleValue(), 0.0);
+ assertEquals(1.0 / 3.0, second.doubleValue(), 0.0);
+ }
+
+ public void testFloatValue() {
+ Fraction first = new Fraction(1, 2);
+ Fraction second = new Fraction(1, 3);
+
+ assertEquals(0.5f, first.floatValue(), 0.0f);
+ assertEquals((float)(1.0 / 3.0), second.floatValue(), 0.0f);
+ }
+
+ public void testIntValue() {
+ Fraction first = new Fraction(1, 2);
+ Fraction second = new Fraction(3, 2);
+
+ assertEquals(0, first.intValue());
+ assertEquals(1, second.intValue());
+ }
+
+ public void testLongValue() {
+ Fraction first = new Fraction(1, 2);
+ Fraction second = new Fraction(3, 2);
+
+ assertEquals(0L, first.longValue());
+ assertEquals(1L, second.longValue());
+ }
+
+ public void testConstructorDouble() {
+ try {
+ 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));
+ } catch (ConvergenceException ex) {
+ fail(ex.getMessage());
+ }
+ }
+
+ 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());
+ }
+
+ public void testReciprocal() {
+ Fraction f = null;
+
+ f = new Fraction(50, 75);
+ f = f.reciprocal();
+ assertEquals(3, f.getNumerator());
+ assertEquals(2, f.getDenominator());
+
+ f = new Fraction(4, 3);
+ f = f.reciprocal();
+ assertEquals(3, f.getNumerator());
+ assertEquals(4, f.getDenominator());
+
+ f = new Fraction(-15, 47);
+ f = f.reciprocal();
+ assertEquals(-47, f.getNumerator());
+ assertEquals(15, f.getDenominator());
+
+ f = new Fraction(0, 3);
+ try {
+ f = f.reciprocal();
+ fail("expecting ArithmeticException");
+ } catch (ArithmeticException ex) {}
+
+ // large values
+ f = new Fraction(Integer.MAX_VALUE, 1);
+ f = f.reciprocal();
+ assertEquals(1, f.getNumerator());
+ assertEquals(Integer.MAX_VALUE, f.getDenominator());
+ }
+
+ public void testNegate() {
+ Fraction f = null;
+
+ f = new Fraction(50, 75);
+ f = f.negate();
+ assertEquals(-2, f.getNumerator());
+ assertEquals(3, f.getDenominator());
+
+ f = new Fraction(-50, 75);
+ f = f.negate();
+ assertEquals(2, f.getNumerator());
+ assertEquals(3, f.getDenominator());
+
+ // large values
+ f = new Fraction(Integer.MAX_VALUE-1, Integer.MAX_VALUE);
+ f = f.negate();
+ assertEquals(Integer.MIN_VALUE+2, f.getNumerator());
+ assertEquals(Integer.MAX_VALUE, f.getDenominator());
+
+ f = new Fraction(Integer.MIN_VALUE, 1);
+ try {
+ f = f.negate();
+ fail("expecting ArithmeticException");
+ } catch (ArithmeticException ex) {}
+ }
+
+ 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);
+ assertEquals(Integer.MAX_VALUE, f.getNumerator());
+ assertEquals(1, f.getDenominator());
+
+ f1 = new Fraction(-1, 13*13*2*2);
+ f2 = new Fraction(-2, 13*17*2);
+ f = f1.add(f2);
+ assertEquals(13*13*17*2*2, f.getDenominator());
+ assertEquals(-17 - 2*13*2, f.getNumerator());
+
+ try {
+ f.add(null);
+ fail("expecting IllegalArgumentException");
+ } catch (IllegalArgumentException 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);
+ assertEquals(52451, f.getNumerator());
+ assertEquals(1934917632, f.getDenominator());
+
+ f1 = new Fraction(Integer.MIN_VALUE, 3);
+ f2 = new Fraction(1,3);
+ f = f1.add(f2);
+ assertEquals(Integer.MIN_VALUE+1, f.getNumerator());
+ assertEquals(3, f.getDenominator());
+
+ f1 = new Fraction(Integer.MAX_VALUE - 1, 1);
+ f2 = Fraction.ONE;
+ f = f1.add(f2);
+ assertEquals(Integer.MAX_VALUE, f.getNumerator());
+ assertEquals(1, f.getDenominator());
+
+ try {
+ f = f.add(Fraction.ONE); // should overflow
+ fail("expecting ArithmeticException but got: " + f.toString());
+ } catch (ArithmeticException 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
+ fail("expecting ArithmeticException but got: " + f.toString());
+ } catch (ArithmeticException ex) {}
+
+ try {
+ f= new Fraction(-Integer.MAX_VALUE, 1);
+ f = f.add(f);
+ fail("expecting ArithmeticException");
+ } catch (ArithmeticException ex) {}
+
+ try {
+ f= new Fraction(-Integer.MAX_VALUE, 1);
+ f = f.add(f);
+ fail("expecting ArithmeticException");
+ } catch (ArithmeticException ex) {}
+
+ f1 = new Fraction(3,327680);
+ f2 = new Fraction(2,59049);
+ try {
+ f = f1.add(f2); // should overflow
+ fail("expecting ArithmeticException but got: " + f.toString());
+ } catch (ArithmeticException ex) {}
+ }
+
+ 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 {
+ Fraction f = f1.divide(f2);
+ fail("expecting ArithmeticException");
+ } catch (ArithmeticException ex) {}
+
+ f1 = new Fraction(0, 5);
+ f2 = new Fraction(2, 7);
+ Fraction f = f1.divide(f2);
+ assertSame(Fraction.ZERO, f);
+
+ f1 = new Fraction(2, 7);
+ f2 = Fraction.ONE;
+ f = f1.divide(f2);
+ assertEquals(2, f.getNumerator());
+ assertEquals(7, f.getDenominator());
+
+ f1 = new Fraction(1, Integer.MAX_VALUE);
+ f = f1.divide(f1);
+ assertEquals(1, f.getNumerator());
+ assertEquals(1, f.getDenominator());
+
+ f1 = new Fraction(Integer.MIN_VALUE, Integer.MAX_VALUE);
+ f2 = new Fraction(1, Integer.MAX_VALUE);
+ f = f1.divide(f2);
+ assertEquals(Integer.MIN_VALUE, f.getNumerator());
+ assertEquals(1, f.getDenominator());
+
+ try {
+ f.divide(null);
+ fail("IllegalArgumentException");
+ } catch (IllegalArgumentException ex) {}
+
+ try {
+ f1 = new Fraction(1, Integer.MAX_VALUE);
+ f = f1.divide(f1.reciprocal()); // should overflow
+ fail("expecting ArithmeticException");
+ } catch (ArithmeticException ex) {}
+ try {
+ f1 = new Fraction(1, -Integer.MAX_VALUE);
+ f = f1.divide(f1.reciprocal()); // should overflow
+ fail("expecting ArithmeticException");
+ } catch (ArithmeticException ex) {}
+ }
+
+ 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);
+ assertEquals(Integer.MIN_VALUE, f.getNumerator());
+ assertEquals(1, f.getDenominator());
+
+ try {
+ f.multiply(null);
+ fail("expecting IllegalArgumentException");
+ } catch (IllegalArgumentException ex) {}
+ }
+
+ 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);
+ fail("expecting IllegalArgumentException");
+ } catch (IllegalArgumentException 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);
+ assertEquals(-13085, f.getNumerator());
+ assertEquals(1934917632, f.getDenominator());
+
+ f1 = new Fraction(Integer.MIN_VALUE, 3);
+ f2 = new Fraction(1,3).negate();
+ f = f1.subtract(f2);
+ assertEquals(Integer.MIN_VALUE+1, f.getNumerator());
+ assertEquals(3, f.getDenominator());
+
+ f1 = new Fraction(Integer.MAX_VALUE, 1);
+ f2 = Fraction.ONE;
+ f = f1.subtract(f2);
+ assertEquals(Integer.MAX_VALUE-1, f.getNumerator());
+ assertEquals(1, f.getDenominator());
+
+ try {
+ f1 = new Fraction(1, Integer.MAX_VALUE);
+ f2 = new Fraction(1, Integer.MAX_VALUE - 1);
+ f = f1.subtract(f2);
+ fail("expecting ArithmeticException"); //should overflow
+ } catch (ArithmeticException 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
+ fail("expecting ArithmeticException but got: " + f.toString());
+ } catch (ArithmeticException ex) {}
+
+ try {
+ f= new Fraction(Integer.MIN_VALUE, 1);
+ f = f.subtract(Fraction.ONE);
+ fail("expecting ArithmeticException");
+ } catch (ArithmeticException ex) {}
+
+ try {
+ f= new Fraction(Integer.MAX_VALUE, 1);
+ f = f.subtract(Fraction.ONE.negate());
+ fail("expecting ArithmeticException");
+ } catch (ArithmeticException ex) {}
+
+ f1 = new Fraction(3,327680);
+ f2 = new Fraction(2,59049);
+ try {
+ f = f1.subtract(f2); // should overflow
+ fail("expecting ArithmeticException but got: " + f.toString());
+ } catch (ArithmeticException ex) {}
+ }
+
+ public void testEqualsAndHashCode() {
+ Fraction zero = new Fraction(0,1);
+ Fraction nullFraction = null;
+ int zeroHash = zero.hashCode();
+ assertTrue( zero.equals(zero));
+ assertFalse(zero.equals(nullFraction));
+ assertFalse(zero.equals(new Double(0)));
+ Fraction zero2 = new Fraction(0,2);
+ assertTrue(zero.equals(zero2));
+ assertEquals(zero.hashCode(), zero2.hashCode());
+ Fraction one = new Fraction(1,1);
+ assertFalse((one.equals(zero) ||zero.equals(one)));
+ }
+
+ public void testGetReducedFraction() {
+ Fraction threeFourths = new Fraction(3, 4);
+ assertTrue(threeFourths.equals(Fraction.getReducedFraction(6, 8)));
+ assertTrue(Fraction.ZERO.equals(Fraction.getReducedFraction(0, -1)));
+ try {
+ Fraction f = Fraction.getReducedFraction(1, 0);
+ fail("expecting ArithmeticException");
+ } catch (ArithmeticException ex) {
+ // expected
+ }
+ assertEquals(Fraction.getReducedFraction
+ (2, Integer.MIN_VALUE).getNumerator(),-1);
+ assertEquals(Fraction.getReducedFraction
+ (1, -1).getNumerator(), -1);
+ }
+}
Propchange:
jakarta/commons/proper/math/trunk/src/test/org/apache/commons/math/fraction/FractionTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jakarta/commons/proper/math/trunk/test-jar.xml
------------------------------------------------------------------------------
svn:eol-style = native
Modified: jakarta/commons/proper/math/trunk/xdocs/userguide/fraction.xml
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/math/trunk/xdocs/userguide/fraction.xml?view=diff&rev=506713&r1=506712&r2=506713
==============================================================================
--- jakarta/commons/proper/math/trunk/xdocs/userguide/fraction.xml (original)
+++ jakarta/commons/proper/math/trunk/xdocs/userguide/fraction.xml Mon Feb 12
14:35:08 2007
@@ -1,106 +1,106 @@
-<?xml version="1.0"?>
-
-<!--
- 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.
- -->
-
-<?xml-stylesheet type="text/xsl" href="./xdoc.xsl"?>
-<!-- $Revision$ $Date$ -->
-<document url="stat.html">
- <properties>
- <title>The Commons Math User Guide - Fractions</title>
- </properties>
- <body>
- <section name="9 Fractions">
- <subsection name="9.1 Overview" href="overview">
- <p>
- The fraction packages provides a fraction number type as well as
- fraction number formatting.
- </p>
- </subsection>
- <subsection name="9.2 Fraction Numbers" href="fraction">
- <p>
- <a href="../apidocs/org/apache/commons/math/fraction/Fraction.html">
- org.apache.commons.math.fraction.Fraction</a> provides a fraction
number
- type that forms the basis for the fraction functionality found in
- commons-math.
- </p>
- <p>
- To create a fraction number, simply call the constructor passing in
two
- integer arguments, the first being the numerator of the fraction and
the second being the denominator:
- <source>Fraction f = new Fraction(1, 3); // 1 / 3</source>
- </p>
- <p>
- Of special note with fraction construction, when a fraction is
created it is always reduced to lowest terms.
- </p>
- <p>
- The <code>Fraction</code> class provides many unary and binary
- fraction operations. These operations provide the means to add,
- subtract, multiple and, divide fractions along with other functions
similar to the real number functions found in
- <code>java.math.BigDecimal</code>:
- <source>Fraction lhs = new Fraction(1, 3);
-Fraction rhs = new Fraction(2, 5);
-
-Fraction answer = lhs.add(rhs); // add two fractions
- answer = lhs.subtract(rhs); // subtract two fractions
- answer = lhs.abs(); // absolute value
- answer = lhs.reciprocal(); // reciprocal of lhs</source>
- </p>
- <p>
- Like fraction construction, for each of the fraction functions, the
resulting fraction is reduced to lowest terms.
- </p>
- </subsection>
- <subsection name="9.3 Fraction Formatting and Parsing" href="formatting">
- <p>
- <code>Fraction</code> instances can be converted to and from strings
- using the<a
href="../apidocs/org/apache/commons/math/fraction/FractionFormat.html">
- org.apache.commons.math.fraction.FractionFormat</a> class.
- <code>FractionFormat</code> is a <code>java.text.Format</code>
- extension and, as such, is used like other formatting objects (e.g.
- <code>java.text.SimpleDateFormat</code>):
- <source>FractionFormat format = new FractionFormat(); // default
format
-Fraction f = new Fraction(2, 4);
-String s = format.format(f); // s contains "1 / 2", note the reduced
fraction</source>
- </p>
- <p>
- To customize the formatting output, one or two
- <code>java.text.NumberFormat</code> instances can be used to
construct
- a <code>FractionFormat</code>. These number formats control the
- formatting of the numerator and denominator of the fraction:
- <source>NumberFormat nf = NumberFormat.getInstance(Locale.FRANCE);
-// create fraction format with custom number format
-// when one number format is used, both numerator and
-// denominator are formatted the same
-FractionFormat format = new FractionFormat(nf);
-Fraction f = new Fraction(2000, 3333);
-String s = format.format(c); // s contains "2.000 / 3.333"
-
-NumberFormat nf2 = NumberFormat.getInstance(Locale.US);
-// create fraction format with custom number formats
-format = new FractionFormat(nf, nf2);
-s = format.format(f); // s contains "2.000 / 3,333"</source>
- </p>
- <p>
- Formatting's inverse operation, parsing, can also be performed by
- <code>FractionFormat</code>. To parse a fraction from a string,
- simply call the <code>parse</code> method:
- <source>FractionFormat ff = new FractionFormat();
-Fraction f = ff.parse("-10 / 21");</source>
- </p>
- </subsection>
- </section>
- </body>
-</document>
+<?xml version="1.0"?>
+
+<!--
+ 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.
+ -->
+
+<?xml-stylesheet type="text/xsl" href="./xdoc.xsl"?>
+<!-- $Revision$ $Date$ -->
+<document url="stat.html">
+ <properties>
+ <title>The Commons Math User Guide - Fractions</title>
+ </properties>
+ <body>
+ <section name="9 Fractions">
+ <subsection name="9.1 Overview" href="overview">
+ <p>
+ The fraction packages provides a fraction number type as well as
+ fraction number formatting.
+ </p>
+ </subsection>
+ <subsection name="9.2 Fraction Numbers" href="fraction">
+ <p>
+ <a href="../apidocs/org/apache/commons/math/fraction/Fraction.html">
+ org.apache.commons.math.fraction.Fraction</a> provides a fraction
number
+ type that forms the basis for the fraction functionality found in
+ commons-math.
+ </p>
+ <p>
+ To create a fraction number, simply call the constructor passing in
two
+ integer arguments, the first being the numerator of the fraction and
the second being the denominator:
+ <source>Fraction f = new Fraction(1, 3); // 1 / 3</source>
+ </p>
+ <p>
+ Of special note with fraction construction, when a fraction is
created it is always reduced to lowest terms.
+ </p>
+ <p>
+ The <code>Fraction</code> class provides many unary and binary
+ fraction operations. These operations provide the means to add,
+ subtract, multiple and, divide fractions along with other functions
similar to the real number functions found in
+ <code>java.math.BigDecimal</code>:
+ <source>Fraction lhs = new Fraction(1, 3);
+Fraction rhs = new Fraction(2, 5);
+
+Fraction answer = lhs.add(rhs); // add two fractions
+ answer = lhs.subtract(rhs); // subtract two fractions
+ answer = lhs.abs(); // absolute value
+ answer = lhs.reciprocal(); // reciprocal of lhs</source>
+ </p>
+ <p>
+ Like fraction construction, for each of the fraction functions, the
resulting fraction is reduced to lowest terms.
+ </p>
+ </subsection>
+ <subsection name="9.3 Fraction Formatting and Parsing" href="formatting">
+ <p>
+ <code>Fraction</code> instances can be converted to and from strings
+ using the<a
href="../apidocs/org/apache/commons/math/fraction/FractionFormat.html">
+ org.apache.commons.math.fraction.FractionFormat</a> class.
+ <code>FractionFormat</code> is a <code>java.text.Format</code>
+ extension and, as such, is used like other formatting objects (e.g.
+ <code>java.text.SimpleDateFormat</code>):
+ <source>FractionFormat format = new FractionFormat(); // default
format
+Fraction f = new Fraction(2, 4);
+String s = format.format(f); // s contains "1 / 2", note the reduced
fraction</source>
+ </p>
+ <p>
+ To customize the formatting output, one or two
+ <code>java.text.NumberFormat</code> instances can be used to
construct
+ a <code>FractionFormat</code>. These number formats control the
+ formatting of the numerator and denominator of the fraction:
+ <source>NumberFormat nf = NumberFormat.getInstance(Locale.FRANCE);
+// create fraction format with custom number format
+// when one number format is used, both numerator and
+// denominator are formatted the same
+FractionFormat format = new FractionFormat(nf);
+Fraction f = new Fraction(2000, 3333);
+String s = format.format(c); // s contains "2.000 / 3.333"
+
+NumberFormat nf2 = NumberFormat.getInstance(Locale.US);
+// create fraction format with custom number formats
+format = new FractionFormat(nf, nf2);
+s = format.format(f); // s contains "2.000 / 3,333"</source>
+ </p>
+ <p>
+ Formatting's inverse operation, parsing, can also be performed by
+ <code>FractionFormat</code>. To parse a fraction from a string,
+ simply call the <code>parse</code> method:
+ <source>FractionFormat ff = new FractionFormat();
+Fraction f = ff.parse("-10 / 21");</source>
+ </p>
+ </subsection>
+ </section>
+ </body>
+</document>
Propchange: jakarta/commons/proper/math/trunk/xdocs/userguide/fraction.xml
------------------------------------------------------------------------------
svn:eol-style = native
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]