File: test-libgmp-mpq.c

package info (click to toggle)
coreutils 9.7-3
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 67,780 kB
  • sloc: ansic: 243,477; sh: 29,063; perl: 7,908; yacc: 1,858; makefile: 196; python: 47; sed: 16
file content (52 lines) | stat: -rw-r--r-- 1,661 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/* Test of libgmp or its mini-mpq substitute.
   Copyright (C) 2024-2025 Free Software Foundation, Inc.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */

#include <config.h>

/* Specification.  */
#include <gmp.h>

#include <limits.h>
#include <string.h>

#include "macros.h"

int
main ()
{
  /* A simple sanity check that 2/3 + 2/3 = 4/3.  */
  static mp_limb_t const twobody[] = { 2 };
  static mp_limb_t const threebody[] = { 3 };
  static mpz_t const two = MPZ_ROINIT_N ((mp_limb_t *) twobody, 1);
  static mpz_t const three = MPZ_ROINIT_N ((mp_limb_t *) threebody, 1);
  ASSERT (mpz_fits_slong_p (two));
  ASSERT (mpz_get_si (two) == 2);
  ASSERT (mpz_fits_slong_p (three));
  ASSERT (mpz_get_si (three) == 3);

  mpq_t q;
  mpq_init (q);
  mpz_set (mpq_numref (q), two);
  mpz_set (mpq_denref (q), three);
  mpq_add (q, q, q);
  ASSERT (mpz_fits_slong_p (mpq_numref (q)));
  ASSERT (mpz_get_si (mpq_numref (q)) == 4);
  ASSERT (mpz_fits_slong_p (mpq_denref (q)));
  ASSERT (mpz_get_si (mpq_denref (q)) == 3);
  mpq_clear (q);

  return test_exit_status;
}