0001: /*
0002: * Licensed to the Apache Software Foundation (ASF) under one
0003: * or more contributor license agreements. See the NOTICE file
0004: * distributed with this work for additional information
0005: * regarding copyright ownership. The ASF licenses this file
0006: * to you under the Apache License, Version 2.0 (the
0007: * "License"); you may not use this file except in compliance
0008: * with the License. You may obtain a copy of the License at
0009: *
0010: * http://www.apache.org/licenses/LICENSE-2.0
0011: *
0012: * Unless required by applicable law or agreed to in writing,
0013: * software distributed under the License is distributed on an
0014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
0015: * KIND, either express or implied. See the License for the
0016: * specific language governing permissions and limitations
0017: * under the License.
0018: */
0019: package org.apache.commons.lang.math;
0020:
0021: import junit.framework.Test;
0022: import junit.framework.TestCase;
0023: import junit.framework.TestSuite;
0024:
0025: /**
0026: * Test cases for the {@link Fraction} class
0027: *
0028: * @author Stephen Colebourne
0029: * @author C. Scott Ananian
0030: * @version $Id: FractionTest.java 437554 2006-08-28 06:21:41Z bayard $
0031: */
0032: public class FractionTest extends TestCase {
0033:
0034: private static final int SKIP = 500; //53
0035:
0036: public FractionTest(String name) {
0037: super (name);
0038: }
0039:
0040: public static Test suite() {
0041: TestSuite suite = new TestSuite(FractionTest.class);
0042: suite.setName("Fraction Tests");
0043: return suite;
0044: }
0045:
0046: public void setUp() {
0047: }
0048:
0049: //--------------------------------------------------------------------------
0050:
0051: public void testConstants() {
0052: assertEquals(0, Fraction.ZERO.getNumerator());
0053: assertEquals(1, Fraction.ZERO.getDenominator());
0054:
0055: assertEquals(1, Fraction.ONE.getNumerator());
0056: assertEquals(1, Fraction.ONE.getDenominator());
0057:
0058: assertEquals(1, Fraction.ONE_HALF.getNumerator());
0059: assertEquals(2, Fraction.ONE_HALF.getDenominator());
0060:
0061: assertEquals(1, Fraction.ONE_THIRD.getNumerator());
0062: assertEquals(3, Fraction.ONE_THIRD.getDenominator());
0063:
0064: assertEquals(2, Fraction.TWO_THIRDS.getNumerator());
0065: assertEquals(3, Fraction.TWO_THIRDS.getDenominator());
0066:
0067: assertEquals(1, Fraction.ONE_QUARTER.getNumerator());
0068: assertEquals(4, Fraction.ONE_QUARTER.getDenominator());
0069:
0070: assertEquals(2, Fraction.TWO_QUARTERS.getNumerator());
0071: assertEquals(4, Fraction.TWO_QUARTERS.getDenominator());
0072:
0073: assertEquals(3, Fraction.THREE_QUARTERS.getNumerator());
0074: assertEquals(4, Fraction.THREE_QUARTERS.getDenominator());
0075:
0076: assertEquals(1, Fraction.ONE_FIFTH.getNumerator());
0077: assertEquals(5, Fraction.ONE_FIFTH.getDenominator());
0078:
0079: assertEquals(2, Fraction.TWO_FIFTHS.getNumerator());
0080: assertEquals(5, Fraction.TWO_FIFTHS.getDenominator());
0081:
0082: assertEquals(3, Fraction.THREE_FIFTHS.getNumerator());
0083: assertEquals(5, Fraction.THREE_FIFTHS.getDenominator());
0084:
0085: assertEquals(4, Fraction.FOUR_FIFTHS.getNumerator());
0086: assertEquals(5, Fraction.FOUR_FIFTHS.getDenominator());
0087: }
0088:
0089: public void testFactory_int_int() {
0090: Fraction f = null;
0091:
0092: // zero
0093: f = Fraction.getFraction(0, 1);
0094: assertEquals(0, f.getNumerator());
0095: assertEquals(1, f.getDenominator());
0096:
0097: f = Fraction.getFraction(0, 2);
0098: assertEquals(0, f.getNumerator());
0099: assertEquals(2, f.getDenominator());
0100:
0101: // normal
0102: f = Fraction.getFraction(1, 1);
0103: assertEquals(1, f.getNumerator());
0104: assertEquals(1, f.getDenominator());
0105:
0106: f = Fraction.getFraction(2, 1);
0107: assertEquals(2, f.getNumerator());
0108: assertEquals(1, f.getDenominator());
0109:
0110: f = Fraction.getFraction(23, 345);
0111: assertEquals(23, f.getNumerator());
0112: assertEquals(345, f.getDenominator());
0113:
0114: // improper
0115: f = Fraction.getFraction(22, 7);
0116: assertEquals(22, f.getNumerator());
0117: assertEquals(7, f.getDenominator());
0118:
0119: // negatives
0120: f = Fraction.getFraction(-6, 10);
0121: assertEquals(-6, f.getNumerator());
0122: assertEquals(10, f.getDenominator());
0123:
0124: f = Fraction.getFraction(6, -10);
0125: assertEquals(-6, f.getNumerator());
0126: assertEquals(10, f.getDenominator());
0127:
0128: f = Fraction.getFraction(-6, -10);
0129: assertEquals(6, f.getNumerator());
0130: assertEquals(10, f.getDenominator());
0131:
0132: // zero denominator
0133: try {
0134: f = Fraction.getFraction(1, 0);
0135: fail("expecting ArithmeticException");
0136: } catch (ArithmeticException ex) {
0137: }
0138:
0139: try {
0140: f = Fraction.getFraction(2, 0);
0141: fail("expecting ArithmeticException");
0142: } catch (ArithmeticException ex) {
0143: }
0144:
0145: try {
0146: f = Fraction.getFraction(-3, 0);
0147: fail("expecting ArithmeticException");
0148: } catch (ArithmeticException ex) {
0149: }
0150:
0151: // very large: can't represent as unsimplified fraction, although
0152: try {
0153: f = Fraction.getFraction(4, Integer.MIN_VALUE);
0154: fail("expecting ArithmeticException");
0155: } catch (ArithmeticException ex) {
0156: }
0157: try {
0158: f = Fraction.getFraction(1, Integer.MIN_VALUE);
0159: fail("expecting ArithmeticException");
0160: } catch (ArithmeticException ex) {
0161: }
0162: }
0163:
0164: public void testFactory_int_int_int() {
0165: Fraction f = null;
0166:
0167: // zero
0168: f = Fraction.getFraction(0, 0, 2);
0169: assertEquals(0, f.getNumerator());
0170: assertEquals(2, f.getDenominator());
0171:
0172: f = Fraction.getFraction(2, 0, 2);
0173: assertEquals(4, f.getNumerator());
0174: assertEquals(2, f.getDenominator());
0175:
0176: f = Fraction.getFraction(0, 1, 2);
0177: assertEquals(1, f.getNumerator());
0178: assertEquals(2, f.getDenominator());
0179:
0180: // normal
0181: f = Fraction.getFraction(1, 1, 2);
0182: assertEquals(3, f.getNumerator());
0183: assertEquals(2, f.getDenominator());
0184:
0185: // negatives
0186: try {
0187: f = Fraction.getFraction(1, -6, -10);
0188: fail("expecting ArithmeticException");
0189: } catch (ArithmeticException ex) {
0190: }
0191:
0192: try {
0193: f = Fraction.getFraction(1, -6, -10);
0194: fail("expecting ArithmeticException");
0195: } catch (ArithmeticException ex) {
0196: }
0197:
0198: try {
0199: f = Fraction.getFraction(1, -6, -10);
0200: fail("expecting ArithmeticException");
0201: } catch (ArithmeticException ex) {
0202: }
0203:
0204: // negative whole
0205: f = Fraction.getFraction(-1, 6, 10);
0206: assertEquals(-16, f.getNumerator());
0207: assertEquals(10, f.getDenominator());
0208:
0209: try {
0210: f = Fraction.getFraction(-1, -6, 10);
0211: fail("expecting ArithmeticException");
0212: } catch (ArithmeticException ex) {
0213: }
0214:
0215: try {
0216: f = Fraction.getFraction(-1, 6, -10);
0217: fail("expecting ArithmeticException");
0218: } catch (ArithmeticException ex) {
0219: }
0220:
0221: try {
0222: f = Fraction.getFraction(-1, -6, -10);
0223: fail("expecting ArithmeticException");
0224: } catch (ArithmeticException ex) {
0225: }
0226:
0227: // zero denominator
0228: try {
0229: f = Fraction.getFraction(0, 1, 0);
0230: fail("expecting ArithmeticException");
0231: } catch (ArithmeticException ex) {
0232: }
0233:
0234: try {
0235: f = Fraction.getFraction(1, 2, 0);
0236: fail("expecting ArithmeticException");
0237: } catch (ArithmeticException ex) {
0238: }
0239:
0240: try {
0241: f = Fraction.getFraction(-1, -3, 0);
0242: fail("expecting ArithmeticException");
0243: } catch (ArithmeticException ex) {
0244: }
0245:
0246: try {
0247: f = Fraction.getFraction(Integer.MAX_VALUE, 1, 2);
0248: fail("expecting ArithmeticException");
0249: } catch (ArithmeticException ex) {
0250: }
0251:
0252: try {
0253: f = Fraction.getFraction(-Integer.MAX_VALUE, 1, 2);
0254: fail("expecting ArithmeticException");
0255: } catch (ArithmeticException ex) {
0256: }
0257:
0258: // very large
0259: f = Fraction.getFraction(-1, 0, Integer.MAX_VALUE);
0260: assertEquals(-Integer.MAX_VALUE, f.getNumerator());
0261: assertEquals(Integer.MAX_VALUE, f.getDenominator());
0262:
0263: try {
0264: // negative denominators not allowed in this constructor.
0265: f = Fraction.getFraction(0, 4, Integer.MIN_VALUE);
0266: fail("expecting ArithmeticException");
0267: } catch (ArithmeticException ex) {
0268: }
0269: try {
0270: f = Fraction.getFraction(1, 1, Integer.MAX_VALUE);
0271: fail("expecting ArithmeticException");
0272: } catch (ArithmeticException ex) {
0273: }
0274: try {
0275: f = Fraction.getFraction(-1, 2, Integer.MAX_VALUE);
0276: fail("expecting ArithmeticException");
0277: } catch (ArithmeticException ex) {
0278: }
0279: }
0280:
0281: public void testReducedFactory_int_int() {
0282: Fraction f = null;
0283:
0284: // zero
0285: f = Fraction.getReducedFraction(0, 1);
0286: assertEquals(0, f.getNumerator());
0287: assertEquals(1, f.getDenominator());
0288:
0289: // normal
0290: f = Fraction.getReducedFraction(1, 1);
0291: assertEquals(1, f.getNumerator());
0292: assertEquals(1, f.getDenominator());
0293:
0294: f = Fraction.getReducedFraction(2, 1);
0295: assertEquals(2, f.getNumerator());
0296: assertEquals(1, f.getDenominator());
0297:
0298: // improper
0299: f = Fraction.getReducedFraction(22, 7);
0300: assertEquals(22, f.getNumerator());
0301: assertEquals(7, f.getDenominator());
0302:
0303: // negatives
0304: f = Fraction.getReducedFraction(-6, 10);
0305: assertEquals(-3, f.getNumerator());
0306: assertEquals(5, f.getDenominator());
0307:
0308: f = Fraction.getReducedFraction(6, -10);
0309: assertEquals(-3, f.getNumerator());
0310: assertEquals(5, f.getDenominator());
0311:
0312: f = Fraction.getReducedFraction(-6, -10);
0313: assertEquals(3, f.getNumerator());
0314: assertEquals(5, f.getDenominator());
0315:
0316: // zero denominator
0317: try {
0318: f = Fraction.getReducedFraction(1, 0);
0319: fail("expecting ArithmeticException");
0320: } catch (ArithmeticException ex) {
0321: }
0322:
0323: try {
0324: f = Fraction.getReducedFraction(2, 0);
0325: fail("expecting ArithmeticException");
0326: } catch (ArithmeticException ex) {
0327: }
0328:
0329: try {
0330: f = Fraction.getReducedFraction(-3, 0);
0331: fail("expecting ArithmeticException");
0332: } catch (ArithmeticException ex) {
0333: }
0334:
0335: // reduced
0336: f = Fraction.getReducedFraction(0, 2);
0337: assertEquals(0, f.getNumerator());
0338: assertEquals(1, f.getDenominator());
0339:
0340: f = Fraction.getReducedFraction(2, 2);
0341: assertEquals(1, f.getNumerator());
0342: assertEquals(1, f.getDenominator());
0343:
0344: f = Fraction.getReducedFraction(2, 4);
0345: assertEquals(1, f.getNumerator());
0346: assertEquals(2, f.getDenominator());
0347:
0348: f = Fraction.getReducedFraction(15, 10);
0349: assertEquals(3, f.getNumerator());
0350: assertEquals(2, f.getDenominator());
0351:
0352: f = Fraction.getReducedFraction(121, 22);
0353: assertEquals(11, f.getNumerator());
0354: assertEquals(2, f.getDenominator());
0355:
0356: // Extreme values
0357: // OK, can reduce before negating
0358: f = Fraction.getReducedFraction(-2, Integer.MIN_VALUE);
0359: assertEquals(1, f.getNumerator());
0360: assertEquals(-(Integer.MIN_VALUE / 2), f.getDenominator());
0361:
0362: // Can't reduce, negation will throw
0363: try {
0364: f = Fraction.getReducedFraction(-7, Integer.MIN_VALUE);
0365: fail("Expecting ArithmeticException");
0366: } catch (ArithmeticException ex) {
0367: }
0368: }
0369:
0370: public void testFactory_double() {
0371: Fraction f = null;
0372:
0373: try {
0374: f = Fraction.getFraction(Double.NaN);
0375: fail("expecting ArithmeticException");
0376: } catch (ArithmeticException ex) {
0377: }
0378:
0379: try {
0380: f = Fraction.getFraction(Double.POSITIVE_INFINITY);
0381: fail("expecting ArithmeticException");
0382: } catch (ArithmeticException ex) {
0383: }
0384:
0385: try {
0386: f = Fraction.getFraction(Double.NEGATIVE_INFINITY);
0387: fail("expecting ArithmeticException");
0388: } catch (ArithmeticException ex) {
0389: }
0390:
0391: try {
0392: f = Fraction.getFraction((double) Integer.MAX_VALUE + 1);
0393: fail("expecting ArithmeticException");
0394: } catch (ArithmeticException ex) {
0395: }
0396:
0397: // zero
0398: f = Fraction.getFraction(0.0d);
0399: assertEquals(0, f.getNumerator());
0400: assertEquals(1, f.getDenominator());
0401:
0402: // one
0403: f = Fraction.getFraction(1.0d);
0404: assertEquals(1, f.getNumerator());
0405: assertEquals(1, f.getDenominator());
0406:
0407: // one half
0408: f = Fraction.getFraction(0.5d);
0409: assertEquals(1, f.getNumerator());
0410: assertEquals(2, f.getDenominator());
0411:
0412: // negative
0413: f = Fraction.getFraction(-0.875d);
0414: assertEquals(-7, f.getNumerator());
0415: assertEquals(8, f.getDenominator());
0416:
0417: // over 1
0418: f = Fraction.getFraction(1.25d);
0419: assertEquals(5, f.getNumerator());
0420: assertEquals(4, f.getDenominator());
0421:
0422: // two thirds
0423: f = Fraction.getFraction(0.66666d);
0424: assertEquals(2, f.getNumerator());
0425: assertEquals(3, f.getDenominator());
0426:
0427: // small
0428: f = Fraction.getFraction(1.0d / 10001d);
0429: assertEquals(0, f.getNumerator());
0430: assertEquals(1, f.getDenominator());
0431:
0432: // normal
0433: Fraction f2 = null;
0434: int remainder, number1, number2 = 0;
0435: for (int i = 1; i <= 100; i++) { // denominator
0436: for (int j = 1; j <= i; j++) { // numerator
0437: try {
0438: f = Fraction.getFraction((double) j / (double) i);
0439: } catch (ArithmeticException ex) {
0440: System.err.println(j + " " + i);
0441: throw ex;
0442: }
0443: f2 = Fraction.getReducedFraction(j, i);
0444: assertEquals(f2.getNumerator(), f.getNumerator());
0445: assertEquals(f2.getDenominator(), f.getDenominator());
0446: }
0447: }
0448: // save time by skipping some tests! (
0449: for (int i = 1001; i <= 10000; i += SKIP) { // denominator
0450: for (int j = 1; j <= i; j++) { // numerator
0451: try {
0452: f = Fraction.getFraction((double) j / (double) i);
0453: } catch (ArithmeticException ex) {
0454: System.err.println(j + " " + i);
0455: throw ex;
0456: }
0457: f2 = Fraction.getReducedFraction(j, i);
0458: assertEquals(f2.getNumerator(), f.getNumerator());
0459: assertEquals(f2.getDenominator(), f.getDenominator());
0460: }
0461: }
0462: }
0463:
0464: public void testFactory_String() {
0465: try {
0466: Fraction.getFraction(null);
0467: fail("expecting IllegalArgumentException");
0468: } catch (IllegalArgumentException ex) {
0469: }
0470: }
0471:
0472: public void testFactory_String_double() {
0473: Fraction f = null;
0474:
0475: f = Fraction.getFraction("0.0");
0476: assertEquals(0, f.getNumerator());
0477: assertEquals(1, f.getDenominator());
0478:
0479: f = Fraction.getFraction("0.2");
0480: assertEquals(1, f.getNumerator());
0481: assertEquals(5, f.getDenominator());
0482:
0483: f = Fraction.getFraction("0.5");
0484: assertEquals(1, f.getNumerator());
0485: assertEquals(2, f.getDenominator());
0486:
0487: f = Fraction.getFraction("0.66666");
0488: assertEquals(2, f.getNumerator());
0489: assertEquals(3, f.getDenominator());
0490:
0491: try {
0492: f = Fraction.getFraction("2.3R");
0493: fail("Expecting NumberFormatException");
0494: } catch (NumberFormatException ex) {
0495: }
0496:
0497: try {
0498: f = Fraction.getFraction("2147483648"); // too big
0499: fail("Expecting NumberFormatException");
0500: } catch (NumberFormatException ex) {
0501: }
0502:
0503: try {
0504: f = Fraction.getFraction(".");
0505: fail("Expecting NumberFormatException");
0506: } catch (NumberFormatException ex) {
0507: }
0508: }
0509:
0510: public void testFactory_String_proper() {
0511: Fraction f = null;
0512:
0513: f = Fraction.getFraction("0 0/1");
0514: assertEquals(0, f.getNumerator());
0515: assertEquals(1, f.getDenominator());
0516:
0517: f = Fraction.getFraction("1 1/5");
0518: assertEquals(6, f.getNumerator());
0519: assertEquals(5, f.getDenominator());
0520:
0521: f = Fraction.getFraction("7 1/2");
0522: assertEquals(15, f.getNumerator());
0523: assertEquals(2, f.getDenominator());
0524:
0525: f = Fraction.getFraction("1 2/4");
0526: assertEquals(6, f.getNumerator());
0527: assertEquals(4, f.getDenominator());
0528:
0529: f = Fraction.getFraction("-7 1/2");
0530: assertEquals(-15, f.getNumerator());
0531: assertEquals(2, f.getDenominator());
0532:
0533: f = Fraction.getFraction("-1 2/4");
0534: assertEquals(-6, f.getNumerator());
0535: assertEquals(4, f.getDenominator());
0536:
0537: try {
0538: f = Fraction.getFraction("2 3");
0539: fail("expecting NumberFormatException");
0540: } catch (NumberFormatException ex) {
0541: }
0542:
0543: try {
0544: f = Fraction.getFraction("a 3");
0545: fail("expecting NumberFormatException");
0546: } catch (NumberFormatException ex) {
0547: }
0548:
0549: try {
0550: f = Fraction.getFraction("2 b/4");
0551: fail("expecting NumberFormatException");
0552: } catch (NumberFormatException ex) {
0553: }
0554:
0555: try {
0556: f = Fraction.getFraction("2 ");
0557: fail("expecting NumberFormatException");
0558: } catch (NumberFormatException ex) {
0559: }
0560:
0561: try {
0562: f = Fraction.getFraction(" 3");
0563: fail("expecting NumberFormatException");
0564: } catch (NumberFormatException ex) {
0565: }
0566:
0567: try {
0568: f = Fraction.getFraction(" ");
0569: fail("expecting NumberFormatException");
0570: } catch (NumberFormatException ex) {
0571: }
0572: }
0573:
0574: public void testFactory_String_improper() {
0575: Fraction f = null;
0576:
0577: f = Fraction.getFraction("0/1");
0578: assertEquals(0, f.getNumerator());
0579: assertEquals(1, f.getDenominator());
0580:
0581: f = Fraction.getFraction("1/5");
0582: assertEquals(1, f.getNumerator());
0583: assertEquals(5, f.getDenominator());
0584:
0585: f = Fraction.getFraction("1/2");
0586: assertEquals(1, f.getNumerator());
0587: assertEquals(2, f.getDenominator());
0588:
0589: f = Fraction.getFraction("2/3");
0590: assertEquals(2, f.getNumerator());
0591: assertEquals(3, f.getDenominator());
0592:
0593: f = Fraction.getFraction("7/3");
0594: assertEquals(7, f.getNumerator());
0595: assertEquals(3, f.getDenominator());
0596:
0597: f = Fraction.getFraction("2/4");
0598: assertEquals(2, f.getNumerator());
0599: assertEquals(4, f.getDenominator());
0600:
0601: try {
0602: f = Fraction.getFraction("2/d");
0603: fail("expecting NumberFormatException");
0604: } catch (NumberFormatException ex) {
0605: }
0606:
0607: try {
0608: f = Fraction.getFraction("2e/3");
0609: fail("expecting NumberFormatException");
0610: } catch (NumberFormatException ex) {
0611: }
0612:
0613: try {
0614: f = Fraction.getFraction("2/");
0615: fail("expecting NumberFormatException");
0616: } catch (NumberFormatException ex) {
0617: }
0618:
0619: try {
0620: f = Fraction.getFraction("/");
0621: fail("expecting NumberFormatException");
0622: } catch (NumberFormatException ex) {
0623: }
0624: }
0625:
0626: public void testGets() {
0627: Fraction f = null;
0628:
0629: f = Fraction.getFraction(3, 5, 6);
0630: assertEquals(23, f.getNumerator());
0631: assertEquals(3, f.getProperWhole());
0632: assertEquals(5, f.getProperNumerator());
0633: assertEquals(6, f.getDenominator());
0634:
0635: f = Fraction.getFraction(-3, 5, 6);
0636: assertEquals(-23, f.getNumerator());
0637: assertEquals(-3, f.getProperWhole());
0638: assertEquals(5, f.getProperNumerator());
0639: assertEquals(6, f.getDenominator());
0640:
0641: f = Fraction.getFraction(Integer.MIN_VALUE, 0, 1);
0642: assertEquals(Integer.MIN_VALUE, f.getNumerator());
0643: assertEquals(Integer.MIN_VALUE, f.getProperWhole());
0644: assertEquals(0, f.getProperNumerator());
0645: assertEquals(1, f.getDenominator());
0646: }
0647:
0648: public void testConversions() {
0649: Fraction f = null;
0650:
0651: f = Fraction.getFraction(3, 7, 8);
0652: assertEquals(3, f.intValue());
0653: assertEquals(3L, f.longValue());
0654: assertEquals(3.875f, f.floatValue(), 0.00001f);
0655: assertEquals(3.875d, f.doubleValue(), 0.00001d);
0656: }
0657:
0658: public void testReduce() {
0659: Fraction f = null;
0660:
0661: f = Fraction.getFraction(50, 75);
0662: Fraction result = f.reduce();
0663: assertEquals(2, result.getNumerator());
0664: assertEquals(3, result.getDenominator());
0665:
0666: f = Fraction.getFraction(-2, -3);
0667: result = f.reduce();
0668: assertEquals(2, result.getNumerator());
0669: assertEquals(3, result.getDenominator());
0670:
0671: f = Fraction.getFraction(2, -3);
0672: result = f.reduce();
0673: assertEquals(-2, result.getNumerator());
0674: assertEquals(3, result.getDenominator());
0675:
0676: f = Fraction.getFraction(-2, 3);
0677: result = f.reduce();
0678: assertEquals(-2, result.getNumerator());
0679: assertEquals(3, result.getDenominator());
0680: assertSame(f, result);
0681:
0682: f = Fraction.getFraction(2, 3);
0683: result = f.reduce();
0684: assertEquals(2, result.getNumerator());
0685: assertEquals(3, result.getDenominator());
0686: assertSame(f, result);
0687: }
0688:
0689: public void testInvert() {
0690: Fraction f = null;
0691:
0692: f = Fraction.getFraction(50, 75);
0693: f = f.invert();
0694: assertEquals(75, f.getNumerator());
0695: assertEquals(50, f.getDenominator());
0696:
0697: f = Fraction.getFraction(4, 3);
0698: f = f.invert();
0699: assertEquals(3, f.getNumerator());
0700: assertEquals(4, f.getDenominator());
0701:
0702: f = Fraction.getFraction(-15, 47);
0703: f = f.invert();
0704: assertEquals(-47, f.getNumerator());
0705: assertEquals(15, f.getDenominator());
0706:
0707: f = Fraction.getFraction(0, 3);
0708: try {
0709: f = f.invert();
0710: fail("expecting ArithmeticException");
0711: } catch (ArithmeticException ex) {
0712: }
0713:
0714: // large values
0715: f = Fraction.getFraction(Integer.MIN_VALUE, 1);
0716: try {
0717: f = f.invert();
0718: fail("expecting ArithmeticException");
0719: } catch (ArithmeticException ex) {
0720: }
0721:
0722: f = Fraction.getFraction(Integer.MAX_VALUE, 1);
0723: f = f.invert();
0724: assertEquals(1, f.getNumerator());
0725: assertEquals(Integer.MAX_VALUE, f.getDenominator());
0726: }
0727:
0728: public void testNegate() {
0729: Fraction f = null;
0730:
0731: f = Fraction.getFraction(50, 75);
0732: f = f.negate();
0733: assertEquals(-50, f.getNumerator());
0734: assertEquals(75, f.getDenominator());
0735:
0736: f = Fraction.getFraction(-50, 75);
0737: f = f.negate();
0738: assertEquals(50, f.getNumerator());
0739: assertEquals(75, f.getDenominator());
0740:
0741: // large values
0742: f = Fraction.getFraction(Integer.MAX_VALUE - 1,
0743: Integer.MAX_VALUE);
0744: f = f.negate();
0745: assertEquals(Integer.MIN_VALUE + 2, f.getNumerator());
0746: assertEquals(Integer.MAX_VALUE, f.getDenominator());
0747:
0748: f = Fraction.getFraction(Integer.MIN_VALUE, 1);
0749: try {
0750: f = f.negate();
0751: fail("expecting ArithmeticException");
0752: } catch (ArithmeticException ex) {
0753: }
0754: }
0755:
0756: public void testAbs() {
0757: Fraction f = null;
0758:
0759: f = Fraction.getFraction(50, 75);
0760: f = f.abs();
0761: assertEquals(50, f.getNumerator());
0762: assertEquals(75, f.getDenominator());
0763:
0764: f = Fraction.getFraction(-50, 75);
0765: f = f.abs();
0766: assertEquals(50, f.getNumerator());
0767: assertEquals(75, f.getDenominator());
0768:
0769: f = Fraction.getFraction(Integer.MAX_VALUE, 1);
0770: f = f.abs();
0771: assertEquals(Integer.MAX_VALUE, f.getNumerator());
0772: assertEquals(1, f.getDenominator());
0773:
0774: f = Fraction.getFraction(Integer.MAX_VALUE, -1);
0775: f = f.abs();
0776: assertEquals(Integer.MAX_VALUE, f.getNumerator());
0777: assertEquals(1, f.getDenominator());
0778:
0779: f = Fraction.getFraction(Integer.MIN_VALUE, 1);
0780: try {
0781: f = f.abs();
0782: fail("expecting ArithmeticException");
0783: } catch (ArithmeticException ex) {
0784: }
0785: }
0786:
0787: public void testPow() {
0788: Fraction f = null;
0789:
0790: f = Fraction.getFraction(3, 5);
0791: assertEquals(Fraction.ONE, f.pow(0));
0792:
0793: f = Fraction.getFraction(3, 5);
0794: assertSame(f, f.pow(1));
0795: assertEquals(f, f.pow(1));
0796:
0797: f = Fraction.getFraction(3, 5);
0798: f = f.pow(2);
0799: assertEquals(9, f.getNumerator());
0800: assertEquals(25, f.getDenominator());
0801:
0802: f = Fraction.getFraction(3, 5);
0803: f = f.pow(3);
0804: assertEquals(27, f.getNumerator());
0805: assertEquals(125, f.getDenominator());
0806:
0807: f = Fraction.getFraction(3, 5);
0808: f = f.pow(-1);
0809: assertEquals(5, f.getNumerator());
0810: assertEquals(3, f.getDenominator());
0811:
0812: f = Fraction.getFraction(3, 5);
0813: f = f.pow(-2);
0814: assertEquals(25, f.getNumerator());
0815: assertEquals(9, f.getDenominator());
0816:
0817: // check unreduced fractions stay that way.
0818: f = Fraction.getFraction(6, 10);
0819: assertEquals(Fraction.ONE, f.pow(0));
0820:
0821: f = Fraction.getFraction(6, 10);
0822: assertEquals(f, f.pow(1));
0823: assertFalse(f.pow(1).equals(Fraction.getFraction(3, 5)));
0824:
0825: f = Fraction.getFraction(6, 10);
0826: f = f.pow(2);
0827: assertEquals(9, f.getNumerator());
0828: assertEquals(25, f.getDenominator());
0829:
0830: f = Fraction.getFraction(6, 10);
0831: f = f.pow(3);
0832: assertEquals(27, f.getNumerator());
0833: assertEquals(125, f.getDenominator());
0834:
0835: f = Fraction.getFraction(6, 10);
0836: f = f.pow(-1);
0837: assertEquals(10, f.getNumerator());
0838: assertEquals(6, f.getDenominator());
0839:
0840: f = Fraction.getFraction(6, 10);
0841: f = f.pow(-2);
0842: assertEquals(25, f.getNumerator());
0843: assertEquals(9, f.getDenominator());
0844:
0845: // zero to any positive power is still zero.
0846: f = Fraction.getFraction(0, 1231);
0847: f = f.pow(1);
0848: assertTrue(0 == f.compareTo(Fraction.ZERO));
0849: assertEquals(0, f.getNumerator());
0850: assertEquals(1231, f.getDenominator());
0851: f = f.pow(2);
0852: assertTrue(0 == f.compareTo(Fraction.ZERO));
0853: assertEquals(0, f.getNumerator());
0854: assertEquals(1, f.getDenominator());
0855:
0856: // zero to negative powers should throw an exception
0857: try {
0858: f = f.pow(-1);
0859: fail("expecting ArithmeticException");
0860: } catch (ArithmeticException ex) {
0861: }
0862: try {
0863: f = f.pow(Integer.MIN_VALUE);
0864: fail("expecting ArithmeticException");
0865: } catch (ArithmeticException ex) {
0866: }
0867:
0868: // one to any power is still one.
0869: f = Fraction.getFraction(1, 1);
0870: f = f.pow(0);
0871: assertEquals(f, Fraction.ONE);
0872: f = f.pow(1);
0873: assertEquals(f, Fraction.ONE);
0874: f = f.pow(-1);
0875: assertEquals(f, Fraction.ONE);
0876: f = f.pow(Integer.MAX_VALUE);
0877: assertEquals(f, Fraction.ONE);
0878: f = f.pow(Integer.MIN_VALUE);
0879: assertEquals(f, Fraction.ONE);
0880:
0881: f = Fraction.getFraction(Integer.MAX_VALUE, 1);
0882: try {
0883: f = f.pow(2);
0884: fail("expecting ArithmeticException");
0885: } catch (ArithmeticException ex) {
0886: }
0887:
0888: // Numerator growing too negative during the pow operation.
0889: f = Fraction.getFraction(Integer.MIN_VALUE, 1);
0890: try {
0891: f = f.pow(3);
0892: fail("expecting ArithmeticException");
0893: } catch (ArithmeticException ex) {
0894: }
0895:
0896: f = Fraction.getFraction(65536, 1);
0897: try {
0898: f = f.pow(2);
0899: fail("expecting ArithmeticException");
0900: } catch (ArithmeticException ex) {
0901: }
0902: }
0903:
0904: public void testAdd() {
0905: Fraction f = null;
0906: Fraction f1 = null;
0907: Fraction f2 = null;
0908:
0909: f1 = Fraction.getFraction(3, 5);
0910: f2 = Fraction.getFraction(1, 5);
0911: f = f1.add(f2);
0912: assertEquals(4, f.getNumerator());
0913: assertEquals(5, f.getDenominator());
0914:
0915: f1 = Fraction.getFraction(3, 5);
0916: f2 = Fraction.getFraction(2, 5);
0917: f = f1.add(f2);
0918: assertEquals(1, f.getNumerator());
0919: assertEquals(1, f.getDenominator());
0920:
0921: f1 = Fraction.getFraction(3, 5);
0922: f2 = Fraction.getFraction(3, 5);
0923: f = f1.add(f2);
0924: assertEquals(6, f.getNumerator());
0925: assertEquals(5, f.getDenominator());
0926:
0927: f1 = Fraction.getFraction(3, 5);
0928: f2 = Fraction.getFraction(-4, 5);
0929: f = f1.add(f2);
0930: assertEquals(-1, f.getNumerator());
0931: assertEquals(5, f.getDenominator());
0932:
0933: f1 = Fraction.getFraction(Integer.MAX_VALUE - 1, 1);
0934: f2 = Fraction.ONE;
0935: f = f1.add(f2);
0936: assertEquals(Integer.MAX_VALUE, f.getNumerator());
0937: assertEquals(1, f.getDenominator());
0938:
0939: f1 = Fraction.getFraction(3, 5);
0940: f2 = Fraction.getFraction(1, 2);
0941: f = f1.add(f2);
0942: assertEquals(11, f.getNumerator());
0943: assertEquals(10, f.getDenominator());
0944:
0945: f1 = Fraction.getFraction(3, 8);
0946: f2 = Fraction.getFraction(1, 6);
0947: f = f1.add(f2);
0948: assertEquals(13, f.getNumerator());
0949: assertEquals(24, f.getDenominator());
0950:
0951: f1 = Fraction.getFraction(0, 5);
0952: f2 = Fraction.getFraction(1, 5);
0953: f = f1.add(f2);
0954: assertSame(f2, f);
0955: f = f2.add(f1);
0956: assertSame(f2, f);
0957:
0958: f1 = Fraction.getFraction(-1, 13 * 13 * 2 * 2);
0959: f2 = Fraction.getFraction(-2, 13 * 17 * 2);
0960: f = f1.add(f2);
0961: assertEquals(13 * 13 * 17 * 2 * 2, f.getDenominator());
0962: assertEquals(-17 - 2 * 13 * 2, f.getNumerator());
0963:
0964: try {
0965: f.add(null);
0966: fail("expecting IllegalArgumentException");
0967: } catch (IllegalArgumentException ex) {
0968: }
0969:
0970: // if this fraction is added naively, it will overflow.
0971: // check that it doesn't.
0972: f1 = Fraction.getFraction(1, 32768 * 3);
0973: f2 = Fraction.getFraction(1, 59049);
0974: f = f1.add(f2);
0975: assertEquals(52451, f.getNumerator());
0976: assertEquals(1934917632, f.getDenominator());
0977:
0978: f1 = Fraction.getFraction(Integer.MIN_VALUE, 3);
0979: f2 = Fraction.ONE_THIRD;
0980: f = f1.add(f2);
0981: assertEquals(Integer.MIN_VALUE + 1, f.getNumerator());
0982: assertEquals(3, f.getDenominator());
0983:
0984: f1 = Fraction.getFraction(Integer.MAX_VALUE - 1, 1);
0985: f2 = Fraction.ONE;
0986: f = f1.add(f2);
0987: assertEquals(Integer.MAX_VALUE, f.getNumerator());
0988: assertEquals(1, f.getDenominator());
0989:
0990: try {
0991: f = f.add(Fraction.ONE); // should overflow
0992: fail("expecting ArithmeticException but got: "
0993: + f.toString());
0994: } catch (ArithmeticException ex) {
0995: }
0996:
0997: // denominator should not be a multiple of 2 or 3 to trigger overflow
0998: f1 = Fraction.getFraction(Integer.MIN_VALUE, 5);
0999: f2 = Fraction.getFraction(-1, 5);
1000: try {
1001: f = f1.add(f2); // should overflow
1002: fail("expecting ArithmeticException but got: "
1003: + f.toString());
1004: } catch (ArithmeticException ex) {
1005: }
1006:
1007: try {
1008: f = Fraction.getFraction(-Integer.MAX_VALUE, 1);
1009: f = f.add(f);
1010: fail("expecting ArithmeticException");
1011: } catch (ArithmeticException ex) {
1012: }
1013:
1014: try {
1015: f = Fraction.getFraction(-Integer.MAX_VALUE, 1);
1016: f = f.add(f);
1017: fail("expecting ArithmeticException");
1018: } catch (ArithmeticException ex) {
1019: }
1020:
1021: f1 = Fraction.getFraction(3, 327680);
1022: f2 = Fraction.getFraction(2, 59049);
1023: try {
1024: f = f1.add(f2); // should overflow
1025: fail("expecting ArithmeticException but got: "
1026: + f.toString());
1027: } catch (ArithmeticException ex) {
1028: }
1029: }
1030:
1031: public void testSubtract() {
1032: Fraction f = null;
1033: Fraction f1 = null;
1034: Fraction f2 = null;
1035:
1036: f1 = Fraction.getFraction(3, 5);
1037: f2 = Fraction.getFraction(1, 5);
1038: f = f1.subtract(f2);
1039: assertEquals(2, f.getNumerator());
1040: assertEquals(5, f.getDenominator());
1041:
1042: f1 = Fraction.getFraction(7, 5);
1043: f2 = Fraction.getFraction(2, 5);
1044: f = f1.subtract(f2);
1045: assertEquals(1, f.getNumerator());
1046: assertEquals(1, f.getDenominator());
1047:
1048: f1 = Fraction.getFraction(3, 5);
1049: f2 = Fraction.getFraction(3, 5);
1050: f = f1.subtract(f2);
1051: assertEquals(0, f.getNumerator());
1052: assertEquals(1, f.getDenominator());
1053:
1054: f1 = Fraction.getFraction(3, 5);
1055: f2 = Fraction.getFraction(-4, 5);
1056: f = f1.subtract(f2);
1057: assertEquals(7, f.getNumerator());
1058: assertEquals(5, f.getDenominator());
1059:
1060: f1 = Fraction.getFraction(0, 5);
1061: f2 = Fraction.getFraction(4, 5);
1062: f = f1.subtract(f2);
1063: assertEquals(-4, f.getNumerator());
1064: assertEquals(5, f.getDenominator());
1065:
1066: f1 = Fraction.getFraction(0, 5);
1067: f2 = Fraction.getFraction(-4, 5);
1068: f = f1.subtract(f2);
1069: assertEquals(4, f.getNumerator());
1070: assertEquals(5, f.getDenominator());
1071:
1072: f1 = Fraction.getFraction(3, 5);
1073: f2 = Fraction.getFraction(1, 2);
1074: f = f1.subtract(f2);
1075: assertEquals(1, f.getNumerator());
1076: assertEquals(10, f.getDenominator());
1077:
1078: f1 = Fraction.getFraction(0, 5);
1079: f2 = Fraction.getFraction(1, 5);
1080: f = f2.subtract(f1);
1081: assertSame(f2, f);
1082:
1083: try {
1084: f.subtract(null);
1085: fail("expecting IllegalArgumentException");
1086: } catch (IllegalArgumentException ex) {
1087: }
1088:
1089: // if this fraction is subtracted naively, it will overflow.
1090: // check that it doesn't.
1091: f1 = Fraction.getFraction(1, 32768 * 3);
1092: f2 = Fraction.getFraction(1, 59049);
1093: f = f1.subtract(f2);
1094: assertEquals(-13085, f.getNumerator());
1095: assertEquals(1934917632, f.getDenominator());
1096:
1097: f1 = Fraction.getFraction(Integer.MIN_VALUE, 3);
1098: f2 = Fraction.ONE_THIRD.negate();
1099: f = f1.subtract(f2);
1100: assertEquals(Integer.MIN_VALUE + 1, f.getNumerator());
1101: assertEquals(3, f.getDenominator());
1102:
1103: f1 = Fraction.getFraction(Integer.MAX_VALUE, 1);
1104: f2 = Fraction.ONE;
1105: f = f1.subtract(f2);
1106: assertEquals(Integer.MAX_VALUE - 1, f.getNumerator());
1107: assertEquals(1, f.getDenominator());
1108:
1109: try {
1110: f1 = Fraction.getFraction(1, Integer.MAX_VALUE);
1111: f2 = Fraction.getFraction(1, Integer.MAX_VALUE - 1);
1112: f = f1.subtract(f2);
1113: fail("expecting ArithmeticException"); //should overflow
1114: } catch (ArithmeticException ex) {
1115: }
1116:
1117: // denominator should not be a multiple of 2 or 3 to trigger overflow
1118: f1 = Fraction.getFraction(Integer.MIN_VALUE, 5);
1119: f2 = Fraction.getFraction(1, 5);
1120: try {
1121: f = f1.subtract(f2); // should overflow
1122: fail("expecting ArithmeticException but got: "
1123: + f.toString());
1124: } catch (ArithmeticException ex) {
1125: }
1126:
1127: try {
1128: f = Fraction.getFraction(Integer.MIN_VALUE, 1);
1129: f = f.subtract(Fraction.ONE);
1130: fail("expecting ArithmeticException");
1131: } catch (ArithmeticException ex) {
1132: }
1133:
1134: try {
1135: f = Fraction.getFraction(Integer.MAX_VALUE, 1);
1136: f = f.subtract(Fraction.ONE.negate());
1137: fail("expecting ArithmeticException");
1138: } catch (ArithmeticException ex) {
1139: }
1140:
1141: f1 = Fraction.getFraction(3, 327680);
1142: f2 = Fraction.getFraction(2, 59049);
1143: try {
1144: f = f1.subtract(f2); // should overflow
1145: fail("expecting ArithmeticException but got: "
1146: + f.toString());
1147: } catch (ArithmeticException ex) {
1148: }
1149: }
1150:
1151: public void testMultiply() {
1152: Fraction f = null;
1153: Fraction f1 = null;
1154: Fraction f2 = null;
1155:
1156: f1 = Fraction.getFraction(3, 5);
1157: f2 = Fraction.getFraction(2, 5);
1158: f = f1.multiplyBy(f2);
1159: assertEquals(6, f.getNumerator());
1160: assertEquals(25, f.getDenominator());
1161:
1162: f1 = Fraction.getFraction(6, 10);
1163: f2 = Fraction.getFraction(6, 10);
1164: f = f1.multiplyBy(f2);
1165: assertEquals(9, f.getNumerator());
1166: assertEquals(25, f.getDenominator());
1167: f = f.multiplyBy(f2);
1168: assertEquals(27, f.getNumerator());
1169: assertEquals(125, f.getDenominator());
1170:
1171: f1 = Fraction.getFraction(3, 5);
1172: f2 = Fraction.getFraction(-2, 5);
1173: f = f1.multiplyBy(f2);
1174: assertEquals(-6, f.getNumerator());
1175: assertEquals(25, f.getDenominator());
1176:
1177: f1 = Fraction.getFraction(-3, 5);
1178: f2 = Fraction.getFraction(-2, 5);
1179: f = f1.multiplyBy(f2);
1180: assertEquals(6, f.getNumerator());
1181: assertEquals(25, f.getDenominator());
1182:
1183: f1 = Fraction.getFraction(0, 5);
1184: f2 = Fraction.getFraction(2, 7);
1185: f = f1.multiplyBy(f2);
1186: assertSame(Fraction.ZERO, f);
1187:
1188: f1 = Fraction.getFraction(2, 7);
1189: f2 = Fraction.ONE;
1190: f = f1.multiplyBy(f2);
1191: assertEquals(2, f.getNumerator());
1192: assertEquals(7, f.getDenominator());
1193:
1194: f1 = Fraction.getFraction(Integer.MAX_VALUE, 1);
1195: f2 = Fraction.getFraction(Integer.MIN_VALUE, Integer.MAX_VALUE);
1196: f = f1.multiplyBy(f2);
1197: assertEquals(Integer.MIN_VALUE, f.getNumerator());
1198: assertEquals(1, f.getDenominator());
1199:
1200: try {
1201: f.multiplyBy(null);
1202: fail("expecting IllegalArgumentException");
1203: } catch (IllegalArgumentException ex) {
1204: }
1205:
1206: try {
1207: f1 = Fraction.getFraction(1, Integer.MAX_VALUE);
1208: f = f1.multiplyBy(f1); // should overflow
1209: fail("expecting ArithmeticException");
1210: } catch (ArithmeticException ex) {
1211: }
1212:
1213: try {
1214: f1 = Fraction.getFraction(1, -Integer.MAX_VALUE);
1215: f = f1.multiplyBy(f1); // should overflow
1216: fail("expecting ArithmeticException");
1217: } catch (ArithmeticException ex) {
1218: }
1219: }
1220:
1221: public void testDivide() {
1222: Fraction f = null;
1223: Fraction f1 = null;
1224: Fraction f2 = null;
1225:
1226: f1 = Fraction.getFraction(3, 5);
1227: f2 = Fraction.getFraction(2, 5);
1228: f = f1.divideBy(f2);
1229: assertEquals(3, f.getNumerator());
1230: assertEquals(2, f.getDenominator());
1231:
1232: f1 = Fraction.getFraction(3, 5);
1233: f2 = Fraction.ZERO;
1234: try {
1235: f = f1.divideBy(f2);
1236: fail("expecting ArithmeticException");
1237: } catch (ArithmeticException ex) {
1238: }
1239:
1240: f1 = Fraction.getFraction(0, 5);
1241: f2 = Fraction.getFraction(2, 7);
1242: f = f1.divideBy(f2);
1243: assertSame(Fraction.ZERO, f);
1244:
1245: f1 = Fraction.getFraction(2, 7);
1246: f2 = Fraction.ONE;
1247: f = f1.divideBy(f2);
1248: assertEquals(2, f.getNumerator());
1249: assertEquals(7, f.getDenominator());
1250:
1251: f1 = Fraction.getFraction(1, Integer.MAX_VALUE);
1252: f = f1.divideBy(f1);
1253: assertEquals(1, f.getNumerator());
1254: assertEquals(1, f.getDenominator());
1255:
1256: f1 = Fraction.getFraction(Integer.MIN_VALUE, Integer.MAX_VALUE);
1257: f2 = Fraction.getFraction(1, Integer.MAX_VALUE);
1258: f = f1.divideBy(f2);
1259: assertEquals(Integer.MIN_VALUE, f.getNumerator());
1260: assertEquals(1, f.getDenominator());
1261:
1262: try {
1263: f.divideBy(null);
1264: fail("IllegalArgumentException");
1265: } catch (IllegalArgumentException ex) {
1266: }
1267:
1268: try {
1269: f1 = Fraction.getFraction(1, Integer.MAX_VALUE);
1270: f = f1.divideBy(f1.invert()); // should overflow
1271: fail("expecting ArithmeticException");
1272: } catch (ArithmeticException ex) {
1273: }
1274: try {
1275: f1 = Fraction.getFraction(1, -Integer.MAX_VALUE);
1276: f = f1.divideBy(f1.invert()); // should overflow
1277: fail("expecting ArithmeticException");
1278: } catch (ArithmeticException ex) {
1279: }
1280: }
1281:
1282: public void testEquals() {
1283: Fraction f1 = null;
1284: Fraction f2 = null;
1285:
1286: f1 = Fraction.getFraction(3, 5);
1287: assertEquals(false, f1.equals(null));
1288: assertEquals(false, f1.equals(new Object()));
1289: assertEquals(false, f1.equals(new Integer(6)));
1290:
1291: f1 = Fraction.getFraction(3, 5);
1292: f2 = Fraction.getFraction(2, 5);
1293: assertEquals(false, f1.equals(f2));
1294: assertEquals(true, f1.equals(f1));
1295: assertEquals(true, f2.equals(f2));
1296:
1297: f2 = Fraction.getFraction(3, 5);
1298: assertEquals(true, f1.equals(f2));
1299:
1300: f2 = Fraction.getFraction(6, 10);
1301: assertEquals(false, f1.equals(f2));
1302: }
1303:
1304: public void testHashCode() {
1305: Fraction f1 = Fraction.getFraction(3, 5);
1306: Fraction f2 = Fraction.getFraction(3, 5);
1307:
1308: assertTrue(f1.hashCode() == f2.hashCode());
1309:
1310: f2 = Fraction.getFraction(2, 5);
1311: assertTrue(f1.hashCode() != f2.hashCode());
1312:
1313: f2 = Fraction.getFraction(6, 10);
1314: assertTrue(f1.hashCode() != f2.hashCode());
1315: }
1316:
1317: public void testCompareTo() {
1318: Fraction f1 = null;
1319: Fraction f2 = null;
1320:
1321: f1 = Fraction.getFraction(3, 5);
1322: assertTrue(f1.compareTo(f1) == 0);
1323:
1324: try {
1325: f1.compareTo(null);
1326: fail("expecting NullPointerException");
1327: } catch (NullPointerException ex) {
1328: }
1329:
1330: try {
1331: f1.compareTo(new Object());
1332: fail("expecting ClassCastException");
1333: } catch (ClassCastException ex) {
1334: }
1335:
1336: f2 = Fraction.getFraction(2, 5);
1337: assertTrue(f1.compareTo(f2) > 0);
1338: assertTrue(f2.compareTo(f2) == 0);
1339:
1340: f2 = Fraction.getFraction(4, 5);
1341: assertTrue(f1.compareTo(f2) < 0);
1342: assertTrue(f2.compareTo(f2) == 0);
1343:
1344: f2 = Fraction.getFraction(3, 5);
1345: assertTrue(f1.compareTo(f2) == 0);
1346: assertTrue(f2.compareTo(f2) == 0);
1347:
1348: f2 = Fraction.getFraction(6, 10);
1349: assertTrue(f1.compareTo(f2) == 0);
1350: assertTrue(f2.compareTo(f2) == 0);
1351:
1352: f2 = Fraction.getFraction(-1, 1, Integer.MAX_VALUE);
1353: assertTrue(f1.compareTo(f2) > 0);
1354: assertTrue(f2.compareTo(f2) == 0);
1355:
1356: }
1357:
1358: public void testToString() {
1359: Fraction f = null;
1360:
1361: f = Fraction.getFraction(3, 5);
1362: String str = f.toString();
1363: assertEquals("3/5", str);
1364: assertSame(str, f.toString());
1365:
1366: f = Fraction.getFraction(7, 5);
1367: assertEquals("7/5", f.toString());
1368:
1369: f = Fraction.getFraction(4, 2);
1370: assertEquals("4/2", f.toString());
1371:
1372: f = Fraction.getFraction(0, 2);
1373: assertEquals("0/2", f.toString());
1374:
1375: f = Fraction.getFraction(2, 2);
1376: assertEquals("2/2", f.toString());
1377:
1378: f = Fraction.getFraction(Integer.MIN_VALUE, 0, 1);
1379: assertEquals("-2147483648/1", f.toString());
1380:
1381: f = Fraction.getFraction(-1, 1, Integer.MAX_VALUE);
1382: assertEquals("-2147483648/2147483647", f.toString());
1383: }
1384:
1385: public void testToProperString() {
1386: Fraction f = null;
1387:
1388: f = Fraction.getFraction(3, 5);
1389: String str = f.toProperString();
1390: assertEquals("3/5", str);
1391: assertSame(str, f.toProperString());
1392:
1393: f = Fraction.getFraction(7, 5);
1394: assertEquals("1 2/5", f.toProperString());
1395:
1396: f = Fraction.getFraction(14, 10);
1397: assertEquals("1 4/10", f.toProperString());
1398:
1399: f = Fraction.getFraction(4, 2);
1400: assertEquals("2", f.toProperString());
1401:
1402: f = Fraction.getFraction(0, 2);
1403: assertEquals("0", f.toProperString());
1404:
1405: f = Fraction.getFraction(2, 2);
1406: assertEquals("1", f.toProperString());
1407:
1408: f = Fraction.getFraction(-7, 5);
1409: assertEquals("-1 2/5", f.toProperString());
1410:
1411: f = Fraction.getFraction(Integer.MIN_VALUE, 0, 1);
1412: assertEquals("-2147483648", f.toProperString());
1413:
1414: f = Fraction.getFraction(-1, 1, Integer.MAX_VALUE);
1415: assertEquals("-1 1/2147483647", f.toProperString());
1416:
1417: assertEquals("-1", Fraction.getFraction(-1).toProperString());
1418: }
1419: }
|