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