t-constr.cc revision 1.1.1.1
1/* Test mp*_class constructors.
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  // mpz_class()
37  {
38    mpz_class a; ASSERT_ALWAYS(a == 0);
39  }
40
41  // mpz_class(const mpz_class &)
42  // see below
43
44  // template <class T, class U> mpz_class(const __gmp_expr<T, U> &)
45  // not tested here, see t-unary.cc, t-binary.cc
46
47  // mpz_class(signed char)
48  {
49    signed char a = -127;
50    mpz_class b(a); ASSERT_ALWAYS(b == -127);
51  }
52
53  // mpz_class(unsigned char)
54  {
55    unsigned char a = 255;
56    mpz_class b(a); ASSERT_ALWAYS(b == 255);
57  }
58
59  // either signed or unsigned char, machine dependent
60  {
61    mpz_class a('A'); ASSERT_ALWAYS(a == 65);
62  }
63  {
64    mpz_class a('z'); ASSERT_ALWAYS(a == 122);
65  }
66
67  // mpz_class(signed int)
68  {
69    signed int a = 0;
70    mpz_class b(a); ASSERT_ALWAYS(b == 0);
71  }
72  {
73    signed int a = -123;
74    mpz_class b(a); ASSERT_ALWAYS(b == -123);
75  }
76  {
77    signed int a = 4567;
78    mpz_class b(a); ASSERT_ALWAYS(b == 4567);
79  }
80
81  // mpz_class(unsigned int)
82  {
83    unsigned int a = 890;
84    mpz_class b(a); ASSERT_ALWAYS(b == 890);
85  }
86
87  // mpz_class(signed short int)
88  {
89    signed short int a = -12345;
90    mpz_class b(a); ASSERT_ALWAYS(b == -12345);
91  }
92
93  // mpz_class(unsigned short int)
94  {
95    unsigned short int a = 54321u;
96    mpz_class b(a); ASSERT_ALWAYS(b == 54321u);
97  }
98
99  // mpz_class(signed long int)
100  {
101    signed long int a = -1234567890L;
102    mpz_class b(a); ASSERT_ALWAYS(b == -1234567890L);
103  }
104
105  // mpz_class(unsigned long int)
106  {
107    unsigned long int a = 1UL << 30;
108    mpz_class b(a); ASSERT_ALWAYS(b == 1073741824L);
109  }
110
111  // mpz_class(float)
112  {
113    float a = 123.45;
114    mpz_class b(a); ASSERT_ALWAYS(b == 123);
115  }
116
117  // mpz_class(double)
118  {
119    double a = 3.141592653589793238;
120    mpz_class b(a); ASSERT_ALWAYS(b == 3);
121  }
122
123  // mpz_class(long double)
124  // currently not implemented
125
126  // mpz_class(const char *)
127  {
128    const char *a = "1234567890";
129    mpz_class b(a); ASSERT_ALWAYS(b == 1234567890L);
130  }
131
132  // mpz_class(const char *, int)
133  {
134    const char *a = "FFFF";
135    int base = 16;
136    mpz_class b(a, base); ASSERT_ALWAYS(b == 65535u);
137  }
138
139  // mpz_class(const std::string &)
140  {
141    string a("1234567890");
142    mpz_class b(a); ASSERT_ALWAYS(b == 1234567890L);
143  }
144
145  // mpz_class(const std::string &, int)
146  {
147    string a("7777");
148    int base = 8;
149    mpz_class b(a, base); ASSERT_ALWAYS(b == 4095);
150  }
151
152  // mpz_class(const char *) with invalid
153  {
154    try {
155      const char *a = "ABC";
156      mpz_class b(a);
157      ASSERT_ALWAYS (0);  /* should not be reached */
158    } catch (invalid_argument) {
159    }
160  }
161
162  // mpz_class(const char *, int) with invalid
163  {
164    try {
165      const char *a = "GHI";
166      int base = 16;
167      mpz_class b(a, base);
168      ASSERT_ALWAYS (0);  /* should not be reached */
169    } catch (invalid_argument) {
170    }
171  }
172
173  // mpz_class(const std::string &) with invalid
174  {
175    try {
176      string a("abc");
177      mpz_class b(a);
178      ASSERT_ALWAYS (0);  /* should not be reached */
179    } catch (invalid_argument) {
180    }
181  }
182
183  // mpz_class(const std::string &, int) with invalid
184  {
185    try {
186      string a("ZZZ");
187      int base = 8;
188      mpz_class b(a, base);
189      ASSERT_ALWAYS (0);  /* should not be reached */
190    } catch (invalid_argument) {
191    }
192  }
193
194  // mpz_class(mpz_srcptr)
195  {
196    mpz_t a;
197    mpz_init_set_ui(a, 100);
198    mpz_class b(a); ASSERT_ALWAYS(b == 100);
199    mpz_clear(a);
200  }
201
202  // mpz_class(const mpz_class &)
203  {
204    mpz_class a(12345); // tested above, assume it works
205    mpz_class b(a); ASSERT_ALWAYS(b == 12345);
206  }
207
208  // no constructor for bool, but it gets casted to int
209  {
210    bool a = true;
211    mpz_class b(a); ASSERT_ALWAYS(b == 1);
212  }
213  {
214    bool a = false;
215    mpz_class b(a); ASSERT_ALWAYS(b == 0);
216  }
217}
218
219void
220check_mpq (void)
221{
222  // mpq_class()
223  {
224    mpq_class a; ASSERT_ALWAYS(a == 0);
225  }
226
227  // mpq_class(const mpq_class &)
228  // see below
229
230  // template <class T, class U> mpq_class(const __gmp_expr<T, U> &)
231  // not tested here, see t-unary.cc, t-binary.cc
232
233  // mpq_class(signed char)
234  {
235    signed char a = -127;
236    mpq_class b(a); ASSERT_ALWAYS(b == -127);
237  }
238
239  // mpq_class(unsigned char)
240  {
241    unsigned char a = 255;
242    mpq_class b(a); ASSERT_ALWAYS(b == 255);
243  }
244
245  // either signed or unsigned char, machine dependent
246  {
247    mpq_class a('A'); ASSERT_ALWAYS(a == 65);
248  }
249  {
250    mpq_class a('z'); ASSERT_ALWAYS(a == 122);
251  }
252
253  // mpq_class(signed int)
254  {
255    signed int a = 0;
256    mpq_class b(a); ASSERT_ALWAYS(b == 0);
257  }
258  {
259    signed int a = -123;
260    mpq_class b(a); ASSERT_ALWAYS(b == -123);
261  }
262  {
263    signed int a = 4567;
264    mpq_class b(a); ASSERT_ALWAYS(b == 4567);
265  }
266
267  // mpq_class(unsigned int)
268  {
269    unsigned int a = 890;
270    mpq_class b(a); ASSERT_ALWAYS(b == 890);
271  }
272
273  // mpq_class(signed short int)
274  {
275    signed short int a = -12345;
276    mpq_class b(a); ASSERT_ALWAYS(b == -12345);
277  }
278
279  // mpq_class(unsigned short int)
280  {
281    unsigned short int a = 54321u;
282    mpq_class b(a); ASSERT_ALWAYS(b == 54321u);
283  }
284
285  // mpq_class(signed long int)
286  {
287    signed long int a = -1234567890L;
288    mpq_class b(a); ASSERT_ALWAYS(b == -1234567890L);
289  }
290
291  // mpq_class(unsigned long int)
292  {
293    unsigned long int a = 1UL << 30;
294    mpq_class b(a); ASSERT_ALWAYS(b == 1073741824L);
295  }
296
297  // mpq_class(float)
298  {
299    float a = 0.625;
300    mpq_class b(a); ASSERT_ALWAYS(b == 0.625);
301  }
302
303  // mpq_class(double)
304  {
305    double a = 1.25;
306    mpq_class b(a); ASSERT_ALWAYS(b == 1.25);
307  }
308
309  // mpq_class(long double)
310  // currently not implemented
311
312  // mpq_class(const char *)
313  {
314    const char *a = "1234567890";
315    mpq_class b(a); ASSERT_ALWAYS(b == 1234567890L);
316  }
317
318  // mpq_class(const char *, int)
319  {
320    const char *a = "FFFF";
321    int base = 16;
322    mpq_class b(a, base); ASSERT_ALWAYS(b == 65535u);
323  }
324
325  // mpq_class(const std::string &)
326  {
327    string a("1234567890");
328    mpq_class b(a); ASSERT_ALWAYS(b == 1234567890L);
329  }
330
331  // mpq_class(const std::string &, int)
332  {
333    string a("7777");
334    int base = 8;
335    mpq_class b(a, base); ASSERT_ALWAYS(b == 4095);
336  }
337
338  // mpq_class(const char *) with invalid
339  {
340    try {
341      const char *a = "abc";
342      mpq_class b(a);
343      ASSERT_ALWAYS (0);  /* should not be reached */
344    } catch (invalid_argument) {
345    }
346  }
347
348  // mpq_class(const char *, int) with invalid
349  {
350    try {
351      const char *a = "ZZZ";
352      int base = 16;
353      mpq_class b (a, base);
354      ASSERT_ALWAYS (0);  /* should not be reached */
355    } catch (invalid_argument) {
356    }
357  }
358
359  // mpq_class(const std::string &) with invalid
360  {
361    try {
362      string a("abc");
363      mpq_class b(a);
364      ASSERT_ALWAYS (0);  /* should not be reached */
365    } catch (invalid_argument) {
366    }
367  }
368
369  // mpq_class(const std::string &, int) with invalid
370  {
371    try {
372      string a("ZZZ");
373      int base = 8;
374      mpq_class b (a, base);
375      ASSERT_ALWAYS (0);  /* should not be reached */
376    } catch (invalid_argument) {
377    }
378  }
379
380  // mpq_class(mpq_srcptr)
381  {
382    mpq_t a;
383    mpq_init(a);
384    mpq_set_ui(a, 100, 1);
385    mpq_class b(a); ASSERT_ALWAYS(b == 100);
386    mpq_clear(a);
387  }
388
389  // mpq_class(const mpz_class &, const mpz_class &)
390  {
391    mpz_class a(123), b(4); // tested above, assume it works
392    mpq_class c(a, b); ASSERT_ALWAYS(c == 30.75);
393  }
394  {
395    mpz_class a(-1), b(2);  // tested above, assume it works
396    mpq_class c(a, b); ASSERT_ALWAYS(c == -0.5);
397  }
398  {
399    mpz_class a(5), b(4); // tested above, assume it works
400    mpq_class c(a, b); ASSERT_ALWAYS(c == 1.25);
401  }
402
403  // mpq_class(const mpz_class &)
404  {
405    mpq_class a(12345); // tested above, assume it works
406    mpq_class b(a); ASSERT_ALWAYS(b == 12345);
407  }
408
409  // no constructor for bool, but it gets casted to int
410  {
411    bool a = true;
412    mpq_class b(a); ASSERT_ALWAYS(b == 1);
413  }
414  {
415    bool a = false;
416    mpq_class b(a); ASSERT_ALWAYS(b == 0);
417  }
418}
419
420void
421check_mpf (void)
422{
423  // mpf_class()
424  {
425    mpf_class a; ASSERT_ALWAYS(a == 0);
426  }
427
428  // mpf_class(const mpf_class &)
429  // mpf_class(const mpf_class &, unsigned long int)
430  // see below
431
432  // template <class T, class U> mpf_class(const __gmp_expr<T, U> &)
433  // template <class T, class U> mpf_class(const __gmp_expr<T, U> &,
434  //                                       unsigned long int)
435  // not tested here, see t-unary.cc, t-binary.cc
436
437  // mpf_class(signed char)
438  {
439    signed char a = -127;
440    mpf_class b(a); ASSERT_ALWAYS(b == -127);
441  }
442
443  // mpf_class(signed char, unsigned long int)
444  {
445    signed char a = -1;
446    int prec = 64;
447    mpf_class b(a, prec); ASSERT_ALWAYS(b == -1);
448  }
449
450  // mpf_class(unsigned char)
451  {
452    unsigned char a = 255;
453    mpf_class b(a); ASSERT_ALWAYS(b == 255);
454  }
455
456  // mpf_class(unsigned char, unsigned long int)
457  {
458    unsigned char a = 128;
459    int prec = 128;
460    mpf_class b(a, prec); ASSERT_ALWAYS(b == 128);
461  }
462
463  // either signed or unsigned char, machine dependent
464  {
465    mpf_class a('A'); ASSERT_ALWAYS(a == 65);
466  }
467  {
468    int prec = 256;
469    mpf_class a('z', prec); ASSERT_ALWAYS(a == 122);
470  }
471
472  // mpf_class(signed int)
473  {
474    signed int a = 0;
475    mpf_class b(a); ASSERT_ALWAYS(b == 0);
476  }
477  {
478    signed int a = -123;
479    mpf_class b(a); ASSERT_ALWAYS(b == -123);
480  }
481  {
482    signed int a = 4567;
483    mpf_class b(a); ASSERT_ALWAYS(b == 4567);
484  }
485
486  // mpf_class(signed int, unsigned long int)
487  {
488    signed int a = -123;
489    int prec = 64;
490    mpf_class b(a, prec); ASSERT_ALWAYS(b == -123);
491  }
492
493  // mpf_class(unsigned int)
494  {
495    unsigned int a = 890;
496    mpf_class b(a); ASSERT_ALWAYS(b == 890);
497  }
498
499  // mpf_class(unsigned int, unsigned long int)
500  {
501    unsigned int a = 890;
502    int prec = 128;
503    mpf_class b(a, prec); ASSERT_ALWAYS(b == 890);
504  }
505
506  // mpf_class(signed short int)
507  {
508    signed short int a = -12345;
509    mpf_class b(a); ASSERT_ALWAYS(b == -12345);
510  }
511
512  // mpf_class(signed short int, unsigned long int)
513  {
514    signed short int a = 6789;
515    int prec = 256;
516    mpf_class b(a, prec); ASSERT_ALWAYS(b == 6789);
517  }
518
519  // mpf_class(unsigned short int)
520  {
521    unsigned short int a = 54321u;
522    mpf_class b(a); ASSERT_ALWAYS(b == 54321u);
523  }
524
525  // mpf_class(unsigned short int, unsigned long int)
526  {
527    unsigned short int a = 54321u;
528    int prec = 64;
529    mpf_class b(a, prec); ASSERT_ALWAYS(b == 54321u);
530  }
531
532  // mpf_class(signed long int)
533  {
534    signed long int a = -1234567890L;
535    mpf_class b(a); ASSERT_ALWAYS(b == -1234567890L);
536  }
537
538  // mpf_class(signed long int, unsigned long int)
539  {
540    signed long int a = -1234567890L;
541    int prec = 128;
542    mpf_class b(a, prec); ASSERT_ALWAYS(b == -1234567890L);
543  }
544
545  // mpf_class(unsigned long int)
546  {
547    unsigned long int a = 3456789012UL;
548    mpf_class b(a); ASSERT_ALWAYS(b == 3456789012UL);
549  }
550
551  // mpf_class(unsigned long int, unsigned long int)
552  {
553    unsigned long int a = 3456789012UL;
554    int prec = 256;
555    mpf_class b(a, prec); ASSERT_ALWAYS(b == 3456789012UL);
556  }
557
558  // mpf_class(float)
559  {
560    float a = 1234.5;
561    mpf_class b(a); ASSERT_ALWAYS(b == 1234.5);
562  }
563
564  // mpf_class(float, unsigned long int)
565  {
566    float a = 1234.5;
567    int prec = 64;
568    mpf_class b(a, prec); ASSERT_ALWAYS(b == 1234.5);
569  }
570
571  // mpf_class(double)
572  {
573    double a = 12345.0;
574    mpf_class b(a); ASSERT_ALWAYS(b == 12345);
575  }
576  {
577    double a = 1.2345e+4;
578    mpf_class b(a); ASSERT_ALWAYS(b == 12345);
579  }
580  {
581    double a = 312.5e-2;
582    mpf_class b(a); ASSERT_ALWAYS(b == 3.125);
583  }
584
585  // mpf_class(double, unsigned long int)
586  {
587    double a = 5.4321e+4;
588    int prec = 128;
589    mpf_class b(a, prec); ASSERT_ALWAYS(b == 54321L);
590  }
591
592  // mpf_class(long double)
593  // mpf_class(long double, unsigned long int)
594  // currently not implemented
595
596  // mpf_class(const char *)
597  {
598    const char *a = "1234567890";
599    mpf_class b(a); ASSERT_ALWAYS(b == 1234567890L);
600  }
601
602  // mpf_class(const char *, unsigned long int, int = 0)
603  {
604    const char *a = "1234567890";
605    int prec = 256;
606    mpf_class b(a, prec); ASSERT_ALWAYS(b == 1234567890L);
607  }
608  {
609    const char *a = "777777";
610    int prec = 64, base = 8;
611    mpf_class b(a, prec, base); ASSERT_ALWAYS(b == 262143L);
612  }
613
614  // mpf_class(const std::string &)
615  {
616    string a("1234567890");
617    mpf_class b(a); ASSERT_ALWAYS(b == 1234567890L);
618  }
619
620  // mpf_class(const std::string &, unsigned long int, int = 0)
621  {
622    string a("1234567890");
623    int prec = 128;
624    mpf_class b(a, prec); ASSERT_ALWAYS(b == 1234567890L);
625  }
626  {
627    string a("FFFF");
628    int prec = 256, base = 16;
629    mpf_class b(a, prec, base); ASSERT_ALWAYS(b == 65535u);
630  }
631
632  // mpf_class(const char *) with invalid
633  {
634    try {
635      const char *a = "abc";
636      mpf_class b(a);
637      ASSERT_ALWAYS (0);  /* should not be reached */
638    } catch (invalid_argument) {
639    }
640  }
641
642  // mpf_class(const char *, unsigned long int, int = 0) with invalid
643  {
644    try {
645      const char *a = "def";
646      int prec = 256;
647      mpf_class b(a, prec); ASSERT_ALWAYS(b == 1234567890L);
648      ASSERT_ALWAYS (0);  /* should not be reached */
649    } catch (invalid_argument) {
650    }
651  }
652  {
653    try {
654      const char *a = "ghi";
655      int prec = 64, base = 8;
656      mpf_class b(a, prec, base); ASSERT_ALWAYS(b == 262143L);
657      ASSERT_ALWAYS (0);  /* should not be reached */
658    } catch (invalid_argument) {
659    }
660  }
661
662  // mpf_class(const std::string &) with invalid
663  {
664    try {
665      string a("abc");
666      mpf_class b(a); ASSERT_ALWAYS(b == 1234567890L);
667      ASSERT_ALWAYS (0);  /* should not be reached */
668    } catch (invalid_argument) {
669    }
670  }
671
672  // mpf_class(const std::string &, unsigned long int, int = 0) with invalid
673  {
674    try {
675      string a("def");
676      int prec = 128;
677      mpf_class b(a, prec); ASSERT_ALWAYS(b == 1234567890L);
678      ASSERT_ALWAYS (0);  /* should not be reached */
679    } catch (invalid_argument) {
680    }
681  }
682  {
683    try {
684      string a("ghi");
685      int prec = 256, base = 16;
686      mpf_class b(a, prec, base); ASSERT_ALWAYS(b == 65535u);
687      ASSERT_ALWAYS (0);  /* should not be reached */
688    } catch (invalid_argument) {
689    }
690  }
691
692  // mpf_class(mpf_srcptr)
693  {
694    mpf_t a;
695    mpf_init_set_ui(a, 100);
696    mpf_class b(a); ASSERT_ALWAYS(b == 100);
697    mpf_clear(a);
698  }
699
700  // mpf_class(mpf_srcptr, unsigned long int)
701  {
702    mpf_t a;
703    int prec = 64;
704    mpf_init_set_ui(a, 100);
705    mpf_class b(a, prec); ASSERT_ALWAYS(b == 100);
706    mpf_clear(a);
707  }
708
709  // mpf_class(const mpf_class &)
710  {
711    mpf_class a(12345); // tested above, assume it works
712    mpf_class b(a); ASSERT_ALWAYS(b == 12345);
713  }
714
715  // mpf_class(const mpf_class &, unsigned long int)
716  {
717    mpf_class a(12345); // tested above, assume it works
718    int prec = 64;
719    mpf_class b(a, prec); ASSERT_ALWAYS(b == 12345);
720  }
721
722  // no constructors for bool, but it gets casted to int
723  {
724    bool a = true;
725    mpf_class b(a); ASSERT_ALWAYS(b == 1);
726  }
727  {
728    bool a = false;
729    mpf_class b(a); ASSERT_ALWAYS(b == 0);
730  }
731  {
732    bool a = true;
733    int prec = 128;
734    mpf_class b(a, prec); ASSERT_ALWAYS(b == 1);
735  }
736  {
737    bool a = false;
738    int prec = 256;
739    mpf_class b(a, prec); ASSERT_ALWAYS(b == 0);
740  }
741}
742
743
744int
745main (void)
746{
747  tests_start();
748
749  check_mpz();
750  check_mpq();
751  check_mpf();
752
753  tests_end();
754  return 0;
755}
756