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