Update.
[glibc.git] / misc / tst-efgcvt.c
blob6eaa8be9f528fc6d3cb58ff27199d76bebdb086c
1 /* Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
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
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
19 #ifndef _GNU_SOURCE
20 # define _GNU_SOURCE 1
21 #endif
23 #include <math.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
28 int error_count;
30 typedef struct
32 double value;
33 int ndigit;
34 int decpt;
35 char result[30];
36 } testcase;
38 typedef char * ((*efcvt_func) (double, int, int *, int *));
40 typedef int ((*efcvt_r_func) (double, int, int *, int *, char *, size_t));
43 static testcase ecvt_tests[] =
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" },
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, "" },
61 { 0.0, 4, 1, "0000" },
62 /* -1.0 is end marker. */
63 { -1.0, 0, 0, "" }
66 static testcase fcvt_tests[] =
68 { 0.0, 0, 1, "0" },
69 { 10.0, 0, 2, "10" },
70 { 10.0, 1, 2, "100" },
71 { 10.0, 4, 2, "100000" },
72 { -12.0, 5, 2, "1200000" },
73 { 0.2, 4, 0, "2000" },
74 { 0.02, 4, -1, "200" },
75 { 5.5, 1, 1, "55" },
76 { 5.5, 0, 1, "6" },
77 { 0.01, 2, -1, "1" },
78 { 100.0, -2, 3, "100" },
79 { 100.0, -5, 3, "100" },
80 { 100.0, -4, 3, "100" },
81 { 100.01, -4, 3, "100" },
82 { 123.01, -4, 3, "100" },
83 { 126.71, -4, 3, "100" },
84 /* -1.0 is end marker. */
85 { -1.0, 0, 0, "" }
88 void
89 output_error (const char *name, double value, int ndigit,
90 const char *exp_p, int exp_decpt, int exp_sign,
91 char *res_p, int res_decpt, int res_sign)
93 printf ("%s returned wrong result for value: %f, ndigits: %d\n",
94 name, value, ndigit);
95 printf ("Result was p: \"%s\", decpt: %d, sign: %d\n",
96 res_p, res_decpt, res_sign);
97 printf ("Should be p: \"%s\", decpt: %d, sign: %d\n",
98 exp_p, exp_decpt, exp_sign);
99 ++error_count;
103 void
104 output_r_error (const char *name, double value, int ndigit,
105 const char *exp_p, int exp_decpt, int exp_sign, int exp_return,
106 char *res_p, int res_decpt, int res_sign, int res_return)
108 printf ("%s returned wrong result for value: %f, ndigits: %d\n",
109 name, value, ndigit);
110 printf ("Result was buf: \"%s\", decpt: %d, sign: %d return value: %d\n",
111 res_p, res_decpt, res_sign, res_return);
112 printf ("Should be buf: \"%s\", decpt: %d, sign: %d\n",
113 exp_p, exp_decpt, exp_sign);
114 ++error_count;
117 void
118 test (testcase tests[], efcvt_func efcvt, const char *name)
120 int no = 0;
121 int decpt, sign;
122 char *p;
124 while (tests[no].value != -1.0)
126 p = efcvt (tests[no].value, tests[no].ndigit, &decpt, &sign);
127 if (decpt != tests[no].decpt
128 || sign != (tests[no].value < 0)
129 || strcmp (p, tests[no].result) != 0)
130 output_error (name, tests[no].value, tests[no].ndigit,
131 tests[no].result, tests[no].decpt,
132 (tests[no].value < 0),
133 p, decpt, sign);
134 ++no;
138 void
139 test_r (testcase tests[], efcvt_r_func efcvt_r, const char *name)
141 int no = 0;
142 int decpt, sign, res;
143 char buf [1024];
146 while (tests[no].value != -1.0)
148 res = efcvt_r (tests[no].value, tests[no].ndigit, &decpt, &sign,
149 buf, sizeof (buf));
150 if (res != 0
151 || decpt != tests[no].decpt
152 || sign != (tests[no].value < 0)
153 || strcmp (buf, tests[no].result) != 0)
154 output_r_error (name, tests[no].value, tests[no].ndigit,
155 tests[no].result, tests[no].decpt, 0,
156 (tests[no].value < 0),
157 buf, decpt, sign, res);
158 ++no;
162 void
163 special (void)
165 int decpt, sign, res;
166 char *p;
167 char buf [1024];
169 p = ecvt (NAN, 10, &decpt, &sign);
170 if (sign != 0 || strcmp (p, "nan") != 0)
171 output_error ("ecvt", NAN, 10, "nan", 0, 0, p, decpt, sign);
173 p = ecvt (INFINITY, 10, &decpt, &sign);
174 if (sign != 0 || strcmp (p, "inf") != 0)
175 output_error ("ecvt", NAN, 10, "inf", 0, 0, p, decpt, sign);
177 /* Simply make sure these calls with large NDIGITs don't crash. */
178 (void) ecvt (123.456, 10000, &decpt, &sign);
179 (void) fcvt (123.456, 10000, &decpt, &sign);
181 /* Some tests for for the reentrant functions. */
182 /* Use a too small buffer. */
183 res = ecvt_r (123.456, 10, &decpt, &sign, buf, 1);
184 if (res == 0)
186 printf ("ecvt_r with a too small buffer was succesful.\n");
187 ++error_count;
189 res = fcvt_r (123.456, 10, &decpt, &sign, buf, 1);
190 if (res == 0)
192 printf ("fcvt_r with a too small buffer was succesful.\n");
193 ++error_count;
199 main (void)
201 test (ecvt_tests, ecvt, "ecvt");
202 test (fcvt_tests, fcvt, "fcvt");
203 test_r (ecvt_tests, ecvt_r, "ecvt_r");
204 test_r (fcvt_tests, fcvt_r, "fcvt_r");
205 special ();
207 return error_count;