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

t_tc_load.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_tc_load.c,v 1.6 2005/06/08 04:38:39 klmitch Exp $
00019 */
00027 #include "test-harness.h"
00028 
00029 #include <errno.h>
00030 #include <stdio.h>
00031 #include <stdlib.h>
00032 #include <string.h>
00033 
00034 #include "treeconf_int.h"
00035 
00036 RCSTAG("@(#)$Id: t_tc_load.c,v 1.6 2005/06/08 04:38:39 klmitch Exp $");
00037 
00038 TEST_PROG(t_tc_load, "Test tc_load() function")
00039 TEST_DEP(t_tc_load, t_treeconf)
00040 TEST_ARG(t_tc_load, "<t_tc_load.conf>")
00041 TEST_ARG(t_tc_load, "<t_tc_load.res>")
00042 
00049 #define RES_CHUNK 10
00050 
00057 #define RES_BUFSIZE     1024
00058 
00065 static const char *prog;
00066 
00067 /* load the results file and register the variables it lists */
00080 static char **
00081 load_results(treeconf_ctx_t *ctx, const char *file)
00082 {
00083   char **results = 0, buf[RES_BUFSIZE], *var, *def, *val = 0, *nl;
00084   unsigned int err;
00085   int pos = 0, size = 0;
00086   FILE *fp;
00087 
00088   /* Open the results file... */
00089   if (!(fp = fopen(file, "r"))) {
00090     perror(file);
00091     exit(1);
00092   }
00093 
00094   while ((var = fgets(buf, sizeof(buf), fp))) {
00095     if (*var == '#' || /* skip comment lines... */
00096       !(nl = strchr(buf, '\n')) || /* look for trailing newline... */
00097       ((def = strchr(var, ':')) && /* find beginning of default value */
00098       !(val = strchr(def + 1, ':')))) /* find beginning of expected value */
00099       continue; /* skip invalid lines... */
00100 
00101     *nl = '\0'; /* clip out newline */
00102 
00103     if (def) { /* if default was specified... */
00104       *(def++) = '\0'; /* terminate variable name... */
00105       *(val++) = '\0'; /* and default value */
00106 
00107       if (pos >= size - 1) { /* need some more space to store pointers... */
00108       if (!(results = (char **)realloc(results, sizeof(char *) *
00109                                (size += RES_CHUNK)))) {
00110         fprintf(stderr, "%s: Out of memory\n", prog);
00111         exit(1);
00112       }
00113       }
00114 
00115       /* now allocate memory for the result string; +2 accounts for = and \0 */
00116       if (!(results[pos] = (char *)malloc(strlen(var) + strlen(val) + 2))) {
00117       fprintf(stderr, "%s: Out of memory\n", prog);
00118       exit(1);
00119       }
00120 
00121       /* next, fill in the string */
00122       sprintf(results[pos++], "%s=%s", var, val);
00123     }
00124 
00125     /* Finally, register the variable */
00126     if ((err = tc_register(ctx, var, def, 0, 0, 0, 0))) {
00127       fprintf(stderr, "%s: tc_register() failed on %s: %u\n", prog, var, err);
00128       exit(1);
00129     }
00130   }
00131 
00132   fclose(fp); /* done with file... */
00133 
00134   if (!results) { /* make sure we actually loaded some results */
00135     fprintf(stderr, "%s: No results in list\n", file);
00136     exit(1);
00137   }
00138 
00139   results[pos] = 0; /* terminate the array */
00140 
00141   return results;
00142 }
00143 
00150 int
00151 main(int argc, char **argv)
00152 {
00153   treeconf_ctx_t ctx = TREECONF_CTX_INIT;
00154   char *conf, *result, **results, *t;
00155   const char *val;
00156   unsigned int err = 0;
00157 
00158   prog = argv[0]; /* remember program name */
00159 
00160   if (argc < 3) { /* must have two arguments: conf and result */
00161     fprintf(stderr, "Usage: %s <conf> <res>\n", prog);
00162     exit(1);
00163   }
00164 
00165   conf = argv[1];
00166   result = argv[2];
00167 
00168   results = load_results(&ctx, result); /* load results and register vars */
00169 
00170   /* OK, now attempt to load the configuration file */
00171   TEST(t_tc_load, tc_load_call, "Test that tc_load() may be called",
00172        (!(err = tc_load(conf, &ctx))), FATAL(0),
00173        ("tc_load() successfully loaded configuration \"%s\"", conf),
00174        ("tc_load() call failed with error %u", err));
00175 
00176   TEST_DECL(t_tc_load, tc_load_verify, "Test that tc_load() loaded expected "
00177           "values")
00178   /* Now check that the load attempt actually worked */
00179   for (; *results; results++) { /* walk through the results */
00180     if (!(t = strchr(*results, '='))) /* splitting them at the '='... */
00181       continue; /* shouldn't ever happen, but just go on if it does */
00182     *(t++) = '\0'; /* split variable name from expected value */
00183 
00184     /* get the variable's value... */
00185     if ((err = tc_get(&ctx, *results, &val, 0)))
00186       FAIL(TEST_NAME(tc_load_verify), FATAL(0), "tc_get() on variable \"%s\" "
00187          "failed with error %u", *results, err);
00188     else if (strcmp(t, val))
00189       FAIL(TEST_NAME(tc_load_verify), FATAL(0), "Value mismatch on variable "
00190          "\"%s\": expected \"%s\", got \"%s\"", *results, t, val);
00191   }
00192   PASS(TEST_NAME(tc_load_verify), "tc_load() successfully loaded all "
00193        "variables");
00194 
00195   return 0;
00196 }

Generated on Wed Jun 8 09:18:27 2005 for treeconf by  doxygen 1.3.9.1