cleaned up stopwatch test and added it.
[feisty_meow.git] / nucleus / library / tests_timely / test_stopwatch.cpp
1 //////////////
2 // Name   : test_stopwatch
3 // Author : Chris Koeritz
4 // Rights : Copyright (c) 1991-$now By Author
5 //////////////
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 ]
9 //////////////
10
11 #include <application/application_shell.h>
12 #include <application/hoople_main.h>
13 #include <basis/functions.h>
14 #include <basis/guards.h>
15 #include <loggers/console_logger.h>
16 #include <loggers/critical_events.h>
17 #include <loggers/program_wide_logger.h>
18 #include <mathematics/chaos.h>
19 #include <structures/static_memory_gremlin.h>
20 #include <timely/stopwatch.h>
21 #include <timely/time_control.h>
22
23 using namespace basis;
24 using namespace application;
25 using namespace loggers;
26 using namespace mathematics;
27 using namespace structures;
28 using 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
45 class test_stopwatch : public application_shell
46 {
47 public:
48   test_stopwatch() : application_shell() {}
49   DEFINE_CLASS_NAME("test_stopwatch");
50   virtual int execute();
51 };
52
53 int test_stopwatch::execute()
54 {
55   stopwatch fred_time;
56   chaos randomizer;
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("stopwatch:: works for those functions tested.");
85   return 0;
86 }
87
88 HOOPLE_MAIN(test_stopwatch, )
89