MATH-1416: Remove BigFractionFormat and ProperBigFractionFormat from commons-math as they have been moved to commons-numbers
This commit is contained in:
parent
b31b5ca32a
commit
2f038f0073
|
@ -1,287 +0,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.
|
||||
*/
|
||||
|
||||
package org.apache.commons.math4.fraction;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.math.BigInteger;
|
||||
import java.text.FieldPosition;
|
||||
import java.text.NumberFormat;
|
||||
import java.text.ParsePosition;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.apache.commons.math4.exception.MathIllegalArgumentException;
|
||||
import org.apache.commons.math4.exception.MathParseException;
|
||||
import org.apache.commons.math4.exception.util.LocalizedFormats;
|
||||
|
||||
/**
|
||||
* Formats a BigFraction number in proper format or improper format.
|
||||
* <p>
|
||||
* The number format for each of the whole number, numerator and,
|
||||
* denominator can be configured.
|
||||
* </p>
|
||||
*
|
||||
* @since 2.0
|
||||
*/
|
||||
public class BigFractionFormat extends AbstractFormat implements Serializable {
|
||||
|
||||
/** Serializable version identifier */
|
||||
private static final long serialVersionUID = -2932167925527338976L;
|
||||
|
||||
/**
|
||||
* Create an improper formatting instance with the default number format
|
||||
* for the numerator and denominator.
|
||||
*/
|
||||
public BigFractionFormat() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an improper formatting instance with a custom number format for
|
||||
* both the numerator and denominator.
|
||||
* @param format the custom format for both the numerator and denominator.
|
||||
*/
|
||||
public BigFractionFormat(final NumberFormat format) {
|
||||
super(format);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an improper formatting instance with a custom number format for
|
||||
* the numerator and a custom number format for the denominator.
|
||||
* @param numeratorFormat the custom format for the numerator.
|
||||
* @param denominatorFormat the custom format for the denominator.
|
||||
*/
|
||||
public BigFractionFormat(final NumberFormat numeratorFormat,
|
||||
final NumberFormat denominatorFormat) {
|
||||
super(numeratorFormat, denominatorFormat);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the set of locales for which complex formats are available. This
|
||||
* is the same set as the {@link NumberFormat} set.
|
||||
* @return available complex format locales.
|
||||
*/
|
||||
public static Locale[] getAvailableLocales() {
|
||||
return NumberFormat.getAvailableLocales();
|
||||
}
|
||||
|
||||
/**
|
||||
* This static method calls formatBigFraction() on a default instance of
|
||||
* BigFractionFormat.
|
||||
*
|
||||
* @param f BigFraction object to format
|
||||
* @return A formatted BigFraction in proper form.
|
||||
*/
|
||||
public static String formatBigFraction(final BigFraction f) {
|
||||
return getImproperInstance().format(f);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the default complex format for the current locale.
|
||||
* @return the default complex format.
|
||||
*/
|
||||
public static BigFractionFormat getImproperInstance() {
|
||||
return getImproperInstance(Locale.getDefault());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the default complex format for the given locale.
|
||||
* @param locale the specific locale used by the format.
|
||||
* @return the complex format specific to the given locale.
|
||||
*/
|
||||
public static BigFractionFormat getImproperInstance(final Locale locale) {
|
||||
return new BigFractionFormat(getDefaultNumberFormat(locale));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the default complex format for the current locale.
|
||||
* @return the default complex format.
|
||||
*/
|
||||
public static BigFractionFormat getProperInstance() {
|
||||
return getProperInstance(Locale.getDefault());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the default complex format for the given locale.
|
||||
* @param locale the specific locale used by the format.
|
||||
* @return the complex format specific to the given locale.
|
||||
*/
|
||||
public static BigFractionFormat getProperInstance(final Locale locale) {
|
||||
return new ProperBigFractionFormat(getDefaultNumberFormat(locale));
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats a {@link BigFraction} object to produce a string. The BigFraction is
|
||||
* output in improper format.
|
||||
*
|
||||
* @param BigFraction the object to format.
|
||||
* @param toAppendTo where the text is to be appended
|
||||
* @param pos On input: an alignment field, if desired. On output: the
|
||||
* offsets of the alignment field
|
||||
* @return the value passed in as toAppendTo.
|
||||
*/
|
||||
public StringBuffer format(final BigFraction BigFraction,
|
||||
final StringBuffer toAppendTo, final FieldPosition pos) {
|
||||
|
||||
pos.setBeginIndex(0);
|
||||
pos.setEndIndex(0);
|
||||
|
||||
getNumeratorFormat().format(BigFraction.getNumerator(), toAppendTo, pos);
|
||||
toAppendTo.append(" / ");
|
||||
getDenominatorFormat().format(BigFraction.getDenominator(), toAppendTo, pos);
|
||||
|
||||
return toAppendTo;
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats an object and appends the result to a StringBuffer.
|
||||
* <code>obj</code> must be either a {@link BigFraction} object or a
|
||||
* {@link BigInteger} object or a {@link Number} object. Any other type of
|
||||
* object will result in an {@link IllegalArgumentException} being thrown.
|
||||
*
|
||||
* @param obj the object to format.
|
||||
* @param toAppendTo where the text is to be appended
|
||||
* @param pos On input: an alignment field, if desired. On output: the
|
||||
* offsets of the alignment field
|
||||
* @return the value passed in as toAppendTo.
|
||||
* @see java.text.Format#format(java.lang.Object, java.lang.StringBuffer, java.text.FieldPosition)
|
||||
* @throws MathIllegalArgumentException if <code>obj</code> is not a valid type.
|
||||
*/
|
||||
@Override
|
||||
public StringBuffer format(final Object obj,
|
||||
final StringBuffer toAppendTo, final FieldPosition pos) {
|
||||
|
||||
final StringBuffer ret;
|
||||
if (obj instanceof BigFraction) {
|
||||
ret = format((BigFraction) obj, toAppendTo, pos);
|
||||
} else if (obj instanceof BigInteger) {
|
||||
ret = format(new BigFraction((BigInteger) obj), toAppendTo, pos);
|
||||
} else if (obj instanceof Number) {
|
||||
ret = format(new BigFraction(((Number) obj).doubleValue()),
|
||||
toAppendTo, pos);
|
||||
} else {
|
||||
throw new MathIllegalArgumentException(LocalizedFormats.CANNOT_FORMAT_OBJECT_TO_FRACTION);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a string to produce a {@link BigFraction} object.
|
||||
* @param source the string to parse
|
||||
* @return the parsed {@link BigFraction} object.
|
||||
* @exception MathParseException if the beginning of the specified string
|
||||
* cannot be parsed.
|
||||
*/
|
||||
@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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a string to produce a {@link BigFraction} object.
|
||||
* This method expects the string to be formatted as an improper BigFraction.
|
||||
* @param source the string to parse
|
||||
* @param pos input/output parsing parameter.
|
||||
* @return the parsed {@link BigFraction} object.
|
||||
*/
|
||||
@Override
|
||||
public BigFraction parse(final String source, final ParsePosition pos) {
|
||||
final int initialIndex = pos.getIndex();
|
||||
|
||||
// parse whitespace
|
||||
parseAndIgnoreWhitespace(source, pos);
|
||||
|
||||
// parse numerator
|
||||
final BigInteger num = parseNextBigInteger(source, pos);
|
||||
if (num == null) {
|
||||
// invalid integer number
|
||||
// set index back to initial, error index should already be set
|
||||
// character examined.
|
||||
pos.setIndex(initialIndex);
|
||||
return null;
|
||||
}
|
||||
|
||||
// parse '/'
|
||||
final int startIndex = pos.getIndex();
|
||||
final char c = parseNextCharacter(source, pos);
|
||||
switch (c) {
|
||||
case 0 :
|
||||
// no '/'
|
||||
// return num as a BigFraction
|
||||
return new BigFraction(num);
|
||||
case '/' :
|
||||
// found '/', continue parsing denominator
|
||||
break;
|
||||
default :
|
||||
// invalid '/'
|
||||
// set index back to initial, error index should be the last
|
||||
// character examined.
|
||||
pos.setIndex(initialIndex);
|
||||
pos.setErrorIndex(startIndex);
|
||||
return null;
|
||||
}
|
||||
|
||||
// parse whitespace
|
||||
parseAndIgnoreWhitespace(source, pos);
|
||||
|
||||
// parse denominator
|
||||
final BigInteger den = parseNextBigInteger(source, pos);
|
||||
if (den == null) {
|
||||
// invalid integer number
|
||||
// set index back to initial, error index should already be set
|
||||
// character examined.
|
||||
pos.setIndex(initialIndex);
|
||||
return null;
|
||||
}
|
||||
|
||||
return new BigFraction(num, den);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a string to produce a <code>BigInteger</code>.
|
||||
* @param source the string to parse
|
||||
* @param pos input/output parsing parameter.
|
||||
* @return a parsed <code>BigInteger</code> or null if string does not
|
||||
* contain a BigInteger at the specified position
|
||||
*/
|
||||
protected BigInteger parseNextBigInteger(final String source,
|
||||
final ParsePosition pos) {
|
||||
|
||||
final int start = pos.getIndex();
|
||||
int end = (source.charAt(start) == '-') ? (start + 1) : start;
|
||||
while((end < source.length()) &&
|
||||
Character.isDigit(source.charAt(end))) {
|
||||
++end;
|
||||
}
|
||||
|
||||
try {
|
||||
BigInteger n = new BigInteger(source.substring(start, end));
|
||||
pos.setIndex(end);
|
||||
return n;
|
||||
} catch (NumberFormatException nfe) {
|
||||
pos.setErrorIndex(start);
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -1,238 +0,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.
|
||||
*/
|
||||
package org.apache.commons.math4.fraction;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.text.FieldPosition;
|
||||
import java.text.NumberFormat;
|
||||
import java.text.ParsePosition;
|
||||
|
||||
import org.apache.commons.math4.exception.NullArgumentException;
|
||||
import org.apache.commons.math4.exception.util.LocalizedFormats;
|
||||
|
||||
/**
|
||||
* Formats a BigFraction 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>.</p>
|
||||
*
|
||||
* @since 1.1
|
||||
*/
|
||||
public class ProperBigFractionFormat extends BigFractionFormat {
|
||||
|
||||
/** Serializable version identifier */
|
||||
private static final long serialVersionUID = -6337346779577272307L;
|
||||
|
||||
/** The format used for the whole number. */
|
||||
private NumberFormat wholeFormat;
|
||||
|
||||
/**
|
||||
* Create a proper formatting instance with the default number format for
|
||||
* the whole, numerator, and denominator.
|
||||
*/
|
||||
public ProperBigFractionFormat() {
|
||||
this(getDefaultNumberFormat());
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a proper formatting instance with a custom number format for the
|
||||
* whole, numerator, and denominator.
|
||||
* @param format the custom format for the whole, numerator, and
|
||||
* denominator.
|
||||
*/
|
||||
public ProperBigFractionFormat(final NumberFormat format) {
|
||||
this(format, (NumberFormat)format.clone(), (NumberFormat)format.clone());
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a proper formatting instance with a custom number format for each
|
||||
* of the whole, numerator, and denominator.
|
||||
* @param wholeFormat the custom format for the whole.
|
||||
* @param numeratorFormat the custom format for the numerator.
|
||||
* @param denominatorFormat the custom format for the denominator.
|
||||
*/
|
||||
public ProperBigFractionFormat(final NumberFormat wholeFormat,
|
||||
final NumberFormat numeratorFormat,
|
||||
final NumberFormat denominatorFormat) {
|
||||
super(numeratorFormat, denominatorFormat);
|
||||
setWholeFormat(wholeFormat);
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats a {@link BigFraction} object to produce a string. The BigFraction
|
||||
* is output in proper format.
|
||||
*
|
||||
* @param fraction the object to format.
|
||||
* @param toAppendTo where the text is to be appended
|
||||
* @param pos On input: an alignment field, if desired. On output: the
|
||||
* offsets of the alignment field
|
||||
* @return the value passed in as toAppendTo.
|
||||
*/
|
||||
@Override
|
||||
public StringBuffer format(final BigFraction fraction,
|
||||
final StringBuffer toAppendTo, final FieldPosition pos) {
|
||||
|
||||
pos.setBeginIndex(0);
|
||||
pos.setEndIndex(0);
|
||||
|
||||
BigInteger num = fraction.getNumerator();
|
||||
BigInteger den = fraction.getDenominator();
|
||||
BigInteger whole = num.divide(den);
|
||||
num = num.remainder(den);
|
||||
|
||||
if (!BigInteger.ZERO.equals(whole)) {
|
||||
getWholeFormat().format(whole, toAppendTo, pos);
|
||||
toAppendTo.append(' ');
|
||||
if (num.compareTo(BigInteger.ZERO) < 0) {
|
||||
num = num.negate();
|
||||
}
|
||||
}
|
||||
getNumeratorFormat().format(num, toAppendTo, pos);
|
||||
toAppendTo.append(" / ");
|
||||
getDenominatorFormat().format(den, toAppendTo, pos);
|
||||
|
||||
return toAppendTo;
|
||||
}
|
||||
|
||||
/**
|
||||
* Access the whole format.
|
||||
* @return the whole format.
|
||||
*/
|
||||
public NumberFormat getWholeFormat() {
|
||||
return wholeFormat;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a string to produce a {@link BigFraction} object. This method
|
||||
* expects the string to be formatted as a proper BigFraction.
|
||||
* <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>.</p>
|
||||
*
|
||||
* @param source the string to parse
|
||||
* @param pos input/ouput parsing parameter.
|
||||
* @return the parsed {@link BigFraction} object.
|
||||
*/
|
||||
@Override
|
||||
public BigFraction parse(final String source, final ParsePosition pos) {
|
||||
// try to parse improper BigFraction
|
||||
BigFraction ret = super.parse(source, pos);
|
||||
if (ret != null) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
final int initialIndex = pos.getIndex();
|
||||
|
||||
// parse whitespace
|
||||
parseAndIgnoreWhitespace(source, pos);
|
||||
|
||||
// parse whole
|
||||
BigInteger whole = parseNextBigInteger(source, pos);
|
||||
if (whole == null) {
|
||||
// invalid integer number
|
||||
// set index back to initial, error index should already be set
|
||||
// character examined.
|
||||
pos.setIndex(initialIndex);
|
||||
return null;
|
||||
}
|
||||
|
||||
// parse whitespace
|
||||
parseAndIgnoreWhitespace(source, pos);
|
||||
|
||||
// parse numerator
|
||||
BigInteger num = parseNextBigInteger(source, pos);
|
||||
if (num == null) {
|
||||
// invalid integer number
|
||||
// set index back to initial, error index should already be set
|
||||
// character examined.
|
||||
pos.setIndex(initialIndex);
|
||||
return null;
|
||||
}
|
||||
|
||||
if (num.compareTo(BigInteger.ZERO) < 0) {
|
||||
// minus signs should be leading, invalid expression
|
||||
pos.setIndex(initialIndex);
|
||||
return null;
|
||||
}
|
||||
|
||||
// parse '/'
|
||||
final int startIndex = pos.getIndex();
|
||||
final char c = parseNextCharacter(source, pos);
|
||||
switch (c) {
|
||||
case 0 :
|
||||
// no '/'
|
||||
// return num as a BigFraction
|
||||
return new BigFraction(num);
|
||||
case '/' :
|
||||
// found '/', continue parsing denominator
|
||||
break;
|
||||
default :
|
||||
// invalid '/'
|
||||
// set index back to initial, error index should be the last
|
||||
// character examined.
|
||||
pos.setIndex(initialIndex);
|
||||
pos.setErrorIndex(startIndex);
|
||||
return null;
|
||||
}
|
||||
|
||||
// parse whitespace
|
||||
parseAndIgnoreWhitespace(source, pos);
|
||||
|
||||
// parse denominator
|
||||
final BigInteger den = parseNextBigInteger(source, pos);
|
||||
if (den == null) {
|
||||
// invalid integer number
|
||||
// set index back to initial, error index should already be set
|
||||
// character examined.
|
||||
pos.setIndex(initialIndex);
|
||||
return null;
|
||||
}
|
||||
|
||||
if (den.compareTo(BigInteger.ZERO) < 0) {
|
||||
// minus signs must be leading, invalid
|
||||
pos.setIndex(initialIndex);
|
||||
return null;
|
||||
}
|
||||
|
||||
boolean wholeIsNeg = whole.compareTo(BigInteger.ZERO) < 0;
|
||||
if (wholeIsNeg) {
|
||||
whole = whole.negate();
|
||||
}
|
||||
num = whole.multiply(den).add(num);
|
||||
if (wholeIsNeg) {
|
||||
num = num.negate();
|
||||
}
|
||||
|
||||
return new BigFraction(num, den);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Modify the whole format.
|
||||
* @param format The new whole format value.
|
||||
* @throws NullArgumentException if {@code format} is {@code null}.
|
||||
*/
|
||||
public void setWholeFormat(final NumberFormat format) {
|
||||
if (format == null) {
|
||||
throw new NullArgumentException(LocalizedFormats.WHOLE_FORMAT);
|
||||
}
|
||||
this.wholeFormat = format;
|
||||
}
|
||||
}
|
|
@ -1,335 +0,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.
|
||||
*/
|
||||
|
||||
package org.apache.commons.math4.fraction;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.text.NumberFormat;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.apache.commons.math4.exception.MathParseException;
|
||||
import org.apache.commons.math4.fraction.BigFraction;
|
||||
import org.apache.commons.math4.fraction.BigFractionFormat;
|
||||
import org.apache.commons.math4.fraction.ProperBigFractionFormat;
|
||||
import org.apache.commons.math4.util.FastMath;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
public class BigFractionFormatTest {
|
||||
|
||||
BigFractionFormat properFormat = null;
|
||||
BigFractionFormat improperFormat = null;
|
||||
|
||||
protected Locale getLocale() {
|
||||
return Locale.getDefault();
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
properFormat = BigFractionFormat.getProperInstance(getLocale());
|
||||
improperFormat = BigFractionFormat.getImproperInstance(getLocale());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFormat() {
|
||||
BigFraction c = new BigFraction(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() {
|
||||
BigFraction c = new BigFraction(-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() {
|
||||
BigFraction c = new BigFraction(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() {
|
||||
BigFraction c = new BigFraction(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() {
|
||||
BigFraction c = new BigFraction(-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";
|
||||
|
||||
{
|
||||
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) {
|
||||
// success
|
||||
}
|
||||
try {
|
||||
improperFormat.parse(source);
|
||||
Assert.fail(msg);
|
||||
} catch (MathParseException 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 (MathParseException ex) {
|
||||
// success
|
||||
}
|
||||
try {
|
||||
improperFormat.parse(source);
|
||||
Assert.fail(msg);
|
||||
} catch (MathParseException ex) {
|
||||
// success
|
||||
}
|
||||
}
|
||||
|
||||
@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) {
|
||||
// success
|
||||
}
|
||||
}
|
||||
|
||||
@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) {
|
||||
// success
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseProperInvalidMinus() {
|
||||
String source = "2 -2 / 3";
|
||||
try {
|
||||
properFormat.parse(source);
|
||||
Assert.fail("invalid minus in improper fraction.");
|
||||
} catch (MathParseException ex) {
|
||||
// expected
|
||||
}
|
||||
source = "2 2 / -3";
|
||||
try {
|
||||
properFormat.parse(source);
|
||||
Assert.fail("invalid minus in improper fraction.");
|
||||
} catch (MathParseException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
@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));
|
||||
}
|
||||
|
||||
@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() {
|
||||
ProperBigFractionFormat format = (ProperBigFractionFormat)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("1 / 16", improperFormat.format(0.0625));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue