(root)/
gmp-6.3.0/
mini-gmp/
tests/
t-lucm.c
       1  /* Tests the (internal) function mpz_lucas_mod
       2  
       3  Copyright 2018, 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 <limits.h>
      21  #include <stdlib.h>
      22  #include <stdio.h>
      23  #include <string.h>
      24  
      25  #include "testutils.h"
      26  
      27  #define MAXBITS 100
      28  #define COUNT 1000
      29  
      30  void
      31  testmain (int argc, char **argv)
      32  {
      33    unsigned i;
      34    mpz_t m, vr, qr, vm, qm, vt;
      35    int resm, resr;
      36    long Q;
      37    unsigned long b0;
      38  
      39    mpz_init (m);
      40    mpz_init (vr);
      41    mpz_init (qr);
      42    mpz_init (vm);
      43    mpz_init (qm);
      44    mpz_init (vt);
      45  
      46    for (i = 0; i < COUNT; i++)
      47      {
      48        mini_random_lucm_op (MAXBITS, vr, qr, m, &Q, &b0, &resr);
      49        if (b0 == 0)
      50  	{
      51  	  fprintf (stderr, "lucas_mod: test disabled (%u tests done).\n", i);
      52  	  break;
      53  	}
      54        resm = mpz_lucas_mod (vm, qm, Q, b0, m);
      55  
      56        if (resr != resm)
      57  	{
      58  	  if (resm != 0 || mpz_cmp_ui (vm, 0) != 0)
      59  	    {
      60  	      fprintf (stderr, "mpz_lucas_mod wrong return value (%d != %d):\n", resr, resm);
      61  	      fprintf (stderr, "Q = %ld , b0 = %lu\n", Q, b0);
      62  	      dump ("m", m);
      63  	      dump ("vm", vm);
      64  	      dump ("qm", qm);
      65  	      abort ();
      66  	    }
      67  	}
      68        else if (resm == 0)
      69  	{
      70  	  mpz_abs (vr, vr);
      71  	  mpz_sub (vt, m, vr);
      72  	  mpz_abs (vm, vm);
      73  	  mpz_mod (qm, qm, m);
      74  	  if (mpz_cmp_ui (qr, 0) < 0)
      75  	    mpz_add (qr, qr, m);
      76  	  if (mpz_cmp (qm, qr) != 0 ||
      77  	      (mpz_cmp (vm, vr) != 0 && mpz_cmp (vm, vt) != 0))
      78  	    {
      79  	      fprintf (stderr, "mpz_lucas_mod error:\n");
      80  	      fprintf (stderr, "Q = %ld , b0 = %lu\n", Q, b0);
      81  	      dump ("m", m);
      82  	      dump ("vm", vm);
      83  	      dump ("vr", vr);
      84  	      dump ("vt", vt);
      85  	      dump ("qm", qm);
      86  	      dump ("qr", qr);
      87  	      abort ();
      88  	    }
      89  
      90  	}
      91      }
      92    mpz_clear (m);
      93    mpz_clear (vr);
      94    mpz_clear (qr);
      95    mpz_clear (vm);
      96    mpz_clear (qm);
      97    mpz_clear (vt);
      98  }