1 /* Test for correct rounding of printf floating-point output.
2 Copyright (C) 2012-2019 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 <https://www.gnu.org/licenses/>. */
19 #include <array_length.h>
27 const char *rd
, *rn
, *rz
, *ru
;
30 static const struct dec_test dec_tests
[] = {
31 { 1.5, "%.0f", "1", "2", "1", "2" },
32 { -1.5, "%.0f", "-2", "-2", "-1", "-1" },
33 { 2.5, "%.0f", "2", "2", "2", "3" },
34 { -2.5, "%.0f", "-3", "-2", "-2", "-2" },
35 { 1.4999, "%.0f", "1", "1", "1", "2" },
36 { -1.4999, "%.0f", "-2", "-1", "-1", "-1" },
37 { 1.5001, "%.0f", "1", "2", "1", "2" },
38 { -1.5001, "%.0f", "-2", "-2", "-1", "-1" },
39 { 2.4999, "%.0f", "2", "2", "2", "3" },
40 { -2.4999, "%.0f", "-3", "-2", "-2", "-2" },
41 { 2.5001, "%.0f", "2", "3", "2", "3" },
42 { -2.5001, "%.0f", "-3", "-3", "-2", "-2" },
43 { 1.0 / 3.0, "%f", "0.333333", "0.333333", "0.333333", "0.333334" },
44 { -1.0 / 3.0, "%f", "-0.333334", "-0.333333", "-0.333333", "-0.333333" },
45 { 0.2500001, "%.2e", "2.50e-01", "2.50e-01", "2.50e-01", "2.51e-01" },
46 { -0.2500001, "%.2e", "-2.51e-01", "-2.50e-01", "-2.50e-01", "-2.50e-01" },
47 { 1000001.0, "%.1e", "1.0e+06", "1.0e+06", "1.0e+06", "1.1e+06" },
48 { -1000001.0, "%.1e", "-1.1e+06", "-1.0e+06", "-1.0e+06", "-1.0e+06" },
52 test_dec_in_one_mode (double d
, const char *fmt
, const char *expected
,
53 const char *mode_name
)
56 int ret
= snprintf (buf
, sizeof buf
, fmt
, d
);
57 if (ret
<= 0 || ret
>= (int) sizeof buf
)
59 printf ("snprintf for %a returned %d\n", d
, ret
);
62 if (strcmp (buf
, expected
) == 0)
66 printf ("snprintf (\"%s\", %a) returned \"%s\" not \"%s\" (%s)\n",
67 fmt
, d
, buf
, expected
, mode_name
);
76 const char *rd
[4], *rn
[4], *rz
[4], *ru
[4];
79 static const struct hex_test hex_tests
[] =
83 { "0x1.fp+4", "0x3.fp+3", "0x7.fp+2", "0xf.fp+1" },
84 { "0x2.0p+4", "0x4.0p+3", "0x8.0p+2", "0x1.0p+5" },
85 { "0x1.fp+4", "0x3.fp+3", "0x7.fp+2", "0xf.fp+1" },
86 { "0x2.0p+4", "0x4.0p+3", "0x8.0p+2", "0x1.0p+5" }
89 -0x1.fffffp
+4, "%.1a",
90 { "-0x2.0p+4", "-0x4.0p+3", "-0x8.0p+2", "-0x1.0p+5" },
91 { "-0x2.0p+4", "-0x4.0p+3", "-0x8.0p+2", "-0x1.0p+5" },
92 { "-0x1.fp+4", "-0x3.fp+3", "-0x7.fp+2", "-0xf.fp+1" },
93 { "-0x1.fp+4", "-0x3.fp+3", "-0x7.fp+2", "-0xf.fp+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.8p+4", "0x3.1p+3", "0x6.2p+2", "0xc.4p+1" },
100 { "0x1.9p+4", "0x3.1p+3", "0x6.2p+2", "0xc.4p+1" }
104 { "-0x1.9p+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 { "-0x1.8p+4", "-0x3.1p+3", "-0x6.2p+2", "-0xc.4p+1" }
111 { "0x1.7p+4", "0x2.fp+3", "0x5.ep+2", "0xb.cp+1" },
112 { "0x1.8p+4", "0x2.fp+3", "0x5.ep+2", "0xb.cp+1" },
113 { "0x1.7p+4", "0x2.fp+3", "0x5.ep+2", "0xb.cp+1" },
114 { "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.8p+4", "-0x2.fp+3", "-0x5.ep+2", "-0xb.cp+1" },
120 { "-0x1.7p+4", "-0x2.fp+3", "-0x5.ep+2", "-0xb.cp+1" },
121 { "-0x1.7p+4", "-0x2.fp+3", "-0x5.ep+2", "-0xb.cp+1" }
125 { "0x1.5p+4", "0x2.ap+3", "0x5.5p+2", "0xa.ap+1" },
126 { "0x1.5p+4", "0x2.bp+3", "0x5.5p+2", "0xa.bp+1" },
127 { "0x1.5p+4", "0x2.ap+3", "0x5.5p+2", "0xa.ap+1" },
128 { "0x1.6p+4", "0x2.bp+3", "0x5.6p+2", "0xa.bp+1" }
132 { "-0x1.6p+4", "-0x2.bp+3", "-0x5.6p+2", "-0xa.bp+1" },
133 { "-0x1.5p+4", "-0x2.bp+3", "-0x5.5p+2", "-0xa.bp+1" },
134 { "-0x1.5p+4", "-0x2.ap+3", "-0x5.5p+2", "-0xa.ap+1" },
135 { "-0x1.5p+4", "-0x2.ap+3", "-0x5.5p+2", "-0xa.ap+1" }
140 test_hex_in_one_mode (double d
, const char *fmt
, const char *const expected
[4],
141 const char *mode_name
)
144 int ret
= snprintf (buf
, sizeof buf
, fmt
, d
);
145 if (ret
<= 0 || ret
>= (int) sizeof buf
)
147 printf ("snprintf for %a returned %d\n", d
, ret
);
150 if (strcmp (buf
, expected
[0]) == 0
151 || strcmp (buf
, expected
[1]) == 0
152 || strcmp (buf
, expected
[2]) == 0
153 || strcmp (buf
, expected
[3]) == 0)
157 printf ("snprintf (\"%s\", %a) returned \"%s\" not "
158 "\"%s\" or \"%s\" or \"%s\" or \"%s\" (%s)\n",
159 fmt
, d
, buf
, expected
[0], expected
[1], expected
[2], expected
[3],
168 int save_round_mode
__attribute__ ((unused
)) = fegetround ();
171 for (size_t i
= 0; i
< array_length (dec_tests
); i
++)
173 result
|= test_dec_in_one_mode (dec_tests
[i
].d
, dec_tests
[i
].fmt
,
174 dec_tests
[i
].rn
, "default rounding mode");
176 if (!fesetround (FE_DOWNWARD
))
178 result
|= test_dec_in_one_mode (dec_tests
[i
].d
, dec_tests
[i
].fmt
,
179 dec_tests
[i
].rd
, "FE_DOWNWARD");
180 fesetround (save_round_mode
);
184 if (!fesetround (FE_TOWARDZERO
))
186 result
|= test_dec_in_one_mode (dec_tests
[i
].d
, dec_tests
[i
].fmt
,
187 dec_tests
[i
].rz
, "FE_TOWARDZERO");
188 fesetround (save_round_mode
);
192 if (!fesetround (FE_UPWARD
))
194 result
|= test_dec_in_one_mode (dec_tests
[i
].d
, dec_tests
[i
].fmt
,
195 dec_tests
[i
].ru
, "FE_UPWARD");
196 fesetround (save_round_mode
);
201 for (size_t i
= 0; i
< array_length (hex_tests
); i
++)
203 result
|= test_hex_in_one_mode (hex_tests
[i
].d
, hex_tests
[i
].fmt
,
204 hex_tests
[i
].rn
, "default rounding mode");
206 if (!fesetround (FE_DOWNWARD
))
208 result
|= test_hex_in_one_mode (hex_tests
[i
].d
, hex_tests
[i
].fmt
,
209 hex_tests
[i
].rd
, "FE_DOWNWARD");
210 fesetround (save_round_mode
);
214 if (!fesetround (FE_TOWARDZERO
))
216 result
|= test_hex_in_one_mode (hex_tests
[i
].d
, hex_tests
[i
].fmt
,
217 hex_tests
[i
].rz
, "FE_TOWARDZERO");
218 fesetround (save_round_mode
);
222 if (!fesetround (FE_UPWARD
))
224 result
|= test_hex_in_one_mode (hex_tests
[i
].d
, hex_tests
[i
].fmt
,
225 hex_tests
[i
].ru
, "FE_UPWARD");
226 fesetround (save_round_mode
);
234 #define TEST_FUNCTION do_test ()
235 #include "../test-skeleton.c"