t-assign.cc revision 1.1.1.1
1/* Test mp*_class assignment operators.
2
3Copyright 2001, 2002, 2003 Free Software Foundation, Inc.
4
5This file is part of the GNU MP Library.
6
7The GNU MP Library is free software; you can redistribute it and/or modify
8it under the terms of the GNU Lesser General Public License as published by
9the Free Software Foundation; either version 3 of the License, or (at your
10option) any later version.
11
12The GNU MP Library is distributed in the hope that it will be useful, but
13WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
15License for more details.
16
17You should have received a copy of the GNU Lesser General Public License
18along with the GNU MP Library.  If not, see http://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
30using namespace std;
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
190void
191check_mpq (void)
192{
193  // operator=(const mpq_class &)
194  {
195    mpq_class a(1, 2), b;
196    b = a; ASSERT_ALWAYS(b == 0.5);
197  }
198
199  // template <class T, class U> operator=(const __gmp_expr<T, U> &)
200  // not tested here, see t-unary.cc, t-binary.cc
201
202  // operator=(signed char)
203  {
204    signed char a = -127;
205    mpq_class b;
206    b = a; ASSERT_ALWAYS(b == -127);
207  }
208
209  // operator=(unsigned char)
210  {
211    unsigned char a = 255;
212    mpq_class b;
213    b = a; ASSERT_ALWAYS(b == 255);
214  }
215
216  // either signed or unsigned char, machine dependent
217  {
218    mpq_class a;
219    a = 'A'; ASSERT_ALWAYS(a == 65);
220  }
221  {
222    mpq_class a;
223    a = 'z'; ASSERT_ALWAYS(a == 122);
224  }
225
226  // operator=(signed int)
227  {
228    signed int a = 0;
229    mpq_class b;
230    b = a; ASSERT_ALWAYS(b == 0);
231  }
232  {
233    signed int a = -123;
234    mpq_class b;
235    b = a; ASSERT_ALWAYS(b == -123);
236  }
237  {
238    signed int a = 32767;
239    mpq_class b;
240    b = a; ASSERT_ALWAYS(b == 32767);
241  }
242
243  // operator=(unsigned int)
244  {
245    unsigned int a = 65535u;
246    mpq_class b;
247    b = a; ASSERT_ALWAYS(b == 65535u);
248  }
249
250  // operator=(signed short int)
251  {
252    signed short int a = -12345;
253    mpq_class b;
254    b = a; ASSERT_ALWAYS(b == -12345);
255  }
256
257  // operator=(unsigned short int)
258  {
259    unsigned short int a = 54321u;
260    mpz_class b;
261    b = a; ASSERT_ALWAYS(b == 54321u);
262  }
263
264  // operator=(signed long int)
265  {
266    signed long int a = -1234567890L;
267    mpq_class b;
268    b = a; ASSERT_ALWAYS(b == -1234567890L);
269  }
270
271  // operator=(unsigned long int)
272  {
273    unsigned long int a = 3456789012UL;
274    mpq_class b;
275    b = a; ASSERT_ALWAYS(b == 3456789012UL);
276  }
277
278  // operator=(float)
279  {
280    float a = 123.0;
281    mpq_class b;
282    b = a; ASSERT_ALWAYS(b == 123);
283  }
284
285  // operator=(double)
286  {
287    double a = 0.0;
288    mpq_class b;
289    b = a; ASSERT_ALWAYS(b == 0);
290  }
291  {
292    double a = -12.375;
293    mpq_class b;
294    b = a; ASSERT_ALWAYS(b == -12.375);
295  }
296  {
297    double a = 6.789e+3;
298    mpq_class b;
299    b = a; ASSERT_ALWAYS(b == 6789);
300  }
301  {
302    double a = 9.375e-1;
303    mpq_class b;
304    b = a; ASSERT_ALWAYS(b == 0.9375);
305  }
306
307  // operator=(long double)
308  // currently not implemented
309
310  // operator=(const char *)
311  {
312    const char *a = "1234567890";
313    mpq_class b;
314    b = a; ASSERT_ALWAYS(b == 1234567890L);
315  }
316
317  // operator=(const std::string &)
318  {
319    string a("1234567890");
320    mpq_class b;
321    b = a; ASSERT_ALWAYS(b == 1234567890L);
322  }
323
324  // operator=(const char *) with invalid
325  {
326    try {
327      const char *a = "abc";
328      mpq_class b;
329      b = a;
330      ASSERT_ALWAYS (0);  /* should not be reached */
331    } catch (invalid_argument) {
332    }
333  }
334
335  // operator=(const std::string &) with invalid
336  {
337    try {
338      string a("def");
339      mpq_class b;
340      b = a;
341      ASSERT_ALWAYS (0);  /* should not be reached */
342    } catch (invalid_argument) {
343    }
344  }
345}
346
347void
348check_mpf (void)
349{
350  // operator=(const mpf_class &)
351  {
352    mpf_class a(123), b;
353    b = a; ASSERT_ALWAYS(b == 123);
354  }
355
356  // template <class T, class U> operator=(const __gmp_expr<T, U> &)
357  // not tested here, see t-unary.cc, t-binary.cc
358
359  // operator=(signed char)
360  {
361    signed char a = -127;
362    mpf_class b;
363    b = a; ASSERT_ALWAYS(b == -127);
364  }
365
366  // operator=(unsigned char)
367  {
368    unsigned char a = 255;
369    mpf_class b;
370    b = a; ASSERT_ALWAYS(b == 255);
371  }
372
373  // either signed or unsigned char, machine dependent
374  {
375    mpf_class a;
376    a = 'A'; ASSERT_ALWAYS(a == 65);
377  }
378  {
379    mpf_class a;
380    a = 'z'; ASSERT_ALWAYS(a == 122);
381  }
382
383  // operator=(signed int)
384  {
385    signed int a = 0;
386    mpf_class b;
387    b = a; ASSERT_ALWAYS(b == 0);
388  }
389  {
390    signed int a = -123;
391    mpf_class b;
392    b = a; ASSERT_ALWAYS(b == -123);
393  }
394  {
395    signed int a = 32767;
396    mpf_class b;
397    b = a; ASSERT_ALWAYS(b == 32767);
398  }
399
400  // operator=(unsigned int)
401  {
402    unsigned int a = 65535u;
403    mpf_class b;
404    b = a; ASSERT_ALWAYS(b == 65535u);
405  }
406
407  // operator=(signed short int)
408  {
409    signed short int a = -12345;
410    mpf_class b;
411    b = a; ASSERT_ALWAYS(b == -12345);
412  }
413
414  // operator=(unsigned short int)
415  {
416    unsigned short int a = 54321u;
417    mpf_class b;
418    b = a; ASSERT_ALWAYS(b == 54321u);
419  }
420
421  // operator=(signed long int)
422  {
423    signed long int a = -1234567890L;
424    mpf_class b;
425    b = a; ASSERT_ALWAYS(b == -1234567890L);
426  }
427
428  // operator=(unsigned long int)
429  {
430    unsigned long int a = 3456789012UL;
431    mpf_class b;
432    b = a; ASSERT_ALWAYS(b == 3456789012UL);
433  }
434
435  // operator=(float)
436  {
437    float a = 123.0;
438    mpf_class b;
439    b = a; ASSERT_ALWAYS(b == 123);
440  }
441
442  // operator=(double)
443  {
444    double a = 0.0;
445    mpf_class b;
446    b = a; ASSERT_ALWAYS(b == 0);
447  }
448  {
449    double a = -12.375;
450    mpf_class b;
451    b = a; ASSERT_ALWAYS(b == -12.375);
452  }
453  {
454    double a = 6.789e+3;
455    mpf_class b;
456    b = a; ASSERT_ALWAYS(b == 6789);
457  }
458  {
459    double a = 9.375e-1;
460    mpf_class b;
461    b = a; ASSERT_ALWAYS(b == 0.9375);
462  }
463
464  // operator=(long double)
465  // currently not implemented
466
467  // operator=(const char *)
468  {
469    const char *a = "1234567890";
470    mpf_class b;
471    b = a; ASSERT_ALWAYS(b == 1234567890L);
472  }
473
474  // operator=(const std::string &)
475  {
476    string a("1234567890");
477    mpf_class b;
478    b = a; ASSERT_ALWAYS(b == 1234567890L);
479  }
480
481  // operator=(const char *) with invalid
482  {
483    try {
484      const char *a = "abc";
485      mpf_class b;
486      b = a;
487      ASSERT_ALWAYS (0);  /* should not be reached */
488    } catch (invalid_argument) {
489    }
490  }
491
492  // operator=(const std::string &) with invalid
493  {
494    try {
495      string a("def");
496      mpf_class b;
497      b = a;
498      ASSERT_ALWAYS (0);  /* should not be reached */
499    } catch (invalid_argument) {
500    }
501  }
502}
503
504
505int
506main (void)
507{
508  tests_start();
509
510  check_mpz();
511  check_mpq();
512  check_mpf();
513
514  tests_end();
515  return 0;
516}
517