1 | /* Test locale support in C++ functions, or attempt to do so. |
---|
2 | |
---|
3 | Copyright 2001, 2004 Free Software Foundation, Inc. |
---|
4 | |
---|
5 | This file is part of the GNU MP Library. |
---|
6 | |
---|
7 | The GNU MP Library is free software; you can redistribute it and/or modify |
---|
8 | it under the terms of the GNU Lesser General Public License as published by |
---|
9 | the Free Software Foundation; either version 2.1 of the License, or (at your |
---|
10 | option) any later version. |
---|
11 | |
---|
12 | The GNU MP Library is distributed in the hope that it will be useful, but |
---|
13 | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
---|
14 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
---|
15 | License for more details. |
---|
16 | |
---|
17 | You should have received a copy of the GNU Lesser General Public License |
---|
18 | along with the GNU MP Library; see the file COPYING.LIB. If not, write to |
---|
19 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
---|
20 | MA 02111-1307, USA. */ |
---|
21 | |
---|
22 | #include "config.h" |
---|
23 | |
---|
24 | #include <strstream> |
---|
25 | |
---|
26 | #include <stdio.h> |
---|
27 | #include <stdlib.h> |
---|
28 | #include <string.h> |
---|
29 | |
---|
30 | #if HAVE_LOCALE_H |
---|
31 | #include <locale.h> /* for lconv */ |
---|
32 | #endif |
---|
33 | |
---|
34 | #include "gmp.h" |
---|
35 | #include "gmp-impl.h" |
---|
36 | #include "tests.h" |
---|
37 | |
---|
38 | using namespace std; |
---|
39 | |
---|
40 | |
---|
41 | #if HAVE_LOCALECONV |
---|
42 | |
---|
43 | char *decimal_point; |
---|
44 | |
---|
45 | /* Replace the libc localeconv with one we can manipulate. */ |
---|
46 | struct lconv * |
---|
47 | localeconv (void) |
---|
48 | { |
---|
49 | static struct lconv l; |
---|
50 | l.decimal_point = decimal_point; |
---|
51 | return &l; |
---|
52 | } |
---|
53 | |
---|
54 | void |
---|
55 | check_input (void) |
---|
56 | { |
---|
57 | static char *point[] = { |
---|
58 | ".", ",", "xy", "xyz", "xyz***" |
---|
59 | }; |
---|
60 | |
---|
61 | static const struct { |
---|
62 | const char *str; |
---|
63 | double d; |
---|
64 | } data[] = { |
---|
65 | |
---|
66 | { "1%s", 1.0 }, |
---|
67 | { "1%s0", 1.0 }, |
---|
68 | { "1%s00", 1.0 }, |
---|
69 | |
---|
70 | { "%s5", 0.5 }, |
---|
71 | { "0%s5", 0.5 }, |
---|
72 | { "00%s5", 0.5 }, |
---|
73 | { "00%s50", 0.5 }, |
---|
74 | |
---|
75 | { "1%s5", 1.5 }, |
---|
76 | { "1%s5e1", 15.0 }, |
---|
77 | }; |
---|
78 | |
---|
79 | mpf_t f; |
---|
80 | double d; |
---|
81 | mpf_init (f); |
---|
82 | |
---|
83 | for (size_t i = 0; i < numberof (point); i++) |
---|
84 | { |
---|
85 | decimal_point = point[i]; |
---|
86 | |
---|
87 | for (int neg = 0; neg <= 1; neg++) |
---|
88 | { |
---|
89 | for (size_t j = 0; j < numberof (data); j++) |
---|
90 | { |
---|
91 | char str[128]; |
---|
92 | strcpy (str, neg ? "-" : ""); |
---|
93 | sprintf (str+strlen(str), data[j].str, decimal_point); |
---|
94 | |
---|
95 | mpf_set_d (f, 123.0); |
---|
96 | istrstream i (str); |
---|
97 | if (! (i >> f)) |
---|
98 | { |
---|
99 | printf ("operator>> input error\n"); |
---|
100 | printf (" point %s\n", decimal_point); |
---|
101 | printf (" str %s\n", str); |
---|
102 | abort (); |
---|
103 | } |
---|
104 | d = data[j].d; |
---|
105 | if (neg) |
---|
106 | d = -d; |
---|
107 | if (mpf_cmp_d (f, d) != 0) |
---|
108 | { |
---|
109 | printf ("operator>> wrong result\n"); |
---|
110 | printf (" point %s\n", decimal_point); |
---|
111 | printf (" str %s\n", str); |
---|
112 | mpf_trace (" f", f); |
---|
113 | printf (" d=%g\n", d); |
---|
114 | abort (); |
---|
115 | } |
---|
116 | } |
---|
117 | } |
---|
118 | } |
---|
119 | |
---|
120 | mpf_clear (f); |
---|
121 | } |
---|
122 | |
---|
123 | int |
---|
124 | main (void) |
---|
125 | { |
---|
126 | tests_start (); |
---|
127 | |
---|
128 | { |
---|
129 | mpf_t f; |
---|
130 | mpf_init (f); |
---|
131 | decimal_point = ","; |
---|
132 | mpf_set_d (f, 1.5); |
---|
133 | ostrstream got; |
---|
134 | got << f << '\0'; |
---|
135 | mpf_clear (f); |
---|
136 | if (strcmp (got.str(), "1,5") != 0) |
---|
137 | { |
---|
138 | printf ("Test skipped, replacing localeconv doesn't work\n"); |
---|
139 | goto done; |
---|
140 | } |
---|
141 | } |
---|
142 | |
---|
143 | check_input (); |
---|
144 | |
---|
145 | done: |
---|
146 | tests_end (); |
---|
147 | exit (0); |
---|
148 | } |
---|
149 | |
---|
150 | |
---|
151 | #else |
---|
152 | |
---|
153 | int |
---|
154 | main (void) |
---|
155 | { |
---|
156 | printf ("Test skipped, no locale support\n"); |
---|
157 | exit (0); |
---|
158 | } |
---|
159 | |
---|
160 | #endif |
---|