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