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