Make printf respect the rounding mode for decimal output (bug 5044).
[platform/upstream/glibc.git] / stdio-common / tst-printf-round.c
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.
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 static int
72 do_test (void)
73 {
74   int save_round_mode = fegetround ();
75   int result = 0;
76
77   for (size_t i = 0; i < sizeof (dec_tests) / sizeof (dec_tests[0]); i++)
78     {
79       result |= test_dec_in_one_mode (dec_tests[i].d, dec_tests[i].fmt,
80                                       dec_tests[i].rn, "default rounding mode");
81 #ifdef FE_DOWNWARD
82       if (!fesetround (FE_DOWNWARD))
83         {
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);
87         }
88 #endif
89 #ifdef FE_TOWARDZERO
90       if (!fesetround (FE_TOWARDZERO))
91         {
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);
95         }
96 #endif
97 #ifdef FE_UPWARD
98       if (!fesetround (FE_UPWARD))
99         {
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);
103         }
104 #endif
105     }
106   return result;
107 }
108
109 #define TEST_FUNCTION do_test ()
110 #include "../test-skeleton.c"