//
// PublicTest.java
//

//
// Public (obvious) test cases for StringTable class.
//

public class PublicTest {
    public static void main (String args[]) {
	System.setSecurityManager (new StrictSecurityManager ());

	try {
	    int errors = 0;
	    StringTable s1;

	    System.out.println ("Constructing empty StringTable...");
	    s1 = new StringTable ();
	    System.out.println ("Result: " + s1);
	    
	    System.out.println ("Adding: <Alice, 0.5>");
	    s1.addName ("Alice", 0.5);
	    System.out.println ("Result: " + s1);

	    double val = s1.getValue ("Alice");
	    if (val != 0.5) {
		System.out.println ("ERROR: value is " + val);
		errors++;
	    } else {
		System.out.println ("Get value: okay");
	    }

	    val = s1.getValue ("Bob");
	    if (val != 0.0) {
		System.out.println ("ERROR: value is " + val);
		errors++;
	    } else {
		System.out.println ("Get value: okay");
	    }

	    String lowest = s1.getNthLowest (0);
	    if (!lowest.equals ("Alice")) {
		System.out.println ("ERROR: lowest is " + lowest);
		errors++;
	    } else {
		System.out.println ("Get lowest: okay");
	    }

	    System.out.println ("Trying keys: should print Alice: ");
	    StringIterator keys = s1.keys ();
	    while (keys.hasNext ()) {
		System.out.println (keys.next ());
	    }

	    System.out.println ("Adding: <Bob, 0.3>");
	    s1.addName ("Bob", 0.3);
	    System.out.println ("Result: " + s1);

	    lowest = s1.getNthLowest (1);
	    if (!lowest.equals ("Alice")) {
		System.out.println ("ERROR: lowest is " + lowest);
		errors++;
	    } else {
		System.out.println ("Get lowest: okay");
	    }

	    lowest = s1.getNthLowest (0);
	    if (!lowest.equals ("Bob")) {
		System.out.println ("ERROR: lowest is " + lowest);
		errors++;
	    } else {
		System.out.println ("Get lowest: okay");
	    }

	    System.out.println ("Trying keys: should print: Bob, Alice");
	    keys = s1.keys ();
	    boolean first = true;
	    while (keys.hasNext ()) {
		if (first) { first = false; }
		else { System.out.print (", "); }
		System.out.print (keys.next ());
	    }
	    System.out.println ("");

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