KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > math > FractionTest


1 /*
2  * Copyright 2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.lang.math;
17
18 import junit.framework.Test;
19 import junit.framework.TestCase;
20 import junit.framework.TestSuite;
21
22 /**
23  * Test cases for the {@link Fraction} class
24  *
25  * @author Stephen Colebourne
26  * @author C. Scott Ananian
27  * @version $Id: FractionTest.java 155423 2005-02-26 13:08:30Z dirkv $
28  */

29 public class FractionTest extends TestCase {
30     
31     private static final int SKIP = 500; //53
32

33     public FractionTest(String JavaDoc name) {
34         super(name);
35     }
36
37     public static Test suite() {
38         TestSuite suite = new TestSuite(FractionTest.class);
39         suite.setName("Fraction Tests");
40         return suite;
41     }
42     
43     public void setUp() {
44     }
45
46     //--------------------------------------------------------------------------
47

48     public void testConstants() {
49         assertEquals(0, Fraction.ZERO.getNumerator());
50         assertEquals(1, Fraction.ZERO.getDenominator());
51         
52         assertEquals(1, Fraction.ONE.getNumerator());
53         assertEquals(1, Fraction.ONE.getDenominator());
54         
55         assertEquals(1, Fraction.ONE_HALF.getNumerator());
56         assertEquals(2, Fraction.ONE_HALF.getDenominator());
57         
58         assertEquals(1, Fraction.ONE_THIRD.getNumerator());
59         assertEquals(3, Fraction.ONE_THIRD.getDenominator());
60         
61         assertEquals(2, Fraction.TWO_THIRDS.getNumerator());
62         assertEquals(3, Fraction.TWO_THIRDS.getDenominator());
63         
64         assertEquals(1, Fraction.ONE_QUARTER.getNumerator());
65         assertEquals(4, Fraction.ONE_QUARTER.getDenominator());
66         
67         assertEquals(2, Fraction.TWO_QUARTERS.getNumerator());
68         assertEquals(4, Fraction.TWO_QUARTERS.getDenominator());
69         
70         assertEquals(3, Fraction.THREE_QUARTERS.getNumerator());
71         assertEquals(4, Fraction.THREE_QUARTERS.getDenominator());
72         
73         assertEquals(1, Fraction.ONE_FIFTH.getNumerator());
74         assertEquals(5, Fraction.ONE_FIFTH.getDenominator());
75         
76         assertEquals(2, Fraction.TWO_FIFTHS.getNumerator());
77         assertEquals(5, Fraction.TWO_FIFTHS.getDenominator());
78         
79         assertEquals(3, Fraction.THREE_FIFTHS.getNumerator());
80         assertEquals(5, Fraction.THREE_FIFTHS.getDenominator());
81         
82         assertEquals(4, Fraction.FOUR_FIFTHS.getNumerator());
83         assertEquals(5, Fraction.FOUR_FIFTHS.getDenominator());
84     }
85
86     public void testFactory_int_int() {
87         Fraction f = null;
88         
89         // zero
90
f = Fraction.getFraction(0, 1);
91         assertEquals(0, f.getNumerator());
92         assertEquals(1, f.getDenominator());
93         
94         f = Fraction.getFraction(0, 2);
95         assertEquals(0, f.getNumerator());
96         assertEquals(2, f.getDenominator());
97         
98         // normal
99
f = Fraction.getFraction(1, 1);
100         assertEquals(1, f.getNumerator());
101         assertEquals(1, f.getDenominator());
102         
103         f = Fraction.getFraction(2, 1);
104         assertEquals(2, f.getNumerator());
105         assertEquals(1, f.getDenominator());
106         
107         f = Fraction.getFraction(23, 345);
108         assertEquals(23, f.getNumerator());
109         assertEquals(345, f.getDenominator());
110         
111         // improper
112
f = Fraction.getFraction(22, 7);
113         assertEquals(22, f.getNumerator());
114         assertEquals(7, f.getDenominator());
115         
116         // negatives
117
f = Fraction.getFraction(-6, 10);
118         assertEquals(-6, f.getNumerator());
119         assertEquals(10, f.getDenominator());
120         
121         f = Fraction.getFraction(6, -10);
122         assertEquals(-6, f.getNumerator());
123         assertEquals(10, f.getDenominator());
124         
125         f = Fraction.getFraction(-6, -10);
126         assertEquals(6, f.getNumerator());
127         assertEquals(10, f.getDenominator());
128         
129         // zero denominator
130
try {
131             f = Fraction.getFraction(1, 0);
132             fail("expecting ArithmeticException");
133         } catch (ArithmeticException JavaDoc ex) {}
134         
135         try {
136             f = Fraction.getFraction(2, 0);
137             fail("expecting ArithmeticException");
138         } catch (ArithmeticException JavaDoc ex) {}
139         
140         try {
141             f = Fraction.getFraction(-3, 0);
142             fail("expecting ArithmeticException");
143         } catch (ArithmeticException JavaDoc ex) {}
144
145         // very large: can't represent as unsimplified fraction, although
146
try {
147             f = Fraction.getFraction(4, Integer.MIN_VALUE);
148             fail("expecting ArithmeticException");
149         } catch (ArithmeticException JavaDoc ex) {}
150         try {
151             f = Fraction.getFraction(1, Integer.MIN_VALUE);
152             fail("expecting ArithmeticException");
153         } catch (ArithmeticException JavaDoc ex) {}
154     }
155
156     public void testFactory_int_int_int() {
157         Fraction f = null;
158         
159         // zero
160
f = Fraction.getFraction(0, 0, 2);
161         assertEquals(0, f.getNumerator());
162         assertEquals(2, f.getDenominator());
163         
164         f = Fraction.getFraction(2, 0, 2);
165         assertEquals(4, f.getNumerator());
166         assertEquals(2, f.getDenominator());
167         
168         f = Fraction.getFraction(0, 1, 2);
169         assertEquals(1, f.getNumerator());
170         assertEquals(2, f.getDenominator());
171         
172         // normal
173
f = Fraction.getFraction(1, 1, 2);
174         assertEquals(3, f.getNumerator());
175         assertEquals(2, f.getDenominator());
176         
177         // negatives
178
try {
179             f = Fraction.getFraction(1, -6, -10);
180             fail("expecting ArithmeticException");
181         } catch (ArithmeticException JavaDoc ex) {}
182
183         try {
184             f = Fraction.getFraction(1, -6, -10);
185             fail("expecting ArithmeticException");
186         } catch (ArithmeticException JavaDoc ex) {}
187
188         try {
189             f = Fraction.getFraction(1, -6, -10);
190             fail("expecting ArithmeticException");
191         } catch (ArithmeticException JavaDoc ex) {}
192         
193         // negative whole
194
f = Fraction.getFraction(-1, 6, 10);
195         assertEquals(-16, f.getNumerator());
196         assertEquals(10, f.getDenominator());
197         
198         try {
199             f = Fraction.getFraction(-1, -6, 10);
200             fail("expecting ArithmeticException");
201         } catch (ArithmeticException JavaDoc ex) {}
202
203         try {
204             f = Fraction.getFraction(-1, 6, -10);
205             fail("expecting ArithmeticException");
206         } catch (ArithmeticException JavaDoc ex) {}
207         
208         try {
209             f = Fraction.getFraction(-1, -6, -10);
210             fail("expecting ArithmeticException");
211         } catch (ArithmeticException JavaDoc ex) {}
212         
213         // zero denominator
214
try {
215             f = Fraction.getFraction(0, 1, 0);
216             fail("expecting ArithmeticException");
217         } catch (ArithmeticException JavaDoc ex) {}
218         
219         try {
220             f = Fraction.getFraction(1, 2, 0);
221             fail("expecting ArithmeticException");
222         } catch (ArithmeticException JavaDoc ex) {}
223         
224         try {
225             f = Fraction.getFraction(-1, -3, 0);
226             fail("expecting ArithmeticException");
227         } catch (ArithmeticException JavaDoc ex) {}
228         
229         try {
230             f = Fraction.getFraction(Integer.MAX_VALUE, 1, 2);
231             fail("expecting ArithmeticException");
232         } catch (ArithmeticException JavaDoc ex) {}
233         
234         try {
235             f = Fraction.getFraction(-Integer.MAX_VALUE, 1, 2);
236             fail("expecting ArithmeticException");
237         } catch (ArithmeticException JavaDoc ex) {}
238
239         // very large
240
f = Fraction.getFraction(-1, 0, Integer.MAX_VALUE);
241         assertEquals(-Integer.MAX_VALUE, f.getNumerator());
242         assertEquals(Integer.MAX_VALUE, f.getDenominator());
243
244         try {
245             // negative denominators not allowed in this constructor.
246
f = Fraction.getFraction(0, 4, Integer.MIN_VALUE);
247             fail("expecting ArithmeticException");
248         } catch (ArithmeticException JavaDoc ex) {}
249         try {
250             f = Fraction.getFraction(1, 1, Integer.MAX_VALUE);
251             fail("expecting ArithmeticException");
252         } catch (ArithmeticException JavaDoc ex) {}
253         try {
254             f = Fraction.getFraction(-1, 2, Integer.MAX_VALUE);
255             fail("expecting ArithmeticException");
256         } catch (ArithmeticException JavaDoc ex) {}
257     }
258     public void testReducedFactory_int_int() {
259         Fraction f = null;
260         
261         // zero
262
f = Fraction.getReducedFraction(0, 1);
263         assertEquals(0, f.getNumerator());
264         assertEquals(1, f.getDenominator());
265         
266         // normal
267
f = Fraction.getReducedFraction(1, 1);
268         assertEquals(1, f.getNumerator());
269         assertEquals(1, f.getDenominator());
270         
271         f = Fraction.getReducedFraction(2, 1);
272         assertEquals(2, f.getNumerator());
273         assertEquals(1, f.getDenominator());
274         
275         // improper
276
f = Fraction.getReducedFraction(22, 7);
277         assertEquals(22, f.getNumerator());
278         assertEquals(7, f.getDenominator());
279         
280         // negatives
281
f = Fraction.getReducedFraction(-6, 10);
282         assertEquals(-3, f.getNumerator());
283         assertEquals(5, f.getDenominator());
284         
285         f = Fraction.getReducedFraction(6, -10);
286         assertEquals(-3, f.getNumerator());
287         assertEquals(5, f.getDenominator());
288         
289         f = Fraction.getReducedFraction(-6, -10);
290         assertEquals(3, f.getNumerator());
291         assertEquals(5, f.getDenominator());
292         
293         // zero denominator
294
try {
295             f = Fraction.getReducedFraction(1, 0);
296             fail("expecting ArithmeticException");
297         } catch (ArithmeticException JavaDoc ex) {}
298         
299         try {
300             f = Fraction.getReducedFraction(2, 0);
301             fail("expecting ArithmeticException");
302         } catch (ArithmeticException JavaDoc ex) {}
303         
304         try {
305             f = Fraction.getReducedFraction(-3, 0);
306             fail("expecting ArithmeticException");
307         } catch (ArithmeticException JavaDoc ex) {}
308
309         // reduced
310
f = Fraction.getReducedFraction(0, 2);
311         assertEquals(0, f.getNumerator());
312         assertEquals(1, f.getDenominator());
313         
314         f = Fraction.getReducedFraction(2, 2);
315         assertEquals(1, f.getNumerator());
316         assertEquals(1, f.getDenominator());
317         
318         f = Fraction.getReducedFraction(2, 4);
319         assertEquals(1, f.getNumerator());
320         assertEquals(2, f.getDenominator());
321         
322         f = Fraction.getReducedFraction(15, 10);
323         assertEquals(3, f.getNumerator());
324         assertEquals(2, f.getDenominator());
325         
326         f = Fraction.getReducedFraction(121, 22);
327         assertEquals(11, f.getNumerator());
328         assertEquals(2, f.getDenominator());
329         
330         // Extreme values
331
// OK, can reduce before negating
332
f = Fraction.getReducedFraction(-2, Integer.MIN_VALUE);
333         assertEquals(1, f.getNumerator());
334         assertEquals(-(Integer.MIN_VALUE / 2), f.getDenominator());
335         
336         // Can't reduce, negation will throw
337
try {
338             f = Fraction.getReducedFraction(-7, Integer.MIN_VALUE);
339             fail("Expecting ArithmeticException");
340         } catch (ArithmeticException JavaDoc ex) {}
341     }
342
343     public void testFactory_double() {
344         Fraction f = null;
345         
346         try {
347             f = Fraction.getFraction(Double.NaN);
348             fail("expecting ArithmeticException");
349         } catch (ArithmeticException JavaDoc ex) {}
350         
351         try {
352             f = Fraction.getFraction(Double.POSITIVE_INFINITY);
353             fail("expecting ArithmeticException");
354         } catch (ArithmeticException JavaDoc ex) {}
355         
356         try {
357             f = Fraction.getFraction(Double.NEGATIVE_INFINITY);
358             fail("expecting ArithmeticException");
359         } catch (ArithmeticException JavaDoc ex) {}
360         
361         try {
362             f = Fraction.getFraction((double) Integer.MAX_VALUE + 1);
363             fail("expecting ArithmeticException");
364         } catch (ArithmeticException JavaDoc ex) {}
365         
366         // zero
367
f = Fraction.getFraction(0.0d);
368         assertEquals(0, f.getNumerator());
369         assertEquals(1, f.getDenominator());
370         
371         // one
372
f = Fraction.getFraction(1.0d);
373         assertEquals(1, f.getNumerator());
374         assertEquals(1, f.getDenominator());
375         
376         // one half
377
f = Fraction.getFraction(0.5d);
378         assertEquals(1, f.getNumerator());
379         assertEquals(2, f.getDenominator());
380         
381         // negative
382
f = Fraction.getFraction(-0.875d);
383         assertEquals(-7, f.getNumerator());
384         assertEquals(8, f.getDenominator());
385         
386         // over 1
387
f = Fraction.getFraction(1.25d);
388         assertEquals(5, f.getNumerator());
389         assertEquals(4, f.getDenominator());
390         
391         // two thirds
392
f = Fraction.getFraction(0.66666d);
393         assertEquals(2, f.getNumerator());
394         assertEquals(3, f.getDenominator());
395         
396         // small
397
f = Fraction.getFraction(1.0d/10001d);
398         assertEquals(0, f.getNumerator());
399         assertEquals(1, f.getDenominator());
400         
401         // normal
402
Fraction f2 = null;
403         int remainder, number1, number2 = 0;
404         for (int i = 1; i <= 100; i++) { // denominator
405
for (int j = 1; j <= i; j++) { // numerator
406
try {
407                     f = Fraction.getFraction((double) j / (double) i);
408                 } catch (ArithmeticException JavaDoc ex) {
409                     System.err.println(j + " " + i);
410                     throw ex;
411                 }
412                 f2 = Fraction.getReducedFraction(j, i);
413                 assertEquals(f2.getNumerator(), f.getNumerator());
414                 assertEquals(f2.getDenominator(), f.getDenominator());
415             }
416         }
417         // save time by skipping some tests! (
418
for (int i = 1001; i <= 10000; i+=SKIP) { // denominator
419
for (int j = 1; j <= i; j++) { // numerator
420
try {
421                     f = Fraction.getFraction((double) j / (double) i);
422                 } catch (ArithmeticException JavaDoc ex) {
423                     System.err.println(j + " " + i);
424                     throw ex;
425                 }
426                 f2 = Fraction.getReducedFraction(j, i);
427                 assertEquals(f2.getNumerator(), f.getNumerator());
428                 assertEquals(f2.getDenominator(), f.getDenominator());
429             }
430         }
431     }
432
433     public void testFactory_String() {
434         try {
435             Fraction.getFraction(null);
436             fail("expecting IllegalArgumentException");
437         } catch (IllegalArgumentException JavaDoc ex) {}
438     }
439     
440     
441     public void testFactory_String_double() {
442         Fraction f = null;
443         
444         f = Fraction.getFraction("0.0");
445         assertEquals(0, f.getNumerator());
446         assertEquals(1, f.getDenominator());
447         
448         f = Fraction.getFraction("0.2");
449         assertEquals(1, f.getNumerator());
450         assertEquals(5, f.getDenominator());
451         
452         f = Fraction.getFraction("0.5");
453         assertEquals(1, f.getNumerator());
454         assertEquals(2, f.getDenominator());
455         
456         f = Fraction.getFraction("0.66666");
457         assertEquals(2, f.getNumerator());
458         assertEquals(3, f.getDenominator());
459         
460         try {
461             f = Fraction.getFraction("2.3R");
462             fail("Expecting NumberFormatException");
463         } catch (NumberFormatException JavaDoc ex) {}
464         
465         try {
466             f = Fraction.getFraction("2147483648"); // too big
467
fail("Expecting NumberFormatException");
468         } catch (NumberFormatException JavaDoc ex) {}
469         
470         try {
471             f = Fraction.getFraction(".");
472             fail("Expecting NumberFormatException");
473         } catch (NumberFormatException JavaDoc ex) {}
474     }
475
476     public void testFactory_String_proper() {
477         Fraction f = null;
478         
479         f = Fraction.getFraction("0 0/1");
480         assertEquals(0, f.getNumerator());
481         assertEquals(1, f.getDenominator());
482         
483         f = Fraction.getFraction("1 1/5");
484         assertEquals(6, f.getNumerator());
485         assertEquals(5, f.getDenominator());
486         
487         f = Fraction.getFraction("7 1/2");
488         assertEquals(15, f.getNumerator());
489         assertEquals(2, f.getDenominator());
490         
491         f = Fraction.getFraction("1 2/4");
492         assertEquals(6, f.getNumerator());
493         assertEquals(4, f.getDenominator());
494         
495         f = Fraction.getFraction("-7 1/2");
496         assertEquals(-15, f.getNumerator());
497         assertEquals(2, f.getDenominator());
498         
499         f = Fraction.getFraction("-1 2/4");
500         assertEquals(-6, f.getNumerator());
501         assertEquals(4, f.getDenominator());
502         
503         try {
504             f = Fraction.getFraction("2 3");
505             fail("expecting NumberFormatException");
506         } catch (NumberFormatException JavaDoc ex) {}
507         
508         try {
509             f = Fraction.getFraction("a 3");
510             fail("expecting NumberFormatException");
511         } catch (NumberFormatException JavaDoc ex) {}
512         
513         try {
514             f = Fraction.getFraction("2 b/4");
515             fail("expecting NumberFormatException");
516         } catch (NumberFormatException JavaDoc ex) {}
517         
518         try {
519             f = Fraction.getFraction("2 ");
520             fail("expecting NumberFormatException");
521         } catch (NumberFormatException JavaDoc ex) {}
522
523         try {
524             f = Fraction.getFraction(" 3");
525             fail("expecting NumberFormatException");
526         } catch (NumberFormatException JavaDoc ex) {}
527         
528         try {
529             f = Fraction.getFraction(" ");
530             fail("expecting NumberFormatException");
531         } catch (NumberFormatException JavaDoc ex) {}
532     }
533
534     public void testFactory_String_improper() {
535         Fraction f = null;
536         
537         f = Fraction.getFraction("0/1");
538         assertEquals(0, f.getNumerator());
539         assertEquals(1, f.getDenominator());
540         
541         f = Fraction.getFraction("1/5");
542         assertEquals(1, f.getNumerator());
543         assertEquals(5, f.getDenominator());
544         
545         f = Fraction.getFraction("1/2");
546         assertEquals(1, f.getNumerator());
547         assertEquals(2, f.getDenominator());
548         
549         f = Fraction.getFraction("2/3");
550         assertEquals(2, f.getNumerator());
551         assertEquals(3, f.getDenominator());
552         
553         f = Fraction.getFraction("7/3");
554         assertEquals(7, f.getNumerator());
555         assertEquals(3, f.getDenominator());
556         
557         f = Fraction.getFraction("2/4");
558         assertEquals(2, f.getNumerator());
559         assertEquals(4, f.getDenominator());
560         
561         try {
562             f = Fraction.getFraction("2/d");
563             fail("expecting NumberFormatException");
564         } catch (NumberFormatException JavaDoc ex) {}
565         
566         try {
567             f = Fraction.getFraction("2e/3");
568             fail("expecting NumberFormatException");
569         } catch (NumberFormatException JavaDoc ex) {}
570         
571         try {
572             f = Fraction.getFraction("2/");
573             fail("expecting NumberFormatException");
574         } catch (NumberFormatException JavaDoc ex) {}
575         
576         try {
577             f = Fraction.getFraction("/");
578             fail("expecting NumberFormatException");
579         } catch (NumberFormatException JavaDoc ex) {}
580     }
581
582     public void testGets() {
583         Fraction f = null;
584         
585         f = Fraction.getFraction(3, 5, 6);
586         assertEquals(23, f.getNumerator());
587         assertEquals(3, f.getProperWhole());
588         assertEquals(5, f.getProperNumerator());
589         assertEquals(6, f.getDenominator());
590         
591         f = Fraction.getFraction(-3, 5, 6);
592         assertEquals(-23, f.getNumerator());
593         assertEquals(-3, f.getProperWhole());
594         assertEquals(5, f.getProperNumerator());
595         assertEquals(6, f.getDenominator());
596
597         f = Fraction.getFraction(Integer.MIN_VALUE, 0, 1);
598         assertEquals(Integer.MIN_VALUE, f.getNumerator());
599         assertEquals(Integer.MIN_VALUE, f.getProperWhole());
600         assertEquals(0, f.getProperNumerator());
601         assertEquals(1, f.getDenominator());
602     }
603             
604     public void testConversions() {
605         Fraction f = null;
606         
607         f = Fraction.getFraction(3, 7, 8);
608         assertEquals(3, f.intValue());
609         assertEquals(3L, f.longValue());
610         assertEquals(3.875f, f.floatValue(), 0.00001f);
611         assertEquals(3.875d, f.doubleValue(), 0.00001d);
612     }
613     
614     public void testReduce() {
615         Fraction f = null;
616         
617         f = Fraction.getFraction(50, 75);
618         f = f.reduce();
619         assertEquals(2, f.getNumerator());
620         assertEquals(3, f.getDenominator());
621
622         f = Fraction.getFraction(2, 3);
623         f = f.reduce();
624         assertEquals(2, f.getNumerator());
625         assertEquals(3, f.getDenominator());
626     }
627     
628     public void testInvert() {
629         Fraction f = null;
630         
631         f = Fraction.getFraction(50, 75);
632         f = f.invert();
633         assertEquals(75, f.getNumerator());
634         assertEquals(50, f.getDenominator());
635         
636         f = Fraction.getFraction(4, 3);
637         f = f.invert();
638         assertEquals(3, f.getNumerator());
639         assertEquals(4, f.getDenominator());
640         
641         f = Fraction.getFraction(-15, 47);
642         f = f.invert();
643         assertEquals(-47, f.getNumerator());
644         assertEquals(15, f.getDenominator());
645         
646         f = Fraction.getFraction(0, 3);
647         try {
648             f = f.invert();
649             fail("expecting ArithmeticException");
650         } catch (ArithmeticException JavaDoc ex) {}
651
652         // large values
653
f = Fraction.getFraction(Integer.MIN_VALUE, 1);
654         try {
655             f = f.invert();
656             fail("expecting ArithmeticException");
657         } catch (ArithmeticException JavaDoc ex) {}
658
659         f = Fraction.getFraction(Integer.MAX_VALUE, 1);
660         f = f.invert();
661         assertEquals(1, f.getNumerator());
662         assertEquals(Integer.MAX_VALUE, f.getDenominator());
663     }
664     
665     public void testNegate() {
666         Fraction f = null;
667         
668         f = Fraction.getFraction(50, 75);
669         f = f.negate();
670         assertEquals(-50, f.getNumerator());
671         assertEquals(75, f.getDenominator());
672         
673         f = Fraction.getFraction(-50, 75);
674         f = f.negate();
675         assertEquals(50, f.getNumerator());
676         assertEquals(75, f.getDenominator());
677
678         // large values
679
f = Fraction.getFraction(Integer.MAX_VALUE-1, Integer.MAX_VALUE);
680         f = f.negate();
681         assertEquals(Integer.MIN_VALUE+2, f.getNumerator());
682         assertEquals(Integer.MAX_VALUE, f.getDenominator());
683
684         f = Fraction.getFraction(Integer.MIN_VALUE, 1);
685         try {
686             f = f.negate();
687             fail("expecting ArithmeticException");
688         } catch (ArithmeticException JavaDoc ex) {}
689     }
690     
691     public void testAbs() {
692         Fraction f = null;
693         
694         f = Fraction.getFraction(50, 75);
695         f = f.abs();
696         assertEquals(50, f.getNumerator());
697         assertEquals(75, f.getDenominator());
698         
699         f = Fraction.getFraction(-50, 75);
700         f = f.abs();
701         assertEquals(50, f.getNumerator());
702         assertEquals(75, f.getDenominator());
703
704         f = Fraction.getFraction(Integer.MAX_VALUE, 1);
705         f = f.abs();
706         assertEquals(Integer.MAX_VALUE, f.getNumerator());
707         assertEquals(1, f.getDenominator());
708
709         f = Fraction.getFraction(Integer.MAX_VALUE, -1);
710         f = f.abs();
711         assertEquals(Integer.MAX_VALUE, f.getNumerator());
712         assertEquals(1, f.getDenominator());
713
714         f = Fraction.getFraction(Integer.MIN_VALUE, 1);
715         try {
716             f = f.abs();
717             fail("expecting ArithmeticException");
718         } catch (ArithmeticException JavaDoc ex) {}
719     }
720     
721     public void testPow() {
722         Fraction f = null;
723         
724         f = Fraction.getFraction(3, 5);
725         assertEquals(Fraction.ONE, f.pow(0));
726         
727         f = Fraction.getFraction(3, 5);
728         assertSame(f, f.pow(1));
729         assertEquals(f, f.pow(1));
730
731         f = Fraction.getFraction(3, 5);
732         f = f.pow(2);
733         assertEquals(9, f.getNumerator());
734         assertEquals(25, f.getDenominator());
735         
736         f = Fraction.getFraction(3, 5);
737         f = f.pow(3);
738         assertEquals(27, f.getNumerator());
739         assertEquals(125, f.getDenominator());
740         
741         f = Fraction.getFraction(3, 5);
742         f = f.pow(-1);
743         assertEquals(5, f.getNumerator());
744         assertEquals(3, f.getDenominator());
745         
746         f = Fraction.getFraction(3, 5);
747         f = f.pow(-2);
748         assertEquals(25, f.getNumerator());
749         assertEquals(9, f.getDenominator());
750         
751         // check unreduced fractions stay that way.
752
f = Fraction.getFraction(6, 10);
753         assertEquals(Fraction.ONE, f.pow(0));
754         
755         f = Fraction.getFraction(6, 10);
756         assertEquals(f, f.pow(1));
757         assertFalse(f.pow(1).equals(Fraction.getFraction(3,5)));
758
759         f = Fraction.getFraction(6, 10);
760         f = f.pow(2);
761         assertEquals(9, f.getNumerator());
762         assertEquals(25, f.getDenominator());
763         
764         f = Fraction.getFraction(6, 10);
765         f = f.pow(3);
766         assertEquals(27, f.getNumerator());
767         assertEquals(125, f.getDenominator());
768         
769         f = Fraction.getFraction(6, 10);
770         f = f.pow(-1);
771         assertEquals(10, f.getNumerator());
772         assertEquals(6, f.getDenominator());
773         
774         f = Fraction.getFraction(6, 10);
775         f = f.pow(-2);
776         assertEquals(25, f.getNumerator());
777         assertEquals(9, f.getDenominator());
778         
779         // zero to any positive power is still zero.
780
f = Fraction.getFraction(0, 1231);
781         f = f.pow(1);
782         assertTrue(0==f.compareTo(Fraction.ZERO));
783         assertEquals(0, f.getNumerator());
784         assertEquals(1231, f.getDenominator());
785         f = f.pow(2);
786         assertTrue(0==f.compareTo(Fraction.ZERO));
787         assertEquals(0, f.getNumerator());
788         assertEquals(1, f.getDenominator());
789
790         // zero to negative powers should throw an exception
791
try {
792             f = f.pow(-1);
793             fail("expecting ArithmeticException");
794         } catch (ArithmeticException JavaDoc ex) {}
795         try {
796             f = f.pow(Integer.MIN_VALUE);
797             fail("expecting ArithmeticException");
798         } catch (ArithmeticException JavaDoc ex) {}
799
800         // one to any power is still one.
801
f = Fraction.getFraction(1, 1);
802         f = f.pow(0);
803         assertEquals(f, Fraction.ONE);
804         f = f.pow(1);
805         assertEquals(f, Fraction.ONE);
806         f = f.pow(-1);
807         assertEquals(f, Fraction.ONE);
808         f = f.pow(Integer.MAX_VALUE);
809         assertEquals(f, Fraction.ONE);
810         f = f.pow(Integer.MIN_VALUE);
811         assertEquals(f, Fraction.ONE);
812
813         f = Fraction.getFraction(Integer.MAX_VALUE, 1);
814         try {
815             f = f.pow(2);
816             fail("expecting ArithmeticException");
817         } catch (ArithmeticException JavaDoc ex) {}
818
819         // Numerator growing too negative during the pow operation.
820
f = Fraction.getFraction(Integer.MIN_VALUE, 1);
821         try {
822             f = f.pow(3);
823             fail("expecting ArithmeticException");
824         } catch (ArithmeticException JavaDoc ex) {}
825
826         f = Fraction.getFraction(65536, 1);
827         try {
828             f = f.pow(2);
829             fail("expecting ArithmeticException");
830         } catch (ArithmeticException JavaDoc ex) {}
831     }
832     
833     public void testAdd() {
834         Fraction f = null;
835         Fraction f1 = null;
836         Fraction f2 = null;
837         
838         f1 = Fraction.getFraction(3, 5);
839         f2 = Fraction.getFraction(1, 5);
840         f = f1.add(f2);
841         assertEquals(4, f.getNumerator());
842         assertEquals(5, f.getDenominator());
843         
844         f1 = Fraction.getFraction(3, 5);
845         f2 = Fraction.getFraction(2, 5);
846         f = f1.add(f2);
847         assertEquals(1, f.getNumerator());
848         assertEquals(1, f.getDenominator());
849         
850         f1 = Fraction.getFraction(3, 5);
851         f2 = Fraction.getFraction(3, 5);
852         f = f1.add(f2);
853         assertEquals(6, f.getNumerator());
854         assertEquals(5, f.getDenominator());
855         
856         f1 = Fraction.getFraction(3, 5);
857         f2 = Fraction.getFraction(-4, 5);
858         f = f1.add(f2);
859         assertEquals(-1, f.getNumerator());
860         assertEquals(5, f.getDenominator());
861         
862         f1 = Fraction.getFraction(Integer.MAX_VALUE - 1, 1);
863         f2 = Fraction.ONE;
864         f = f1.add(f2);
865         assertEquals(Integer.MAX_VALUE, f.getNumerator());
866         assertEquals(1, f.getDenominator());
867         
868         f1 = Fraction.getFraction(3, 5);
869         f2 = Fraction.getFraction(1, 2);
870         f = f1.add(f2);
871         assertEquals(11, f.getNumerator());
872         assertEquals(10, f.getDenominator());
873         
874         f1 = Fraction.getFraction(3, 8);
875         f2 = Fraction.getFraction(1, 6);
876         f = f1.add(f2);
877         assertEquals(13, f.getNumerator());
878         assertEquals(24, f.getDenominator());
879         
880         f1 = Fraction.getFraction(0, 5);
881         f2 = Fraction.getFraction(1, 5);
882         f = f1.add(f2);
883         assertSame(f2, f);
884         f = f2.add(f1);
885         assertSame(f2, f);
886         
887         f1 = Fraction.getFraction(-1, 13*13*2*2);
888         f2 = Fraction.getFraction(-2, 13*17*2);
889         f = f1.add(f2);
890         assertEquals(13*13*17*2*2, f.getDenominator());
891         assertEquals(-17 - 2*13*2, f.getNumerator());
892         
893         try {
894             f.add(null);
895             fail("expecting IllegalArgumentException");
896         } catch (IllegalArgumentException JavaDoc ex) {}
897         
898         // if this fraction is added naively, it will overflow.
899
// check that it doesn't.
900
f1 = Fraction.getFraction(1,32768*3);
901         f2 = Fraction.getFraction(1,59049);
902         f = f1.add(f2);
903         assertEquals(52451, f.getNumerator());
904         assertEquals(1934917632, f.getDenominator());
905
906         f1 = Fraction.getFraction(Integer.MIN_VALUE, 3);
907         f2 = Fraction.ONE_THIRD;
908         f = f1.add(f2);
909         assertEquals(Integer.MIN_VALUE+1, f.getNumerator());
910         assertEquals(3, f.getDenominator());
911         
912         f1 = Fraction.getFraction(Integer.MAX_VALUE - 1, 1);
913         f2 = Fraction.ONE;
914         f = f1.add(f2);
915         assertEquals(Integer.MAX_VALUE, f.getNumerator());
916         assertEquals(1, f.getDenominator());
917         
918         try {
919             f = f.add(Fraction.ONE); // should overflow
920
fail("expecting ArithmeticException but got: " + f.toString());
921         } catch (ArithmeticException JavaDoc ex) {}
922         
923         // denominator should not be a multiple of 2 or 3 to trigger overflow
924
f1 = Fraction.getFraction(Integer.MIN_VALUE, 5);
925         f2 = Fraction.getFraction(-1,5);
926         try {
927             f = f1.add(f2); // should overflow
928
fail("expecting ArithmeticException but got: " + f.toString());
929         } catch (ArithmeticException JavaDoc ex) {}
930         
931         try {
932             f= Fraction.getFraction(-Integer.MAX_VALUE, 1);
933             f = f.add(f);
934             fail("expecting ArithmeticException");
935         } catch (ArithmeticException JavaDoc ex) {}
936             
937         try {
938             f= Fraction.getFraction(-Integer.MAX_VALUE, 1);
939             f = f.add(f);
940             fail("expecting ArithmeticException");
941         } catch (ArithmeticException JavaDoc ex) {}
942             
943         f1 = Fraction.getFraction(3,327680);
944         f2 = Fraction.getFraction(2,59049);
945         try {
946             f = f1.add(f2); // should overflow
947
fail("expecting ArithmeticException but got: " + f.toString());
948         } catch (ArithmeticException JavaDoc ex) {}
949     }
950             
951     public void testSubtract() {
952         Fraction f = null;
953         Fraction f1 = null;
954         Fraction f2 = null;
955         
956         f1 = Fraction.getFraction(3, 5);
957         f2 = Fraction.getFraction(1, 5);
958         f = f1.subtract(f2);
959         assertEquals(2, f.getNumerator());
960         assertEquals(5, f.getDenominator());
961         
962         f1 = Fraction.getFraction(7, 5);
963         f2 = Fraction.getFraction(2, 5);
964         f = f1.subtract(f2);
965         assertEquals(1, f.getNumerator());
966         assertEquals(1, f.getDenominator());
967         
968         f1 = Fraction.getFraction(3, 5);
969         f2 = Fraction.getFraction(3, 5);
970         f = f1.subtract(f2);
971         assertEquals(0, f.getNumerator());
972         assertEquals(1, f.getDenominator());
973         
974         f1 = Fraction.getFraction(3, 5);
975         f2 = Fraction.getFraction(-4, 5);
976         f = f1.subtract(f2);
977         assertEquals(7, f.getNumerator());
978         assertEquals(5, f.getDenominator());
979         
980         f1 = Fraction.getFraction(0, 5);
981         f2 = Fraction.getFraction(4, 5);
982         f = f1.subtract(f2);
983         assertEquals(-4, f.getNumerator());
984         assertEquals(5, f.getDenominator());
985         
986         f1 = Fraction.getFraction(0, 5);
987         f2 = Fraction.getFraction(-4, 5);
988         f = f1.subtract(f2);
989         assertEquals(4, f.getNumerator());
990         assertEquals(5, f.getDenominator());
991         
992         f1 = Fraction.getFraction(3, 5);
993         f2 = Fraction.getFraction(1, 2);
994         f = f1.subtract(f2);
995         assertEquals(1, f.getNumerator());
996         assertEquals(10, f.getDenominator());
997         
998         f1 = Fraction.getFraction(0, 5);
999         f2 = Fraction.getFraction(1, 5);
1000        f = f2.subtract(f1);
1001        assertSame(f2, f);
1002        
1003        try {
1004            f.subtract(null);
1005            fail("expecting IllegalArgumentException");
1006        } catch (IllegalArgumentException JavaDoc ex) {}
1007        
1008        // if this fraction is subtracted naively, it will overflow.
1009
// check that it doesn't.
1010
f1 = Fraction.getFraction(1,32768*3);
1011        f2 = Fraction.getFraction(1,59049);
1012        f = f1.subtract(f2);
1013        assertEquals(-13085, f.getNumerator());
1014        assertEquals(1934917632, f.getDenominator());
1015
1016        f1 = Fraction.getFraction(Integer.MIN_VALUE, 3);
1017        f2 = Fraction.ONE_THIRD.negate();
1018        f = f1.subtract(f2);
1019        assertEquals(Integer.MIN_VALUE+1, f.getNumerator());
1020        assertEquals(3, f.getDenominator());
1021        
1022        f1 = Fraction.getFraction(Integer.MAX_VALUE, 1);
1023        f2 = Fraction.ONE;
1024        f = f1.subtract(f2);
1025        assertEquals(Integer.MAX_VALUE-1, f.getNumerator());
1026        assertEquals(1, f.getDenominator());
1027
1028        try {
1029            f1 = Fraction.getFraction(1, Integer.MAX_VALUE);
1030            f2 = Fraction.getFraction(1, Integer.MAX_VALUE - 1);
1031            f = f1.subtract(f2);
1032            fail("expecting ArithmeticException"); //should overflow
1033
} catch (ArithmeticException JavaDoc ex) {}
1034            
1035        // denominator should not be a multiple of 2 or 3 to trigger overflow
1036
f1 = Fraction.getFraction(Integer.MIN_VALUE, 5);
1037        f2 = Fraction.getFraction(1,5);
1038        try {
1039            f = f1.subtract(f2); // should overflow
1040
fail("expecting ArithmeticException but got: " + f.toString());
1041        } catch (ArithmeticException JavaDoc ex) {}
1042        
1043        try {
1044            f= Fraction.getFraction(Integer.MIN_VALUE, 1);
1045            f = f.subtract(Fraction.ONE);
1046            fail("expecting ArithmeticException");
1047        } catch (ArithmeticException JavaDoc ex) {}
1048            
1049        try {
1050            f= Fraction.getFraction(Integer.MAX_VALUE, 1);
1051            f = f.subtract(Fraction.ONE.negate());
1052            fail("expecting ArithmeticException");
1053        } catch (ArithmeticException JavaDoc ex) {}
1054            
1055        f1 = Fraction.getFraction(3,327680);
1056        f2 = Fraction.getFraction(2,59049);
1057        try {
1058            f = f1.subtract(f2); // should overflow
1059
fail("expecting ArithmeticException but got: " + f.toString());
1060        } catch (ArithmeticException JavaDoc ex) {}
1061    }
1062            
1063    public void testMultiply() {
1064        Fraction f = null;
1065        Fraction f1 = null;
1066        Fraction f2 = null;
1067        
1068        f1 = Fraction.getFraction(3, 5);
1069        f2 = Fraction.getFraction(2, 5);
1070        f = f1.multiplyBy(f2);
1071        assertEquals(6, f.getNumerator());
1072        assertEquals(25, f.getDenominator());
1073        
1074        f1 = Fraction.getFraction(6, 10);
1075        f2 = Fraction.getFraction(6, 10);
1076        f = f1.multiplyBy(f2);
1077        assertEquals(9, f.getNumerator());
1078        assertEquals(25, f.getDenominator());
1079        f = f.multiplyBy(f2);
1080        assertEquals(27, f.getNumerator());
1081        assertEquals(125, f.getDenominator());
1082        
1083        f1 = Fraction.getFraction(3, 5);
1084        f2 = Fraction.getFraction(-2, 5);
1085        f = f1.multiplyBy(f2);
1086        assertEquals(-6, f.getNumerator());
1087        assertEquals(25, f.getDenominator());
1088        
1089        f1 = Fraction.getFraction(-3, 5);
1090        f2 = Fraction.getFraction(-2, 5);
1091        f = f1.multiplyBy(f2);
1092        assertEquals(6, f.getNumerator());
1093        assertEquals(25, f.getDenominator());
1094        
1095        
1096        f1 = Fraction.getFraction(0, 5);
1097        f2 = Fraction.getFraction(2, 7);
1098        f = f1.multiplyBy(f2);
1099        assertSame(Fraction.ZERO, f);
1100        
1101        f1 = Fraction.getFraction(2, 7);
1102        f2 = Fraction.ONE;
1103        f = f1.multiplyBy(f2);
1104        assertEquals(2, f.getNumerator());
1105        assertEquals(7, f.getDenominator());
1106        
1107        f1 = Fraction.getFraction(Integer.MAX_VALUE, 1);
1108        f2 = Fraction.getFraction(Integer.MIN_VALUE, Integer.MAX_VALUE);
1109        f = f1.multiplyBy(f2);
1110        assertEquals(Integer.MIN_VALUE, f.getNumerator());
1111        assertEquals(1, f.getDenominator());
1112
1113        try {
1114            f.multiplyBy(null);
1115            fail("expecting IllegalArgumentException");
1116        } catch (IllegalArgumentException JavaDoc ex) {}
1117        
1118        try {
1119            f1 = Fraction.getFraction(1, Integer.MAX_VALUE);
1120            f = f1.multiplyBy(f1); // should overflow
1121
fail("expecting ArithmeticException");
1122        } catch (ArithmeticException JavaDoc ex) {}
1123            
1124        try {
1125            f1 = Fraction.getFraction(1, -Integer.MAX_VALUE);
1126            f = f1.multiplyBy(f1); // should overflow
1127
fail("expecting ArithmeticException");
1128        } catch (ArithmeticException JavaDoc ex) {}
1129    }
1130            
1131    public void testDivide() {
1132        Fraction f = null;
1133        Fraction f1 = null;
1134        Fraction f2 = null;
1135        
1136        f1 = Fraction.getFraction(3, 5);
1137        f2 = Fraction.getFraction(2, 5);
1138        f = f1.divideBy(f2);
1139        assertEquals(3, f.getNumerator());
1140        assertEquals(2, f.getDenominator());
1141        
1142        f1 = Fraction.getFraction(3, 5);
1143        f2 = Fraction.ZERO;
1144        try {
1145            f = f1.divideBy(f2);
1146            fail("expecting ArithmeticException");
1147        } catch (ArithmeticException JavaDoc ex) {}
1148        
1149        f1 = Fraction.getFraction(0, 5);
1150        f2 = Fraction.getFraction(2, 7);
1151        f = f1.divideBy(f2);
1152        assertSame(Fraction.ZERO, f);
1153        
1154        f1 = Fraction.getFraction(2, 7);
1155        f2 = Fraction.ONE;
1156        f = f1.divideBy(f2);
1157        assertEquals(2, f.getNumerator());
1158        assertEquals(7, f.getDenominator());
1159        
1160        f1 = Fraction.getFraction(1, Integer.MAX_VALUE);
1161        f = f1.divideBy(f1);
1162        assertEquals(1, f.getNumerator());
1163        assertEquals(1, f.getDenominator());
1164        
1165        f1 = Fraction.getFraction(Integer.MIN_VALUE, Integer.MAX_VALUE);
1166        f2 = Fraction.getFraction(1, Integer.MAX_VALUE);
1167        f = f1.divideBy(f2);
1168        assertEquals(Integer.MIN_VALUE, f.getNumerator());
1169        assertEquals(1, f.getDenominator());
1170
1171        try {
1172            f.divideBy(null);
1173            fail("IllegalArgumentException");
1174        } catch (IllegalArgumentException JavaDoc ex) {}
1175        
1176        try {
1177            f1 = Fraction.getFraction(1, Integer.MAX_VALUE);
1178            f = f1.divideBy(f1.invert()); // should overflow
1179
fail("expecting ArithmeticException");
1180        } catch (ArithmeticException JavaDoc ex) {}
1181        try {
1182            f1 = Fraction.getFraction(1, -Integer.MAX_VALUE);
1183            f = f1.divideBy(f1.invert()); // should overflow
1184
fail("expecting ArithmeticException");
1185        } catch (ArithmeticException JavaDoc ex) {}
1186    }
1187            
1188    public void testEquals() {
1189        Fraction f1 = null;
1190        Fraction f2 = null;
1191        
1192        f1 = Fraction.getFraction(3, 5);
1193        assertEquals(false, f1.equals(null));
1194        assertEquals(false, f1.equals(new Object JavaDoc()));
1195        assertEquals(false, f1.equals(new Integer JavaDoc(6)));
1196        
1197        f1 = Fraction.getFraction(3, 5);
1198        f2 = Fraction.getFraction(2, 5);
1199        assertEquals(false, f1.equals(f2));
1200        assertEquals(true, f1.equals(f1));
1201        assertEquals(true, f2.equals(f2));
1202        
1203        f2 = Fraction.getFraction(3, 5);
1204        assertEquals(true, f1.equals(f2));
1205        
1206        f2 = Fraction.getFraction(6, 10);
1207        assertEquals(false, f1.equals(f2));
1208    }
1209    
1210    public void testHashCode() {
1211        Fraction f1 = Fraction.getFraction(3, 5);
1212        Fraction f2 = Fraction.getFraction(3, 5);
1213        
1214        assertTrue(f1.hashCode() == f2.hashCode());
1215        
1216        f2 = Fraction.getFraction(2, 5);
1217        assertTrue(f1.hashCode() != f2.hashCode());
1218        
1219        f2 = Fraction.getFraction(6, 10);
1220        assertTrue(f1.hashCode() != f2.hashCode());
1221    }
1222    
1223    public void testCompareTo() {
1224        Fraction f1 = null;
1225        Fraction f2 = null;
1226        
1227        f1 = Fraction.getFraction(3, 5);
1228        assertTrue(f1.compareTo(f1) == 0);
1229        
1230        try {
1231            f1.compareTo(null);
1232            fail("expecting NullPointerException");
1233        } catch (NullPointerException JavaDoc ex) {}
1234        
1235        try {
1236            f1.compareTo(new Object JavaDoc());
1237            fail("expecting ClassCastException");
1238        } catch (ClassCastException JavaDoc ex) {}
1239        
1240        f2 = Fraction.getFraction(2, 5);
1241        assertTrue(f1.compareTo(f2) > 0);
1242        assertTrue(f2.compareTo(f2) == 0);
1243        
1244        f2 = Fraction.getFraction(4, 5);
1245        assertTrue(f1.compareTo(f2) < 0);
1246        assertTrue(f2.compareTo(f2) == 0);
1247        
1248        f2 = Fraction.getFraction(3, 5);
1249        assertTrue(f1.compareTo(f2) == 0);
1250        assertTrue(f2.compareTo(f2) == 0);
1251        
1252        f2 = Fraction.getFraction(6, 10);
1253        assertTrue(f1.compareTo(f2) == 0);
1254        assertTrue(f2.compareTo(f2) == 0);
1255
1256        f2 = Fraction.getFraction(-1, 1, Integer.MAX_VALUE);
1257        assertTrue(f1.compareTo(f2) > 0);
1258        assertTrue(f2.compareTo(f2) == 0);
1259
1260    }
1261    
1262    public void testToString() {
1263        Fraction f = null;
1264
1265        f = Fraction.getFraction(3, 5);
1266        String JavaDoc str = f.toString();
1267        assertEquals("3/5", str);
1268        assertSame(str, f.toString());
1269        
1270        f = Fraction.getFraction(7, 5);
1271        assertEquals("7/5", f.toString());
1272        
1273        f = Fraction.getFraction(4, 2);
1274        assertEquals("4/2", f.toString());
1275        
1276        f = Fraction.getFraction(0, 2);
1277        assertEquals("0/2", f.toString());
1278        
1279        f = Fraction.getFraction(2, 2);
1280        assertEquals("2/2", f.toString());
1281
1282        f = Fraction.getFraction(Integer.MIN_VALUE, 0, 1);
1283        assertEquals("-2147483648/1", f.toString());
1284
1285        f = Fraction.getFraction(-1, 1, Integer.MAX_VALUE);
1286        assertEquals("-2147483648/2147483647", f.toString());
1287    }
1288    
1289    public void testToProperString() {
1290        Fraction f = null;
1291
1292        f = Fraction.getFraction(3, 5);
1293        String JavaDoc str = f.toProperString();
1294        assertEquals("3/5", str);
1295        assertSame(str, f.toProperString());
1296        
1297        f = Fraction.getFraction(7, 5);
1298        assertEquals("1 2/5", f.toProperString());
1299        
1300        f = Fraction.getFraction(14, 10);
1301        assertEquals("1 4/10", f.toProperString());
1302        
1303        f = Fraction.getFraction(4, 2);
1304        assertEquals("2", f.toProperString());
1305        
1306        f = Fraction.getFraction(0, 2);
1307        assertEquals("0", f.toProperString());
1308        
1309        f = Fraction.getFraction(2, 2);
1310        assertEquals("1", f.toProperString());
1311        
1312        f = Fraction.getFraction(-7, 5);
1313        assertEquals("-1 2/5", f.toProperString());
1314
1315        f = Fraction.getFraction(Integer.MIN_VALUE, 0, 1);
1316        assertEquals("-2147483648", f.toProperString());
1317
1318        f = Fraction.getFraction(-1, 1, Integer.MAX_VALUE);
1319        assertEquals("-1 1/2147483647", f.toProperString());
1320    }
1321}
1322
Popular Tags