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

Copyright 2001-2003 Free Software Foundation, Inc.

This file is part of the GNU MP Library test suite.

The GNU MP Library test suite is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 3 of the License,
or (at your option) any later version.

The GNU MP Library test suite is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
Public License for more details.

You should have received a copy of the GNU General Public License along with
the GNU MP Library test suite.  If not, see https://www.gnu.org/licenses/.  */

#include "config.h"

#include <iostream>
#include <string>

#include "gmpxx.h"
#include "gmp-impl.h"
#include "tests.h"

using namespace std;


void
check_mpz (void)
{
  // mpz_class()
  {
    mpz_class a; ASSERT_ALWAYS(a == 0);
  }

  // mpz_class(const mpz_class &)
  // see below

  // template <class T, class U> mpz_class(const __gmp_expr<T, U> &)
  // not tested here, see t-unary.cc, t-binary.cc

  // mpz_class(signed char)
  {
    signed char a = -127;
    mpz_class b(a); ASSERT_ALWAYS(b == -127);
  }

  // mpz_class(unsigned char)
  {
    unsigned char a = 255;
    mpz_class b(a); ASSERT_ALWAYS(b == 255);
  }

  // either signed or unsigned char, machine dependent
  {
    mpz_class a('A'); ASSERT_ALWAYS(a == 65);
  }
  {
    mpz_class a('z'); ASSERT_ALWAYS(a == 122);
  }

  // mpz_class(signed int)
  {
    signed int a = 0;
    mpz_class b(a); ASSERT_ALWAYS(b == 0);
  }
  {
    signed int a = -123;
    mpz_class b(a); ASSERT_ALWAYS(b == -123);
  }
  {
    signed int a = 4567;
    mpz_class b(a); ASSERT_ALWAYS(b == 4567);
  }

  // mpz_class(unsigned int)
  {
    unsigned int a = 890;
    mpz_class b(a); ASSERT_ALWAYS(b == 890);
  }

  // mpz_class(signed short int)
  {
    signed short int a = -12345;
    mpz_class b(a); ASSERT_ALWAYS(b == -12345);
  }

  // mpz_class(unsigned short int)
  {
    unsigned short int a = 54321u;
    mpz_class b(a); ASSERT_ALWAYS(b == 54321u);
  }

  // mpz_class(signed long int)
  {
    signed long int a = -1234567890L;
    mpz_class b(a); ASSERT_ALWAYS(b == -1234567890L);
  }

  // mpz_class(unsigned long int)
  {
    unsigned long int a = 1UL << 30;
    mpz_class b(a); ASSERT_ALWAYS(b == 1073741824L);
  }

  // mpz_class(float)
  {
    float a = 123.45;
    mpz_class b(a); ASSERT_ALWAYS(b == 123);
  }

  // mpz_class(double)
  {
    double a = 3.141592653589793238;
    mpz_class b(a); ASSERT_ALWAYS(b == 3);
  }

  // mpz_class(long double)
  // currently not implemented

  // mpz_class(const char *)
  {
    const char *a = "1234567890";
    mpz_class b(a); ASSERT_ALWAYS(b == 1234567890L);
  }

  // mpz_class(const char *, int)
  {
    const char *a = "FFFF";
    int base = 16;
    mpz_class b(a, base); ASSERT_ALWAYS(b == 65535u);
  }

  // mpz_class(const std::string &)
  {
    string a("1234567890");
    mpz_class b(a); ASSERT_ALWAYS(b == 1234567890L);
  }

  // mpz_class(const std::string &, int)
  {
    string a("7777");
    int base = 8;
    mpz_class b(a, base); ASSERT_ALWAYS(b == 4095);
  }

  // mpz_class(const char *) with invalid
  {
    try {
      const char *a = "ABC";
      mpz_class b(a);
      ASSERT_ALWAYS (0);  /* should not be reached */
    } catch (invalid_argument&) {
    }
  }

  // mpz_class(const char *, int) with invalid
  {
    try {
      const char *a = "GHI";
      int base = 16;
      mpz_class b(a, base);
      ASSERT_ALWAYS (0);  /* should not be reached */
    } catch (invalid_argument&) {
    }
  }

  // mpz_class(const std::string &) with invalid
  {
    try {
      string a("abc");
      mpz_class b(a);
      ASSERT_ALWAYS (0);  /* should not be reached */
    } catch (invalid_argument&) {
    }
  }

  // mpz_class(const std::string &, int) with invalid
  {
    try {
      string a("ZZZ");
      int base = 8;
      mpz_class b(a, base);
      ASSERT_ALWAYS (0);  /* should not be reached */
    } catch (invalid_argument&) {
    }
  }

  // mpz_class(mpz_srcptr)
  {
    mpz_t a;
    mpz_init_set_ui(a, 100);
    mpz_class b(a); ASSERT_ALWAYS(b == 100);
    mpz_clear(a);
  }

  // mpz_class(const mpz_class &)
  {
    mpz_class a(12345); // tested above, assume it works
    mpz_class b(a); ASSERT_ALWAYS(b == 12345);
  }

  // no constructor for bool, but it gets casted to int
  {
    bool a = true;
    mpz_class b(a); ASSERT_ALWAYS(b == 1);
  }
  {
    bool a = false;
    mpz_class b(a); ASSERT_ALWAYS(b == 0);
  }
}

