import java.util.Arrays;
import java.util.ArrayList;
import java.util.Random;
public class BinarySearchDriver {
  public static final int ARRAY_SIZE = 10000;
  public static final int MAX_RAND_STEP = 10;
  public static final int NUM_TEST_VALS = 100;

  public static void main (String[] args) {
    int[] array = buildRandomArray();
    int[] testValues = getRandomTestValues(array);

    ArrayList<Integer> list = arrayToList(array);

    for (int testValue : testValues) {
      int userIndex = BinarySearcher.find(array, testValue);
      int actualIndex = list.indexOf(testValue);

      if (userIndex != actualIndex) {
        System.out.println("Error: Search index does not match actual index.");
        System.exit(1);
      }
    }
    System.out.println("All values match.");
  }

  public static int[] getRandomTestValues(int[] array) {
    Random rand = new Random();
    int[] tests = new int[ARRAY_SIZE];
    for (int i = 0; i < tests.length; i++) {
      tests[i] = array[0] + rand.nextInt(array[array.length - 1]);
    }
    return tests;
  }

  public static int[] buildRandomArray() {
    Random rand = new Random();
    int[] arr = new int[ARRAY_SIZE];
    arr[0] = rand.nextInt(MAX_RAND_STEP);
    for (int i = 1; i < 10000; i++) {
      arr[i] = arr[i - 1] + rand.nextInt(MAX_RAND_STEP) + 1;
    }
    return arr;
  }

  public static ArrayList<Integer> arrayToList(int[] array) {
    ArrayList<Integer> intList = new ArrayList<Integer>();
    for (int i = 0; i < array.length; i++) {
      intList.add(array[i]);
    }
    return intList;
  }
}
