//
// SecretTest.java
//

public class SecretTest {
    public static void main (String args[]) {
	// This is done to make sure your code doesn't trash my files!
	// We'll learn more about Java security later.
	System.setSecurityManager (new StrictSecurityManager ());

	try {
	    int errors = 0;
	    StringTable s1;

	    s1 = new StringTable ();
	    s1.addName ("Alice", 0.5);
	    s1.addName (new String ("Alice"), 0.4); // Should replace Alice's entry

	    System.out.print ("Test 1: ");
	    double val = s1.getValue ("Alice");

	    if (val != 0.4) {
		System.out.println ("FAILURE [hint: you might be using == to compare Objects, where you should be using .equals () to compare values]");
		errors++;
	    } else {
		System.out.println ("okay");
	    }

	    System.out.flush ();

	    System.out.print ("Test 2: ");
	    val = s1.getValue ("Bob");
	    if (val != 0.0) {
		System.out.println ("FAILURE");
	    } else {
		System.out.println ("okay");
	    }

	    System.out.print ("Test 3: ");
	    String lowest = s1.getNthLowest (0);
	    if (!lowest.equals ("Alice")) {
		System.out.println ("FAILURE");
		errors++;
	    } else {
		System.out.println ("okay");
	    }

	    s1.addName (new String ("Bob"), 0.4);

	    lowest = s1.getNthLowest (0);

	    System.out.print ("Test 4: ");

	    if (lowest.equals ("Alice")) {
		lowest = s1.getNthLowest (1);
		if (!lowest.equals ("Bob")) {
		    System.out.println ("FAILURE");
		    errors++;
		} else {
		    System.out.println ("okay");
		}
	    } else if (lowest.equals ("Bob")) {
		lowest = s1.getNthLowest (1);
		if (!lowest.equals ("Alice")) {
		    System.out.println ("FAILURE");
		    errors++;
		} else {
		    System.out.println ("okay");
		}
	    } else {
		System.out.println ("FAILURE");
		errors++;
	    }

	    // Add a lot of entries
	    for (int i = 1; i < 20; i++) {
		s1.addName ("entry" + i, i);
	    }

	    System.out.print ("Test 5: ");

	    lowest = s1.getNthLowest (2);
	    if (!lowest.equals ("entry1")) {
		System.out.println ("FAILURE");
		errors++;
	    } else {
		System.out.println ("okay");
	    }

	    System.out.print ("Test 6: ");
	    
	    lowest = s1.getNthLowest (3);
	    if (!lowest.equals ("entry2")) {
		System.out.println ("FAILURE");
		errors++;
	    } else {
		System.out.println ("okay");
	    }

	    System.out.print ("Test 7: ");

	    lowest = s1.getNthLowest (20);
	    if (!lowest.equals ("entry19")) {
		System.out.println ("FAILURE");
		errors++;
	    } else {
		System.out.println ("okay");
	    }

	    System.out.print ("Test 8: ");

	    int size = s1.size ();

	    if (size != 21) {
		System.out.println ("FAILURE");
		errors++;
	    } else {
		System.out.println ("okay");
	    }

	    StringIterator keys = s1.keys ();
	    int iterno = 0;

	    while (keys.hasNext ()) {
		String next = keys.next ();

		if (iterno == 17) {
		    System.out.print ("Test 9: ");
		    
		    if (!next.equals ("entry16")) {
			System.out.println ("FAILURE");
			// System.err.println ("next: " + next);
			errors++;
		    } else {
			System.out.println ("okay");
		    }
		}
		iterno++;
	    }

	    System.out.print ("Test 10: ");

	    if (iterno != 21) {
		System.out.println ("FAILURE");
		errors++;
	    } else {
		System.out.println ("okay");
	    }

	    if (errors == 0) {
		System.out.println ("Congratulations!  No errors in secret tests.");
	    } else {
		System.out.println ("Finished secret tests.  " + errors + " apparent errors.");
	    }
	} catch (Exception e) {
	    System.out.println ("Failure: unexpected exception: " + e);
	}
    }
}