void
check_mpq (void)
{
  // mpq_class()
  {
    mpq_class a; ASSERT_ALWAYS(a == 0);
  }

  // mpq_class(const mpq_class &)
  // see below

  // template <class T, class U> mpq_class(const __gmp_expr<T, U> &)
  // not tested here, see t-unary.cc, t-binary.cc

  // mpq_class(signed char)
  {
    signed char a = -127;
    mpq_class b(a); ASSERT_ALWAYS(b == -127);
  }

  // mpq_class(unsigned char)
  {
    unsigned char a = 255;
    mpq_class b(a); ASSERT_ALWAYS(b == 255);
  }

  // either signed or unsigned char, machine dependent
  {
    mpq_class a('A'); ASSERT_ALWAYS(a == 65);
  }
  {
    mpq_class a('z'); ASSERT_ALWAYS(a == 122);
  }

  // mpq_class(signed int)
  {
    signed int a = 0;
    mpq_class b(a); ASSERT_ALWAYS(b == 0);
  }
  {
    signed int a = -123;
    mpq_class b(a); ASSERT_ALWAYS(b == -123);
  }
  {
    signed int a = 4567;
    mpq_class b(a); ASSERT_ALWAYS(b == 4567);
  }

  // mpq_class(unsigned int)
  {
    unsigned int a = 890;
    mpq_class b(a); ASSERT_ALWAYS(b == 890);
  }

  // mpq_class(signed short int)
  {
    signed short int a = -12345;
    mpq_class b(a); ASSERT_ALWAYS(b == -12345);
  }

  // mpq_class(unsigned short int)
  {
    unsigned short int a = 54321u;
    mpq_class b(a); ASSERT_ALWAYS(b == 54321u);
  }

  // mpq_class(signed long int)
  {
    signed long int a = -1234567890L;
    mpq_class b(a); ASSERT_ALWAYS(b == -1234567890L);
  }

  // mpq_class(unsigned long int)
  {
    unsigned long int a = 1UL << 30;
    mpq_class b(a); ASSERT_ALWAYS(b == 1073741824L);
  }

  // mpq_class(float)
  {
    float a = 0.625;
    mpq_class b(a); ASSERT_ALWAYS(b == 0.625);
  }

  // mpq_class(double)
  {
    double a = 1.25;
    mpq_class b(a); ASSERT_ALWAYS(b == 1.25);
  }

  // mpq_class(long double)
  // currently not implemented

  // mpq_class(const char *)
  {
    const char *a = "1234567890";
    mpq_class b(a); ASSERT_ALWAYS(b == 1234567890L);
  }

  // mpq_class(const char *, int)
  {
    const char *a = "FFFF";
    int base = 16;
    mpq_class b(a, base); ASSERT_ALWAYS(b == 65535u);
    mpq_class c(0, 1); ASSERT_ALWAYS(c == 0);
  }

  // mpq_class(const std::string &)
  {
    string a("1234567890");
    mpq_class b(a); ASSERT_ALWAYS(b == 1234567890L);
  }

  // mpq_class(const std::string &, int)
  {
    string a("7777");
    int base = 8;
    mpq_class b(a, base); ASSERT_ALWAYS(b == 4095);
  }

  // mpq_class(const char *) with invalid
  {
    try {
      const char *a = "abc";
      mpq_class b(a);
      ASSERT_ALWAYS (0);  /* should not be reached */
    } catch (invalid_argument&) {
    }
  }

  // mpq_class(const char *, int) with invalid
  {
    try {
      const char *a = "ZZZ";
      int base = 16;
      mpq_class b (a, base);
      ASSERT_ALWAYS (0);  /* should not be reached */
    } catch (invalid_argument&) {
    }
  }

  // mpq_class(const std::string &) with invalid
  {
    try {
      string a("abc");
      mpq_class b(a);
      ASSERT_ALWAYS (0);  /* should not be reached */
    } catch (invalid_argument&) {
    }
  }

  // mpq_class(const std::string &, int) with invalid
  {
    try {
      string a("ZZZ");
      int base = 8;
      mpq_class b (a, base);
      ASSERT_ALWAYS (0);  /* should not be reached */
    } catch (invalid_argument&) {
    }
  }

  // mpq_class(mpq_srcptr)
  {
    mpq_t a;
    mpq_init(a);
    mpq_set_ui(a, 100, 1);
    mpq_class b(a); ASSERT_ALWAYS(b == 100);
    mpq_clear(a);
  }

  // mpq_class(const mpz_class &, const mpz_class &)
  {
    mpz_class a(123), b(4); // tested above, assume it works
    mpq_class c(a, b); ASSERT_ALWAYS(c == 30.75);
  }
  {
    mpz_class a(-1), b(2);  // tested above, assume it works
    mpq_class c(a, b); ASSERT_ALWAYS(c == -0.5);
  }
  {
    mpz_class a(5), b(4); // tested above, assume it works
    mpq_class c(a, b); ASSERT_ALWAYS(c == 1.25);
  }

  // mpq_class(const mpz_class &)
  {
    mpq_class a(12345); // tested above, assume it works
    mpq_class b(a); ASSERT_ALWAYS(b == 12345);
  }

  // no constructor for bool, but it gets casted to int
  {
    bool a = true;
    mpq_class b(a); ASSERT_ALWAYS(b == 1);
  }
  {
    bool a = false;
    mpq_class b(a); ASSERT_ALWAYS(b == 0);
  }
}

