LAL  7.5.0.1-89842e6
UserInputTest.c
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2010 Reinhard Prix
3 * Copyright (C) 2007 Jolien Creighton, Reinhard Prix
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with with program; see the file COPYING. If not, write to the
17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 * MA 02110-1301 USA
19 */
20 #include <math.h>
21 
22 #include <lal/LALVCSInfo.h>
23 #include <lal/Date.h>
24 #include <lal/LALString.h>
25 
26 #include <lal/UserInput.h>
27 
28 // for test print usage and help page
29 int XLALUserVarPrintUsage ( FILE *file );
30 int XLALUserVarPrintHelp ( FILE *file );
31 
32 typedef struct
33 {
40  CHAR *string2; // will be read from config-file
49  int argEnum;
50  int argFlag;
52 } UserInput_t;
53 
54 /**
55  * some basic consistency checks of the (XLAL) UserInput module, far from exhaustive,
56  * but should be enough to catch big obvious malfunctions
57  */
58 int
59 main(int argc, char *argv[])
60 {
61 
62  const char *argv_in[] = {
63  "progname",
64  "--argNum=1",
65  "--argStr=xyz",
66  "--argBool=true",
67  "-a", "-1",
68  "-A", "7",
69  "-b",
70  "--argEnum=enum2",
71  "--argFlag=flagA,flagC",
72  "@" TEST_DATA_DIR "ConfigFileSample.cfg"
73  };
74 
75  const int my_argc = XLAL_NUM_ELEM( argv_in );
76  char **my_argv = XLALCalloc ( my_argc, sizeof(char*) );
77  for ( int i=0; i < my_argc; i ++ )
78  {
79  my_argv[i] = XLALCalloc ( 1, strlen(argv_in[i])+1);
80  strcpy ( my_argv[i], argv_in[i] );
81  }
82 
83  /* ---------- Register all test user-variables ---------- */
84  UserInput_t XLAL_INIT_DECL(my_uvars);
85  UserInput_t *uvar = &my_uvars;
86  uvar->string2 = XLALStringDuplicate ( "this is the default value");
87  uvar->argEnum = 0;
88  uvar->argFlag = 0;
89 
90  const UserChoices enumData = { { -1, "noenum" }, { 1, "enum1" }, { 2, "enum2" }, { 2, "enumB" }, { 0, "enum0" } };
91  const UserChoices flagData = { { 0, "none" }, { -1, "noflag" }, { 1, "flagA" }, { 2, "flagB" }, { 4, "flagC" }, { 5, "flagAC" } };
92 
93  lalUserVarHelpBrief = "brief description";
95  "This is a longer description of the program. This is a longer description of the program. This is a longer description of the program."
96  "This is a longer description of the program. This is a longer description of the program. This is a longer description of the program."
97  "This is a longer description of the program. This is a longer description of the program. This is a longer description of the program."
98  "\n"
99  "This is a longer description of the program. This is a longer description of the program. This is a longer description of the program."
100  "This is a longer description of the program. This is a longer description of the program. This is a longer description of the program.";
101 
102  XLAL_CHECK ( XLALRegisterUvarMember( argNum, REAL8, 0, REQUIRED, "Testing float argument") == XLAL_SUCCESS, XLAL_EFUNC );
103  XLAL_CHECK ( XLALRegisterUvarMember( argStr, STRING, 0, REQUIRED, "Testing string argument") == XLAL_SUCCESS, XLAL_EFUNC );
104  XLAL_CHECK ( XLALRegisterUvarMember( argBool, BOOLEAN, 0, REQUIRED, "Testing bool argument") == XLAL_SUCCESS, XLAL_EFUNC );
105  XLAL_CHECK ( XLALRegisterUvarMember( argInt, INT4, 'a', REQUIRED, "Testing INT4 argument") == XLAL_SUCCESS, XLAL_EFUNC );
106  XLAL_CHECK ( XLALRegisterUvarMember( argUInt, UINT4, 'A', REQUIRED, "Testing UINT4 argument") == XLAL_SUCCESS, XLAL_EFUNC );
107 
108  lalUserVarHelpOptionSubsection = "More Options";
109  XLAL_CHECK ( XLALRegisterUvarMember( dummy, INT4, 'c', OPTIONAL, "Testing INT4 argument") == XLAL_SUCCESS, XLAL_EFUNC );
110  XLAL_CHECK ( XLALRegisterUvarMember( argB2, BOOLEAN, 'b', REQUIRED, "Testing short-option bool argument") == XLAL_SUCCESS, XLAL_EFUNC );
111  XLAL_CHECK ( XLALRegisterUvarMember( string2, STRING, 0, REQUIRED, "Testing another string argument") == XLAL_SUCCESS, XLAL_EFUNC );
112  XLAL_CHECK ( XLALRegisterUvarMember( epochGPS, EPOCH, 0, REQUIRED, "Testing epoch given as GPS time") == XLAL_SUCCESS, XLAL_EFUNC );
113  XLAL_CHECK ( XLALRegisterUvarMember( epochMJDTT, EPOCH, 0, REQUIRED, "Testing epoch given as MJD(TT) time") == XLAL_SUCCESS, XLAL_EFUNC );
114 
115  lalUserVarHelpOptionSubsection = "Here's Some More Options";
116  XLAL_CHECK ( XLALRegisterUvarMember( longHMS, RAJ, 0, REQUIRED, "Testing RAJ(HMS) argument") == XLAL_SUCCESS, XLAL_EFUNC );
117  XLAL_CHECK ( XLALRegisterUvarMember( longRad, RAJ, 0, REQUIRED, "Testing RAJ(rad) argument") == XLAL_SUCCESS, XLAL_EFUNC );
118  XLAL_CHECK ( XLALRegisterUvarMember( latDMS, DECJ, 0, REQUIRED, "Testing DECJ(DMS) argument") == XLAL_SUCCESS, XLAL_EFUNC );
119  XLAL_CHECK ( XLALRegisterUvarMember( latRad, DECJ, 0, REQUIRED, "Testing DECJ(rad) argument") == XLAL_SUCCESS, XLAL_EFUNC );
120  XLAL_CHECK ( XLALRegisterUvarMember( longInt, INT8, 0, REQUIRED, "Testing INT8 argument") == XLAL_SUCCESS, XLAL_EFUNC );
121 
122  lalUserVarHelpOptionSubsection = "And Hey Look There's More Options Over Here";
123  XLAL_CHECK ( XLALRegisterUvarAuxDataMember( argEnum, UserEnum, &enumData, 0, REQUIRED, "Testing user enumeration") == XLAL_SUCCESS, XLAL_EFUNC );
124  XLAL_CHECK ( XLALRegisterUvarAuxDataMember( argFlag, UserFlag, &flagData, 0, REQUIRED, "Testing user bitflag") == XLAL_SUCCESS, XLAL_EFUNC );
125  XLAL_CHECK ( XLALRegisterUvarMember( long_help, BOOLEAN, 0, NODEFAULT,
126  "This option is here to test the help page wrapping of long strings. "
127  "This option is here to test the help page wrapping of long strings. "
128  "This option is here to test the help page wrapping of long strings. "
129  "This option is here to test the help page wrapping of long strings. "
130  "This option is here to test the help page wrapping of long strings. "
131  "\n"
132  "This~option~is~here~to~test~the~help~page~wrapping~of~long~strings~without~spaces.~"
133  "This~option~is~here~to~test~the~help~page~wrapping~of~long~strings~without~spaces.~"
134  "This~option~is~here~to~test~the~help~page~wrapping~of~long~strings~without~spaces."
135  ) == XLAL_SUCCESS, XLAL_EFUNC );
136 
137  /* ---------- now read all input from commandline and config-file ---------- */
138  BOOLEAN should_exit = 0;
139  if ( argc > 1 ) {
140  /* parse actual command-line arguments: this is only useful for debugging and testing, never used by 'make check' */
141  XLAL_CHECK ( XLALUserVarReadAllInput ( &should_exit, argc, argv, lalVCSInfoList ) == XLAL_SUCCESS, XLAL_EFUNC );
142  if ( should_exit ) {
143  return EXIT_FAILURE;
144  }
145  } else {
146  /* parse constructed command-line arguments: used by 'make check' */
147  XLAL_CHECK ( XLALUserVarReadAllInput ( &should_exit, my_argc, my_argv, lalVCSInfoList ) == XLAL_SUCCESS, XLAL_EFUNC );
148  XLAL_CHECK ( should_exit == 0, XLAL_EFUNC );
149  }
150 
151  /* ---------- test print usage and help page */
152  printf( "=== Begin usage string ===\n" );
153  fflush( stdout );
154  XLALUserVarPrintUsage( stdout );
155  printf( "--- End usage string ---\n" );
156  printf( "=== Begin help page ===\n" );
157  fflush( stdout );
158  XLALUserVarPrintHelp( stdout );
159  printf( "--- End help page ---\n" );
160  fflush( stdout );
161 
162  /* ---------- test log-generation */
163  printf( "=== Begin log generation test ===\n" );
164  for ( int i = UVAR_LOGFMT_RAWFORM; i < UVAR_LOGFMT_LAST; ++i ) {
165  CHAR *logstr;
166  XLAL_CHECK ( ( logstr = XLALUserVarGetLog ( i )) != NULL, XLAL_EFUNC );
167  printf( "Log format #%i: >>>%s<<<\n", i, logstr );
168  XLALFree ( logstr );
169  }
170  printf( "--- End log generation test ---\n" );
171 
172  /* ---------- test values were read in correctly ---------- */
173  XLAL_CHECK ( uvar->argNum == 1, XLAL_EFAILED, "Failed to read in argNum\n" );
174  XLAL_CHECK ( strcmp ( uvar->argStr, "xyz" ) == 0, XLAL_EFAILED, "Failed to read in argStr\n" );
175  XLAL_CHECK ( uvar->argBool, XLAL_EFAILED, "Failed to read in argBool\n" );
176  XLAL_CHECK ( uvar->argInt == -1, XLAL_EFAILED, "Failed to read in argInt\n" );
177  XLAL_CHECK ( uvar->argUInt == 7, XLAL_EFAILED, "Failed to read in argUInt\n" );
178  XLAL_CHECK ( uvar->argB2, XLAL_EFAILED, "Failed to read in argB2\n" );
179  XLAL_CHECK ( strcmp ( uvar->string2, "this is also possible, and # here does nothing; and neither does semi-colon " ) == 0, XLAL_EFAILED, "Failed to read in string2\n" );
180  XLAL_CHECK ( uvar->argEnum == 2, XLAL_EFAILED, "Failed to read in argEnum\n" );
181  XLAL_CHECK ( uvar->argFlag == 5, XLAL_EFAILED, "Failed to read in argFlag\n" );
182 
183  char buf1[256], buf2[256];
184  XLAL_CHECK ( XLALGPSCmp ( &uvar->epochGPS, &uvar->epochMJDTT ) == 0, XLAL_EFAILED, "GPS epoch %s differs from MJD(TT) epoch %s\n",
185  XLALGPSToStr ( buf1, &uvar->epochGPS), XLALGPSToStr ( buf2, &uvar->epochMJDTT ) );
186 
187  REAL8 diff, tol = 3e-15;
188  XLAL_CHECK ( (diff = fabs(uvar->longHMS - uvar->longRad)) < tol, XLAL_EFAILED, "longitude(HMS) = %.16g differs from longitude(rad) = %.16g by %g > %g\n", uvar->longHMS, uvar->longRad, diff, tol );
189  XLAL_CHECK ( (diff = fabs(uvar->latDMS - uvar->latRad)) < tol, XLAL_EFAILED, "latitude(HMS) = %.16g differs from latitude(rad) = %.16g by %g > %g\n", uvar->latDMS, uvar->latRad, diff, tol );
190 
191  XLAL_CHECK ( uvar->longInt == 4294967294, XLAL_EFAILED, "Failed to read an INT8: longInt = %" LAL_INT8_FORMAT " != 4294967294", uvar->longInt );
192 
193  /* ----- cleanup ---------- */
195  for ( int i=0; i < my_argc; i ++ ) {
196  XLALFree ( my_argv[i] );
197  }
198  XLALFree ( my_argv );
199 
201 
202  return XLAL_SUCCESS;
203 
204 } // main()
void LALCheckMemoryLeaks(void)
Definition: LALMalloc.c:784
const LALVCSInfoList lalVCSInfoList
NULL-terminated list of VCS and build information for LAL and its dependencies
#define STRING(a)
Definition: PrintVector.c:12
int main(int argc, char *argv[])
some basic consistency checks of the (XLAL) UserInput module, far from exhaustive,...
Definition: UserInputTest.c:59
int XLALUserVarPrintHelp(FILE *file)
Print help page.
Definition: UserInput.c:875
int XLALUserVarPrintUsage(FILE *file)
Print a one-line usage string.
Definition: UserInput.c:729
char * XLALGPSToStr(char *, const LIGOTimeGPS *t)
Return a string containing the ASCII base 10 representation of a LIGOTimeGPS.
Definition: StrToGPS.c:274
unsigned char BOOLEAN
Boolean logical type, see Headers LAL(Atomic)Datatypes.h for more details.
#define XLAL_NUM_ELEM(x)
MACRO which gives the number of elements in a fixed-size array.
double REAL8
Double precision real floating-point number (8 bytes).
#define XLAL_INIT_DECL(var,...)
C99 MACRO to declare and zero-initialize a variable, use as "type XLAL_INIT_DECL(var);".
int64_t INT8
Eight-byte signed integer; on some platforms this is equivalent to long int instead.
char CHAR
One-byte signed integer, see Headers LAL(Atomic)Datatypes.h for more details.
uint32_t UINT4
Four-byte unsigned integer.
int32_t INT4
Four-byte signed integer.
#define XLALCalloc(m, n)
Definition: LALMalloc.h:45
#define XLALFree(p)
Definition: LALMalloc.h:47
#define LAL_INT8_FORMAT
Definition: LALStdio.h:128
char * XLALStringDuplicate(const char *s)
Like strdup but uses LAL allocation routines (free with LALFree).
Definition: LALString.c:89
const char * lalUserVarHelpOptionSubsection
An optional subsection heading under OPTIONS, under which all subsequently-defined user variables are...
Definition: UserInput.c:258
const char * lalUserVarHelpBrief
An optional brief description of the program, printed after its name as part of the help page.
Definition: UserInput.c:248
CHAR * XLALUserVarGetLog(UserVarLogFormat format)
Return a log-string representing the complete user-input.
Definition: UserInput.c:1228
int XLALUserVarReadAllInput(BOOLEAN *should_exit, int argc, char *argv[], const LALVCSInfoList vcs_list)
Put all the pieces together, and basically does everything: print help (if requested),...
Definition: UserInput.c:1064
void XLALDestroyUserVars(void)
Free all memory associated with user-variable linked list.
Definition: UserInput.c:349
#define XLALRegisterUvarMember(name, type, option, category,...)
Shortcut macro for registering new user variables, which are accessed via the struct-pointer '*uvar'.
Definition: UserInput.h:157
const char * lalUserVarHelpDescription
An optional longer description of the program, printed in its own section as part of the help page.
Definition: UserInput.c:253
#define XLALRegisterUvarAuxDataMember(name, type, cdata, option, category,...)
Shortcut macro for registering new user variables, which are accessed via the struct-pointer '*uvar',...
Definition: UserInput.h:164
@ UVAR_LOGFMT_RAWFORM
return UserVars in a raw format suitable for further parsing
Definition: UserInput.h:202
@ UVAR_LOGFMT_LAST
Definition: UserInput.h:206
#define XLAL_CHECK(assertion,...)
Macro to test an assertion and invoke a failure if it is not true in a function that returns an integ...
Definition: XLALError.h:810
@ XLAL_SUCCESS
Success return value (not an error number)
Definition: XLALError.h:401
@ XLAL_EFUNC
Internal function call failed bit: "or" this with existing error number.
Definition: XLALError.h:462
@ XLAL_EFAILED
Generic failure.
Definition: XLALError.h:418
int XLALGPSCmp(const LIGOTimeGPS *t0, const LIGOTimeGPS *t1)
Compares two GPS times.
Definition: XLALTime.c:174
Epoch relative to GPS epoch, see LIGOTimeGPS type for more details.
Definition: LALDatatypes.h:458
Possible choices the user may select for an enumeration or bitflag.
BOOLEAN argB2
Definition: UserInputTest.c:39
BOOLEAN long_help
Definition: UserInputTest.c:51
BOOLEAN argBool
Definition: UserInputTest.c:36
CHAR * string2
Definition: UserInputTest.c:40
LIGOTimeGPS epochGPS
Definition: UserInputTest.c:42
LIGOTimeGPS epochMJDTT
Definition: UserInputTest.c:43
CHAR * argStr
Definition: UserInputTest.c:35