#############################################################################

[main]

[main - data:]

# method
method: TDR

[main - header:]

/* prototypes */
double pdf( double x, const UNUR_DISTR *distr );

double pdf_bimodal( double x, const UNUR_DISTR *distr );
double dpdf_bimodal( double x, const UNUR_DISTR *distr );

double pdf_negative( double x, const UNUR_DISTR *distr );
double dpdf_negative( double x, const UNUR_DISTR *distr );

double pdf_partnegative( double x, const UNUR_DISTR *distr );
double dpdf_partnegative( double x, const UNUR_DISTR *distr );

\#define COMPARE_SAMPLE_SIZE   (10000)
\#define VIOLATE_SAMPLE_SIZE   (20)

#############################################################################

[new]

[new - invalid NULL ptr: 
   distr = NULL; ]

~( distr );
	--> expected_NULL --> UNUR_ERR_NULL

[new - invalid distribution type:
   distr = unur_distr_discr_new(); ]

~( distr );
	 --> expected_NULL --> UNUR_ERR_DISTR_INVALID

[new - data missing in distribution object:
   distr = unur_distr_cont_new(); ]

/* pdf, dpdf */
~( distr );
	--> expected_NULL --> UNUR_ERR_DISTR_REQUIRED

/* dpdf */
unur_distr_cont_set_pdf(distr,pdf);
~( distr );
	--> expected_NULL --> UNUR_ERR_DISTR_REQUIRED

#############################################################################

[set]

[set - invalid NULL ptr: 
   par = NULL; ]

~_cpoints( par, 0, NULL );
	--> expected_setfailed --> UNUR_ERR_NULL

~_reinit_percentiles( par, 0, NULL );
	--> expected_setfailed --> UNUR_ERR_NULL

~_guidefactor( par, 1. );
	--> expected_setfailed --> UNUR_ERR_NULL

~_max_sqhratio( par, 0.95 );
	--> expected_setfailed --> UNUR_ERR_NULL

~_max_intervals( par, 100 );
	--> expected_setfailed --> UNUR_ERR_NULL

~_usecenter( par, 1 );
	--> expected_setfailed --> UNUR_ERR_NULL

~_usemode( par, 1 );
	--> expected_setfailed --> UNUR_ERR_NULL

~_variant_gw( par );
	--> expected_setfailed --> UNUR_ERR_NULL

~_variant_ps( par );
	--> expected_setfailed --> UNUR_ERR_NULL

~_variant_ia( par );
	--> expected_setfailed --> UNUR_ERR_NULL

~_usedars( par, TRUE );
	--> expected_setfailed --> UNUR_ERR_NULL

~_darsfactor( par, 1. );
	--> expected_setfailed --> UNUR_ERR_NULL

~_c( par, -0.5 );
	--> expected_setfailed --> UNUR_ERR_NULL

~_verify( par, 1 );
	--> expected_setfailed --> UNUR_ERR_NULL

~_pedantic( par, 1 );
	--> expected_setfailed --> UNUR_ERR_NULL


[set - invalid parameter object:
   double fpar[2] = {0.,1.};
   distr = unur_distr_normal(fpar,2);
   par = unur_arou_new(distr); ]

~_cpoints( par, 0, NULL );
	--> expected_setfailed --> UNUR_ERR_PAR_INVALID

~_reinit_percentiles( par, 0, NULL );
	--> expected_setfailed --> UNUR_ERR_PAR_INVALID

~_guidefactor( par, 1. );
	--> expected_setfailed --> UNUR_ERR_PAR_INVALID

~_max_sqhratio( par, 0.95 );
	--> expected_setfailed --> UNUR_ERR_PAR_INVALID

~_max_intervals( par, 100 );
	--> expected_setfailed --> UNUR_ERR_PAR_INVALID

~_usecenter( par, 1 );
	--> expected_setfailed --> UNUR_ERR_PAR_INVALID

~_usemode( par, 1 );
	--> expected_setfailed --> UNUR_ERR_PAR_INVALID

~_variant_gw( par );
	--> expected_setfailed --> UNUR_ERR_PAR_INVALID

~_variant_ps( par );
	--> expected_setfailed --> UNUR_ERR_PAR_INVALID

~_variant_ia( par );
	--> expected_setfailed --> UNUR_ERR_PAR_INVALID

~_usedars( par, TRUE );
	--> expected_setfailed --> UNUR_ERR_PAR_INVALID

~_darsfactor( par, 1. );
	--> expected_setfailed --> UNUR_ERR_PAR_INVALID

~_c( par, -0.5 );
	--> expected_setfailed --> UNUR_ERR_PAR_INVALID

