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.
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.
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.
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/>. */
26 const char *rd
, *rn
, *rz
, *ru
;
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" },
51 test_dec_in_one_mode (double d
, const char *fmt
, const char *expected
,
52 const char *mode_name
)
55 int ret
= snprintf (buf
, sizeof buf
, fmt
, d
);
56 if (ret
<= 0 || ret
>= (int) sizeof buf
)
58 printf ("snprintf for %a returned %d\n", d
, ret
);
61 if (strcmp (buf
, expected
) == 0)
65 printf ("snprintf (\"%s\", %a) returned \"%s\" not \"%s\" (%s)\n",
66 fmt
, d
, buf
, expected
, mode_name
);
75 const char *rd
[4], *rn
[4], *rz
[4], *ru
[4];
78 static const struct hex_test hex_tests
[] =
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" }
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" }
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" }
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" }
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" }
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" }
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" }
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" }
139 test_hex_in_one_mode (double d
, const char *fmt
, const char *expected
[4],
140 const char *mode_name
)
143 int ret
= snprintf (buf
, sizeof buf
, fmt
, d
);
144 if (ret
<= 0 || ret
>= (int) sizeof buf
)
146 printf ("snprintf for %a returned %d\n", d
, ret
);
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)
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],
167 int save_round_mode
= fegetround ();
170 for (size_t i
= 0; i
< sizeof (dec_tests
) / sizeof (dec_tests
[0]); i
++)
172 result
|= test_dec_in_one_mode (dec_tests
[i
].d
, dec_tests
[i
].fmt
,
173 dec_tests
[i
].rn
, "default rounding mode");
175 if (!fesetround (FE_DOWNWARD
))
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
);
183 if (!fesetround (FE_TOWARDZERO
))
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
);
191 if (!fesetround (FE_UPWARD
))
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
);
200 for (size_t i
= 0; i
< sizeof (hex_tests
) / sizeof (hex_tests
[0]); i
++)
202 result
|= test_hex_in_one_mode (hex_tests
[i
].d
, hex_tests
[i
].fmt
,
203 hex_tests
[i
].rn
, "default rounding mode");
205 if (!fesetround (FE_DOWNWARD
))
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
);
213 if (!fesetround (FE_TOWARDZERO
))
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
);
221 if (!fesetround (FE_UPWARD
))
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
);
233 #define TEST_FUNCTION do_test ()
234 #include "../test-skeleton.c"