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