1 /* Test mp*_class constructors.
3 Copyright 2001, 2002, 2003 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
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 3 of the License, or (at your
10 option) any later version.
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.
17 You should have received a copy of the GNU Lesser General Public License
18 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
38 mpz_class a; ASSERT_ALWAYS(a == 0);
41 // mpz_class(const mpz_class &)
44 // template <class T, class U> mpz_class(const __gmp_expr<T, U> &)
45 // not tested here, see t-unary.cc, t-binary.cc
47 // mpz_class(signed char)
50 mpz_class b(a); ASSERT_ALWAYS(b == -127);
53 // mpz_class(unsigned char)
55 unsigned char a = 255;
56 mpz_class b(a); ASSERT_ALWAYS(b == 255);
59 // either signed or unsigned char, machine dependent
61 mpz_class a('A'); ASSERT_ALWAYS(a == 65);
64 mpz_class a('z'); ASSERT_ALWAYS(a == 122);
67 // mpz_class(signed int)
70 mpz_class b(a); ASSERT_ALWAYS(b == 0);
74 mpz_class b(a); ASSERT_ALWAYS(b == -123);
78 mpz_class b(a); ASSERT_ALWAYS(b == 4567);
81 // mpz_class(unsigned int)
84 mpz_class b(a); ASSERT_ALWAYS(b == 890);
87 // mpz_class(signed short int)
89 signed short int a = -12345;
90 mpz_class b(a); ASSERT_ALWAYS(b == -12345);
93 // mpz_class(unsigned short int)
95 unsigned short int a = 54321u;
96 mpz_class b(a); ASSERT_ALWAYS(b == 54321u);
99 // mpz_class(signed long int)
101 signed long int a = -1234567890L;
102 mpz_class b(a); ASSERT_ALWAYS(b == -1234567890L);
105 // mpz_class(unsigned long int)
107 unsigned long int a = 1UL << 30;
108 mpz_class b(a); ASSERT_ALWAYS(b == 1073741824L);
114 mpz_class b(a); ASSERT_ALWAYS(b == 123);
119 double a = 3.141592653589793238;
120 mpz_class b(a); ASSERT_ALWAYS(b == 3);
123 // mpz_class(long double)
124 // currently not implemented
126 // mpz_class(const char *)
128 const char *a = "1234567890";
129 mpz_class b(a); ASSERT_ALWAYS(b == 1234567890L);
132 // mpz_class(const char *, int)
134 const char *a = "FFFF";
136 mpz_class b(a, base); ASSERT_ALWAYS(b == 65535u);
139 // mpz_class(const std::string &)
141 string a("1234567890");
142 mpz_class b(a); ASSERT_ALWAYS(b == 1234567890L);
145 // mpz_class(const std::string &, int)
149 mpz_class b(a, base); ASSERT_ALWAYS(b == 4095);
152 // mpz_class(const char *) with invalid
155 const char *a = "ABC";
157 ASSERT_ALWAYS (0); /* should not be reached */
158 } catch (invalid_argument) {
162 // mpz_class(const char *, int) with invalid
165 const char *a = "GHI";
167 mpz_class b(a, base);
168 ASSERT_ALWAYS (0); /* should not be reached */
169 } catch (invalid_argument) {
173 // mpz_class(const std::string &) with invalid
178 ASSERT_ALWAYS (0); /* should not be reached */
179 } catch (invalid_argument) {
183 // mpz_class(const std::string &, int) with invalid
188 mpz_class b(a, base);
189 ASSERT_ALWAYS (0); /* should not be reached */
190 } catch (invalid_argument) {
194 // mpz_class(mpz_srcptr)
197 mpz_init_set_ui(a, 100);
198 mpz_class b(a); ASSERT_ALWAYS(b == 100);
202 // mpz_class(const mpz_class &)
204 mpz_class a(12345); // tested above, assume it works
205 mpz_class b(a); ASSERT_ALWAYS(b == 12345);
208 // no constructor for bool, but it gets casted to int
211 mpz_class b(a); ASSERT_ALWAYS(b == 1);
215 mpz_class b(a); ASSERT_ALWAYS(b == 0);
224 mpq_class a; ASSERT_ALWAYS(a == 0);
227 // mpq_class(const mpq_class &)
230 // template <class T, class U> mpq_class(const __gmp_expr<T, U> &)
231 // not tested here, see t-unary.cc, t-binary.cc
233 // mpq_class(signed char)
235 signed char a = -127;
236 mpq_class b(a); ASSERT_ALWAYS(b == -127);
239 // mpq_class(unsigned char)
241 unsigned char a = 255;
242 mpq_class b(a); ASSERT_ALWAYS(b == 255);
245 // either signed or unsigned char, machine dependent
247 mpq_class a('A'); ASSERT_ALWAYS(a == 65);
250 mpq_class a('z'); ASSERT_ALWAYS(a == 122);
253 // mpq_class(signed int)
256 mpq_class b(a); ASSERT_ALWAYS(b == 0);
260 mpq_class b(a); ASSERT_ALWAYS(b == -123);
264 mpq_class b(a); ASSERT_ALWAYS(b == 4567);
267 // mpq_class(unsigned int)
269 unsigned int a = 890;
270 mpq_class b(a); ASSERT_ALWAYS(b == 890);
273 // mpq_class(signed short int)
275 signed short int a = -12345;
276 mpq_class b(a); ASSERT_ALWAYS(b == -12345);
279 // mpq_class(unsigned short int)
281 unsigned short int a = 54321u;
282 mpq_class b(a); ASSERT_ALWAYS(b == 54321u);
285 // mpq_class(signed long int)
287 signed long int a = -1234567890L;
288 mpq_class b(a); ASSERT_ALWAYS(b == -1234567890L);
291 // mpq_class(unsigned long int)
293 unsigned long int a = 1UL << 30;
294 mpq_class b(a); ASSERT_ALWAYS(b == 1073741824L);
300 mpq_class b(a); ASSERT_ALWAYS(b == 0.625);
306 mpq_class b(a); ASSERT_ALWAYS(b == 1.25);
309 // mpq_class(long double)
310 // currently not implemented
312 // mpq_class(const char *)
314 const char *a = "1234567890";
315 mpq_class b(a); ASSERT_ALWAYS(b == 1234567890L);
318 // mpq_class(const char *, int)
320 const char *a = "FFFF";
322 mpq_class b(a, base); ASSERT_ALWAYS(b == 65535u);
325 // mpq_class(const std::string &)
327 string a("1234567890");
328 mpq_class b(a); ASSERT_ALWAYS(b == 1234567890L);
331 // mpq_class(const std::string &, int)
335 mpq_class b(a, base); ASSERT_ALWAYS(b == 4095);
338 // mpq_class(const char *) with invalid
341 const char *a = "abc";
343 ASSERT_ALWAYS (0); /* should not be reached */
344 } catch (invalid_argument) {
348 // mpq_class(const char *, int) with invalid
351 const char *a = "ZZZ";
353 mpq_class b (a, base);
354 ASSERT_ALWAYS (0); /* should not be reached */
355 } catch (invalid_argument) {
359 // mpq_class(const std::string &) with invalid
364 ASSERT_ALWAYS (0); /* should not be reached */
365 } catch (invalid_argument) {
369 // mpq_class(const std::string &, int) with invalid
374 mpq_class b (a, base);
375 ASSERT_ALWAYS (0); /* should not be reached */
376 } catch (invalid_argument) {
380 // mpq_class(mpq_srcptr)
384 mpq_set_ui(a, 100, 1);
385 mpq_class b(a); ASSERT_ALWAYS(b == 100);
389 // mpq_class(const mpz_class &, const mpz_class &)
391 mpz_class a(123), b(4); // tested above, assume it works
392 mpq_class c(a, b); ASSERT_ALWAYS(c == 30.75);
395 mpz_class a(-1), b(2); // tested above, assume it works
396 mpq_class c(a, b); ASSERT_ALWAYS(c == -0.5);
399 mpz_class a(5), b(4); // tested above, assume it works
400 mpq_class c(a, b); ASSERT_ALWAYS(c == 1.25);
403 // mpq_class(const mpz_class &)
405 mpq_class a(12345); // tested above, assume it works
406 mpq_class b(a); ASSERT_ALWAYS(b == 12345);
409 // no constructor for bool, but it gets casted to int
412 mpq_class b(a); ASSERT_ALWAYS(b == 1);
416 mpq_class b(a); ASSERT_ALWAYS(b == 0);
425 mpf_class a; ASSERT_ALWAYS(a == 0);
428 // mpf_class(const mpf_class &)
429 // mpf_class(const mpf_class &, unsigned long int)
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
437 // mpf_class(signed char)
439 signed char a = -127;
440 mpf_class b(a); ASSERT_ALWAYS(b == -127);
443 // mpf_class(signed char, unsigned long int)
447 mpf_class b(a, prec); ASSERT_ALWAYS(b == -1);
450 // mpf_class(unsigned char)
452 unsigned char a = 255;
453 mpf_class b(a); ASSERT_ALWAYS(b == 255);
456 // mpf_class(unsigned char, unsigned long int)
458 unsigned char a = 128;
460 mpf_class b(a, prec); ASSERT_ALWAYS(b == 128);
463 // either signed or unsigned char, machine dependent
465 mpf_class a('A'); ASSERT_ALWAYS(a == 65);
469 mpf_class a('z', prec); ASSERT_ALWAYS(a == 122);
472 // mpf_class(signed int)
475 mpf_class b(a); ASSERT_ALWAYS(b == 0);
479 mpf_class b(a); ASSERT_ALWAYS(b == -123);
483 mpf_class b(a); ASSERT_ALWAYS(b == 4567);
486 // mpf_class(signed int, unsigned long int)
490 mpf_class b(a, prec); ASSERT_ALWAYS(b == -123);
493 // mpf_class(unsigned int)
495 unsigned int a = 890;
496 mpf_class b(a); ASSERT_ALWAYS(b == 890);
499 // mpf_class(unsigned int, unsigned long int)
501 unsigned int a = 890;
503 mpf_class b(a, prec); ASSERT_ALWAYS(b == 890);
506 // mpf_class(signed short int)
508 signed short int a = -12345;
509 mpf_class b(a); ASSERT_ALWAYS(b == -12345);
512 // mpf_class(signed short int, unsigned long int)
514 signed short int a = 6789;
516 mpf_class b(a, prec); ASSERT_ALWAYS(b == 6789);
519 // mpf_class(unsigned short int)
521 unsigned short int a = 54321u;
522 mpf_class b(a); ASSERT_ALWAYS(b == 54321u);
525 // mpf_class(unsigned short int, unsigned long int)
527 unsigned short int a = 54321u;
529 mpf_class b(a, prec); ASSERT_ALWAYS(b == 54321u);
532 // mpf_class(signed long int)
534 signed long int a = -1234567890L;
535 mpf_class b(a); ASSERT_ALWAYS(b == -1234567890L);
538 // mpf_class(signed long int, unsigned long int)
540 signed long int a = -1234567890L;
542 mpf_class b(a, prec); ASSERT_ALWAYS(b == -1234567890L);
545 // mpf_class(unsigned long int)
547 unsigned long int a = 3456789012UL;
548 mpf_class b(a); ASSERT_ALWAYS(b == 3456789012UL);
551 // mpf_class(unsigned long int, unsigned long int)
553 unsigned long int a = 3456789012UL;
555 mpf_class b(a, prec); ASSERT_ALWAYS(b == 3456789012UL);
561 mpf_class b(a); ASSERT_ALWAYS(b == 1234.5);
564 // mpf_class(float, unsigned long int)
568 mpf_class b(a, prec); ASSERT_ALWAYS(b == 1234.5);
574 mpf_class b(a); ASSERT_ALWAYS(b == 12345);
577 double a = 1.2345e+4;
578 mpf_class b(a); ASSERT_ALWAYS(b == 12345);
582 mpf_class b(a); ASSERT_ALWAYS(b == 3.125);
585 // mpf_class(double, unsigned long int)
587 double a = 5.4321e+4;
589 mpf_class b(a, prec); ASSERT_ALWAYS(b == 54321L);
592 // mpf_class(long double)
593 // mpf_class(long double, unsigned long int)
594 // currently not implemented
596 // mpf_class(const char *)
598 const char *a = "1234567890";
599 mpf_class b(a); ASSERT_ALWAYS(b == 1234567890L);
602 // mpf_class(const char *, unsigned long int, int = 0)
604 const char *a = "1234567890";
606 mpf_class b(a, prec); ASSERT_ALWAYS(b == 1234567890L);
609 const char *a = "777777";
610 int prec = 64, base = 8;
611 mpf_class b(a, prec, base); ASSERT_ALWAYS(b == 262143L);
614 // mpf_class(const std::string &)
616 string a("1234567890");
617 mpf_class b(a); ASSERT_ALWAYS(b == 1234567890L);
620 // mpf_class(const std::string &, unsigned long int, int = 0)
622 string a("1234567890");
624 mpf_class b(a, prec); ASSERT_ALWAYS(b == 1234567890L);
628 int prec = 256, base = 16;
629 mpf_class b(a, prec, base); ASSERT_ALWAYS(b == 65535u);
632 // mpf_class(const char *) with invalid
635 const char *a = "abc";
637 ASSERT_ALWAYS (0); /* should not be reached */
638 } catch (invalid_argument) {
642 // mpf_class(const char *, unsigned long int, int = 0) with invalid
645 const char *a = "def";
647 mpf_class b(a, prec); ASSERT_ALWAYS(b == 1234567890L);
648 ASSERT_ALWAYS (0); /* should not be reached */
649 } catch (invalid_argument) {
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) {
662 // mpf_class(const std::string &) with invalid
666 mpf_class b(a); ASSERT_ALWAYS(b == 1234567890L);
667 ASSERT_ALWAYS (0); /* should not be reached */
668 } catch (invalid_argument) {
672 // mpf_class(const std::string &, unsigned long int, int = 0) with invalid
677 mpf_class b(a, prec); ASSERT_ALWAYS(b == 1234567890L);
678 ASSERT_ALWAYS (0); /* should not be reached */
679 } catch (invalid_argument) {
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) {
692 // mpf_class(mpf_srcptr)
695 mpf_init_set_ui(a, 100);
696 mpf_class b(a); ASSERT_ALWAYS(b == 100);
700 // mpf_class(mpf_srcptr, unsigned long int)
704 mpf_init_set_ui(a, 100);
705 mpf_class b(a, prec); ASSERT_ALWAYS(b == 100);
709 // mpf_class(const mpf_class &)
711 mpf_class a(12345); // tested above, assume it works
712 mpf_class b(a); ASSERT_ALWAYS(b == 12345);
715 // mpf_class(const mpf_class &, unsigned long int)
717 mpf_class a(12345); // tested above, assume it works
719 mpf_class b(a, prec); ASSERT_ALWAYS(b == 12345);
722 // no constructors for bool, but it gets casted to int
725 mpf_class b(a); ASSERT_ALWAYS(b == 1);
729 mpf_class b(a); ASSERT_ALWAYS(b == 0);
734 mpf_class b(a, prec); ASSERT_ALWAYS(b == 1);
739 mpf_class b(a, prec); ASSERT_ALWAYS(b == 0);