Source of test_stopwatch.cpp


  1: //test_stopwatch.cpp
  2: //A test driver for the Scobey::Stopwatch class.

  4: #include <iostream>
  5: using namespace std;

  7: #include "utilities.h"
  8: using Scobey::Stopwatch;
  9: using Scobey::Pause;

 11: int main()
 12: {
 13:     Pause(0, "\nTesting the Scobey::Stopwatch class ..."
 14:         "\n\nThis program performs three different calculations, "
 15:         "times each, and displays\nthe timing results in two ways: "
 16:         "once by accessing the elapsed seconds directly\nand once "
 17:         "using the display() method of the Stopwatch class. In the "
 18:         "first case,\nthe event being timed has no name, in the second "
 19:         "case it is given a name in the\nconstructor of the Stopwatch "
 20:         "object, and in the last case it is given a name\nvia a call "
 21:         "to the setEventName() method.");

 23:     int sum = 0;
 24:     Stopwatch timer1;
 25:     timer1.start();
 26:     for (int i=1; i<=500; i++) sum += 6;
 27:     timer1.stop();
 28:     cout << "sum = " << sum << "\n";
 29:     cout << "Time taken to add 500 values, each equal to 6, is "
 30:         << timer1.getSeconds() << " seconds.\n";
 31:     Pause(0, "The above information was obtained by calling the "
 32:         "timer's getSeconds() method.");
 33:     Pause(0, "Now we report the same information by calling the "
 34:         "timer's display() method.");
 35:     timer1.display();
 36:     Pause();

 38:     sum = 0;
 39:     Stopwatch timer2("adding the same 500 values (with delay)");
 40:     timer2.start();
 41:     for (int i=1; i<=500; i++)
 42:     {
 43:         sum += 6;
 44:         timer2.delay();
 45:     }
 46:     timer2.stop();
 47:     cout << "sum = " << sum << "\n";
 48:     cout << "Time taken for " << timer2.getEventName() << " = "
 49:         << timer2.getSeconds() << " seconds\n";
 50:     Pause();
 51:     Pause(0, "Again we report the same information by calling the "
 52:         "timer's display() method.");
 53:     timer2.display();
 54:     Pause();

 56:     sum = 0;
 57:     Stopwatch timer3;
 58:     timer3.setEventName("adding 1000 values");
 59:     timer3.start();
 60:     for (int i=1; i<=1000; i++)
 61:     {
 62:         sum += 6;
 63:         timer3.delay();
 64:     }
 65:     timer3.stop();
 66:     cout << "sum = " << sum << "\n";
 67:     cout << "Time taken to add 1000 values, each equal to 6 (with delay) = "
 68:         << timer3.getSeconds() << " seconds\n";
 69:     Pause();
 70:     Pause(0, "And now we report the same information by calling the "
 71:         "timer's display() method.");
 72:     timer3.display();
 73:     Pause();

 75:     Pause(0, "No more tests to perform."
 76:         "\nProgram will now terminate.");
 77: }