1 /* Test for correct rounding of printf floating-point output.
2 Copyright (C) 2012 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
);
74 int save_round_mode
= fegetround ();
77 for (size_t i
= 0; i
< sizeof (dec_tests
) / sizeof (dec_tests
[0]); i
++)
79 result
|= test_dec_in_one_mode (dec_tests
[i
].d
, dec_tests
[i
].fmt
,
80 dec_tests
[i
].rn
, "default rounding mode");
82 if (!fesetround (FE_DOWNWARD
))
84 result
|= test_dec_in_one_mode (dec_tests
[i
].d
, dec_tests
[i
].fmt
,
85 dec_tests
[i
].rd
, "FE_DOWNWARD");
86 fesetround (save_round_mode
);
90 if (!fesetround (FE_TOWARDZERO
))
92 result
|= test_dec_in_one_mode (dec_tests
[i
].d
, dec_tests
[i
].fmt
,
93 dec_tests
[i
].rz
, "FE_TOWARDZERO");
94 fesetround (save_round_mode
);
98 if (!fesetround (FE_UPWARD
))
100 result
|= test_dec_in_one_mode (dec_tests
[i
].d
, dec_tests
[i
].fmt
,
101 dec_tests
[i
].ru
, "FE_UPWARD");
102 fesetround (save_round_mode
);
109 #define TEST_FUNCTION do_test ()
110 #include "../test-skeleton.c"