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