openat: don’t close (-1)
[gnulib.git] / tests / test-libgmp-mpq.c
blob0217d130acd395aadef6b3ae06224e7ce5f53fb3
1 /* Test of libgmp or its mini-mpq substitute.
2 Copyright (C) 2024 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 #include <config.h>
19 /* Specification. */
20 #include <gmp.h>
22 #include <limits.h>
23 #include <string.h>
25 #include "macros.h"
27 int
28 main ()
30 /* A simple sanity check that 2/3 + 2/3 = 4/3. */
31 static mp_limb_t const twobody[] = { 2 };
32 static mp_limb_t const threebody[] = { 3 };
33 static mpz_t const two = MPZ_ROINIT_N ((mp_limb_t *) twobody, 1);
34 static mpz_t const three = MPZ_ROINIT_N ((mp_limb_t *) threebody, 1);
35 ASSERT (mpz_fits_slong_p (two));
36 ASSERT (mpz_get_si (two) == 2);
37 ASSERT (mpz_fits_slong_p (three));
38 ASSERT (mpz_get_si (three) == 3);
40 mpq_t q;
41 mpq_init (q);
42 mpz_set (mpq_numref (q), two);
43 mpz_set (mpq_denref (q), three);
44 mpq_add (q, q, q);
45 ASSERT (mpz_fits_slong_p (mpq_numref (q)));
46 ASSERT (mpz_get_si (mpq_numref (q)) == 4);
47 ASSERT (mpz_fits_slong_p (mpq_denref (q)));
48 ASSERT (mpz_get_si (mpq_denref (q)) == 3);
49 mpq_clear (q);
51 return test_exit_status;