diff options
author | thevenyp <thevenyp@211d60ee-9f03-0410-a15a-8952a2c7a4e4> | 2014-01-20 18:19:00 +0000 |
---|---|---|
committer | thevenyp <thevenyp@211d60ee-9f03-0410-a15a-8952a2c7a4e4> | 2014-01-20 18:19:00 +0000 |
commit | 595536faf3608c8ba49d213b3bf5ac53dce3881f (patch) | |
tree | f680c4ae852201766e91e0e07ba46dc6e34f5cf1 /tests/random.c | |
parent | 4499643a3ab4178c248dfc5b13bf2585fd74ef15 (diff) | |
download | mpc-595536faf3608c8ba49d213b3bf5ac53dce3881f.tar.gz |
New test framework, merged from test branch.
git-svn-id: svn://scm.gforge.inria.fr/svn/mpc/trunk@1416 211d60ee-9f03-0410-a15a-8952a2c7a4e4
Diffstat (limited to 'tests/random.c')
-rw-r--r-- | tests/random.c | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/tests/random.c b/tests/random.c index 0730fee..89da546 100644 --- a/tests/random.c +++ b/tests/random.c @@ -158,3 +158,114 @@ test_default_random (mpc_ptr z, mpfr_exp_t emin, mpfr_exp_t emax, if (((r>>8) & 0xFF) < negative_probability) mpfr_neg (mpc_imagref (z), mpc_imagref (z), MPFR_RNDN); } + +/* Set n to a non zero value random value with absolute values less than + 2^emax. + + n is negative with probability equal to NEGATIVE_PROBABILITY / 256. +*/ +void test_random_si (long int *n, unsigned long emax, + unsigned int negative_probability) +{ + unsigned long r; + + if (!rands_initialized) + { + fprintf (stderr, + "Put test_start at the beginning of your test function.\n"); + exit (1); + } + + do + { + *n = gmp_urandomb_ui (rands, emax); + } while (*n == 0); + + if (negative_probability > 256) + negative_probability = 256; + r = gmp_urandomb_ui (rands, 8); + if ((r & 0xFF) < negative_probability) + *n = -(*n); +} + +/* Set x to a non zero value random value with absolute values greater than + or equal to 2^{emin-1} and less than 2^emax. + + x is negative with probability equal to NEGATIVE_PROBABILITY / 256. +*/ +void +test_random_mpfr (mpfr_ptr x, mpfr_exp_t emin, mpfr_exp_t emax, + unsigned int negative_probability) +{ + const unsigned long range = (unsigned long int) (emax - emin) + 1; + unsigned long r; + + if (!rands_initialized) + { + fprintf (stderr, + "Put test_start at the beginning of your test function.\n"); + exit (1); + } + + do + { + mpfr_urandom (x, rands, MPFR_RNDN); + } while (mpfr_zero_p (x)); + + mpfr_set_exp (x, (mpfr_exp_t) gmp_urandomm_ui (rands, range) + emin); + if (negative_probability > 256) + negative_probability = 256; + r = gmp_urandomb_ui (rands, 8); + if ((r & 0xFF) < negative_probability) + mpfr_neg (x, x, MPFR_RNDN); +} + +/* Set x to a non zero value random value. + x is negative with probability equal to NEGATIVE_PROBABILITY / 256. +*/ +void +test_random_d (double *x, unsigned int negative_probability) +{ + MPFR_DECL_INIT (mpfr_x, 53); + test_random_mpfr (mpfr_x, -1022, 1022, negative_probability); + *x = mpfr_get_d (mpfr_x, MPFR_RNDN); +} + +/* Set z to a non zero value random value with absolute values of Re(z) and + Im(z) greater than or equal to 2^{emin-1} and less than 2^emax. + + Each part is negative with probability equal to NEGATIVE_PROBABILITY / 256. +*/ +void +test_random_mpc (mpc_ptr z, mpfr_exp_t emin, mpfr_exp_t emax, + unsigned int negative_probability) +{ + const unsigned long range = (unsigned long int) (emax - emin) + 1; + unsigned long r; + + if (!rands_initialized) + { + fprintf (stderr, + "Put test_start at the beginning of your test function.\n"); + exit (1); + } + + do + { + mpc_urandom (z, rands); + } while (mpfr_zero_p (mpc_realref (z)) || mpfr_zero_p (mpc_imagref (z))); + + mpfr_set_exp (mpc_realref (z), + (mpfr_exp_t) gmp_urandomm_ui (rands, range) + emin); + + mpfr_set_exp (mpc_imagref (z), + (mpfr_exp_t) gmp_urandomm_ui (rands, range) + emin); + + if (negative_probability > 256) + negative_probability = 256; + r = gmp_urandomb_ui (rands, 16); + if ((r & 0xFF) < negative_probability) + mpfr_neg (mpc_realref (z), mpc_realref (z), MPFR_RNDN); + if (((r>>8) & 0xFF) < negative_probability) + mpfr_neg (mpc_imagref (z), mpc_imagref (z), MPFR_RNDN); +} |