~_verify( par, 1 );
	--> expected_setfailed --> UNUR_ERR_PAR_INVALID

~_pedantic( par, 1 );
	--> expected_setfailed --> UNUR_ERR_PAR_INVALID

unur_par_free(par);

[set - invalid parameters:
   double perc[] = {0.1,0.4,0.2};
   double stp[] = {1.,0.,1.};
   int n_stp = 3;
   double fpar[2] = {0.,1.};
   distr = unur_distr_normal(fpar,2);
   par = unur_tdr_new(distr); ]

~_cpoints( par, -1, NULL );
	--> expected_setfailed --> UNUR_ERR_PAR_SET

~_cpoints( par, n_stp, stp );
	--> expected_setfailed --> UNUR_ERR_PAR_SET

~_reinit_percentiles( par, -1, NULL );
	--> none --> UNUR_ERR_PAR_SET

~_reinit_percentiles( par, n_stp, stp );
	--> expected_setfailed --> UNUR_ERR_PAR_SET

~_reinit_percentiles( par, n_stp, perc );
	--> expected_setfailed --> UNUR_ERR_PAR_SET

~_guidefactor( par, -1. );
	--> expected_setfailed --> UNUR_ERR_PAR_SET

~_max_sqhratio( par, -1. );
	--> expected_setfailed --> UNUR_ERR_PAR_SET

~_max_sqhratio( par, 2. );
	--> expected_setfailed --> UNUR_ERR_PAR_SET

~_max_intervals( par, 0 );
	--> expected_setfailed --> UNUR_ERR_PAR_SET

~_usedars( par, -1 );
	--> expected_setfailed --> UNUR_ERR_PAR_SET

~_darsfactor( par, -1. );
	--> expected_setfailed --> UNUR_ERR_PAR_SET

~_c( par, 0.5 );
	--> expected_setfailed --> UNUR_ERR_PAR_SET

~_c( par, -0.6 );
	--> expected_setfailed --> UNUR_ERR_PAR_SET

~_c( par, -0.3 );
	--> none --> UNUR_ERR_PAR_SET

unur_par_free(par);

#############################################################################

[get]

[get - invalid generator object:
   distr = unur_distr_normal(NULL,0);
   par = unur_arou_new(distr);
   gen = unur_init(par); <-- ! NULL ]

~_sqhratio(gen);
        --> expected_INFINITY --> UNUR_ERR_GEN_INVALID

~_hatarea(gen);
        --> expected_INFINITY --> UNUR_ERR_GEN_INVALID

~_squeezearea(gen);
        --> expected_INFINITY --> UNUR_ERR_GEN_INVALID


#############################################################################

[chg]

[chg - invalid generator object:
   double fpar[2] = {0.,1.};
   distr = unur_distr_normal(fpar,2);
   par = unur_arou_new(distr);
   unur_set_debug(par,0);
   gen = unur_init( par ); <-- ! NULL ]

~_reinit_percentiles(gen, -1, NULL );
	--> expected_setfailed --> UNUR_ERR_GEN_INVALID

~_verify(gen,1);
	--> expected_setfailed --> UNUR_ERR_GEN_INVALID


[chg - invalid parameters:
   double percmon[] = {0.1,0.4,0.2};
   double percdom[] = {0.1,0.4,2.};
   int n_perc = 3;
   distr = unur_distr_normal(NULL,0);
   par = unur_tdr_new(distr); 
   unur_set_debug(par,0);
   gen = unur_init( par ); <-- ! NULL ]

~_reinit_percentiles( gen, -1, NULL );
	--> none --> UNUR_ERR_PAR_SET

~_reinit_percentiles( gen, n_perc, percmon );
	--> expected_setfailed --> UNUR_ERR_PAR_SET

~_reinit_percentiles( gen, n_perc, percdom );
	--> expected_setfailed --> UNUR_ERR_PAR_SET


#############################################################################

[init]

[init - invalid NULL ptr: 
   par = NULL; ]

unur_init( par );
	--> expected_NULL --> UNUR_ERR_NULL

[init - stp out of domain:
   double stp[] = {-2.5,-1.,0.,1.,2.5};
   int n_stp = 5;
   gen = NULL;
   distr = unur_distr_normal(NULL,0);
   unur_distr_cont_set_domain(distr,-2.,2.);
   par = unur_tdr_new(distr);
   unur_tdr_set_usedars(par,FALSE);
   unur_tdr_set_cpoints( par, n_stp, stp ); ]

gen = unur_init( par );
	--> none --> UNUR_ERR_GEN_DATA

