1 | /* Test mpz_fib_ui and mpz_fib2_ui. |
---|
2 | |
---|
3 | Copyright 2000, 2001, 2002 Free Software Foundation, Inc. |
---|
4 | |
---|
5 | This file is part of the GNU MP Library. |
---|
6 | |
---|
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 2.1 of the License, or (at your |
---|
10 | option) any later version. |
---|
11 | |
---|
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. |
---|
16 | |
---|
17 | You should have received a copy of the GNU Lesser General Public License |
---|
18 | along with the GNU MP Library; see the file COPYING.LIB. If not, write to |
---|
19 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
---|
20 | MA 02111-1307, USA. */ |
---|
21 | |
---|
22 | #include <stdio.h> |
---|
23 | #include <stdlib.h> |
---|
24 | #include "gmp.h" |
---|
25 | #include "gmp-impl.h" |
---|
26 | #include "tests.h" |
---|
27 | |
---|
28 | |
---|
29 | /* Usage: t-fib_ui [x|num] |
---|
30 | |
---|
31 | Run with no arguments, tests goes up to the initial value of "limit" |
---|
32 | below. With a number argument tests are carried up that far, or with a |
---|
33 | literal "x" tests are continued without limit (this being only meant for |
---|
34 | development purposes). |
---|
35 | |
---|
36 | The size tests performed are designed to partially replicate what will be |
---|
37 | going on in mpz_fib_ui. There's plenty of ASSERTs there, but of course |
---|
38 | they're not normally enabled. |
---|
39 | |
---|
40 | Misfeatures: |
---|
41 | |
---|
42 | The tests on MPN_FIB2_SIZE are a bit useless, since that macro includes a |
---|
43 | +2 for the internal purposes of mpn_fib2_ui. It's probably better to |
---|
44 | give mpn_fib2_ui a run with assertion checking enabled. */ |
---|
45 | |
---|
46 | |
---|
47 | #define MPZ_FIB_SIZE_FLOAT(n) \ |
---|
48 | ((mp_size_t) ((n) * 0.6942419 / GMP_NUMB_BITS + 1)) |
---|
49 | |
---|
50 | |
---|
51 | /* If the data put into the table by mpn/generic/fib_ui.c doesn't match the |
---|
52 | size setup by gmp-impl.h then there'll be zero entries at the end. Check |
---|
53 | that this hasn't happened. Any problem here should be blamed on the |
---|
54 | program at the end of mpn/generic/fib_ui.c, which generates the data. |
---|
55 | |
---|
56 | FIB_TABLE(0) is of course meant to be zero, so skip that. */ |
---|
57 | |
---|
58 | void |
---|
59 | check_fib_table (void) |
---|
60 | { |
---|
61 | int i; |
---|
62 | for (i = 1; i <= FIB_TABLE_LIMIT; i++) |
---|
63 | ASSERT_ALWAYS (FIB_TABLE(i) != 0); |
---|
64 | } |
---|
65 | |
---|
66 | |
---|
67 | int |
---|
68 | main (int argc, char *argv[]) |
---|
69 | { |
---|
70 | unsigned long n; |
---|
71 | unsigned long limit = 100 * BITS_PER_MP_LIMB; |
---|
72 | mpz_t want_fn, want_fn1, got_fn, got_fn1; |
---|
73 | |
---|
74 | tests_start (); |
---|
75 | mp_trace_base = -16; |
---|
76 | if (argc > 1 && argv[1][0] == 'x') |
---|
77 | limit = ULONG_MAX; |
---|
78 | else if (argc > 1) |
---|
79 | limit = atoi (argv[1]); |
---|
80 | |
---|
81 | check_fib_table (); |
---|
82 | |
---|
83 | /* start at n==0 */ |
---|
84 | mpz_init_set_ui (want_fn1, 1); /* F[-1] */ |
---|
85 | mpz_init_set_ui (want_fn, 0); /* F[0] */ |
---|
86 | mpz_init (got_fn); |
---|
87 | mpz_init (got_fn1); |
---|
88 | |
---|
89 | for (n = 0; n < limit; n++) |
---|
90 | { |
---|
91 | /* check our float formula seems right */ |
---|
92 | if (MPZ_FIB_SIZE_FLOAT (n) < SIZ(want_fn)) |
---|
93 | { |
---|
94 | printf ("MPZ_FIB_SIZE_FLOAT wrong at n=%lu\n", n); |
---|
95 | printf (" MPZ_FIB_SIZE_FLOAT %ld\n", MPZ_FIB_SIZE_FLOAT (n)); |
---|
96 | printf (" SIZ(want_fn) %d\n", SIZ(want_fn)); |
---|
97 | abort (); |
---|
98 | } |
---|
99 | |
---|
100 | /* check MPN_FIB2_SIZE seems right, compared to actual size and |
---|
101 | compared to our float formula */ |
---|
102 | if (MPN_FIB2_SIZE (n) < MPZ_FIB_SIZE_FLOAT (n)) |
---|
103 | { |
---|
104 | printf ("MPN_FIB2_SIZE wrong at n=%lu\n", n); |
---|
105 | printf (" MPN_FIB2_SIZE %ld\n", MPN_FIB2_SIZE (n)); |
---|
106 | printf (" MPZ_FIB_SIZE_FLOAT %ld\n", MPZ_FIB_SIZE_FLOAT (n)); |
---|
107 | abort (); |
---|
108 | } |
---|
109 | if (MPN_FIB2_SIZE (n) < SIZ(want_fn)) |
---|
110 | { |
---|
111 | printf ("MPN_FIB2_SIZE wrong at n=%lu\n", n); |
---|
112 | printf (" MPN_FIB2_SIZE %ld\n", MPN_FIB2_SIZE (n)); |
---|
113 | printf (" SIZ(want_fn) %d\n", SIZ(want_fn)); |
---|
114 | abort (); |
---|
115 | } |
---|
116 | |
---|
117 | mpz_fib2_ui (got_fn, got_fn1, n); |
---|
118 | MPZ_CHECK_FORMAT (got_fn); |
---|
119 | MPZ_CHECK_FORMAT (got_fn1); |
---|
120 | if (mpz_cmp (got_fn, want_fn) != 0 || mpz_cmp (got_fn1, want_fn1) != 0) |
---|
121 | { |
---|
122 | printf ("mpz_fib2_ui(%lu) wrong\n", n); |
---|
123 | mpz_trace ("want fn ", want_fn); |
---|
124 | mpz_trace ("got fn ", got_fn); |
---|
125 | mpz_trace ("want fn1", want_fn1); |
---|
126 | mpz_trace ("got fn1", got_fn1); |
---|
127 | abort (); |
---|
128 | } |
---|
129 | |
---|
130 | mpz_fib_ui (got_fn, n); |
---|
131 | MPZ_CHECK_FORMAT (got_fn); |
---|
132 | if (mpz_cmp (got_fn, want_fn) != 0) |
---|
133 | { |
---|
134 | printf ("mpz_fib_ui(%lu) wrong\n", n); |
---|
135 | mpz_trace ("want fn", want_fn); |
---|
136 | mpz_trace ("got fn", got_fn); |
---|
137 | abort (); |
---|
138 | } |
---|
139 | |
---|
140 | mpz_add (want_fn1, want_fn1, want_fn); /* F[n+1] = F[n] + F[n-1] */ |
---|
141 | mpz_swap (want_fn1, want_fn); |
---|
142 | } |
---|
143 | |
---|
144 | mpz_clear (want_fn); |
---|
145 | mpz_clear (want_fn1); |
---|
146 | mpz_clear (got_fn); |
---|
147 | mpz_clear (got_fn1); |
---|
148 | |
---|
149 | tests_end (); |
---|
150 | exit (0); |
---|
151 | } |
---|