import junit.framework.TestCase;

public class TestRational extends TestCase {
  private static Rational zero;
  private static Rational oneHalf;
  public static void main(String[] args) {
    junit.textui.TestRunner.run(TestRational.class);
  }

  protected void setUp() {
    zero = new Rational();
    oneHalf = new Rational(1, 2);
  }

  private static void assertNumDenomEquals(int num, int denom, Rational r) {
    assertEquals(num, r.numerator());
    assertEquals(denom, r.denominator());
  }

  public static void testDefaultConstructor() {
    assertNumDenomEquals(0, 1, zero);
  }

  public static void testSimpleRational() {
    assertNumDenomEquals(1, 2, oneHalf);
  }

  public static void testNeedsSimplifyingRational() {
    Rational r = new Rational(2, 4);
    assertNumDenomEquals(1, 2, r);
  }

  public static void testNegativeNum() {
    Rational r = new Rational(-2, 4);
    assertNumDenomEquals(-1, 2, r);
  }

  public static void testNegativeDenom() {
    Rational r = new Rational(2, -4);
    assertNumDenomEquals(-1, 2, r);
  }

  public static void testNegativeNumAndDenom() {
    Rational r = new Rational(-2, -4);
    assertNumDenomEquals(1, 2, r);
  }

  public static void testZeroNum() {
    Rational r = new Rational(0, -4);
    assertNumDenomEquals(0, 1, r);
  }

  public static void testMultiplication() {
    Rational r = new Rational(2, 3);
    Rational r2 = new Rational(5, 2);
    assertNumDenomEquals(5, 3, r.product(r2));
  }

  public static void testDivision() {
    Rational r = new Rational(2, 3);
    Rational r2 = new Rational(5, 2);
    assertNumDenomEquals(4, 15, r.quotient(r2));
  }

  public static void testAddition() {
    Rational r = new Rational(2, 3);
    Rational r2 = new Rational(5, 2);
    assertNumDenomEquals(19, 6, r.sum(r2));
  }

  public static void testNegate() {
    Rational r = new Rational(2, 3);
    assertNumDenomEquals(-2, 3, r.negate());
  }

  public static void testSubtraction() {
    Rational r = new Rational(2, 3);
    Rational r2 = new Rational(5, 2);
    assertNumDenomEquals(-11, 6, r.difference(r2));
  }

  public static void testDivideByZero() {
    try {
      Rational r = new Rational(2, 3);
      r.quotient(zero);
      fail();
    } catch (ArithmeticException e) {
      assertTrue(true);
    }
  } 

  public static void testConstructDenomWithZero() {
    try {
      Rational r = new Rational(0, 0);
      fail();
    } catch (ArithmeticException e) {
      assertTrue(true);
    }
  }

  public static void testToStringWithRealDenominator() {
    assertEquals("1/2", oneHalf.toString());
  }

  public static void testToStringWithDenominatorOfOne() {
    assertEquals("0", zero.toString());
  }
}
