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