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