
/**
 * This tester checks out a Stopwatch class with a startStop() method, a
 * getTimeElapsed() method, and a reset() method.
 *
 * @author Richard White
 * @version 2019-04-01
 */
public class StopwatchUtilityTester
{
    public static void delay(double sec)
    {
        long start = System.currentTimeMillis();
        while ((System.currentTimeMillis() - start) / 1000.0 < sec)
        {
            // la la la
        }
        return;
    }
    
    public static void main(String[] args)
    {
        final double EPSILON = 0.002;
        int testsPassed = 0;
        System.out.print("Creating new StopwatchUtility...");
        StopwatchUtility s = new StopwatchUtility();
        testsPassed += 1;
        System.out.println("passed!");
        System.out.print("Starting stopwatch with startStop method...");
        s.startStop();
        testsPassed += 1;
        System.out.println("passed!");
        delay(1.5);
        System.out.println("Stopping stopwatch with startStop method...");
        s.startStop();
        System.out.print("Checking stopwatch with timeElapsed method...");
        double elapsed = s.getTimeElapsed();
        testsPassed += 1;
        System.out.println("passed!");
        System.out.print("Checking value of time elapsed...");
        System.out.print(elapsed);
        if (Math.abs(elapsed - 1.5) < EPSILON)
        {
            System.out.println(" passed!");
            testsPassed += 1;
        }
        else
        {
            System.out.println(" failed.");
        }
        System.out.println("Starting stopwatch again without resetting...");
        s.startStop();
        System.out.println(s.getTimeElapsed());
        delay(1.5);
        System.out.println("Stopping stopwatch");
        s.startStop();
        System.out.print("Checking value of time elapsed while running...");
        elapsed = s.getTimeElapsed();
        if (Math.abs(elapsed - 3.0) < EPSILON)
        {
            System.out.println(" passed!");
            testsPassed += 1;
        }
        else
        {
            System.out.println(" failed.");
        }
        System.out.print("Checking reset() method...");
        s.reset();
        System.out.println(" passed!");
        testsPassed += 1;
        
        System.out.print("Checking timeElapsed() results...");
        if (s.getTimeElapsed() == 0)
        {
            System.out.println(" passed!");
            testsPassed += 1;
        }
        else
        {
            System.out.println(" failed.");
        }
        System.out.println(testsPassed + "/7 tests passed.");
    }   
}
