Skip to content
Snippets Groups Projects
Forked from EC504 / HW1 / hw1p1
31 commits behind the upstream repository.
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
Main.java 2.21 KiB
package edu.bu.ec504.hw1p1;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {
    // constants
    static final int stateSize = 10; // the number of bytes in the myDistinctCounter object

    /**
     * Run {@link myDistinctCounter} through the test strings provided, and report its results.
     * @param testStrings The strings to run by the {@link myDistinctCounter}.
     */
    static void test(ArrayList<String> testStrings) {
        // Show the counter my test strings
        myDistinctCounter tester = new myDistinctCounter(stateSize);
        for (String test: testStrings) {
            tester.saw(test);
        }

        // Record the counter's state
        Byte[] savedState = tester.currentState();
        System.out.println("Counter state is: "+ Arrays.toString(savedState));

        // Output the prediction
        myDistinctCounter newFoo = new myDistinctCounter(savedState);
        System.out.println("# of distinct strings: "+newFoo.numDistinct());
    }

    /**
     * Runs a simple test, based on the example in the homework description.
     */
    public static void runSimpleTest() {
        System.out.println("Simple test:");
        test(new ArrayList<>(List.of("Bravo", "Alfa", "Charlie", "Kilo", "Charlie", "Alfa", "Bravo")));
    }

    /**
     * Runs a longer, more complicated test based on a supplied argument
     * @param arg The argument to use in the test.
     */
    public static void runLongerTest(String arg) {
        System.out.println("\n\n\n"+"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;
        ArrayList<String> longList = new ArrayList<>(10000);
        for (int ii=0; ii<10000; ii++) {
            longList.add(base.toString());
            base=base.multiply(two).mod(modulus);
        }
        test(longList);
    }

    public static void main(String[] args) {
        runSimpleTest();

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