diff options
author | zimmerma <zimmerma@280ebfd0-de03-0410-8827-d642c229c3f4> | 2001-04-03 17:09:44 +0000 |
---|---|---|
committer | zimmerma <zimmerma@280ebfd0-de03-0410-8827-d642c229c3f4> | 2001-04-03 17:09:44 +0000 |
commit | 58cd782d1552514cf17e083508265e405f2fd1af (patch) | |
tree | 3001606dcaa25bdb33ddd0d587d99e9809b04c63 /tests/tcmp2.c | |
parent | ee292a934ed82769858eb5b9845d85ea68eb64cf (diff) | |
download | mpfr-58cd782d1552514cf17e083508265e405f2fd1af.tar.gz |
added several tests, and a routine that generates worst cases
git-svn-id: svn://scm.gforge.inria.fr/svn/mpfr/trunk@1044 280ebfd0-de03-0410-8827-d642c229c3f4
Diffstat (limited to 'tests/tcmp2.c')
-rw-r--r-- | tests/tcmp2.c | 132 |
1 files changed, 129 insertions, 3 deletions
diff --git a/tests/tcmp2.c b/tests/tcmp2.c index b4e5e05fe..8d9866c60 100644 --- a/tests/tcmp2.c +++ b/tests/tcmp2.c @@ -1,6 +1,6 @@ /* Test file for mpfr_cmp2. -Copyright (C) 1999-2000 Free Software Foundation. +Copyright (C) 1999-2001 Free Software Foundation. This file is part of the MPFR Library. @@ -29,8 +29,93 @@ MA 02111-1307, USA. */ void tcmp2 (double, double, int); void special (void); +void worst_cases (void); +void set_bit (mpfr_t, unsigned int, int); -void tcmp2 (double x, double y, int i) +/* set bit n of x to b, where bit 0 is the most significant one */ +void +set_bit (mpfr_t x, unsigned int n, int b) +{ + unsigned l; + mp_size_t xn; + + xn = (MPFR_PREC(x) - 1) / mp_bits_per_limb; + l = n / mp_bits_per_limb; + n %= mp_bits_per_limb; + n = mp_bits_per_limb - 1 - n; + if (b) + MPFR_MANT(x)[xn - l] |= (mp_limb_t) 1 << n; + else + MPFR_MANT(x)[xn - l] &= ~((mp_limb_t) 1 << n); +} + +/* check that for x = 1.u 1 v 0^k low(x) + y = 1.u 0 v 1^k low(y) + mpfr_cmp2 (x, y) returns 1 + |u| + |v| + k for low(x) >= low(y), + and 1 + |u| + |v| + k + 1 otherwise */ +void +worst_cases () +{ + mpfr_t x, y; + unsigned int i, j, k, l, b, expected; + + mpfr_init2 (x, 200); + mpfr_init2 (y, 200); + for (i=0; i<64; i++) /* |u| = i */ + { + mpfr_random (x); + mpfr_set (y, x, GMP_RNDN); + set_bit (x, i + 1, 1); + set_bit (y, i + 1, 0); + for (j=0; j<64; j++) /* |v| = j */ + { + b = random () % 2; + set_bit (x, i + j + 2, b); + set_bit (y, i + j + 2, b); + + for (k=0; k<64; k++) + { + + if (k) set_bit (x, i + j + k + 1, 0); + if (k) set_bit (y, i + j + k + 1, 1); + + set_bit (x, i + j + k + 2, 1); + set_bit (y, i + j + k + 2, 0); + l = mpfr_cmp2 (x, y); + expected = i + j + k + 1; + if (l != expected) + { + fprintf (stderr, "Error in mpfr_cmp2:\nx="); + mpfr_out_str (stderr, 2, 0, x, GMP_RNDN); + fprintf (stderr, "\ny="); + mpfr_out_str (stderr, 2, 0, y, GMP_RNDN); + fprintf (stderr, "\ngot %u instead of %u\n", l, expected); + exit(1); + } + + set_bit (x, i + j + k + 2, 0); + set_bit (x, i + j + k + 3, 0); + set_bit (y, i + j + k + 3, 1); + l = mpfr_cmp2 (x, y); + expected = i + j + k + 2; + if (l != expected) + { + fprintf (stderr, "Error in mpfr_cmp2:\nx="); + mpfr_out_str (stderr, 2, 0, x, GMP_RNDN); + fprintf (stderr, "\ny="); + mpfr_out_str (stderr, 2, 0, y, GMP_RNDN); + fprintf (stderr, "\ngot %u instead of %u\n", l, expected); + exit(1); + } + } + } + } + mpfr_clear (x); + mpfr_clear (y); +} + +void +tcmp2 (double x, double y, int i) { mpfr_t xx, yy; int j; @@ -44,7 +129,11 @@ void tcmp2 (double x, double y, int i) mpfr_set_d(yy, y, 0); j = mpfr_cmp2(xx, yy); if (j != i) { - printf("Error in mpfr_cmp2: x=%1.20e y=%1.20e mpfr_cmp2(x,y)=%d instead of %d\n",x,y,j,i); + fprintf (stderr, "Error in mpfr_cmp2 for\nx="); + mpfr_out_str (stderr, 2, 0, xx, GMP_RNDN); + fprintf (stderr, "\ny="); + mpfr_out_str (stderr, 2, 0, yy, GMP_RNDN); + fprintf (stderr, "\ngot %u instead of %u\n", j, i); exit(1); } mpfr_clear(xx); mpfr_clear(yy); @@ -96,6 +185,42 @@ void special () exit(1); } + /* bug found by Nathalie Revol, 29 March 2001 */ + mpfr_set_prec (x, 130); mpfr_set_prec (y, 130); + mpfr_set_str_raw (x, "0.1100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000E2"); + mpfr_set_str_raw (y, "0.1011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100E2"); + if ((j=mpfr_cmp2(x, y)) != 127) { + printf("Error in mpfr_cmp2:\n"); + printf("x="); mpfr_print_raw(x); putchar('\n'); + printf("y="); mpfr_print_raw(y); putchar('\n'); + printf("got %d, expected 127\n", j); + exit(1); + } + + /* bug found by Nathalie Revol, 2 Apr 2001 */ + mpfr_set_prec (x, 65); mpfr_set_prec (y, 65); + mpfr_set_ui (x, 5, GMP_RNDN); + mpfr_set_str_raw (y, "0.10011111111111111111111111111111111111111111111111111111111111101E3"); + if ((j=mpfr_cmp2(x, y)) != 63) { + printf("Error in mpfr_cmp2:\n"); + printf("x="); mpfr_print_raw(x); putchar('\n'); + printf("y="); mpfr_print_raw(y); putchar('\n'); + printf("got %d, expected 63\n", j); + exit(1); + } + + /* bug found by Nathalie Revol, 2 Apr 2001 */ + mpfr_set_prec (x, 65); mpfr_set_prec (y, 65); + mpfr_set_str_raw (x, "0.10011011111000101001110000000000000000000000000000000000000000000E-69"); + mpfr_set_str_raw (y, "0.10011011111000101001101111111111111111111111111111111111111111101E-69"); + if ((j=mpfr_cmp2(x, y)) != 63) { + printf("Error in mpfr_cmp2:\n"); + printf("x="); mpfr_print_raw(x); putchar('\n'); + printf("y="); mpfr_print_raw(y); putchar('\n'); + printf("got %d, expected 63\n", j); + exit(1); + } + mpfr_clear(x); mpfr_clear(y); } @@ -111,6 +236,7 @@ main (void) set_fpc_csr(exp.fc_word); #endif + worst_cases (); special (); tcmp2(5.43885304644369510000e+185, -1.87427265794105340000e-57, 1); tcmp2(1.06022698059744327881e+71, 1.05824655795525779205e+71, -1); |