blob: 27d87caf0e87008b6823280d8bcad5624c9f2968 [file] [log] [blame]
Austin Schuhdace2a62020-08-18 10:56:48 -07001/*
2
3Copyright 2012, 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
24#include "testutils.h"
25
26#define MAXBITS 400
27#define COUNT 10000
28
29void
30testmain (int argc, char **argv)
31{
32 unsigned i;
33 mpz_t a, res, ref;
34 mp_bitcnt_t b;
35
36 mpz_init (a);
37 mpz_init (res);
38 mpz_init (ref);
39
40 for (i = 0; i < COUNT; i++)
41 {
42 mini_random_bit_op (OP_SETBIT, MAXBITS, a, &b, ref);
43 mpz_set (res, a);
44 mpz_setbit (res, b);
45 if (mpz_cmp (res, ref))
46 {
47 fprintf (stderr, "mpz_setbit failed:\n");
48 dump ("a", a);
49 fprintf (stderr, "b: %lu\n", b);
50 dump ("r", res);
51 dump ("ref", ref);
52 abort ();
53 }
54 if (!mpz_tstbit (res, b))
55 {
56 fprintf (stderr, "mpz_tstbit failed (after mpz_setbit):\n");
57 dump ("res", a);
58 fprintf (stderr, "b: %lu\n", b);
59 abort ();
60 }
61 mini_random_bit_op (OP_CLRBIT, MAXBITS, a, &b, ref);
62 mpz_set (res, a);
63 mpz_clrbit (res, b);
64 if (mpz_cmp (res, ref))
65 {
66 fprintf (stderr, "mpz_clrbit failed:\n");
67 dump ("a", a);
68 fprintf (stderr, "b: %lu\n", b);
69 dump ("r", res);
70 dump ("ref", ref);
71 abort ();
72 }
73 if (mpz_tstbit (res, b))
74 {
75 fprintf (stderr, "mpz_tstbit failed (after mpz_clrbit):\n");
76 dump ("res", a);
77 fprintf (stderr, "b: %lu\n", b);
78 abort ();
79 }
80 mini_random_bit_op (OP_COMBIT, MAXBITS, a, &b, ref);
81 mpz_set (res, a);
82 mpz_com (a, a);
83 mpz_combit (res, b);
84 if (mpz_cmp (res, ref))
85 {
86 fprintf (stderr, "mpz_combit failed:\n");
87 dump ("a", a);
88 fprintf (stderr, "b: %lu\n", b);
89 dump ("r", res);
90 dump ("ref", ref);
91 abort ();
92 }
93 if (mpz_tstbit (res, b) != mpz_tstbit (a, b))
94 {
95 fprintf (stderr, "mpz_tstbit failed (after mpz_combit):\n");
96 dump ("res", a);
97 fprintf (stderr, "b: %lu\n", b);
98 abort ();
99 }
100 }
101 mpz_clear (a);
102 mpz_clear (res);
103 mpz_clear (ref);
104}