BigFractionFormat extends AbstractFormat implements Serializable { @Override public BigFraction parse(final String source) throws MathParseException { final ParsePosition parsePosition = new ParsePosition(0); final BigFraction result = parse(source, parsePosition); if (parsePosition.getIndex() == 0) { throw new MathParseException(source, parsePosition.getErrorIndex(), BigFraction.class); } return result; } BigFractionFormat();  BigFractionFormat(final NumberFormat format);  BigFractionFormat(final NumberFormat numeratorFormat,
                             final NumberFormat denominatorFormat); static Locale[] getAvailableLocales(); static String formatBigFraction(final BigFraction f); static BigFractionFormat getImproperInstance(); static BigFractionFormat getImproperInstance(final Locale locale); static BigFractionFormat getProperInstance(); static BigFractionFormat getProperInstance(final Locale locale); StringBuffer format(final BigFraction BigFraction,
                               final StringBuffer toAppendTo, final FieldPosition pos); @Override StringBuffer format(final Object obj,
                               final StringBuffer toAppendTo, final FieldPosition pos); @Override BigFraction parse(final String source); @Override BigFraction parse(final String source, final ParsePosition pos);  }
@Test public void testParse() { String source = "1 / 2"; { BigFraction c = properFormat.parse(source); Assert.assertNotNull(c); Assert.assertEquals(BigInteger.ONE, c.getNumerator()); Assert.assertEquals(BigInteger.valueOf(2l), c.getDenominator()); c = improperFormat.parse(source); Assert.assertNotNull(c); Assert.assertEquals(BigInteger.ONE, c.getNumerator()); Assert.assertEquals(BigInteger.valueOf(2l), c.getDenominator()); } }
@Test public void testParseInteger() { String source = "10"; { BigFraction c = properFormat.parse(source); Assert.assertNotNull(c); Assert.assertEquals(BigInteger.TEN, c.getNumerator()); Assert.assertEquals(BigInteger.ONE, c.getDenominator()); } { BigFraction c = improperFormat.parse(source); Assert.assertNotNull(c); Assert.assertEquals(BigInteger.TEN, c.getNumerator()); Assert.assertEquals(BigInteger.ONE, c.getDenominator()); } }
@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 (MathParseException ex) { } try { improperFormat.parse(source); Assert.fail(msg); } catch (MathParseException ex) { } }
@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 (MathParseException ex) { } try { improperFormat.parse(source); Assert.fail(msg); } catch (MathParseException ex) { } }
@Test public void testParseNegative() { { String source = "-1 / 2"; BigFraction c = properFormat.parse(source); Assert.assertNotNull(c); Assert.assertEquals(-1, c.getNumeratorAsInt()); Assert.assertEquals(2, c.getDenominatorAsInt()); c = improperFormat.parse(source); Assert.assertNotNull(c); Assert.assertEquals(-1, c.getNumeratorAsInt()); Assert.assertEquals(2, c.getDenominatorAsInt()); source = "1 / -2"; c = properFormat.parse(source); Assert.assertNotNull(c); Assert.assertEquals(-1, c.getNumeratorAsInt()); Assert.assertEquals(2, c.getDenominatorAsInt()); c = improperFormat.parse(source); Assert.assertNotNull(c); Assert.assertEquals(-1, c.getNumeratorAsInt()); Assert.assertEquals(2, c.getDenominatorAsInt()); } }
@Test public void testParseProper() { String source = "1 2 / 3"; { BigFraction c = properFormat.parse(source); Assert.assertNotNull(c); Assert.assertEquals(5, c.getNumeratorAsInt()); Assert.assertEquals(3, c.getDenominatorAsInt()); } try { improperFormat.parse(source); Assert.fail("invalid improper fraction."); } catch (MathParseException ex) { } }
@Test public void testParseProperNegative() { String source = "-1 2 / 3"; { BigFraction c = properFormat.parse(source); Assert.assertNotNull(c); Assert.assertEquals(-5, c.getNumeratorAsInt()); Assert.assertEquals(3, c.getDenominatorAsInt()); } try { improperFormat.parse(source); Assert.fail("invalid improper fraction."); } catch (MathParseException ex) { } }
@Test public void testParseProperInvalidMinus() { String source = "2 -2 / 3"; try { properFormat.parse(source); Assert.fail("invalid minus in improper fraction."); } catch (MathParseException ex) { } source = "2 2 / -3"; try { properFormat.parse(source); Assert.fail("invalid minus in improper fraction."); } catch (MathParseException ex) { } }
@Test public void testParseBig() { BigFraction f1 = improperFormat.parse("167213075789791382630275400487886041651764456874403" + " / " + "53225575123090058458126718248444563466137046489291"); Assert.assertEquals(FastMath.PI, f1.doubleValue(), 0.0); BigFraction f2 = properFormat.parse("3 " + "7536350420521207255895245742552351253353317406530" + " / " + "53225575123090058458126718248444563466137046489291"); Assert.assertEquals(FastMath.PI, f2.doubleValue(), 0.0); Assert.assertEquals(f1, f2); BigDecimal pi = new BigDecimal("3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068"); Assert.assertEquals(pi, f1.bigDecimalValue(99, BigDecimal.ROUND_HALF_EVEN)); }