diff options
author | Kevin Ryde <user42@zip.com.au> | 2003-08-13 00:12:37 +0200 |
---|---|---|
committer | Kevin Ryde <user42@zip.com.au> | 2003-08-13 00:12:37 +0200 |
commit | a9cd1139267db5c47372d1c7e3475f30a8f0e02b (patch) | |
tree | 57a7d71aa9d8d9c4b52dad7555971ef3ff0492c7 /tests | |
parent | b9cab7a0281a4ae3d38578548178a24d94d68919 (diff) | |
download | gmp-a9cd1139267db5c47372d1c7e3475f30a8f0e02b.tar.gz |
* tests/mpq/t-get_d.c (check_onebit): New test.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/mpq/t-get_d.c | 91 |
1 files changed, 82 insertions, 9 deletions
diff --git a/tests/mpq/t-get_d.c b/tests/mpq/t-get_d.c index 23342cc0b..2ea7986ac 100644 --- a/tests/mpq/t-get_d.c +++ b/tests/mpq/t-get_d.c @@ -1,7 +1,7 @@ /* Test mpq_get_d and mpq_set_d -Copyright 1991, 1993, 1994, 1996, 2000, 2001, 2002 Free Software Foundation, -Inc. +Copyright 1991, 1993, 1994, 1996, 2000, 2001, 2002, 2003 Free Software +Foundation, Inc. This file is part of the GNU MP Library. @@ -43,8 +43,8 @@ MA 02111-1307, USA. */ void dump _PROTO ((mpq_t)); -int -main (int argc, char **argv) +void +check_monotonic (int argc, char **argv) { mpq_t a; mp_size_t size; @@ -54,8 +54,6 @@ main (int argc, char **argv) mpq_t qlast_d, qnew_d; mpq_t eps; - tests_start (); - if (argc == 2) reps = atoi (argv[1]); @@ -119,9 +117,6 @@ main (int argc, char **argv) mpq_clear (eps); mpq_clear (qlast_d); mpq_clear (qnew_d); - - tests_end (); - exit (0); } void @@ -132,3 +127,81 @@ dump (mpq_t x) mpz_out_str (stdout, 10, mpq_denref (x)); printf ("\n"); } + +/* Check various values 2^n and 1/2^n. */ +void +check_onebit (void) +{ + static const long data[] = { + -3*GMP_NUMB_BITS-1, -3*GMP_NUMB_BITS, -3*GMP_NUMB_BITS+1, + -2*GMP_NUMB_BITS-1, -2*GMP_NUMB_BITS, -2*GMP_NUMB_BITS+1, + -GMP_NUMB_BITS-1, -GMP_NUMB_BITS, -GMP_NUMB_BITS+1, + -5, -2, -1, 0, 1, 2, 5, + GMP_NUMB_BITS-1, GMP_NUMB_BITS, GMP_NUMB_BITS+1, + 2*GMP_NUMB_BITS-1, 2*GMP_NUMB_BITS, 2*GMP_NUMB_BITS+1, + 3*GMP_NUMB_BITS-1, 3*GMP_NUMB_BITS, 3*GMP_NUMB_BITS+1, + }; + + int i, neg; + long exp, l; + mpq_t q; + double got, want; + /* FIXME: It'd be better to base this on the float format. */ +#ifdef __vax + int limit = 127; /* vax fp numbers have limited range */ +#else + int limit = 512; +#endif + + mpq_init (q); + + for (i = 0; i < numberof (data); i++) + { + exp = data[i]; + + mpq_set_ui (q, 1L, 1L); + if (exp >= 0) + mpq_mul_2exp (q, q, exp); + else + mpq_div_2exp (q, q, -exp); + + want = 1.0; + for (l = 0; l < exp; l++) + want *= 2.0; + for (l = 0; l > exp; l--) + want /= 2.0; + + for (neg = 0; neg <= 1; neg++) + { + if (neg) + { + mpq_neg (q, q); + want = -want; + } + + got = mpq_get_d (q); + + if (got != want) + { + printf ("mpq_get_d wrong on %s2**%d\n", i, neg ? "-" : ""); + mpq_trace (" q ", q); + d_trace (" want ", want); + d_trace (" got ", got); + abort(); + } + } + } + mpq_clear (q); +} + +int +main (int argc, char **argv) +{ + tests_start (); + + check_onebit (); + check_monotonic (argc, argv); + + tests_end (); + exit (0); +} |