1 /* Test mp*_class assignment operators.
2
3 Copyright 2001-2003 Free Software Foundation, Inc.
4
5 This file is part of the GNU MP Library test suite.
6
7 The GNU MP Library test suite is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 3 of the License,
10 or (at your option) any later version.
11
12 The GNU MP Library test suite is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
15 Public License for more details.
16
17 You should have received a copy of the GNU General Public License along with
18 the GNU MP Library test suite. If not, see https://www.gnu.org/licenses/. */
19
20 #include "config.h"
21
22 #include <iostream>
23 #include <string>
24
25 #include "gmp.h"
26 #include "gmpxx.h"
27 #include "gmp-impl.h"
28 #include "tests.h"
29
30 using std::string;
31 using std::invalid_argument;
32
33
34 void
check_mpz(void)35 check_mpz (void)
36 {
37 // operator=(const mpz_class &)
38 {
39 mpz_class a(123), b;
40 b = a; ASSERT_ALWAYS(b == 123);
41 }
42
43 // template <class T, class U> operator=(const __gmp_expr<T, U> &)
44 // not tested here, see t-unary.cc, t-binary.cc
45
46 // operator=(signed char)
47 {
48 signed char a = -127;
49 mpz_class b;
50 b = a; ASSERT_ALWAYS(b == -127);
51 }
52
53 // operator=(unsigned char)
54 {
55 unsigned char a = 255;
56 mpz_class b;
57 b = a; ASSERT_ALWAYS(b == 255);
58 }
59
60 // either signed or unsigned char, machine dependent
61 {
62 mpz_class a;
63 a = 'A'; ASSERT_ALWAYS(a == 65);
64 }
65 {
66 mpz_class a;
67 a = 'z'; ASSERT_ALWAYS(a == 122);
68 }
69
70 // operator=(signed int)
71 {
72 signed int a = 0;
73 mpz_class b;
74 b = a; ASSERT_ALWAYS(b == 0);
75 }
76 {
77 signed int a = -123;
78 mpz_class b;
79 b = a; ASSERT_ALWAYS(b == -123);
80 }
81 {
82 signed int a = 32767;
83 mpz_class b;
84 b = a; ASSERT_ALWAYS(b == 32767);
85 }
86
87 // operator=(unsigned int)
88 {
89 unsigned int a = 65535u;
90 mpz_class b;
91 b = a; ASSERT_ALWAYS(b == 65535u);
92 }
93
94 // operator=(signed short int)
95 {
96 signed short int a = -12345;
97 mpz_class b;
98 b = a; ASSERT_ALWAYS(b == -12345);
99 }
100
101 // operator=(unsigned short int)
102 {
103 unsigned short int a = 54321u;
104 mpz_class b;
105 b = a; ASSERT_ALWAYS(b == 54321u);
106 }
107
108 // operator=(signed long int)
109 {
110 signed long int a = -1234567890L;
111 mpz_class b;
112 b = a; ASSERT_ALWAYS(b == -1234567890L);
113 }
114
115 // operator=(unsigned long int)
116 {
117 unsigned long int a = 3456789012UL;
118 mpz_class b;
119 b = a; ASSERT_ALWAYS(b == 3456789012UL);
120 }
121
122 // operator=(float)
123 {
124 float a = 123.0;
125 mpz_class b;
126 b = a; ASSERT_ALWAYS(b == 123);
127 }
128
129 // operator=(double)
130 {
131 double a = 0.0;
132 mpz_class b;
133 b = a; ASSERT_ALWAYS(b == 0);
134 }
135 {
136 double a = -12.375;
137 mpz_class b;
138 b = a; ASSERT_ALWAYS(b == -12);
139 }
140 {
141 double a = 6.789e+3;
142 mpz_class b;
143 b = a; ASSERT_ALWAYS(b == 6789);
144 }
145 {
146 double a = 9.375e-1;
147 mpz_class b;
148 b = a; ASSERT_ALWAYS(b == 0);
149 }
150
151 // operator=(long double)
152 // currently not implemented
153
154 // operator=(const char *)
155 {
156 const char *a = "1234567890";
157 mpz_class b;
158 b = a; ASSERT_ALWAYS(b == 1234567890L);
159 }
160
161 // operator=(const std::string &)
162 {
163 string a("1234567890");
164 mpz_class b;
165 b = a; ASSERT_ALWAYS(b == 1234567890L);
166 }
167
168 // operator=(const char *) with invalid
169 {
170 try {
171 const char *a = "abc";
172 mpz_class b;
173 b = a;
174 ASSERT_ALWAYS (0); /* should not be reached */
175 } catch (invalid_argument) {
176 }
177 }
178
179 // operator=(const std::string &) with invalid
180 {
181 try {
182 string a("def");
183 mpz_class b;
184 b = a;
185 ASSERT_ALWAYS (0); /* should not be reached */
186 } catch (invalid_argument) {
187 }
188 }
189
190 // swap(mpz_class &)
191 {
192 mpz_class a(123);
193 mpz_class b(456);
194 a.swap(b);
195 a.swap(a);
196 ASSERT_ALWAYS(a == 456);
197 ASSERT_ALWAYS(b == 123);
198 }
199
200 // swap(mpz_class &, mpz_class &)
201 {
202 mpz_class a(123);
203 mpz_class b(456);
204 ::swap(a, b);
205 ::swap(a, a);
206 ASSERT_ALWAYS(a == 456);
207 ASSERT_ALWAYS(b == 123);
208 }
209 {
210 using std::swap;
211 mpz_class a(123);
212 mpz_class b(456);
213 swap(a, b);
214 swap(a, a);
215 ASSERT_ALWAYS(a == 456);
216 ASSERT_ALWAYS(b == 123);
217 }
218 }
219
220 void
check_mpq(void)221 check_mpq (void)
222 {
223 // operator=(const mpq_class &)
224 {
225 mpq_class a(1, 2), b;
226 b = a; ASSERT_ALWAYS(b == 0.5);
227 }
228
229 // template <class T, class U> operator=(const __gmp_expr<T, U> &)
230 // not tested here, see t-unary.cc, t-binary.cc
231
232 // operator=(signed char)
233 {
234 signed char a = -127;
235 mpq_class b;
236 b = a; ASSERT_ALWAYS(b == -127);
237 }
238
239 // operator=(unsigned char)
240 {
241 unsigned char a = 255;
242 mpq_class b;
243 b = a; ASSERT_ALWAYS(b == 255);
244 }
245
246 // either signed or unsigned char, machine dependent
247 {
248 mpq_class a;
249 a = 'A'; ASSERT_ALWAYS(a == 65);
250 }
251 {
252 mpq_class a;
253 a = 'z'; ASSERT_ALWAYS(a == 122);
254 }
255
256 // operator=(signed int)
257 {
258 signed int a = 0;
259 mpq_class b;
260 b = a; ASSERT_ALWAYS(b == 0);
261 }
262 {
263 signed int a = -123;
264 mpq_class b;
265 b = a; ASSERT_ALWAYS(b == -123);
266 }
267 {
268 signed int a = 32767;
269 mpq_class b;
270 b = a; ASSERT_ALWAYS(b == 32767);
271 }
272
273 // operator=(unsigned int)
274 {
275 unsigned int a = 65535u;
276 mpq_class b;
277 b = a; ASSERT_ALWAYS(b == 65535u);
278 }
279
280 // operator=(signed short int)
281 {
282 signed short int a = -12345;
283 mpq_class b;
284 b = a; ASSERT_ALWAYS(b == -12345);
285 }
286
287 // operator=(unsigned short int)
288 {
289 unsigned short int a = 54321u;
290 mpq_class b;
291 b = a; ASSERT_ALWAYS(b == 54321u);
292 }
293
294 // operator=(signed long int)
295 {
296 signed long int a = -1234567890L;
297 mpq_class b;
298 b = a; ASSERT_ALWAYS(b == -1234567890L);
299 }
300
301 // operator=(unsigned long int)
302 {
303 unsigned long int a = 3456789012UL;
304 mpq_class b;
305 b = a; ASSERT_ALWAYS(b == 3456789012UL);
306 }
307
308 // operator=(float)
309 {
310 float a = 123.0;
311 mpq_class b;
312 b = a; ASSERT_ALWAYS(b == 123);
313 }
314
315 // operator=(double)
316 {
317 double a = 0.0;
318 mpq_class b;
319 b = a; ASSERT_ALWAYS(b == 0);
320 }
321 {
322 double a = -12.375;
323 mpq_class b;
324 b = a; ASSERT_ALWAYS(b == -12.375);
325 }
326 {
327 double a = 6.789e+3;
328 mpq_class b;
329 b = a; ASSERT_ALWAYS(b == 6789);
330 }
331 {
332 double a = 9.375e-1;
333 mpq_class b;
334 b = a; ASSERT_ALWAYS(b == 0.9375);
335 }
336
337 // operator=(long double)
338 // currently not implemented
339
340 // operator=(const char *)
341 {
342 const char *a = "1234567890";
343 mpq_class b;
344 b = a; ASSERT_ALWAYS(b == 1234567890L);
345 }
346
347 // operator=(const std::string &)
348 {
349 string a("1234567890");
350 mpq_class b;
351 b = a; ASSERT_ALWAYS(b == 1234567890L);
352 }
353
354 // operator=(const char *) with invalid
355 {
356 try {
357 const char *a = "abc";
358 mpq_class b;
359 b = a;
360 ASSERT_ALWAYS (0); /* should not be reached */
361 } catch (invalid_argument) {
362 }
363 }
364
365 // operator=(const std::string &) with invalid
366 {
367 try {
368 string a("def");
369 mpq_class b;
370 b = a;
371 ASSERT_ALWAYS (0); /* should not be reached */
372 } catch (invalid_argument) {
373 }
374 }
375
376 // swap(mpq_class &)
377 {
378 mpq_class a(3, 2);
379 mpq_class b(-1, 4);
380 a.swap(b);
381 a.swap(a);
382 ASSERT_ALWAYS(a == -.25);
383 ASSERT_ALWAYS(b == 1.5);
384 }
385
386 // swap(mpq_class &, mpq_class &)
387 {
388 mpq_class a(3, 2);
389 mpq_class b(-1, 4);
390 ::swap(a, b);
391 ::swap(a, a);
392 ASSERT_ALWAYS(a == -.25);
393 ASSERT_ALWAYS(b == 1.5);
394 }
395 {
396 using std::swap;
397 mpq_class a(3, 2);
398 mpq_class b(-1, 4);
399 swap(a, b);
400 swap(a, a);
401 ASSERT_ALWAYS(a == -.25);
402 ASSERT_ALWAYS(b == 1.5);
403 }
404 }
405
406 void
check_mpf(void)407 check_mpf (void)
408 {
409 // operator=(const mpf_class &)
410 {
411 mpf_class a(123), b;
412 b = a; ASSERT_ALWAYS(b == 123);
413 }
414
415 // template <class T, class U> operator=(const __gmp_expr<T, U> &)
416 // not tested here, see t-unary.cc, t-binary.cc
417
418 // operator=(signed char)
419 {
420 signed char a = -127;
421 mpf_class b;
422 b = a; ASSERT_ALWAYS(b == -127);
423 }
424
425 // operator=(unsigned char)
426 {
427 unsigned char a = 255;
428 mpf_class b;
429 b = a; ASSERT_ALWAYS(b == 255);
430 }
431
432 // either signed or unsigned char, machine dependent
433 {
434 mpf_class a;
435 a = 'A'; ASSERT_ALWAYS(a == 65);
436 }
437 {
438 mpf_class a;
439 a = 'z'; ASSERT_ALWAYS(a == 122);
440 }
441
442 // operator=(signed int)
443 {
444 signed int a = 0;
445 mpf_class b;
446 b = a; ASSERT_ALWAYS(b == 0);
447 }
448 {
449 signed int a = -123;
450 mpf_class b;
451 b = a; ASSERT_ALWAYS(b == -123);
452 }
453 {
454 signed int a = 32767;
455 mpf_class b;
456 b = a; ASSERT_ALWAYS(b == 32767);
457 }
458
459 // operator=(unsigned int)
460 {
461 unsigned int a = 65535u;
462 mpf_class b;
463 b = a; ASSERT_ALWAYS(b == 65535u);
464 }
465
466 // operator=(signed short int)
467 {
468 signed short int a = -12345;
469 mpf_class b;
470 b = a; ASSERT_ALWAYS(b == -12345);
471 }
472
473 // operator=(unsigned short int)
474 {
475 unsigned short int a = 54321u;
476 mpf_class b;
477 b = a; ASSERT_ALWAYS(b == 54321u);
478 }
479
480 // operator=(signed long int)
481 {
482 signed long int a = -1234567890L;
483 mpf_class b;
484 b = a; ASSERT_ALWAYS(b == -1234567890L);
485 }
486
487 // operator=(unsigned long int)
488 {
489 unsigned long int a = 3456789012UL;
490 mpf_class b;
491 b = a; ASSERT_ALWAYS(b == 3456789012UL);
492 }
493
494 // operator=(float)
495 {
496 float a = 123.0;
497 mpf_class b;
498 b = a; ASSERT_ALWAYS(b == 123);
499 }
500
501 // operator=(double)
502 {
503 double a = 0.0;
504 mpf_class b;
505 b = a; ASSERT_ALWAYS(b == 0);
506 }
507 {
508 double a = -12.375;
509 mpf_class b;
510 b = a; ASSERT_ALWAYS(b == -12.375);
511 }
512 {
513 double a = 6.789e+3;
514 mpf_class b;
515 b = a; ASSERT_ALWAYS(b == 6789);
516 }
517 {
518 double a = 9.375e-1;
519 mpf_class b;
520 b = a; ASSERT_ALWAYS(b == 0.9375);
521 }
522
523 // operator=(long double)
524 // currently not implemented
525
526 // operator=(const char *)
527 {
528 const char *a = "1234567890";
529 mpf_class b;
530 b = a; ASSERT_ALWAYS(b == 1234567890L);
531 }
532
533 // operator=(const std::string &)
534 {
535 string a("1234567890");
536 mpf_class b;
537 b = a; ASSERT_ALWAYS(b == 1234567890L);
538 }
539
540 // operator=(const char *) with invalid
541 {
542 try {
543 const char *a = "abc";
544 mpf_class b;
545 b = a;
546 ASSERT_ALWAYS (0); /* should not be reached */
547 } catch (invalid_argument) {
548 }
549 }
550
551 // operator=(const std::string &) with invalid
552 {
553 try {
554 string a("def");
555 mpf_class b;
556 b = a;
557 ASSERT_ALWAYS (0); /* should not be reached */
558 } catch (invalid_argument) {
559 }
560 }
561
562 // swap(mpf_class &)
563 {
564 mpf_class a(123);
565 mpf_class b(456);
566 a.swap(b);
567 a.swap(a);
568 ASSERT_ALWAYS(a == 456);
569 ASSERT_ALWAYS(b == 123);
570 }
571
572 // swap(mpf_class &, mpf_class &)
573 {
574 mpf_class a(123);
575 mpf_class b(456);
576 ::swap(a, b);
577 ::swap(a, a);
578 ASSERT_ALWAYS(a == 456);
579 ASSERT_ALWAYS(b == 123);
580 }
581 {
582 using std::swap;
583 mpf_class a(123);
584 mpf_class b(456);
585 swap(a, b);
586 swap(a, a);
587 ASSERT_ALWAYS(a == 456);
588 ASSERT_ALWAYS(b == 123);
589 }
590 }
591
592
593 int
main(void)594 main (void)
595 {
596 tests_start();
597
598 check_mpz();
599 check_mpq();
600 check_mpf();
601
602 tests_end();
603 return 0;
604 }
605