github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/tests/devel/tst-addsub.c (about)

     1  /* Copyright 1996, 2001 Free Software Foundation, Inc.
     2  
     3  This file is part of the GNU MP Library test suite.
     4  
     5  The GNU MP Library test suite is free software; you can redistribute it
     6  and/or modify it under the terms of the GNU General Public License as
     7  published by the Free Software Foundation; either version 3 of the License,
     8  or (at your option) any later version.
     9  
    10  The GNU MP Library test suite is distributed in the hope that it will be
    11  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
    13  Public License for more details.
    14  
    15  You should have received a copy of the GNU General Public License along with
    16  the GNU MP Library test suite.  If not, see https://www.gnu.org/licenses/.  */
    17  
    18  #include <stdio.h>
    19  #include <stdlib.h>
    20  #include "gmp.h"
    21  #include "gmp-impl.h"
    22  #include "tests.h"
    23  
    24  #define ADD 1
    25  #define SUB 2
    26  
    27  #ifndef METHOD
    28  #define METHOD ADD
    29  #endif
    30  
    31  #if METHOD == ADD
    32  #define REFCALL refmpn_add_n
    33  #define TESTCALL mpn_add_n
    34  #endif
    35  
    36  #if METHOD == SUB
    37  #define REFCALL refmpn_sub_n
    38  #define TESTCALL mpn_sub_n
    39  #endif
    40  
    41  #define SIZE 100
    42  
    43  int
    44  main (int argc, char **argv)
    45  {
    46    mp_size_t alloc_size, max_size, size, i, cumul_size;
    47    mp_ptr s1, s2, dx, dy;
    48    int s1_align, s2_align, d_align;
    49    long pass, n_passes;
    50    mp_limb_t cx, cy;
    51  
    52    max_size = SIZE;
    53    n_passes = 1000000;
    54  
    55    argc--; argv++;
    56    if (argc)
    57      {
    58        max_size = atol (*argv);
    59        argc--; argv++;
    60      }
    61  
    62    alloc_size = max_size + 32;
    63    s1 = malloc (alloc_size * GMP_LIMB_BYTES);
    64    s2 = malloc (alloc_size * GMP_LIMB_BYTES);
    65    dx = malloc (alloc_size * GMP_LIMB_BYTES);
    66    dy = malloc (alloc_size * GMP_LIMB_BYTES);
    67  
    68    cumul_size = 0;
    69    for (pass = 0; pass < n_passes; pass++)
    70      {
    71        size = random () % max_size + 1;
    72  
    73        cumul_size += size;
    74        if (cumul_size >= 1000000)
    75  	{
    76  	  cumul_size -= 1000000;
    77  	  printf ("\r%ld", pass); fflush (stdout);
    78  	}
    79        s1_align = random () % 32;
    80        s2_align = random () % 32;
    81        d_align = random () % 32;
    82  
    83        mpn_random2 (s1 + s1_align, size);
    84        mpn_random2 (s2 + s2_align, size);
    85  
    86        for (i = 0; i < alloc_size; i++)
    87  	dx[i] = dy[i] = i + 0x9876500;
    88  
    89        cx = TESTCALL (dx + d_align, s1 + s1_align, s2 + s2_align, size);
    90        cy = REFCALL (dy + d_align, s1 + s1_align, s2 + s2_align, size);
    91  
    92        if (cx != cy || mpn_cmp (dx, dy, alloc_size) != 0)
    93  	abort ();
    94      }
    95  
    96    printf ("%ld passes OK\n", n_passes);
    97    exit (0);
    98  }