001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.commons.math.fraction;
019
020 import java.math.BigDecimal;
021 import java.math.BigInteger;
022 import java.text.NumberFormat;
023 import java.text.ParseException;
024 import java.util.Locale;
025
026 import junit.framework.TestCase;
027
028 public class BigFractionFormatTest extends TestCase {
029
030 BigFractionFormat properFormat = null;
031 BigFractionFormat improperFormat = null;
032
033 protected Locale getLocale() {
034 return Locale.getDefault();
035 }
036
037 @Override
038 protected void setUp() throws Exception {
039 properFormat = BigFractionFormat.getProperInstance(getLocale());
040 improperFormat = BigFractionFormat.getImproperInstance(getLocale());
041 }
042
043 public void testFormat() {
044 BigFraction c = new BigFraction(1, 2);
045 String expected = "1 / 2";
046
047 String actual = properFormat.format(c);
048 assertEquals(expected, actual);
049
050 actual = improperFormat.format(c);
051 assertEquals(expected, actual);
052 }
053
054 public void testFormatNegative() {
055 BigFraction c = new BigFraction(-1, 2);
056 String expected = "-1 / 2";
057
058 String actual = properFormat.format(c);
059 assertEquals(expected, actual);
060
061 actual = improperFormat.format(c);
062 assertEquals(expected, actual);
063 }
064
065 public void testFormatZero() {
066 BigFraction c = new BigFraction(0, 1);
067 String expected = "0 / 1";
068
069 String actual = properFormat.format(c);
070 assertEquals(expected, actual);
071
072 actual = improperFormat.format(c);
073 assertEquals(expected, actual);
074 }
075
076 public void testFormatImproper() {
077 BigFraction c = new BigFraction(5, 3);
078
079 String actual = properFormat.format(c);
080 assertEquals("1 2 / 3", actual);
081
082 actual = improperFormat.format(c);
083 assertEquals("5 / 3", actual);
084 }
085
086 public void testFormatImproperNegative() {
087 BigFraction c = new BigFraction(-5, 3);
088
089 String actual = properFormat.format(c);
090 assertEquals("-1 2 / 3", actual);
091
092 actual = improperFormat.format(c);
093 assertEquals("-5 / 3", actual);
094 }
095
096 public void testParse() {
097 String source = "1 / 2";
098
099 try {
100 BigFraction c = properFormat.parse(source);
101 assertNotNull(c);
102 assertEquals(BigInteger.ONE, c.getNumerator());
103 assertEquals(BigInteger.valueOf(2l), c.getDenominator());
104
105 c = improperFormat.parse(source);
106 assertNotNull(c);
107 assertEquals(BigInteger.ONE, c.getNumerator());
108 assertEquals(BigInteger.valueOf(2l), c.getDenominator());
109 } catch (ParseException ex) {
110 fail(ex.getMessage());
111 }
112 }
113
114 public void testParseInteger() {
115 String source = "10";
116 try {
117 BigFraction c = properFormat.parse(source);
118 assertNotNull(c);
119 assertEquals(BigInteger.TEN, c.getNumerator());
120 assertEquals(BigInteger.ONE, c.getDenominator());
121 } catch (ParseException ex) {
122 fail(ex.getMessage());
123 }
124 try {
125 BigFraction c = improperFormat.parse(source);
126 assertNotNull(c);
127 assertEquals(BigInteger.TEN, c.getNumerator());
128 assertEquals(BigInteger.ONE, c.getDenominator());
129 } catch (ParseException ex) {
130 fail(ex.getMessage());
131 }
132 }
133
134 public void testParseInvalid() {
135 String source = "a";
136 String msg = "should not be able to parse '10 / a'.";
137 try {
138 properFormat.parse(source);
139 fail(msg);
140 } catch (ParseException ex) {
141 // success
142 }
143 try {
144 improperFormat.parse(source);
145 fail(msg);
146 } catch (ParseException ex) {
147 // success
148 }
149 }
150
151 public void testParseInvalidDenominator() {
152 String source = "10 / a";
153 String msg = "should not be able to parse '10 / a'.";
154 try {
155 properFormat.parse(source);
156 fail(msg);
157 } catch (ParseException ex) {
158 // success
159 }
160 try {
161 improperFormat.parse(source);
162 fail(msg);
163 } catch (ParseException ex) {
164 // success
165 }
166 }
167
168 public void testParseNegative() {
169
170 try {
171 String source = "-1 / 2";
172 BigFraction c = properFormat.parse(source);
173 assertNotNull(c);
174 assertEquals(-1, c.getNumeratorAsInt());
175 assertEquals(2, c.getDenominatorAsInt());
176
177 c = improperFormat.parse(source);
178 assertNotNull(c);
179 assertEquals(-1, c.getNumeratorAsInt());
180 assertEquals(2, c.getDenominatorAsInt());
181
182 source = "1 / -2";
183 c = properFormat.parse(source);
184 assertNotNull(c);
185 assertEquals(-1, c.getNumeratorAsInt());
186 assertEquals(2, c.getDenominatorAsInt());
187
188 c = improperFormat.parse(source);
189 assertNotNull(c);
190 assertEquals(-1, c.getNumeratorAsInt());
191 assertEquals(2, c.getDenominatorAsInt());
192 } catch (ParseException ex) {
193 fail(ex.getMessage());
194 }
195 }
196
197 public void testParseProper() {
198 String source = "1 2 / 3";
199
200 try {
201 BigFraction c = properFormat.parse(source);
202 assertNotNull(c);
203 assertEquals(5, c.getNumeratorAsInt());
204 assertEquals(3, c.getDenominatorAsInt());
205 } catch (ParseException ex) {
206 fail(ex.getMessage());
207 }
208
209 try {
210 improperFormat.parse(source);
211 fail("invalid improper fraction.");
212 } catch (ParseException ex) {
213 // success
214 }
215 }
216
217 public void testParseProperNegative() {
218 String source = "-1 2 / 3";
219 try {
220 BigFraction c = properFormat.parse(source);
221 assertNotNull(c);
222 assertEquals(-5, c.getNumeratorAsInt());
223 assertEquals(3, c.getDenominatorAsInt());
224 } catch (ParseException ex) {
225 fail(ex.getMessage());
226 }
227
228 try {
229 improperFormat.parse(source);
230 fail("invalid improper fraction.");
231 } catch (ParseException ex) {
232 // success
233 }
234 }
235
236 public void testParseProperInvalidMinus() {
237 String source = "2 -2 / 3";
238 try {
239 properFormat.parse(source);
240 fail("invalid minus in improper fraction.");
241 } catch (ParseException ex) {
242 // expected
243 }
244 source = "2 2 / -3";
245 try {
246 properFormat.parse(source);
247 fail("invalid minus in improper fraction.");
248 } catch (ParseException ex) {
249 // expected
250 }
251 }
252
253 public void testParseBig() throws ParseException {
254 BigFraction f1 =
255 improperFormat.parse("167213075789791382630275400487886041651764456874403" +
256 " / " +
257 "53225575123090058458126718248444563466137046489291");
258 assertEquals(Math.PI, f1.doubleValue(), 0.0);
259 BigFraction f2 =
260 properFormat.parse("3 " +
261 "7536350420521207255895245742552351253353317406530" +
262 " / " +
263 "53225575123090058458126718248444563466137046489291");
264 assertEquals(Math.PI, f2.doubleValue(), 0.0);
265 assertEquals(f1, f2);
266 BigDecimal pi =
267 new BigDecimal("3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068");
268 assertEquals(pi, f1.bigDecimalValue(99, BigDecimal.ROUND_HALF_EVEN));
269 }
270
271 public void testNumeratorFormat() {
272 NumberFormat old = properFormat.getNumeratorFormat();
273 NumberFormat nf = NumberFormat.getInstance();
274 nf.setParseIntegerOnly(true);
275 properFormat.setNumeratorFormat(nf);
276 assertEquals(nf, properFormat.getNumeratorFormat());
277 properFormat.setNumeratorFormat(old);
278
279 old = improperFormat.getNumeratorFormat();
280 nf = NumberFormat.getInstance();
281 nf.setParseIntegerOnly(true);
282 improperFormat.setNumeratorFormat(nf);
283 assertEquals(nf, improperFormat.getNumeratorFormat());
284 improperFormat.setNumeratorFormat(old);
285 }
286
287 public void testDenominatorFormat() {
288 NumberFormat old = properFormat.getDenominatorFormat();
289 NumberFormat nf = NumberFormat.getInstance();
290 nf.setParseIntegerOnly(true);
291 properFormat.setDenominatorFormat(nf);
292 assertEquals(nf, properFormat.getDenominatorFormat());
293 properFormat.setDenominatorFormat(old);
294
295 old = improperFormat.getDenominatorFormat();
296 nf = NumberFormat.getInstance();
297 nf.setParseIntegerOnly(true);
298 improperFormat.setDenominatorFormat(nf);
299 assertEquals(nf, improperFormat.getDenominatorFormat());
300 improperFormat.setDenominatorFormat(old);
301 }
302
303 public void testWholeFormat() {
304 ProperBigFractionFormat format = (ProperBigFractionFormat)properFormat;
305
306 NumberFormat old = format.getWholeFormat();
307 NumberFormat nf = NumberFormat.getInstance();
308 nf.setParseIntegerOnly(true);
309 format.setWholeFormat(nf);
310 assertEquals(nf, format.getWholeFormat());
311 format.setWholeFormat(old);
312 }
313
314 public void testLongFormat() {
315 assertEquals("10 / 1", improperFormat.format(10l));
316 }
317
318 public void testDoubleFormat() {
319 assertEquals("1 / 16", improperFormat.format(0.0625));
320 }
321 }