feisty meow concerns codebase 2.140
test_stopwatch.cpp
Go to the documentation of this file.
1
2// Name : test_stopwatch
3// Author : Chris Koeritz
4// Rights : Copyright (c) 1991-$now By Author
6// This file is free software; you can modify/redistribute it under the terms
7// of the GNU General Public License. [ http://www.gnu.org/licenses/gpl.html ]
8// Feel free to send updates to: [ fred@gruntose.com ]
10
13#include <basis/functions.h>
14#include <basis/guards.h>
18#include <mathematics/chaos.h>
20#include <timely/stopwatch.h>
21#include <timely/time_control.h>
22
23using namespace basis;
24using namespace application;
25using namespace loggers;
26using namespace mathematics;
27using namespace structures;
28using namespace timely;
29
30#define DEBUG_TIMER
31
32// ACCEPT: acceptable timer deviation from the total time to wait.
33// NOTE: timer characteristics and the sleep characteristics for machines vary.
34// it may be necessary to play with ACCEPT to get this program to accept the
35// machine's particular characteristics.
36//#define ACCEPT 0.01
37//#define WIDER_ACCEPT 0.05
38//#define WIDEST_ACCEPT 0.20
39#define ACCEPT 0.9
40#define WIDER_ACCEPT 0.95
41#define WIDEST_ACCEPT 1.20
42
43#define LOG(s) EMERGENCY_LOG(program_wide_logger::get(), s)
44
45class test_stopwatch : public application_shell
46{
47public:
48 test_stopwatch() : application_shell() {}
49 DEFINE_CLASS_NAME("test_stopwatch");
50 virtual int execute();
51};
52
53int test_stopwatch::execute()
54{
55 stopwatch fred_time;
57
58 int to_sleep = randomizer.inclusive(20, 100);
59 // needs longer ints for the last two checks...
60 fred_time.start();
61 time_control::sleep_ms(to_sleep);
62 fred_time.halt();
63#ifdef DEBUG_TIMER
64 LOG(a_sprintf("sleep of %0.3f seconds took %d milliseconds\n",
65 float(to_sleep) / 1000.0, fred_time.milliseconds()));
66#endif
67 if (absolute_value(to_sleep - fred_time.milliseconds())
68 > ACCEPT * to_sleep)
69 deadly_error(class_name(), "first", "unacceptable timer deviation");
70 fred_time.reset();
71
72 to_sleep = randomizer.inclusive(76, 420);
73 fred_time.start();
74 time_control::sleep_ms(to_sleep);
75 fred_time.halt();
76#ifdef DEBUG_TIMER
77 LOG(a_sprintf("sleep of %0.3f seconds took %d milliseconds\n",
78 float(to_sleep) / 1000.0, fred_time.milliseconds()));
79#endif
80 if (absolute_value(to_sleep - fred_time.milliseconds()) > ACCEPT * to_sleep)
81 deadly_error(class_name(), "second", "unacceptable timer deviation");
82 fred_time.reset();
83
84 critical_events::alert_message(astring(class_name()) + ": works for those functions tested.");
85
86 return 0;
87}
88
89HOOPLE_MAIN(test_stopwatch, )
90
The application_shell is a base object for console programs.
virtual int execute()=0
< retrieves the command line from the /proc hierarchy on linux.
application_shell()
constructs an application_shell to serve as the root of the program.
a_sprintf is a specialization of astring that provides printf style support.
Definition astring.h:440
Provides a dynamically resizable ASCII character string.
Definition astring.h:35
static void alert_message(const char *info, const char *title="Alert Message")
shows the message in "info", with an optional "title" on the message.
a platform-independent way to acquire random numbers in a specific range.
Definition chaos.h:51
int inclusive(int low, int high) const
< Returns a pseudo-random number r, such that "low" <= r <= "high".
Definition chaos.h:88
A class for measuring event durations in real time.
Definition stopwatch.h:29
int milliseconds()
Returns the elapsed number of milliseconds on the stopwatch, overall.
Definition stopwatch.cpp:59
void halt()
Stops the timing.
Definition stopwatch.cpp:71
void reset()
Stops the stopwatch and clears it to zero time elapsed.
Definition stopwatch.cpp:57
void start()
Begins the timing.
Definition stopwatch.cpp:61
static void sleep_ms(basis::un_int msec)
a system independent name for a forced snooze measured in milliseconds.
#define deadly_error(c, f, i)
#define DEFINE_CLASS_NAME(objname)
Defines the name of a class by providing a couple standard methods.
Definition enhance_cpp.h:42
Provides macros that implement the 'main' program of an application.
#define HOOPLE_MAIN(obj_name, obj_args)
options that should work for most unix and linux apps.
Definition hoople_main.h:61
Implements an application lock to ensure only one is running at once.
The guards collection helps in testing preconditions and reporting errors.
Definition array.h:30
type absolute_value(type a)
Returns a if a is non-negative, and returns -a otherwise.
Definition functions.h:33
A logger that sends to the console screen using the standard output device.
An extension to floating point primitives providing approximate equality.
Definition averager.h:21
A dynamic container class that holds any kind of object via pointers.
Definition amorph.h:55
#include <time.h>
#define randomizer()
#define LOG(s)
#define ACCEPT