Austin Schuh | bb1338c | 2024-06-15 19:31:16 -0700 | [diff] [blame] | 1 | /* Test mpq_inv (and set/get_num/den). |
| 2 | |
| 3 | Copyright 2012 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 "gmp-impl.h" |
| 21 | #include "tests.h" |
| 22 | |
| 23 | int |
| 24 | main (int argc, char **argv) |
| 25 | { |
| 26 | mpq_t a, b; |
| 27 | mpz_t m, n; |
| 28 | const char* s = "-420000000000000000000000"; |
| 29 | |
| 30 | tests_start (); |
| 31 | |
| 32 | mpq_inits (a, b, (mpq_ptr)0); |
| 33 | mpz_inits (m, n, (mpz_ptr)0); |
| 34 | |
| 35 | mpz_set_ui (m, 13); |
| 36 | mpq_set_den (a, m); |
| 37 | mpz_set_str (m, s, 0); |
| 38 | mpq_set_num (a, m); |
| 39 | MPQ_CHECK_FORMAT (a); |
| 40 | mpq_inv (b, a); |
| 41 | MPQ_CHECK_FORMAT (b); |
| 42 | mpq_get_num (n, b); |
| 43 | ASSERT_ALWAYS (mpz_cmp_si (n, -13) == 0); |
| 44 | mpq_neg (b, b); |
| 45 | mpq_inv (a, b); |
| 46 | MPQ_CHECK_FORMAT (a); |
| 47 | mpq_inv (b, b); |
| 48 | MPQ_CHECK_FORMAT (b); |
| 49 | mpq_get_den (n, b); |
| 50 | ASSERT_ALWAYS (mpz_cmp_ui (n, 13) == 0); |
| 51 | mpq_get_num (n, a); |
| 52 | mpz_add (n, n, m); |
| 53 | ASSERT_ALWAYS (mpz_sgn (n) == 0); |
| 54 | |
| 55 | mpq_clears (a, b, (mpq_ptr)0); |
| 56 | mpz_clears (m, n, (mpz_ptr)0); |
| 57 | |
| 58 | tests_end (); |
| 59 | return 0; |
| 60 | } |