Added exception handling and test function to Stopwatch

This commit is contained in:
2019-02-12 00:21:12 -05:00
parent 45f6d69507
commit 53d2a266c3
2 changed files with 103 additions and 5 deletions

88
testStopwatch.cpp Normal file
View File

@@ -0,0 +1,88 @@
//myClasses/testStopwatch.cpp
//Matthew Ellison
// Created: 02-12-19
//Modified: 02-12-19
//This file is a simple test for the Stopwatch class
#include <iostream>
#include "Stopwatch.hpp"
int main(){
mee::Stopwatch timer;
//Try to stop the timer without starting it
std::cout << "Testing the stopBeforeStart error" << std::endl;
try{
timer.stop();
std::cout << "Test failed" << std::endl;
}
catch(mee::Stopwatch::invalidTimeResolution){
std::cout << "There was an invalid time resolution" << std::endl;
std::cout << "Test failed" << std::endl;
}
catch(mee::Stopwatch::timeBeforeStart){
std::cout << "Tried to get time before the stopwatch was started" << std::endl;
std::cout << "Test failed" << std::endl;
}
catch(mee::Stopwatch::stopBeforeStart){
std::cout << "Tried to stop the stopwatch before it was started" << std::endl;
std::cout << "Test successful" << std::endl;
}
//Try to get the time on it without starting it
std::cout << "Testing the timeBeforeStart error" << std::endl;
try{
std::cout << timer.getStr() << std::endl;
std::cout << "Test failed" << std::endl;
}
catch(mee::Stopwatch::invalidTimeResolution){
std::cout << "There was an invalid time resolution" << std::endl;
std::cout << "Test failed" << std::endl;
}
catch(mee::Stopwatch::timeBeforeStart){
std::cout << "Tried to get time before the stopwatch was started" << std::endl;
std::cout << "Test successful" << std::endl;
}
catch(mee::Stopwatch::stopBeforeStart){
std::cout << "Tried to stop the stopwatch before it was started" << std::endl;
std::cout << "Test failed" << std::endl;
}
//Use it correctly
std::cout << "Using the class correctly" << std::endl;
try{
timer.start();
for(int cnt = 0;cnt < 1000000;++cnt){
int num = cnt;
}
timer.stop();
std::cout << "It took " << timer.getStr() << " to complete this loop" << std::endl;
std::cout << "Test successful" << std::endl;
}
catch(mee::Stopwatch::invalidTimeResolution){
std::cout << "There was an invalid time resolution" << std::endl;
std::cout << "Test failed" << std::endl;
}
catch(mee::Stopwatch::timeBeforeStart){
std::cout << "Tried to get time before the stopwatch was started" << std::endl;
std::cout << "Test failed" << std::endl;
}
catch(mee::Stopwatch::stopBeforeStart){
std::cout << "Tried to stop the stopwatch before it was started" << std::endl;
std::cout << "Test failed" << std::endl;
}
return 0;
}
/* Results:
Testing the stopBeforeStart error
Tried to stop the stopwatch before it was started
Test successful
Testing the timeBeforeStart error
Tried to get time before the stopwatch was started
Test successful
Using the class correctly
It took 1.855 milliseconds to complete this loop
Test successful
*/