package highOrder;

import java.util.LinkedList;
import java.util.List;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

public class TestMap extends TestCase {
  public TestMap(String name) {
    super(name);
  }

  public static Test suite() {
    return new TestSuite(TestMap.class);
  }

  /**
   * Test if we increment a list of integers correctly.
   * 
   * @throws Exception
   *         in case something goes wrong.
   */
  public void testIncrementer() throws Exception {
    List<Integer> l0 = new LinkedList<Integer>();
    for (int i = 0; i < 16384; i++) {
      l0.add(i);
    }
    Mapper<Integer, Integer> m = new Incrementer();
    List<Integer> l1 = m.map(l0);
    assertEquals(l0.size(), l1.size());
    for (int i = 0; i < l1.size(); i++) {
      assertEquals((int) l1.get(i), i + 1);
    }
  }

  /**
   * Test if we can map a list of String's to a list of the sizes of these
   * String's.
   * 
   * @throws Exception
   *         in case something gets screwed.
   */
  public void testGetSize() throws Exception {
    List<String> l0 = new LinkedList<String>();
    for (int i = 0; i < 16384; i++) {
      l0.add("AAA");
    }
    Mapper<String, Integer> m = new Mapper<String, Integer>() {
      public Integer apply(String e) {
        return e.length();
      };
    };
    List<Integer> l1 = m.map(l0);
    assertEquals(l0.size(), l1.size());
    for (int i = 0; i < l1.size(); i++) {
      assertEquals((int) l1.get(i), 3);
    }
  }

  /**
   * Test if we can map a list of String's to a list of the first character of
   * each string.
   * 
   * @throws Exception
   *         in case something fails.
   */
  public void testToFirstChar() throws Exception {
    List<String> l0 = new LinkedList<String>();
    for (int i = 0; i < 16384; i++) {
      l0.add("A");
    }
    Mapper<String, Character> m = new Mapper<String, Character>() {
      public Character apply(String e) {
        if (e.length() > 0)
          return e.charAt(0);
        else
          return new Character('\0');
      };
    };
    List<Character> l1 = m.map(l0);
    assertEquals(l0.size(), l1.size());
    for (int i = 0; i < l1.size(); i++) {
      assertEquals((char) l1.get(i), 'A');
    }
  }
}
