diff options
author | vlefevre <vlefevre@280ebfd0-de03-0410-8827-d642c229c3f4> | 2012-06-26 17:06:47 +0000 |
---|---|---|
committer | vlefevre <vlefevre@280ebfd0-de03-0410-8827-d642c229c3f4> | 2012-06-26 17:06:47 +0000 |
commit | 0c34547d9fec112a38b3ad03bf4bdf345a94c4f6 (patch) | |
tree | e000b0de63a592fa0d5eb6857aa9c078392a8744 /tests/tstckintc.c | |
parent | f4da583d9e5c3c85186d78a1063740db4dc79221 (diff) | |
download | mpfr-0c34547d9fec112a38b3ad03bf4bdf345a94c4f6.tar.gz |
[tests/tstckintc.c] Correction and test improvement (from a patch
by Patrick).
git-svn-id: svn://scm.gforge.inria.fr/svn/mpfr/trunk@8264 280ebfd0-de03-0410-8827-d642c229c3f4
Diffstat (limited to 'tests/tstckintc.c')
-rw-r--r-- | tests/tstckintc.c | 210 |
1 files changed, 153 insertions, 57 deletions
diff --git a/tests/tstckintc.c b/tests/tstckintc.c index db8a439b6..068b88d15 100644 --- a/tests/tstckintc.c +++ b/tests/tstckintc.c @@ -27,6 +27,7 @@ http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc., long Buffer[BUFFER_SIZE]; char *stack = (char *) Buffer; +long *org = (long *) Buffer; mpfr_prec_t p = PREC_TESTED; #define ALIGNED(s) (((s) + sizeof (long) - 1) / sizeof (long) * sizeof (long)) @@ -38,12 +39,20 @@ new_st (size_t s) stack += ALIGNED (s); if (MPFR_UNLIKELY (stack > (char *) &Buffer[BUFFER_SIZE])) { - printf ("Stack overflow.\n"); + printf ("[INTERNAL TEST ERROR] Stack overflow.\n"); exit (1); } return p; } +static void +reset_stack (void) +{ + stack = (char *) Buffer; +} + +/*************************************************************************/ + /* Alloc a new mpfr_t on the main stack */ static mpfr_ptr new_mpfr (mpfr_prec_t p) @@ -55,7 +64,29 @@ new_mpfr (mpfr_prec_t p) return x; } - /* Garbage the stack by keeping only x */ + /* Alloc a new mpfr_t on the main stack */ +static mpfr_ptr +new_nan (mpfr_prec_t p) +{ + mpfr_ptr x = (mpfr_ptr) new_st (sizeof (mpfr_t)); + void *mantissa = new_st ((mpfr_custom_get_size) (p)); + (mpfr_custom_init) (mantissa, p); + (mpfr_custom_init_set) (x, MPFR_NAN_KIND, 0, p, mantissa); + return x; +} + + /* Alloc a new mpfr_t on the main stack */ +static mpfr_ptr +new_inf (mpfr_prec_t p) +{ + mpfr_ptr x = (mpfr_ptr) new_st (sizeof (mpfr_t)); + void *mantissa = new_st ((mpfr_custom_get_size) (p)); + (mpfr_custom_init) (mantissa, p); + (mpfr_custom_init_set) (x, -MPFR_INF_KIND, 0, p, mantissa); + return x; +} + + /* Garbage the stack by keeping only x and save it in old_stack */ static mpfr_ptr return_mpfr (mpfr_ptr x, char *old_stack) { @@ -71,27 +102,122 @@ return_mpfr (mpfr_ptr x, char *old_stack) return newx; } + /* Garbage the stack by keeping only x and save it in old_stack */ +static mpfr_ptr +return_mpfr_func (mpfr_ptr x, char *old_stack) +{ + void *mantissa = (mpfr_custom_get_significand) (x); + size_t size_mantissa = (mpfr_custom_get_size) (mpfr_get_prec (x)); + mpfr_ptr newx; + + memmove (old_stack, x, sizeof (mpfr_t)); + memmove (old_stack + ALIGNED (sizeof (mpfr_t)), mantissa, size_mantissa); + newx = (mpfr_ptr) old_stack; + (mpfr_custom_move) (newx, old_stack + ALIGNED (sizeof (mpfr_t))); + stack = old_stack + ALIGNED (sizeof (mpfr_t)) + ALIGNED (size_mantissa); + return newx; +} + +/*************************************************************************/ + static void test1 (void) { mpfr_ptr x, y; - char *org; - org = stack; + reset_stack (); + org = (long *) stack; + x = new_mpfr (p); y = new_mpfr (p); mpfr_set_ui (x, 42, MPFR_RNDN); mpfr_set_ui (y, 17, MPFR_RNDN); mpfr_add (y, x, y, MPFR_RNDN); - y = return_mpfr (y, org); - if (y != x || mpfr_cmp_ui (y, 59) != 0) + y = return_mpfr (y, (char *) org); + if ((long *) y != org || mpfr_cmp_ui (y, 59) != 0) { printf ("Compact (1) failed!\n"); exit (1); } - stack = org; + + x = new_mpfr (p); + y = new_mpfr (p); + mpfr_set_ui (x, 4217, MPFR_RNDN); + mpfr_set_ui (y, 1742, MPFR_RNDN); + mpfr_add (y, x, y, MPFR_RNDN); + y = return_mpfr_func (y, (char *) org); + if ((long *) y != org || mpfr_cmp_ui (y, 5959) != 0) + { + printf ("Compact (5) failed!\n"); + exit (1); + } + + reset_stack (); +} + +static void +test_nan_inf_zero (void) +{ + mpfr_ptr val; + int sign; + int kind; + + reset_stack (); + + val = new_mpfr (MPFR_PREC_MIN); + mpfr_set_nan (val); + kind = (mpfr_custom_get_kind) (val); + if (kind != MPFR_NAN_KIND) + { + printf ("mpfr_custom_get_kind error: "); + mpfr_dump (val); + printf (" is kind %d instead of %d\n", kind, (int) MPFR_NAN_KIND); + exit (1); + } + + val = new_nan (MPFR_PREC_MIN); + if (!mpfr_nan_p(val)) + { + printf ("Error: mpfr_custom_init_set doesn't set NAN mpfr.\n"); + exit (1); + } + + val = new_inf (MPFR_PREC_MIN); + if (!mpfr_inf_p(val) || mpfr_sgn(val) >= 0) + { + printf ("Error: mpfr_custom_init_set doesn't set -INF mpfr.\n"); + exit (1); + } + + sign = 1; + mpfr_set_inf (val, sign); + kind = (mpfr_custom_get_kind) (val); + if ((ABS (kind) != MPFR_INF_KIND) || (SIGN (kind) != SIGN (sign))) + { + printf ("mpfr_custom_get_kind error: "); + mpfr_dump (val); + printf (" is kind %d instead of %d\n", kind, (int) MPFR_INF_KIND); + printf (" have sign %d instead of %d\n", SIGN (kind), SIGN (sign)); + exit (1); + } + + sign = -1; + mpfr_set_zero (val, sign); + kind = (mpfr_custom_get_kind) (val); + if ((ABS (kind) != MPFR_ZERO_KIND) || (SIGN (kind) != SIGN (sign))) + { + printf ("mpfr_custom_get_kind error: "); + mpfr_dump (val); + printf (" is kind %d instead of %d\n", kind, (int) MPFR_ZERO_KIND); + printf (" have sign %d instead of %d\n", SIGN (kind), SIGN (sign)); + exit (1); + } + + reset_stack (); } +/*************************************************************************/ + /* We build the MPFR variable each time it is needed */ /* a[0] is the kind, a[1] is the exponent, &a[2] is the mantissa */ static long * @@ -101,7 +227,6 @@ dummy_new (void) r = (long *) new_st (ALIGNED (2 * sizeof (long)) + ALIGNED (mpfr_custom_get_size (p))); - MPFR_ASSERTN (r != NULL); (mpfr_custom_init) (&r[2], p); r[0] = (int) MPFR_NAN_KIND; r[1] = 0; @@ -113,7 +238,7 @@ dummy_set_si (long si) { mpfr_t x; long * r = dummy_new (); - (mpfr_custom_init_set) (x, 0, 0, p, &r[2]); + (mpfr_custom_init_set) (x, MPFR_REGULAR_KIND, 0, p, &r[2]); mpfr_set_si (x, si, MPFR_RNDN); r[0] = mpfr_custom_get_kind (x); r[1] = mpfr_custom_get_exp (x); @@ -125,7 +250,7 @@ dummy_add (long *a, long *b) { mpfr_t x, y, z; long *r = dummy_new (); - mpfr_custom_init_set (x, 0, 0, p, &r[2]); + mpfr_custom_init_set (x, MPFR_REGULAR_KIND, 0, p, &r[2]); (mpfr_custom_init_set) (y, a[0], a[1], p, &a[2]); mpfr_custom_init_set (z, b[0], b[1], p, &b[2]); mpfr_add (x, y, z, MPFR_RNDN); @@ -135,81 +260,52 @@ dummy_add (long *a, long *b) } static long * -dummy_compact (long *r, char *org_stack) +dummy_compact (long *r, long *org_stack) { memmove (org_stack, r, ALIGNED (2*sizeof (long)) + ALIGNED ((mpfr_custom_get_size) (p))); - return (long *) org_stack; + return org_stack; } +/*************************************************************************/ + static void test2 (void) { mpfr_t x; - char *org = stack; long *a, *b, *c; + reset_stack (); + org = (long *) stack; + a = dummy_set_si (42); b = dummy_set_si (17); c = dummy_add (a, b); c = dummy_compact (c, org); (mpfr_custom_init_set) (x, c[0], c[1], p, &c[2]); - if (c != a || mpfr_cmp_ui (x, 59) != 0) + if (c != org || mpfr_cmp_ui (x, 59) != 0) { printf ("Compact (2) failed! c=%p a=%p\n", (void *) c, (void *) a); mpfr_dump (x); exit (1); } - stack = org; -} - -static void -test_nan_inf_zero (void) -{ - mpfr_ptr val; - int sign; - int kind; - char *org = stack; - val = new_mpfr (MPFR_PREC_MIN); - - mpfr_set_nan (val); - kind = (mpfr_custom_get_kind) (val); - if (kind != MPFR_NAN_KIND) - { - printf ("mpfr_custom_get_kind error : "); - mpfr_dump (val); - printf (" is kind %d instead of %d\n", kind, MPFR_NAN_KIND); - exit (1); - } - - sign = 1; - mpfr_set_inf (val, sign); - kind = (mpfr_custom_get_kind) (val); - if ((ABS (kind) != MPFR_INF_KIND) || (SIGN (kind) != SIGN (sign))) - { - printf ("mpfr_custom_get_kind error : "); - mpfr_dump (val); - printf (" is kind %d instead of %d\n", kind, MPFR_INF_KIND); - printf (" have sign %d instead of %d\n", SIGN (kind), SIGN (sign)); - exit (1); - } - - sign = -1; - mpfr_set_zero (val, sign); - kind = (mpfr_custom_get_kind) (val); - if ((ABS (kind) != MPFR_ZERO_KIND) || (SIGN (kind) != SIGN (sign))) + a = dummy_set_si (42); + b = dummy_set_si (-17); + c = dummy_add (a, b); + c = dummy_compact (c, org); + (mpfr_custom_init_set) (x, c[0], c[1], p, &c[2]); + if (c != org || mpfr_cmp_ui (x, 25) != 0) { - printf ("mpfr_custom_get_kind error : "); - mpfr_dump (val); - printf (" is kind %d instead of %d\n", kind, MPFR_ZERO_KIND); - printf (" have sign %d instead of %d\n", SIGN (kind), SIGN (sign)); + printf ("Compact (6) failed! c=%p a=%p\n", (void *) c, (void *) a); + mpfr_dump (x); exit (1); } - stack = org; + reset_stack (); } + int main (void) { |