Main Page | Modules | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

t_event_log.c

Go to the documentation of this file.
00001 /*
00002 ** Copyright (C) 2005 by Kevin L. Mitchell <klmitch@mit.edu>
00003 **
00004 ** This program is free software; you can redistribute it and/or modify
00005 ** it under the terms of the GNU General Public License as published by
00006 ** the Free Software Foundation; either version 2 of the License, or
00007 ** (at your option) any later version.
00008 **
00009 ** This program is distributed in the hope that it will be useful,
00010 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 ** GNU General Public License for more details.
00013 **
00014 ** You should have received a copy of the GNU General Public License
00015 ** along with this program; if not, write to the Free Software
00016 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 **
00018 ** @(#)$Id: t_event_log.c,v 1.6 2005/12/18 00:15:17 klmitch Exp $
00019 */
00029 #include <stdio.h>
00030 #include <stdlib.h>
00031 
00032 #include "event-test.h"
00033 
00034 RCSTAG("@(#)$Id: t_event_log.c,v 1.6 2005/12/18 00:15:17 klmitch Exp $");
00035 
00036 TEST_PROG(t_event_log, "Test operation of event_log_* functions")
00037 
00038 
00045 #define _str(a) #a
00046 
00054 #define str(a)  _str(a)
00055 
00062 #define LC_START 0xf720e0a9
00063 
00070 #define LC_EVLOGTEST 0x4ed403d2
00071 
00078 #define LC_EVDEBUGTEST 0x2d8fcebe
00079 
00086 static unsigned int log_called = LC_START; /* zeroed by first log call... */
00087 
00103 static void
00104 t_log(const char *file, int line, const char *func, const char *msg)
00105 {
00106   fprintf(stderr, "%s:%d%s%s%s: %s\n", file, line,
00107           func ? " (" : "", func ? func : "", func ? ")" : "", msg);
00108   log_called = strtoul(msg, 0, 16);
00109 }
00110 
00111 /* this is an attempt to keep these macros from being optimized away */
00112 static unsigned int _ev_log(ev_flags_t flag, const char *msg);
00113 static ev_err_t _ev_assert(int expr);
00114 static ev_err_t _ev_abort(void);
00115 static ev_err_t _ev_init(void);
00116 
00125 int
00126 main(int argc, char **argv)
00127 {
00128   ev_log_t logger = _ev_debuglog; /* save original logger for checking */
00129   ev_err_t err;
00130 
00131   /* explicitly not using ev_test_init(), since this program tests the
00132    * infrastructure that ev_test_init() is using...
00133    */
00134 
00135   /* First, check that event_log_set() can be called... */
00136   TEST(t_event_log, el_set, "Test event_log_set() operation",
00137        (!(err = event_log_set(t_log)) && _ev_debuglog == t_log &&
00138         _ev_asflags == EVAS_LOG), FATAL(0),
00139        ("event_log_set() properly sets logging callback"),
00140        ("event_log_set() operation failed; error code %u, logger %p (should "
00141         "be %p), _ev_asflags 0x%08lx (should be 0x%08lx)", err,
00142         (void *)_ev_debuglog, (void *)t_log, _ev_asflags,
00143         (ev_flags_t)EVAS_LOG));
00144 
00145   /* Now, does event_log_reset() do what it's supposed to? */
00146   TEST(t_event_log, el_reset, "Test event_log_reset() operation",
00147        (!(err = event_log_reset()) && _ev_debuglog == logger &&
00148         _ev_asflags == 0), FATAL(0),
00149        ("event_log_reset() properly resets logging callback"),
00150        ("event_log_reset() operation failed; error code %u, logger %p (should "
00151         "be %p), _ev_asflags 0x%08lx (should be 0)", err, (void *)_ev_debuglog,
00152         (void *)logger, _ev_asflags));
00153 
00154   /* OK, put the logger back; do it directly to avoid side-effects */
00155   _ev_debuglog = t_log;
00156 
00157   /* Test the bit flag toggles */
00158   /* Turn on debug logging... */
00159   TEST(t_event_log, el_debug_on, "Test event_log_debug() can turn on logging",
00160        (!(err = event_log_debug(1)) && _ev_asflags == EVAS_LOG), 0,
00161        ("event_log_debug(1) properly turns on debug logging"),
00162        ("event_log_debug(1) fails to turn on debug logging; error %u", err));
00163 
00164   /* ...and off... */
00165   TEST(t_event_log, el_debug_off, "Test event_log_debug() can turn off "
00166        "logging",
00167        (!(err = event_log_debug(0)) && _ev_asflags == 0), 0,
00168        ("event_log_debug(0) properly turns off debug logging"),
00169        ("event_log_debug(0) fails to turn off debug logging; error %u", err));
00170 
00171   _ev_asflags = 0; /* reset the asflags... */
00172 
00173   /* Make sure no messages have been output yet */
00174   TEST(t_event_log, el_logmsg_start, "Test that log messages have not yet "
00175        "been written",
00176        (log_called == LC_START), 0,
00177        ("Log messages have not been written yet (as expected)"),
00178        ("An unexpected log message appears to have been written..."));
00179 
00180   /* Turn on function tracing... */
00181   TEST(t_event_log, el_trace_on, "Test event_log_trace() can turn on tracing",
00182        (!(err = event_log_trace(1)) && _ev_asflags == EVAS_TRACE), 0,
00183        ("event_log_trace(1) properly turns function tracing on"),
00184        ("event_log_trace(1) fails to turn on function tracing; error %u",
00185         err));
00186 
00187   /* Now turn tracing off... */
00188   TEST(t_event_log, el_trace_off, "Test event_log_trace() can turn off "
00189        "tracing",
00190        (!(err = event_log_trace(0)) && _ev_asflags == 0), 0,
00191        ("event_log_trace(0) properly turns function tracing off"),
00192        ("event_log_trace(0) fails to turn off function tracing; error %u",
00193         err));
00194 
00195   /* Now, a log message should have been written while tracing was turned on */
00196   TEST(t_event_log, el_logmsg_first, "Test that a function trace message was "
00197        "written",
00198        (log_called != LC_START), 0,
00199        ("Function trace log message written (as expected)"),
00200        ("Function trace log message seems to have not been written..."));
00201 
00202   /* Restore log_called to its starting value... */
00203   log_called = LC_START;
00204 
00205   _ev_asflags = 0; /* reset the asflags... */
00206 
00207   /* Now lets turn aborting on... */
00208   TEST(t_event_log, el_abort_on, "Test event_log_abort() can turn on aborting",
00209        (!(err = event_log_abort(1)) && _ev_asflags == EVAS_ABORT), 0,
00210        ("event_log_abort(1) properly turns on aborting functionality"),
00211        ("event_log_abort(1) fails to turn on library abort; error %u", err));
00212 
00213   /* Now turn it off... */
00214   TEST(t_event_log, el_abort_off, "Test event_log_abort() can turn off "
00215        "aborting",
00216        (!(err = event_log_abort(0)) && _ev_asflags == 0), 0,
00217        ("event_log_abort(0) properly turns aborting functionality off"),
00218        ("event_log_abort(0) fails to turn off library abort; error %u", err));
00219 
00220   _ev_asflags = 0; /* reset the asflags... */
00221 
00222   /* Test that ev_log() functions correctly */
00223   TEST(t_event_log, el_evlog_off, "Test that ev_log() does nothing when off",
00224        (_ev_log(EVAS_LOG, str(LC_EVLOGTEST)) == LC_START), 0,
00225        ("ev_log() macro properly ignored log message when off"),
00226        ("ev_log() macro wrote a message even though it was turned off"));
00227 
00228   log_called = LC_START; /* reset, just in case... */
00229   _ev_asflags = EVAS_LOG; /* turn on logging... */
00230 
00231   /* Try again with logging turned on... */
00232   TEST(t_event_log, el_evlog_on, "Test that ev_log() logs when on",
00233        (_ev_log(EVAS_LOG, str(LC_EVLOGTEST)) == LC_EVLOGTEST), 0,
00234        ("ev_log() macro properly writes log message when on"),
00235        ("ev_log() failed to write log message even though it was turned on"));
00236 
00237   log_called = LC_START; /* reset the log_called value... */
00238   _ev_asflags = 0; /* reset the asflags... */
00239 
00240   /* Test that _ev_debug() functions correctly */
00241   _ev_debug(EVAS_LOG, __FILE__, __LINE__, _gca_func, "0x%08x", LC_EVDEBUGTEST);
00242   TEST(t_event_log, el_evdebug_off, "Test that _ev_debug() does nothing "
00243        "when off",
00244        (log_called == LC_START), 0,
00245        ("_ev_debug() call properly ignored log message when off"),
00246        ("_ev_debug() call wrote a message even though it was turned off"));
00247 
00248   log_called = LC_START; /* reset again, just in case... */
00249   _ev_asflags = EVAS_LOG; /* turn on logging... */
00250 
00251   /* Try again with logging turned on... */
00252   _ev_debug(EVAS_LOG, __FILE__, __LINE__, _gca_func, "0x%08x", LC_EVDEBUGTEST);
00253   TEST(t_event_log, el_evdebug_on, "Test that _ev_debug() logs when on",
00254        (log_called == LC_EVDEBUGTEST), 0,
00255        ("_ev_debug() properly writes log message when on"),
00256        ("_ev_debug() failed to write log message even though it was "
00257         "turned on"));
00258 
00259   /* Test the ev_abort() macro... */
00260   _ev_asflags = 0; /* first turn off log (and abort) flags... */
00261   TEST(t_event_log, el_evabort_off, "Test that ev_abort() does a return when "
00262        "turned off",
00263        ((err = _ev_abort()) == EV_ERR_ASSERT), 0,
00264        ("ev_abort() macro properly returns EV_ERR_ASSERT when turned off"),
00265        ("ev_abort() macro failed to operate properly; error %u", err));
00266 
00267   /* Next, let's test the ev_assert() macro... */
00268   _ev_asflags = 0; /* clear the asflags */
00269   TEST(t_event_log, el_assert_true, "Test that ev_assert() accepts true "
00270        "values",
00271        ((err = _ev_assert(1)) == 0 && _ev_asflags == 0), 0,
00272        ("ev_assert() macro properly accepts true value"),
00273        ("ev_assert() macro rejected true value; error %u asflags 0x%08lx", err,
00274         _ev_asflags));
00275 
00276   _ev_asflags = 0; /* just in case... */
00277   TEST(t_event_log, el_assert_false, "Test that ev_assert() rejects false "
00278        "values",
00279        ((err = _ev_assert(0)) == EV_ERR_ASSERT && _ev_asflags == EVAS_LOOP), 0,
00280        ("ev_assert() macro properly rejects false value"),
00281        ("ev_assert() macro accepted false value; error %u asflags 0x%08lx",
00282         err, _ev_asflags));
00283 
00284   /* OK, ev_init() with EVAS_LOOP unset has already been tested; try now with
00285    * EVAS_LOOP set...
00286    */
00287   _ev_asflags = EVAS_LOOP; /* just in case... */
00288   TEST(t_event_log, el_init_loop, "Test that ev_init() returns if an "
00289        "assertion has been thrown",
00290        ((err = _ev_init()) == EV_ERR_ASSERT), 0,
00291        ("ev_init() macro causes return when EVAS_LOOP is set"),
00292        ("ev_init() macro failed to return properly; error %u", err));
00293 
00294   return 0;
00295 }
00296 
00308 static unsigned int
00309 _ev_log(ev_flags_t flag, const char *msg)
00310 {
00311   ev_log(flag, msg); /* do logging... */
00312 
00313   return log_called; /* convenience return... */
00314 }
00315 
00325 static ev_err_t
00326 _ev_assert(int expr)
00327 {
00328   ev_assert(expr); /* do the assertion... */
00329 
00330   return 0;
00331 }
00332 
00340 static ev_err_t
00341 _ev_abort(void)
00342 {
00343   ev_abort(); /* do the abort... */
00344 
00345   return 0;
00346 }
00347 
00354 static ev_err_t
00355 _ev_init(void)
00356 {
00357   ev_init(); /* do the initialize... */
00358 
00359   return 0;
00360 }

Generated on Wed Dec 28 23:36:56 2005 for event by  doxygen 1.4.4