github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/tests/cxx/t-constr.cc (about)

     1  /* Test mp*_class constructors.
     2  
     3  Copyright 2001-2003 Free Software Foundation, Inc.
     4  
     5  This file is part of the GNU MP Library test suite.
     6  
     7  The GNU MP Library test suite is free software; you can redistribute it
     8  and/or modify it under the terms of the GNU General Public License as
     9  published by the Free Software Foundation; either version 3 of the License,
    10  or (at your option) any later version.
    11  
    12  The GNU MP Library test suite is distributed in the hope that it will be
    13  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
    14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
    15  Public License for more details.
    16  
    17  You should have received a copy of the GNU General Public License along with
    18  the 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 "gmp.h"
    26  #include "gmpxx.h"
    27  #include "gmp-impl.h"
    28  #include "tests.h"
    29  
    30  using namespace std;
    31  
    32  
    33  void
    34  check_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  
   219  void
   220  check_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      mpq_class c(0, 1); ASSERT_ALWAYS(c == 0);
   324    }
   325  
   326    // mpq_class(const std::string &)
   327    {
   328      string a("1234567890");
   329      mpq_class b(a); ASSERT_ALWAYS(b == 1234567890L);
   330    }
   331  
   332    // mpq_class(const std::string &, int)
   333    {
   334      string a("7777");
   335      int base = 8;
   336      mpq_class b(a, base); ASSERT_ALWAYS(b == 4095);
   337    }
   338  
   339    // mpq_class(const char *) with invalid
   340    {
   341      try {
   342        const char *a = "abc";
   343        mpq_class b(a);
   344        ASSERT_ALWAYS (0);  /* should not be reached */
   345      } catch (invalid_argument) {
   346      }
   347    }
   348  
   349    // mpq_class(const char *, int) with invalid
   350    {
   351      try {
   352        const char *a = "ZZZ";
   353        int base = 16;
   354        mpq_class b (a, base);
   355        ASSERT_ALWAYS (0);  /* should not be reached */
   356      } catch (invalid_argument) {
   357      }
   358    }
   359  
   360    // mpq_class(const std::string &) with invalid
   361    {
   362      try {
   363        string a("abc");
   364        mpq_class b(a);
   365        ASSERT_ALWAYS (0);  /* should not be reached */
   366      } catch (invalid_argument) {
   367      }
   368    }
   369  
   370    // mpq_class(const std::string &, int) with invalid
   371    {
   372      try {
   373        string a("ZZZ");
   374        int base = 8;
   375        mpq_class b (a, base);
   376        ASSERT_ALWAYS (0);  /* should not be reached */
   377      } catch (invalid_argument) {
   378      }
   379    }
   380  
   381    // mpq_class(mpq_srcptr)
   382    {
   383      mpq_t a;
   384      mpq_init(a);
   385      mpq_set_ui(a, 100, 1);
   386      mpq_class b(a); ASSERT_ALWAYS(b == 100);
   387      mpq_clear(a);
   388    }
   389  
   390    // mpq_class(const mpz_class &, const mpz_class &)
   391    {
   392      mpz_class a(123), b(4); // tested above, assume it works
   393      mpq_class c(a, b); ASSERT_ALWAYS(c == 30.75);
   394    }
   395    {
   396      mpz_class a(-1), b(2);  // tested above, assume it works
   397      mpq_class c(a, b); ASSERT_ALWAYS(c == -0.5);
   398    }
   399    {
   400      mpz_class a(5), b(4); // tested above, assume it works
   401      mpq_class c(a, b); ASSERT_ALWAYS(c == 1.25);
   402    }
   403  
   404    // mpq_class(const mpz_class &)
   405    {
   406      mpq_class a(12345); // tested above, assume it works
   407      mpq_class b(a); ASSERT_ALWAYS(b == 12345);
   408    }
   409  
   410    // no constructor for bool, but it gets casted to int
   411    {
   412      bool a = true;
   413      mpq_class b(a); ASSERT_ALWAYS(b == 1);
   414    }
   415    {
   416      bool a = false;
   417      mpq_class b(a); ASSERT_ALWAYS(b == 0);
   418    }
   419  }
   420  
   421  void
   422  check_mpf (void)
   423  {
   424    // mpf_class()
   425    {
   426      mpf_class a; ASSERT_ALWAYS(a == 0);
   427    }
   428  
   429    // mpf_class(const mpf_class &)
   430    // mpf_class(const mpf_class &, unsigned long int)
   431    // see below
   432  
   433    // template <class T, class U> mpf_class(const __gmp_expr<T, U> &)
   434    // template <class T, class U> mpf_class(const __gmp_expr<T, U> &,
   435    //                                       unsigned long int)
   436    // not tested here, see t-unary.cc, t-binary.cc
   437  
   438    // mpf_class(signed char)
   439    {
   440      signed char a = -127;
   441      mpf_class b(a); ASSERT_ALWAYS(b == -127);
   442    }
   443  
   444    // mpf_class(signed char, unsigned long int)
   445    {
   446      signed char a = -1;
   447      int prec = 64;
   448      mpf_class b(a, prec); ASSERT_ALWAYS(b == -1);
   449    }
   450  
   451    // mpf_class(unsigned char)
   452    {
   453      unsigned char a = 255;
   454      mpf_class b(a); ASSERT_ALWAYS(b == 255);
   455    }
   456  
   457    // mpf_class(unsigned char, unsigned long int)
   458    {
   459      unsigned char a = 128;
   460      int prec = 128;
   461      mpf_class b(a, prec); ASSERT_ALWAYS(b == 128);
   462    }
   463  
   464    // either signed or unsigned char, machine dependent
   465    {
   466      mpf_class a('A'); ASSERT_ALWAYS(a == 65);
   467    }
   468    {
   469      int prec = 256;
   470      mpf_class a('z', prec); ASSERT_ALWAYS(a == 122);
   471    }
   472  
   473    // mpf_class(signed int)
   474    {
   475      signed int a = 0;
   476      mpf_class b(a); ASSERT_ALWAYS(b == 0);
   477    }
   478    {
   479      signed int a = -123;
   480      mpf_class b(a); ASSERT_ALWAYS(b == -123);
   481    }
   482    {
   483      signed int a = 4567;
   484      mpf_class b(a); ASSERT_ALWAYS(b == 4567);
   485    }
   486  
   487    // mpf_class(signed int, unsigned long int)
   488    {
   489      signed int a = -123;
   490      int prec = 64;
   491      mpf_class b(a, prec); ASSERT_ALWAYS(b == -123);
   492    }
   493  
   494    // mpf_class(unsigned int)
   495    {
   496      unsigned int a = 890;
   497      mpf_class b(a); ASSERT_ALWAYS(b == 890);
   498    }
   499  
   500    // mpf_class(unsigned int, unsigned long int)
   501    {
   502      unsigned int a = 890;
   503      int prec = 128;
   504      mpf_class b(a, prec); ASSERT_ALWAYS(b == 890);
   505    }
   506  
   507    // mpf_class(signed short int)
   508    {
   509      signed short int a = -12345;
   510      mpf_class b(a); ASSERT_ALWAYS(b == -12345);
   511    }
   512  
   513    // mpf_class(signed short int, unsigned long int)
   514    {
   515      signed short int a = 6789;
   516      int prec = 256;
   517      mpf_class b(a, prec); ASSERT_ALWAYS(b == 6789);
   518    }
   519  
   520    // mpf_class(unsigned short int)
   521    {
   522      unsigned short int a = 54321u;
   523      mpf_class b(a); ASSERT_ALWAYS(b == 54321u);
   524    }
   525  
   526    // mpf_class(unsigned short int, unsigned long int)
   527    {
   528      unsigned short int a = 54321u;
   529      int prec = 64;
   530      mpf_class b(a, prec); ASSERT_ALWAYS(b == 54321u);
   531    }
   532  
   533    // mpf_class(signed long int)
   534    {
   535      signed long int a = -1234567890L;
   536      mpf_class b(a); ASSERT_ALWAYS(b == -1234567890L);
   537    }
   538  
   539    // mpf_class(signed long int, unsigned long int)
   540    {
   541      signed long int a = -1234567890L;
   542      int prec = 128;
   543      mpf_class b(a, prec); ASSERT_ALWAYS(b == -1234567890L);
   544    }
   545  
   546    // mpf_class(unsigned long int)
   547    {
   548      unsigned long int a = 3456789012UL;
   549      mpf_class b(a); ASSERT_ALWAYS(b == 3456789012UL);
   550    }
   551  
   552    // mpf_class(unsigned long int, unsigned long int)
   553    {
   554      unsigned long int a = 3456789012UL;
   555      int prec = 256;
   556      mpf_class b(a, prec); ASSERT_ALWAYS(b == 3456789012UL);
   557    }
   558  
   559    // mpf_class(float)
   560    {
   561      float a = 1234.5;
   562      mpf_class b(a); ASSERT_ALWAYS(b == 1234.5);
   563    }
   564  
   565    // mpf_class(float, unsigned long int)
   566    {
   567      float a = 1234.5;
   568      int prec = 64;
   569      mpf_class b(a, prec); ASSERT_ALWAYS(b == 1234.5);
   570    }
   571  
   572    // mpf_class(double)
   573    {
   574      double a = 12345.0;
   575      mpf_class b(a); ASSERT_ALWAYS(b == 12345);
   576    }
   577    {
   578      double a = 1.2345e+4;
   579      mpf_class b(a); ASSERT_ALWAYS(b == 12345);
   580    }
   581    {
   582      double a = 312.5e-2;
   583      mpf_class b(a); ASSERT_ALWAYS(b == 3.125);
   584    }
   585  
   586    // mpf_class(double, unsigned long int)
   587    {
   588      double a = 5.4321e+4;
   589      int prec = 128;
   590      mpf_class b(a, prec); ASSERT_ALWAYS(b == 54321L);
   591    }
   592  
   593    // mpf_class(long double)
   594    // mpf_class(long double, unsigned long int)
   595    // currently not implemented
   596  
   597    // mpf_class(const char *)
   598    {
   599      const char *a = "1234567890";
   600      mpf_class b(a); ASSERT_ALWAYS(b == 1234567890L);
   601    }
   602  
   603    // mpf_class(const char *, unsigned long int, int = 0)
   604    {
   605      const char *a = "1234567890";
   606      int prec = 256;
   607      mpf_class b(a, prec); ASSERT_ALWAYS(b == 1234567890L);
   608    }
   609    {
   610      const char *a = "777777";
   611      int prec = 64, base = 8;
   612      mpf_class b(a, prec, base); ASSERT_ALWAYS(b == 262143L);
   613    }
   614  
   615    // mpf_class(const std::string &)
   616    {
   617      string a("1234567890");
   618      mpf_class b(a); ASSERT_ALWAYS(b == 1234567890L);
   619    }
   620  
   621    // mpf_class(const std::string &, unsigned long int, int = 0)
   622    {
   623      string a("1234567890");
   624      int prec = 128;
   625      mpf_class b(a, prec); ASSERT_ALWAYS(b == 1234567890L);
   626    }
   627    {
   628      string a("FFFF");
   629      int prec = 256, base = 16;
   630      mpf_class b(a, prec, base); ASSERT_ALWAYS(b == 65535u);
   631    }
   632  
   633    // mpf_class(const char *) with invalid
   634    {
   635      try {
   636        const char *a = "abc";
   637        mpf_class b(a);
   638        ASSERT_ALWAYS (0);  /* should not be reached */
   639      } catch (invalid_argument) {
   640      }
   641    }
   642  
   643    // mpf_class(const char *, unsigned long int, int = 0) with invalid
   644    {
   645      try {
   646        const char *a = "def";
   647        int prec = 256;
   648        mpf_class b(a, prec); ASSERT_ALWAYS(b == 1234567890L);
   649        ASSERT_ALWAYS (0);  /* should not be reached */
   650      } catch (invalid_argument) {
   651      }
   652    }
   653    {
   654      try {
   655        const char *a = "ghi";
   656        int prec = 64, base = 8;
   657        mpf_class b(a, prec, base); ASSERT_ALWAYS(b == 262143L);
   658        ASSERT_ALWAYS (0);  /* should not be reached */
   659      } catch (invalid_argument) {
   660      }
   661    }
   662  
   663    // mpf_class(const std::string &) with invalid
   664    {
   665      try {
   666        string a("abc");
   667        mpf_class b(a); ASSERT_ALWAYS(b == 1234567890L);
   668        ASSERT_ALWAYS (0);  /* should not be reached */
   669      } catch (invalid_argument) {
   670      }
   671    }
   672  
   673    // mpf_class(const std::string &, unsigned long int, int = 0) with invalid
   674    {
   675      try {
   676        string a("def");
   677        int prec = 128;
   678        mpf_class b(a, prec); ASSERT_ALWAYS(b == 1234567890L);
   679        ASSERT_ALWAYS (0);  /* should not be reached */
   680      } catch (invalid_argument) {
   681      }
   682    }
   683    {
   684      try {
   685        string a("ghi");
   686        int prec = 256, base = 16;
   687        mpf_class b(a, prec, base); ASSERT_ALWAYS(b == 65535u);
   688        ASSERT_ALWAYS (0);  /* should not be reached */
   689      } catch (invalid_argument) {
   690      }
   691    }
   692  
   693    // mpf_class(mpf_srcptr)
   694    {
   695      mpf_t a;
   696      mpf_init_set_ui(a, 100);
   697      mpf_class b(a); ASSERT_ALWAYS(b == 100);
   698      mpf_clear(a);
   699    }
   700  
   701    // mpf_class(mpf_srcptr, unsigned long int)
   702    {
   703      mpf_t a;
   704      int prec = 64;
   705      mpf_init_set_ui(a, 100);
   706      mpf_class b(a, prec); ASSERT_ALWAYS(b == 100);
   707      mpf_clear(a);
   708    }
   709  
   710    // mpf_class(const mpf_class &)
   711    {
   712      mpf_class a(12345); // tested above, assume it works
   713      mpf_class b(a); ASSERT_ALWAYS(b == 12345);
   714    }
   715  
   716    // mpf_class(const mpf_class &, unsigned long int)
   717    {
   718      mpf_class a(12345); // tested above, assume it works
   719      int prec = 64;
   720      mpf_class b(a, prec); ASSERT_ALWAYS(b == 12345);
   721    }
   722  
   723    // no constructors for bool, but it gets casted to int
   724    {
   725      bool a = true;
   726      mpf_class b(a); ASSERT_ALWAYS(b == 1);
   727    }
   728    {
   729      bool a = false;
   730      mpf_class b(a); ASSERT_ALWAYS(b == 0);
   731    }
   732    {
   733      bool a = true;
   734      int prec = 128;
   735      mpf_class b(a, prec); ASSERT_ALWAYS(b == 1);
   736    }
   737    {
   738      bool a = false;
   739      int prec = 256;
   740      mpf_class b(a, prec); ASSERT_ALWAYS(b == 0);
   741    }
   742  }
   743  
   744  
   745  int
   746  main (void)
   747  {
   748    tests_start();
   749  
   750    check_mpz();
   751    check_mpq();
   752    check_mpf();
   753  
   754    tests_end();
   755    return 0;
   756  }