00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include <basis/chaos.h>
00016 #include <basis/function.h>
00017 #include <basis/guards.h>
00018 #include <basis/log_base.h>
00019 #include <mechanisms/timer.h>
00020 #include <opsystem/application_shell.h>
00021 #include <loggers/console_logger.h>
00022 #include <data_struct/static_memory_gremlin.h>
00023
00024 #define DEBUG_TIMER
00025
00026
00027
00028
00029
00030 #define ACCEPT 0.01
00031 #define WIDER_ACCEPT 0.05
00032 #define WIDEST_ACCEPT 0.20
00033
00034 #define LOG(s) EMERGENCY_LOG(program_wide_logger(), s)
00035
00036 class test_timer : public application_shell
00037 {
00038 public:
00039 test_timer() : application_shell(class_name()) {}
00040 IMPLEMENT_CLASS_NAME("test_timer");
00041 virtual int execute();
00042 };
00043
00044 int test_timer::execute()
00045 {
00046 timer fred_time;
00047 chaos randomizer;
00048
00049 int to_sleep = randomizer.inclusive(2000, 10000);
00050
00051 fred_time.start();
00052 portable::sleep_ms(to_sleep);
00053 fred_time.halt();
00054 #ifdef DEBUG_TIMER
00055 LOG(isprintf("sleep of %0.3f seconds took %d milliseconds\n",
00056 float(to_sleep) / 1000.0, fred_time.milliseconds()));
00057 #endif
00058 if (absolute_value(to_sleep - fred_time.milliseconds())
00059 > ACCEPT * to_sleep)
00060 deadly_error(class_name(), "first", "unacceptable timer deviation");
00061 fred_time.reset();
00062
00063 to_sleep = randomizer.inclusive(7000, 12000);
00064 fred_time.start();
00065 portable::sleep_ms(to_sleep);
00066 fred_time.halt();
00067 #ifdef DEBUG_TIMER
00068 LOG(isprintf("sleep of %0.3f seconds took %d milliseconds\n",
00069 float(to_sleep) / 1000.0, fred_time.milliseconds()));
00070 #endif
00071 if (absolute_value(to_sleep - fred_time.milliseconds()) > ACCEPT * to_sleep)
00072 deadly_error(class_name(), "second", "unacceptable timer deviation");
00073 fred_time.reset();
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104 guards::alert_message("timer:: works for those functions tested.");
00105 return 0;
00106 }
00107
00108 HOOPLE_MAIN(test_timer, )
00109