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_DISCR
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_discr_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 #include<unuran_config.h>
59 
60 /* prototypes */
61 
62 #define COMPARE_SAMPLE_SIZE  (500)
63 #define VIOLATE_SAMPLE_SIZE   (20)
64 
65 double pmf(int k, const UNUR_DISTR *distr);
66 double icdf(int k, const UNUR_DISTR *distr);
67 int invcdf(double x, const UNUR_DISTR *distr);
68 
69 
70 
71 
72 /*---------------------------------------------------------------------------*/
73 
74 #ifndef CHI2_FAILURES_TOLERATED
75 #  define CHI2_FAILURES_TOLERATED DEFAULT_CHI2_FAILURES_TOLERATED
76 #endif
77 
78 /*---------------------------------------------------------------------------*/
79 /* [verbatim] */
80 
81 
82 
pmf(int k ATTRIBUTE__UNUSED,const UNUR_DISTR * distr ATTRIBUTE__UNUSED)83 double pmf(int k ATTRIBUTE__UNUSED, const UNUR_DISTR *distr ATTRIBUTE__UNUSED) { return 1.; }
icdf(int k ATTRIBUTE__UNUSED,const UNUR_DISTR * distr ATTRIBUTE__UNUSED)84 double icdf(int k ATTRIBUTE__UNUSED, const UNUR_DISTR *distr ATTRIBUTE__UNUSED) { return 1.; }
invcdf(double x ATTRIBUTE__UNUSED,const UNUR_DISTR * distr ATTRIBUTE__UNUSED)85 int invcdf(double x ATTRIBUTE__UNUSED, const UNUR_DISTR *distr ATTRIBUTE__UNUSED) { return 1; }
86 
87 /*---------------------------------------------------------------------------*/
88 /* [new] */
89 
test_new(void)90 void test_new (void)
91 {
92         int n_tests_failed;          /* number of failed tests */
93 
94 	/* start test */
95 	printf("[new "); fflush(stdout);
96 	fprintf(TESTLOG,"\n[new]\n");
97 
98 	/* reset counter */
99 	n_tests_failed = 0;
100 
101 	/* set stop watch */
102 	stopwatch_lap(&watch);
103 
104 
105 	/* timing */
106 	stopwatch_print(TESTLOG,"\n<*>time = %.3f ms\n\n", stopwatch_lap(&watch));
107 
108 	/* test finished */
109 	test_ok &= (n_tests_failed) ? 0 : 1;
110 	(n_tests_failed) ? printf(" ==> failed] ") : printf(" ==> ok] ");
111 
112 } /* end of test_new() */
113 
114 /*---------------------------------------------------------------------------*/
115 /* [set] */
116 
test_set(void)117 void test_set (void)
118 {
119         int n_tests_failed;          /* number of failed tests */
120 
121 	/* start test */
122 	printf("[set "); fflush(stdout);
123 	fprintf(TESTLOG,"\n[set]\n");
124 
125 	/* reset counter */
126 	n_tests_failed = 0;
127 
128 	/* set stop watch */
129 	stopwatch_lap(&watch);
130 
131 { /* invalid NULL ptr */
132 UNUR_DISTR *distr = NULL;
133    distr = NULL;
134 
135 
136 unur_reset_errno();
137 n_tests_failed += (check_expected_setfailed(TESTLOG,37,(unur_distr_discr_set_pv( distr, NULL, 0 )))==UNUR_SUCCESS)?0:1;
138 n_tests_failed += (check_errorcode(TESTLOG,37,UNUR_ERR_NULL)==UNUR_SUCCESS)?0:1;
139 
140 unur_reset_errno();
141 n_tests_failed += (check_expected_setfailed(TESTLOG,40,(unur_distr_discr_set_pmf( distr, pmf )))==UNUR_SUCCESS)?0:1;
142 n_tests_failed += (check_errorcode(TESTLOG,40,UNUR_ERR_NULL)==UNUR_SUCCESS)?0:1;
143 
144 unur_reset_errno();
145 n_tests_failed += (check_expected_setfailed(TESTLOG,43,(unur_distr_discr_set_cdf( distr, icdf )))==UNUR_SUCCESS)?0:1;
146 n_tests_failed += (check_errorcode(TESTLOG,43,UNUR_ERR_NULL)==UNUR_SUCCESS)?0:1;
147 
148 unur_reset_errno();
149 n_tests_failed += (check_expected_setfailed(TESTLOG,46,(unur_distr_discr_set_invcdf( distr, invcdf )))==UNUR_SUCCESS)?0:1;
150 n_tests_failed += (check_errorcode(TESTLOG,46,UNUR_ERR_NULL)==UNUR_SUCCESS)?0:1;
151 
152 unur_reset_errno();
153 n_tests_failed += (check_expected_setfailed(TESTLOG,49,(unur_distr_discr_set_pmfstr( distr, "pmf" )))==UNUR_SUCCESS)?0:1;
154 n_tests_failed += (check_errorcode(TESTLOG,49,UNUR_ERR_NULL)==UNUR_SUCCESS)?0:1;
155 
156 unur_reset_errno();
157 n_tests_failed += (check_expected_setfailed(TESTLOG,52,(unur_distr_discr_set_cdfstr( distr, "cdf" )))==UNUR_SUCCESS)?0:1;
158 n_tests_failed += (check_errorcode(TESTLOG,52,UNUR_ERR_NULL)==UNUR_SUCCESS)?0:1;
159 
160 unur_reset_errno();
161 n_tests_failed += (check_expected_setfailed(TESTLOG,55,(unur_distr_discr_set_pmfparams( distr, NULL, 0 )))==UNUR_SUCCESS)?0:1;
162 n_tests_failed += (check_errorcode(TESTLOG,55,UNUR_ERR_NULL)==UNUR_SUCCESS)?0:1;
163 
164 unur_reset_errno();
165 n_tests_failed += (check_expected_setfailed(TESTLOG,58,(unur_distr_discr_set_domain( distr, 0, 10 )))==UNUR_SUCCESS)?0:1;
166 n_tests_failed += (check_errorcode(TESTLOG,58,UNUR_ERR_NULL)==UNUR_SUCCESS)?0:1;
167 
168 unur_reset_errno();
169 n_tests_failed += (check_expected_setfailed(TESTLOG,61,(unur_distr_discr_set_mode( distr, 0 )))==UNUR_SUCCESS)?0:1;
170 n_tests_failed += (check_errorcode(TESTLOG,61,UNUR_ERR_NULL)==UNUR_SUCCESS)?0:1;
171 
172 unur_reset_errno();
173 n_tests_failed += (check_expected_setfailed(TESTLOG,64,(unur_distr_discr_set_pmfsum( distr, 1. )))==UNUR_SUCCESS)?0:1;
174 n_tests_failed += (check_errorcode(TESTLOG,64,UNUR_ERR_NULL)==UNUR_SUCCESS)?0:1;
175 unur_distr_free(distr);
176 }
177 
178 { /* invalid distribution type */
179 UNUR_DISTR *distr = NULL;
180    distr = unur_distr_cont_new();
181 
182 
183 unur_reset_errno();
184 n_tests_failed += (check_expected_setfailed(TESTLOG,71,(unur_distr_discr_set_pv( distr, NULL, 0 )))==UNUR_SUCCESS)?0:1;
185 n_tests_failed += (check_errorcode(TESTLOG,71,UNUR_ERR_DISTR_INVALID)==UNUR_SUCCESS)?0:1;
186 
187 unur_reset_errno();
188 n_tests_failed += (check_expected_setfailed(TESTLOG,74,(unur_distr_discr_set_pmf( distr, pmf )))==UNUR_SUCCESS)?0:1;
189 n_tests_failed += (check_errorcode(TESTLOG,74,UNUR_ERR_DISTR_INVALID)==UNUR_SUCCESS)?0:1;
190 
191 unur_reset_errno();
192 n_tests_failed += (check_expected_setfailed(TESTLOG,77,(unur_distr_discr_set_cdf( distr, icdf )))==UNUR_SUCCESS)?0:1;
193 n_tests_failed += (check_errorcode(TESTLOG,77,UNUR_ERR_DISTR_INVALID)==UNUR_SUCCESS)?0:1;
194 
195 unur_reset_errno();
196 n_tests_failed += (check_expected_setfailed(TESTLOG,80,(unur_distr_discr_set_invcdf( distr, invcdf )))==UNUR_SUCCESS)?0:1;
197 n_tests_failed += (check_errorcode(TESTLOG,80,UNUR_ERR_DISTR_INVALID)==UNUR_SUCCESS)?0:1;
198 
199 unur_reset_errno();
200 n_tests_failed += (check_expected_setfailed(TESTLOG,83,(unur_distr_discr_set_pmfstr( distr, "exp(-k)" )))==UNUR_SUCCESS)?0:1;
201 n_tests_failed += (check_errorcode(TESTLOG,83,UNUR_ERR_DISTR_INVALID)==UNUR_SUCCESS)?0:1;
202 
203 unur_reset_errno();
204 n_tests_failed += (check_expected_setfailed(TESTLOG,86,(unur_distr_discr_set_cdfstr( distr, "exp(-k)" )))==UNUR_SUCCESS)?0:1;
205 n_tests_failed += (check_errorcode(TESTLOG,86,UNUR_ERR_DISTR_INVALID)==UNUR_SUCCESS)?0:1;
206 
207 unur_reset_errno();
208 n_tests_failed += (check_expected_setfailed(TESTLOG,89,(unur_distr_discr_set_pmfparams( distr, NULL, 0 )))==UNUR_SUCCESS)?0:1;
209 n_tests_failed += (check_errorcode(TESTLOG,89,UNUR_ERR_DISTR_INVALID)==UNUR_SUCCESS)?0:1;
210 
211 unur_reset_errno();
212 n_tests_failed += (check_expected_setfailed(TESTLOG,92,(unur_distr_discr_set_domain( distr, 0, 10 )))==UNUR_SUCCESS)?0:1;
213 n_tests_failed += (check_errorcode(TESTLOG,92,UNUR_ERR_DISTR_INVALID)==UNUR_SUCCESS)?0:1;
214 
215 unur_reset_errno();
216 n_tests_failed += (check_expected_setfailed(TESTLOG,95,(unur_distr_discr_set_mode( distr, 0 )))==UNUR_SUCCESS)?0:1;
217 n_tests_failed += (check_errorcode(TESTLOG,95,UNUR_ERR_DISTR_INVALID)==UNUR_SUCCESS)?0:1;
218 
219 unur_reset_errno();
220 n_tests_failed += (check_expected_setfailed(TESTLOG,98,(unur_distr_discr_set_pmfsum( distr, 1. )))==UNUR_SUCCESS)?0:1;
221 n_tests_failed += (check_errorcode(TESTLOG,98,UNUR_ERR_DISTR_INVALID)==UNUR_SUCCESS)?0:1;
222 unur_distr_free(distr);
223 }
224 
225 { /* invalid NULL ptr */
226 UNUR_DISTR *distr = NULL;
227    distr = unur_distr_discr_new();
228 
229 
230 unur_reset_errno();
231 n_tests_failed += (check_expected_setfailed(TESTLOG,105,(unur_distr_discr_set_pmf( distr, NULL )))==UNUR_SUCCESS)?0:1;
232 n_tests_failed += (check_errorcode(TESTLOG,105,UNUR_ERR_NULL)==UNUR_SUCCESS)?0:1;
233 
234 unur_reset_errno();
235 n_tests_failed += (check_expected_setfailed(TESTLOG,108,(unur_distr_discr_set_cdf( distr, NULL )))==UNUR_SUCCESS)?0:1;
236 n_tests_failed += (check_errorcode(TESTLOG,108,UNUR_ERR_NULL)==UNUR_SUCCESS)?0:1;
237 
238 unur_reset_errno();
239 n_tests_failed += (check_expected_setfailed(TESTLOG,111,(unur_distr_discr_set_invcdf( distr, NULL )))==UNUR_SUCCESS)?0:1;
240 n_tests_failed += (check_errorcode(TESTLOG,111,UNUR_ERR_NULL)==UNUR_SUCCESS)?0:1;
241 
242 unur_reset_errno();
243 n_tests_failed += (check_expected_setfailed(TESTLOG,114,(unur_distr_discr_set_pmfstr( distr, NULL )))==UNUR_SUCCESS)?0:1;
244 n_tests_failed += (check_errorcode(TESTLOG,114,UNUR_ERR_NULL)==UNUR_SUCCESS)?0:1;
245 
246 unur_reset_errno();
247 n_tests_failed += (check_expected_setfailed(TESTLOG,117,(unur_distr_discr_set_cdfstr( distr, NULL )))==UNUR_SUCCESS)?0:1;
248 n_tests_failed += (check_errorcode(TESTLOG,117,UNUR_ERR_NULL)==UNUR_SUCCESS)?0:1;
249 
250 unur_reset_errno();
251 n_tests_failed += (check_expected_setfailed(TESTLOG,120,(unur_distr_discr_set_pmfparams( distr, NULL, 1 )))==UNUR_SUCCESS)?0:1;
252 n_tests_failed += (check_errorcode(TESTLOG,120,UNUR_ERR_NULL)==UNUR_SUCCESS)?0:1;
253 unur_distr_free(distr);
254 }
255 
256 { /* invalid parameters */
257 UNUR_DISTR *distr = NULL;
258    double pv[] = {1.,2.,3.};
259    distr = unur_distr_discr_new();
260 
261 
262 unur_reset_errno();
263 n_tests_failed += (check_expected_setfailed(TESTLOG,128,(unur_distr_discr_set_pmfstr( distr, "" )))==UNUR_SUCCESS)?0:1;
264 n_tests_failed += (check_errorcode(TESTLOG,128,UNUR_ERR_DISTR_SET)==UNUR_SUCCESS)?0:1;
265 
266 unur_reset_errno();
267 n_tests_failed += (check_expected_setfailed(TESTLOG,131,(unur_distr_discr_set_pmfstr( distr, "beta(2.)" )))==UNUR_SUCCESS)?0:1;
268 n_tests_failed += (check_errorcode(TESTLOG,131,UNUR_ERR_DISTR_SET)==UNUR_SUCCESS)?0:1;
269 
270 unur_reset_errno();
271 n_tests_failed += (check_expected_setfailed(TESTLOG,134,(unur_distr_discr_set_cdfstr( distr, "" )))==UNUR_SUCCESS)?0:1;
272 n_tests_failed += (check_errorcode(TESTLOG,134,UNUR_ERR_DISTR_SET)==UNUR_SUCCESS)?0:1;
273 
274 unur_reset_errno();
275 n_tests_failed += (check_expected_setfailed(TESTLOG,137,(unur_distr_discr_set_cdfstr( distr, "beta(2.)" )))==UNUR_SUCCESS)?0:1;
276 n_tests_failed += (check_errorcode(TESTLOG,137,UNUR_ERR_DISTR_SET)==UNUR_SUCCESS)?0:1;
277 
278 unur_reset_errno();
279 n_tests_failed += (check_expected_setfailed(TESTLOG,140,(unur_distr_discr_set_pmfparams( distr, pv, -1 )))==UNUR_SUCCESS)?0:1;
280 n_tests_failed += (check_errorcode(TESTLOG,140,UNUR_ERR_DISTR_NPARAMS)==UNUR_SUCCESS)?0:1;
281 
282 unur_reset_errno();
283 n_tests_failed += (check_expected_setfailed(TESTLOG,143,(unur_distr_discr_set_pmfparams( distr, pv, UNUR_DISTR_MAXPARAMS +1 )))==UNUR_SUCCESS)?0:1;
284 n_tests_failed += (check_errorcode(TESTLOG,143,UNUR_ERR_DISTR_NPARAMS)==UNUR_SUCCESS)?0:1;
285 
286 unur_reset_errno();
287 n_tests_failed += (check_expected_setfailed(TESTLOG,146,(unur_distr_discr_set_domain( distr, 10, 1 )))==UNUR_SUCCESS)?0:1;
288 n_tests_failed += (check_errorcode(TESTLOG,146,UNUR_ERR_DISTR_SET)==UNUR_SUCCESS)?0:1;
289 
290 unur_reset_errno();
291 n_tests_failed += (check_expected_setfailed(TESTLOG,149,(unur_distr_discr_set_pmfsum( distr, -1. )))==UNUR_SUCCESS)?0:1;
292 n_tests_failed += (check_errorcode(TESTLOG,149,UNUR_ERR_DISTR_SET)==UNUR_SUCCESS)?0:1;
293 unur_distr_free(distr);
294 }
295 
296 
297 	/* timing */
298 	stopwatch_print(TESTLOG,"\n<*>time = %.3f ms\n\n", stopwatch_lap(&watch));
299 
300 	/* test finished */
301 	test_ok &= (n_tests_failed) ? 0 : 1;
302 	(n_tests_failed) ? printf(" ==> failed] ") : printf(" ==> ok] ");
303 
304 } /* end of test_set() */
305 
306 /*---------------------------------------------------------------------------*/
307 /* [get] */
308 
test_get(void)309 void test_get (void)
310 {
311         int n_tests_failed;          /* number of failed tests */
312 
313 	/* start test */
314 	printf("[get "); fflush(stdout);
315 	fprintf(TESTLOG,"\n[get]\n");
316 
317 	/* reset counter */
318 	n_tests_failed = 0;
319 
320 	/* set stop watch */
321 	stopwatch_lap(&watch);
322 
323 { /* invalid NULL ptr */
324 UNUR_DISTR *distr = NULL;
325    distr = NULL;
326 
327 
328 unur_reset_errno();
329 n_tests_failed += (check_expected_zero(TESTLOG,160,(unur_distr_discr_get_pv( distr, NULL )))==UNUR_SUCCESS)?0:1;
330 n_tests_failed += (check_errorcode(TESTLOG,160,UNUR_ERR_NULL)==UNUR_SUCCESS)?0:1;
331 
332 unur_reset_errno();
333 n_tests_failed += (check_expected_NULL(TESTLOG,163,(unur_distr_discr_get_pmf( distr )))==UNUR_SUCCESS)?0:1;
334 n_tests_failed += (check_errorcode(TESTLOG,163,UNUR_ERR_NULL)==UNUR_SUCCESS)?0:1;
335 
336 unur_reset_errno();
337 n_tests_failed += (check_expected_NULL(TESTLOG,166,(unur_distr_discr_get_cdf( distr )))==UNUR_SUCCESS)?0:1;
338 n_tests_failed += (check_errorcode(TESTLOG,166,UNUR_ERR_NULL)==UNUR_SUCCESS)?0:1;
339 
340 unur_reset_errno();
341 n_tests_failed += (check_expected_NULL(TESTLOG,169,(unur_distr_discr_get_invcdf( distr )))==UNUR_SUCCESS)?0:1;
342 n_tests_failed += (check_errorcode(TESTLOG,169,UNUR_ERR_NULL)==UNUR_SUCCESS)?0:1;
343 
344 unur_reset_errno();
345 n_tests_failed += (check_expected_NULL(TESTLOG,172,(unur_distr_discr_get_pmfstr( distr )))==UNUR_SUCCESS)?0:1;
346 n_tests_failed += (check_errorcode(TESTLOG,172,UNUR_ERR_NULL)==UNUR_SUCCESS)?0:1;
347 
348 unur_reset_errno();
349 n_tests_failed += (check_expected_NULL(TESTLOG,175,(unur_distr_discr_get_cdfstr( distr )))==UNUR_SUCCESS)?0:1;
350 n_tests_failed += (check_errorcode(TESTLOG,175,UNUR_ERR_NULL)==UNUR_SUCCESS)?0:1;
351 
352 unur_reset_errno();
353 n_tests_failed += (check_expected_zero(TESTLOG,178,(unur_distr_discr_get_pmfparams( distr, NULL )))==UNUR_SUCCESS)?0:1;
354 n_tests_failed += (check_errorcode(TESTLOG,178,UNUR_ERR_NULL)==UNUR_SUCCESS)?0:1;
355 
356 unur_reset_errno();
357 n_tests_failed += (check_expected_INFINITY(TESTLOG,181,(unur_distr_discr_get_pmfsum( distr )))==UNUR_SUCCESS)?0:1;
358 n_tests_failed += (check_errorcode(TESTLOG,181,UNUR_ERR_NULL)==UNUR_SUCCESS)?0:1;
359 
360 unur_reset_errno();
361 n_tests_failed += (check_expected_zero(TESTLOG,184,(unur_distr_discr_make_pv( distr )))==UNUR_SUCCESS)?0:1;
362 n_tests_failed += (check_errorcode(TESTLOG,184,UNUR_ERR_NULL)==UNUR_SUCCESS)?0:1;
363 unur_distr_free(distr);
364 }
365 
366 { /* invalid distribution type */
367 UNUR_DISTR *distr = NULL;
368    int ia, ib;
369    distr = unur_distr_cont_new();
370 
371 
372 unur_reset_errno();
373 n_tests_failed += (check_expected_NULL(TESTLOG,192,(unur_distr_discr_get_pmf( distr )))==UNUR_SUCCESS)?0:1;
374 n_tests_failed += (check_errorcode(TESTLOG,192,UNUR_ERR_DISTR_INVALID)==UNUR_SUCCESS)?0:1;
375 
376 unur_reset_errno();
377 n_tests_failed += (check_expected_NULL(TESTLOG,195,(unur_distr_discr_get_cdf( distr )))==UNUR_SUCCESS)?0:1;
378 n_tests_failed += (check_errorcode(TESTLOG,195,UNUR_ERR_DISTR_INVALID)==UNUR_SUCCESS)?0:1;
379 
380 unur_reset_errno();
381 n_tests_failed += (check_expected_NULL(TESTLOG,198,(unur_distr_discr_get_invcdf( distr )))==UNUR_SUCCESS)?0:1;
382 n_tests_failed += (check_errorcode(TESTLOG,198,UNUR_ERR_DISTR_INVALID)==UNUR_SUCCESS)?0:1;
383 
384 unur_reset_errno();
385 n_tests_failed += (check_expected_NULL(TESTLOG,201,(unur_distr_discr_get_pmfstr( distr )))==UNUR_SUCCESS)?0:1;
386 n_tests_failed += (check_errorcode(TESTLOG,201,UNUR_ERR_DISTR_INVALID)==UNUR_SUCCESS)?0:1;
387 
388 unur_reset_errno();
389 n_tests_failed += (check_expected_NULL(TESTLOG,204,(unur_distr_discr_get_cdfstr( distr )))==UNUR_SUCCESS)?0:1;
390 n_tests_failed += (check_errorcode(TESTLOG,204,UNUR_ERR_DISTR_INVALID)==UNUR_SUCCESS)?0:1;
391 
392 unur_reset_errno();
393 n_tests_failed += (check_expected_zero(TESTLOG,207,(unur_distr_discr_get_pv( distr, NULL )))==UNUR_SUCCESS)?0:1;
394 n_tests_failed += (check_errorcode(TESTLOG,207,UNUR_ERR_DISTR_INVALID)==UNUR_SUCCESS)?0:1;
395 
396 unur_reset_errno();
397 n_tests_failed += (check_expected_setfailed(TESTLOG,210,(unur_distr_discr_get_domain( distr, &ia, &ib )))==UNUR_SUCCESS)?0:1;
398 n_tests_failed += (check_errorcode(TESTLOG,210,UNUR_ERR_DISTR_INVALID)==UNUR_SUCCESS)?0:1;
399 
400 unur_reset_errno();
401 n_tests_failed += (check_expected_zero(TESTLOG,213,(unur_distr_discr_get_pmfparams( distr, NULL )))==UNUR_SUCCESS)?0:1;
402 n_tests_failed += (check_errorcode(TESTLOG,213,UNUR_ERR_DISTR_INVALID)==UNUR_SUCCESS)?0:1;
403 
404 unur_reset_errno();
405 n_tests_failed += (check_expected_INFINITY(TESTLOG,216,(unur_distr_discr_get_pmfsum( distr )))==UNUR_SUCCESS)?0:1;
406 n_tests_failed += (check_errorcode(TESTLOG,216,UNUR_ERR_DISTR_INVALID)==UNUR_SUCCESS)?0:1;
407 
408 unur_reset_errno();
409 n_tests_failed += (check_expected_zero(TESTLOG,219,(unur_distr_discr_make_pv( distr )))==UNUR_SUCCESS)?0:1;
410 n_tests_failed += (check_errorcode(TESTLOG,219,UNUR_ERR_DISTR_INVALID)==UNUR_SUCCESS)?0:1;
411 unur_distr_free(distr);
412 }
413 
414 { /* parameters not unknown */
415 UNUR_DISTR *distr = NULL;
416    distr = unur_distr_discr_new();
417 
418 
419 unur_reset_errno();
420 n_tests_failed += (check_expected_NULL(TESTLOG,226,(unur_distr_discr_get_pmfstr( distr )))==UNUR_SUCCESS)?0:1;
421 n_tests_failed += (check_errorcode(TESTLOG,226,UNUR_ERR_NULL)==UNUR_SUCCESS)?0:1;
422 
423 unur_reset_errno();
424 n_tests_failed += (check_expected_NULL(TESTLOG,229,(unur_distr_discr_get_cdfstr( distr )))==UNUR_SUCCESS)?0:1;
425 n_tests_failed += (check_errorcode(TESTLOG,229,UNUR_ERR_NULL)==UNUR_SUCCESS)?0:1;
426 
427 unur_reset_errno();
428 n_tests_failed += (check_expected_INFINITY(TESTLOG,232,(unur_distr_discr_get_pmfsum( distr )))==UNUR_SUCCESS)?0:1;
429 n_tests_failed += (check_errorcode(TESTLOG,232,UNUR_ERR_DISTR_GET)==UNUR_SUCCESS)?0:1;
430 
431 unur_reset_errno();
432 n_tests_failed += (check_expected_INTMAX(TESTLOG,235,(unur_distr_discr_get_mode( distr )))==UNUR_SUCCESS)?0:1;
433 n_tests_failed += (check_errorcode(TESTLOG,235,UNUR_ERR_DISTR_GET)==UNUR_SUCCESS)?0:1;
434 
435 unur_reset_errno();
436 n_tests_failed += (check_expected_zero(TESTLOG,238,(unur_distr_discr_make_pv( distr )))==UNUR_SUCCESS)?0:1;
437 n_tests_failed += (check_errorcode(TESTLOG,238,UNUR_ERR_DISTR_GET)==UNUR_SUCCESS)?0:1;
438 unur_distr_free(distr);
439 }
440 
441 
442 	/* timing */
443 	stopwatch_print(TESTLOG,"\n<*>time = %.3f ms\n\n", stopwatch_lap(&watch));
444 
445 	/* test finished */
446 	test_ok &= (n_tests_failed) ? 0 : 1;
447 	(n_tests_failed) ? printf(" ==> failed] ") : printf(" ==> ok] ");
448 
449 } /* end of test_get() */
450 
451 /*---------------------------------------------------------------------------*/
452 /* [chg] */
453 
test_chg(void)454 void test_chg (void)
455 {
456         int n_tests_failed;          /* number of failed tests */
457 
458 	/* start test */
459 	printf("[chg "); fflush(stdout);
460 	fprintf(TESTLOG,"\n[chg]\n");
461 
462 	/* reset counter */
463 	n_tests_failed = 0;
464 
465 	/* set stop watch */
466 	stopwatch_lap(&watch);
467 
468 { /* invalid NULL ptr */
469 UNUR_DISTR *distr = NULL;
470    distr = NULL;
471 
472 
473 unur_reset_errno();
474 n_tests_failed += (check_expected_setfailed(TESTLOG,249,(unur_distr_discr_upd_mode( distr )))==UNUR_SUCCESS)?0:1;
475 n_tests_failed += (check_errorcode(TESTLOG,249,UNUR_ERR_NULL)==UNUR_SUCCESS)?0:1;
476 
477 unur_reset_errno();
478 n_tests_failed += (check_expected_setfailed(TESTLOG,252,(unur_distr_discr_upd_pmfsum( distr )))==UNUR_SUCCESS)?0:1;
479 n_tests_failed += (check_errorcode(TESTLOG,252,UNUR_ERR_NULL)==UNUR_SUCCESS)?0:1;
480 unur_distr_free(distr);
481 }
482 
483 { /* invalid distribution type */
484 UNUR_DISTR *distr = NULL;
485    distr = unur_distr_cont_new();
486 
487 
488 unur_reset_errno();
489 n_tests_failed += (check_expected_setfailed(TESTLOG,259,(unur_distr_discr_upd_mode( distr )))==UNUR_SUCCESS)?0:1;
490 n_tests_failed += (check_errorcode(TESTLOG,259,UNUR_ERR_DISTR_INVALID)==UNUR_SUCCESS)?0:1;
491 
492 unur_reset_errno();
493 n_tests_failed += (check_expected_setfailed(TESTLOG,262,(unur_distr_discr_upd_pmfsum( distr )))==UNUR_SUCCESS)?0:1;
494 n_tests_failed += (check_errorcode(TESTLOG,262,UNUR_ERR_DISTR_INVALID)==UNUR_SUCCESS)?0:1;
495 unur_distr_free(distr);
496 }
497 
498 { /* missing data */
499 UNUR_DISTR *distr = NULL;
500    distr = unur_distr_discr_new();
501 
502 
503 unur_reset_errno();
504 n_tests_failed += (check_expected_setfailed(TESTLOG,269,(unur_distr_discr_upd_mode( distr )))==UNUR_SUCCESS)?0:1;
505 n_tests_failed += (check_errorcode(TESTLOG,269,UNUR_ERR_DISTR_DATA)==UNUR_SUCCESS)?0:1;
506 
507 unur_reset_errno();
508 n_tests_failed += (check_expected_setfailed(TESTLOG,272,(unur_distr_discr_upd_pmfsum( distr )))==UNUR_SUCCESS)?0:1;
509 n_tests_failed += (check_errorcode(TESTLOG,272,UNUR_ERR_DISTR_DATA)==UNUR_SUCCESS)?0:1;
510 unur_distr_free(distr);
511 }
512 
513 
514 	/* timing */
515 	stopwatch_print(TESTLOG,"\n<*>time = %.3f ms\n\n", stopwatch_lap(&watch));
516 
517 	/* test finished */
518 	test_ok &= (n_tests_failed) ? 0 : 1;
519 	(n_tests_failed) ? printf(" ==> failed] ") : printf(" ==> ok] ");
520 
521 } /* end of test_chg() */
522 
523 /*---------------------------------------------------------------------------*/
524 /* [sample] */
525 
test_sample(void)526 void test_sample (void)
527 {
528         int n_tests_failed;          /* number of failed tests */
529 
530 	/* start test */
531 	printf("[sample "); fflush(stdout);
532 	fprintf(TESTLOG,"\n[sample]\n");
533 
534 	/* reset counter */
535 	n_tests_failed = 0;
536 
537 	/* set stop watch */
538 	stopwatch_lap(&watch);
539 
540 { /* invalid NULL ptr */
541 UNUR_DISTR *distr = NULL;
542    distr = NULL;
543 
544 
545 unur_reset_errno();
546 n_tests_failed += (check_expected_INFINITY(TESTLOG,291,(unur_distr_discr_eval_pmf( 1, distr )))==UNUR_SUCCESS)?0:1;
547 n_tests_failed += (check_errorcode(TESTLOG,291,UNUR_ERR_NULL)==UNUR_SUCCESS)?0:1;
548 
549 unur_reset_errno();
550 n_tests_failed += (check_expected_INFINITY(TESTLOG,294,(unur_distr_discr_eval_cdf( 1, distr )))==UNUR_SUCCESS)?0:1;
551 n_tests_failed += (check_errorcode(TESTLOG,294,UNUR_ERR_NULL)==UNUR_SUCCESS)?0:1;
552 
553 unur_reset_errno();
554 n_tests_failed += (check_expected_INTMAX(TESTLOG,297,(unur_distr_discr_eval_invcdf( 0.5, distr )))==UNUR_SUCCESS)?0:1;
555 n_tests_failed += (check_errorcode(TESTLOG,297,UNUR_ERR_NULL)==UNUR_SUCCESS)?0:1;
556 unur_distr_free(distr);
557 }
558 
559 { /* invalid distribution object */
560 UNUR_DISTR *distr = NULL;
561    distr = unur_distr_cont_new();
562 
563 
564 unur_reset_errno();
565 n_tests_failed += (check_expected_INFINITY(TESTLOG,305,(unur_distr_discr_eval_pmf( 1, distr )))==UNUR_SUCCESS)?0:1;
566 n_tests_failed += (check_errorcode(TESTLOG,305,UNUR_ERR_DISTR_INVALID)==UNUR_SUCCESS)?0:1;
567 
568 unur_reset_errno();
569 n_tests_failed += (check_expected_INFINITY(TESTLOG,308,(unur_distr_discr_eval_cdf( 1, distr )))==UNUR_SUCCESS)?0:1;
570 n_tests_failed += (check_errorcode(TESTLOG,308,UNUR_ERR_DISTR_INVALID)==UNUR_SUCCESS)?0:1;
571 
572 unur_reset_errno();
573 n_tests_failed += (check_expected_INTMAX(TESTLOG,311,(unur_distr_discr_eval_invcdf( 0.5, distr )))==UNUR_SUCCESS)?0:1;
574 n_tests_failed += (check_errorcode(TESTLOG,311,UNUR_ERR_DISTR_INVALID)==UNUR_SUCCESS)?0:1;
575 unur_distr_free(distr);
576 }
577 
578 { /* missing data */
579 UNUR_DISTR *distr = NULL;
580    distr = unur_distr_discr_new();
581 
582 
583 unur_reset_errno();
584 n_tests_failed += (check_expected_INFINITY(TESTLOG,319,(unur_distr_discr_eval_pmf( 1, distr )))==UNUR_SUCCESS)?0:1;
585 n_tests_failed += (check_errorcode(TESTLOG,319,UNUR_ERR_DISTR_DATA)==UNUR_SUCCESS)?0:1;
586 
587 unur_reset_errno();
588 n_tests_failed += (check_expected_INFINITY(TESTLOG,322,(unur_distr_discr_eval_cdf( 1, distr )))==UNUR_SUCCESS)?0:1;
589 n_tests_failed += (check_errorcode(TESTLOG,322,UNUR_ERR_DISTR_DATA)==UNUR_SUCCESS)?0:1;
590 
591 unur_reset_errno();
592 n_tests_failed += (check_expected_INTMAX(TESTLOG,325,(unur_distr_discr_eval_invcdf( 0.5, distr )))==UNUR_SUCCESS)?0:1;
593 n_tests_failed += (check_errorcode(TESTLOG,325,UNUR_ERR_DISTR_DATA)==UNUR_SUCCESS)?0:1;
594 unur_distr_free(distr);
595 }
596 
597 
598 	/* timing */
599 	stopwatch_print(TESTLOG,"\n<*>time = %.3f ms\n\n", stopwatch_lap(&watch));
600 
601 	/* test finished */
602 	test_ok &= (n_tests_failed) ? 0 : 1;
603 	(n_tests_failed) ? printf(" ==> failed] ") : printf(" ==> ok] ");
604 
605 } /* end of test_sample() */
606 
607 
608 /*---------------------------------------------------------------------------*/
609 /* run generator in verifying mode */
610 
run_verify_generator(FILE * LOG,int line,UNUR_PAR * par)611 void run_verify_generator( FILE *LOG, int line, UNUR_PAR *par )
612 {
613 	UNUR_GEN *gen;
614 	int i;
615 
616 	/* switch to verifying mode */
617 	unur_distr_discr_set_verify(par,1);
618 
619 	/* initialize generator */
620 	gen = unur_init( par ); abort_if_NULL(LOG, line, gen);
621 
622 	/* run generator */
623 	for (i=0; i<VIOLATE_SAMPLE_SIZE; i++)
624 		unur_sample_cont(gen);
625 
626 	/* destroy generator */
627 	unur_free(gen);
628 
629 } /* end of run_verify_generator() */
630 
unur_distr_discr_set_verify(UNUR_PAR * par ATTRIBUTE__UNUSED,int verify ATTRIBUTE__UNUSED)631 int unur_distr_discr_set_verify(UNUR_PAR *par ATTRIBUTE__UNUSED, int verify ATTRIBUTE__UNUSED) {return 0;}
632 
633 /*---------------------------------------------------------------------------*/
634 
main(void)635 int main(void)
636 {
637         unsigned long seed;
638 	char *str_seed, *str_tail;
639 
640 	/* start stop watch */
641 	stopwatch_init();
642 	stopwatch_start(&watch);
643 
644         /* open log file for unuran and set output stream for unuran messages */
645         UNURANLOG = fopen( "t_distr_discr_unuran.log","w" );
646         abort_if_NULL( stderr,-1, UNURANLOG );
647         unur_set_stream( UNURANLOG );
648 
649         /* open log file for testing */
650 	TESTLOG = fopen( "t_distr_discr_test.log","w" );
651 	abort_if_NULL( stderr,-1, TESTLOG );
652 
653         /* seed for uniform generators */
654 
655 	/* seed set by environment */
656 	str_seed = getenv("SEED");
657 
658 	if (str_seed != NULL) {
659 	    seed = strtol(str_seed, &str_tail, 10);
660 	    if (seed == 0u)
661 		seed = 427389;
662 	}
663 	else {
664 #ifdef SEED
665 	    seed = SEED;
666 #else
667 	    seed = 427389;
668 #endif
669 	}
670 
671         /* seed build-in uniform generators */
672         unur_urng_MRG31k3p_seed(NULL,seed);
673         unur_urng_fish_seed(NULL,seed);
674 	unur_urng_mstd_seed(NULL,seed);
675 
676 	/* seed uniform random number generator */
677 #ifdef UNUR_URNG_UNURAN
678 #  ifdef UNUR_URNG_DEFAULT_RNGSTREAM
679 	{
680 	        unsigned long sa[6];
681 	        int i;
682 	        for (i=0; i<6; i++) sa[i] = seed;
683                 RngStream_SetPackageSeed(sa);
684         }
685 #  else
686 	if (unur_urng_seed(NULL,seed) != UNUR_SUCCESS) {
687 	        fprintf(stderr,"WARNING: Seed could not be set at random\n");
688                 seed = ~0u;
689 	}
690 #  endif  /* UNUR_URNG_DEFAULT_RNGSTREAM */
691 #endif  /* UNUR_URNG_UNURAN */
692 
693 	/* set default debugging flag */
694 	unur_set_default_debug(UNUR_DEBUG_ALL);
695 
696         /* detect required check mode */
697         fullcheck = (getenv("UNURANFULLCHECK")==NULL) ? FALSE : TRUE;
698 
699 	/* write header into log file */
700         print_test_log_header( TESTLOG, seed, fullcheck );
701 
702 	/* set timer for sending SIGALRM signal */
703 	set_alarm(TESTLOG);
704 
705 	/* start test */
706 	printf("distr_discr: ");
707 
708 	/* run tests */
709 test_new();
710 test_set();
711 test_get();
712 test_chg();
713 test_sample();
714 
715 
716 	/* test finished */
717 	printf("\n");  fflush(stdout);
718 
719 	/* close log files */
720 	fprintf(TESTLOG,"\n====================================================\n\n");
721 	if (test_ok)
722 		fprintf(TESTLOG,"All tests PASSED.\n");
723 	else
724 		fprintf(TESTLOG,"Test(s) FAILED.\n");
725 
726 	/* timing */
727 	stopwatch_print(TESTLOG,"\n<*>total time = %.0f ms\n\n", stopwatch_stop(&watch));
728 
729 	fclose(UNURANLOG);
730 	fclose(TESTLOG);
731 
732 	/* free memory */
733 	compare_free_memory();
734 	unur_urng_free(unur_get_default_urng());
735 	unur_urng_free(unur_get_default_urng_aux());
736 
737 	/* exit */
738 	exit( (test_ok) ? EXIT_SUCCESS : EXIT_FAILURE );
739 
740 } /* end of main */
741 
742