[init - bimodal: 
   gen = NULL;
   distr = unur_distr_cont_new(); 
   unur_distr_cont_set_pdf(distr,pdf_bimodal);
   unur_distr_cont_set_dpdf(distr,dpdf_bimodal);
   par = unur_tdr_new(distr);
   unur_tdr_set_usedars(par,FALSE);
   unur_tdr_set_cpoints( par, 30, NULL ); ]

gen = unur_init( par );
	--> expected_NULL --> UNUR_ERR_GEN_CONDITION

[init - wrong mode:
   gen = NULL;
   distr = unur_distr_normal(NULL,0);
   par = NULL; ]

unur_distr_cont_set_mode(distr,-1.);
par = unur_tdr_new(distr);
unur_tdr_set_usedars(par,FALSE);
unur_tdr_set_cpoints( par, 30, NULL );
gen = unur_init( par );
	--> expected_NULL --> UNUR_ERR_GEN_DATA

unur_distr_cont_set_mode(distr,1.);
par = unur_tdr_new(distr);
unur_tdr_set_usedars(par,FALSE);
unur_tdr_set_cpoints( par, 30, NULL );
gen = unur_init( par );
	--> none --> UNUR_ERR_GEN_DATA

[init - negative value of pdf:
   gen = NULL;
   distr = unur_distr_cont_new(); 
   unur_distr_cont_set_pdf(distr,pdf_negative);
   unur_distr_cont_set_dpdf(distr,dpdf_negative);
   par = NULL; ]

par = unur_tdr_new(distr);
unur_tdr_set_usedars(par,FALSE);
unur_tdr_set_cpoints( par, 30, NULL );
gen = unur_init( par );
	--> expected_NULL --> UNUR_ERR_GEN_DATA

unur_distr_cont_set_domain(distr,-2.,2.);
par = unur_tdr_new(distr);
unur_tdr_set_usedars(par,FALSE);
unur_tdr_set_cpoints( par, 30, NULL );
gen = unur_init( par );
	--> expected_NULL --> UNUR_ERR_GEN_DATA

#############################################################################

[reinit]

[reinit - exist:
   distr = unur_distr_normal(NULL,0);
   par = unur_tdr_new(distr);
   gen = unur_init( par ); <-- ! NULL ]

unur_reinit( gen );
	--> expected_reinit

#############################################################################

[sample]

[sample - invalid NULL ptr: 
   gen = NULL; ]

unur_tdr_eval_invcdfhat(gen,0.5,NULL,NULL,NULL);
	--> expected_INFINITY --> UNUR_ERR_NULL

#.....................................................................

[sample - invalid generator object:
   distr = unur_distr_normal(NULL,0);
   par = unur_arou_new(distr);
   gen = unur_init(par); <-- ! NULL ]

unur_tdr_eval_invcdfhat(gen,0.5,NULL,NULL,NULL);
	--> expected_INFINITY --> UNUR_ERR_GEN_INVALID

#.....................................................................

[sample - invalid domain:
   distr = unur_distr_normal(NULL,0);
   par = unur_tdr_new(distr);
   unur_set_debug(par,0);
   gen = unur_init( par ); <-- ! NULL ]

unur_tdr_eval_invcdfhat(gen,1.5,NULL,NULL,NULL);
	--> expected_INFINITY --> UNUR_ERR_DOMAIN

unur_tdr_eval_invcdfhat(gen,-0.5,NULL,NULL,NULL);
	--> expected_negINFINITY --> UNUR_ERR_DOMAIN

#.....................................................................

[sample - partial negative value of pdf:
   int i;
   double x;
   double stp[] = {-0.9,0.9};
   int n_stp = 2;
   distr = unur_distr_cont_new();
   unur_distr_cont_set_pdf(distr,pdf_partnegative);
   unur_distr_cont_set_dpdf(distr,dpdf_partnegative);
   par = unur_tdr_new(distr);
   unur_tdr_set_usedars(par,FALSE);
   unur_tdr_set_usecenter(par,FALSE);
   unur_tdr_set_cpoints( par, n_stp, stp );
   unur_tdr_set_pedantic( par, 1 );
   gen = unur_init( par ); <-- ! NULL ]

for (i=0; i<100; i++) { x = unur_sample_cont(gen); }
x;
	--> expected_INFINITY --> UNUR_ERR_GEN_CONDITION

#.....................................................................

[sample - compare:
   distr = unur_distr_normal(NULL,0); 
   par = NULL; ]

# T = -1/sqrt

/* GW, c = -0.5 */
par = unur_tdr_new(distr);
unur_tdr_set_variant_gw(par);
unur_tdr_set_c(par,-0.5);
unur_tdr_set_usedars(par,FALSE);
        -->compare_sequence_par_start

