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