Mercurial > hg > octave-kai > gnulib-hg
changeset 8498:accc767de343
Tests for module 'frexpl'.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Fri, 23 Mar 2007 01:27:57 +0000 |
parents | 280cabcbb0ca |
children | 7304ea530640 |
files | ChangeLog modules/frexpl-tests tests/test-frexpl.c |
diffstat | 3 files changed, 196 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,8 @@ 2007-03-22 Bruno Haible <bruno@clisp.org> + * modules/frexpl-tests: New file. + * tests/test-frexpl.c: New file. + * modules/frexpl: New file. * m4/frexpl.m4: New file. * modules/math (Makefile.am): Also substitute GNULIB_FREXPL into math.h.
new file mode 100644 --- /dev/null +++ b/modules/frexpl-tests @@ -0,0 +1,13 @@ +Files: +tests/test-frexpl.c + +Depends-on: + +configure.ac: + +Makefile.am: +TESTS += test-frexpl +check_PROGRAMS += test-frexpl + +License: +LGPL
new file mode 100644 --- /dev/null +++ b/tests/test-frexpl.c @@ -0,0 +1,180 @@ +/* Test of splitting a 'long double' into fraction and mantissa. + Copyright (C) 2007 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +/* Written by Bruno Haible <bruno@clisp.org>, 2007. */ + +#include <config.h> + +#include <math.h> + +#include <float.h> +#include <stdlib.h> + +#define ASSERT(expr) if (!(expr)) abort (); + +static long double +my_ldexp (long double x, int d) +{ + for (; d > 0; d--) + x *= 2.0L; + for (; d < 0; d++) + x *= 0.5L; + return x; +} + +int +main () +{ + int i; + long double x; + + { /* NaN. */ + int exp = -9999; + long double mantissa; + x = 0.0L / 0.0L; + mantissa = frexpl (x, &exp); + ASSERT (mantissa != mantissa); + } + + { /* Positive infinity. */ + int exp = -9999; + long double mantissa; + x = 1.0L / 0.0L; + mantissa = frexpl (x, &exp); + ASSERT (mantissa == x); + } + + { /* Negative infinity. */ + int exp = -9999; + long double mantissa; + x = -1.0L / 0.0L; + mantissa = frexpl (x, &exp); + ASSERT (mantissa == x); + } + + { /* Positive zero. */ + int exp = -9999; + long double mantissa; + x = 0.0L; + mantissa = frexpl (x, &exp); + ASSERT (exp == 0); + ASSERT (mantissa == x); + } + + { /* Negative zero. */ + int exp = -9999; + long double mantissa; + x = -0.0L; + mantissa = frexpl (x, &exp); + ASSERT (exp == 0); + ASSERT (mantissa == x); + } + + for (i = 1, x = 1.0L; i <= LDBL_MAX_EXP; i++, x *= 2.0L) + { + int exp = -9999; + long double mantissa = frexpl (x, &exp); + ASSERT (exp == i); + ASSERT (mantissa == 0.5L); + } + for (i = 1, x = 1.0L; i >= LDBL_MIN_EXP; i--, x *= 0.5L) + { + int exp = -9999; + long double mantissa = frexpl (x, &exp); + ASSERT (exp == i); + ASSERT (mantissa == 0.5L); + } + for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L) + { + int exp = -9999; + long double mantissa = frexpl (x, &exp); + ASSERT (exp == i); + ASSERT (mantissa == 0.5L); + } + + for (i = 1, x = -1.0L; i <= LDBL_MAX_EXP; i++, x *= 2.0L) + { + int exp = -9999; + long double mantissa = frexpl (x, &exp); + ASSERT (exp == i); + ASSERT (mantissa == -0.5L); + } + for (i = 1, x = -1.0L; i >= LDBL_MIN_EXP; i--, x *= 0.5L) + { + int exp = -9999; + long double mantissa = frexpl (x, &exp); + ASSERT (exp == i); + ASSERT (mantissa == -0.5L); + } + for (; i >= LDBL_MIN_EXP - 100 && x < 0.0L; i--, x *= 0.5L) + { + int exp = -9999; + long double mantissa = frexpl (x, &exp); + ASSERT (exp == i); + ASSERT (mantissa == -0.5L); + } + + for (i = 1, x = 1.01L; i <= LDBL_MAX_EXP; i++, x *= 2.0L) + { + int exp = -9999; + long double mantissa = frexpl (x, &exp); + ASSERT (exp == i); + ASSERT (mantissa == 0.505L); + } + for (i = 1, x = 1.01L; i >= LDBL_MIN_EXP; i--, x *= 0.5L) + { + int exp = -9999; + long double mantissa = frexpl (x, &exp); + ASSERT (exp == i); + ASSERT (mantissa == 0.505L); + } + for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L) + { + int exp = -9999; + long double mantissa = frexpl (x, &exp); + ASSERT (exp == i); + ASSERT (mantissa >= 0.5L); + ASSERT (mantissa < 1.0L); + ASSERT (mantissa == my_ldexp (x, - exp)); + } + + for (i = 1, x = 1.73205L; i <= LDBL_MAX_EXP; i++, x *= 2.0L) + { + int exp = -9999; + long double mantissa = frexpl (x, &exp); + ASSERT (exp == i); + ASSERT (mantissa == 0.866025L); + } + for (i = 1, x = 1.73205L; i >= LDBL_MIN_EXP; i--, x *= 0.5L) + { + int exp = -9999; + long double mantissa = frexpl (x, &exp); + ASSERT (exp == i); + ASSERT (mantissa == 0.866025L); + } + for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L) + { + int exp = -9999; + long double mantissa = frexpl (x, &exp); + ASSERT (exp == i || exp == i + 1); + ASSERT (mantissa >= 0.5L); + ASSERT (mantissa < 1.0L); + ASSERT (mantissa == my_ldexp (x, - exp)); + } + + return 0; +}