]> git.ipfire.org Git - thirdparty/glibc.git/blob - stdlib/tst-strtod-round.c
Refactor tst-strtod-round.c for type-generic-ness
[thirdparty/glibc.git] / stdlib / tst-strtod-round.c
1 /* Test for correct rounding of results of strtod and related
2 functions.
3 Copyright (C) 2012-2016 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
19
20 /* Defining _LIBC_TEST ensures long double math functions are
21 declared in the headers. */
22 #define _LIBC_TEST 1
23 #include <fenv.h>
24 #include <float.h>
25 #include <math.h>
26 #include <stdbool.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <math-tests.h>
31
32 #include "tst-strtod.h"
33
34 #define _CONCAT(a, b) a ## b
35 #define CONCAT(a, b) _CONCAT (a, b)
36
37 #if LDBL_MANT_DIG == 106 && LDBL_MAX_EXP == 1024
38 /* This is a stupid hack for IBM long double. This test ignores
39 inexact values for long double due to the limitations of the
40 format. This ensures rounding tests are ignored. */
41 # undef ROUNDING_TESTS_long_double
42 # define ROUNDING_TESTS_long_double(x) 0
43 #endif
44
45 /* Generator to create an FTYPE member variabled named FSUF
46 used to populate struct member variables. */
47 #define FTYPE_MEMBER(FSUF, FTYPE, FTOSTR, FTOSTRM, LSUF, CSUF) \
48 FTYPE FSUF;
49
50 /* Likewise, but each member is of type bool. */
51 #define BOOL_MEMBER(FSUF, FTYPE, FTOSTR, FTOSTRM, LSUF, CSUF) \
52 bool FSUF;
53
54 #define STRUCT_FOREACH_FLOAT_FTYPE GEN_TEST_STRTOD_FOREACH (FTYPE_MEMBER)
55 #define STRUCT_FOREACH_FLOAT_BOOL GEN_TEST_STRTOD_FOREACH (BOOL_MEMBER)
56
57 /* Define the long double choose (CHOOSE_ld) macro
58 to select the appropriate generated long double
59 value from the generated test data. */
60 #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
61 /* This is for the long double == double format. */
62 # define CHOOSE_ld(f,d,...) d
63 #elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 && LDBL_MIN_EXP == -16381
64 /* This is for the Intel extended float format. */
65 # define CHOOSE_ld(f,d,ld64i,...) ld64i
66 #elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384 && LDBL_MIN_EXP == -16382
67 /* This is for the Motorola extended float format. */
68 # define CHOOSE_ld(f,d,ld64i,ld64m,...) ld64m
69 #elif LDBL_MANT_DIG == 106 && LDBL_MAX_EXP == 1024
70 /* This is for the IBM extended double format. */
71 # define CHOOSE_ld(f,d,ld64i,ld64m,ld106,...) ld106
72 #elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384
73 /* This is for the IEEE binary128 format. */
74 # define CHOOSE_ld(f,d,ld64i,ld64m,ld106,ld113,...) ld113
75 #else
76 # error "unknown long double format"
77 #endif
78
79 /* Add type specific choosing macros below. */
80 #define CHOOSE_f(f,...) f
81 #define CHOOSE_d(f,d,...) d
82 /* long double is special, and handled above. */
83
84 /* Selector for expected result field of a given type. */
85 #define _ENTRY(FSUF, FTYPE, FTOSTR, FTOSTRM, LSUF, CSUF, ...) \
86 CONCAT (CHOOSE_ ## FSUF (__VA_ARGS__), LSUF),
87 #define ENTRY(...) \
88 GEN_TEST_STRTOD_FOREACH (_ENTRY, __VA_ARGS__)
89
90 /* Selector for boolean exact tag of expected results. */
91 #define _XNTRY(FSUF, FTYPE, FTOSTR, FTOSTRM, LSUF, CSUF, ...) \
92 CHOOSE_ ## FSUF (__VA_ARGS__),
93 #define XNTRY(...) \
94 GEN_TEST_STRTOD_FOREACH (_XNTRY, __VA_ARGS__)
95
96 /* This is hacky way around the seemingly unavoidable macro
97 expansion of the INFINITY or HUGE_VAL like macros in the
98 above. It is assumed the compiler will implicitly convert
99 the infinity correctly. */
100 #define INF INFINITY + 0.0
101
102 /* This macro is used in conjunction with the output from the
103 gen-tst-strtod-round utility to select the appropriately
104 rounded long double value for a given format. */
105 #define TEST(s, \
106 fx, fd, fn, fz, fu, \
107 dx, dd, dn, dz, du, \
108 ld64ix, ld64id, ld64in, ld64iz, ld64iu, \
109 ld64mx, ld64md, ld64mn, ld64mz, ld64mu, \
110 ld106x, ld106d, ld106n, ld106z, ld106u, \
111 ld113x, ld113d, ld113n, ld113z, ld113u) \
112 { \
113 s, \
114 { XNTRY (fx, dx, ld64ix, ld64mx, ld106x, ld113x) }, \
115 { \
116 { ENTRY (fn, dn, ld64in, ld64mn, ld106n, ld113n) }, \
117 { ENTRY (fd, dd, ld64id, ld64md, ld106d, ld113d) }, \
118 { ENTRY (fz, dz, ld64iz, ld64mz, ld106z, ld113z) }, \
119 { ENTRY (fu, du, ld64iu, ld64mu, ld106u, ld113u) } \
120 } \
121 }
122
123 struct test_exactness
124 {
125 STRUCT_FOREACH_FLOAT_BOOL
126 };
127
128 struct test_results
129 {
130 STRUCT_FOREACH_FLOAT_FTYPE
131 };
132
133 struct test {
134 const char *s;
135 struct test_exactness exact;
136 struct test_results r[4];
137 };
138
139 /* Include the generated test data. */
140 #include "tst-strtod-round-data.h"
141
142 #define GEN_ONE_TEST(FSUF, FTYPE, FTOSTR, FTOSTRM, LSUF, CSUF) \
143 { \
144 FTYPE f = strto ## FSUF (s, NULL); \
145 if (f != expected->FSUF \
146 || (copysign ## CSUF) (1.0 ## LSUF, f) \
147 != (copysign ## CSUF) (1.0 ## LSUF, expected->FSUF)) \
148 { \
149 char efstr[FSTRLENMAX]; \
150 char fstr[FSTRLENMAX]; \
151 FTOSTR (efstr, FSTRLENMAX, "%" FTOSTRM "a", expected->FSUF); \
152 FTOSTR (fstr, FSTRLENMAX, "%" FTOSTRM "a", f); \
153 printf ("strto" #FSUF " (%s) returned %s not %s" \
154 " (%s)\n", s, fstr, efstr, mode_name); \
155 if (ROUNDING_TESTS (FTYPE, rnd_mode) || exact->FSUF) \
156 result = 1; \
157 else \
158 printf ("ignoring this inexact result\n"); \
159 } \
160 }
161
162 static int
163 test_in_one_mode (const char *s, const struct test_results *expected,
164 const struct test_exactness *exact, const char *mode_name,
165 int rnd_mode)
166 {
167 int result = 0;
168 GEN_TEST_STRTOD_FOREACH (GEN_ONE_TEST)
169 return result;
170 }
171
172 static const struct fetestmodes
173 {
174 const char *mode_name;
175 int rnd_mode;
176 int rnd_i; /* Corresponding index into r array of struct test. */
177 } modes[] = {
178 { "default rounding mode", FE_TONEAREST, 0 },
179 #ifdef FE_DOWNWARD
180 { "FE_DOWNWARD", FE_DOWNWARD, 1 },
181 #endif
182 #ifdef FE_TOWARDZERO
183 { "FE_TOWARDZERO", FE_TOWARDZERO, 2 },
184 #endif
185 #ifdef FE_UPWARD
186 { "FE_UPWARD", FE_UPWARD, 3 },
187 #endif
188 {}
189 };
190
191 static int
192 do_test (void)
193 {
194 int save_round_mode __attribute__ ((unused)) = fegetround ();
195 int result = 0;
196 for (size_t i = 0; i < sizeof (tests) / sizeof (tests[0]); i++)
197 {
198 result |= test_in_one_mode (tests[i].s, &tests[i].r[modes[0].rnd_i],
199 &tests[i].exact, modes[0].mode_name,
200 modes[0].rnd_mode);
201 for (const struct fetestmodes *m = &modes[1]; m->mode_name != NULL; m++)
202 {
203 if (!fesetround (m->rnd_mode))
204 {
205 result |= test_in_one_mode (tests[i].s, &tests[i].r[m->rnd_i],
206 &tests[i].exact, m->mode_name,
207 m->rnd_mode);
208 fesetround (save_round_mode);
209 }
210 }
211 }
212 return result;
213 }
214
215 #define TEST_FUNCTION do_test ()
216 #include "../test-skeleton.c"