/* Testing binary search trees
 * 15-122 Principles of Imperative Computation, Fall 2010
 * Frank Pfenning
 */

#use <string>
#use <conio>
#use <rand>

elem elem_fromint(int k) {
  string s = string_fromint(k);
  wcount wc = alloc(struct wcount);
  wc->word = s;
  wc->count = k;
  return wc;
}

int main() {
  int n = (1<<9)-1;		// 1<<9 for -d; 1<<13 for timing
  int num_tests = 10;		// 10 for -d;  100 for timing
  int seed = 0xc0c0ffee;
  rand_t gen = init_rand(seed);
  elem[] A = alloc_array(elem, n);
  for (int i = 0; i < n; i++)
    A[i] = elem_fromint(rand(gen));
  
  assert(n+num_tests > 0);
  print("Testing bst of size "); printint(n);
  print(" "); printint(num_tests); print(" times\n");
  for (int j = 0; j < num_tests; j++) {
    bst B = bst_new();
    for (int i = 0; i < n; i++) {
      bst_insert(B, A[(j+i)%n]);
    }
    for (int i = 0; i < n; i++) {
      elem e = A[(j+i) % n];
      assert(bst_search(B, elem_key(e)) == e); /* insert ok? */
    }
  }
  print("Passed all tests!\n");
  return 0;
}
