diff options
author | Marc Glisse <marc.glisse@inria.fr> | 2011-03-13 21:11:29 +0100 |
---|---|---|
committer | Marc Glisse <marc.glisse@inria.fr> | 2011-03-13 21:11:29 +0100 |
commit | 130f7333450f7f905aa4bf61f684a7c5348ca487 (patch) | |
tree | b3cd2860dcf5a7e1c38fa3cc248aad971c92b006 /tests/cxx | |
parent | 8bb086393f171a8c2f9ffda5968551ed924bb4b7 (diff) | |
download | gmp-130f7333450f7f905aa4bf61f684a7c5348ca487.tar.gz |
Increase C++ test coverage.
Diffstat (limited to 'tests/cxx')
-rw-r--r-- | tests/cxx/Makefile.am | 5 | ||||
-rw-r--r-- | tests/cxx/t-ops2.cc | 235 |
2 files changed, 238 insertions, 2 deletions
diff --git a/tests/cxx/Makefile.am b/tests/cxx/Makefile.am index 41764ee6c..db6a4d141 100644 --- a/tests/cxx/Makefile.am +++ b/tests/cxx/Makefile.am @@ -30,8 +30,8 @@ LDADD = -L$(top_builddir)/.libs \ $(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-ops t-ostream t-prec t-rand t-ternary t-unary +check_PROGRAMS = t-assign t-binary t-cast t-constr t-headers t-istream \ + t-locale t-misc t-ops t-ops2 t-ostream t-prec t-rand t-ternary t-unary TESTS = $(check_PROGRAMS) endif @@ -44,6 +44,7 @@ t_istream_SOURCES = t-istream.cc t_locale_SOURCES = t-locale.cc clocale.c t_misc_SOURCES = t-misc.cc t_ops_SOURCES = t-ops.cc +t_ops2_SOURCES = t-ops2.cc t_ostream_SOURCES = t-ostream.cc t_prec_SOURCES = t-prec.cc t_rand_SOURCES = t-rand.cc diff --git a/tests/cxx/t-ops2.cc b/tests/cxx/t-ops2.cc new file mode 100644 index 000000000..54efb5fbc --- /dev/null +++ b/tests/cxx/t-ops2.cc @@ -0,0 +1,235 @@ +/* Test mp*_class operators and functions. + +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 "config.h" + +#include <iostream> +#include <cmath> + +#include "gmp.h" +#include "gmpxx.h" +#include "gmp-impl.h" +#include "tests.h" + +using namespace std; + + +#define CHECK1(Type,a,fun) \ + ASSERT_ALWAYS(fun((Type)(a))==fun(a)) +#define CHECK(Type1,Type2,a,b,op) \ + ASSERT_ALWAYS(((Type1)(a) op (Type2)(b))==((a) op (b))) +#define CHECK_G(Type,a,b,op) \ + CHECK(Type,Type,a,b,op) +#define CHECK_UI(Type,a,b,op) \ + CHECK(Type,unsigned long,a,b,op); \ + CHECK(unsigned long,Type,a,b,op) +#define CHECK_SI(Type,a,b,op) \ + CHECK(Type,long,a,b,op); \ + CHECK(long,Type,a,b,op) +#define CHECK_D(Type,a,b,op) \ + CHECK(Type,double,a,b,op); \ + CHECK(double,Type,a,b,op) +#define CHECK_MPZ(Type,a,b,op) \ + CHECK(Type,mpz_class,a,b,op); \ + CHECK(mpz_class,Type,a,b,op) +#define CHECK_MPQ(Type,a,b,op) \ + CHECK(Type,mpq_class,a,b,op); \ + CHECK(mpq_class,Type,a,b,op) +#define CHECK_ALL_SIGNED(Type,a,b,op) \ + CHECK_G(Type,a,b,op); \ + CHECK_SI(Type,a,b,op); \ + CHECK_D(Type,a,b,op) +#define CHECK_ALL_SIGNS(Type,a,b,op) \ + CHECK_ALL_SIGNED(Type,a,b,op); \ + CHECK_ALL_SIGNED(Type,-(a),b,op); \ + CHECK_ALL_SIGNED(Type,a,-(b),op); \ + CHECK_ALL_SIGNED(Type,-(a),-(b),op) +#define CHECK_ALL(Type,a,b,op) \ + CHECK_ALL_SIGNED(Type,a,b,op); \ + CHECK_UI(Type,a,b,op) +#define CHECK_ALL_SIGNED_COMPARISONS(Type,a,b) \ + CHECK_ALL_SIGNED(Type,a,b,<); \ + CHECK_ALL_SIGNED(Type,a,b,>); \ + CHECK_ALL_SIGNED(Type,a,b,<=); \ + CHECK_ALL_SIGNED(Type,a,b,>=); \ + CHECK_ALL_SIGNED(Type,a,b,==); \ + CHECK_ALL_SIGNED(Type,a,b,!=) +#define CHECK_ALL_SIGNS_COMPARISONS(Type,a,b) \ + CHECK_ALL_SIGNS(Type,a,b,<); \ + CHECK_ALL_SIGNS(Type,a,b,>); \ + CHECK_ALL_SIGNS(Type,a,b,<=); \ + CHECK_ALL_SIGNS(Type,a,b,>=); \ + CHECK_ALL_SIGNS(Type,a,b,==); \ + CHECK_ALL_SIGNS(Type,a,b,!=) +#define CHECK_ALL_COMPARISONS(Type,a,b) \ + CHECK_ALL(Type,a,b,<); \ + CHECK_ALL(Type,a,b,>); \ + CHECK_ALL(Type,a,b,<=); \ + CHECK_ALL(Type,a,b,>=); \ + CHECK_ALL(Type,a,b,==); \ + CHECK_ALL(Type,a,b,!=) + + +void checkz (){ + CHECK_ALL(mpz_class,5,2,+); + CHECK_ALL(mpz_class,5,2,-); + CHECK_ALL(mpz_class,5,2,*); + CHECK_ALL(mpz_class,5,2,/); + CHECK_ALL(mpz_class,5,2,%); + CHECK_ALL_COMPARISONS(mpz_class,5,2); + CHECK_ALL_SIGNS(mpz_class,11,3,+); + CHECK_ALL_SIGNS(mpz_class,11,3,-); + CHECK_ALL_SIGNS(mpz_class,11,3,*); + CHECK_ALL_SIGNS(mpz_class,11,3,/); + CHECK_ALL_SIGNS(mpz_class,11,3,%); + CHECK_ALL_SIGNS_COMPARISONS(mpz_class,11,3); + CHECK_ALL(mpz_class,6,3,&); + CHECK_ALL(mpz_class,6,3,|); + CHECK_ALL(mpz_class,6,3,^); + CHECK(mpz_class,unsigned long,6,2,<<); + CHECK(mpz_class,unsigned long,6,2,>>); + CHECK(mpz_class,unsigned long,-13,2,<<); + CHECK(mpz_class,unsigned long,-13,2,>>); + ASSERT_ALWAYS(++mpz_class(7)==8); + ASSERT_ALWAYS(++mpz_class(-8)==-7); + ASSERT_ALWAYS(--mpz_class(8)==7); + ASSERT_ALWAYS(--mpz_class(-7)==-8); + ASSERT_ALWAYS(~mpz_class(7)==-8); + ASSERT_ALWAYS(~mpz_class(-8)==7); + ASSERT_ALWAYS(+mpz_class(7)==7); + ASSERT_ALWAYS(+mpz_class(-8)==-8); + ASSERT_ALWAYS(-mpz_class(7)==-7); + ASSERT_ALWAYS(-mpz_class(-8)==8); + ASSERT_ALWAYS(abs(mpz_class(7))==7); + ASSERT_ALWAYS(abs(mpz_class(-8))==8); + ASSERT_ALWAYS(sqrt(mpz_class(7))==2); + ASSERT_ALWAYS(sqrt(mpz_class(0))==0); + ASSERT_ALWAYS(sgn(mpz_class(0))==0); + ASSERT_ALWAYS(sgn(mpz_class(9))==1); + ASSERT_ALWAYS(sgn(mpz_class(-17))==-1); +} + +template<class T> +void checkqf (){ + CHECK_ALL(T,5.,2,+); CHECK_MPZ(T,5.,2,+); + CHECK_ALL(T,5.,2,-); CHECK_MPZ(T,5.,2,-); + CHECK_ALL(T,5.,2,*); CHECK_MPZ(T,5.,2,*); + CHECK_ALL(T,5.,2,/); CHECK_MPZ(T,5.,2,/); + CHECK_ALL_SIGNS(T,11.,3,+); + CHECK_ALL_SIGNS(T,11.,3,-); + CHECK_ALL_SIGNS(T,11.,3,*); + CHECK_ALL_SIGNS(T,11.,4,/); + CHECK_ALL_COMPARISONS(T,5.,2); + CHECK_ALL_SIGNS_COMPARISONS(T,11.,3); + CHECK_MPZ(T,5,-2,<); + CHECK_MPZ(T,5,-2,>); + CHECK_MPZ(T,5,-2,<=); + CHECK_MPZ(T,5,-2,<=); + CHECK_MPZ(T,5,-2,==); + CHECK_MPZ(T,5,-2,!=); + CHECK_MPZ(T,0,0,<); + CHECK_MPZ(T,0,0,>); + CHECK_MPZ(T,0,0,<=); + CHECK_MPZ(T,0,0,<=); + CHECK_MPZ(T,0,0,==); + CHECK_MPZ(T,0,0,!=); + ASSERT_ALWAYS(T(6)<<2==6.*4); + ASSERT_ALWAYS(T(6)>>2==6./4); + ASSERT_ALWAYS(T(-13)<<2==-13.*4); + ASSERT_ALWAYS(T(-13)>>2==-13./4); + ASSERT_ALWAYS(++T(7)==8); + ASSERT_ALWAYS(++T(-8)==-7); + ASSERT_ALWAYS(--T(8)==7); + ASSERT_ALWAYS(--T(-7)==-8); + ASSERT_ALWAYS(+T(7)==7); + ASSERT_ALWAYS(+T(-8)==-8); + ASSERT_ALWAYS(-T(7)==-7); + ASSERT_ALWAYS(-T(-8)==8); + ASSERT_ALWAYS(abs(T(7))==7); + ASSERT_ALWAYS(abs(T(-8))==8); + ASSERT_ALWAYS(sgn(T(0))==0); + ASSERT_ALWAYS(sgn(T(9))==1); + ASSERT_ALWAYS(sgn(T(-17))==-1); +} + +void checkf (){ + ASSERT_ALWAYS(sqrt(mpf_class(7))>2.64); + ASSERT_ALWAYS(sqrt(mpf_class(7))<2.65); + ASSERT_ALWAYS(sqrt(mpf_class(0))==0); + CHECK1(mpf_class,1.9,trunc); + CHECK1(mpf_class,1.9,floor); + CHECK1(mpf_class,1.9,ceil); + CHECK1(mpf_class,4.3,trunc); + CHECK1(mpf_class,4.3,floor); + CHECK1(mpf_class,4.3,ceil); + CHECK1(mpf_class,-7.1,trunc); + CHECK1(mpf_class,-7.1,floor); + CHECK1(mpf_class,-7.1,ceil); + CHECK1(mpf_class,-2.8,trunc); + CHECK1(mpf_class,-2.8,floor); + CHECK1(mpf_class,-2.8,ceil); + CHECK1(mpf_class,-1.5,trunc); + CHECK1(mpf_class,-1.5,floor); + CHECK1(mpf_class,-1.5,ceil); + CHECK1(mpf_class,2.5,trunc); + CHECK1(mpf_class,2.5,floor); + CHECK1(mpf_class,2.5,ceil); + ASSERT_ALWAYS(hypot(mpf_class(-3),mpf_class(4))>4.9); + ASSERT_ALWAYS(hypot(mpf_class(-3),mpf_class(4))<5.1); + ASSERT_ALWAYS(hypot(mpf_class(-3),4.)>4.9); + ASSERT_ALWAYS(hypot(-3.,mpf_class(4))<5.1); + ASSERT_ALWAYS(hypot(mpf_class(-3),4l)>4.9); + ASSERT_ALWAYS(hypot(-3l,mpf_class(4))<5.1); + ASSERT_ALWAYS(hypot(mpf_class(-3),4ul)>4.9); + ASSERT_ALWAYS(hypot(3ul,mpf_class(4))<5.1); + CHECK(mpf_class,mpq_class,1.5,2.25,+); + CHECK(mpf_class,mpq_class,1.5,2.25,-); + CHECK(mpf_class,mpq_class,1.5,-2.25,*); + CHECK(mpf_class,mpq_class,1.5,-2,/); + CHECK_MPQ(mpf_class,-5.5,-2.25,+); + CHECK_MPQ(mpf_class,-5.5,-2.25,-); + CHECK_MPQ(mpf_class,-5.5,-2.25,*); + CHECK_MPQ(mpf_class,-5.25,-0.5,/); + CHECK_MPQ(mpf_class,5,-2,<); + CHECK_MPQ(mpf_class,5,-2,>); + CHECK_MPQ(mpf_class,5,-2,<=); + CHECK_MPQ(mpf_class,5,-2,<=); + CHECK_MPQ(mpf_class,5,-2,==); + CHECK_MPQ(mpf_class,5,-2,!=); + CHECK_MPQ(mpf_class,0,0,<); + CHECK_MPQ(mpf_class,0,0,>); + CHECK_MPQ(mpf_class,0,0,<=); + CHECK_MPQ(mpf_class,0,0,<=); + CHECK_MPQ(mpf_class,0,0,==); + CHECK_MPQ(mpf_class,0,0,!=); +} + +int +main (void) +{ + tests_start(); + + checkz(); + checkqf<mpq_class>(); + checkqf<mpf_class>(); + checkf(); + + tests_end(); + return 0; +} |