github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/tests/cxx/t-ops2.cc (about) 1 /* Test mp*_class operators and functions. 2 3 Copyright 2011, 2012 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 <math.h> 23 24 #include "gmp.h" 25 #include "gmpxx.h" 26 #include "gmp-impl.h" 27 #include "tests.h" 28 29 30 #define CHECK1(Type,a,fun) \ 31 ASSERT_ALWAYS(fun((Type)(a))==fun(a)) 32 #define CHECK(Type1,Type2,a,b,op) \ 33 ASSERT_ALWAYS(((Type1)(a) op (Type2)(b))==((a) op (b))) 34 #define CHECK_G(Type,a,b,op) \ 35 CHECK(Type,Type,a,b,op) 36 #define CHECK_UI(Type,a,b,op) \ 37 CHECK(Type,unsigned long,a,b,op); \ 38 CHECK(unsigned long,Type,a,b,op) 39 #define CHECK_SI(Type,a,b,op) \ 40 CHECK(Type,long,a,b,op); \ 41 CHECK(long,Type,a,b,op) 42 #define CHECK_D(Type,a,b,op) \ 43 CHECK(Type,double,a,b,op); \ 44 CHECK(double,Type,a,b,op) 45 #define CHECK_MPZ(Type,a,b,op) \ 46 CHECK(Type,mpz_class,a,b,op); \ 47 CHECK(mpz_class,Type,a,b,op) 48 #define CHECK_MPQ(Type,a,b,op) \ 49 CHECK(Type,mpq_class,a,b,op); \ 50 CHECK(mpq_class,Type,a,b,op) 51 #define CHECK_ALL_SIGNED(Type,a,b,op) \ 52 CHECK_G(Type,a,b,op); \ 53 CHECK_SI(Type,a,b,op); \ 54 CHECK_D(Type,a,b,op) 55 #define CHECK_ALL_SIGNS(Type,a,b,op) \ 56 CHECK_ALL_SIGNED(Type,a,b,op); \ 57 CHECK_ALL_SIGNED(Type,-(a),b,op); \ 58 CHECK_ALL_SIGNED(Type,a,-(b),op); \ 59 CHECK_ALL_SIGNED(Type,-(a),-(b),op) 60 #define CHECK_ALL(Type,a,b,op) \ 61 CHECK_ALL_SIGNED(Type,a,b,op); \ 62 CHECK_UI(Type,a,b,op) 63 #define CHECK_ALL_SIGNED_COMPARISONS(Type,a,b) \ 64 CHECK_ALL_SIGNED(Type,a,b,<); \ 65 CHECK_ALL_SIGNED(Type,a,b,>); \ 66 CHECK_ALL_SIGNED(Type,a,b,<=); \ 67 CHECK_ALL_SIGNED(Type,a,b,>=); \ 68 CHECK_ALL_SIGNED(Type,a,b,==); \ 69 CHECK_ALL_SIGNED(Type,a,b,!=) 70 #define CHECK_ALL_SIGNS_COMPARISONS(Type,a,b) \ 71 CHECK_ALL_SIGNS(Type,a,b,<); \ 72 CHECK_ALL_SIGNS(Type,a,b,>); \ 73 CHECK_ALL_SIGNS(Type,a,b,<=); \ 74 CHECK_ALL_SIGNS(Type,a,b,>=); \ 75 CHECK_ALL_SIGNS(Type,a,b,==); \ 76 CHECK_ALL_SIGNS(Type,a,b,!=) 77 #define CHECK_ALL_COMPARISONS(Type,a,b) \ 78 CHECK_ALL(Type,a,b,<); \ 79 CHECK_ALL(Type,a,b,>); \ 80 CHECK_ALL(Type,a,b,<=); \ 81 CHECK_ALL(Type,a,b,>=); \ 82 CHECK_ALL(Type,a,b,==); \ 83 CHECK_ALL(Type,a,b,!=) 84 85 86 void checkz (){ 87 CHECK_ALL(mpz_class,5,2,+); 88 CHECK_ALL(mpz_class,5,2,-); 89 CHECK_ALL(mpz_class,5,2,*); 90 CHECK_ALL(mpz_class,5,2,/); 91 CHECK_ALL(mpz_class,5,2,%); 92 CHECK_ALL_COMPARISONS(mpz_class,5,2); 93 CHECK_ALL_SIGNS(mpz_class,11,3,+); 94 CHECK_ALL_SIGNS(mpz_class,11,3,-); 95 CHECK_ALL_SIGNS(mpz_class,11,3,*); 96 CHECK_ALL_SIGNS(mpz_class,11,3,/); 97 CHECK_ALL_SIGNS(mpz_class,11,3,%); 98 CHECK_ALL_SIGNS(mpz_class,17,2,*); 99 CHECK_ALL_SIGNS(mpz_class,17,2,/); 100 CHECK_ALL_SIGNS(mpz_class,17,2,%); 101 CHECK(unsigned long,mpz_class,5,-2,/); 102 CHECK(unsigned long,mpz_class,5,-2,%); 103 ASSERT_ALWAYS(7ul/mpz_class(1e35)==0); 104 ASSERT_ALWAYS(7ul%mpz_class(1e35)==7); 105 ASSERT_ALWAYS(7ul/mpz_class(-1e35)==0); 106 ASSERT_ALWAYS(7ul%mpz_class(-1e35)==7); 107 CHECK_ALL_SIGNS_COMPARISONS(mpz_class,11,3); 108 CHECK_ALL(mpz_class,6,3,&); 109 CHECK_ALL(mpz_class,6,3,|); 110 CHECK_ALL(mpz_class,6,3,^); 111 CHECK(mpz_class,unsigned long,6,2,<<); 112 CHECK(mpz_class,unsigned long,6,2,>>); 113 ASSERT_ALWAYS(mpz_class(-13)<<(unsigned long)2 == (-13)*4); 114 CHECK(mpz_class,unsigned long,-13,2,>>); 115 ASSERT_ALWAYS(++mpz_class(7)==8); 116 ASSERT_ALWAYS(++mpz_class(-8)==-7); 117 ASSERT_ALWAYS(--mpz_class(8)==7); 118 ASSERT_ALWAYS(--mpz_class(-7)==-8); 119 ASSERT_ALWAYS(~mpz_class(7)==-8); 120 ASSERT_ALWAYS(~mpz_class(-8)==7); 121 ASSERT_ALWAYS(+mpz_class(7)==7); 122 ASSERT_ALWAYS(+mpz_class(-8)==-8); 123 ASSERT_ALWAYS(-mpz_class(7)==-7); 124 ASSERT_ALWAYS(-mpz_class(-8)==8); 125 ASSERT_ALWAYS(abs(mpz_class(7))==7); 126 ASSERT_ALWAYS(abs(mpz_class(-8))==8); 127 ASSERT_ALWAYS(sqrt(mpz_class(7))==2); 128 ASSERT_ALWAYS(sqrt(mpz_class(0))==0); 129 ASSERT_ALWAYS(sgn(mpz_class(0))==0); 130 ASSERT_ALWAYS(sgn(mpz_class(9))==1); 131 ASSERT_ALWAYS(sgn(mpz_class(-17))==-1); 132 ASSERT_ALWAYS(mpz_class(1)+DBL_MAX>2); 133 ASSERT_ALWAYS(mpz_class(1)+DBL_MIN<2); 134 ASSERT_ALWAYS(mpz_class(1)+std::numeric_limits<double>::denorm_min()<2); 135 ASSERT_ALWAYS(gcd(mpz_class(6),mpz_class(8))==2); 136 ASSERT_ALWAYS(gcd(-mpz_class(6),mpz_class(8))==2); 137 ASSERT_ALWAYS(gcd(-mpz_class(6),-mpz_class(8))==2); 138 ASSERT_ALWAYS(gcd(mpz_class(6),8.f)==2); 139 ASSERT_ALWAYS(gcd(-mpz_class(6),static_cast<unsigned char>(8))==2); 140 ASSERT_ALWAYS(gcd(static_cast<long>(-6),mpz_class(5)+3)==2); 141 ASSERT_ALWAYS(lcm(mpz_class(6),mpz_class(8))==24); 142 ASSERT_ALWAYS(lcm(-mpz_class(6),mpz_class(8))==24); 143 ASSERT_ALWAYS(lcm(-mpz_class(6),-mpz_class(8))==24); 144 ASSERT_ALWAYS(lcm(mpz_class(6),static_cast<short>(8))==24); 145 ASSERT_ALWAYS(lcm(-mpz_class(6),static_cast<unsigned char>(8))==24); 146 ASSERT_ALWAYS(lcm(-6.,mpz_class(5)+3)==24); 147 } 148 149 template<class T> 150 void checkqf (){ 151 CHECK_ALL(T,5.,2,+); CHECK_MPZ(T,5.,2,+); 152 CHECK_ALL(T,5.,2,-); CHECK_MPZ(T,5.,2,-); 153 CHECK_ALL(T,5.,2,*); CHECK_MPZ(T,5.,2,*); 154 CHECK_ALL(T,5.,2,/); CHECK_MPZ(T,5.,2,/); 155 CHECK_ALL(T,0.,2,/); 156 CHECK_ALL_SIGNS(T,11.,3,+); 157 CHECK_ALL_SIGNS(T,11.,3,-); 158 CHECK_ALL_SIGNS(T,11.,3,*); 159 CHECK_ALL_SIGNS(T,11.,4,/); 160 CHECK_SI(T,LONG_MIN,1,*); 161 CHECK_SI(T,0,3,*); 162 CHECK_ALL_COMPARISONS(T,5.,2); 163 CHECK_ALL_SIGNS_COMPARISONS(T,11.,3); 164 CHECK_MPZ(T,5,-2,<); 165 CHECK_MPZ(T,5,-2,>); 166 CHECK_MPZ(T,5,-2,<=); 167 CHECK_MPZ(T,5,-2,>=); 168 CHECK_MPZ(T,5,-2,==); 169 CHECK_MPZ(T,5,-2,!=); 170 CHECK_MPZ(T,0,0,<); 171 CHECK_MPZ(T,0,0,>); 172 CHECK_MPZ(T,0,0,<=); 173 CHECK_MPZ(T,0,0,>=); 174 CHECK_MPZ(T,0,0,==); 175 CHECK_MPZ(T,0,0,!=); 176 ASSERT_ALWAYS(T(6)<<2==6.*4); 177 ASSERT_ALWAYS(T(6)>>2==6./4); 178 ASSERT_ALWAYS(T(-13)<<2==-13.*4); 179 ASSERT_ALWAYS(T(-13)>>2==-13./4); 180 ASSERT_ALWAYS(++T(7)==8); 181 ASSERT_ALWAYS(++T(-8)==-7); 182 ASSERT_ALWAYS(--T(8)==7); 183 ASSERT_ALWAYS(--T(-7)==-8); 184 ASSERT_ALWAYS(+T(7)==7); 185 ASSERT_ALWAYS(+T(-8)==-8); 186 ASSERT_ALWAYS(-T(7)==-7); 187 ASSERT_ALWAYS(-T(-8)==8); 188 ASSERT_ALWAYS(abs(T(7))==7); 189 ASSERT_ALWAYS(abs(T(-8))==8); 190 ASSERT_ALWAYS(sgn(T(0))==0); 191 ASSERT_ALWAYS(sgn(T(9))==1); 192 ASSERT_ALWAYS(sgn(T(-17))==-1); 193 ASSERT_ALWAYS(T(1)+DBL_MAX>2); 194 ASSERT_ALWAYS(T(1)+DBL_MIN>1); 195 ASSERT_ALWAYS(T(1)+DBL_MIN<1.001); 196 ASSERT_ALWAYS(T(1)+std::numeric_limits<double>::denorm_min()>1); 197 ASSERT_ALWAYS(T(1)+std::numeric_limits<double>::denorm_min()<1.001); 198 } 199 200 void checkf (){ 201 ASSERT_ALWAYS(sqrt(mpf_class(7))>2.64); 202 ASSERT_ALWAYS(sqrt(mpf_class(7))<2.65); 203 ASSERT_ALWAYS(sqrt(mpf_class(0))==0); 204 // TODO: add some consistency checks, as described in 205 // https://gmplib.org/list-archives/gmp-bugs/2013-February/002940.html 206 CHECK1(mpf_class,1.9,trunc); 207 CHECK1(mpf_class,1.9,floor); 208 CHECK1(mpf_class,1.9,ceil); 209 CHECK1(mpf_class,4.3,trunc); 210 CHECK1(mpf_class,4.3,floor); 211 CHECK1(mpf_class,4.3,ceil); 212 CHECK1(mpf_class,-7.1,trunc); 213 CHECK1(mpf_class,-7.1,floor); 214 CHECK1(mpf_class,-7.1,ceil); 215 CHECK1(mpf_class,-2.8,trunc); 216 CHECK1(mpf_class,-2.8,floor); 217 CHECK1(mpf_class,-2.8,ceil); 218 CHECK1(mpf_class,-1.5,trunc); 219 CHECK1(mpf_class,-1.5,floor); 220 CHECK1(mpf_class,-1.5,ceil); 221 CHECK1(mpf_class,2.5,trunc); 222 CHECK1(mpf_class,2.5,floor); 223 CHECK1(mpf_class,2.5,ceil); 224 ASSERT_ALWAYS(hypot(mpf_class(-3),mpf_class(4))>4.9); 225 ASSERT_ALWAYS(hypot(mpf_class(-3),mpf_class(4))<5.1); 226 ASSERT_ALWAYS(hypot(mpf_class(-3),4.)>4.9); 227 ASSERT_ALWAYS(hypot(-3.,mpf_class(4))<5.1); 228 ASSERT_ALWAYS(hypot(mpf_class(-3),4l)>4.9); 229 ASSERT_ALWAYS(hypot(-3l,mpf_class(4))<5.1); 230 ASSERT_ALWAYS(hypot(mpf_class(-3),4ul)>4.9); 231 ASSERT_ALWAYS(hypot(3ul,mpf_class(4))<5.1); 232 CHECK(mpf_class,mpq_class,1.5,2.25,+); 233 CHECK(mpf_class,mpq_class,1.5,2.25,-); 234 CHECK(mpf_class,mpq_class,1.5,-2.25,*); 235 CHECK(mpf_class,mpq_class,1.5,-2,/); 236 CHECK_MPQ(mpf_class,-5.5,-2.25,+); 237 CHECK_MPQ(mpf_class,-5.5,-2.25,-); 238 CHECK_MPQ(mpf_class,-5.5,-2.25,*); 239 CHECK_MPQ(mpf_class,-5.25,-0.5,/); 240 CHECK_MPQ(mpf_class,5,-2,<); 241 CHECK_MPQ(mpf_class,5,-2,>); 242 CHECK_MPQ(mpf_class,5,-2,<=); 243 CHECK_MPQ(mpf_class,5,-2,>=); 244 CHECK_MPQ(mpf_class,5,-2,==); 245 CHECK_MPQ(mpf_class,5,-2,!=); 246 CHECK_MPQ(mpf_class,0,0,<); 247 CHECK_MPQ(mpf_class,0,0,>); 248 CHECK_MPQ(mpf_class,0,0,<=); 249 CHECK_MPQ(mpf_class,0,0,>=); 250 CHECK_MPQ(mpf_class,0,0,==); 251 CHECK_MPQ(mpf_class,0,0,!=); 252 } 253 254 int 255 main (void) 256 { 257 tests_start(); 258 259 // Enough precision for 1 + denorm_min 260 mpf_set_default_prec(DBL_MANT_DIG-DBL_MIN_EXP+42); 261 checkz(); 262 checkqf<mpq_class>(); 263 checkqf<mpf_class>(); 264 checkf(); 265 266 tests_end(); 267 return 0; 268 }