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