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