import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;

/**
 * This class implements several tests to illustrate the use of MyList.
 * @author fpereira
 */
public class TestMyList {
  /**
   * Class constructor.
   * @param name the name of the test.
   */
  public static void main(String args[]) {
    testAdd();
    testSet();
    testRemove();
  }

  private static <E> void assertEquals(E s1, E s2) {
    if (!s1.equals(s2)) {
      throw new Error("Test failed!");
    }
  }


  private static void assertEquals(String s1, String s2) {
    if (!s1.equals(s2)) {
      throw new Error("Test failed!");
    }
  }

  /**
   * Test if we can correctly add elements into MyList.
   * @throws Exception in case something goes wrong.
   */
  public static final void testAdd() {
    String[] as = {"a", "b", "c", "a", "d", "e"};
    MyList<String> myList = new MyList<String>();
    for (String s : as) {
      myList.add(s);
    }
    Collection<String> cs = new LinkedList<String>();
    for (String s : as) {
      cs.add(s);
    }
    for (String s : myList) {
      assertEquals(cs.contains(s), true);
    }
  }

  /**
   * Test if we can set elements of MyList.
   * @throws Exception in case something goes wrong.
   */
  public static final void testSet() {
    String[] as = {"a", "b", "c", "d", "e"};
    MyList<String> myList = new MyList<String>();
    for (String s : as) {
      myList.add(s);
    }
    for (int i = 0; i < as.length; i++) {
      String newValue = myList.get(i) + "" + myList.get(i);
      myList.set(i, newValue);
    }
    for (int i = 0; i < as.length; i++) {
      assertEquals(myList.get(i), as[i] + as[i]);
    }
  }

  /**
   * Test if we can remove elements of MyList.
   * @throws Exception in case something goes wrong.
   */
  public static final void testRemove() {
    String[] as = {"a", "b", "c", "d", "e"};
    MyList<String> myList = new MyList<String>();
    for (String s : as) {
      myList.add(s);
    }
    for (Iterator<String> it = myList.iterator(); it.hasNext();) {
      String next = it.next();
      if (next.equals("c") | next.equals("e")) {
        it.remove();
      }
    }
    assertEquals(myList.get(0), "a");
    assertEquals(myList.get(1), "b");
    assertEquals(myList.get(2), "d");
  }

}

