Commit e8483e61 authored by Ari Trachtenberg's avatar Ari Trachtenberg
Browse files

Updated basic tests.

parent 88e25084
Loading
Loading
Loading
Loading
+31 −13
Original line number Diff line number Diff line
@@ -10,11 +10,16 @@ public class Main {
    // constants
    static final int memSize = 10; // the number of bits backing the counter

    // ... test constants
    static final int DEFAULT_SIZE = 10; // number of bits in the default counter

    /**
     * Run {@link MyDistinctCounter} through the test strings provided, and report its results.
     * @param testStrings The strings to run by the {@link MyDistinctCounter}.
     * @param MAX_DIFF The maximum multiplicative error tolerance between
     *                 ctual and reported numbers of distinct strings.
     */
    static void test(ArrayList<String> testStrings) {
    static void test(ArrayList<String> testStrings, int MAX_DIFF) {
        // Show the counter my test strings
        MyDistinctCounter tester = new MyDistinctCounter(memSize);
        Set<String> verifier = new HashSet<>();
@@ -26,13 +31,20 @@ public class Main {

        // Record the counter's state
        FixedBitArray savedState = tester.currentState();
        System.out.println("Counter state is: "+ savedState);

        // Output the guess
        MyDistinctCounter newFoo = new MyDistinctCounter(memSize);
        newFoo.setMem(savedState);
        System.out.println("guessed # of distinct strings: "+newFoo.numDistinct());
        System.out.println(" actual # of distinct strings: "+verifier.size());
        MyDistinctCounter newTest = new MyDistinctCounter(memSize);
        newTest.setMem(savedState);

        // Check that the answer is within accepted parameters
        int recorded = newTest.numDistinct();
        int actual = verifier.size();
        if ( ((float) recorded / actual > MAX_DIFF)
            || ((float) actual / recorded > MAX_DIFF) )
            throw new RuntimeException("Test failed - factor > " +
                    MAX_DIFF+
                    " between actual and guessed number of distinct entries.");
        System.out.println("... passed");
    }

    /**
@@ -40,7 +52,7 @@ public class Main {
     */
    public static void runSimpleTest() {
        System.out.println("Simple test:");
        test(new ArrayList<>(List.of("Bravo", "Alfa", "Charlie", "Kilo", "Charlie", "Alfa", "Bravo")));
        test(new ArrayList<>(List.of("Bravo", "Alfa", "Charlie", "Kilo", "Charlie", "Alfa", "Bravo")), 10);
    }

    /**
@@ -48,7 +60,7 @@ public class Main {
     * @param arg The argument to use in the test.
     */
    public static void runLongerTest(String arg) {
        System.out.println("\n\n\n"+"Longer test:");
        System.out.println("Longer test:");
        final BigInteger two = new BigInteger("2");
        final BigInteger modulus = new BigInteger(1,arg.getBytes()); // makes up a modulus based on the supplied argument
        BigInteger base = two;
@@ -57,15 +69,21 @@ public class Main {
            longList.add(base.toString());
            base=base.multiply(two).mod(modulus);
        }
        test(longList);
        test(longList, 1000);
    }

    public static void main(String[] args) {
        // Method tests
        System.out.println("Method tests:");
        MyDistinctCounter test = new MyDistinctCounter(DEFAULT_SIZE);
        test.saw("This is only a test.");
        test.numDistinct();
        System.out.println("... passed");

        // System tests
        runSimpleTest();
        runLongerTest("Hello World");

        if (args.length<1)
            throw new Error("Please provide a command-line argument.");
        else
            runLongerTest(args[0]);
        System.exit(0);
    }
}