Added a Stopwatch class and tests to make sure it works propperly

This commit is contained in:
2019-03-01 19:48:05 -05:00
parent 8dd1a5e840
commit 3a3ed27845
5 changed files with 188 additions and 0 deletions

67
testStopwatch.java Normal file
View File

@@ -0,0 +1,67 @@
//Java/JavaClasses/testStopwatch.java
//Matthew Ellison
// Created: 03-01-19
//Modified: 03-01-19
//This class is used to test that stopwatch does what it is supposed to do (approximately)
import mattrixwv.Stopwatch;
public class testStopwatch{
private static final int NUM_TO_RUN = 100000;
public static void main(String[] argv){
Boolean failed = false; //A flag to determine if all tests were passed
//Print the test begin message
System.out.println("Begin test");
//Describe which test is being performed
System.out.println("Starting loop");
//Start the timer
Stopwatch timer = new Stopwatch();
timer.start();
for(int cnt = 0;cnt < NUM_TO_RUN;++cnt){
System.out.print(cnt);
}
//Stop the timer
timer.stop();
System.out.println("\nLoop completed");
//Check that the different resolutions work out correctly
System.out.println("Checking that resolutions line up correctly");
Double nano = timer.getNano();
if(timer.getMicro() != (nano.doubleValue() / 1000D)){
System.out.println("Error on microsecond resolution");
failed = true;
}
else if(timer.getMilli() != (nano.doubleValue() / 1000000D)){
System.out.println("Error on millisecond resolution");
failed = true;
}
else if(timer.getSecond() != (nano.doubleValue() / 1000000000D)){
System.out.println("Error on second resolution");
failed = true;
}
else if(timer.getMinute() != (nano.doubleValue() / 60000000000D)){
System.out.println("Error on minute resolution");
failed = true;
}
else if(timer.getHour() != (nano.doubleValue() / 3600000000000D)){
System.out.println("Error on hour resolution");
failed = true;
}
else{
System.out.println("All time resolution tests completed successfully");
}
System.out.println("Completed resolution checking");
//Print the results
System.out.printf("The timer results in: %s\n", timer.getStr());
if(!failed){
System.out.println("All tests completed successfully");
}
else{
System.out.println("Test failed. Try again!");
}
}
}