FractionFormat extends AbstractFormat { @Override public Fraction parse(final String source) throws MathParseException { final ParsePosition parsePosition = new ParsePosition(0); final Fraction result = parse(source, parsePosition); if (parsePosition.getIndex() == 0) { throw new MathParseException(source, parsePosition.getErrorIndex(), Fraction.class); } return result; } FractionFormat();  FractionFormat(final NumberFormat format);  FractionFormat(final NumberFormat numeratorFormat,
                          final NumberFormat denominatorFormat); static Locale[] getAvailableLocales(); static String formatFraction(Fraction f); static FractionFormat getImproperInstance(); static FractionFormat getImproperInstance(final Locale locale); static FractionFormat getProperInstance(); static FractionFormat getProperInstance(final Locale locale); StringBuffer format(final Fraction fraction,
                               final StringBuffer toAppendTo, final FieldPosition pos); @Override StringBuffer format(final Object obj,
                               final StringBuffer toAppendTo, final FieldPosition pos); @Override Fraction parse(final String source); @Override Fraction parse(final String source, final ParsePosition pos);  }
@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 (MathParseException 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()); 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 (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"; 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 (MathParseException ex) { } }
@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 (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) { } }