Update copyright in libstdc++-v3.
[official-gcc.git] / libstdc++-v3 / testsuite / decimal / binary-arith.cc
blob8a742cb8c621ac2582c849dc4523268fd8479a7c
1 // Copyright (C) 2009-2013 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
18 // { dg-require-effective-target dfp }
20 // ISO/IEC TR 24733 3.2.8 Binary arithmetic operators.
22 #include <decimal/decimal>
23 #include <testsuite_hooks.h>
25 using namespace std::decimal;
27 decimal32 d32 (5);
28 decimal64 d64 (-10);
29 decimal128 d128 (25);
30 int si = -20;
31 unsigned int ui = 50;
32 long sl = -10;
33 unsigned long ul = 20;
34 long long sll = -25;
35 unsigned long long ull = 50;
37 void
38 binary_add_32 (void)
40 bool test __attribute__((unused)) = true;
41 decimal32 a (1000), b;
43 b = si + a; VERIFY (b == 980);
44 b = ui + a; VERIFY (b == 1050);
45 b = sl + a; VERIFY (b == 990);
46 b = ul + a; VERIFY (b == 1020);
47 b = sll + a; VERIFY (b == 975);
48 b = ull + a; VERIFY (b == 1050);
49 b = d32 + a; VERIFY (b == 1005);
50 b = (decimal32)(d64 + a); VERIFY (b == 990);
51 b = (decimal32)(d128 + a); VERIFY (b == 1025);
53 b = a + si; VERIFY (b == 980);
54 b = a + ui; VERIFY (b == 1050);
55 b = a + sl; VERIFY (b == 990);
56 b = a + ul; VERIFY (b == 1020);
57 b = a + sll; VERIFY (b == 975);
58 b = a + ull; VERIFY (b == 1050);
59 b = a + d32; VERIFY (b == 1005);
60 b = (decimal32)(a + d64); VERIFY (b == 990);
61 b = (decimal32)(a + d128); VERIFY (b == 1025);
64 void
65 binary_subtract_32 (void)
67 bool test __attribute__((unused)) = true;
68 decimal32 a (1000), b;
70 b = a - si; VERIFY (b == 1020);
71 b = a - ui; VERIFY (b == 950);
72 b = a - sl; VERIFY (b == 1010);
73 b = a - ul; VERIFY (b == 980);
74 b = a - sll; VERIFY (b == 1025);
75 b = a - ull; VERIFY (b == 950);
76 b = a - d32; VERIFY (b == 995);
77 b = (decimal32)(a - d64); VERIFY (b == 1010);
78 b = (decimal32)(a - d128); VERIFY (b == 975);
80 a = -1000;
81 b = si - a; VERIFY (b == 980);
82 b = ui - a; VERIFY (b == 1050);
83 b = sl - a; VERIFY (b == 990);
84 b = ul - a; VERIFY (b == 1020);
85 b = sll - a; VERIFY (b == 975);
86 b = ull - a; VERIFY (b == 1050);
87 b = d32 - a; VERIFY (b == 1005);
88 b = (decimal32)(d64 - a); VERIFY (b == 990);
89 b = (decimal32)(d128 - a); VERIFY (b == 1025);
92 void
93 binary_multiply_32 (void)
95 bool test __attribute__((unused)) = true;
96 decimal32 a (1000), b;
98 b = a * si; VERIFY (b == -20000);
99 b = a * ui; VERIFY (b == 50000);
100 b = a * sl; VERIFY (b == -10000);
101 b = a * ul; VERIFY (b == 20000);
102 b = a * sll; VERIFY (b == -25000);
103 b = a * ull; VERIFY (b == 50000);
104 b = a * d32; VERIFY (b == 5000);
105 b = (decimal32)(a * d64); VERIFY (b == -10000);
106 b = (decimal32)(a * d128); VERIFY (b == 25000);
108 b = si * a; VERIFY (b == -20000);
109 b = ui * a; VERIFY (b == 50000);
110 b = sl * a; VERIFY (b == -10000);
111 b = ul * a; VERIFY (b == 20000);
112 b = sll * a; VERIFY (b == -25000);
113 b = ull * a; VERIFY (b == 50000);
114 b = d32 * a; VERIFY (b == 5000);
115 b = (decimal32)(d64 * a); VERIFY (b == -10000);
116 b = (decimal32)(d128 * a); VERIFY (b == 25000);
119 void
120 binary_divide_32 (void)
122 bool test __attribute__((unused)) = true;
123 decimal32 a (1000), b;
125 b = a / si; VERIFY (b == -50);
126 b = a / ui; VERIFY (b == 20);
127 b = a / sl; VERIFY (b == -100);
128 b = a / ul; VERIFY (b == 50);
129 b = a / sll; VERIFY (b == -40);
130 b = a / ull; VERIFY (b == 20);
131 b = a / d32; VERIFY (b == 200);
132 b = (decimal32)(a / d64); VERIFY (b == -100);
133 b = (decimal32)(a / d128); VERIFY (b == 40);
135 a = 5;
136 b = si / a; VERIFY (b == -4);
137 b = ui / a; VERIFY (b == 10);
138 b = sl / a; VERIFY (b == -2);
139 b = ul / a; VERIFY (b == 4);
140 b = sll / a; VERIFY (b == -5);
141 b = ull / a; VERIFY (b == 10);
142 b = d32 / a; VERIFY (b == 1);
143 b = (decimal32)(d64 / a); VERIFY (b == -2);
144 b = (decimal32)(d128 / a); VERIFY (b == 5);
147 void
148 binary_add_64 (void)
150 bool test __attribute__((unused)) = true;
151 decimal64 a (1000), b;
153 b = a + si; VERIFY (b == 980);
154 b = a + ui; VERIFY (b == 1050);
155 b = a + sl; VERIFY (b == 990);
156 b = a + ul; VERIFY (b == 1020);
157 b = a + sll; VERIFY (b == 975);
158 b = a + ull; VERIFY (b == 1050);
159 b = a + d32; VERIFY (b == 1005);
160 b = a + d64; VERIFY (b == 990);
161 b = (decimal64)(a + d128); VERIFY (b == 1025);
163 b = a + si; VERIFY (b == 980);
164 b = a + ui; VERIFY (b == 1050);
165 b = a + sl; VERIFY (b == 990);
166 b = a + ul; VERIFY (b == 1020);
167 b = a + sll; VERIFY (b == 975);
168 b = a + ull; VERIFY (b == 1050);
169 b = a + d32; VERIFY (b == 1005);
170 b = a + d64; VERIFY (b == 990);
171 b = (decimal64)(a + d128); VERIFY (b == 1025);
174 void
175 binary_subtract_64 (void)
177 bool test __attribute__((unused)) = true;
178 decimal64 a (1000), b;
180 b = a - si; VERIFY (b == 1020);
181 b = a - ui; VERIFY (b == 950);
182 b = a - sl; VERIFY (b == 1010);
183 b = a - ul; VERIFY (b == 980);
184 b = a - sll; VERIFY (b == 1025);
185 b = a - ull; VERIFY (b == 950);
186 b = a - d32; VERIFY (b == 995);
187 b = a - d64; VERIFY (b == 1010);
188 b = (decimal64)(a - d128); VERIFY (b == 975);
190 a = -1000;
191 b = si - a; VERIFY (b == 980);
192 b = ui - a; VERIFY (b == 1050);
193 b = sl - a; VERIFY (b == 990);
194 b = ul - a; VERIFY (b == 1020);
195 b = sll - a; VERIFY (b == 975);
196 b = ull - a; VERIFY (b == 1050);
197 b = d32 - a; VERIFY (b == 1005);
198 b = d64 - a; VERIFY (b == 990);
199 b = (decimal64)(d128 - a); VERIFY (b == 1025);
202 void
203 binary_multiply_64 (void)
205 bool test __attribute__((unused)) = true;
206 decimal64 a (1000), b;
208 b = a * si; VERIFY (b == -20000);
209 b = a * ui; VERIFY (b == 50000);
210 b = a * sl; VERIFY (b == -10000);
211 b = a * ul; VERIFY (b == 20000);
212 b = a * sll; VERIFY (b == -25000);
213 b = a * ull; VERIFY (b == 50000);
214 b = a * d32; VERIFY (b == 5000);
215 b = a * d64; VERIFY (b == -10000);
216 b = (decimal64)(a * d128); VERIFY (b == 25000);
218 b = si * a; VERIFY (b == -20000);
219 b = ui * a; VERIFY (b == 50000);
220 b = sl * a; VERIFY (b == -10000);
221 b = ul * a; VERIFY (b == 20000);
222 b = sll * a; VERIFY (b == -25000);
223 b = ull * a; VERIFY (b == 50000);
224 b = d32 * a; VERIFY (b == 5000);
225 b = d64 * a; VERIFY (b == -10000);
226 b = (decimal64)(d128 * a); VERIFY (b == 25000);
229 void
230 binary_divide_64 (void)
232 bool test __attribute__((unused)) = true;
233 decimal64 a (1000), b;
235 b = a / si; VERIFY (b == -50);
236 b = a / ui; VERIFY (b == 20);
237 b = a / sl; VERIFY (b == -100);
238 b = a / ul; VERIFY (b == 50);
239 b = a / sll; VERIFY (b == -40);
240 b = a / ull; VERIFY (b == 20);
241 b = a / d32; VERIFY (b == 200);
242 b = a / d64; VERIFY (b == -100);
243 b = (decimal64)(a / d128); VERIFY (b == 40);
245 a = 5;
246 b = si / a; VERIFY (b == -4);
247 b = ui / a; VERIFY (b == 10);
248 b = sl / a; VERIFY (b == -2);
249 b = ul / a; VERIFY (b == 4);
250 b = sll / a; VERIFY (b == -5);
251 b = ull / a; VERIFY (b == 10);
252 b = d32 / a; VERIFY (b == 1);
253 b = d64 / a; VERIFY (b == -2);
254 b = (decimal64)(d128 / a); VERIFY (b == 5);
257 void
258 binary_add_128 (void)
260 bool test __attribute__((unused)) = true;
261 decimal128 a (1000), b;
263 b = a + si; VERIFY (b == 980);
264 b = a + ui; VERIFY (b == 1050);
265 b = a + sl; VERIFY (b == 990);
266 b = a + ul; VERIFY (b == 1020);
267 b = a + sll; VERIFY (b == 975);
268 b = a + ull; VERIFY (b == 1050);
269 b = a + d32; VERIFY (b == 1005);
270 b = a + d64; VERIFY (b == 990);
271 b = a + d128; VERIFY (b == 1025);
273 b = a + si; VERIFY (b == 980);
274 b = a + ui; VERIFY (b == 1050);
275 b = a + sl; VERIFY (b == 990);
276 b = a + ul; VERIFY (b == 1020);
277 b = a + sll; VERIFY (b == 975);
278 b = a + ull; VERIFY (b == 1050);
279 b = a + d32; VERIFY (b == 1005);
280 b = a + d64; VERIFY (b == 990);
281 b = a + d128; VERIFY (b == 1025);
284 void
285 binary_subtract_128 (void)
287 bool test __attribute__((unused)) = true;
288 decimal128 a (1000), b;
290 b = a - si; VERIFY (b == 1020);
291 b = a - ui; VERIFY (b == 950);
292 b = a - sl; VERIFY (b == 1010);
293 b = a - ul; VERIFY (b == 980);
294 b = a - sll; VERIFY (b == 1025);
295 b = a - ull; VERIFY (b == 950);
296 b = a - d32; VERIFY (b == 995);
297 b = a - d64; VERIFY (b == 1010);
298 b = a - d128; VERIFY (b == 975);
300 a = -1000;
301 b = si - a; VERIFY (b == 980);
302 b = ui - a; VERIFY (b == 1050);
303 b = sl - a; VERIFY (b == 990);
304 b = ul - a; VERIFY (b == 1020);
305 b = sll - a; VERIFY (b == 975);
306 b = ull - a; VERIFY (b == 1050);
307 b = d32 - a; VERIFY (b == 1005);
308 b = d64 - a; VERIFY (b == 990);
309 b = d128 - a; VERIFY (b == 1025);
312 void
313 binary_multiply_128 (void)
315 bool test __attribute__((unused)) = true;
316 decimal128 a (1000), b;
318 b = a * si; VERIFY (b == -20000);
319 b = a * ui; VERIFY (b == 50000);
320 b = a * sl; VERIFY (b == -10000);
321 b = a * ul; VERIFY (b == 20000);
322 b = a * sll; VERIFY (b == -25000);
323 b = a * ull; VERIFY (b == 50000);
324 b = a * d32; VERIFY (b == 5000);
325 b = a * d64; VERIFY (b == -10000);
326 b = a * d128; VERIFY (b == 25000);
328 b = si * a; VERIFY (b == -20000);
329 b = ui * a; VERIFY (b == 50000);
330 b = sl * a; VERIFY (b == -10000);
331 b = ul * a; VERIFY (b == 20000);
332 b = sll * a; VERIFY (b == -25000);
333 b = ull * a; VERIFY (b == 50000);
334 b = d32 * a; VERIFY (b == 5000);
335 b = d64 * a; VERIFY (b == -10000);
336 b = d128 * a; VERIFY (b == 25000);
339 void
340 binary_divide_128 (void)
342 bool test __attribute__((unused)) = true;
343 decimal128 a (1000), b;
345 b = a / si; VERIFY (b == -50);
346 b = a / ui; VERIFY (b == 20);
347 b = a / sl; VERIFY (b == -100);
348 b = a / ul; VERIFY (b == 50);
349 b = a / sll; VERIFY (b == -40);
350 b = a / ull; VERIFY (b == 20);
351 b = a / d32; VERIFY (b == 200);
352 b = a / d64; VERIFY (b == -100);
353 b = a / d128; VERIFY (b == 40);
355 a = 5;
356 b = si / a; VERIFY (b == -4);
357 b = ui / a; VERIFY (b == 10);
358 b = sl / a; VERIFY (b == -2);
359 b = ul / a; VERIFY (b == 4);
360 b = sll / a; VERIFY (b == -5);
361 b = ull / a; VERIFY (b == 10);
362 b = d32 / a; VERIFY (b == 1);
363 b = d64 / a; VERIFY (b == -2);
364 b = d128 / a; VERIFY (b == 5);
368 main ()
370 binary_add_32 ();
371 binary_subtract_32 ();
372 binary_multiply_32 ();
373 binary_divide_32 ();
375 binary_add_64 ();
376 binary_subtract_64 ();
377 binary_multiply_64 ();
378 binary_divide_64 ();
380 binary_add_128 ();
381 binary_subtract_128 ();
382 binary_multiply_128 ();
383 binary_divide_128 ();