xref: /netbsd-src/external/lgpl3/gmp/dist/tests/cxx/t-ops3.cc (revision 72c7faa4dbb41dbb0238d6b4a109da0d4b236dd4)
1 /* Test mp*_class assignment operators (+=, -=, etc)
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 "config.h"
21 
22 #include "gmpxx.h"
23 #include "gmp-impl.h"
24 #include "tests.h"
25 
26 using namespace std;
27 
28 #define FOR_ALL_SIGNED_BUILTIN(F) \
29 	F(signed char) \
30 	F(signed short) \
31 	F(signed int) \
32 	F(signed long) \
33 	F(float) \
34 	F(double)
35 
36 #define FOR_ALL_BUILTIN(F) \
37 	FOR_ALL_SIGNED_BUILTIN(F) \
38 	F(char) \
39 	F(unsigned char) \
40 	F(unsigned short) \
41 	F(unsigned int) \
42 	F(unsigned long)
43 
44 #define FOR_ALL_GMPXX(F) \
45 	F(mpz_class) \
46 	F(mpq_class) \
47 	F(mpf_class)
48 
f(T t,U u)49 template<class T,class U> void f(T t, U u){
50   T a=t;
51   ASSERT_ALWAYS((a+=u)==(t+u)); ASSERT_ALWAYS(a==(t+u));
52   ASSERT_ALWAYS((a-=u)==t); ASSERT_ALWAYS(a==t);
53   ASSERT_ALWAYS((a*=u)==(t*u)); ASSERT_ALWAYS(a==(t*u));
54   ASSERT_ALWAYS((a/=u)==t); ASSERT_ALWAYS(a==t);
55   ASSERT_ALWAYS((a<<=5)==(t<<5)); ASSERT_ALWAYS(a==(t<<5));
56   ASSERT_ALWAYS((a>>=5)==t); ASSERT_ALWAYS(a==t);
57 }
58 
g(T t,U u)59 template<class T,class U> void g(T t, U u){
60   T a=t;
61   ASSERT_ALWAYS((a%=u)==(t%u)); ASSERT_ALWAYS(a==(t%u));
62   a=t;
63   ASSERT_ALWAYS((a&=u)==(t&u)); ASSERT_ALWAYS(a==(t&u));
64   a=t;
65   ASSERT_ALWAYS((a|=u)==(t|u)); ASSERT_ALWAYS(a==(t|u));
66   a=t;
67   ASSERT_ALWAYS((a^=u)==(t^u)); ASSERT_ALWAYS(a==(t^u));
68 }
69 
h(T t)70 template<class T> void h(T t){
71   T a=t;
72   ASSERT_ALWAYS((a<<=5)==(t<<5)); ASSERT_ALWAYS(a==(t<<5));
73   ASSERT_ALWAYS((a>>=5)==t); ASSERT_ALWAYS(a==t);
74 }
75 
ffs(T t,U u)76 template<class T, class U> void ffs(T t, U u){
77 #define F(V) f(t,(V)u);
78 	FOR_ALL_SIGNED_BUILTIN(F)
79 	FOR_ALL_GMPXX(F)
80 #undef F
81 #define F(V) f(t,-(V)u);
82 	FOR_ALL_GMPXX(F)
83 #undef F
84 }
85 
ff(T t,U u)86 template<class T, class U> void ff(T t, U u){
87 #define F(V) f(t,(V)u);
88 	FOR_ALL_BUILTIN(F)
89 	FOR_ALL_GMPXX(F)
90 #undef F
91 #define F(V) f(t,-(V)u);
92 	FOR_ALL_GMPXX(F)
93 #undef F
94 }
95 
ggs(mpz_class t,U u)96 template<class U> void ggs(mpz_class t, U u){
97 #define F(V) g(t,(V)u);
98 	FOR_ALL_SIGNED_BUILTIN(F)
99 #undef F
100 	g(t,(mpz_class)u);
101 	g(t,-(mpz_class)u);
102 }
103 
gg(mpz_class t,U u)104 template<class U> void gg(mpz_class t, U u){
105 #define F(V) g(t,(V)u);
106 	FOR_ALL_BUILTIN(F)
107 #undef F
108 	g(t,(mpz_class)u);
109 	g(t,-(mpz_class)u);
110 }
111 
check()112 void check(){
113 	mpz_class z=18;
114 	mpq_class q(7,2);
115 	mpf_class d=3.375;
116 	h(z); h(q); h(d);
117 	ff(z,13); ff(q,13); ff(d,13);
118 	ffs(z,-42); ffs(q,-42); ffs(d,-42);
119 	gg(z,33); ggs(z,-22);
120 }
121 
122 
123 int
main(void)124 main (void)
125 {
126   tests_start();
127 
128   check();
129 
130   tests_end();
131   return 0;
132 }
133