]>
Commit | Line | Data |
---|---|---|
688903eb | 1 | /* Copyright (C) 1998-2018 Free Software Foundation, Inc. |
07b51ba5 UD |
2 | This file is part of the GNU C Library. |
3 | ||
4 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
5 | modify it under the terms of the GNU Lesser General Public |
6 | License as published by the Free Software Foundation; either | |
7 | version 2.1 of the License, or (at your option) any later version. | |
07b51ba5 UD |
8 | |
9 | The GNU C Library is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 12 | Lesser General Public License for more details. |
07b51ba5 | 13 | |
41bdb6e2 | 14 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
15 | License along with the GNU C Library; if not, see |
16 | <http://www.gnu.org/licenses/>. */ | |
07b51ba5 | 17 | |
da2d1bc5 UD |
18 | #ifndef _GNU_SOURCE |
19 | # define _GNU_SOURCE 1 | |
20 | #endif | |
21 | ||
7d585303 | 22 | #include <float.h> |
da2d1bc5 | 23 | #include <math.h> |
07b51ba5 UD |
24 | #include <stdio.h> |
25 | #include <stdlib.h> | |
3e1f480e | 26 | #include <string.h> |
07b51ba5 | 27 | |
da2d1bc5 UD |
28 | int error_count; |
29 | ||
a7f804d8 | 30 | typedef struct |
da2d1bc5 UD |
31 | { |
32 | double value; | |
33 | int ndigit; | |
34 | int decpt; | |
35 | char result[30]; | |
36 | } testcase; | |
37 | ||
38 | typedef char * ((*efcvt_func) (double, int, int *, int *)); | |
39 | ||
c9e17a9c UD |
40 | typedef int ((*efcvt_r_func) (double, int, int *, int *, char *, size_t)); |
41 | ||
da2d1bc5 UD |
42 | |
43 | static testcase ecvt_tests[] = | |
44 | { | |
45 | { 0.0, 0, 1, "" }, | |
46 | { 10.0, 0, 2, "" }, | |
47 | { 10.0, 1, 2, "1" }, | |
48 | { 10.0, 5, 2, "10000" }, | |
49 | { -12.0, 5, 2, "12000" }, | |
50 | { 0.2, 4, 0, "2000" }, | |
51 | { 0.02, 4, -1, "2000" }, | |
52 | { 5.5, 1, 1, "6" }, | |
53 | { 1.0, -1, 1, "" }, | |
54 | { 0.01, 2, -1, "10" }, | |
1eb687d0 UD |
55 | { 100.0, -2, 3, "" }, |
56 | { 100.0, -5, 3, "" }, | |
57 | { 100.0, -4, 3, "" }, | |
58 | { 100.01, -4, 3, "" }, | |
59 | { 123.01, -4, 3, "" }, | |
60 | { 126.71, -4, 3, "" }, | |
a7f804d8 | 61 | { 0.0, 4, 1, "0000" }, |
7d585303 UD |
62 | #if DBL_MANT_DIG == 53 |
63 | { 0x1p-1074, 3, -323, "494" }, | |
64 | { -0x1p-1074, 3, -323, "494" }, | |
65 | #endif | |
da2d1bc5 UD |
66 | /* -1.0 is end marker. */ |
67 | { -1.0, 0, 0, "" } | |
68 | }; | |
69 | ||
70 | static testcase fcvt_tests[] = | |
71 | { | |
72 | { 0.0, 0, 1, "0" }, | |
73 | { 10.0, 0, 2, "10" }, | |
74 | { 10.0, 1, 2, "100" }, | |
75 | { 10.0, 4, 2, "100000" }, | |
76 | { -12.0, 5, 2, "1200000" }, | |
77 | { 0.2, 4, 0, "2000" }, | |
78 | { 0.02, 4, -1, "200" }, | |
79 | { 5.5, 1, 1, "55" }, | |
80 | { 5.5, 0, 1, "6" }, | |
81 | { 0.01, 2, -1, "1" }, | |
1eb687d0 UD |
82 | { 100.0, -2, 3, "100" }, |
83 | { 100.0, -5, 3, "100" }, | |
84 | { 100.0, -4, 3, "100" }, | |
85 | { 100.01, -4, 3, "100" }, | |
86 | { 123.01, -4, 3, "100" }, | |
87 | { 126.71, -4, 3, "100" }, | |
887e7ab6 | 88 | { 322.5, 16, 3, "3225000000000000000" }, |
da2d1bc5 UD |
89 | /* -1.0 is end marker. */ |
90 | { -1.0, 0, 0, "" } | |
91 | }; | |
92 | ||
de149cdb | 93 | static void |
da2d1bc5 UD |
94 | output_error (const char *name, double value, int ndigit, |
95 | const char *exp_p, int exp_decpt, int exp_sign, | |
96 | char *res_p, int res_decpt, int res_sign) | |
97 | { | |
98 | printf ("%s returned wrong result for value: %f, ndigits: %d\n", | |
99 | name, value, ndigit); | |
100 | printf ("Result was p: \"%s\", decpt: %d, sign: %d\n", | |
101 | res_p, res_decpt, res_sign); | |
102 | printf ("Should be p: \"%s\", decpt: %d, sign: %d\n", | |
103 | exp_p, exp_decpt, exp_sign); | |
104 | ++error_count; | |
105 | } | |
106 | ||
c9e17a9c | 107 | |
de149cdb | 108 | static void |
c9e17a9c UD |
109 | output_r_error (const char *name, double value, int ndigit, |
110 | const char *exp_p, int exp_decpt, int exp_sign, int exp_return, | |
111 | char *res_p, int res_decpt, int res_sign, int res_return) | |
112 | { | |
113 | printf ("%s returned wrong result for value: %f, ndigits: %d\n", | |
114 | name, value, ndigit); | |
115 | printf ("Result was buf: \"%s\", decpt: %d, sign: %d return value: %d\n", | |
116 | res_p, res_decpt, res_sign, res_return); | |
117 | printf ("Should be buf: \"%s\", decpt: %d, sign: %d\n", | |
1fc6f813 | 118 | exp_p, exp_decpt, exp_sign); |
c9e17a9c UD |
119 | ++error_count; |
120 | } | |
121 | ||
de149cdb | 122 | static void |
da2d1bc5 UD |
123 | test (testcase tests[], efcvt_func efcvt, const char *name) |
124 | { | |
125 | int no = 0; | |
126 | int decpt, sign; | |
127 | char *p; | |
128 | ||
129 | while (tests[no].value != -1.0) | |
130 | { | |
131 | p = efcvt (tests[no].value, tests[no].ndigit, &decpt, &sign); | |
132 | if (decpt != tests[no].decpt | |
133 | || sign != (tests[no].value < 0) | |
134 | || strcmp (p, tests[no].result) != 0) | |
135 | output_error (name, tests[no].value, tests[no].ndigit, | |
136 | tests[no].result, tests[no].decpt, | |
137 | (tests[no].value < 0), | |
138 | p, decpt, sign); | |
139 | ++no; | |
140 | } | |
141 | } | |
142 | ||
de149cdb | 143 | static void |
c9e17a9c UD |
144 | test_r (testcase tests[], efcvt_r_func efcvt_r, const char *name) |
145 | { | |
146 | int no = 0; | |
147 | int decpt, sign, res; | |
148 | char buf [1024]; | |
1fc6f813 | 149 | |
c9e17a9c UD |
150 | |
151 | while (tests[no].value != -1.0) | |
152 | { | |
153 | res = efcvt_r (tests[no].value, tests[no].ndigit, &decpt, &sign, | |
154 | buf, sizeof (buf)); | |
155 | if (res != 0 | |
156 | || decpt != tests[no].decpt | |
157 | || sign != (tests[no].value < 0) | |
158 | || strcmp (buf, tests[no].result) != 0) | |
159 | output_r_error (name, tests[no].value, tests[no].ndigit, | |
160 | tests[no].result, tests[no].decpt, 0, | |
161 | (tests[no].value < 0), | |
162 | buf, decpt, sign, res); | |
163 | ++no; | |
164 | } | |
165 | } | |
166 | ||
de149cdb | 167 | static void |
da2d1bc5 | 168 | special (void) |
07b51ba5 | 169 | { |
b9d3d9f7 | 170 | int decpt, sign, res; |
07b51ba5 | 171 | char *p; |
b9d3d9f7 | 172 | char buf [1024]; |
1fc6f813 | 173 | |
da2d1bc5 UD |
174 | p = ecvt (NAN, 10, &decpt, &sign); |
175 | if (sign != 0 || strcmp (p, "nan") != 0) | |
176 | output_error ("ecvt", NAN, 10, "nan", 0, 0, p, decpt, sign); | |
07b51ba5 | 177 | |
da2d1bc5 UD |
178 | p = ecvt (INFINITY, 10, &decpt, &sign); |
179 | if (sign != 0 || strcmp (p, "inf") != 0) | |
e015e27b | 180 | output_error ("ecvt", INFINITY, 10, "inf", 0, 0, p, decpt, sign); |
da2d1bc5 | 181 | |
67994d6f UD |
182 | /* Simply make sure these calls with large NDIGITs don't crash. */ |
183 | (void) ecvt (123.456, 10000, &decpt, &sign); | |
184 | (void) fcvt (123.456, 10000, &decpt, &sign); | |
185 | ||
ded5b9b7 | 186 | /* Some tests for the reentrant functions. */ |
b9d3d9f7 UD |
187 | /* Use a too small buffer. */ |
188 | res = ecvt_r (123.456, 10, &decpt, &sign, buf, 1); | |
189 | if (res == 0) | |
190 | { | |
191 | printf ("ecvt_r with a too small buffer was succesful.\n"); | |
192 | ++error_count; | |
193 | } | |
194 | res = fcvt_r (123.456, 10, &decpt, &sign, buf, 1); | |
195 | if (res == 0) | |
196 | { | |
197 | printf ("fcvt_r with a too small buffer was succesful.\n"); | |
198 | ++error_count; | |
199 | } | |
da2d1bc5 UD |
200 | } |
201 | ||
a7f804d8 | 202 | |
29955b5d AS |
203 | static int |
204 | do_test (void) | |
da2d1bc5 UD |
205 | { |
206 | test (ecvt_tests, ecvt, "ecvt"); | |
207 | test (fcvt_tests, fcvt, "fcvt"); | |
c9e17a9c UD |
208 | test_r (ecvt_tests, ecvt_r, "ecvt_r"); |
209 | test_r (fcvt_tests, fcvt_r, "fcvt_r"); | |
da2d1bc5 | 210 | special (); |
a7f804d8 | 211 | |
da2d1bc5 | 212 | return error_count; |
07b51ba5 | 213 | } |
29955b5d AS |
214 | |
215 | #define TEST_FUNCTION do_test () | |
216 | #include "../test-skeleton.c" |