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

t_event_init.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_init.c,v 1.7 2005/12/18 00:15:17 klmitch Exp $
00019 */
00028 #include "event-test.h"
00029 #include "engines_int.h"
00030 
00031 #include <stdio.h>
00032 #include <string.h>
00033 
00034 RCSTAG("@(#)$Id: t_event_init.c,v 1.7 2005/12/18 00:15:17 klmitch Exp $");
00035 
00036 TEST_PROG(t_event_init, "Test operation of event_init() and related functions")
00037      TEST_ARG(t_event_init, "<event-test.tc>")
00038      TEST_ARG(t_event_init, "<engmodinit.la>")
00039      TEST_ARG(t_event_init, "<engmodule.la>")
00040      TEST_DEP(t_event_init, t_event_log)
00041      TEST_DEP(t_event_init, t_event_attrs)
00042 
00060 static ev_err_t
00061 ei_init(ev_ctx_t *ctx, ev_engine_t *engine, ev_flags_t *eng_flags)
00062 {
00063   eng_data_set(engine, (void *)(*eng_flags)); /* save the called flags */
00064   return EINVAL; /* and return an error code */
00065 }
00066 
00082 static ev_err_t
00083 ei_poll(ev_ctx_t *ctx, ev_engine_t *engine, struct timeval *timeout)
00084 {
00085   return 0;
00086 }
00087 
00100 static ev_err_t
00101 ei_sock(ev_ctx_t *ctx, ev_engine_t *engine, ev_sock_t *sock)
00102 {
00103   return 0;
00104 }
00105 
00120 static ev_err_t
00121 ei_sockev(ev_ctx_t *ctx, ev_engine_t *engine, ev_sock_t *sock, ev_flags_t evs)
00122 {
00123   return 0;
00124 }
00125 
00138 static ev_err_t
00139 ei_sig(ev_ctx_t *ctx, ev_engine_t *engine, ev_sig_t *sig)
00140 {
00141   return 0;
00142 }
00143 
00157 static ev_err_t
00158 ei_tim(ev_ctx_t *ctx, ev_engine_t *engine, ev_tim_t *tim)
00159 {
00160   return 0;
00161 }
00162 
00169 static struct teng {
00170   ev_err_t      err_code;       
00172   ev_flags_t    comp_flags;     
00173   ev_engine_t   engine;         
00174 } tengs[] = {
00175   /* Engine with bad socket component. */
00176   { EINVAL, 0, EV_ENGINE_INIT("badsock", "Test bad socket engine rejection",
00177                               EV_ENGINE_SOCKET, ei_init, ei_poll, 0,
00178                               0, 0, 0, 0, 0, 0, 0) },
00179 
00180   /* Engine with bad signal component. */
00181   { EINVAL, 0, EV_ENGINE_INIT("badsig", "Test bad signal engine rejection",
00182                               EV_ENGINE_SIGNAL, ei_init, ei_poll, 0,
00183                               0, 0, 0, 0, 0, 0, 0) },
00184 
00185   /* Engine with bad timer component. */
00186   { 0, 0, EV_ENGINE_INIT("badtim", "Test bad timer engine rejection",
00187                          EV_ENGINE_TIMER, ei_init, ei_poll, 0,
00188                          0, 0, 0, 0, 0, 0, 0) },
00189 
00190   /* Socket-only engine. */
00191   { 0, EV_ENGINE_SOCKET,
00192     EV_ENGINE_INIT("sockonly", "Test socket-only initialization",
00193                    (EV_ENGINE_SOCKET | EV_ENGINE_SIGNAL | EV_ENGINE_TIMER),
00194                    ei_init, ei_poll, 0,
00195                    ei_sock, ei_sock, ei_sockev,
00196                    ei_sig, ei_sig,
00197                    ei_tim, ei_tim) },
00198 
00199   /* Signal-only engine. */
00200   { 0, EV_ENGINE_SIGNAL,
00201     EV_ENGINE_INIT("sigonly", "Test signal-only initialization",
00202                    (EV_ENGINE_SOCKET | EV_ENGINE_SIGNAL | EV_ENGINE_TIMER),
00203                    ei_init, ei_poll, 0,
00204                    ei_sock, ei_sock, ei_sockev,
00205                    ei_sig, ei_sig,
00206                    ei_tim, ei_tim) },
00207 
00208   /* Timer-only engine. */
00209   { 0, EV_ENGINE_TIMER,
00210     EV_ENGINE_INIT("timonly", "Test timer-only initialization",
00211                    (EV_ENGINE_SOCKET | EV_ENGINE_SIGNAL | EV_ENGINE_TIMER),
00212                    ei_init, ei_poll, 0,
00213                    ei_sock, ei_sock, ei_sockev,
00214                    ei_sig, ei_sig,
00215                    ei_tim, ei_tim) },
00216 
00217   /* Default component set engine. */
00218   { 0, (EV_ENGINE_SOCKET | EV_ENGINE_SIGNAL | EV_ENGINE_TIMER),
00219     EV_ENGINE_INIT("allcomps", "Test all-comps initialization",
00220                    (EV_ENGINE_SOCKET | EV_ENGINE_SIGNAL | EV_ENGINE_TIMER),
00221                    ei_init, ei_poll, 0,
00222                    ei_sock, ei_sock, ei_sockev,
00223                    ei_sig, ei_sig,
00224                    ei_tim, ei_tim) }
00225 };
00226 
00233 #define TENG_CNT        (sizeof(tengs) / sizeof(struct teng))
00234 
00255 static int
00256 gen_verify(ev_ctx_t *ctx, unsigned int gentype, ev_magic_t genmagic,
00257            size_t gensize)
00258 {
00259   ev_gendesc_t *gd = &ctx->ec_gens->gs_gens[gentype - ctx->ec_gens->gs_first];
00260 
00261   return gd->gd_type == gentype && gd->gd_magic == genmagic &&
00262     gd->gd_size == gensize && !gd->gd_active.gl_count &&
00263     !gd->gd_active.gl_list && !gd->gd_free.gl_count && !gd->gd_free.gl_list;
00264 }
00265 
00272 int
00273 main(int argc, char **argv)
00274 {
00275   const char *conf;
00276   char *prog, pathbuf[1024], *t1, *t2;
00277   const char *path = pathbuf;
00278   const char *modlist = "sockonly:s sigonly:i timonly:t allcomps "
00279     "engmodule:s engmodinit:it";
00280   ev_err_t err;
00281   ev_ctx_t ctx;
00282   ev_attr_t attrs;
00283   int i, pathlen;
00284 
00285   ev_test_init(t_event_init); /* initialize test program */
00286   ev_prog_name(prog, argv); /* calculate program name... */
00287 
00288   if (argc < 4) { /* must have three arguments: path to conf file,
00289                      path to first module, path to second module */
00290     fprintf(stderr, "Usage: %s <conf> <mod1> <mod2>\n", prog);
00291     exit(1);
00292   }
00293 
00294   conf = argv[1]; /* prepare conf file pointer... */
00295 
00296   if ((t1 = strrchr(t2 = argv[2], '/'))) /* grab first path component */
00297     *t1 = '\0';
00298   else
00299     t2 = "."; /* No path component, must be current directory */
00300   pathlen = sprintf(pathbuf, "%s", t2); /* format first path component */
00301 
00302   if ((t1 = strrchr(t2 = argv[3], '/'))) /* grab second path component */
00303     *t1 = '\0';
00304   else
00305     t2 = "."; /* No path component, must be current directory */
00306   /* if not identical to the first... */
00307   if (strcmp(pathbuf, t2))
00308     sprintf(pathbuf + pathlen, ":%s", t2); /* add second path component */
00309 
00310   /* Set t to point to the pathbuf... */
00311   path = pathbuf;
00312 
00313   if ((err = event_attr_init(&attrs)) || /* initialize attributes... */
00314       (err = event_attr_confpath(&attrs, &conf)) ||
00315       (err = event_attr_confreg(&attrs, "app.t_event_init.foo", "blah",
00316                                 0, 0, 0)) ||
00317       (err = event_attr_confreg(&attrs, "app.t_event_init.bar", "halb",
00318                                 0, 0, 0)) ||
00319       (err = event_attr_confvar(&attrs, "app.t_event_init.foo", "halb")) ||
00320       (err = event_attr_confvar(&attrs, "event.modules.foo.bar", "bar")) ||
00321       (err = event_attr_libpath(&attrs, &path)) ||
00322       (err = event_attr_modlist(&attrs, &modlist))) {
00323     fprintf(stderr, "%s: Unable to initialize configuration file path: error "
00324             "code %u\n", prog, err);
00325     exit(1);
00326   }
00327 
00328   /* Run through the list of application-specific generators... */
00329   TEST_DECL(t_event_init, ea_engine, "Test that adding engines to the "
00330             "attributes set works")
00331     for (i = 0; i < TENG_CNT; i++)
00332       if ((err = event_attr_engine(&attrs, &tengs[i].engine)) !=
00333           tengs[i].err_code)
00334         FAIL(TEST_NAME(ea_engine), FATAL(0), "Failure adding engine \"%s\" to "
00335              "attributes set; error code %u (expected %u)",
00336              eng_name(&tengs[i].engine), err, tengs[i].err_code);
00337   PASS(TEST_NAME(ea_engine), "Successfully added application-specific "
00338        "engines");
00339 
00340   /* Test that event_init() can be called... */
00341   TEST(t_event_init, event_init, "Test that event_init may be called",
00342        (!(err = event_init(&ctx, &attrs, 0, prog, 0))), FATAL(0),
00343        ("event_init() call succeeded"),
00344        ("event_init() call failed with error %u", err));
00345 
00346   /* Release attribute memory... */
00347   if ((err = event_attr_destroy(&attrs))) {
00348     fprintf(stderr, "%s: Unable to release attribute memory: error code %u\n",
00349             prog, err);
00350     exit(1);
00351   }
00352 
00353   /* Check that the engines are consistent with expected results */
00354   TEST_DECL(t_event_init, ea_enginit, "Test that added engines were "
00355             "initialized properly")
00356     for (i = 0; i < TENG_CNT; i++)
00357       if (tengs[i].comp_flags != (ev_flags_t)eng_data(&tengs[i].engine))
00358         FAIL(TEST_NAME(ea_enginit), FATAL(0), "Failure initializing engine "
00359              "\"%s\"; init called with flags 0x%08lx, expected 0x%08lx",
00360              eng_name(&tengs[i].engine),
00361              (ev_flags_t)eng_data(&tengs[i].engine), tengs[i].comp_flags);
00362   PASS(TEST_NAME(ea_enginit), "All application-specific engines successfully "
00363        "initialized");
00364 
00365   /* Now, test that the generator list is setup properly */
00366   TEST(t_event_init, event_genlist,
00367        "Test that event_init() left generator list in expected state",
00368        (ctx.ec_gens && !ctx.ec_gens->gs_next && !ctx.ec_gens->gs_prev &&
00369         ctx.ec_gens->gs_first == EGT_SOCKET && ctx.ec_gens->gs_count == 3),
00370        FATAL(0),
00371        ("event_init() left generator list in consistent state"),
00372        ("event_init() failed to leave generator list in consistent state"));
00373 
00374   /* And test the actual generator characteristics... */
00375   TEST(t_event_init, event_defgens,
00376        "Test that event_init() set the proper parameters for the default "
00377        "generators",
00378        (gen_verify(&ctx, EGT_SOCKET, EV_SOCK_MAGIC, sizeof(ev_sock_t)) &&
00379         gen_verify(&ctx, EGT_SIGNAL, EV_SIG_MAGIC, sizeof(ev_sig_t)) &&
00380         gen_verify(&ctx, EGT_TIMER, EV_TIM_MAGIC, sizeof(ev_tim_t))), 0,
00381        ("event_init() initialized default generators properly"),
00382        ("event_init() failed to initialize default generators properly"));
00383 
00384   /* Finally, test that event_destroy() may be called */
00385   TEST(t_event_init, event_destroy, "Test that event_destroy() may be "
00386        "successfully called",
00387        (!(err = event_destroy(&ctx)) && !ec_verify(&ctx)), FATAL(0),
00388        ("event_destroy() call succeeded"),
00389        ("event_destroy() failed with error %u", err));
00390 
00391   return 0;
00392 }

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