1   /*
2    * Copyright 2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.commons.math.fraction;
18  
19  import java.text.ParseException;
20  import java.util.Locale;
21  
22  import junit.framework.TestCase;
23  
24  public class FractionFormatTest extends TestCase {
25   
26      FractionFormat properFormat = null;
27      FractionFormat improperFormat = null;
28  
29      protected Locale getLocale() {
30          return Locale.getDefault();
31      }
32  
33      protected void setUp() throws Exception {
34          properFormat = FractionFormat.getProperInstance(getLocale());
35          improperFormat = FractionFormat.getImproperInstance(getLocale());
36      }
37     
38      public void testFormat() {
39          Fraction c = new Fraction(1, 2);
40          String expected = "1 / 2";
41          
42          String actual = properFormat.format(c); 
43          assertEquals(expected, actual);
44  
45          actual = improperFormat.format(c);
46          assertEquals(expected, actual);
47      }
48  
49      public void testFormatNegative() {
50          Fraction c = new Fraction(-1, 2);
51          String expected = "-1 / 2";
52  
53          String actual = properFormat.format(c); 
54          assertEquals(expected, actual);
55  
56          actual = improperFormat.format(c); 
57          assertEquals(expected, actual);
58      }
59  
60      public void testFormatZero() {
61          Fraction c = new Fraction(0, 1);
62          String expected = "0 / 1";
63  
64          String actual = properFormat.format(c); 
65          assertEquals(expected, actual);
66  
67          actual = improperFormat.format(c); 
68          assertEquals(expected, actual);
69      }
70      
71      public void testFormatImproper() {
72          Fraction c = new Fraction(5, 3);
73  
74          String actual = properFormat.format(c); 
75          assertEquals("1 2 / 3", actual);
76  
77          actual = improperFormat.format(c); 
78          assertEquals("5 / 3", actual);
79      }
80      
81      public void testFormatImproperNegative() {
82          Fraction c = new Fraction(-5, 3);
83  
84          String actual = properFormat.format(c); 
85          assertEquals("-1 2 / 3", actual);
86  
87          actual = improperFormat.format(c); 
88          assertEquals("-5 / 3", actual);
89      }
90      
91      public void testParse() {
92          String source = "1 / 2";
93  
94          try {
95              Fraction c = properFormat.parse(source);
96              assertNotNull(c);
97              assertEquals(1, c.getNumerator());
98              assertEquals(2, c.getDenominator());
99              
100             c = improperFormat.parse(source);
101             assertNotNull(c);
102             assertEquals(1, c.getNumerator());
103             assertEquals(2, c.getDenominator());
104         } catch (ParseException ex) {
105             fail(ex.getMessage());
106         }
107     }
108     
109     public void testParseNegative() {
110 
111         try {
112             String source = "-1 / 2";
113             Fraction c = properFormat.parse(source);
114             assertNotNull(c);
115             assertEquals(-1, c.getNumerator());
116             assertEquals(2, c.getDenominator());
117             
118             c = improperFormat.parse(source);
119             assertNotNull(c);
120             assertEquals(-1, c.getNumerator());
121             assertEquals(2, c.getDenominator());
122 
123             source = "1 / -2";
124             c = properFormat.parse(source);
125             assertNotNull(c);
126             assertEquals(-1, c.getNumerator());
127             assertEquals(2, c.getDenominator());
128             
129             c = improperFormat.parse(source);
130             assertNotNull(c);
131             assertEquals(-1, c.getNumerator());
132             assertEquals(2, c.getDenominator());
133         } catch (ParseException ex) {
134             fail(ex.getMessage());
135         }
136     }
137     
138     public void testParseProper() {
139         String source = "1 2 / 3";
140 
141         try {
142             Fraction c = properFormat.parse(source);
143             assertNotNull(c);
144             assertEquals(5, c.getNumerator());
145             assertEquals(3, c.getDenominator());
146         } catch (ParseException ex) {
147             fail(ex.getMessage());
148         }
149         
150         try {
151             improperFormat.parse(source);
152             fail("invalid improper fraction.");
153         } catch (ParseException ex) {
154             // success
155         }
156     }
157     
158     public void testParseProperNegative() {
159         String source = "-1 2 / 3";
160         try {
161             Fraction c = properFormat.parse(source);
162             assertNotNull(c);
163             assertEquals(-5, c.getNumerator());
164             assertEquals(3, c.getDenominator());
165         } catch (ParseException ex) {
166             fail(ex.getMessage());
167         }
168         
169         try {
170             improperFormat.parse(source);
171             fail("invalid improper fraction.");
172         } catch (ParseException ex) {
173             // success
174         }
175     }
176 }