[Gmp-commit] /var/hg/gmp: Test stream input/output on mp*_class and not just ...

mercurial at gmplib.org mercurial at gmplib.org
Tue May 10 22:12:38 CEST 2011


details:   /var/hg/gmp/rev/5d20b365f7bc
changeset: 14182:5d20b365f7bc
user:      Marc Glisse <marc.glisse at inria.fr>
date:      Tue May 10 22:12:34 2011 +0200
description:
Test stream input/output on mp*_class and not just mp*_t.
Simple optimization: c=-(a+b) becomes c=a+b;c=-c.

diffstat:

 ChangeLog               |   7 +++
 gmpxx.h                 |   9 +++-
 tests/cxx/Makefile.am   |   7 ++-
 tests/cxx/t-iostream.cc |  89 +++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 106 insertions(+), 6 deletions(-)

diffs (164 lines):

diff -r 26acba148727 -r 5d20b365f7bc ChangeLog
--- a/ChangeLog	Tue May 10 14:31:06 2011 +0200
+++ b/ChangeLog	Tue May 10 22:12:34 2011 +0200
@@ -1,3 +1,10 @@
+2011-05-10  Marc Glisse  <marc.glisse at inria.fr>
+
+	* gmpxx.h (__gmp_unary_expr): Let things happen in place.
+	(operator>>): Clean the commenting out.
+	* tests/cxx/t-iostream.cc: New file.
+	* tests/cxx/Makefile.am: Added t-iostream.
+
 2011-05-10  Niels Möller  <nisse at lysator.liu.se>
 
 	* doc/gmp.texi (mpz_gcd): Document that gcd(0,0) = 0.
diff -r 26acba148727 -r 5d20b365f7bc gmpxx.h
--- a/gmpxx.h	Tue May 10 14:31:06 2011 +0200
+++ b/gmpxx.h	Tue May 10 22:12:34 2011 +0200
@@ -1814,12 +1814,15 @@
   return i >> expr.__get_mp();
 }
 
+/*
+// you might want to uncomment this
 inline std::istream & operator>>(std::istream &i, mpq_class &q)
 {
   i >> q.get_mpq_t();
-  // q.canonicalize(); // you might want to uncomment this
+  q.canonicalize();
   return i;
 }
+*/
 
 
 /**************** Functions for type conversion ****************/
@@ -1948,10 +1951,10 @@
 public:
   __gmp_expr(const val_type &val) : expr(val) { }
   void eval(typename __gmp_resolve_expr<T>::ptr_type p) const
-  { __gmp_expr<T, T> temp(expr.val); Op::eval(p, temp.__get_mp()); }
+  { expr.val.eval(p); Op::eval(p, p); }
   void eval(typename __gmp_resolve_expr<T>::ptr_type p,
 	    mp_bitcnt_t prec) const
-  { __gmp_expr<T, T> temp(expr.val, prec); Op::eval(p, temp.__get_mp()); }
+  { expr.val.eval(p, prec); Op::eval(p, p); }
   const val_type & get_val() const { return expr.val; }
   mp_bitcnt_t get_prec() const { return expr.val.get_prec(); }
 };
diff -r 26acba148727 -r 5d20b365f7bc tests/cxx/Makefile.am
--- a/tests/cxx/Makefile.am	Tue May 10 14:31:06 2011 +0200
+++ b/tests/cxx/Makefile.am	Tue May 10 22:12:34 2011 +0200
@@ -30,9 +30,9 @@
   $(top_builddir)/libgmp.la
 
 if WANT_CXX
-check_PROGRAMS = t-assign t-binary t-cast t-constr t-headers t-istream \
-  t-locale t-misc t-mix t-ops t-ops2 t-ops3 t-ostream t-prec t-rand \
-  t-ternary t-unary
+check_PROGRAMS = t-assign t-binary t-cast t-constr t-headers t-iostream \
+  t-istream t-locale t-misc t-mix t-ops t-ops2 t-ops3 t-ostream t-prec \
+  t-rand t-ternary t-unary
 TESTS = $(check_PROGRAMS)
 endif
 
@@ -41,6 +41,7 @@
 t_cast_SOURCES    = t-cast.cc
 t_constr_SOURCES  = t-constr.cc
 t_headers_SOURCES = t-headers.cc
+t_iostream_SOURCES= t-iostream.cc
 t_istream_SOURCES = t-istream.cc
 t_locale_SOURCES  = t-locale.cc clocale.c
 t_misc_SOURCES    = t-misc.cc
diff -r 26acba148727 -r 5d20b365f7bc tests/cxx/t-iostream.cc
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/cxx/t-iostream.cc	Tue May 10 22:12:34 2011 +0200
@@ -0,0 +1,89 @@
+/* Test stream formatted input and output on mp*_class
+
+Copyright 2011 Free Software Foundation, Inc.
+
+This file is part of the GNU MP Library.
+
+The GNU MP Library is free software; you can redistribute it and/or modify
+it under the terms of the GNU Lesser 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 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 Lesser General Public
+License for more details.
+
+You should have received a copy of the GNU Lesser General Public License
+along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
+
+#include <sstream>
+
+#include "gmp.h"
+#include "gmpxx.h"
+#include "gmp-impl.h"
+#include "tests.h"
+
+using namespace std;
+
+// The tests are extremely basic. These functions just forward to the
+// ones tested in t-istream.cc and t-ostream.cc; we rely on those for
+// advanced tests and only check the syntax here.
+
+void
+checki ()
+{
+  {
+    istringstream i("123");
+    mpz_class x;
+    i >> x;
+    ASSERT_ALWAYS (x == 123);
+  }
+  {
+    istringstream i("3/4");
+    mpq_class x;
+    i >> x;
+    ASSERT_ALWAYS (x == .75);
+  }
+  {
+    istringstream i("1.5");
+    mpf_class x;
+    i >> x;
+    ASSERT_ALWAYS (x == 1.5);
+  }
+}
+
+void
+checko ()
+{
+  {
+    ostringstream o;
+    mpz_class x=123;
+    o << x;
+    ASSERT_ALWAYS (o.str() == "123");
+  }
+  {
+    ostringstream o;
+    mpq_class x(3,4);
+    o << x;
+    ASSERT_ALWAYS (o.str() == "3/4");
+  }
+  {
+    ostringstream o;
+    mpf_class x=1.5;
+    o << x;
+    ASSERT_ALWAYS (o.str() == "1.5");
+  }
+}
+
+int
+main (int argc, char *argv[])
+{
+  tests_start ();
+
+  checki ();
+  checko ();
+
+  tests_end ();
+  return 0;
+}


More information about the gmp-commit mailing list