1 /* Test mp*_class assignment operators.
2
3 Copyright 2001, 2002, 2003 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., 51 Franklin Street, Fifth Floor, Boston,
20 MA 02110-1301, USA. */
21
22 #include <iostream>
23 #include <string>
24
25 #include "mpir.h"
26 #include "mpirxx.h"
27 #include "gmp-impl.h"
28 #include "tests.h"
29
30 using namespace std;
31
32
33 void
check_mpz(void)34 check_mpz (void)
35 {
36 // operator=(const mpz_class &)
37 {
38 mpz_class a(123), b;
39 b = a; ASSERT_ALWAYS(b == 123);
40 }
41
42 // template <class T, class U> operator=(const __gmp_expr<T, U> &)
43 // not tested here, see t-unary.cc, t-binary.cc
44
45 // operator=(signed char)
46 {
47 signed char a = -127;
48 mpz_class b;
49 b = a; ASSERT_ALWAYS(b == -127);
50 }
51
52 // operator=(unsigned char)
53 {
54 unsigned char a = 255;
55 mpz_class b;
56 b = a; ASSERT_ALWAYS(b == 255);
57 }
58
59 // either signed or unsigned char, machine dependent
60 {
61 mpz_class a;
62 a = 'A'; ASSERT_ALWAYS(a == 65);
63 }
64 {
65 mpz_class a;
66 a = 'z'; ASSERT_ALWAYS(a == 122);
67 }
68
69 // operator=(signed int)
70 {
71 signed int a = 0;
72 mpz_class b;
73 b = a; ASSERT_ALWAYS(b == 0);
74 }
75 {
76 signed int a = -123;
77 mpz_class b;
78 b = a; ASSERT_ALWAYS(b == -123);
79 }
80 {
81 signed int a = 32767;
82 mpz_class b;
83 b = a; ASSERT_ALWAYS(b == 32767);
84 }
85
86 // operator=(unsigned int)
87 {
88 unsigned int a = 65535u;
89 mpz_class b;
90 b = a; ASSERT_ALWAYS(b == 65535u);
91 }
92
93 // operator=(signed short int)
94 {
95 signed short int a = -12345;
96 mpz_class b;
97 b = a; ASSERT_ALWAYS(b == -12345);
98 }
99
100 // operator=(unsigned short int)
101 {
102 unsigned short int a = 54321u;
103 mpz_class b;
104 b = a; ASSERT_ALWAYS(b == 54321u);
105 }
106
107 // operator=(signed long int)
108 {
109 signed long int a = -1234567890L;
110 mpz_class b;
111 b = a; ASSERT_ALWAYS(b == -1234567890L);
112 }
113
114 // operator=(unsigned long int)
115 {
116 unsigned long int a = 3456789012UL;
117 mpz_class b;
118 b = a; ASSERT_ALWAYS(b == 3456789012UL);
119 }
120
121 #ifdef MPIRXX_HAVE_LLONG
122 // operator=(unsigned long long int)
123 {
124 unsigned long long int a = 0x1234567812345678ULL;
125 mpz_class b;
126 b = a;
127 ASSERT_ALWAYS(b == 0x1234567812345678ULL);
128 }
129
130 // operator=(unsigned long long int)
131 {
132 long long int a = 0xfedcba9876543210ULL;
133 mpz_class b;
134 b = a;
135 ASSERT_ALWAYS(b == (mpir_si)0xfedcba9876543210ULL);
136 }
137 #endif
138
139 // operator=(float)
140 {
141 float a = 123.0;
142 mpz_class b;
143 b = a; ASSERT_ALWAYS(b == 123);
144 }
145
146 // operator=(double)
147 {
148 double a = 0.0;
149 mpz_class b;
150 b = a; ASSERT_ALWAYS(b == 0);
151 }
152 {
153 double a = -12.375;
154 mpz_class b;
155 b = a; ASSERT_ALWAYS(b == -12);
156 }
157 {
158 double a = 6.789e+3;
159 mpz_class b;
160 b = a; ASSERT_ALWAYS(b == 6789);
161 }
162 {
163 double a = 9.375e-1;
164 mpz_class b;
165 b = a; ASSERT_ALWAYS(b == 0);
166 }
167
168 // operator=(long double)
169 // currently not implemented
170
171 // operator=(const char *)
172 {
173 const char *a = "1234567890";
174 mpz_class b;
175 b = a; ASSERT_ALWAYS(b == 1234567890L);
176 }
177
178 // operator=(const std::string &)
179 {
180 string a("1234567890");
181 mpz_class b;
182 b = a; ASSERT_ALWAYS(b == 1234567890L);
183 }
184
185 // operator=(const char *) with invalid
186 {
187 try {
188 const char *a = "abc";
189 mpz_class b;
190 b = a;
191 ASSERT_ALWAYS (0); /* should not be reached */
192 } catch (invalid_argument) {
193 }
194 }
195
196 // operator=(const std::string &) with invalid
197 {
198 try {
199 string a("def");
200 mpz_class b;
201 b = a;
202 ASSERT_ALWAYS (0); /* should not be reached */
203 } catch (invalid_argument) {
204 }
205 }
206 }
207
208 void
check_mpq(void)209 check_mpq (void)
210 {
211 // operator=(const mpq_class &)
212 {
213 mpq_class a(1, 2), b;
214 b = a; ASSERT_ALWAYS(b == 0.5);
215 }
216
217 // template <class T, class U> operator=(const __gmp_expr<T, U> &)
218 // not tested here, see t-unary.cc, t-binary.cc
219
220 // operator=(signed char)
221 {
222 signed char a = -127;
223 mpq_class b;
224 b = a; ASSERT_ALWAYS(b == -127);
225 }
226
227 // operator=(unsigned char)
228 {
229 unsigned char a = 255;
230 mpq_class b;
231 b = a; ASSERT_ALWAYS(b == 255);
232 }
233
234 // either signed or unsigned char, machine dependent
235 {
236 mpq_class a;
237 a = 'A'; ASSERT_ALWAYS(a == 65);
238 }
239 {
240 mpq_class a;
241 a = 'z'; ASSERT_ALWAYS(a == 122);
242 }
243
244 // operator=(signed int)
245 {
246 signed int a = 0;
247 mpq_class b;
248 b = a; ASSERT_ALWAYS(b == 0);
249 }
250 {
251 signed int a = -123;
252 mpq_class b;
253 b = a; ASSERT_ALWAYS(b == -123);
254 }
255 {
256 signed int a = 32767;
257 mpq_class b;
258 b = a; ASSERT_ALWAYS(b == 32767);
259 }
260
261 // operator=(unsigned int)
262 {
263 unsigned int a = 65535u;
264 mpq_class b;
265 b = a; ASSERT_ALWAYS(b == 65535u);
266 }
267
268 // operator=(signed short int)
269 {
270 signed short int a = -12345;
271 mpq_class b;
272 b = a; ASSERT_ALWAYS(b == -12345);
273 }
274
275 // operator=(unsigned short int)
276 {
277 unsigned short int a = 54321u;
278 mpz_class b;
279 b = a; ASSERT_ALWAYS(b == 54321u);
280 }
281
282 // operator=(signed long int)
283 {
284 signed long int a = -1234567890L;
285 mpq_class b;
286 b = a; ASSERT_ALWAYS(b == -1234567890L);
287 }
288
289 // operator=(unsigned long int)
290 {
291 unsigned long int a = 3456789012UL;
292 mpq_class b;
293 b = a; ASSERT_ALWAYS(b == 3456789012UL);
294 }
295
296 // operator=(float)
297 {
298 float a = 123.0;
299 mpq_class b;
300 b = a; ASSERT_ALWAYS(b == 123);
301 }
302
303 // operator=(double)
304 {
305 double a = 0.0;
306 mpq_class b;
307 b = a; ASSERT_ALWAYS(b == 0);
308 }
309 {
310 double a = -12.375;
311 mpq_class b;
312 b = a; ASSERT_ALWAYS(b == -12.375);
313 }
314 {
315 double a = 6.789e+3;
316 mpq_class b;
317 b = a; ASSERT_ALWAYS(b == 6789);
318 }
319 {
320 double a = 9.375e-1;
321 mpq_class b;
322 b = a; ASSERT_ALWAYS(b == 0.9375);
323 }
324
325 // operator=(long double)
326 // currently not implemented
327
328 // operator=(const char *)
329 {
330 const char *a = "1234567890";
331 mpq_class b;
332 b = a; ASSERT_ALWAYS(b == 1234567890L);
333 }
334
335 // operator=(const std::string &)
336 {
337 string a("1234567890");
338 mpq_class b;
339 b = a; ASSERT_ALWAYS(b == 1234567890L);
340 }
341
342 // operator=(const char *) with invalid
343 {
344 try {
345 const char *a = "abc";
346 mpq_class b;
347 b = a;
348 ASSERT_ALWAYS (0); /* should not be reached */
349 } catch (invalid_argument) {
350 }
351 }
352
353 // operator=(const std::string &) with invalid
354 {
355 try {
356 string a("def");
357 mpq_class b;
358 b = a;
359 ASSERT_ALWAYS (0); /* should not be reached */
360 } catch (invalid_argument) {
361 }
362 }
363 }
364
365 void
check_mpf(void)366 check_mpf (void)
367 {
368 // operator=(const mpf_class &)
369 {
370 mpf_class a(123), b;
371 b = a; ASSERT_ALWAYS(b == 123);
372 }
373
374 // template <class T, class U> operator=(const __gmp_expr<T, U> &)
375 // not tested here, see t-unary.cc, t-binary.cc
376
377 // operator=(signed char)
378 {
379 signed char a = -127;
380 mpf_class b;
381 b = a; ASSERT_ALWAYS(b == -127);
382 }
383
384 // operator=(unsigned char)
385 {
386 unsigned char a = 255;
387 mpf_class b;
388 b = a; ASSERT_ALWAYS(b == 255);
389 }
390
391 // either signed or unsigned char, machine dependent
392 {
393 mpf_class a;
394 a = 'A'; ASSERT_ALWAYS(a == 65);
395 }
396 {
397 mpf_class a;
398 a = 'z'; ASSERT_ALWAYS(a == 122);
399 }
400
401 // operator=(signed int)
402 {
403 signed int a = 0;
404 mpf_class b;
405 b = a; ASSERT_ALWAYS(b == 0);
406 }
407 {
408 signed int a = -123;
409 mpf_class b;
410 b = a; ASSERT_ALWAYS(b == -123);
411 }
412 {
413 signed int a = 32767;
414 mpf_class b;
415 b = a; ASSERT_ALWAYS(b == 32767);
416 }
417
418 // operator=(unsigned int)
419 {
420 unsigned int a = 65535u;
421 mpf_class b;
422 b = a; ASSERT_ALWAYS(b == 65535u);
423 }
424
425 // operator=(signed short int)
426 {
427 signed short int a = -12345;
428 mpf_class b;
429 b = a; ASSERT_ALWAYS(b == -12345);
430 }
431
432 // operator=(unsigned short int)
433 {
434 unsigned short int a = 54321u;
435 mpf_class b;
436 b = a; ASSERT_ALWAYS(b == 54321u);
437 }
438
439 // operator=(signed long int)
440 {
441 signed long int a = -1234567890L;
442 mpf_class b;
443 b = a; ASSERT_ALWAYS(b == -1234567890L);
444 }
445
446 // operator=(unsigned long int)
447 {
448 unsigned long int a = 3456789012UL;
449 mpf_class b;
450 b = a; ASSERT_ALWAYS(b == 3456789012UL);
451 }
452
453 // operator=(float)
454 {
455 float a = 123.0;
456 mpf_class b;
457 b = a; ASSERT_ALWAYS(b == 123);
458 }
459
460 // operator=(double)
461 {
462 double a = 0.0;
463 mpf_class b;
464 b = a; ASSERT_ALWAYS(b == 0);
465 }
466 {
467 double a = -12.375;
468 mpf_class b;
469 b = a; ASSERT_ALWAYS(b == -12.375);
470 }
471 {
472 double a = 6.789e+3;
473 mpf_class b;
474 b = a; ASSERT_ALWAYS(b == 6789);
475 }
476 {
477 double a = 9.375e-1;
478 mpf_class b;
479 b = a; ASSERT_ALWAYS(b == 0.9375);
480 }
481
482 // operator=(long double)
483 // currently not implemented
484
485 // operator=(const char *)
486 {
487 const char *a = "1234567890";
488 mpf_class b;
489 b = a; ASSERT_ALWAYS(b == 1234567890L);
490 }
491
492 // operator=(const std::string &)
493 {
494 string a("1234567890");
495 mpf_class b;
496 b = a; ASSERT_ALWAYS(b == 1234567890L);
497 }
498
499 // operator=(const char *) with invalid
500 {
501 try {
502 const char *a = "abc";
503 mpf_class b;
504 b = a;
505 ASSERT_ALWAYS (0); /* should not be reached */
506 } catch (invalid_argument) {
507 }
508 }
509
510 // operator=(const std::string &) with invalid
511 {
512 try {
513 string a("def");
514 mpf_class b;
515 b = a;
516 ASSERT_ALWAYS (0); /* should not be reached */
517 } catch (invalid_argument) {
518 }
519 }
520 }
521
522
523
524 int
main(void)525 main (void)
526 {
527 tests_start();
528
529 check_mpz();
530 check_mpq();
531 check_mpf();
532
533 tests_end();
534 return 0;
535 }
536