LALMomentTest.c

Go to the documentation of this file.
00001 /*
00002 *  Copyright (C) 2007 Jolien Creighton, Matt Tibbits
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="LALMomentTestCV">
00021 Author: Tibbits, M. M.
00022 $Id: LALMomentTest.c,v 1.4 2007/06/08 14:41:52 bema Exp $
00023 ********************************* </lalVerbatim> */
00024 
00025 /********************************************************** <lalLaTeX>
00026 \subsection{Program \texttt{LALMomentTest.c}}
00027 \label{s:LALMomentTest.c}
00028 
00029 A program to test \texttt{LALDMoment()}.
00030 - Note only the double precision is tested because both are derived from the same code.
00031 \subsubsection*{Usage}
00032 
00033 \begin{verbatim}
00034 ./LALMomentTest [options]
00035 Options:
00036   -h             print usage message
00037   -q             quiet: run silently
00038   -v             verbose: print extra information
00039   -d level       set lalDebugLevel to level
00040 \end{verbatim}
00041 
00042 This program tests the function
00043 \texttt{LALDMoment()}, which calculates the moment
00044 of a given data set.
00045 
00046 First, it tests that the correct error codes 
00047 are generated for the following error conditions (tests in
00048 \textit{italics} are not performed if \verb+LAL_NEDEBUG+ is set, as
00049 the corresponding checks in the code are made using the ASSERT macro):
00050 \begin{itemize}
00051 \item \textit{null pointer to output structure}
00052 \item \textit{null pointer to input structure}
00053 \item \textit{null pointer to data member of input structure}
00054 \item \textit{null pointer to data member of data member of input structure}
00055 \item \textit{zero length}
00056 \end{itemize}
00057 
00058 It then verifies that the correct moment (value and units) is 
00059 generated for each of the following simple test cases:
00060 \begin{enumerate}
00061 \item data set all same value, find moments 2-5.
00062 \item mixed data set, find moments 2-5.
00063 \item evenly distributed data set, find moments 2-5.
00064 \end{enumerate}
00065 
00066 For each successful test
00067 (both of these valid data and the invalid ones described above), it
00068 prints ``\texttt{PASS}'' to standard output; if a test fails, it
00069 prints ``\texttt{FAIL}''.
00070 
00071 \subsubsection*{Exit codes}
00072 
00073 \subsubsection*{Uses}
00074 
00075 \begin{verbatim}
00076 LALDMoment()
00077 LALSMoment()
00078 \end{verbatim}
00079 
00080 \subsubsection*{Notes}
00081 
00082 \vfill{\footnotesize\input{LALMomentTestCV}}
00083 
00084 ******************************************************* </lalLaTeX> */
00085 
00086 #include <lal/LALStdlib.h>
00087 
00088 #include <math.h>
00089 #include <string.h>
00090 #include <stdio.h>
00091 #include <config.h>
00092 
00093 #ifdef HAVE_UNISTD_H
00094 #include <unistd.h>
00095 #endif
00096 
00097 #ifdef HAVE_GETOPT_H
00098 #include <getopt.h>
00099 #endif
00100 
00101 #include <lal/LALMoment.h>
00102 #include "CheckStatus.h"
00103 
00104 
00105 NRCSID (LALMOMENTTESTC, "$Id: LALMomentTest.c,v 1.4 2007/06/08 14:41:52 bema Exp $");
00106 
00107 
00108 /*  constants  */
00109 #define LALMOMENTTESTC_TRUE     1
00110 #define LALMOMENTTESTC_FALSE    0
00111 
00112 extern char     *optarg;
00113 extern int      optind;
00114 
00115 
00116 /*  Setting Global debug level  */
00117 int     lalDebugLevel   = 0;
00118 
00119 
00120 /*  Setting variables to parse command line flags  */
00121 BOOLEAN optVerbose      = LALMOMENTTESTC_FALSE;
00122 UINT4   optLength       = 0;
00123 
00124 
00125 static void Usage 
00126 (
00127         const char      *program,
00128         int             exitflag
00129 );
00130 
00131 static void ParseOptions
00132 (
00133         int             argc,
00134         char            *argv[]
00135 );
00136 
00137 
00138 /*************** <lalErrTab > */
00139 #define LALMOMENTTESTC_ENOM     0
00140 #define LALMOMENTTESTC_EARG     1
00141 #define LALMOMENTTESTC_ECHK     2
00142 #define LALMOMENTTESTC_EFLS     3
00143 #define LALMOMENTTESTC_EUSE     4
00144 #define LALMOMENTTESTC_ENULL    5
00145 #define LALMOMENTTESTC_EALOC    6
00146 #define LALMOMENTTESTC_MSGENOM  "Nominal exit"
00147 #define LALMOMENTTESTC_MSGEARG  "Error parsing command-line arguments"
00148 #define LALMOMENTTESTC_MSGECHK  "Error checking failed to catch bad data"
00149 #define LALMOMENTTESTC_MSGEFLS  "Incorrect answer for valid data"
00150 #define LALMOMENTTESTC_MSGEUSE  "Bad user-entered data"
00151 #define LALMOMENTTESTC_MSGENULL "Null Pointer."
00152 #define LALMOMENTTESTC_MSGEALOC "Memory Allocation Error"
00153 /***************************** </lalErrTab> */
00154 
00155 
00156 
00157 int main( int argc, char *argv[] )
00158 {
00159 
00160         static  LALStatus       status;
00161 
00162         /* Variable declarations */
00163         REAL8                   data[40];
00164         REAL8                   *result;
00165         REAL8                   *nullResult;
00166         INT4                    length;
00167         INT4                    whichMoment;
00168         INT4                    iterator;
00169         INT4                    code;
00170         REAL8                   testOutput[7];
00171         REAL8Sequence           *sequence;
00172         REAL8Sequence           *nullSequence;
00173 
00174 
00175         const  INT4             constantData[]  =
00176                                 { 2,4,2,4,2,4,2,4,2,4,2,4,2,4,2,4,2,4,2,4,
00177                                 2,4,2,4,2,4,2,4,2,4,2,4,2,4,2,4,2,4,2,4};
00178 
00179         const  INT4             constantData2[] =
00180                                 { 10,20,10,20,10,20,10,20,10,20};
00181 
00182         length          =  40;
00183         nullResult      =  NULL;
00184         nullSequence    =  NULL;
00185         whichMoment     =  3;
00186 
00187         result  =  (REAL8*) LALMalloc(sizeof(REAL8));
00188 
00189         if( !result )
00190         {
00191                 return LALMOMENTTESTC_EALOC;
00192         }
00193 
00194         sequence =  (REAL8Sequence*) LALMalloc(sizeof(REAL8Sequence));
00195 
00196         if( !sequence )
00197         {
00198                 return LALMOMENTTESTC_EALOC;
00199         }
00200 
00201         for ( iterator = 0;  iterator < length;  iterator++ )
00202         {
00203                 data[iterator] = ((REAL8)(20 - iterator));
00204         }
00205 
00206         sequence->length = length;
00207 
00208         ParseOptions( argc, argv );
00209 
00210         printf("\n\nMESG: %s \n",LALMOMENTTESTC);
00211 
00212 #ifndef LAL_NDEBUG
00213   if ( ! lalNoDebug )
00214   {
00215         /* test behavior for null pointer to input structure */
00216         LALDMoment(&status, result, nullSequence, whichMoment);
00217         if ( ( code = CheckStatus(&status, LALMOMENTH_ENULL, LALMOMENTH_MSGENULL,
00218                                         LALMOMENTTESTC_ECHK, LALMOMENTTESTC_MSGECHK)) )
00219         {
00220                 return code;
00221         }
00222         printf("\nPASS: null pointer to input structure results in error:\n");
00223         printf("       \"%s\"\n", LALMOMENTH_MSGENULL);
00224 
00225 
00226         /* test behavior for zero length of input sequnce */
00227         sequence->length = 0;
00228 
00229         LALDMoment(&status, result, sequence, whichMoment);
00230         if ( ( code = CheckStatus(&status, LALMOMENTH_ELNTH, LALMOMENTH_MSGELNTH,
00231                                         LALMOMENTTESTC_ECHK, LALMOMENTTESTC_MSGECHK)) )
00232         {
00233                 return code;
00234         }
00235         printf("\nPASS: null pointer to input structure results in error:\n");
00236         printf("       \"%s\"\n", LALMOMENTH_MSGELNTH);
00237 
00238         /*  Set proper length  */
00239         sequence->length = length;
00240 
00241 
00242         /* test behavior for null pointer to output structure */
00243         LALDMoment(&status, nullResult, sequence, whichMoment);
00244         if ( ( code = CheckStatus(&status, LALMOMENTH_ENULL, LALMOMENTH_MSGENULL,
00245                                         LALMOMENTTESTC_ECHK, LALMOMENTTESTC_MSGECHK)) )
00246         {
00247                 return code;
00248         }
00249         printf("\nPASS: non-null pointer to output structure results in error:\n");
00250         printf("       \"%s\"\n", LALMOMENTH_MSGENULL);
00251   }
00252 #endif
00253 
00254 
00255         /*  Setting the parameter data pointer to equal the local data variable  */
00256         sequence->data = data;
00257 
00258         /*  Setting all data points to 5 for first test  */
00259         for ( iterator = 0;  iterator < length;  iterator++ )
00260         {
00261                 data[iterator] = 5.0;
00262         }
00263 
00264         /**********  First Test  **********/
00265         for(whichMoment = 2; whichMoment < 6; whichMoment++)
00266         {
00267                 LALDMoment(&status, result, sequence, whichMoment);
00268                 testOutput[whichMoment] = *result;
00269 
00270                 if( lalDebugLevel > 0 )
00271                 {
00272                         printf("MESG:  whichMoment := %d\n",whichMoment);
00273                         printf("MESG:  testOutput[%d] := %f\n\n",whichMoment, testOutput[whichMoment]);
00274                 }
00275         }
00276 
00277         /*  Data set to equal distribution from 20 to -20  */
00278         for ( iterator = 0;  iterator < length;  iterator++ )
00279         {
00280                 data[iterator] = ((REAL8)(20 - iterator));
00281         }
00282 
00283         /**********  Second Test  **********/
00284         for(whichMoment = 2; whichMoment < 6; whichMoment++)
00285         {
00286                 LALDMoment(&status, result, sequence, whichMoment);
00287                 testOutput[whichMoment] = *result;
00288 
00289                 if( lalDebugLevel > 0 )
00290                 {
00291                         printf("MESG:  whichMoment := %d\n",whichMoment);
00292                         printf("MESG:  testOutput[%d] := %f\n\n",whichMoment, testOutput[whichMoment]);
00293                 }
00294         }
00295 
00296         /*  Data set to 50% 2's & 50% 4's  */
00297         for ( iterator = 0;  iterator < length;  iterator++ )
00298         {
00299                 data[iterator] = ((REAL8)(constantData[iterator]));
00300         }
00301 
00302         /**********  Third Test  **********/
00303         for(whichMoment = 2; whichMoment < 6; whichMoment++)
00304         {
00305                 LALDMoment(&status, result, sequence, whichMoment);
00306                 testOutput[whichMoment] = *result;
00307 
00308                 if( lalDebugLevel > 0 )
00309                 {
00310                         printf("MESG:  whichMoment := %d\n",whichMoment);
00311                         printf("MESG:  testOutput[%d] := %f\n\n",whichMoment, testOutput[whichMoment]);
00312                 }
00313         }
00314 
00315         /*  Change length  */
00316         sequence->length = length = 10;
00317 
00318         /*  Data set to 50% 2's & 50% 4's  */
00319         for ( iterator = 0;  iterator < length;  iterator++ )
00320         {
00321                 data[iterator] = ((REAL8)(constantData2[iterator]));
00322         }
00323 
00324         /**********  Fourth Test  **********/
00325         for(whichMoment = 2; whichMoment < 6; whichMoment++)
00326         {
00327                 LALDMoment(&status, result, sequence, whichMoment);
00328                 testOutput[whichMoment] = *result;
00329 
00330                 if( lalDebugLevel > 0 )
00331                 {
00332                         printf("MESG:  whichMoment := %d\n",whichMoment);
00333                         printf("MESG:  testOutput[%d] := %f\n\n",whichMoment, testOutput[whichMoment]);
00334                 }
00335         }
00336 
00337         printf("MESG:   More option available from command line.\n");
00338         printf("MESG:   Type LALMomentTest -h for options.\n");
00339 
00340         /* normal exit */
00341         return LALMOMENTTESTC_ENOM;
00342 }
00343 
00344 
00345 /*  CODE LISTED BEYOND THIS POINT WAS TAKEN FROM STOCHASTICCROSSCORRELATIONTEST.C  */
00346 
00347 /*
00348  * Usage ()
00349  *
00350  * Prints a usage message for program program and exits with code exitcode.
00351  *
00352  */
00353 
00354 static void Usage (const char *program, int exitcode)
00355 {
00356   fprintf (stderr, "Usage: %s [options]\n", program);
00357   fprintf (stderr, "Options:\n");
00358   fprintf (stderr, "  -h             print this message\n");
00359   fprintf (stderr, "  -q             quiet: run silently\n");
00360   fprintf (stderr, "  -v             verbose: print extra information\n");
00361   fprintf (stderr, "  -d level       set lalDebugLevel to level\n");
00362   exit (exitcode);
00363 }
00364 
00365 /*
00366  * ParseOptions ()
00367  *
00368  * Parses the argc - 1 option strings in argv[].
00369  *
00370  */
00371 
00372 static void ParseOptions (int argc, char *argv[])
00373 {
00374   while (1)
00375   {
00376     int c = -1;
00377 
00378     c = getopt (argc, argv, "hqvd:");
00379     if (c == -1)
00380     {
00381       break;
00382     }
00383 
00384     switch (c)
00385     {        
00386       case 'd': /* set debug level */
00387         lalDebugLevel = atoi (optarg);
00388         break;
00389 
00390       case 'v': /* optVerbose */
00391         optVerbose = LALMOMENTTESTC_TRUE;
00392         break;
00393 
00394       case 'q': /* quiet: run silently (ignore error messages) */
00395         freopen ("/dev/null", "w", stderr);
00396         freopen ("/dev/null", "w", stdout);
00397         break;
00398 
00399       case 'h':
00400         Usage (argv[0], 0);
00401         break;
00402 
00403       default:
00404         Usage (argv[0], 1);
00405     }
00406 
00407   }
00408 
00409   if (optind < argc)
00410   {
00411     Usage (argv[0], 1);
00412   }
00413 
00414   return;
00415 }

Generated on Fri Sep 5 03:07:08 2008 for LAL by  doxygen 1.5.2