Ejemplo n.º 1
0
  public void test1() {
    Fraction r1 = new Fraction(2, 3);
    Fraction r2 = new Fraction(4, 6);
    Fraction r3 = new Fraction(4, 9);
    Fraction r4 = new Fraction(4, 3);
    Fraction r5 = new Fraction(0, 1);
    Fraction r6 = new Fraction(1, 1);
    Fraction r7 = new Fraction(1, 3);
    Fraction r8 = new Fraction(2, 1);
    Fraction r9 = new Fraction(2, 9);
    Fraction r10 = new Fraction(-1, 3);

    assertEquals(r1, r2);
    assertEquals(r3, r1.multiply(r2));
    assertEquals(r1, r1.multiply(r6));
    assertEquals(r1, r6.multiply(r1));
    assertEquals(r5, r5.multiply(r1));
    assertEquals(r10, r10.multiply(r6));

    assertEquals(r6, r1.divide(r2));
    assertEquals(r5, r5.divide(r1));
    assertEquals(r1, r1.divide(r6));
    assertEquals(r7, r3.divide(r4));
    try {
      assertEquals(r7, r3.divide(r5));
    } catch (ArithmeticException e) {
      assertTrue(true);
    }

    assertEquals(r4, r1.add(r2));
    assertEquals(r8, r2.add(r4));
    assertEquals(r6, r1.add(r7));
    assertEquals(r6, r5.add(r6));
    assertEquals(r1, r10.add(r6));

    assertEquals(r5, r1.subtract(r2));
    assertEquals(r9, r2.subtract(r3));
    assertEquals(r4, r4.subtract(r5));
    assertEquals(r10, r7.subtract(r1));
  }
Ejemplo n.º 2
0
  @Test
  public void testDivide() {
    assertEquals(zero, zero.divide(one));
    assertEquals(zero, zero.divide(subtractOne));
    assertEquals(zero, zero.divide(posRat));
    assertEquals(zero, zero.divide(negRat));

    assertEquals(one, one.divide(one));
    assertEquals(posRat, posRat.divide(one));
    assertEquals(negRat, negRat.divide(one));

    assertEquals(new Fraction(1, 2), new Fraction(1, 4).divide(new Fraction(1, 2)));
    assertEquals(new Fraction(1, 2), new Fraction(-1, 4).divide(new Fraction(-1, 2)));
    assertEquals(new Fraction(2, 5), new Fraction(-3, 10).divide(new Fraction(-3, 4)));
    assertEquals(new Fraction(100, 111), new Fraction(1700, 8547).divide(new Fraction(17, 77)));

    assertEquals(posRat, posRat.multiply(negRat).divide(negRat));
  }
Ejemplo n.º 3
0
  @Test
  public void testMultiply() {
    assertEquals(zero, zero.multiply(zero));
    assertEquals(zero, one.multiply(zero));
    assertEquals(zero, zero.multiply(posRat));
    assertEquals(zero, zero.multiply(negRat));

    assertEquals(one, one.multiply(one));
    assertEquals(posRat, one.multiply(posRat));
    assertEquals(negRat, one.multiply(negRat));
    assertEquals(negRat, negRat.multiply(one));

    assertEquals(new Fraction(1, 4), new Fraction(1, 2).multiply(new Fraction(1, 2)));
    assertEquals(new Fraction(-1, 4), new Fraction(1, 2).multiply(new Fraction(-1, 2)));
    assertEquals(new Fraction(-3, 10), new Fraction(2, 5).multiply(new Fraction(-3, 4)));
    assertEquals(new Fraction(1700, 8547), new Fraction(17, 77).multiply(new Fraction(100, 111)));
  }