github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/tests/mpq/t-inv.c (about)

     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.h"
    21  #include "gmp-impl.h"
    22  #include "tests.h"
    23  
    24  int
    25  main (int argc, char **argv)
    26  {
    27    mpq_t a, b;
    28    mpz_t m, n;
    29    const char* s = "-420000000000000000000000";
    30  
    31    tests_start ();
    32  
    33    mpq_inits (a, b, (mpq_ptr)0);
    34    mpz_inits (m, n, (mpz_ptr)0);
    35  
    36    mpz_set_ui (m, 13);
    37    mpq_set_den (a, m);
    38    mpz_set_str (m, s, 0);
    39    mpq_set_num (a, m);
    40    MPQ_CHECK_FORMAT (a);
    41    mpq_inv (b, a);
    42    MPQ_CHECK_FORMAT (b);
    43    mpq_get_num (n, b);
    44    ASSERT_ALWAYS (mpz_cmp_si (n, -13) == 0);
    45    mpq_neg (b, b);
    46    mpq_inv (a, b);
    47    MPQ_CHECK_FORMAT (a);
    48    mpq_inv (b, b);
    49    MPQ_CHECK_FORMAT (b);
    50    mpq_get_den (n, b);
    51    ASSERT_ALWAYS (mpz_cmp_ui (n, 13) == 0);
    52    mpq_get_num (n, a);
    53    mpz_add (n, n, m);
    54    ASSERT_ALWAYS (mpz_sgn (n) == 0);
    55  
    56    mpq_clears (a, b, (mpq_ptr)0);
    57    mpz_clears (m, n, (mpz_ptr)0);
    58  
    59    tests_end ();
    60    return 0;
    61  }