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

     1  /* Test for sizeinbase function.
     2  
     3  Copyright 2014 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  
    21  #include <stdlib.h>
    22  #include <stdio.h>
    23  
    24  #include "gmp.h"
    25  #include "gmp-impl.h"
    26  #include "tests.h"
    27  
    28  /* Exponents up to 2^SIZE_LOG */
    29  #ifndef SIZE_LOG
    30  #define SIZE_LOG 13
    31  #endif
    32  
    33  #ifndef COUNT
    34  #define COUNT 30
    35  #endif
    36  
    37  #define MAX_N (1<<SIZE_LOG)
    38  
    39  int
    40  main (int argc, char **argv)
    41  {
    42    mp_limb_t a;
    43    mp_ptr pp, scratch;
    44    mp_limb_t max_b;
    45    int count = COUNT;
    46    int test;
    47    gmp_randstate_ptr rands;
    48    TMP_DECL;
    49  
    50    if (argc > 1)
    51      {
    52        char *end;
    53        count = strtol (argv[1], &end, 0);
    54        if (*end || count <= 0)
    55  	{
    56  	  fprintf (stderr, "Invalid test count: %s.\n", argv[1]);
    57  	  return 1;
    58  	}
    59      }
    60  
    61    tests_start ();
    62    TMP_MARK;
    63    rands = RANDS;
    64  
    65    pp = TMP_ALLOC_LIMBS (MAX_N);
    66    scratch = TMP_ALLOC_LIMBS (MAX_N);
    67    max_b = numberof (mp_bases);
    68  
    69    ASSERT_ALWAYS (max_b > 62);
    70    ASSERT_ALWAYS (max_b < GMP_NUMB_MAX);
    71  
    72    for (a = 2; a < max_b; ++a)
    73      for (test = 0; test < count; ++test)
    74        {
    75  	mp_size_t pn;
    76  	mp_limb_t exp;
    77  	mp_bitcnt_t res;
    78  
    79  	exp = gmp_urandomm_ui (rands, MAX_N);
    80  
    81  	pn = mpn_pow_1 (pp, &a, 1, exp, scratch);
    82  
    83  	res = mpn_sizeinbase (pp, pn, a) - 1;
    84  
    85  	if ((res < exp) || (res > exp + 1))
    86  	  {
    87  	    printf ("ERROR in test %d, base = %d, exp = %d, res = %d\n",
    88  		    test, (int) a, (int) exp, (int) res);
    89  	    abort();
    90  	  }
    91  
    92  	mpn_sub_1 (pp, pp, pn, CNST_LIMB(1));
    93  	pn -= pp[pn-1] == 0;
    94  
    95  	res = mpn_sizeinbase (pp, pn, a);
    96  
    97  	if ((res < exp) || (res - 1 > exp))
    98  	  {
    99  	    printf ("ERROR in -1 test %d, base = %d, exp = %d, res = %d\n",
   100  		    test, (int) a, (int) exp, (int) res);
   101  	    abort();
   102  	  }
   103        }
   104  
   105    TMP_FREE;
   106    tests_end ();
   107    return 0;
   108  }