1 /* Test mp*_class operators and functions.
2
3 Copyright 2001, 2002, 2003 Free Software Foundation, Inc.
4
5 This file is part of the GNU MP Library.
6
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or (at your
10 option) any later version.
11
12 The GNU MP Library is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with the GNU MP Library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20 MA 02110-1301, USA. */
21
22 #include <iostream>
23
24 #include "mpir.h"
25 #include "mpirxx.h"
26 #include "gmp-impl.h"
27 #include "tests.h"
28
29 using namespace std;
30
31
32 void
check_mpz(void)33 check_mpz (void)
34 {
35 // unary operators and functions
36
37 // operator+
38 {
39 mpz_class a(1);
40 mpz_class b;
41 b = +a; ASSERT_ALWAYS(b == 1);
42 }
43
44 // operator-
45 {
46 mpz_class a(2);
47 mpz_class b;
48 b = -a; ASSERT_ALWAYS(b == -2);
49 }
50
51 // operator~
52 {
53 mpz_class a(3);
54 mpz_class b;
55 b = ~a; ASSERT_ALWAYS(b == -4);
56 }
57
58 // abs
59 {
60 mpz_class a(-123);
61 mpz_class b;
62 b = abs(a); ASSERT_ALWAYS(b == 123);
63 }
64
65 // sqrt
66 {
67 mpz_class a(25);
68 mpz_class b;
69 b = sqrt(a); ASSERT_ALWAYS(b == 5);
70 }
71 {
72 mpz_class a(125);
73 mpz_class b;
74 b = sqrt(a); ASSERT_ALWAYS(b == 11); // round toward zero
75 }
76
77 // sgn
78 {
79 mpz_class a(123);
80 int b = sgn(a); ASSERT_ALWAYS(b == 1);
81 }
82 {
83 mpz_class a(0);
84 int b = sgn(a); ASSERT_ALWAYS(b == 0);
85 }
86 {
87 mpz_class a(-123);
88 int b = sgn(a); ASSERT_ALWAYS(b == -1);
89 }
90
91
92 // binary operators and functions
93
94 // operator+
95 {
96 mpz_class a(1), b(2);
97 mpz_class c;
98 c = a + b; ASSERT_ALWAYS(c == 3);
99 }
100 {
101 mpz_class a(3);
102 signed int b = 4;
103 mpz_class c;
104 c = a + b; ASSERT_ALWAYS(c == 7);
105 }
106 {
107 mpz_class a(5);
108 double b = 6.0;
109 mpz_class c;
110 c = b + a; ASSERT_ALWAYS(c == 11);
111 }
112
113 // operator-
114 {
115 mpz_class a(3), b(6);
116 mpz_class c;
117 c = a - b; ASSERT_ALWAYS(c == -3);
118 }
119
120 // operator*
121 {
122 mpz_class a(-2), b(4);
123 mpz_class c;
124 c = a * b; ASSERT_ALWAYS(c == -8);
125 }
126 {
127 mpz_class a(2);
128 long b = -4;
129 mpz_class c;
130 c = a * b; ASSERT_ALWAYS(c == -8);
131 c = b * a; ASSERT_ALWAYS(c == -8);
132 }
133 {
134 mpz_class a(-2);
135 unsigned long b = 4;
136 mpz_class c;
137 c = a * b; ASSERT_ALWAYS(c == -8);
138 c = b * a; ASSERT_ALWAYS(c == -8);
139 }
140
141 // operator/ and operator%
142 {
143 mpz_class a(12), b(4);
144 mpz_class c;
145 c = a / b; ASSERT_ALWAYS(c == 3);
146 c = a % b; ASSERT_ALWAYS(c == 0);
147 }
148 {
149 mpz_class a(7), b(5);
150 mpz_class c;
151 c = a / b; ASSERT_ALWAYS(c == 1);
152 c = a % b; ASSERT_ALWAYS(c == 2);
153 }
154 {
155 mpz_class a(-10);
156 signed int ai = -10;
157 mpz_class b(3);
158 signed int bi = 3;
159 mpz_class c;
160 c = a / b; ASSERT_ALWAYS(c == -3);
161 c = a % b; ASSERT_ALWAYS(c == -1);
162 c = a / bi; ASSERT_ALWAYS(c == -3);
163 c = a % bi; ASSERT_ALWAYS(c == -1);
164 c = ai / b; ASSERT_ALWAYS(c == -3);
165 c = ai % b; ASSERT_ALWAYS(c == -1);
166 }
167 {
168 mpz_class a(-10);
169 signed int ai = -10;
170 mpz_class b(-3);
171 signed int bi = -3;
172 mpz_class c;
173 c = a / b; ASSERT_ALWAYS(c == 3);
174 c = a % b; ASSERT_ALWAYS(c == -1);
175 c = a / bi; ASSERT_ALWAYS(c == 3);
176 c = a % bi; ASSERT_ALWAYS(c == -1);
177 c = ai / b; ASSERT_ALWAYS(c == 3);
178 c = ai % b; ASSERT_ALWAYS(c == -1);
179 }
180 {
181 mpz_class a (static_cast<mpir_si>(GMP_SI_MIN));
182 mpir_si ai = GMP_SI_MIN;
183 mpz_class b = - mpz_class (static_cast<mpir_si>(GMP_SI_MIN));
184 mpz_class c;
185 c = a / b; ASSERT_ALWAYS(c == -1);
186 c = a % b; ASSERT_ALWAYS(c == 0);
187 c = ai / b; ASSERT_ALWAYS(c == -1);
188 c = ai % b; ASSERT_ALWAYS(c == 0);
189 }
190
191 // operator&
192 // operator|
193 // operator^
194
195 // operator<<
196 {
197 mpz_class a(3);
198 unsigned int b = 4;
199 mpz_class c;
200 c = a << b; ASSERT_ALWAYS(c == 48);
201 }
202
203 // operator>>
204 {
205 mpz_class a(127);
206 unsigned int b = 4;
207 mpz_class c;
208 c = a >> b; ASSERT_ALWAYS(c == 7);
209 }
210
211 // operator==
212 // operator!=
213 // operator<
214 // operator<=
215 // operator>
216 // operator>=
217
218 // cmp
219 {
220 mpz_class a(123), b(45);
221 int c;
222 c = cmp(a, b); ASSERT_ALWAYS(c > 0);
223 c = cmp(b, a); ASSERT_ALWAYS(c < 0);
224 }
225 {
226 mpz_class a(123);
227 unsigned long b = 45;
228 int c;
229 c = cmp(a, b); ASSERT_ALWAYS(c > 0);
230 c = cmp(b, a); ASSERT_ALWAYS(c < 0);
231 }
232 {
233 mpz_class a(123);
234 long b = 45;
235 int c;
236 c = cmp(a, b); ASSERT_ALWAYS(c > 0);
237 c = cmp(b, a); ASSERT_ALWAYS(c < 0);
238 }
239 {
240 mpz_class a(123);
241 double b = 45;
242 int c;
243 c = cmp(a, b); ASSERT_ALWAYS(c > 0);
244 c = cmp(b, a); ASSERT_ALWAYS(c < 0);
245 }
246
247
248 // ternary operators
249
250 // mpz_addmul
251 {
252 mpz_class a(1), b(2), c(3);
253 mpz_class d;
254 d = a + b * c; ASSERT_ALWAYS(d == 7);
255 }
256 {
257 mpz_class a(1), b(2);
258 unsigned int c = 3;
259 mpz_class d;
260 d = a + b * c; ASSERT_ALWAYS(d == 7);
261 }
262 {
263 mpz_class a(1), b(3);
264 unsigned int c = 2;
265 mpz_class d;
266 d = a + c * b; ASSERT_ALWAYS(d == 7);
267 }
268 {
269 mpz_class a(1), b(2);
270 signed int c = 3;
271 mpz_class d;
272 d = a + b * c; ASSERT_ALWAYS(d == 7);
273 }
274 {
275 mpz_class a(1), b(3);
276 signed int c = 2;
277 mpz_class d;
278 d = a + c * b; ASSERT_ALWAYS(d == 7);
279 }
280 {
281 mpz_class a(1), b(2);
282 double c = 3.0;
283 mpz_class d;
284 d = a + b * c; ASSERT_ALWAYS(d == 7);
285 }
286 {
287 mpz_class a(1), b(3);
288 double c = 2.0;
289 mpz_class d;
290 d = a + c * b; ASSERT_ALWAYS(d == 7);
291 }
292
293 {
294 mpz_class a(2), b(3), c(4);
295 mpz_class d;
296 d = a * b + c; ASSERT_ALWAYS(d == 10);
297 }
298 {
299 mpz_class a(2), b(4);
300 unsigned int c = 3;
301 mpz_class d;
302 d = a * c + b; ASSERT_ALWAYS(d == 10);
303 }
304 {
305 mpz_class a(3), b(4);
306 unsigned int c = 2;
307 mpz_class d;
308 d = c * a + b; ASSERT_ALWAYS(d == 10);
309 }
310 {
311 mpz_class a(2), b(4);
312 signed int c = 3;
313 mpz_class d;
314 d = a * c + b; ASSERT_ALWAYS(d == 10);
315 }
316 {
317 mpz_class a(3), b(4);
318 signed int c = 2;
319 mpz_class d;
320 d = c * a + b; ASSERT_ALWAYS(d == 10);
321 }
322 {
323 mpz_class a(2), b(4);
324 double c = 3.0;
325 mpz_class d;
326 d = a * c + b; ASSERT_ALWAYS(d == 10);
327 }
328 {
329 mpz_class a(3), b(4);
330 double c = 2.0;
331 mpz_class d;
332 d = c * a + b; ASSERT_ALWAYS(d == 10);
333 }
334
335 // mpz_submul
336 {
337 mpz_class a(1), b(2), c(3);
338 mpz_class d;
339 d = a - b * c; ASSERT_ALWAYS(d == -5);
340 }
341 {
342 mpz_class a(1), b(2);
343 unsigned int c = 3;
344 mpz_class d;
345 d = a - b * c; ASSERT_ALWAYS(d == -5);
346 }
347 {
348 mpz_class a(1), b(3);
349 unsigned int c = 2;
350 mpz_class d;
351 d = a - c * b; ASSERT_ALWAYS(d == -5);
352 }
353 {
354 mpz_class a(1), b(2);
355 signed int c = 3;
356 mpz_class d;
357 d = a - b * c; ASSERT_ALWAYS(d == -5);
358 }
359 {
360 mpz_class a(1), b(3);
361 signed int c = 2;
362 mpz_class d;
363 d = a - c * b; ASSERT_ALWAYS(d == -5);
364 }
365 {
366 mpz_class a(1), b(2);
367 double c = 3.0;
368 mpz_class d;
369 d = a - b * c; ASSERT_ALWAYS(d == -5);
370 }
371 {
372 mpz_class a(1), b(3);
373 double c = 2.0;
374 mpz_class d;
375 d = a - c * b; ASSERT_ALWAYS(d == -5);
376 }
377
378 {
379 mpz_class a(2), b(3), c(4);
380 mpz_class d;
381 d = a * b - c; ASSERT_ALWAYS(d == 2);
382 }
383 {
384 mpz_class a(2), b(4);
385 unsigned int c = 3;
386 mpz_class d;
387 d = a * c - b; ASSERT_ALWAYS(d == 2);
388 }
389 {
390 mpz_class a(3), b(4);
391 unsigned int c = 2;
392 mpz_class d;
393 d = c * a - b; ASSERT_ALWAYS(d == 2);
394 }
395 {
396 mpz_class a(2), b(4);
397 signed int c = 3;
398 mpz_class d;
399 d = a * c - b; ASSERT_ALWAYS(d == 2);
400 }
401 {
402 mpz_class a(3), b(4);
403 signed int c = 2;
404 mpz_class d;
405 d = c * a - b; ASSERT_ALWAYS(d == 2);
406 }
407 {
408 mpz_class a(2), b(4);
409 double c = 3.0;
410 mpz_class d;
411 d = a * c - b; ASSERT_ALWAYS(d == 2);
412 }
413 {
414 mpz_class a(3), b(4);
415 double c = 2.0;
416 mpz_class d;
417 d = c * a - b; ASSERT_ALWAYS(d == 2);
418 }
419 }
420
421 void
check_mpq(void)422 check_mpq (void)
423 {
424 // unary operators and functions
425
426 // operator+
427 {
428 mpq_class a(1, 2);
429 mpq_class b;
430 b = +a; ASSERT_ALWAYS(b == 0.5);
431 }
432
433 // operator-
434 {
435 mpq_class a(3, 4);
436 mpq_class b;
437 b = -a; ASSERT_ALWAYS(b == -0.75);
438 }
439
440 // abs
441 {
442 mpq_class a(-123);
443 mpq_class b;
444 b = abs(a); ASSERT_ALWAYS(b == 123);
445 }
446
447 // sgn
448 {
449 mpq_class a(123);
450 int b = sgn(a); ASSERT_ALWAYS(b == 1);
451 }
452 {
453 mpq_class a(0);
454 int b = sgn(a); ASSERT_ALWAYS(b == 0);
455 }
456 {
457 mpq_class a(-123);
458 int b = sgn(a); ASSERT_ALWAYS(b == -1);
459 }
460
461
462 // binary operators and functions
463
464 // operator+
465 {
466 mpq_class a(1, 2), b(3, 4);
467 mpq_class c;
468 c = a + b; ASSERT_ALWAYS(c == 1.25);
469 }
470 {
471 mpq_class a(1, 2);
472 signed int b = 2;
473 mpq_class c;
474 c = a + b; ASSERT_ALWAYS(c == 2.5);
475 }
476 {
477 mpq_class a(1, 2);
478 double b = 1.5;
479 mpq_class c;
480 c = b + a; ASSERT_ALWAYS(c == 2);
481 }
482
483 // operator-
484 {
485 mpq_class a(1, 2), b(3, 4);
486 mpq_class c;
487 c = a - b; ASSERT_ALWAYS(c == -0.25);
488 }
489
490 // operator*
491 {
492 mpq_class a(1, 3), b(3, 4);
493 mpq_class c;
494 c = a * b; ASSERT_ALWAYS(c == 0.25);
495 }
496
497 // operator/
498 {
499 mpq_class a(1, 2), b(2, 3);
500 mpq_class c;
501 c = a / b; ASSERT_ALWAYS(c == 0.75);
502 }
503
504 // operator<<
505 // operator>>
506 // operator==
507 // operator!=
508 // operator<
509 // operator<=
510 // operator>
511 // operator>=
512
513 // cmp
514 {
515 mpq_class a(123), b(45);
516 int c;
517 c = cmp(a, b); ASSERT_ALWAYS(c > 0);
518 c = cmp(b, a); ASSERT_ALWAYS(c < 0);
519 }
520 {
521 mpq_class a(123);
522 unsigned long b = 45;
523 int c;
524 c = cmp(a, b); ASSERT_ALWAYS(c > 0);
525 c = cmp(b, a); ASSERT_ALWAYS(c < 0);
526 }
527 {
528 mpq_class a(123);
529 long b = 45;
530 int c;
531 c = cmp(a, b); ASSERT_ALWAYS(c > 0);
532 c = cmp(b, a); ASSERT_ALWAYS(c < 0);
533 }
534 {
535 mpq_class a(123);
536 double b = 45;
537 int c;
538 c = cmp(a, b); ASSERT_ALWAYS(c > 0);
539 c = cmp(b, a); ASSERT_ALWAYS(c < 0);
540 }
541 }
542
543 void
check_mpf(void)544 check_mpf (void)
545 {
546 // unary operators and functions
547
548 // operator+
549 {
550 mpf_class a(1);
551 mpf_class b;
552 b = +a; ASSERT_ALWAYS(b == 1);
553 }
554
555 // operator-
556 {
557 mpf_class a(2);
558 mpf_class b;
559 b = -a; ASSERT_ALWAYS(b == -2);
560 }
561
562 // abs
563 {
564 mpf_class a(-123);
565 mpf_class b;
566 b = abs(a); ASSERT_ALWAYS(b == 123);
567 }
568
569 // trunc
570 {
571 mpf_class a(1.5);
572 mpf_class b;
573 b = trunc(a); ASSERT_ALWAYS(b == 1);
574 }
575 {
576 mpf_class a(-1.5);
577 mpf_class b;
578 b = trunc(a); ASSERT_ALWAYS(b == -1);
579 }
580
581 // floor
582 {
583 mpf_class a(1.9);
584 mpf_class b;
585 b = floor(a); ASSERT_ALWAYS(b == 1);
586 }
587 {
588 mpf_class a(-1.1);
589 mpf_class b;
590 b = floor(a); ASSERT_ALWAYS(b == -2);
591 }
592
593 // ceil
594 {
595 mpf_class a(1.1);
596 mpf_class b;
597 b = ceil(a); ASSERT_ALWAYS(b == 2);
598 }
599 {
600 mpf_class a(-1.9);
601 mpf_class b;
602 b = ceil(a); ASSERT_ALWAYS(b == -1);
603 }
604
605 // sqrt
606 {
607 mpf_class a(25);
608 mpf_class b;
609 b = sqrt(a); ASSERT_ALWAYS(b == 5);
610 }
611 {
612 mpf_class a(2.25);
613 mpf_class b;
614 b = sqrt(a); ASSERT_ALWAYS(b == 1.5);
615 }
616
617 // sgn
618 {
619 mpf_class a(123);
620 int b = sgn(a); ASSERT_ALWAYS(b == 1);
621 }
622 {
623 mpf_class a(0);
624 int b = sgn(a); ASSERT_ALWAYS(b == 0);
625 }
626 {
627 mpf_class a(-123);
628 int b = sgn(a); ASSERT_ALWAYS(b == -1);
629 }
630
631
632 // binary operators and functions
633
634 // operator+
635 {
636 mpf_class a(1), b(2);
637 mpf_class c;
638 c = a + b; ASSERT_ALWAYS(c == 3);
639 }
640
641 // operator-
642 {
643 mpf_class a(3), b(4);
644 mpf_class c;
645 c = a - b; ASSERT_ALWAYS(c == -1);
646 }
647
648 // operator*
649 {
650 mpf_class a(2), b(5);
651 mpf_class c;
652 c = a * b; ASSERT_ALWAYS(c == 10);
653 }
654
655 // operator/
656 {
657 mpf_class a(7), b(4);
658 mpf_class c;
659 c = a / b; ASSERT_ALWAYS(c == 1.75);
660 }
661
662 // operator<<
663 // operator>>
664 // operator==
665 // operator!=
666 // operator<
667 // operator<=
668 // operator>
669 // operator>=
670
671 // hypot
672 {
673 mpf_class a(3), b(4);
674 mpf_class c;
675 c = hypot(a, b); ASSERT_ALWAYS(c == 5);
676 }
677
678 // cmp
679 {
680 mpf_class a(123), b(45);
681 int c;
682 c = cmp(a, b); ASSERT_ALWAYS(c > 0);
683 c = cmp(b, a); ASSERT_ALWAYS(c < 0);
684 }
685 {
686 mpf_class a(123);
687 unsigned long b = 45;
688 int c;
689 c = cmp(a, b); ASSERT_ALWAYS(c > 0);
690 c = cmp(b, a); ASSERT_ALWAYS(c < 0);
691 }
692 {
693 mpf_class a(123);
694 long b = 45;
695 int c;
696 c = cmp(a, b); ASSERT_ALWAYS(c > 0);
697 c = cmp(b, a); ASSERT_ALWAYS(c < 0);
698 }
699 {
700 mpf_class a(123);
701 double b = 45;
702 int c;
703 c = cmp(a, b); ASSERT_ALWAYS(c > 0);
704 c = cmp(b, a); ASSERT_ALWAYS(c < 0);
705 }
706 }
707
708
709
710 int
main(void)711 main (void)
712 {
713 tests_start();
714
715 check_mpz();
716 check_mpq();
717 check_mpf();
718
719 tests_end();
720 return 0;
721 }
722