From: Bruno Haible Date: Sun, 25 Feb 2007 14:29:34 +0000 (+0000) Subject: Tests for module 'printf-frexpl'. X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f6183e64302b3af947cdd932049180b5a32f6443;hp=e4bed0733ad7c8e2b0bda1f77cf52e0d32c5e4f9;p=pspp Tests for module 'printf-frexpl'. --- diff --git a/ChangeLog b/ChangeLog index 67cc44824b..93910c17f4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,8 @@ 2007-02-25 Bruno Haible + * modules/printf-frexpl-tests: New file. + * tests/test-printf-frexpl.c: New file. + * modules/printf-frexpl: New file. * lib/printf-frexpl.h: New file. * lib/printf-frexpl.c: New file. diff --git a/modules/printf-frexpl-tests b/modules/printf-frexpl-tests new file mode 100644 index 0000000000..ab58971c0d --- /dev/null +++ b/modules/printf-frexpl-tests @@ -0,0 +1,13 @@ +Files: +tests/test-printf-frexpl.c + +Depends-on: + +configure.ac: + +Makefile.am: +TESTS += test-printf-frexpl +check_PROGRAMS += test-printf-frexpl + +License: +LGPL diff --git a/tests/test-printf-frexpl.c b/tests/test-printf-frexpl.c new file mode 100644 index 0000000000..32c92e8f30 --- /dev/null +++ b/tests/test-printf-frexpl.c @@ -0,0 +1,116 @@ +/* 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 , 2007. */ + +#include + +#include "printf-frexpl.h" + +#include +#include + +#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; + + for (i = 1, x = 1.0L; i <= LDBL_MAX_EXP; i++, x *= 2.0L) + { + int exp = -9999; + long double mantissa = printf_frexpl (x, &exp); + ASSERT (exp == i - 1); + ASSERT (mantissa == 1.0L); + } + for (i = 1, x = 1.0L; i >= LDBL_MIN_EXP; i--, x *= 0.5L) + { + int exp = -9999; + long double mantissa = printf_frexpl (x, &exp); + ASSERT (exp == i - 1); + ASSERT (mantissa == 1.0L); + } + for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L) + { + int exp = -9999; + long double mantissa = printf_frexpl (x, &exp); + ASSERT (exp == LDBL_MIN_EXP - 1); + ASSERT (mantissa == my_ldexp (1.0L, i - LDBL_MIN_EXP)); + } + + for (i = 1, x = 1.01L; i <= LDBL_MAX_EXP; i++, x *= 2.0L) + { + int exp = -9999; + long double mantissa = printf_frexpl (x, &exp); + ASSERT (exp == i - 1); + ASSERT (mantissa == 1.01L); + } + for (i = 1, x = 1.01L; i >= LDBL_MIN_EXP; i--, x *= 0.5L) + { + int exp = -9999; + long double mantissa = printf_frexpl (x, &exp); + ASSERT (exp == i - 1); + ASSERT (mantissa == 1.01L); + } + for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L) + { + int exp = -9999; + long double mantissa = printf_frexpl (x, &exp); + ASSERT (exp == LDBL_MIN_EXP - 1); + ASSERT (mantissa >= my_ldexp (1.0L, i - LDBL_MIN_EXP)); + ASSERT (mantissa <= my_ldexp (2.0L, i - LDBL_MIN_EXP)); + 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 = printf_frexpl (x, &exp); + ASSERT (exp == i - 1); + ASSERT (mantissa == 1.73205L); + } + for (i = 1, x = 1.73205L; i >= LDBL_MIN_EXP; i--, x *= 0.5L) + { + int exp = -9999; + long double mantissa = printf_frexpl (x, &exp); + ASSERT (exp == i - 1); + ASSERT (mantissa == 1.73205L); + } + for (; i >= LDBL_MIN_EXP - 100 && x > 0.0L; i--, x *= 0.5L) + { + int exp = -9999; + long double mantissa = printf_frexpl (x, &exp); + ASSERT (exp == LDBL_MIN_EXP - 1); + ASSERT (mantissa >= my_ldexp (1.0L, i - LDBL_MIN_EXP)); + ASSERT (mantissa <= my_ldexp (2.0L, i - LDBL_MIN_EXP)); + ASSERT (mantissa == my_ldexp (x, - exp)); + } + + return 0; +}