t-locale.c revision 1.1.1.2 1 1.1 mrg /* Test locale support, or attempt to do so.
2 1.1 mrg
3 1.1.1.2 mrg Copyright 2001, 2002, 2011 Free Software Foundation, Inc.
4 1.1 mrg
5 1.1.1.2 mrg This file is part of the GNU MP Library test suite.
6 1.1 mrg
7 1.1.1.2 mrg The GNU MP Library test suite is free software; you can redistribute it
8 1.1.1.2 mrg and/or modify it under the terms of the GNU General Public License as
9 1.1.1.2 mrg published by the Free Software Foundation; either version 3 of the License,
10 1.1.1.2 mrg or (at your option) any later version.
11 1.1.1.2 mrg
12 1.1.1.2 mrg The GNU MP Library test suite is distributed in the hope that it will be
13 1.1.1.2 mrg useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 1.1.1.2 mrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
15 1.1.1.2 mrg Public License for more details.
16 1.1 mrg
17 1.1.1.2 mrg You should have received a copy of the GNU General Public License along with
18 1.1.1.2 mrg the GNU MP Library test suite. If not, see http://www.gnu.org/licenses/. */
19 1.1 mrg
20 1.1 mrg #define _GNU_SOURCE /* for DECIMAL_POINT in glibc langinfo.h */
21 1.1 mrg
22 1.1 mrg #include "config.h"
23 1.1 mrg
24 1.1 mrg #include <stdio.h>
25 1.1 mrg #include <stdlib.h>
26 1.1 mrg #include <string.h>
27 1.1 mrg
28 1.1 mrg #if HAVE_NL_TYPES_H
29 1.1 mrg #include <nl_types.h> /* for nl_item (on netbsd 1.4.1 at least) */
30 1.1 mrg #endif
31 1.1 mrg
32 1.1 mrg #if HAVE_LANGINFO_H
33 1.1 mrg #include <langinfo.h> /* for nl_langinfo */
34 1.1 mrg #endif
35 1.1 mrg
36 1.1 mrg #if HAVE_LOCALE_H
37 1.1 mrg #include <locale.h> /* for lconv */
38 1.1 mrg #endif
39 1.1 mrg
40 1.1 mrg #include "gmp.h"
41 1.1 mrg #include "gmp-impl.h"
42 1.1 mrg #include "tests.h"
43 1.1 mrg
44 1.1.1.2 mrg #ifdef __MINGW32__
45 1.1.1.2 mrg int
46 1.1.1.2 mrg main (void)
47 1.1.1.2 mrg {
48 1.1.1.2 mrg exit (0);
49 1.1.1.2 mrg }
50 1.1.1.2 mrg #else
51 1.1 mrg
52 1.1.1.2 mrg const char *decimal_point;
53 1.1 mrg
54 1.1 mrg /* Replace the libc localeconv with one we can manipulate. */
55 1.1 mrg #if HAVE_LOCALECONV
56 1.1 mrg struct lconv *
57 1.1 mrg localeconv (void)
58 1.1 mrg {
59 1.1 mrg static struct lconv l;
60 1.1.1.2 mrg l.decimal_point = (char *) decimal_point;
61 1.1 mrg return &l;
62 1.1 mrg }
63 1.1 mrg #endif
64 1.1 mrg
65 1.1 mrg /* Replace the libc nl_langinfo with one we can manipulate. */
66 1.1 mrg #if HAVE_NL_LANGINFO
67 1.1 mrg char *
68 1.1 mrg nl_langinfo (nl_item n)
69 1.1 mrg {
70 1.1 mrg #if defined (DECIMAL_POINT)
71 1.1 mrg if (n == DECIMAL_POINT)
72 1.1.1.2 mrg return (char *) decimal_point;
73 1.1 mrg #endif
74 1.1 mrg #if defined (RADIXCHAR)
75 1.1 mrg if (n == RADIXCHAR)
76 1.1.1.2 mrg return (char *) decimal_point;
77 1.1 mrg #endif
78 1.1.1.2 mrg return (char *) "";
79 1.1 mrg }
80 1.1 mrg #endif
81 1.1 mrg
82 1.1 mrg void
83 1.1 mrg check_input (void)
84 1.1 mrg {
85 1.1.1.2 mrg static const char *point[] = {
86 1.1 mrg ".", ",", "WU", "STR", "ZTV***"
87 1.1 mrg };
88 1.1 mrg
89 1.1 mrg static const struct {
90 1.1 mrg const char *str;
91 1.1 mrg double d;
92 1.1 mrg } data[] = {
93 1.1 mrg
94 1.1 mrg { "1%s", 1.0 },
95 1.1 mrg { "1%s0", 1.0 },
96 1.1 mrg { "1%s00", 1.0 },
97 1.1 mrg
98 1.1 mrg { "%s5", 0.5 },
99 1.1 mrg { "0%s5", 0.5 },
100 1.1 mrg { "00%s5", 0.5 },
101 1.1 mrg { "00%s50", 0.5 },
102 1.1 mrg
103 1.1 mrg { "1%s5", 1.5 },
104 1.1 mrg { "1%s5e1", 15.0 },
105 1.1 mrg };
106 1.1 mrg
107 1.1 mrg int i, j, neg, ret;
108 1.1 mrg char str[128];
109 1.1 mrg mpf_t f;
110 1.1 mrg double d;
111 1.1 mrg
112 1.1 mrg mpf_init (f);
113 1.1 mrg
114 1.1 mrg for (i = 0; i < numberof (point); i++)
115 1.1 mrg {
116 1.1.1.2 mrg decimal_point = (const char *) point[i];
117 1.1 mrg
118 1.1 mrg for (neg = 0; neg <= 1; neg++)
119 1.1 mrg {
120 1.1 mrg for (j = 0; j < numberof (data); j++)
121 1.1 mrg {
122 1.1 mrg strcpy (str, neg ? "-" : "");
123 1.1 mrg sprintf (str+strlen(str), data[j].str, decimal_point);
124 1.1 mrg
125 1.1 mrg d = data[j].d;
126 1.1 mrg if (neg)
127 1.1 mrg d = -d;
128 1.1 mrg
129 1.1 mrg mpf_set_d (f, 123.0);
130 1.1 mrg if (mpf_set_str (f, str, 10) != 0)
131 1.1 mrg {
132 1.1 mrg printf ("mpf_set_str error\n");
133 1.1 mrg printf (" point %s\n", decimal_point);
134 1.1 mrg printf (" str %s\n", str);
135 1.1 mrg abort ();
136 1.1 mrg }
137 1.1 mrg if (mpf_cmp_d (f, d) != 0)
138 1.1 mrg {
139 1.1 mrg printf ("mpf_set_str wrong result\n");
140 1.1 mrg printf (" point %s\n", decimal_point);
141 1.1 mrg printf (" str %s\n", str);
142 1.1 mrg mpf_trace (" f", f);
143 1.1 mrg printf (" d=%g\n", d);
144 1.1 mrg abort ();
145 1.1 mrg }
146 1.1 mrg
147 1.1 mrg mpf_set_d (f, 123.0);
148 1.1 mrg ret = gmp_sscanf (str, "%Ff", f);
149 1.1 mrg if (ret != 1)
150 1.1 mrg {
151 1.1 mrg printf ("gmp_sscanf wrong return value\n");
152 1.1 mrg printf (" point %s\n", decimal_point);
153 1.1 mrg printf (" str %s\n", str);
154 1.1 mrg printf (" ret %d\n", ret);
155 1.1 mrg abort ();
156 1.1 mrg }
157 1.1 mrg if (mpf_cmp_d (f, d) != 0)
158 1.1 mrg {
159 1.1 mrg printf ("gmp_sscanf wrong result\n");
160 1.1 mrg printf (" point %s\n", decimal_point);
161 1.1 mrg printf (" str %s\n", str);
162 1.1 mrg mpf_trace (" f", f);
163 1.1 mrg printf (" d=%g\n", d);
164 1.1 mrg abort ();
165 1.1 mrg }
166 1.1 mrg }
167 1.1 mrg }
168 1.1 mrg }
169 1.1 mrg mpf_clear (f);
170 1.1 mrg }
171 1.1 mrg
172 1.1 mrg int
173 1.1 mrg main (void)
174 1.1 mrg {
175 1.1 mrg /* The localeconv replacement breaks printf "%lu" on SunOS 4, so we can't
176 1.1 mrg print the seed in tests_rand_start(). Nothing random is used in this
177 1.1 mrg program though, so just use the memory tests alone. */
178 1.1 mrg tests_memory_start ();
179 1.1 mrg
180 1.1 mrg {
181 1.1 mrg mpf_t f;
182 1.1 mrg char buf[128];
183 1.1 mrg mpf_init (f);
184 1.1 mrg decimal_point = ",";
185 1.1 mrg mpf_set_d (f, 1.5);
186 1.1 mrg gmp_snprintf (buf, sizeof(buf), "%.1Ff", f);
187 1.1 mrg mpf_clear (f);
188 1.1 mrg if (strcmp (buf, "1,5") != 0)
189 1.1 mrg {
190 1.1 mrg printf ("Test skipped, replacing localeconv/nl_langinfo doesn't work\n");
191 1.1 mrg goto done;
192 1.1 mrg }
193 1.1 mrg }
194 1.1 mrg
195 1.1 mrg check_input ();
196 1.1 mrg
197 1.1 mrg done:
198 1.1 mrg tests_memory_end ();
199 1.1 mrg exit (0);
200 1.1 mrg }
201 1.1.1.2 mrg #endif
202