1 /*
2 file automatically generated by make_test_files.pl
3 Tue Apr 19 14:01:02 2011
4 */
5
6 /*****************************************************************************
7 * *
8 * UNU.RAN -- Universal Non-Uniform Random number generator *
9 * *
10 *****************************************************************************/
11
12 /**
13 ** Tests for DISTR_MATR
14 **/
15
16 /*---------------------------------------------------------------------------*/
17 #include "testunuran.h"
18
19 #ifdef UNUR_URNG_DEFAULT_RNGSTREAM
20 #include <RngStream.h>
21 #endif
22 /*---------------------------------------------------------------------------*/
23
24 /*---------------------------------------------------------------------------*/
25 /* global variables */
26
27 static FILE *TESTLOG; /* test log file */
28 static FILE *UNURANLOG; /* unuran log file */
29
30 static int test_ok = TRUE; /* all tests ok (boolean) */
31 static int fullcheck = FALSE; /* whether all checks are performed */
32
33 static TIMER watch; /* stop watch */
34
35 /*---------------------------------------------------------------------------*/
36
37 void run_verify_generator( FILE *LOG, int line, UNUR_PAR *par );
38
39 int unur_distr_matr_set_verify( UNUR_PAR *par, int verify);
40
41
42 /*---------------------------------------------------------------------------*/
43
44 void test_new (void);
45 void test_set (void);
46 void test_get (void);
47 void test_chg (void);
48 void test_init (void);
49 void test_reinit (void);
50 void test_sample (void);
51 void test_validate (void);
52 void test_special(void);
53
54 /*---------------------------------------------------------------------------*/
55
56
57
58 /* prototypes */
59
60 #define COMPARE_SAMPLE_SIZE (500)
61 #define VIOLATE_SAMPLE_SIZE (20)
62
63
64
65
66 /*---------------------------------------------------------------------------*/
67
68 #ifndef CHI2_FAILURES_TOLERATED
69 # define CHI2_FAILURES_TOLERATED DEFAULT_CHI2_FAILURES_TOLERATED
70 #endif
71
72 /*---------------------------------------------------------------------------*/
73 /* [verbatim] */
74
75
76
77 /*---------------------------------------------------------------------------*/
78 /* [new] */
79
test_new(void)80 void test_new (void)
81 {
82 int n_tests_failed; /* number of failed tests */
83
84 /* start test */
85 printf("[new "); fflush(stdout);
86 fprintf(TESTLOG,"\n[new]\n");
87
88 /* reset counter */
89 n_tests_failed = 0;
90
91 /* set stop watch */
92 stopwatch_lap(&watch);
93
94 { /* invalid data */
95 UNUR_DISTR *distr = NULL;
96 distr = NULL;
97
98
99 unur_reset_errno();
100 n_tests_failed += (check_expected_NULL(TESTLOG,26,(unur_distr_matr_new(0,3)))==UNUR_SUCCESS)?0:1;
101 n_tests_failed += (check_errorcode(TESTLOG,26,UNUR_ERR_DISTR_SET)==UNUR_SUCCESS)?0:1;
102
103 unur_reset_errno();
104 n_tests_failed += (check_expected_NULL(TESTLOG,29,(unur_distr_matr_new(3,0)))==UNUR_SUCCESS)?0:1;
105 n_tests_failed += (check_errorcode(TESTLOG,29,UNUR_ERR_DISTR_SET)==UNUR_SUCCESS)?0:1;
106 unur_distr_free(distr);
107 }
108
109
110 /* timing */
111 stopwatch_print(TESTLOG,"\n<*>time = %.3f ms\n\n", stopwatch_lap(&watch));
112
113 /* test finished */
114 test_ok &= (n_tests_failed) ? 0 : 1;
115 (n_tests_failed) ? printf(" ==> failed] ") : printf(" ==> ok] ");
116
117 } /* end of test_new() */
118
119 /*---------------------------------------------------------------------------*/
120 /* [get] */
121
test_get(void)122 void test_get (void)
123 {
124 int n_tests_failed; /* number of failed tests */
125
126 /* start test */
127 printf("[get "); fflush(stdout);
128 fprintf(TESTLOG,"\n[get]\n");
129
130 /* reset counter */
131 n_tests_failed = 0;
132
133 /* set stop watch */
134 stopwatch_lap(&watch);
135
136 { /* invalid NULL ptr */
137 UNUR_DISTR *distr = NULL;
138 int n_rows, n_cols;
139 distr = NULL;
140
141
142 unur_reset_errno();
143 n_tests_failed += (check_expected_zero(TESTLOG,45,(unur_distr_matr_get_dim( distr, &n_rows, &n_cols )))==UNUR_SUCCESS)?0:1;
144 n_tests_failed += (check_errorcode(TESTLOG,45,UNUR_ERR_NULL)==UNUR_SUCCESS)?0:1;
145 unur_distr_free(distr);
146 }
147
148 { /* invalid distribution type */
149 UNUR_DISTR *distr = NULL;
150 int n_rows, n_cols;
151 distr = unur_distr_discr_new();
152
153
154 unur_reset_errno();
155 n_tests_failed += (check_expected_zero(TESTLOG,52,(unur_distr_matr_get_dim( distr, &n_rows, &n_cols )))==UNUR_SUCCESS)?0:1;
156 n_tests_failed += (check_errorcode(TESTLOG,52,UNUR_ERR_DISTR_INVALID)==UNUR_SUCCESS)?0:1;
157 unur_distr_free(distr);
158 }
159
160
161 /* timing */
162 stopwatch_print(TESTLOG,"\n<*>time = %.3f ms\n\n", stopwatch_lap(&watch));
163
164 /* test finished */
165 test_ok &= (n_tests_failed) ? 0 : 1;
166 (n_tests_failed) ? printf(" ==> failed] ") : printf(" ==> ok] ");
167
168 } /* end of test_get() */
169
170
171 /*---------------------------------------------------------------------------*/
172 /* run generator in verifying mode */
173
run_verify_generator(FILE * LOG,int line,UNUR_PAR * par)174 void run_verify_generator( FILE *LOG, int line, UNUR_PAR *par )
175 {
176 UNUR_GEN *gen;
177 int i;
178
179 /* switch to verifying mode */
180 unur_distr_matr_set_verify(par,1);
181
182 /* initialize generator */
183 gen = unur_init( par ); abort_if_NULL(LOG, line, gen);
184
185 /* run generator */
186 for (i=0; i<VIOLATE_SAMPLE_SIZE; i++)
187 unur_sample_cont(gen);
188
189 /* destroy generator */
190 unur_free(gen);
191
192 } /* end of run_verify_generator() */
193
unur_distr_matr_set_verify(UNUR_PAR * par ATTRIBUTE__UNUSED,int verify ATTRIBUTE__UNUSED)194 int unur_distr_matr_set_verify(UNUR_PAR *par ATTRIBUTE__UNUSED, int verify ATTRIBUTE__UNUSED) {return 0;}
195
196 /*---------------------------------------------------------------------------*/
197
main(void)198 int main(void)
199 {
200 unsigned long seed;
201 char *str_seed, *str_tail;
202
203 /* start stop watch */
204 stopwatch_init();
205 stopwatch_start(&watch);
206
207 /* open log file for unuran and set output stream for unuran messages */
208 UNURANLOG = fopen( "t_distr_matr_unuran.log","w" );
209 abort_if_NULL( stderr,-1, UNURANLOG );
210 unur_set_stream( UNURANLOG );
211
212 /* open log file for testing */
213 TESTLOG = fopen( "t_distr_matr_test.log","w" );
214 abort_if_NULL( stderr,-1, TESTLOG );
215
216 /* seed for uniform generators */
217
218 /* seed set by environment */
219 str_seed = getenv("SEED");
220
221 if (str_seed != NULL) {
222 seed = strtol(str_seed, &str_tail, 10);
223 if (seed == 0u)
224 seed = 740333;
225 }
226 else {
227 #ifdef SEED
228 seed = SEED;
229 #else
230 seed = 740333;
231 #endif
232 }
233
234 /* seed build-in uniform generators */
235 unur_urng_MRG31k3p_seed(NULL,seed);
236 unur_urng_fish_seed(NULL,seed);
237 unur_urng_mstd_seed(NULL,seed);
238
239 /* seed uniform random number generator */
240 #ifdef UNUR_URNG_UNURAN
241 # ifdef UNUR_URNG_DEFAULT_RNGSTREAM
242 {
243 unsigned long sa[6];
244 int i;
245 for (i=0; i<6; i++) sa[i] = seed;
246 RngStream_SetPackageSeed(sa);
247 }
248 # else
249 if (unur_urng_seed(NULL,seed) != UNUR_SUCCESS) {
250 fprintf(stderr,"WARNING: Seed could not be set at random\n");
251 seed = ~0u;
252 }
253 # endif /* UNUR_URNG_DEFAULT_RNGSTREAM */
254 #endif /* UNUR_URNG_UNURAN */
255
256 /* set default debugging flag */
257 unur_set_default_debug(UNUR_DEBUG_ALL);
258
259 /* detect required check mode */
260 fullcheck = (getenv("UNURANFULLCHECK")==NULL) ? FALSE : TRUE;
261
262 /* write header into log file */
263 print_test_log_header( TESTLOG, seed, fullcheck );
264
265 /* set timer for sending SIGALRM signal */
266 set_alarm(TESTLOG);
267
268 /* start test */
269 printf("distr_matr: ");
270
271 /* run tests */
272 test_new();
273 test_get();
274
275
276 /* test finished */
277 printf("\n"); fflush(stdout);
278
279 /* close log files */
280 fprintf(TESTLOG,"\n====================================================\n\n");
281 if (test_ok)
282 fprintf(TESTLOG,"All tests PASSED.\n");
283 else
284 fprintf(TESTLOG,"Test(s) FAILED.\n");
285
286 /* timing */
287 stopwatch_print(TESTLOG,"\n<*>total time = %.0f ms\n\n", stopwatch_stop(&watch));
288
289 fclose(UNURANLOG);
290 fclose(TESTLOG);
291
292 /* free memory */
293 compare_free_memory();
294 unur_urng_free(unur_get_default_urng());
295 unur_urng_free(unur_get_default_urng_aux());
296
297 /* exit */
298 exit( (test_ok) ? EXIT_SUCCESS : EXIT_FAILURE );
299
300 } /* end of main */
301
302