/* GW, c = -0.5 - verifying mode */
par = unur_tdr_new(distr);
unur_tdr_set_variant_gw(par);
unur_tdr_set_c(par,-0.5);
unur_tdr_set_verify(par,1);
unur_tdr_set_usedars(par,FALSE);
        -->compare_sequence_par

/* PS, c = -0.5 */
par = unur_tdr_new(distr);
unur_tdr_set_variant_ps(par);
unur_tdr_set_c(par,-0.5);
unur_tdr_set_usedars(par,FALSE);
        -->compare_sequence_par_start

/* PS, c = -0.5 - verifying mode */
par = unur_tdr_new(distr);
unur_tdr_set_variant_ps(par);
unur_tdr_set_c(par,-0.5);
unur_tdr_set_verify(par,1);
unur_tdr_set_usedars(par,FALSE);
        -->compare_sequence_par

/* IA, c = -0.5 */
par = unur_tdr_new(distr);
unur_tdr_set_variant_ia(par);
unur_tdr_set_c(par,-0.5);
unur_tdr_set_usedars(par,FALSE);
        -->compare_sequence_par_start

/* IA, c = -0.5 - verifying mode */
par = unur_tdr_new(distr);
unur_tdr_set_variant_ia(par);
unur_tdr_set_c(par,-0.5);
unur_tdr_set_verify(par,1);
unur_tdr_set_usedars(par,FALSE);
        -->compare_sequence_par


# T = log

/* GW, c = 0 */
par = unur_tdr_new(distr);
unur_tdr_set_variant_gw(par);
unur_tdr_set_c(par,0.);
unur_tdr_set_usedars(par,FALSE);
        -->compare_sequence_par_start

/* GW, c = 0, verifying mode */
par = unur_tdr_new(distr);
unur_tdr_set_variant_gw(par);
unur_tdr_set_c(par,0.);
unur_tdr_set_verify(par,1);
unur_tdr_set_usedars(par,FALSE);
        -->compare_sequence_par

/* PS, c = 0 */
par = unur_tdr_new(distr);
unur_tdr_set_variant_ps(par);
unur_tdr_set_c(par,0.);
unur_tdr_set_usedars(par,FALSE);
        -->compare_sequence_par_start

/* PS, c = 0, verifying mode */
par = unur_tdr_new(distr);
unur_tdr_set_variant_ps(par);
unur_tdr_set_c(par,0.);
unur_tdr_set_verify(par,1);
unur_tdr_set_usedars(par,FALSE);
        -->compare_sequence_par

/* IA, c = 0 */
par = unur_tdr_new(distr);
unur_tdr_set_variant_ia(par);
unur_tdr_set_c(par,0.);
unur_tdr_set_usedars(par,FALSE);
        -->compare_sequence_par_start

/* IA, c = 0, verifying mode */
par = unur_tdr_new(distr);
unur_tdr_set_variant_ia(par);
unur_tdr_set_c(par,0.);
unur_tdr_set_verify(par,1);
unur_tdr_set_usedars(par,FALSE);
        -->compare_sequence_par

#.....................................................................

[sample - compare clone:
   UNUR_GEN *clone;
   distr = unur_distr_normal(NULL,0); 
   par = NULL;
   gen = NULL; ]

/* original generator object */
par = unur_tdr_new(distr);
unur_tabl_set_max_sqhratio(par,0.);
gen = unur_init(par);
        -->compare_sequence_gen_start

/* clone */
clone = unur_gen_clone(gen);
unur_free(gen);
gen = clone;
        -->compare_sequence_gen

#.....................................................................

[sample - compare stringparser:
   double fpar[2] = {3.,3.};
   distr = NULL;
   par = NULL;
   gen = NULL; ]

distr = unur_distr_gamma(fpar,2);
unur_distr_cont_set_center(distr,2.1);
par = unur_tdr_new(distr);
unur_tdr_set_cpoints(par,20,NULL);
unur_tdr_set_guidefactor(par,2.);
unur_tdr_set_max_intervals(par,32);
unur_tdr_set_max_sqhratio(par,0.9);
unur_tdr_set_usecenter(par,TRUE);
unur_tdr_set_verify(par,TRUE);
gen = unur_init(par);
	-->compare_sequence_gen_start
	
unur_free(gen); gen = NULL;
unur_distr_free(distr); distr = NULL;
gen = unur_str2gen( "gamma(3.,3.); center = 2.1 & \
  method = tdr; usecenter; cpoints = 20; guidefactor = 2.; \
  max_intervals = 32; max_sqhratio = 0.9; verify" );
	-->compare_sequence_gen


