t_timer.cpp

Go to the documentation of this file.
00001 /*****************************************************************************\
00002 *                                                                             *
00003 *  Name   : test_timer                                                        *
00004 *  Author : Chris Koeritz                                                     *
00005 *                                                                             *
00006 *******************************************************************************
00007 * Copyright (c) 1991-$now By Author.  This program is free software; you can  *
00008 * redistribute it and/or modify it under the terms of the GNU General Public  *
00009 * License as published by the Free Software Foundation; either version 2 of   *
00010 * the License or (at your option) any later version.  This is online at:      *
00011 *     http://www.fsf.org/copyleft/gpl.html                                    *
00012 * Please send any updates to: fred@gruntose.com                               *
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 // ACCEPT: acceptable timer deviation from the total time to wait.
00027 // NOTE: timer characteristics and the sleep characteristics for machines vary.
00028 // it may be necessary to play with ACCEPT to get this program to accept the
00029 // machine's particular characteristics.
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     // needs longer ints for the last two checks...
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   to_sleep = randomizer.inclusive(18, 188);
00077   to_sleep *= 11000;
00078   fred_time.start();
00079   usleep(to_sleep);
00080   fred_time.halt();
00081 #ifdef DEBUG_TIMER
00082   MessageBox(NIL, istring("sleep of %d milliseconds took %d milliseconds\n",
00083     to_sleep / 1000, fred_time.milliseconds()), "info", MB_OK);
00084 #endif
00085   if (absolute_value(to_sleep - 1000.0 * fred_time.milliseconds())
00086       > WIDER_ACCEPT * to_sleep)
00087     deadly_error(class_name(), "third", "unacceptable timer deviation");
00088   fred_time.reset();
00089 
00090   to_sleep = randomizer.inclusive(190, 88);
00091   to_sleep *= 100;
00092   fred_time.start();
00093   usleep(to_sleep);
00094   fred_time.halt();
00095 #ifdef DEBUG_TIMER
00096   LOG(isprintf("sleep of %d milliseconds took %d milliseconds\n",
00097       to_sleep / 1000, fred_time.milliseconds());
00098 #endif
00099   if (absolute_value(to_sleep - 1000.0 * fred_time.milliseconds())
00100       > WIDEST_ACCEPT * to_sleep)
00101     deadly_error(class_name(), "fourth", "unacceptable timer deviation");
00102   fred_time.reset();
00103 */
00104   guards::alert_message("timer:: works for those functions tested.");
00105   return 0;
00106 }
00107 
00108 HOOPLE_MAIN(test_timer, )
00109 

Generated on Fri Nov 28 04:29:38 2008 for HOOPLE Libraries by  doxygen 1.5.1