void
check_mpf (void)
{
  // mpf_class()
  {
    mpf_class a; ASSERT_ALWAYS(a == 0);
  }

  // mpf_class(const mpf_class &)
  // mpf_class(const mpf_class &, unsigned long int)
  // see below

  // template <class T, class U> mpf_class(const __gmp_expr<T, U> &)
  // template <class T, class U> mpf_class(const __gmp_expr<T, U> &,
  //                                       unsigned long int)
  // not tested here, see t-unary.cc, t-binary.cc

  // mpf_class(signed char)
  {
    signed char a = -127;
    mpf_class b(a); ASSERT_ALWAYS(b == -127);
  }

  // mpf_class(signed char, unsigned long int)
  {
    signed char a = -1;
    int prec = 64;
    mpf_class b(a, prec); ASSERT_ALWAYS(b == -1);
  }

  // mpf_class(unsigned char)
  {
    unsigned char a = 255;
    mpf_class b(a); ASSERT_ALWAYS(b == 255);
  }

  // mpf_class(unsigned char, unsigned long int)
  {
    unsigned char a = 128;
    int prec = 128;
    mpf_class b(a, prec); ASSERT_ALWAYS(b == 128);
  }

  // either signed or unsigned char, machine dependent
  {
    mpf_class a('A'); ASSERT_ALWAYS(a == 65);
  }
  {
    int prec = 256;
    mpf_class a('z', prec); ASSERT_ALWAYS(a == 122);
  }

  // mpf_class(signed int)
  {
    signed int a = 0;
    mpf_class b(a); ASSERT_ALWAYS(b == 0);
  }
  {
    signed int a = -123;
    mpf_class b(a); ASSERT_ALWAYS(b == -123);
  }
  {
    signed int a = 4567;
    mpf_class b(a); ASSERT_ALWAYS(b == 4567);
  }

  // mpf_class(signed int, unsigned long int)
  {
    signed int a = -123;
    int prec = 64;
    mpf_class b(a, prec); ASSERT_ALWAYS(b == -123);
  }

  // mpf_class(unsigned int)
  {
    unsigned int a = 890;
    mpf_class b(a); ASSERT_ALWAYS(b == 890);
  }

  // mpf_class(unsigned int, unsigned long int)
  {
    unsigned int a = 890;
    int prec = 128;
    mpf_class b(a, prec); ASSERT_ALWAYS(b == 890);
  }

  // mpf_class(signed short int)
  {
    signed short int a = -12345;
    mpf_class b(a); ASSERT_ALWAYS(b == -12345);
  }

  // mpf_class(signed short int, unsigned long int)
  {
    signed short int a = 6789;
    int prec = 256;
    mpf_class b(a, prec); ASSERT_ALWAYS(b == 6789);
  }

  // mpf_class(unsigned short int)
  {
    unsigned short int a = 54321u;
    mpf_class b(a); ASSERT_ALWAYS(b == 54321u);
  }

  // mpf_class(unsigned short int, unsigned long int)
  {
    unsigned short int a = 54321u;
    int prec = 64;
    mpf_class b(a, prec); ASSERT_ALWAYS(b == 54321u);
  }

  // mpf_class(signed long int)
  {
    signed long int a = -1234567890L;
    mpf_class b(a); ASSERT_ALWAYS(b == -1234567890L);
  }

  // mpf_class(signed long int, unsigned long int)
  {
    signed long int a = -1234567890L;
    int prec = 128;
    mpf_class b(a, prec); ASSERT_ALWAYS(b == -1234567890L);
  }

  // mpf_class(unsigned long int)
  {
    unsigned long int a = 3456789012UL;
    mpf_class b(a); ASSERT_ALWAYS(b == 3456789012UL);
  }

  // mpf_class(unsigned long int, unsigned long int)
  {
    unsigned long int a = 3456789012UL;
    int prec = 256;
    mpf_class b(a, prec); ASSERT_ALWAYS(b == 3456789012UL);
  }

  // mpf_class(float)
  {
    float a = 1234.5;
    mpf_class b(a); ASSERT_ALWAYS(b == 1234.5);
  }

  // mpf_class(float, unsigned long int)
  {
    float a = 1234.5;
    int prec = 64;
    mpf_class b(a, prec); ASSERT_ALWAYS(b == 1234.5);
  }

  // mpf_class(double)
  {
    double a = 12345.0;
    mpf_class b(a); ASSERT_ALWAYS(b == 12345);
  }
  {
    double a = 1.2345e+4;
    mpf_class b(a); ASSERT_ALWAYS(b == 12345);
  }
  {
    double a = 312.5e-2;
    mpf_class b(a); ASSERT_ALWAYS(b == 3.125);
  }

  // mpf_class(double, unsigned long int)
  {
    double a = 5.4321e+4;
    int prec = 128;
    mpf_class b(a, prec); ASSERT_ALWAYS(b == 54321L);
  }

  // mpf_class(long double)
  // mpf_class(long double, unsigned long int)
  // currently not implemented

  // mpf_class(const char *)
  {
    const char *a = "1234567890";
    mpf_class b(a); ASSERT_ALWAYS(b == 1234567890L);
  }

  // mpf_class(const char *, unsigned long int, int = 0)
  {
    const char *a = "1234567890";
    int prec = 256;
    mpf_class b(a, prec); ASSERT_ALWAYS(b == 1234567890L);
  }
  {
    const char *a = "777777";
    int prec = 64, base = 8;
    mpf_class b(a, prec, base); ASSERT_ALWAYS(b == 262143L);
  }

  // mpf_class(const std::string &)
  {
    string a("1234567890");
    mpf_class b(a); ASSERT_ALWAYS(b == 1234567890L);
  }

  // mpf_class(const std::string &, unsigned long int, int = 0)
  {
    string a("1234567890");
    int prec = 128;
    mpf_class b(a, prec); ASSERT_ALWAYS(b == 1234567890L);
  }
  {
    string a("FFFF");
    int prec = 256, base = 16;
    mpf_class b(a, prec, base); ASSERT_ALWAYS(b == 65535u);
  }

  // mpf_class(const char *) with invalid
  {
    try {
      const char *a = "abc";
      mpf_class b(a);
      ASSERT_ALWAYS (0);  /* should not be reached */
    } catch (invalid_argument&) {
    }
  }

  // mpf_class(const char *, unsigned long int, int = 0) with invalid
  {
    try {
      const char *a = "def";
      int prec = 256;
      mpf_class b(a, prec); ASSERT_ALWAYS(b == 1234567890L);
      ASSERT_ALWAYS (0);  /* should not be reached */
    } catch (invalid_argument&) {
    }
  }
  {
    try {
      const char *a = "ghi";
      int prec = 64, base = 8;
      mpf_class b(a, prec, base); ASSERT_ALWAYS(b == 262143L);
      ASSERT_ALWAYS (0);  /* should not be reached */
    } catch (invalid_argument&) {
    }
  }

  // mpf_class(const std::string &) with invalid
  {
    try {
      string a("abc");
      mpf_class b(a); ASSERT_ALWAYS(b == 1234567890L);
      ASSERT_ALWAYS (0);  /* should not be reached */
    } catch (invalid_argument&) {
    }
  }

  // mpf_class(const std::string &, unsigned long int, int = 0) with invalid
  {
    try {
      string a("def");
      int prec = 128;
      mpf_class b(a, prec); ASSERT_ALWAYS(b == 1234567890L);
      ASSERT_ALWAYS (0);  /* should not be reached */
    } catch (invalid_argument&) {
    }
  }
  {
    try {
      string a("ghi");
      int prec = 256, base = 16;
      mpf_class b(a, prec, base); ASSERT_ALWAYS(b == 65535u);
      ASSERT_ALWAYS (0);  /* should not be reached */
    } catch (invalid_argument&) {
    }
  }

  // mpf_class(mpf_srcptr)
  {
    mpf_t a;
    mpf_init_set_ui(a, 100);
    mpf_class b(a); ASSERT_ALWAYS(b == 100);
    mpf_clear(a);
  }

  // mpf_class(mpf_srcptr, unsigned long int)
  {
    mpf_t a;
    int prec = 64;
    mpf_init_set_ui(a, 100);
    mpf_class b(a, prec); ASSERT_ALWAYS(b == 100);
    mpf_clear(a);
  }

  // mpf_class(const mpf_class &)
  {
    mpf_class a(12345); // tested above, assume it works
    mpf_class b(a); ASSERT_ALWAYS(b == 12345);
  }

  // mpf_class(const mpf_class &, unsigned long int)
  {
    mpf_class a(12345); // tested above, assume it works
    int prec = 64;
    mpf_class b(a, prec); ASSERT_ALWAYS(b == 12345);
  }

  // no constructors for bool, but it gets casted to int
  {
    bool a = true;
    mpf_class b(a); ASSERT_ALWAYS(b == 1);
  }
  {
    bool a = false;
    mpf_class b(a); ASSERT_ALWAYS(b == 0);
  }
  {
    bool a = true;
    int prec = 128;
    mpf_class b(a, prec); ASSERT_ALWAYS(b == 1);
  }
  {
    bool a = false;
    int prec = 256;
    mpf_class b(a, prec); ASSERT_ALWAYS(b == 0);
  }
}


int
main (void)
{
  tests_start();

  check_mpz();
  check_mpq();
  check_mpf();

  tests_end();
  return 0;
}