LALHelloTest.c

Go to the documentation of this file.
00001 /*
00002 *  Copyright (C) 2007 Jolien Creighton
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 with program; see the file COPYING. If not, write to the
00016 *  Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00017 *  MA  02111-1307  USA
00018 */
00019 
00020 /*************** <lalVerbatim file="LALHelloTestCV"> **************
00021 $Id: LALHelloTest.c,v 1.12 2007/06/08 14:41:46 bema Exp $
00022 **************** </lalVerbatim> ***********************************/
00023 
00024 /* <lalLaTeX>
00025 
00026 \subsection{Program \texttt{LALHelloTest.c}}
00027 \label{ss:LALHelloTest.c}
00028 
00029 Tests the routine in \verb@LALHello.h@.  Exercises some of the error
00030 conditions and makes sure that they work.
00031 
00032 \subsubsection*{Usage}
00033 \begin{verbatim}
00034 LALHelloTest [options]
00035 Options:
00036   -h         print help
00037   -q         quiet: run silently
00038   -v         verbose: print extra information
00039   -d level   set lalDebugLevel to level
00040 \end{verbatim}
00041 
00042 \subsubsection*{Description}
00043 \subsubsection*{Exit codes}
00044 \begin{tabular}{|c|l|}
00045 \hline
00046  Code & Explanation                   \\
00047 \hline
00048 \tt 0 & Success, normal exit.         \\
00049 \tt 1 & Subroutine failed.            \\
00050 \hline
00051 \end{tabular}
00052 
00053 \subsubsection*{Uses}
00054 \begin{verbatim}
00055 lalDebugLevel
00056 LALHello()
00057 \end{verbatim}
00058 
00059 \subsubsection*{Notes}
00060 
00061 \vfill{\footnotesize\input{LALHelloTestCV}}
00062 
00063 </lalLaTeX> */
00064 
00065 
00066 #include <stdio.h>
00067 #include <stdlib.h>
00068 #include <string.h>
00069 #include <lal/LALConfig.h>
00070 
00071 #ifdef HAVE_UNISTD_H
00072 #include <unistd.h>
00073 #endif
00074 
00075 #ifdef HAVE_GETOPT_H
00076 #include <getopt.h>
00077 #endif
00078 
00079 #include <lal/LALStdlib.h>
00080 #include <lal/LALHello.h>
00081 
00082 #define CODES_(x) #x
00083 #define CODES(x) CODES_(x)
00084 
00085 NRCSID( MAIN, "$Id: LALHelloTest.c,v 1.12 2007/06/08 14:41:46 bema Exp $" );
00086 
00087 extern char *optarg;
00088 extern int   optind;
00089 
00090 int lalDebugLevel = 0;
00091 int verbose    = 0;
00092 
00093 static void
00094 Usage( const char *program, int exitflag );
00095 
00096 static void
00097 ParseOptions( int argc, char *argv[] );
00098 
00099 static void
00100 TestStatus( LALStatus *status, const char *expectedCodes, int exitCode );
00101 
00102 static void
00103 ClearStatus( LALStatus *status );
00104 
00105 int
00106 main( int argc, char *argv[] )
00107 {
00108   static LALStatus status;
00109   LALStatus keep;
00110 
00111   ParseOptions( argc, argv );
00112 
00113   LALHello( &status, NULL );
00114   TestStatus( &status, CODES(0), 1 );
00115 
00116   LALHello( &status, "\0" );
00117   TestStatus( &status, CODES(-1), 1 );
00118   /* forget to clear status; keep status so we can deallocate it later */
00119   keep = status;
00120 
00121   LALHello( &status, NULL );
00122   TestStatus( &status, CODES(-2), 1 );
00123 
00124   /* now clear status we kept */
00125   ClearStatus( &keep );
00126 
00127   LALCheckMemoryLeaks();
00128   return 0;
00129 }
00130 
00131 
00132 /*
00133  * TestStatus()
00134  *
00135  * Routine to check that the status code status->statusCode agrees with one of
00136  * the codes specified in the space-delimited string ignored; if not,
00137  * exit to the system with code exitcode.
00138  *
00139  */
00140 static void
00141 TestStatus( LALStatus *status, const char *ignored, int exitcode )
00142 {
00143   char  str[64];
00144   char *tok;
00145 
00146   if ( verbose )
00147   {
00148     REPORTSTATUS( status );
00149   }
00150 
00151   if ( strncpy( str, ignored, sizeof( str ) ) )
00152   {
00153     if ( ( tok = strtok( str, " " ) ) )
00154     {
00155       do
00156       {
00157         if ( status->statusCode == atoi( tok ) )
00158         {
00159           return;
00160         }
00161       }
00162       while ( ( tok = strtok( NULL, " " ) ) );
00163     }
00164     else
00165     {
00166       if ( status->statusCode == atoi( tok ) )
00167       {
00168         return;
00169       }
00170     }
00171   }
00172 
00173   fprintf( stderr, "\nExiting to system with code %d\n", exitcode );
00174   exit( exitcode );
00175 }
00176 
00177 
00178 /*
00179  *
00180  * ClearStatus()
00181  *
00182  * Recursively applies DETATCHSTATUSPTR() to status structure to destroy
00183  * linked list of statuses.
00184  *
00185  */
00186 void
00187 ClearStatus( LALStatus *status )
00188 {
00189   if ( status->statusPtr )
00190   {
00191     ClearStatus( status->statusPtr );
00192     DETATCHSTATUSPTR( status );
00193   }
00194 }
00195 
00196 
00197 /*
00198  * Usage()
00199  *
00200  * Prints a usage message for program program and exits with code exitcode.
00201  *
00202  */
00203 static void
00204 Usage( const char *program, int exitcode )
00205 {
00206   fprintf( stderr, "Usage: %s [options]\n", program );
00207   fprintf( stderr, "Options:\n" );
00208   fprintf( stderr, "  -h         print this message\n" );
00209   fprintf( stderr, "  -q         quiet: run silently\n" );
00210   fprintf( stderr, "  -v         verbose: print extra information\n" );
00211   fprintf( stderr, "  -d level   set lalDebugLevel to level\n" );
00212   exit( exitcode );
00213 }
00214 
00215 
00216 /*
00217  * ParseOptions()
00218  *
00219  * Parses the argc - 1 option strings in argv[].
00220  *
00221  */
00222 static void
00223 ParseOptions( int argc, char *argv[] )
00224 {
00225   while ( 1 )
00226   {
00227     int c = -1;
00228 
00229     c = getopt( argc, argv, "hqvd:" );
00230     if ( c == -1 )
00231     {
00232       break;
00233     }
00234 
00235     switch ( c )
00236     {
00237       case 'd': /* set debug level */
00238         lalDebugLevel = atoi( optarg );
00239         break;
00240 
00241       case 'v': /* verbose */
00242         ++verbose;
00243         break;
00244 
00245       case 'q': /* quiet: run silently (ignore error messages) */
00246         freopen( "/dev/null", "w", stderr );
00247         freopen( "/dev/null", "w", stdout );
00248         break;
00249 
00250       case 'h':
00251         Usage( argv[0], 0 );
00252         break;
00253 
00254       default:
00255         Usage( argv[0], 1 );
00256     }
00257 
00258   }
00259 
00260   if ( optind < argc )
00261   {
00262     Usage( argv[0], 1 );
00263   }
00264 
00265   return;
00266 }

Generated on Tue Oct 7 02:39:59 2008 for LAL by  doxygen 1.5.2