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

t_evg_alloc.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_evg_alloc.c,v 1.3 2005/12/18 00:15:17 klmitch Exp $
00019 */
00027 #include "event-test.h"
00028 
00029 RCSTAG("@(#)$Id: t_evg_alloc.c,v 1.3 2005/12/18 00:15:17 klmitch Exp $");
00030 
00031 TEST_PROG(t_evg_alloc, "Test operation of event_gen_alloc() and "
00032           "event_gen_release()")
00033      TEST_ARG(t_evg_alloc, "<event-test.tc>")
00034      TEST_ARG(t_evg_alloc, "<engmodule.la>")
00035      TEST_DEP(t_evg_alloc, t_evg_register)
00036 
00043 struct t_gen {
00044   ev_genhdr_t           tg_hdr;         
00045   unsigned int          tg_seq;         
00046 };
00047 
00059 #define cnt(array)              (sizeof(array) / sizeof(struct t_gen *))
00060 
00067 int
00068 main(int argc, char **argv)
00069 {
00070   int i, fcnt = 0;
00071   char *prog;
00072   ev_err_t err;
00073   ev_ctx_t ctx;
00074   struct t_gen *alloc[25], *gen;
00075   ev_gendesc_t *gd;
00076 
00077   ev_test_init(t_evg_alloc); /* initialize test program */
00078   ev_prog_name(prog, argv); /* calculate program name... */
00079   ev_lib_init(argc, argv, prog, &ctx); /* initialize event library */
00080 
00081   /* First, let's register our testing generator... */
00082   if ((err = event_gen_register(&ctx, EGT_RESERVED, EGT_RESERVED,
00083                                 sizeof(struct t_gen), 0)) ||
00084       (err = _ev_gen_lookup(&ctx, EGT_RESERVED, &gd))) {
00085     fprintf(stderr, "%s: Unable to register testing generator: "
00086             "error code %u\n", prog, err);
00087     exit(1);
00088   }
00089 
00090   /* allocate some test generators... */
00091   TEST_DECL(t_evg_alloc, evg_alloc, "Test that event_gen_alloc() can allocate "
00092             "generators")
00093     for (i = 0; i < cnt(alloc); i++)
00094       if ((err = event_gen_alloc(&ctx, EGT_RESERVED, &alloc[i])))
00095         FAIL(TEST_NAME(evg_alloc), FATAL(0), "Unable to allocate "
00096              "generator %d; error code %u", i, err);
00097       else
00098         alloc[i]->tg_seq = i;
00099   /* double-check the allocation count... */
00100   if (gd->gd_active.gl_count == cnt(alloc))
00101     PASS(TEST_NAME(evg_alloc), "Successfully allocated event generators");
00102   else
00103     FAIL(TEST_NAME(evg_alloc), FATAL(0), "Inconsistent generator allocation "
00104          "count; was %u, should be %u", gd->gd_active.gl_count,
00105          cnt(alloc));
00106 
00107   /* Now release some test generators back onto the free list */
00108   TEST_DECL(t_evg_alloc, evg_release, "Test that event_gen_release() can "
00109             "release generators")
00110     for (i = 0; i < cnt(alloc); i += 2)
00111       if ((err = event_gen_release(&ctx, (ev_genhdr_t *)alloc[i])))
00112         FAIL(TEST_NAME(evg_release), FATAL(0), "Unable to release "
00113              "generator %d; error code %u", i, err);
00114       else
00115         fcnt++; /* keep track of number we released... */
00116   /* double-check the counts... */
00117   if (gd->gd_active.gl_count == cnt(alloc) - fcnt &&
00118       gd->gd_free.gl_count == fcnt)
00119     PASS(TEST_NAME(evg_release), "Successfully released event generators");
00120   else
00121     FAIL(TEST_NAME(evg_release), FATAL(0), "Inconsistent generator allocation "
00122          "counts; active %u, should be %u; free %u, should be %u",
00123          gd->gd_active.gl_count, cnt(alloc) - fcnt, gd->gd_free.gl_count,
00124          fcnt);
00125 
00126   /* Finally, try resurrecting some of the released generators */
00127   TEST_DECL(t_evg_alloc, evg_realloc, "Test that event_gen_alloc() can "
00128             "resurrect released generators from the free list")
00129     for (i = 0; i < fcnt; i++)
00130       if ((err = event_gen_alloc(&ctx, EGT_RESERVED, &gen)))
00131         FAIL(TEST_NAME(evg_realloc), FATAL(0), "Unable to allocate "
00132              "generator %d; error code %u", i, err);
00133       else if (gen->tg_seq >= cnt(alloc) || gen != alloc[gen->tg_seq])
00134         FAIL(TEST_NAME(evg_realloc), FATAL(0), "Generator allocated is not "
00135              "from the free list; seq is %u but address %p is not %p",
00136              gen->tg_seq, (void *)gen,
00137              (void *)(gen->tg_seq >= cnt(alloc) ? 0 : alloc[gen->tg_seq]));
00138   /* double-check the counts... */
00139   if (gd->gd_active.gl_count == cnt(alloc) && gd->gd_free.gl_count == 0)
00140     PASS(TEST_NAME(evg_realloc), "Successfully reallocated items from the "
00141          "free list");
00142   else
00143     FAIL(TEST_NAME(evg_realloc), FATAL(0), "Inconsistent generator allocation "
00144          "counts; active %u, should be %u; free %u, should be 0",
00145          gd->gd_active.gl_count, cnt(alloc), gd->gd_free.gl_count);
00146 
00147   /* Test the parts of event_destroy() that release the free lists */
00148   TEST(t_evg_alloc, evg_destroy, "Test that event_destroy() releases "
00149        "free-listed generators",
00150        (!(err = event_destroy(&ctx))), 0,
00151        ("event_destroy() successfully released free-listed generators"),
00152        ("event_destroy() call failed; error %u", err));
00153 
00154   return 0;
00155 }

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