Loading [MathJax]/extensions/TeX/AMSsymbols.js
LAL 7.7.0.1-5e288d3
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
RandomTest.c
Go to the documentation of this file.
1/*
2* Copyright (C) 2007 Jolien Creighton
3*
4* This program is free software; you can redistribute it and/or modify
5* it under the terms of the GNU General Public License as published by
6* the Free Software Foundation; either version 2 of the License, or
7* (at your option) any later version.
8*
9* This program is distributed in the hope that it will be useful,
10* but WITHOUT ANY WARRANTY; without even the implied warranty of
11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12* GNU General Public License for more details.
13*
14* You should have received a copy of the GNU General Public License
15* along with with program; see the file COPYING. If not, write to the
16* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17* MA 02110-1301 USA
18*/
19#include <config.h>
20
21#include <stdio.h>
22#include <string.h>
23#include <stdlib.h>
24
25#include <lal/LALStdlib.h>
26#include <lal/LALgetopt.h>
27#include <lal/AVFactories.h>
28#include <lal/Random.h>
29#include <lal/LALString.h>
30
31/**
32 * \file
33 * \ingroup Random_h
34 *
35 * \brief Tests the routines in \ref Random.h. Outputs random numbers to a file.
36 *
37 * ### Usage ###
38 *
39 * \code
40 * RandomTest [options]
41 * Options:
42 * -h print this message
43 * -q quiet: run silently
44 * -v verbose: print extra information
45 * -d level set lalDebugLevel to level
46 * -o output random numbers to files
47 * \endcode
48 *
49 * ### Exit codes ###
50 *
51 * <table>
52 * <tr><th>Code</th><th>Explanation</th></tr>
53 * <tr><td>0</td><td>Success, normal exit.</td></tr>
54 * <tr><td>1</td><td>Subroutine failed.</td></tr>
55 * </table>
56 *
57 */
58/** \cond DONT_DOXYGEN */
59
60#define CODES_(x) #x
61#define CODES(x) CODES_(x)
62
63int output = 0;
64int verbose = 0;
65
66static void
67Usage (const char *program, int exitflag);
68
69static void
70ParseOptions (int argc, char *argv[]);
71
72static void
73TestStatus (LALStatus *status, const char *expectedCodes, int exitCode);
74
75int
76main (int argc, char *argv[])
77{
78 const INT4 numPoints = 999;
79 static LALStatus status;
80 static REAL4Vector *vector;
81 static RandomParams *randpar;
82 UINT4 i;
83
84
85 /*
86 *
87 * Parse the command line options.
88 *
89 */
90
91
92 ParseOptions (argc, argv);
93
94
95 /*
96 *
97 * Allocate memory.
98 *
99 */
100
101
102 if (verbose)
103 {
104 printf ("\n===== Allocate Memory =====\n");
105 }
106
107 LALCreateVector (&status, &vector, numPoints);
108 TestStatus (&status, CODES(0), 1);
109
110
111 /*
112 *
113 * Create random number parameters with seed drawn from clock.
114 *
115 */
116
117
118 if (verbose)
119 {
120 printf ("\n===== Test Random Routines =====\n");
121 }
122
123 randpar = XLALCreateRandomParams (0);
124 if (!randpar)
125 exit (1);
126
127
128 /*
129 *
130 * Fill vector with uniform random numbers.
131 *
132 */
133
134
135 for (i = 0; i < vector->length; ++i)
136 {
137 vector->data[i] = XLALUniformDeviate (randpar);
138 if (XLALIsREAL4FailNaN(vector->data[i]))
139 {
140 exit (1);
141 }
142 }
143
144 if (output)
145 {
146 FILE *fp = fopen ("PrintVector.000", "w");
147 for (i = 0; i < vector->length; ++i)
148 {
149 fprintf (fp, "%e\n", vector->data[i]);
150 }
151 fclose (fp);
152 }
153
154
155 /*
156 *
157 * Fill vector with Gaussian random numbers.
158 *
159 */
160
161
162 if (XLALNormalDeviates (vector, randpar))
163 exit (1);
164
165 if (output)
166 {
167 FILE *fp = fopen ("PrintVector.001", "w");
168 for (i = 0; i < vector->length; ++i)
169 {
170 fprintf (fp, "%e\n", vector->data[i]);
171 }
172 fclose (fp);
173 }
174
175
176 /*
177 *
178 * Check to make sure that correct error codes are generated.
179 *
180 */
181
182
183 if (verbose || lalDebugLevel)
184 {
185 printf ("\n===== Check Errors =====\n");
186 }
187
188 /* vector length error */
189
190 if (verbose)
191 {
192 printf ("\n----- Invalid Length Error: Code 4\n");
193 }
194
195 {
196 REAL4Vector tmp;
197 tmp.length = 0;
198 tmp.data = (REAL4 *)1;
199 if (!XLALNormalDeviates (&tmp, randpar))
200 exit (1);
202 }
203
204
205 /*
206 *
207 * Free memory.
208 *
209 */
210
211
212 if (verbose || lalDebugLevel)
213 {
214 printf ("\n===== Clean up and Exit =====\n");
215 }
216
217 XLALDestroyRandomParams (randpar);
218
219 LALDestroyVector (&status, &vector);
220 TestStatus (&status, CODES(0), 1);
221
223 return 0;
224}
225
226
227/*
228 * TestStatus ()
229 *
230 * Routine to check that the status code status->statusCode agrees with one of
231 * the codes specified in the space-delimited string ignored; if not,
232 * exit to the system with code exitcode.
233 *
234 */
235static void
236TestStatus (LALStatus *status, const char *ignored, int exitcode)
237{
238 char str[64];
239 char *tok;
240
241 if (verbose)
242 {
244 }
245
246 if (XLALStringCopy(str, ignored, sizeof(str)))
247 {
248 if ((tok = strtok (str, " ")))
249 {
250 do
251 {
252 if (status->statusCode == atoi (tok))
253 {
254 return;
255 }
256 }
257 while ((tok = strtok (NULL, " ")));
258 }
259 else
260 {
261 if (status->statusCode == atoi (str))
262 {
263 return;
264 }
265 }
266 }
267
268 fprintf (stderr, "\nExiting to system with code %d\n", exitcode);
269 exit (exitcode);
270}
271
272
273/*
274 * Usage ()
275 *
276 * Prints a usage message for program program and exits with code exitcode.
277 *
278 */
279static void
280Usage (const char *program, int exitcode)
281{
282 fprintf (stderr, "Usage: %s [options]\n", program);
283 fprintf (stderr, "Options:\n");
284 fprintf (stderr, " -h print this message\n");
285 fprintf (stderr, " -q quiet: run silently\n");
286 fprintf (stderr, " -v verbose: print extra information\n");
287 fprintf (stderr, " -d level set lalDebugLevel to level\n");
288 fprintf (stderr, " -o output random numbers to files\n");
289 exit (exitcode);
290}
291
292
293/*
294 * ParseOptions ()
295 *
296 * Parses the argc - 1 option strings in argv[].
297 *
298 */
299static void
300ParseOptions (int argc, char *argv[])
301{
302 FILE *fp;
303
304 while (1)
305 {
306 int c = -1;
307
308 c = LALgetopt (argc, argv, "hqvd:""o");
309 if (c == -1)
310 {
311 break;
312 }
313
314 switch (c)
315 {
316 case 'o': /* set output */
317 output = 1;
318 break;
319
320 case 'd': /* set debug level */
321 break;
322
323 case 'v': /* verbose */
324 ++verbose;
325 break;
326
327 case 'q': /* quiet: run silently (ignore error messages) */
328 fp = freopen ("/dev/null", "w", stderr);
329 if (fp == NULL)
330 {
331 fprintf(stderr, "Error: Unable to open /dev/null\n");
332 exit(1);
333 }
334 fp = freopen ("/dev/null", "w", stdout);
335 if (fp == NULL)
336 {
337 fprintf(stderr, "Error: Unable to open /dev/null\n");
338 exit(1);
339 }
340 break;
341
342 case 'h':
343 Usage (argv[0], 0);
344 break;
345
346 default:
347 Usage (argv[0], 1);
348 }
349
350 }
351
352 if (LALoptind < argc)
353 {
354 Usage (argv[0], 1);
355 }
356
357 return;
358}
359
360/** \endcond */
const char * program
void REPORTSTATUS(LALStatus *status)
Definition: LALError.c:322
void LALCheckMemoryLeaks(void)
Definition: LALMalloc.c:784
int LALgetopt(int argc, char *const *argv, const char *optstring)
Definition: LALgetopt.c:172
int LALoptind
Definition: LALgetopt.c:79
#define fprintf
static void Usage(const char *program, int exitcode)
Definition: XLALChisqTest.c:70
static void ParseOptions(int argc, char *argv[])
Definition: XLALChisqTest.c:88
int main(int argc, char *argv[])
Definition: cache.c:25
uint32_t UINT4
Four-byte unsigned integer.
int32_t INT4
Four-byte signed integer.
float REAL4
Single precision real floating-point number (4 bytes).
#define lalDebugLevel
Definition: LALDebugLevel.h:58
size_t XLALStringCopy(char *dst, const char *src, size_t size)
Copy sources string src to destination string dst.
Definition: LALString.c:104
RandomParams * XLALCreateRandomParams(INT4 seed)
Definition: Random.c:102
REAL4 XLALUniformDeviate(RandomParams *params)
Definition: Random.c:146
void XLALDestroyRandomParams(RandomParams *params)
Definition: Random.c:140
int XLALNormalDeviates(REAL4Vector *deviates, RandomParams *params)
Definition: Random.c:171
void LALDestroyVector(LALStatus *, REAL4Vector **)
void LALCreateVector(LALStatus *, REAL4Vector **, UINT4)
int XLALClearErrno(void)
Clears the XLAL error number, returns the old value.
Definition: XLALError.c:363
static _LAL_INLINE_ int XLALIsREAL4FailNaN(REAL4 val)
Tests if a value is an XLAL REAL4 failure NaN.
Definition: XLALError.h:347
LAL status structure, see The LALStatus structure for more details.
Definition: LALDatatypes.h:947
Vector of type REAL4, see DATATYPE-Vector types for more details.
Definition: LALDatatypes.h:145
REAL4 * data
Pointer to the data array.
Definition: LALDatatypes.h:150
UINT4 length
Number of elements in array.
Definition: LALDatatypes.h:149
This structure contains the parameters necessary for generating the current sequence of random number...
Definition: Random.h:86
int verbose
Definition: tconvert.c:103
FILE * fp
Definition: tconvert.c:105
void output(int gps_sec, int output_type)
Definition: tconvert.c:440