Flush the standard error stream before aborting.
[gnulib.git] / tests / test-printf-frexp.c
1 /* Test of splitting a double into fraction and mantissa.
2    Copyright (C) 2007-2008 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
18
19 #include <config.h>
20
21 #include "printf-frexp.h"
22
23 #include <float.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26
27 #define ASSERT(expr) \
28   do                                                                         \
29     {                                                                        \
30       if (!(expr))                                                           \
31         {                                                                    \
32           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
33           fflush (stderr);                                                   \
34           abort ();                                                          \
35         }                                                                    \
36     }                                                                        \
37   while (0)
38
39 static double
40 my_ldexp (double x, int d)
41 {
42   for (; d > 0; d--)
43     x *= 2.0;
44   for (; d < 0; d++)
45     x *= 0.5;
46   return x;
47 }
48
49 int
50 main ()
51 {
52   int i;
53   /* The use of 'volatile' guarantees that excess precision bits are dropped
54      when dealing with denormalized numbers.  It is necessary on x86 systems
55      where double-floats are not IEEE compliant by default, to avoid that the
56      results become platform and compiler option dependent.  'volatile' is a
57      portable alternative to gcc's -ffloat-store option.  */
58   volatile double x;
59
60   for (i = 1, x = 1.0; i <= DBL_MAX_EXP; i++, x *= 2.0)
61     {
62       int exp = -9999;
63       double mantissa = printf_frexp (x, &exp);
64       ASSERT (exp == i - 1);
65       ASSERT (mantissa == 1.0);
66     }
67   for (i = 1, x = 1.0; i >= DBL_MIN_EXP; i--, x *= 0.5)
68     {
69       int exp = -9999;
70       double mantissa = printf_frexp (x, &exp);
71       ASSERT (exp == i - 1);
72       ASSERT (mantissa == 1.0);
73     }
74   for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
75     {
76       int exp = -9999;
77       double mantissa = printf_frexp (x, &exp);
78       ASSERT (exp == DBL_MIN_EXP - 1);
79       ASSERT (mantissa == my_ldexp (1.0, i - DBL_MIN_EXP));
80     }
81
82   for (i = 1, x = 1.01; i <= DBL_MAX_EXP; i++, x *= 2.0)
83     {
84       int exp = -9999;
85       double mantissa = printf_frexp (x, &exp);
86       ASSERT (exp == i - 1);
87       ASSERT (mantissa == 1.01);
88     }
89   for (i = 1, x = 1.01; i >= DBL_MIN_EXP; i--, x *= 0.5)
90     {
91       int exp = -9999;
92       double mantissa = printf_frexp (x, &exp);
93       ASSERT (exp == i - 1);
94       ASSERT (mantissa == 1.01);
95     }
96   for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
97     {
98       int exp = -9999;
99       double mantissa = printf_frexp (x, &exp);
100       ASSERT (exp == DBL_MIN_EXP - 1);
101       ASSERT (mantissa >= my_ldexp (1.0, i - DBL_MIN_EXP));
102       ASSERT (mantissa <= my_ldexp (2.0, i - DBL_MIN_EXP));
103       ASSERT (mantissa == my_ldexp (x, - exp));
104     }
105
106   for (i = 1, x = 1.73205; i <= DBL_MAX_EXP; i++, x *= 2.0)
107     {
108       int exp = -9999;
109       double mantissa = printf_frexp (x, &exp);
110       ASSERT (exp == i - 1);
111       ASSERT (mantissa == 1.73205);
112     }
113   for (i = 1, x = 1.73205; i >= DBL_MIN_EXP; i--, x *= 0.5)
114     {
115       int exp = -9999;
116       double mantissa = printf_frexp (x, &exp);
117       ASSERT (exp == i - 1);
118       ASSERT (mantissa == 1.73205);
119     }
120   for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5)
121     {
122       int exp = -9999;
123       double mantissa = printf_frexp (x, &exp);
124       ASSERT (exp == DBL_MIN_EXP - 1);
125       ASSERT (mantissa >= my_ldexp (1.0, i - DBL_MIN_EXP));
126       ASSERT (mantissa <= my_ldexp (2.0, i - DBL_MIN_EXP));
127       ASSERT (mantissa == my_ldexp (x, - exp));
128     }
129
130   return 0;
131 }