blob: 22ad57593081020f78dc9cc5cc82a5784f55671d [file] [log] [blame]
Austin Schuhdace2a62020-08-18 10:56:48 -07001/* Tests the (internal) function mpz_lucas_mod
2
3Copyright 2018, Free Software Foundation, Inc.
4
5This file is part of the GNU MP Library test suite.
6
7The GNU MP Library test suite is free software; you can redistribute it
8and/or modify it under the terms of the GNU General Public License as
9published by the Free Software Foundation; either version 3 of the License,
10or (at your option) any later version.
11
12The GNU MP Library test suite is distributed in the hope that it will be
13useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
15Public License for more details.
16
17You should have received a copy of the GNU General Public License along with
18the GNU MP Library test suite. If not, see https://www.gnu.org/licenses/. */
19
20#include <limits.h>
21#include <stdlib.h>
22#include <stdio.h>
23#include <string.h>
24
25#include "testutils.h"
26
27#define MAXBITS 100
28#define COUNT 1000
29
30void
31testmain (int argc, char **argv)
32{
33 unsigned i;
34 mpz_t m, vr, qr, vm, qm, vt;
35 int resm, resr;
36 long Q;
37 unsigned long b0;
38
39 mpz_init (m);
40 mpz_init (vr);
41 mpz_init (qr);
42 mpz_init (vm);
43 mpz_init (qm);
44 mpz_init (vt);
45
46 for (i = 0; i < COUNT; i++)
47 {
48 mini_random_lucm_op (MAXBITS, vr, qr, m, &Q, &b0, &resr);
49 if (b0 == 0)
50 {
51 fprintf (stderr, "lucas_mod: test disabled (%u tests done).\n", i);
52 break;
53 }
54 resm = mpz_lucas_mod (vm, qm, Q, b0, m);
55
56 if (resr != resm)
57 {
58 if (resm != 0 || mpz_cmp_ui (vm, 0) != 0)
59 {
60 fprintf (stderr, "mpz_lucas_mod wrong return value (%d != %d):\n", resr, resm);
61 fprintf (stderr, "Q = %ld , b0 = %lu\n", Q, b0);
62 dump ("m", m);
63 dump ("vm", vm);
64 dump ("qm", qm);
65 abort ();
66 }
67 }
68 else if (resm == 0)
69 {
70 mpz_abs (vr, vr);
71 mpz_sub (vt, m, vr);
72 mpz_abs (vm, vm);
73 mpz_mod (qm, qm, m);
74 if (mpz_cmp_ui (qr, 0) < 0)
75 mpz_add (qr, qr, m);
76 if (mpz_cmp (qm, qr) != 0 ||
77 (mpz_cmp (vm, vr) != 0 && mpz_cmp (vm, vt) != 0))
78 {
79 fprintf (stderr, "mpz_lucas_mod error:\n");
80 fprintf (stderr, "Q = %ld , b0 = %lu\n", Q, b0);
81 dump ("m", m);
82 dump ("vm", vm);
83 dump ("vr", vr);
84 dump ("vt", vt);
85 dump ("qm", qm);
86 dump ("qr", qr);
87 abort ();
88 }
89
90 }
91 }
92 mpz_clear (m);
93 mpz_clear (vr);
94 mpz_clear (qr);
95 mpz_clear (vm);
96 mpz_clear (qm);
97 mpz_clear (vt);
98}