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