Back to index

glibc  2.9
basic-test.c
Go to the documentation of this file.
00001 /* Copyright (C) 1999, 2007 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Andreas Jaeger <aj@suse.de>, 1999.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <math.h>
00021 #include <float.h>
00022 #include <stdio.h>
00023 
00024 static int errors = 0;
00025 
00026 
00027 static void
00028 check (const char *testname, int result)
00029 {
00030   if (!result) {
00031     printf ("Failure: %s\n", testname);
00032     errors++;
00033   }
00034 }
00035 
00036 #define TEST_FUNC(NAME, FLOAT, NANFUNC, EPSILON, HUGEVAL) \
00037 static void                                                          \
00038 NAME (void)                                                          \
00039 {                                                                    \
00040   /* Variables are declared volatile to forbid some compiler                \
00041      optimizations.  */                                                     \
00042   volatile FLOAT Inf_var, NaN_var, zero_var, one_var;                       \
00043   FLOAT x1, x2;                                                             \
00044                                                                      \
00045   zero_var = 0.0;                                                    \
00046   one_var = 1.0;                                                     \
00047   NaN_var = zero_var / zero_var;                                     \
00048   Inf_var = one_var / zero_var;                                             \
00049                                                                      \
00050   (void) &zero_var;                                                  \
00051   (void) &one_var;                                                   \
00052   (void) &NaN_var;                                                   \
00053   (void) &Inf_var;                                                   \
00054                                                                      \
00055                                                                      \
00056   check (#FLOAT " isinf (inf) == 1", isinf (Inf_var) == 1);                 \
00057   check (#FLOAT " isinf (-inf) == -1", isinf (-Inf_var) == -1);                    \
00058   check (#FLOAT " !isinf (1)", !(isinf (one_var)));                         \
00059   check (#FLOAT " !isinf (NaN)", !(isinf (NaN_var)));                       \
00060                                                                      \
00061   check (#FLOAT " isnan (NaN)", isnan (NaN_var));                           \
00062   check (#FLOAT " isnan (-NaN)", isnan (-NaN_var));                         \
00063   check (#FLOAT " !isnan (1)", !(isnan (one_var)));                         \
00064   check (#FLOAT " !isnan (inf)", !(isnan (Inf_var)));                       \
00065                                                                      \
00066   check (#FLOAT " inf == inf", Inf_var == Inf_var);                         \
00067   check (#FLOAT " -inf == -inf", -Inf_var == -Inf_var);                     \
00068   check (#FLOAT " inf != -inf", Inf_var != -Inf_var);                       \
00069   check (#FLOAT " NaN != NaN", NaN_var != NaN_var);                         \
00070                                                                      \
00071   /*                                                                 \
00072      the same tests but this time with NAN from <bits/nan.h>                \
00073      NAN is a double const                                           \
00074    */                                                                \
00075   check (#FLOAT " isnan (NAN)", isnan (NAN));                               \
00076   check (#FLOAT " isnan (-NAN)", isnan (-NAN));                             \
00077   check (#FLOAT " !isinf (NAN)", !(isinf (NAN)));                           \
00078   check (#FLOAT " !isinf (-NAN)", !(isinf (-NAN)));                         \
00079   check (#FLOAT " NAN != NAN", NAN != NAN);                                 \
00080                                                                      \
00081   /*                                                                 \
00082      And again with the value returned by the `nan' function.               \
00083    */                                                                \
00084   check (#FLOAT " isnan (NAN)", isnan (NANFUNC ("")));                      \
00085   check (#FLOAT " isnan (-NAN)", isnan (-NANFUNC ("")));                    \
00086   check (#FLOAT " !isinf (NAN)", !(isinf (NANFUNC (""))));                  \
00087   check (#FLOAT " !isinf (-NAN)", !(isinf (-NANFUNC (""))));                \
00088   check (#FLOAT " NAN != NAN", NANFUNC ("") != NANFUNC (""));               \
00089                                                                      \
00090   /* test if EPSILON is ok */                                               \
00091   x1 = 1.0;                                                          \
00092   x2 = x1 + EPSILON;                                                 \
00093   check (#FLOAT " 1 != 1+EPSILON", x1 != x2);                               \
00094                                                                      \
00095   x1 = 1.0;                                                          \
00096   x2 = x1 - EPSILON;                                                 \
00097   check (#FLOAT " 1 != 1-EPSILON", x1 != x2);                               \
00098                                                                      \
00099   /* test if HUGE_VALx is ok */                                             \
00100   x1 = HUGEVAL;                                                             \
00101   check (#FLOAT " isinf (HUGE_VALx) == +1", isinf (x1) == +1);              \
00102   x1 = - HUGEVAL;                                                    \
00103   check (#FLOAT " isinf (-HUGE_VALx) == -1", isinf (x1) == -1);                    \
00104 }
00105 
00106 #define TEST_TRUNC(NAME, FLOAT, DOUBLE) \
00107 void                                                                 \
00108 NAME (void)                                                          \
00109 {                                                                    \
00110   volatile DOUBLE Inf_var, NaN_var, zero_var, one_var;                      \
00111   FLOAT x1, x2;                                                             \
00112                                                                      \
00113   zero_var = 0.0;                                                    \
00114   one_var = 1.0;                                                     \
00115   NaN_var = zero_var / zero_var;                                     \
00116   Inf_var = one_var / zero_var;                                             \
00117                                                                      \
00118   (void) &NaN_var;                                                   \
00119   (void) &Inf_var;                                                   \
00120                                                                      \
00121   x1 = (FLOAT) NaN_var;                                                     \
00122   check (" "#FLOAT" x = ("#FLOAT") ("#DOUBLE") NaN", isnan (x1) != 0);             \
00123   x2 = (FLOAT) Inf_var;                                                     \
00124   check (" "#FLOAT" x = ("#FLOAT") ("#DOUBLE") Inf", isinf (x2) != 0);             \
00125 }
00126 
00127 TEST_FUNC (float_test, float, nanf, FLT_EPSILON, HUGE_VALF)
00128 TEST_FUNC (double_test, double, nan, DBL_EPSILON, HUGE_VAL)
00129 TEST_TRUNC (truncdfsf_test, float, double)
00130 #ifndef NO_LONG_DOUBLE
00131 TEST_FUNC (ldouble_test, long double, nanl, LDBL_EPSILON, HUGE_VALL)
00132 TEST_TRUNC (trunctfsf_test, float, long double)
00133 TEST_TRUNC (trunctfdf_test, double, long double)
00134 #endif
00135 
00136 int
00137 do_test (void)
00138 {
00139   float_test ();
00140   double_test ();
00141   truncdfsf_test();
00142 
00143 #ifndef NO_LONG_DOUBLE
00144   ldouble_test ();
00145   trunctfsf_test();
00146   trunctfdf_test();
00147 #endif
00148 
00149   return errors != 0;
00150 }
00151 
00152 #define TEST_FUNCTION do_test ()
00153 #include "../test-skeleton.c"