1 | /* Test mp*_class constructors. |
---|
2 | |
---|
3 | Copyright 2001 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 <iostream> |
---|
23 | #include <strstream> |
---|
24 | #include <string> |
---|
25 | #include <cstdlib> |
---|
26 | #include "gmp.h" |
---|
27 | #include "gmp-impl.h" |
---|
28 | #include "gmpxx.h" |
---|
29 | #include "tests.h" |
---|
30 | |
---|
31 | using namespace std; |
---|
32 | |
---|
33 | |
---|
34 | #define CHECK_MPZ(args, want) \ |
---|
35 | do { \ |
---|
36 | mpz_set_str (ref, want, 0); \ |
---|
37 | if (mpz_cmp (z.get_mpz_t(), ref) != 0) \ |
---|
38 | { \ |
---|
39 | cout << "mpz_class constructor wrong, args: " << args << "\n"; \ |
---|
40 | cout << " want: " << ref << "\n"; \ |
---|
41 | cout << " got: " << z.get_mpz_t() << "\n"; \ |
---|
42 | abort (); \ |
---|
43 | } \ |
---|
44 | } while (0) |
---|
45 | |
---|
46 | #define CHECK_MPQ(args, want) \ |
---|
47 | do { \ |
---|
48 | mpq_set_str (ref, want, 0); \ |
---|
49 | if (mpq_cmp (q.get_mpq_t(), ref) != 0) \ |
---|
50 | { \ |
---|
51 | cout << "mpq_class constructor wrong, args: " << args << "\n"; \ |
---|
52 | cout << " want: " << ref << "\n"; \ |
---|
53 | cout << " got: " << q.get_mpq_t() << "\n"; \ |
---|
54 | abort (); \ |
---|
55 | } \ |
---|
56 | } while (0) |
---|
57 | |
---|
58 | #define CHECK_MPF(args, want) \ |
---|
59 | do { \ |
---|
60 | mpf_set_str (ref, want, 10); \ |
---|
61 | if (mpf_cmp (f.get_mpf_t(), ref) != 0) \ |
---|
62 | { \ |
---|
63 | cout << "mpf_class constructor wrong, args: " << args << "\n"; \ |
---|
64 | cout << " want: " << ref << "\n"; \ |
---|
65 | cout << " got: " << f.get_mpf_t() << "\n"; \ |
---|
66 | abort (); \ |
---|
67 | } \ |
---|
68 | } while (0) |
---|
69 | |
---|
70 | void |
---|
71 | check_mpz (void) |
---|
72 | { |
---|
73 | mpz_t ref; |
---|
74 | mpz_init (ref); |
---|
75 | |
---|
76 | { // no arguments |
---|
77 | mpz_class z; |
---|
78 | CHECK_MPZ ("none", "0"); |
---|
79 | } |
---|
80 | |
---|
81 | { // argument: mpz_class |
---|
82 | mpz_class w; |
---|
83 | mpz_class z (w); |
---|
84 | CHECK_MPZ ("w [mpz_class]", "0"); |
---|
85 | } |
---|
86 | |
---|
87 | { // argument: int |
---|
88 | mpz_class z (0); |
---|
89 | CHECK_MPZ ("0", "0"); |
---|
90 | } |
---|
91 | |
---|
92 | { // argument: bool |
---|
93 | mpz_class z (true); |
---|
94 | CHECK_MPZ ("true", "1"); |
---|
95 | } |
---|
96 | |
---|
97 | { // argument: unsigned short |
---|
98 | mpz_class z ((unsigned short) 1); |
---|
99 | CHECK_MPZ ("(unsigned short) 1", "1"); |
---|
100 | } |
---|
101 | |
---|
102 | { // argument: long |
---|
103 | mpz_class z (-1234567890L); |
---|
104 | CHECK_MPZ ("-1234567890L", "-1234567890"); |
---|
105 | } |
---|
106 | |
---|
107 | { // argument: double |
---|
108 | mpz_class z (3.141592653589793238); |
---|
109 | CHECK_MPZ ("3.141592653589793238", "3"); |
---|
110 | } |
---|
111 | |
---|
112 | { // argument: char * |
---|
113 | mpz_class z ("12345678901234567890"); |
---|
114 | CHECK_MPZ ("\"12345678901234567890\"", "12345678901234567890"); |
---|
115 | } |
---|
116 | |
---|
117 | { // arguments: char *, int |
---|
118 | mpz_class z ("FFFF", 16); |
---|
119 | CHECK_MPZ ("\"FFFF\", 16", "65535"); |
---|
120 | } |
---|
121 | |
---|
122 | { // argument: string |
---|
123 | mpz_class z (string ("1234567890")); |
---|
124 | CHECK_MPZ ("string (\"1234567890\")", "1234567890"); |
---|
125 | } |
---|
126 | |
---|
127 | { // arguments: string, int |
---|
128 | mpz_class z (string ("7777"), 8); |
---|
129 | CHECK_MPZ ("string (\"7777\", 8)", "4095"); |
---|
130 | } |
---|
131 | |
---|
132 | { // argument: mpz_t |
---|
133 | mpz_class z (ref); |
---|
134 | CHECK_MPZ ("ref [mpz_t]", "4095"); |
---|
135 | } |
---|
136 | |
---|
137 | mpz_clear (ref); |
---|
138 | } |
---|
139 | |
---|
140 | void |
---|
141 | check_mpq (void) |
---|
142 | { |
---|
143 | mpq_t ref; |
---|
144 | mpq_init (ref); |
---|
145 | |
---|
146 | { // no arguments |
---|
147 | mpq_class q; |
---|
148 | CHECK_MPQ ("none", "0"); |
---|
149 | } |
---|
150 | |
---|
151 | { // argument: mpq_class |
---|
152 | mpq_class r; |
---|
153 | mpq_class q (r); |
---|
154 | CHECK_MPQ ("r [mpq_class]", "0"); |
---|
155 | } |
---|
156 | |
---|
157 | { // argument: int |
---|
158 | mpq_class q (0); |
---|
159 | CHECK_MPQ ("0", "0"); |
---|
160 | } |
---|
161 | |
---|
162 | { // argument: bool |
---|
163 | mpq_class q (true); |
---|
164 | CHECK_MPQ ("true", "1"); |
---|
165 | } |
---|
166 | |
---|
167 | { // argument: unsigned short |
---|
168 | mpq_class q ((unsigned short) 1); |
---|
169 | CHECK_MPQ ("(unsigned short) 1", "1"); |
---|
170 | } |
---|
171 | |
---|
172 | { // argument: long |
---|
173 | mpq_class q (-1234567890L); |
---|
174 | CHECK_MPQ ("-1234567890L", "-1234567890"); |
---|
175 | } |
---|
176 | |
---|
177 | { // argument: double |
---|
178 | mpq_class q (1.25); |
---|
179 | CHECK_MPQ ("1.25", "5/4"); |
---|
180 | } |
---|
181 | |
---|
182 | { // argument: char * |
---|
183 | mpq_class q ("12345678901234567890"); |
---|
184 | CHECK_MPQ ("\"12345678901234567890\"", "12345678901234567890"); |
---|
185 | } |
---|
186 | |
---|
187 | { // arguments: char *, int |
---|
188 | mpq_class q ("FFFF", 16); |
---|
189 | CHECK_MPQ ("\"FFFF\", 16", "65535"); |
---|
190 | } |
---|
191 | |
---|
192 | { // argument: string |
---|
193 | mpq_class q (string ("1234567890")); |
---|
194 | CHECK_MPQ ("string (\"1234567890\")", "1234567890"); |
---|
195 | } |
---|
196 | |
---|
197 | { // arguments: string, int |
---|
198 | mpq_class q (string ("7777"), 8); |
---|
199 | CHECK_MPQ ("string (\"7777\", 8)", "4095"); |
---|
200 | } |
---|
201 | |
---|
202 | { // argument: mpz_t |
---|
203 | mpq_class q (ref); |
---|
204 | CHECK_MPQ ("ref [mpq_t]", "4095"); |
---|
205 | } |
---|
206 | |
---|
207 | { // arguments: int, int |
---|
208 | mpq_class q (1, 2); |
---|
209 | CHECK_MPQ ("1, 2", "1/2"); |
---|
210 | } |
---|
211 | |
---|
212 | { // arguments: mpz_class, mpz_class |
---|
213 | mpz_class z (3), w (4); |
---|
214 | mpq_class q (z, w); |
---|
215 | CHECK_MPQ ("z, w [mpz_class, mpz_class]", "3/4"); |
---|
216 | } |
---|
217 | |
---|
218 | { // arguments: int, mpz_class |
---|
219 | mpz_class z (5); |
---|
220 | mpq_class q (6, z); |
---|
221 | CHECK_MPQ ("11, z [mpz_class]", "6/5"); |
---|
222 | } |
---|
223 | |
---|
224 | mpq_clear (ref); |
---|
225 | } |
---|
226 | |
---|
227 | void |
---|
228 | check_mpf (void) |
---|
229 | { |
---|
230 | mpf_t ref; |
---|
231 | mpf_init (ref); |
---|
232 | |
---|
233 | { // no arguments |
---|
234 | mpf_class f; |
---|
235 | CHECK_MPF ("none", "0.0"); |
---|
236 | } |
---|
237 | |
---|
238 | { // argument: mpf_class |
---|
239 | mpf_class g; |
---|
240 | mpf_class f (g); |
---|
241 | CHECK_MPF ("g [mpf_class]", "0.0"); |
---|
242 | } |
---|
243 | |
---|
244 | { // argument: int |
---|
245 | mpf_class f (0); |
---|
246 | CHECK_MPF ("0", "0.0"); |
---|
247 | } |
---|
248 | |
---|
249 | { // argument: bool |
---|
250 | mpf_class f (true); |
---|
251 | CHECK_MPF ("true", "1.0"); |
---|
252 | } |
---|
253 | |
---|
254 | { // argument: unsigned short |
---|
255 | mpf_class f ((unsigned short) 1); |
---|
256 | CHECK_MPF ("(unsigned short) 1", "1.0"); |
---|
257 | } |
---|
258 | |
---|
259 | { // argument: long |
---|
260 | mpf_class f (-1234567890L); |
---|
261 | CHECK_MPF ("-1234567890L", "-1234567890.0"); |
---|
262 | } |
---|
263 | |
---|
264 | { // argument: double |
---|
265 | mpf_class f (3.125e+4); |
---|
266 | CHECK_MPF ("3.125e+4", "31250.0"); |
---|
267 | } |
---|
268 | |
---|
269 | { // argument: char * |
---|
270 | mpf_class f ("12345678901234567890"); |
---|
271 | CHECK_MPF ("\"12345678901234567890\"", "12345678901234567890.0"); |
---|
272 | } |
---|
273 | |
---|
274 | { // argument: string |
---|
275 | mpf_class f (string ("1234567890")); |
---|
276 | CHECK_MPF ("string (\"1234567890\")", "1234567890.0"); |
---|
277 | } |
---|
278 | |
---|
279 | { // argument: mpf_t |
---|
280 | mpf_class f (ref); |
---|
281 | CHECK_MPF ("ref [mpf_t]", "1234567890.0"); |
---|
282 | } |
---|
283 | |
---|
284 | { // arguments: mpf_class, int |
---|
285 | mpf_class g; |
---|
286 | mpf_class f (g, 64); |
---|
287 | CHECK_MPF ("g [mpf_class], 64", "0.0"); |
---|
288 | } |
---|
289 | |
---|
290 | { // arguments: int, int |
---|
291 | mpf_class f (0, 128); |
---|
292 | CHECK_MPF ("0, 128", "0.0"); |
---|
293 | } |
---|
294 | |
---|
295 | { // arguments: bool, int |
---|
296 | mpf_class f (true, 192); |
---|
297 | CHECK_MPF ("true, 192", "1.0"); |
---|
298 | } |
---|
299 | |
---|
300 | { // arguments: unsigned short, int |
---|
301 | mpf_class f ((unsigned short) 1, 256); |
---|
302 | CHECK_MPF ("(unsigned short) 1, 256", "1.0"); |
---|
303 | } |
---|
304 | |
---|
305 | { // arguments: long, unsigned |
---|
306 | mpf_class f (-1234567890L, 64u); |
---|
307 | CHECK_MPF ("-1234567890L, 64u", "-1234567890.0"); |
---|
308 | } |
---|
309 | |
---|
310 | { // argument: double, unsigned |
---|
311 | mpf_class f (3.125e+4, 128u); |
---|
312 | CHECK_MPF ("3.125e+4, 128u", "31250.0"); |
---|
313 | } |
---|
314 | |
---|
315 | { // argument: char *, unsigned |
---|
316 | mpf_class f ("12345678901234567890", 192u); |
---|
317 | CHECK_MPF ("\"12345678901234567890\", 192u", "12345678901234567890.0"); |
---|
318 | } |
---|
319 | |
---|
320 | { // argument: string, unsigned |
---|
321 | mpf_class f (string ("1234567890", 256u)); |
---|
322 | CHECK_MPF ("string (\"1234567890\", 256u)", "1234567890.0"); |
---|
323 | } |
---|
324 | |
---|
325 | { // argument: mpf_t, long |
---|
326 | mpf_class f (ref, 64L); |
---|
327 | CHECK_MPF ("ref [mpf_t], 64L", "1234567890.0"); |
---|
328 | } |
---|
329 | |
---|
330 | mpf_clear (ref); |
---|
331 | } |
---|
332 | |
---|
333 | |
---|
334 | int |
---|
335 | main (int argc, char *argv[]) |
---|
336 | { |
---|
337 | tests_start (); |
---|
338 | |
---|
339 | check_mpz (); |
---|
340 | check_mpq (); |
---|
341 | check_mpf (); |
---|
342 | |
---|
343 | tests_end (); |
---|
344 | exit (0); |
---|
345 | } |
---|