blob: 87ba8d45eb7231e8dba19c62a2022fc3018962c0 [file] [log] [blame]
Austin Schuhbb1338c2024-06-15 19:31:16 -07001/* Test mpq_inv (and set/get_num/den).
2
3Copyright 2012 Free Software Foundation, Inc.
4
5This file is part of the GNU MP Library test suite.
6
7The GNU MP Library test suite is free software; you can redistribute it
8and/or modify it under the terms of the GNU General Public License as
9published by the Free Software Foundation; either version 3 of the License,
10or (at your option) any later version.
11
12The GNU MP Library test suite is distributed in the hope that it will be
13useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
15Public License for more details.
16
17You should have received a copy of the GNU General Public License along with
18the GNU MP Library test suite. If not, see https://www.gnu.org/licenses/. */
19
20#include "gmp-impl.h"
21#include "tests.h"
22
23int
24main (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}