View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.math3.fraction;
19  
20  import java.math.BigDecimal;
21  import java.math.BigInteger;
22  import java.text.NumberFormat;
23  import java.util.Locale;
24  
25  import org.apache.commons.math3.exception.MathParseException;
26  import org.apache.commons.math3.util.FastMath;
27  import org.junit.Assert;
28  import org.junit.Before;
29  import org.junit.Test;
30  
31  
32  public class BigFractionFormatTest {
33  
34      BigFractionFormat properFormat = null;
35      BigFractionFormat improperFormat = null;
36  
37      protected Locale getLocale() {
38          return Locale.getDefault();
39      }
40  
41      @Before
42      public void setUp() {
43          properFormat = BigFractionFormat.getProperInstance(getLocale());
44          improperFormat = BigFractionFormat.getImproperInstance(getLocale());
45      }
46  
47      @Test
48      public void testFormat() {
49          BigFraction c = new BigFraction(1, 2);
50          String expected = "1 / 2";
51  
52          String actual = properFormat.format(c);
53          Assert.assertEquals(expected, actual);
54  
55          actual = improperFormat.format(c);
56          Assert.assertEquals(expected, actual);
57      }
58  
59      @Test
60      public void testFormatNegative() {
61          BigFraction c = new BigFraction(-1, 2);
62          String expected = "-1 / 2";
63  
64          String actual = properFormat.format(c);
65          Assert.assertEquals(expected, actual);
66  
67          actual = improperFormat.format(c);
68          Assert.assertEquals(expected, actual);
69      }
70  
71      @Test
72      public void testFormatZero() {
73          BigFraction c = new BigFraction(0, 1);
74          String expected = "0 / 1";
75  
76          String actual = properFormat.format(c);
77          Assert.assertEquals(expected, actual);
78  
79          actual = improperFormat.format(c);
80          Assert.assertEquals(expected, actual);
81      }
82  
83      @Test
84      public void testFormatImproper() {
85          BigFraction c = new BigFraction(5, 3);
86  
87          String actual = properFormat.format(c);
88          Assert.assertEquals("1 2 / 3", actual);
89  
90          actual = improperFormat.format(c);
91          Assert.assertEquals("5 / 3", actual);
92      }
93  
94      @Test
95      public void testFormatImproperNegative() {
96          BigFraction c = new BigFraction(-5, 3);
97  
98          String actual = properFormat.format(c);
99          Assert.assertEquals("-1 2 / 3", actual);
100 
101         actual = improperFormat.format(c);
102         Assert.assertEquals("-5 / 3", actual);
103     }
104 
105     @Test
106     public void testParse() {
107         String source = "1 / 2";
108 
109         {
110             BigFraction c = properFormat.parse(source);
111             Assert.assertNotNull(c);
112             Assert.assertEquals(BigInteger.ONE, c.getNumerator());
113             Assert.assertEquals(BigInteger.valueOf(2l), c.getDenominator());
114 
115             c = improperFormat.parse(source);
116             Assert.assertNotNull(c);
117             Assert.assertEquals(BigInteger.ONE, c.getNumerator());
118             Assert.assertEquals(BigInteger.valueOf(2l), c.getDenominator());
119         }
120     }
121 
122     @Test
123     public void testParseInteger() {
124         String source = "10";
125         {
126             BigFraction c = properFormat.parse(source);
127             Assert.assertNotNull(c);
128             Assert.assertEquals(BigInteger.TEN, c.getNumerator());
129             Assert.assertEquals(BigInteger.ONE, c.getDenominator());
130         }
131         {
132             BigFraction c = improperFormat.parse(source);
133             Assert.assertNotNull(c);
134             Assert.assertEquals(BigInteger.TEN, c.getNumerator());
135             Assert.assertEquals(BigInteger.ONE, c.getDenominator());
136         }
137     }
138 
139     @Test
140     public void testParseInvalid() {
141         String source = "a";
142         String msg = "should not be able to parse '10 / a'.";
143         try {
144             properFormat.parse(source);
145             Assert.fail(msg);
146         } catch (MathParseException ex) {
147             // success
148         }
149         try {
150             improperFormat.parse(source);
151             Assert.fail(msg);
152         } catch (MathParseException ex) {
153             // success
154         }
155     }
156 
157     @Test
158     public void testParseInvalidDenominator() {
159         String source = "10 / a";
160         String msg = "should not be able to parse '10 / a'.";
161         try {
162             properFormat.parse(source);
163             Assert.fail(msg);
164         } catch (MathParseException ex) {
165             // success
166         }
167         try {
168             improperFormat.parse(source);
169             Assert.fail(msg);
170         } catch (MathParseException ex) {
171             // success
172         }
173     }
174 
175     @Test
176     public void testParseNegative() {
177 
178         {
179             String source = "-1 / 2";
180             BigFraction c = properFormat.parse(source);
181             Assert.assertNotNull(c);
182             Assert.assertEquals(-1, c.getNumeratorAsInt());
183             Assert.assertEquals(2, c.getDenominatorAsInt());
184 
185             c = improperFormat.parse(source);
186             Assert.assertNotNull(c);
187             Assert.assertEquals(-1, c.getNumeratorAsInt());
188             Assert.assertEquals(2, c.getDenominatorAsInt());
189 
190             source = "1 / -2";
191             c = properFormat.parse(source);
192             Assert.assertNotNull(c);
193             Assert.assertEquals(-1, c.getNumeratorAsInt());
194             Assert.assertEquals(2, c.getDenominatorAsInt());
195 
196             c = improperFormat.parse(source);
197             Assert.assertNotNull(c);
198             Assert.assertEquals(-1, c.getNumeratorAsInt());
199             Assert.assertEquals(2, c.getDenominatorAsInt());
200         }
201     }
202 
203     @Test
204     public void testParseProper() {
205         String source = "1 2 / 3";
206 
207         {
208             BigFraction c = properFormat.parse(source);
209             Assert.assertNotNull(c);
210             Assert.assertEquals(5, c.getNumeratorAsInt());
211             Assert.assertEquals(3, c.getDenominatorAsInt());
212         }
213 
214         try {
215             improperFormat.parse(source);
216             Assert.fail("invalid improper fraction.");
217         } catch (MathParseException ex) {
218             // success
219         }
220     }
221 
222     @Test
223     public void testParseProperNegative() {
224         String source = "-1 2 / 3";
225         {
226             BigFraction c = properFormat.parse(source);
227             Assert.assertNotNull(c);
228             Assert.assertEquals(-5, c.getNumeratorAsInt());
229             Assert.assertEquals(3, c.getDenominatorAsInt());
230         }
231 
232         try {
233             improperFormat.parse(source);
234             Assert.fail("invalid improper fraction.");
235         } catch (MathParseException ex) {
236             // success
237         }
238     }
239 
240     @Test
241     public void testParseProperInvalidMinus() {
242         String source = "2 -2 / 3";
243         try {
244             properFormat.parse(source);
245             Assert.fail("invalid minus in improper fraction.");
246         } catch (MathParseException ex) {
247             // expected
248         }
249         source = "2 2 / -3";
250         try {
251             properFormat.parse(source);
252             Assert.fail("invalid minus in improper fraction.");
253         } catch (MathParseException ex) {
254             // expected
255         }
256     }
257 
258     @Test
259     public void testParseBig() {
260         BigFraction f1 =
261             improperFormat.parse("167213075789791382630275400487886041651764456874403" +
262                                  " / " +
263                                  "53225575123090058458126718248444563466137046489291");
264         Assert.assertEquals(FastMath.PI, f1.doubleValue(), 0.0);
265         BigFraction f2 =
266             properFormat.parse("3 " +
267                                "7536350420521207255895245742552351253353317406530" +
268                                " / " +
269                                "53225575123090058458126718248444563466137046489291");
270         Assert.assertEquals(FastMath.PI, f2.doubleValue(), 0.0);
271         Assert.assertEquals(f1, f2);
272         BigDecimal pi =
273             new BigDecimal("3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068");
274         Assert.assertEquals(pi, f1.bigDecimalValue(99, BigDecimal.ROUND_HALF_EVEN));
275     }
276 
277     @Test
278     public void testNumeratorFormat() {
279         NumberFormat old = properFormat.getNumeratorFormat();
280         NumberFormat nf = NumberFormat.getInstance();
281         nf.setParseIntegerOnly(true);
282         properFormat.setNumeratorFormat(nf);
283         Assert.assertEquals(nf, properFormat.getNumeratorFormat());
284         properFormat.setNumeratorFormat(old);
285 
286         old = improperFormat.getNumeratorFormat();
287         nf = NumberFormat.getInstance();
288         nf.setParseIntegerOnly(true);
289         improperFormat.setNumeratorFormat(nf);
290         Assert.assertEquals(nf, improperFormat.getNumeratorFormat());
291         improperFormat.setNumeratorFormat(old);
292     }
293 
294     @Test
295     public void testDenominatorFormat() {
296         NumberFormat old = properFormat.getDenominatorFormat();
297         NumberFormat nf = NumberFormat.getInstance();
298         nf.setParseIntegerOnly(true);
299         properFormat.setDenominatorFormat(nf);
300         Assert.assertEquals(nf, properFormat.getDenominatorFormat());
301         properFormat.setDenominatorFormat(old);
302 
303         old = improperFormat.getDenominatorFormat();
304         nf = NumberFormat.getInstance();
305         nf.setParseIntegerOnly(true);
306         improperFormat.setDenominatorFormat(nf);
307         Assert.assertEquals(nf, improperFormat.getDenominatorFormat());
308         improperFormat.setDenominatorFormat(old);
309     }
310 
311     @Test
312     public void testWholeFormat() {
313         ProperBigFractionFormat format = (ProperBigFractionFormat)properFormat;
314 
315         NumberFormat old = format.getWholeFormat();
316         NumberFormat nf = NumberFormat.getInstance();
317         nf.setParseIntegerOnly(true);
318         format.setWholeFormat(nf);
319         Assert.assertEquals(nf, format.getWholeFormat());
320         format.setWholeFormat(old);
321     }
322 
323     @Test
324     public void testLongFormat() {
325         Assert.assertEquals("10 / 1", improperFormat.format(10l));
326     }
327 
328     @Test
329     public void testDoubleFormat() {
330         Assert.assertEquals("1 / 16", improperFormat.format(0.0625));
331     }
332 }