package com.primeFactorizer;

import junit.framework.TestCase;

/**
 * This class tests the PrimeFactorizer class.
 * @author fpereira
 *
 */
public class TestFactors extends TestCase {
  /**
   * Class constructor.
   * @param name the name of the test.
   */
  public TestFactors(final String name) {
    super(name);
  }

  /**
   * Example of a test. Just tests if 2 + 2 == 4.
   * @throws Exception in case an error happens.
   */
  public final void testEx() throws Exception {
    int tmp = 2 + 2;
    assertEquals(4, tmp);
  }

  /**
   * Tests if the factorizer can factor the integer 2.
   * @throws Exception in case an error happens.
   */
  public final void testTwo() throws Exception {
    int[] factors = PrimeFactorizer.factor(2);
    assertEquals(1, factors.length);
    assertEquals(2, factors[0]);
  }

  /**
   * This method tests if we can factor the integer 3.
   * @throws Exception in case some error happens.
   */
  public final void testThree() throws Exception {
    int[] factors = PrimeFactorizer.factor(3);
    assertEquals(1, factors.length);
    assertEquals(3, factors[0]);
  }

  /**
   * This method tests if we can factor the integer 4.
   * @throws Exception in case some error happens.
   */
  public final void testFour() throws Exception {
    int[] factors = PrimeFactorizer.factor(4);
    assertEquals(2, factors.length);
    assertEquals(2, factors[0]);
    assertEquals(2, factors[1]);
  }

  /**
   * This method tests if we can factor the integer 5.
   * @throws Exception in case some error happens.
   */
  public final void testFive() throws Exception {
    int[] factors = PrimeFactorizer.factor(5);
    assertEquals(1, factors.length);
    assertEquals(5, factors[0]);
  }

  /**
   * This method tests if we can factor the integer 6.
   * @throws Exception in case some error happens.
   */
  public final void testSix() throws Exception {
    int[] factors = PrimeFactorizer.factor(6);
    assertEquals(2, factors.length);
    assertEquals(2, factors[0]);
    assertEquals(3, factors[1]);
  }

  /**
   * This method tests if we can factor the integer 7.
   * @throws Exception in case some error happens.
   */
  public final void testSeven() throws Exception {
    int[] factors = PrimeFactorizer.factor(7);
    assertEquals(1, factors.length);
    assertEquals(7, factors[0]);
  }

  /**
   * This method tests if we can factor the integer 8.
   * @throws Exception in case some error happens.
   */
  public final void testEight() throws Exception {
    int[] factors = PrimeFactorizer.factor(8);
    assertEquals(3, factors.length);
    assertEquals(2, factors[0]);
    assertEquals(2, factors[1]);
    assertEquals(2, factors[2]);
  }

  /**
   * This method tests if we can factor the integer 9.
   * @throws Exception in case some error happens.
   */
  public final void testNine() throws Exception {
    int[] factors = PrimeFactorizer.factor(9);
    assertEquals(2, factors.length);
    assertEquals(3, factors[0]);
    assertEquals(3, factors[1]);
  }

  /**
   * This method tests if we can factor the integer 9.
   * @throws Exception in case some error happens.
   */
  public final void testBig() throws Exception {
    int num = 2 * 3 * 5 * 7 * 11 * 13;
    int[] knownFacts = {2, 3, 5, 7, 11, 13};
    int[] factors = PrimeFactorizer.factor(num);
    assertEquals(6, factors.length);
    assertEquals(2, factors[0]);
    for (int i = 0; i < factors.length; i++) {
      assertEquals(knownFacts[i], factors[i]);
    }
  }
}
