From 53d2a266c3e5a97f3a9fe0ebb160aa46199f4de6 Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Tue, 12 Feb 2019 00:21:12 -0500 Subject: [PATCH] Added exception handling and test function to Stopwatch --- Stopwatch.hpp | 20 ++++++++--- testStopwatch.cpp | 88 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 5 deletions(-) create mode 100644 testStopwatch.cpp diff --git a/Stopwatch.hpp b/Stopwatch.hpp index 6f556db..aceb2a8 100644 --- a/Stopwatch.hpp +++ b/Stopwatch.hpp @@ -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 diff --git a/testStopwatch.cpp b/testStopwatch.cpp new file mode 100644 index 0000000..20f2e25 --- /dev/null +++ b/testStopwatch.cpp @@ -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 +#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 +*/