1 /* Test mpz_lucnum_ui and mpz_lucnum2_ui.
2
3 Copyright 2001 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 #include <stdio.h>
21 #include <stdlib.h>
22 #include "gmp-impl.h"
23 #include "tests.h"
24
25
26 /* Usage: t-lucnum_ui [n]
27
28 Test up to L[n], or if n is omitted then the default limit below. A
29 literal "x" for the limit means continue forever, this being meant only
30 for development. */
31
32
33 void
check_sequence(int argc,char * argv[])34 check_sequence (int argc, char *argv[])
35 {
36 unsigned long n;
37 unsigned long limit = 100 * GMP_LIMB_BITS;
38 mpz_t want_ln, want_ln1, got_ln, got_ln1;
39
40 if (argc > 1 && argv[1][0] == 'x')
41 limit = ULONG_MAX;
42 else
43 TESTS_REPS (limit, argv, argc);
44
45 /* start at n==0 */
46 mpz_init_set_si (want_ln1, -1); /* L[-1] */
47 mpz_init_set_ui (want_ln, 2); /* L[0] */
48 mpz_init (got_ln);
49 mpz_init (got_ln1);
50
51 for (n = 0; n < limit; n++)
52 {
53 mpz_lucnum2_ui (got_ln, got_ln1, n);
54 MPZ_CHECK_FORMAT (got_ln);
55 MPZ_CHECK_FORMAT (got_ln1);
56 if (mpz_cmp (got_ln, want_ln) != 0 || mpz_cmp (got_ln1, want_ln1) != 0)
57 {
58 printf ("mpz_lucnum2_ui(%lu) wrong\n", n);
59 mpz_trace ("want ln ", want_ln);
60 mpz_trace ("got ln ", got_ln);
61 mpz_trace ("want ln1", want_ln1);
62 mpz_trace ("got ln1", got_ln1);
63 abort ();
64 }
65
66 mpz_lucnum_ui (got_ln, n);
67 MPZ_CHECK_FORMAT (got_ln);
68 if (mpz_cmp (got_ln, want_ln) != 0)
69 {
70 printf ("mpz_lucnum_ui(%lu) wrong\n", n);
71 mpz_trace ("want ln", want_ln);
72 mpz_trace ("got ln", got_ln);
73 abort ();
74 }
75
76 mpz_add (want_ln1, want_ln1, want_ln); /* L[n+1] = L[n] + L[n-1] */
77 mpz_swap (want_ln1, want_ln);
78 }
79
80 mpz_clear (want_ln);
81 mpz_clear (want_ln1);
82 mpz_clear (got_ln);
83 mpz_clear (got_ln1);
84 }
85
86 int
main(int argc,char * argv[])87 main (int argc, char *argv[])
88 {
89 tests_start ();
90 mp_trace_base = -16;
91
92 check_sequence (argc, argv);
93
94 tests_end ();
95 exit (0);
96 }
97