1 /*****************************************************************************
2  *                                                                           *
3  *          UNURAN -- Universal Non-Uniform Random number generator          *
4  *                                                                           *
5  *****************************************************************************
6  *                                                                           *
7  *   FILE:      testunuran.h                                                 *
8  *                                                                           *
9  *   Prototypes for common test routines                                     *
10  *                                                                           *
11  *****************************************************************************
12  *                                                                           *
13  *   Copyright (c) 2000-2006 Wolfgang Hoermann and Josef Leydold             *
14  *   Department of Statistics and Mathematics, WU Wien, Austria              *
15  *                                                                           *
16  *   This program is free software; you can redistribute it and/or modify    *
17  *   it under the terms of the GNU General Public License as published by    *
18  *   the Free Software Foundation; either version 2 of the License, or       *
19  *   (at your option) any later version.                                     *
20  *                                                                           *
21  *   This program is distributed in the hope that it will be useful,         *
22  *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
23  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
24  *   GNU General Public License for more details.                            *
25  *                                                                           *
26  *   You should have received a copy of the GNU General Public License       *
27  *   along with this program; if not, write to the                           *
28  *   Free Software Foundation, Inc.,                                         *
29  *   59 Temple Place, Suite 330, Boston, MA 02111-1307, USA                  *
30  *                                                                           *
31  *****************************************************************************/
32 
33 /*---------------------------------------------------------------------------*/
34 
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <time.h>
39 #include <math.h>
40 #include <float.h>
41 
42 #include <config.h>
43 #include <unuran.h>
44 #include <unuran_tests.h>
45 #include "testdistributions/testdistributions.h"
46 
47 /* macros and functions for handling floats */
48 #include <utils/unur_fp_source.h>
49 #include <utils/unur_fp_const_source.h>
50 
51 /*---------------------------------------------------------------------------*/
52 /* define macros for GCC attributes                                          */
53 
54 #ifdef __GNUC__
55 #  define ATTRIBUTE__UNUSED        __attribute__ ((unused))
56 #else
57 #  define ATTRIBUTE__UNUSED
58 #endif
59 
60 /*---------------------------------------------------------------------------*/
61 
62 #define CHI_TEST_INTERVALS 100  /* number of intervals for chi^2 test        */
63 #define CHI_TEST_VERBOSITY 0    /* verbosity level for chi^2 test: 0 | 1 | 2 */
64 
65 /*---------------------------------------------------------------------------*/
66 /* global variables                                                          */
67 
68 /* tresholds */
69 #define PVAL_LIMIT (1.e-3)                  /* treshold for p-value for stat. test */
70 #define DEFAULT_CHI2_FAILURES_TOLERATED (3) /* tolerated number of failed tests    */
71 
72 /*---------------------------------------------------------------------------*/
73 /* True and false                                                            */
74 
75 #ifndef TRUE
76 #define TRUE   (1)
77 #endif
78 
79 #ifndef FALSE
80 #define FALSE  (0)
81 #endif
82 
83 #ifndef M_LN10
84 #define M_LN10  2.302585092994046
85 #endif
86 
87 /*---------------------------------------------------------------------------*/
88 /* stop watch (return milliseconds)                                          */
89 
90 #if defined(HAVE_GETTIMEOFDAY) && defined(HAVE_SYS_TIME_H)
91 /* use gettimeofday() command. Not in ANSI C! */
92 
93 #include <sys/time.h>
94 typedef struct timer {
95   struct timeval tv;
96   double start;
97   double interim;
98   double stop;
99 } TIMER;
100 #define stopwatch_get_time(tv) \
101   ( gettimeofday(&tv, NULL), ((tv).tv_sec * 1.e3 + (tv).tv_usec * 1.e-3) )
102 
103 #else
104 /* use clock() command. ANSI C but less accurate */
105 
106 #include <time.h>
107 typedef struct timer {
108   clock_t tv;
109   double start;
110   double interim;
111   double stop;
112 } TIMER;
113 #define stopwatch_get_time(tv) \
114   ( (1.e3 * clock()) / CLOCKS_PER_SEC )
115 
116 #endif
117 
118 void stopwatch_start(TIMER *t);
119 double stopwatch_lap(TIMER *t);
120 double stopwatch_stop(TIMER *t);
121 
122 void stopwatch_init(void);
123 void stopwatch_print( FILE *LOG, const char *format, double etime );
124 
125 /*---------------------------------------------------------------------------*/
126 /* set alarm when run time exceeds given limit                               */
127 void set_alarm(FILE *LOG);
128 
129 /*---------------------------------------------------------------------------*/
130 /* count number of function evaluations                                      */
131 
132 /* set and start counter for PDF and similar functions in parameter object */
133 int start_counter_fcalls( UNUR_PAR *par );
134 /* IMPORTANT:
135  * This function uses global variables.
136  * Thus the corresponding parameter/generator object has to be DESTROYED
137  * before this routine is called again.
138  * In addition, it creates a clone of the distribution object to which
139  * the parameter objects points to.
140  * Thus one should run
141  *    stop_counter_fcalls()
142  * immediately after the corresponding parameter/generator object has been
143  * destroyed to avoid memory leaks.
144  */
145 
146 int stop_counter_fcalls(void);
147 /* stop counter for PDF calls and clear memory */
148 
149 /* reset counter to 0 */
150 void reset_counter_fcalls(void);
151 
152 /* get number of PDF evaluations */
153 int get_counter_pdf(void);
154 int get_counter_logpdf(void);
155 int get_counter_cdf(void);
156 
157 /*---------------------------------------------------------------------------*/
158 /* print header for test log file                                            */
159 void print_test_log_header( FILE *LOG, unsigned long seed, int fullcheck );
160 
161 /*---------------------------------------------------------------------------*/
162 /* check for invalid NULL pointer, that should not happen in this program */
163 void abort_if_NULL( FILE *LOG, int line, const void *ptr );
164 
165 /* compare error code */
166 int check_errorcode( FILE *LOG, int line, int cherrno );
167 
168 /* check for expected NULL pointer */
169 /* int do_check_expected_NULL( FILE *LOG, int line, const void *ptr ); */
170 int do_check_expected_NULL( FILE *LOG, int line, int is_NULL );
171 #define check_expected_NULL(LOG,line,ptr) \
172    do_check_expected_NULL((LOG),(line),((ptr)==NULL)?1:0 )
173 
174 /* check for "set failed" */
175 int check_expected_setfailed( FILE *LOG, int line, int rcode );
176 
177 /* check for expected zero (int 0) */
178 int check_expected_zero( FILE *LOG, int line, int k );
179 
180 /* check for INFINITY */
181 int check_expected_INFINITY( FILE *LOG, int line, double x );
182 int check_expected_negINFINITY( FILE *LOG, int line, double x );
183 int check_expected_INTMAX( FILE *LOG, int line, int k );
184 
185 /* check for reinit */
186 int check_expected_reinit( FILE *LOG, int line, int rcode );
187 
188 /* check for non existing reinit */
189 int check_expected_no_reinit( FILE *LOG, int line, int rcode );
190 
191 /* compare sequences generated by generator */
192 int compare_sequence_gen_start ( FILE *LOG, int line, UNUR_GEN *gen, int sample_size );
193 int compare_sequence_gen       ( FILE *LOG, int line, UNUR_GEN *gen, int sample_size );
194 int compare_sequence_par_start ( FILE *LOG, int line, UNUR_PAR *par, int sample_size );
195 int compare_sequence_par       ( FILE *LOG, int line, UNUR_PAR *par, int sample_size );
196 int compare_sequence_urng_start( FILE *LOG, int line, int sample_size );
197 
198 /* free memory used for comparing sequences */
199 void compare_free_memory( void );
200 
201 /* print name of distribution */
202 void print_distr_name( FILE *LOG, const UNUR_DISTR *distr, const char *genid );
203 
204 /* check p-value of statistical test and print result */
205 int print_pval( FILE *LOG, UNUR_GEN *gen, const UNUR_DISTR *distr, double pval, int trial, int todo );
206 
207 /* run chi2 test */
208 int run_validate_chi2( FILE *LOG, int line, UNUR_GEN *gen, const UNUR_DISTR *distr, int todo );
209 
210 /* run verify hat test */
211 int run_validate_verifyhat( FILE *LOG, int line, UNUR_GEN *gen, const UNUR_DISTR *distr, int todo );
212 
213 /* print result of verify hat test */
214 int print_verifyhat_result( FILE *LOG, UNUR_GEN *gen, const UNUR_DISTR *distr, int failed, int todo );
215 
216 /* print result of timings */
217 void print_timing_results( FILE *LOG, int line, const UNUR_DISTR *distr,
218 			   double *timing_setup, double *timing_marginal, int n_results );
219 
220 /* run test for u-error of inversion method and print results */
221 int run_validate_u_error( FILE *LOG, UNUR_GEN *gen, const UNUR_DISTR *distr,
222 			  double u_resolution, int samplesize );
223 
224 /*---------------------------------------------------------------------------*/
225