Fix some warnings in the absence of FP round/exception support
[platform/upstream/glibc.git] / stdio-common / tst-printf-round.c
1 /* Test for correct rounding of printf floating-point output.
2    Copyright (C) 2012-2014 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <http://www.gnu.org/licenses/>.  */
18
19 #include <fenv.h>
20 #include <stdio.h>
21 #include <string.h>
22
23 struct dec_test {
24   double d;
25   const char *fmt;
26   const char *rd, *rn, *rz, *ru;
27 };
28
29 static const struct dec_test dec_tests[] = {
30   { 1.5, "%.0f", "1", "2", "1", "2" },
31   { -1.5, "%.0f", "-2", "-2", "-1", "-1" },
32   { 2.5, "%.0f", "2", "2", "2", "3" },
33   { -2.5, "%.0f", "-3", "-2", "-2", "-2" },
34   { 1.4999, "%.0f", "1", "1", "1", "2" },
35   { -1.4999, "%.0f", "-2", "-1", "-1", "-1" },
36   { 1.5001, "%.0f", "1", "2", "1", "2" },
37   { -1.5001, "%.0f", "-2", "-2", "-1", "-1" },
38   { 2.4999, "%.0f", "2", "2", "2", "3" },
39   { -2.4999, "%.0f", "-3", "-2", "-2", "-2" },
40   { 2.5001, "%.0f", "2", "3", "2", "3" },
41   { -2.5001, "%.0f", "-3", "-3", "-2", "-2" },
42   { 1.0 / 3.0, "%f", "0.333333", "0.333333", "0.333333", "0.333334" },
43   { -1.0 / 3.0, "%f", "-0.333334", "-0.333333", "-0.333333", "-0.333333" },
44   { 0.2500001, "%.2e", "2.50e-01", "2.50e-01", "2.50e-01", "2.51e-01" },
45   { -0.2500001, "%.2e", "-2.51e-01", "-2.50e-01", "-2.50e-01", "-2.50e-01" },
46   { 1000001.0, "%.1e", "1.0e+06", "1.0e+06", "1.0e+06", "1.1e+06" },
47   { -1000001.0, "%.1e", "-1.1e+06", "-1.0e+06", "-1.0e+06", "-1.0e+06" },
48 };
49
50 static int
51 test_dec_in_one_mode (double d, const char *fmt, const char *expected,
52                       const char *mode_name)
53 {
54   char buf[100];
55   int ret = snprintf (buf, sizeof buf, fmt, d);
56   if (ret <= 0 || ret >= (int) sizeof buf)
57     {
58       printf ("snprintf for %a returned %d\n", d, ret);
59       return 1;
60     }
61   if (strcmp (buf, expected) == 0)
62     return 0;
63   else
64     {
65       printf ("snprintf (\"%s\", %a) returned \"%s\" not \"%s\" (%s)\n",
66               fmt, d, buf, expected, mode_name);
67       return 1;
68     }
69 }
70
71 struct hex_test
72 {
73   double d;
74   const char *fmt;
75   const char *rd[4], *rn[4], *rz[4], *ru[4];
76 };
77
78 static const struct hex_test hex_tests[] =
79   {
80     {
81       0x1.fffffp+4, "%.1a",
82       { "0x1.fp+4", "0x3.fp+3", "0x7.fp+2", "0xf.fp+1" },
83       { "0x2.0p+4", "0x4.0p+3", "0x8.0p+2", "0x1.0p+5" },
84       { "0x1.fp+4", "0x3.fp+3", "0x7.fp+2", "0xf.fp+1" },
85       { "0x2.0p+4", "0x4.0p+3", "0x8.0p+2", "0x1.0p+5" }
86     },
87     {
88       -0x1.fffffp+4, "%.1a",
89       { "-0x2.0p+4", "-0x4.0p+3", "-0x8.0p+2", "-0x1.0p+5" },
90       { "-0x2.0p+4", "-0x4.0p+3", "-0x8.0p+2", "-0x1.0p+5" },
91       { "-0x1.fp+4", "-0x3.fp+3", "-0x7.fp+2", "-0xf.fp+1" },
92       { "-0x1.fp+4", "-0x3.fp+3", "-0x7.fp+2", "-0xf.fp+1" }
93     },
94     {
95       0x1.88p+4, "%.1a",
96       { "0x1.8p+4", "0x3.1p+3", "0x6.2p+2", "0xc.4p+1" },
97       { "0x1.8p+4", "0x3.1p+3", "0x6.2p+2", "0xc.4p+1" },
98       { "0x1.8p+4", "0x3.1p+3", "0x6.2p+2", "0xc.4p+1" },
99       { "0x1.9p+4", "0x3.1p+3", "0x6.2p+2", "0xc.4p+1" }
100     },
101     {
102       -0x1.88p+4, "%.1a",
103       { "-0x1.9p+4", "-0x3.1p+3", "-0x6.2p+2", "-0xc.4p+1" },
104       { "-0x1.8p+4", "-0x3.1p+3", "-0x6.2p+2", "-0xc.4p+1" },
105       { "-0x1.8p+4", "-0x3.1p+3", "-0x6.2p+2", "-0xc.4p+1" },
106       { "-0x1.8p+4", "-0x3.1p+3", "-0x6.2p+2", "-0xc.4p+1" }
107     },
108     {
109       0x1.78p+4, "%.1a",
110       { "0x1.7p+4", "0x2.fp+3", "0x5.ep+2", "0xb.cp+1" },
111       { "0x1.8p+4", "0x2.fp+3", "0x5.ep+2", "0xb.cp+1" },
112       { "0x1.7p+4", "0x2.fp+3", "0x5.ep+2", "0xb.cp+1" },
113       { "0x1.8p+4", "0x2.fp+3", "0x5.ep+2", "0xb.cp+1" }
114     },
115     {
116       -0x1.78p+4, "%.1a",
117       { "-0x1.8p+4", "-0x2.fp+3", "-0x5.ep+2", "-0xb.cp+1" },
118       { "-0x1.8p+4", "-0x2.fp+3", "-0x5.ep+2", "-0xb.cp+1" },
119       { "-0x1.7p+4", "-0x2.fp+3", "-0x5.ep+2", "-0xb.cp+1" },
120       { "-0x1.7p+4", "-0x2.fp+3", "-0x5.ep+2", "-0xb.cp+1" }
121     },
122     {
123       64.0 / 3.0, "%.1a",
124       { "0x1.5p+4", "0x2.ap+3", "0x5.5p+2", "0xa.ap+1" },
125       { "0x1.5p+4", "0x2.bp+3", "0x5.5p+2", "0xa.bp+1" },
126       { "0x1.5p+4", "0x2.ap+3", "0x5.5p+2", "0xa.ap+1" },
127       { "0x1.6p+4", "0x2.bp+3", "0x5.6p+2", "0xa.bp+1" }
128     },
129     {
130       -64.0 / 3.0, "%.1a",
131       { "-0x1.6p+4", "-0x2.bp+3", "-0x5.6p+2", "-0xa.bp+1" },
132       { "-0x1.5p+4", "-0x2.bp+3", "-0x5.5p+2", "-0xa.bp+1" },
133       { "-0x1.5p+4", "-0x2.ap+3", "-0x5.5p+2", "-0xa.ap+1" },
134       { "-0x1.5p+4", "-0x2.ap+3", "-0x5.5p+2", "-0xa.ap+1" }
135     },
136   };
137
138 static int
139 test_hex_in_one_mode (double d, const char *fmt, const char *const expected[4],
140                       const char *mode_name)
141 {
142   char buf[100];
143   int ret = snprintf (buf, sizeof buf, fmt, d);
144   if (ret <= 0 || ret >= (int) sizeof buf)
145     {
146       printf ("snprintf for %a returned %d\n", d, ret);
147       return 1;
148     }
149   if (strcmp (buf, expected[0]) == 0
150       || strcmp (buf, expected[1]) == 0
151       || strcmp (buf, expected[2]) == 0
152       || strcmp (buf, expected[3]) == 0)
153     return 0;
154   else
155     {
156       printf ("snprintf (\"%s\", %a) returned \"%s\" not "
157               "\"%s\" or \"%s\" or \"%s\" or \"%s\" (%s)\n",
158               fmt, d, buf, expected[0], expected[1], expected[2], expected[3],
159               mode_name);
160       return 1;
161     }
162 }
163
164 static int
165 do_test (void)
166 {
167   int save_round_mode __attribute__ ((unused)) = fegetround ();
168   int result = 0;
169
170   for (size_t i = 0; i < sizeof (dec_tests) / sizeof (dec_tests[0]); i++)
171     {
172       result |= test_dec_in_one_mode (dec_tests[i].d, dec_tests[i].fmt,
173                                       dec_tests[i].rn, "default rounding mode");
174 #ifdef FE_DOWNWARD
175       if (!fesetround (FE_DOWNWARD))
176         {
177           result |= test_dec_in_one_mode (dec_tests[i].d, dec_tests[i].fmt,
178                                           dec_tests[i].rd, "FE_DOWNWARD");
179           fesetround (save_round_mode);
180         }
181 #endif
182 #ifdef FE_TOWARDZERO
183       if (!fesetround (FE_TOWARDZERO))
184         {
185           result |= test_dec_in_one_mode (dec_tests[i].d, dec_tests[i].fmt,
186                                           dec_tests[i].rz, "FE_TOWARDZERO");
187           fesetround (save_round_mode);
188         }
189 #endif
190 #ifdef FE_UPWARD
191       if (!fesetround (FE_UPWARD))
192         {
193           result |= test_dec_in_one_mode (dec_tests[i].d, dec_tests[i].fmt,
194                                           dec_tests[i].ru, "FE_UPWARD");
195           fesetround (save_round_mode);
196         }
197 #endif
198     }
199
200   for (size_t i = 0; i < sizeof (hex_tests) / sizeof (hex_tests[0]); i++)
201     {
202       result |= test_hex_in_one_mode (hex_tests[i].d, hex_tests[i].fmt,
203                                       hex_tests[i].rn, "default rounding mode");
204 #ifdef FE_DOWNWARD
205       if (!fesetround (FE_DOWNWARD))
206         {
207           result |= test_hex_in_one_mode (hex_tests[i].d, hex_tests[i].fmt,
208                                           hex_tests[i].rd, "FE_DOWNWARD");
209           fesetround (save_round_mode);
210         }
211 #endif
212 #ifdef FE_TOWARDZERO
213       if (!fesetround (FE_TOWARDZERO))
214         {
215           result |= test_hex_in_one_mode (hex_tests[i].d, hex_tests[i].fmt,
216                                           hex_tests[i].rz, "FE_TOWARDZERO");
217           fesetround (save_round_mode);
218         }
219 #endif
220 #ifdef FE_UPWARD
221       if (!fesetround (FE_UPWARD))
222         {
223           result |= test_hex_in_one_mode (hex_tests[i].d, hex_tests[i].fmt,
224                                           hex_tests[i].ru, "FE_UPWARD");
225           fesetround (save_round_mode);
226         }
227 #endif
228     }
229
230   return result;
231 }
232
233 #define TEST_FUNCTION do_test ()
234 #include "../test-skeleton.c"