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