Update.
[platform/upstream/glibc.git] / misc / tst-efgcvt.c
1 /* Copyright (C) 1998, 1999 Free Software Foundation, Inc.
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
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.
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
12    Library General Public License for more details.
13
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.  */
18
19 #ifndef _GNU_SOURCE
20 # define _GNU_SOURCE    1
21 #endif
22
23 #include <math.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26
27 int error_count;
28
29 typedef struct
30 {
31   double value;
32   int ndigit;
33   int decpt;
34   char result[30];
35 } testcase;
36
37 typedef char * ((*efcvt_func) (double, int, int *, int *));
38
39
40 static testcase ecvt_tests[] =
41 {
42   { 0.0, 0, 1, "" },
43   { 10.0, 0, 2, "" },
44   { 10.0, 1, 2, "1" },
45   { 10.0, 5, 2, "10000" },
46   { -12.0, 5, 2, "12000" },
47   { 0.2, 4, 0, "2000" },
48   { 0.02, 4, -1, "2000" },
49   { 5.5, 1, 1, "6" },
50   { 1.0, -1, 1, "" },
51   { 0.01, 2, -1, "10" },
52   { 100.0, -2, 3, "" },
53   { 100.0, -5, 3, "" },
54   { 100.0, -4, 3, "" },
55   { 100.01, -4, 3, "" },
56   { 123.01, -4, 3, "" },
57   { 126.71, -4, 3, "" },
58   { 0.0, 4, 1, "0000" },
59   /* -1.0 is end marker.  */
60   { -1.0, 0, 0, "" }
61 };
62
63 static testcase fcvt_tests[] =
64 {
65   { 0.0, 0, 1, "0" },
66   { 10.0, 0, 2, "10" },
67   { 10.0, 1, 2, "100" },
68   { 10.0, 4, 2, "100000" },
69   { -12.0, 5, 2, "1200000" },
70   { 0.2, 4, 0, "2000" },
71   { 0.02, 4, -1, "200" },
72   { 5.5, 1, 1, "55" },
73   { 5.5, 0, 1, "6" },
74   { 0.01, 2, -1, "1" },
75   { 100.0, -2, 3, "100" },
76   { 100.0, -5, 3, "100" },
77   { 100.0, -4, 3, "100" },
78   { 100.01, -4, 3, "100" },
79   { 123.01, -4, 3, "100" },
80   { 126.71, -4, 3, "100" },
81   /* -1.0 is end marker.  */
82   { -1.0, 0, 0, "" }
83 };
84
85 void
86 output_error (const char *name, double value, int ndigit,
87               const char *exp_p, int exp_decpt, int exp_sign,
88               char *res_p, int res_decpt, int res_sign)
89 {
90   printf ("%s returned wrong result for value: %f, ndigits: %d\n",
91           name, value, ndigit);
92   printf ("Result was p: \"%s\", decpt: %d, sign: %d\n",
93           res_p, res_decpt, res_sign);
94   printf ("Should be  p: \"%s\", decpt: %d, sign: %d\n",
95           exp_p, exp_decpt, exp_sign);
96   ++error_count;
97 }
98
99 void
100 test (testcase tests[], efcvt_func efcvt, const char *name)
101 {
102   int no = 0;
103   int decpt, sign;
104   char *p;
105
106   while (tests[no].value != -1.0)
107     {
108       p = efcvt (tests[no].value, tests[no].ndigit, &decpt, &sign);
109       if (decpt != tests[no].decpt
110           || sign != (tests[no].value < 0)
111           || strcmp (p, tests[no].result) != 0)
112         output_error (name, tests[no].value, tests[no].ndigit,
113                       tests[no].result, tests[no].decpt,
114                       (tests[no].value < 0),
115                       p, decpt, sign);
116       ++no;
117     }
118 }
119
120 void
121 special (void)
122 {
123   int decpt, sign, res;
124   char *p;
125   char buf [1024];
126   
127   p = ecvt (NAN, 10, &decpt, &sign);
128   if (sign != 0 || strcmp (p, "nan") != 0)
129     output_error ("ecvt", NAN, 10, "nan", 0, 0, p, decpt, sign);
130
131   p = ecvt (INFINITY, 10, &decpt, &sign);
132   if (sign != 0 || strcmp (p, "inf") != 0)
133     output_error ("ecvt", NAN, 10, "inf", 0, 0, p, decpt, sign);
134
135   /* Simply make sure these calls with large NDIGITs don't crash.  */
136   (void) ecvt (123.456, 10000, &decpt, &sign);
137   (void) fcvt (123.456, 10000, &decpt, &sign);
138
139   /* Some tests for for the reentrant functions.  */
140   /* Use a too small buffer.  */
141   res = ecvt_r (123.456, 10, &decpt, &sign, buf, 1);
142   if (res == 0)
143     {
144       printf ("ecvt_r with a too small buffer was succesful.\n");
145       ++error_count;
146     }
147   res = fcvt_r (123.456, 10, &decpt, &sign, buf, 1);
148   if (res == 0)
149     {
150       printf ("fcvt_r with a too small buffer was succesful.\n");
151       ++error_count;
152     }
153 }
154
155
156 int
157 main (void)
158 {
159   test (ecvt_tests, ecvt, "ecvt");
160   test (fcvt_tests, fcvt, "fcvt");
161   special ();
162
163   return error_count;
164 }