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.text.NumberFormat;
21  import java.util.Locale;
22  
23  import org.apache.commons.math3.exception.MathParseException;
24  import org.apache.commons.math3.util.FastMath;
25  import org.junit.Assert;
26  import org.junit.Before;
27  import org.junit.Test;
28  
29  
30  public class FractionFormatTest {
31  
32      FractionFormat properFormat = null;
33      FractionFormat improperFormat = null;
34  
35      protected Locale getLocale() {
36          return Locale.getDefault();
37      }
38  
39      @Before
40      public void setUp() {
41          properFormat = FractionFormat.getProperInstance(getLocale());
42          improperFormat = FractionFormat.getImproperInstance(getLocale());
43      }
44  
45      @Test
46      public void testFormat() {
47          Fraction c = new Fraction(1, 2);
48          String expected = "1 / 2";
49  
50          String actual = properFormat.format(c);
51          Assert.assertEquals(expected, actual);
52  
53          actual = improperFormat.format(c);
54          Assert.assertEquals(expected, actual);
55      }
56  
57      @Test
58      public void testFormatNegative() {
59          Fraction c = new Fraction(-1, 2);
60          String expected = "-1 / 2";
61  
62          String actual = properFormat.format(c);
63          Assert.assertEquals(expected, actual);
64  
65          actual = improperFormat.format(c);
66          Assert.assertEquals(expected, actual);
67      }
68  
69      @Test
70      public void testFormatZero() {
71          Fraction c = new Fraction(0, 1);
72          String expected = "0 / 1";
73  
74          String actual = properFormat.format(c);
75          Assert.assertEquals(expected, actual);
76  
77          actual = improperFormat.format(c);
78          Assert.assertEquals(expected, actual);
79      }
80  
81      @Test
82      public void testFormatImproper() {
83          Fraction c = new Fraction(5, 3);
84  
85          String actual = properFormat.format(c);
86          Assert.assertEquals("1 2 / 3", actual);
87  
88          actual = improperFormat.format(c);
89          Assert.assertEquals("5 / 3", actual);
90      }
91  
92      @Test
93      public void testFormatImproperNegative() {
94          Fraction c = new Fraction(-5, 3);
95  
96          String actual = properFormat.format(c);
97          Assert.assertEquals("-1 2 / 3", actual);
98  
99          actual = improperFormat.format(c);
100         Assert.assertEquals("-5 / 3", actual);
101     }
102 
103     @Test
104     public void testParse() {
105         String source = "1 / 2";
106 
107         try {
108             Fraction c = properFormat.parse(source);
109             Assert.assertNotNull(c);
110             Assert.assertEquals(1, c.getNumerator());
111             Assert.assertEquals(2, c.getDenominator());
112 
113             c = improperFormat.parse(source);
114             Assert.assertNotNull(c);
115             Assert.assertEquals(1, c.getNumerator());
116             Assert.assertEquals(2, c.getDenominator());
117         } catch (MathParseException ex) {
118             Assert.fail(ex.getMessage());
119         }
120     }
121 
122     @Test
123     public void testParseInteger() {
124         String source = "10";
125         {
126             Fraction c = properFormat.parse(source);
127             Assert.assertNotNull(c);
128             Assert.assertEquals(10, c.getNumerator());
129             Assert.assertEquals(1, c.getDenominator());
130         }
131         {
132             Fraction c = improperFormat.parse(source);
133             Assert.assertNotNull(c);
134             Assert.assertEquals(10, c.getNumerator());
135             Assert.assertEquals(1, c.getDenominator());
136         }
137     }
138 
139     @Test
140     public void testParseOne1() {
141         String source = "1 / 1";
142         Fraction c = properFormat.parse(source);
143         Assert.assertNotNull(c);
144         Assert.assertEquals(1, c.getNumerator());
145         Assert.assertEquals(1, c.getDenominator());
146     }
147 
148     @Test
149     public void testParseOne2() {
150         String source = "10 / 10";
151         Fraction c = properFormat.parse(source);
152         Assert.assertNotNull(c);
153         Assert.assertEquals(1, c.getNumerator());
154         Assert.assertEquals(1, c.getDenominator());
155     }
156 
157     @Test
158     public void testParseZero1() {
159         String source = "0 / 1";
160         Fraction c = properFormat.parse(source);
161         Assert.assertNotNull(c);
162         Assert.assertEquals(0, c.getNumerator());
163         Assert.assertEquals(1, c.getDenominator());
164     }
165 
166     @Test
167     public void testParseZero2() {
168         String source = "-0 / 1";
169         Fraction c = properFormat.parse(source);
170         Assert.assertNotNull(c);
171         Assert.assertEquals(0, c.getNumerator());
172         Assert.assertEquals(1, c.getDenominator());
173         // This test shows that the sign is not preserved.
174         Assert.assertEquals(Double.POSITIVE_INFINITY, 1d / c.doubleValue(), 0);
175     }
176 
177     @Test
178     public void testParseInvalid() {
179         String source = "a";
180         String msg = "should not be able to parse '10 / a'.";
181         try {
182             properFormat.parse(source);
183             Assert.fail(msg);
184         } catch (MathParseException ex) {
185             // success
186         }
187         try {
188             improperFormat.parse(source);
189             Assert.fail(msg);
190         } catch (MathParseException ex) {
191             // success
192         }
193     }
194 
195     @Test
196     public void testParseInvalidDenominator() {
197         String source = "10 / a";
198         String msg = "should not be able to parse '10 / a'.";
199         try {
200             properFormat.parse(source);
201             Assert.fail(msg);
202         } catch (MathParseException ex) {
203             // success
204         }
205         try {
206             improperFormat.parse(source);
207             Assert.fail(msg);
208         } catch (MathParseException ex) {
209             // success
210         }
211     }
212 
213     @Test
214     public void testParseNegative() {
215 
216         {
217             String source = "-1 / 2";
218             Fraction c = properFormat.parse(source);
219             Assert.assertNotNull(c);
220             Assert.assertEquals(-1, c.getNumerator());
221             Assert.assertEquals(2, c.getDenominator());
222 
223             c = improperFormat.parse(source);
224             Assert.assertNotNull(c);
225             Assert.assertEquals(-1, c.getNumerator());
226             Assert.assertEquals(2, c.getDenominator());
227 
228             source = "1 / -2";
229             c = properFormat.parse(source);
230             Assert.assertNotNull(c);
231             Assert.assertEquals(-1, c.getNumerator());
232             Assert.assertEquals(2, c.getDenominator());
233 
234             c = improperFormat.parse(source);
235             Assert.assertNotNull(c);
236             Assert.assertEquals(-1, c.getNumerator());
237             Assert.assertEquals(2, c.getDenominator());
238         }
239     }
240 
241     @Test
242     public void testParseProper() {
243         String source = "1 2 / 3";
244 
245         {
246             Fraction c = properFormat.parse(source);
247             Assert.assertNotNull(c);
248             Assert.assertEquals(5, c.getNumerator());
249             Assert.assertEquals(3, c.getDenominator());
250         }
251 
252         try {
253             improperFormat.parse(source);
254             Assert.fail("invalid improper fraction.");
255         } catch (MathParseException ex) {
256             // success
257         }
258     }
259 
260     @Test
261     public void testParseProperNegative() {
262         String source = "-1 2 / 3";
263         {
264             Fraction c = properFormat.parse(source);
265             Assert.assertNotNull(c);
266             Assert.assertEquals(-5, c.getNumerator());
267             Assert.assertEquals(3, c.getDenominator());
268         }
269 
270         try {
271             improperFormat.parse(source);
272             Assert.fail("invalid improper fraction.");
273         } catch (MathParseException ex) {
274             // success
275         }
276     }
277 
278     @Test
279     public void testParseProperInvalidMinus() {
280         String source = "2 -2 / 3";
281         try {
282             properFormat.parse(source);
283             Assert.fail("invalid minus in improper fraction.");
284         } catch (MathParseException ex) {
285             // expected
286         }
287         source = "2 2 / -3";
288         try {
289             properFormat.parse(source);
290             Assert.fail("invalid minus in improper fraction.");
291         } catch (MathParseException ex) {
292             // expected
293         }
294     }
295 
296     @Test
297     public void testNumeratorFormat() {
298         NumberFormat old = properFormat.getNumeratorFormat();
299         NumberFormat nf = NumberFormat.getInstance();
300         nf.setParseIntegerOnly(true);
301         properFormat.setNumeratorFormat(nf);
302         Assert.assertEquals(nf, properFormat.getNumeratorFormat());
303         properFormat.setNumeratorFormat(old);
304 
305         old = improperFormat.getNumeratorFormat();
306         nf = NumberFormat.getInstance();
307         nf.setParseIntegerOnly(true);
308         improperFormat.setNumeratorFormat(nf);
309         Assert.assertEquals(nf, improperFormat.getNumeratorFormat());
310         improperFormat.setNumeratorFormat(old);
311     }
312 
313     @Test
314     public void testDenominatorFormat() {
315         NumberFormat old = properFormat.getDenominatorFormat();
316         NumberFormat nf = NumberFormat.getInstance();
317         nf.setParseIntegerOnly(true);
318         properFormat.setDenominatorFormat(nf);
319         Assert.assertEquals(nf, properFormat.getDenominatorFormat());
320         properFormat.setDenominatorFormat(old);
321 
322         old = improperFormat.getDenominatorFormat();
323         nf = NumberFormat.getInstance();
324         nf.setParseIntegerOnly(true);
325         improperFormat.setDenominatorFormat(nf);
326         Assert.assertEquals(nf, improperFormat.getDenominatorFormat());
327         improperFormat.setDenominatorFormat(old);
328     }
329 
330     @Test
331     public void testWholeFormat() {
332         ProperFractionFormat format = (ProperFractionFormat)properFormat;
333 
334         NumberFormat old = format.getWholeFormat();
335         NumberFormat nf = NumberFormat.getInstance();
336         nf.setParseIntegerOnly(true);
337         format.setWholeFormat(nf);
338         Assert.assertEquals(nf, format.getWholeFormat());
339         format.setWholeFormat(old);
340     }
341 
342     @Test
343     public void testLongFormat() {
344         Assert.assertEquals("10 / 1", improperFormat.format(10l));
345     }
346 
347     @Test
348     public void testDoubleFormat() {
349         Assert.assertEquals("355 / 113", improperFormat.format(FastMath.PI));
350     }
351 }