1 /* Test mpz_fib_ui and mpz_fib2_ui.
3 Copyright 2000, 2001, 2002 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
12 The GNU MP Library is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
27 /* Usage: t-fib_ui [x|num]
29 Run with no arguments, tests goes up to the initial value of "limit"
30 below. With a number argument tests are carried up that far, or with a
31 literal "x" tests are continued without limit (this being only meant for
32 development purposes).
34 The size tests performed are designed to partially replicate what will be
35 going on in mpz_fib_ui. There's plenty of ASSERTs there, but of course
36 they're not normally enabled.
40 The tests on MPN_FIB2_SIZE are a bit useless, since that macro includes a
41 +2 for the internal purposes of mpn_fib2_ui. It's probably better to
42 give mpn_fib2_ui a run with assertion checking enabled. */
45 #define MPZ_FIB_SIZE_FLOAT(n) \
46 ((mp_size_t) ((n) * 0.6942419 / GMP_NUMB_BITS + 1))
50 check_fib_table (void)
55 ASSERT_ALWAYS (FIB_TABLE(-1) == 1);
56 ASSERT_ALWAYS (FIB_TABLE(0) == 0);
58 for (i = 1; i <= FIB_TABLE_LIMIT; i++)
60 want = FIB_TABLE(i-1) + FIB_TABLE(i-2);
61 if (FIB_TABLE(i) != want)
63 printf ("FIB_TABLE(%d) wrong\n", i);
64 gmp_printf (" got %#Nx\n", &FIB_TABLE(i), 1);
65 gmp_printf (" want %#Nx\n", &want, 1);
73 main (int argc, char *argv[])
76 unsigned long limit = 100 * GMP_LIMB_BITS;
77 mpz_t want_fn, want_fn1, got_fn, got_fn1;
81 if (argc > 1 && argv[1][0] == 'x')
84 limit = atoi (argv[1]);
89 mpz_init_set_ui (want_fn1, 1); /* F[-1] */
90 mpz_init_set_ui (want_fn, 0); /* F[0] */
94 for (n = 0; n < limit; n++)
96 /* check our float formula seems right */
97 if (MPZ_FIB_SIZE_FLOAT (n) < SIZ(want_fn))
99 printf ("MPZ_FIB_SIZE_FLOAT wrong at n=%lu\n", n);
100 printf (" MPZ_FIB_SIZE_FLOAT %ld\n", MPZ_FIB_SIZE_FLOAT (n));
101 printf (" SIZ(want_fn) %d\n", SIZ(want_fn));
105 /* check MPN_FIB2_SIZE seems right, compared to actual size and
106 compared to our float formula */
107 if (MPN_FIB2_SIZE (n) < MPZ_FIB_SIZE_FLOAT (n))
109 printf ("MPN_FIB2_SIZE wrong at n=%lu\n", n);
110 printf (" MPN_FIB2_SIZE %ld\n", MPN_FIB2_SIZE (n));
111 printf (" MPZ_FIB_SIZE_FLOAT %ld\n", MPZ_FIB_SIZE_FLOAT (n));
114 if (MPN_FIB2_SIZE (n) < SIZ(want_fn))
116 printf ("MPN_FIB2_SIZE wrong at n=%lu\n", n);
117 printf (" MPN_FIB2_SIZE %ld\n", MPN_FIB2_SIZE (n));
118 printf (" SIZ(want_fn) %d\n", SIZ(want_fn));
122 mpz_fib2_ui (got_fn, got_fn1, n);
123 MPZ_CHECK_FORMAT (got_fn);
124 MPZ_CHECK_FORMAT (got_fn1);
125 if (mpz_cmp (got_fn, want_fn) != 0 || mpz_cmp (got_fn1, want_fn1) != 0)
127 printf ("mpz_fib2_ui(%lu) wrong\n", n);
128 mpz_trace ("want fn ", want_fn);
129 mpz_trace ("got fn ", got_fn);
130 mpz_trace ("want fn1", want_fn1);
131 mpz_trace ("got fn1", got_fn1);
135 mpz_fib_ui (got_fn, n);
136 MPZ_CHECK_FORMAT (got_fn);
137 if (mpz_cmp (got_fn, want_fn) != 0)
139 printf ("mpz_fib_ui(%lu) wrong\n", n);
140 mpz_trace ("want fn", want_fn);
141 mpz_trace ("got fn", got_fn);
145 mpz_add (want_fn1, want_fn1, want_fn); /* F[n+1] = F[n] + F[n-1] */
146 mpz_swap (want_fn1, want_fn);
150 mpz_clear (want_fn1);