import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;

public class ArrayListUtilsDriver {
  private static int MAX_ARR_SIZE = 10000;
  private static int MAX_STRING_LEN = 5;

  private static boolean testRemoveDuplicates() {
    Character[] arr = { 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a',
      'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a' };
    ArrayList<Character> testArray = new ArrayList<Character>(Arrays.asList(arr));
    ArrayList<Character> result = ArrayListUtils.removeDuplicates(testArray);

    if (result.size() != 1) {
      return false;
    }

    arr[0] = 'b';
    testArray = new ArrayList<Character>(Arrays.asList(arr));
    result = ArrayListUtils.removeDuplicates(testArray);

    if (result.size() != 2) {
      return false;
    }

    arr[arr.length - 1] = 'b';
    testArray = new ArrayList<Character>(Arrays.asList(arr));
    result = ArrayListUtils.removeDuplicates(testArray);

    if (result.size() != 2 || !result.get(0).equals('b') || !result.get(1).equals('a')) {
      return false;
    }

    return randomizedRemoveDuplicates();
  }

  private static boolean randomizedRemoveDuplicates() {
    ArrayList<Character> testList = new ArrayList<Character>();
    Random rand = new Random();
    for (int i = 0; i < MAX_ARR_SIZE; i++) {
      testList.add((char) rand.nextInt(128));
    }
    ArrayList<Character> result = ArrayListUtils.removeDuplicates(testList);

    for (int i = 0; i < result.size(); i ++) {
      if (result.get(i) != testList.get(0)) {
        return false;
      }
      ArrayList<Character> singleton = new ArrayList<Character>();
      singleton.add(testList.get(0));
      testList.removeAll(singleton);
    }
    return true;
  }

  private static boolean testGetCommonStrings() {
    ArrayList<String> test1 = new ArrayList<String>();
    ArrayList<String> test2 = new ArrayList<String>();

    for (int i = 0; i < MAX_ARR_SIZE; i++) {
      String rand1 = newRandString();
      String rand2 = newRandString();
      test1.add(rand1);
      test2.add(rand2);
    }

    ArrayList<String> result = ArrayListUtils.getCommonStrings(test1, test2);

    for (String elem : result) {
      ArrayList<String> singleton = new ArrayList<String>();
      singleton.add(elem);

      test1.removeAll(singleton);
      test2.removeAll(singleton);
    }

    result = ArrayListUtils.getCommonStrings(test1, test2);
    if (result.size() > 0) {
      return false;
    }


    for (String elem : test1) {
      if (test2.contains(elem)) {
        return false;
      }
    }

    return true;
  }

  private static String newRandString() {
    StringBuilder protoString = new StringBuilder();
    Random rand = new Random();
    for (int i = 0; i < MAX_STRING_LEN; i++) {
      protoString.append(65 + rand.nextInt(26));
    }
    return protoString.toString();
  }

  /**
   * Entry point.
   */
  public static void main(String[] args) {
    ArrayListUtils.wordRecall("LoremIpsum.txt");

    System.out.println();

    if (testRemoveDuplicates()) {
      System.out.println("removeDuplicates Passed");
    } else {
      System.out.println("removeDuplicates Failed.");
    }

    System.out.println();

    if (testGetCommonStrings()) {
      System.out.println("getCommonStrings Passed");
    } else {
      System.out.println("getCommonStrings Failed.");
    }
  }

}