unur_free(gen); gen = NULL;
distr = unur_distr_gamma(fpar,2);
par = unur_tdr_new(distr);
unur_tdr_set_cpoints(par,9,NULL);
unur_tdr_set_pedantic(par,TRUE);
unur_tdr_set_usemode(par,FALSE);
gen = unur_init(par);
	-->compare_sequence_gen_start
	
unur_free(gen); gen = NULL;
unur_distr_free(distr); distr = NULL;
gen = unur_str2gen( "gamma(3.,3.) & \
  method = tdr; cpoints = 9; pedantic; usemode = false" );
	-->compare_sequence_gen


unur_free(gen); gen = NULL;
distr = unur_distr_gamma(fpar,2);
par = unur_tdr_new(distr);
unur_tdr_set_c(par,0.);
gen = unur_init(par);
	-->compare_sequence_gen_start
	
unur_free(gen); gen = NULL;
unur_distr_free(distr); distr = NULL;
gen = unur_str2gen( "gamma(3.,3.) & \
  method = tdr; c = 0." );
	-->compare_sequence_gen


unur_free(gen); gen = NULL;
distr = unur_distr_gamma(fpar,2);
par = unur_tdr_new(distr);
unur_tdr_set_c(par,-0.5);
gen = unur_init(par);
	-->compare_sequence_gen_start
	
unur_free(gen); gen = NULL;
unur_distr_free(distr); distr = NULL;
gen = unur_str2gen( "gamma(3.,3.) & \
  method = tdr; c = -0.5" );
	-->compare_sequence_gen


unur_free(gen); gen = NULL;
distr = unur_distr_gamma(fpar,2);
par = unur_tdr_new(distr);
unur_tdr_set_variant_gw(par);
gen = unur_init(par);
	-->compare_sequence_gen_start

unur_free(gen); gen = NULL;
unur_distr_free(distr); distr = NULL;
gen = unur_str2gen( "gamma(3.,3.) & \
  method = tdr; variant_gw" );
	-->compare_sequence_gen


unur_free(gen); gen = NULL;
distr = unur_distr_gamma(fpar,2);
par = unur_tdr_new(distr);
unur_tdr_set_variant_ia(par);
gen = unur_init(par);
	-->compare_sequence_gen_start
	
unur_free(gen); gen = NULL;
unur_distr_free(distr); distr = NULL;
gen = unur_str2gen( "gamma(3.,3.) & \
  method = tdr; variant_ia" );
	-->compare_sequence_gen


unur_free(gen); gen = NULL;
distr = unur_distr_gamma(fpar,2);
par = unur_tdr_new(distr);
unur_tdr_set_variant_ps(par);
gen = unur_init(par);
	-->compare_sequence_gen_start
	
unur_free(gen); gen = NULL;
unur_distr_free(distr); distr = NULL;
gen = unur_str2gen( "gamma(3.,3.) & \
  method = tdr; variant_ps" );
	-->compare_sequence_gen


#############################################################################

# [validate]

#############################################################################

[verbatim]

/* pdf of bimodal density */
double pdf_bimodal( double x, const UNUR_DISTR *distr ATTRIBUTE__UNUSED)
{ 
	return ( exp(-(x-1.)*(x-1.)) + exp(-(x+1.)*(x+1.)) );
}
double dpdf_bimodal( double x, const UNUR_DISTR *distr ATTRIBUTE__UNUSED)
{ 
	return ( -2. * (x-1.) * exp(-(x-1.)*(x-1.)) -2. * (x+1.) * exp(-(x+1.)*(x+1.)) );
}

/* pdf with negative value */
double pdf_negative( double x, const UNUR_DISTR *distr ATTRIBUTE__UNUSED)
{
	return (-x*x);
}
double dpdf_negative( double x, const UNUR_DISTR *distr ATTRIBUTE__UNUSED)
{
	return (-2.*x);
}

/* pdf with partial negative value */
double pdf_partnegative( double x, const UNUR_DISTR *distr ATTRIBUTE__UNUSED)  
{ 
	return ((x>-0.89 && x<0.89) ? -1.: exp(-x*x)); 
}
double dpdf_partnegative( double x, const UNUR_DISTR *distr ATTRIBUTE__UNUSED) 
{
	return ((x>-0.89 && x<0.89) ?0.: -2.*x*exp(-x*x));
}

/* pdf of normal density */
double pdf( double x, const UNUR_DISTR *distr ATTRIBUTE__UNUSED)
{ 
	return exp(-x*x/2.);
} /* end of pdf */

#############################################################################
