mirror of
https://bitbucket.org/Mattrixwv/my-classes.git
synced 2025-12-06 18:23:57 -05:00
Added exception handling and test function to Stopwatch
This commit is contained in:
@@ -31,6 +31,11 @@
|
||||
|
||||
namespace mee{
|
||||
class Stopwatch{
|
||||
public:
|
||||
//Create an error class
|
||||
class stopBeforeStart{}; //Used in stop() to check if you are trying to stop the stopwatch before starting it
|
||||
class timeBeforeStart{}; //Used in getTime() to check if you are trying to get a time before you start it
|
||||
class invalidTimeResolution{}; //Used to detect invalid time resolution in the getStr function
|
||||
private:
|
||||
std::chrono::high_resolution_clock::time_point startTime; //The time the start function was called
|
||||
std::chrono::high_resolution_clock::time_point endTime; //The time the stop function was called
|
||||
@@ -45,9 +50,9 @@ private:
|
||||
if(!hasStopped){
|
||||
endTime = std::chrono::high_resolution_clock::now();
|
||||
}
|
||||
//If the timer hasn't been started return a -1 as an error
|
||||
//If the timer hasn't been started throw an exception
|
||||
if(!hasStarted){
|
||||
return -1;
|
||||
throw timeBeforeStart();
|
||||
}
|
||||
//Decide what resolution to make the duration
|
||||
if(timeResolution == HOUR){
|
||||
@@ -100,11 +105,16 @@ public:
|
||||
//Set the stop time and flag
|
||||
void stop(){
|
||||
//Put this first to ensure the time recorded is as close to the call time as possible
|
||||
endTime = std::chrono::high_resolution_clock::now();
|
||||
std::chrono::high_resolution_clock::time_point tempTime = std::chrono::high_resolution_clock::now();
|
||||
//Make sure the stopwatch has started before you say it has stopped
|
||||
if(hasStarted){
|
||||
endTime = tempTime; //Set the end time appropriately
|
||||
hasStopped = true; //Show that the stop function has been called
|
||||
}
|
||||
//If the stopwatch hadn't been started throw an exception
|
||||
else{
|
||||
throw stopBeforeStart();
|
||||
}
|
||||
}
|
||||
//Return the duration in nanoseconds
|
||||
double getNano(){
|
||||
@@ -158,8 +168,8 @@ public:
|
||||
case MILLISECOND: timeStr << "milliseconds"; break;
|
||||
case MICROSECOND: timeStr << "microseconds"; break;
|
||||
case NANOSECOND: timeStr << "nanoseconds"; break;
|
||||
case DEFAULT: break;
|
||||
default: timeStr << "ERROR";
|
||||
case DEFAULT: timeStr << "time"; break;
|
||||
default: throw invalidTimeResolution(); //This should never be hit with this code, but it's good to have all the bases covered
|
||||
}
|
||||
|
||||
//Return the string
|
||||
|
||||
88
testStopwatch.cpp
Normal file
88
testStopwatch.cpp
Normal 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
|
||||
*/
|
||||
Reference in New Issue
Block a user