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

     1  /* Test stream formatted input and output on mp*_class
     2  
     3  Copyright 2011 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 <sstream>
    21  
    22  #include "gmp.h"
    23  #include "gmpxx.h"
    24  #include "gmp-impl.h"
    25  #include "tests.h"
    26  
    27  using namespace std;
    28  
    29  // The tests are extremely basic. These functions just forward to the
    30  // ones tested in t-istream.cc and t-ostream.cc; we rely on those for
    31  // advanced tests and only check the syntax here.
    32  
    33  void
    34  checki ()
    35  {
    36    {
    37      istringstream i("123");
    38      mpz_class x;
    39      i >> x;
    40      ASSERT_ALWAYS (x == 123);
    41    }
    42    {
    43      istringstream i("3/4");
    44      mpq_class x;
    45      i >> x;
    46      ASSERT_ALWAYS (x == .75);
    47    }
    48    {
    49      istringstream i("1.5");
    50      mpf_class x;
    51      i >> x;
    52      ASSERT_ALWAYS (x == 1.5);
    53    }
    54  }
    55  
    56  void
    57  checko ()
    58  {
    59    {
    60      ostringstream o;
    61      mpz_class x=123;
    62      o << x;
    63      ASSERT_ALWAYS (o.str() == "123");
    64    }
    65    {
    66      ostringstream o;
    67      mpz_class x=123;
    68      o << (x+1);
    69      ASSERT_ALWAYS (o.str() == "124");
    70    }
    71    {
    72      ostringstream o;
    73      mpq_class x(3,4);
    74      o << x;
    75      ASSERT_ALWAYS (o.str() == "3/4");
    76    }
    77    {
    78      ostringstream o;
    79      mpq_class x(3,4);
    80      o << (x+1);
    81      ASSERT_ALWAYS (o.str() == "7/4");
    82    }
    83    {
    84      ostringstream o;
    85      mpf_class x=1.5;
    86      o << x;
    87      ASSERT_ALWAYS (o.str() == "1.5");
    88    }
    89    {
    90      ostringstream o;
    91      mpf_class x=1.5;
    92      o << (x+1);
    93      ASSERT_ALWAYS (o.str() == "2.5");
    94    }
    95  }
    96  
    97  int
    98  main (int argc, char *argv[])
    99  {
   100    tests_start ();
   101  
   102    checki ();
   103    checko ();
   104  
   105    tests_end ();
   106    return 0;
   107  }