aboutsummaryrefslogtreecommitdiff
path: root/vendor/gmp-6.3.0/mini-gmp/tests/testutils.c
blob: 46e368a914035d8f70f4bea6b04822eaf9bf2150 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*

Copyright 2013-2015, 2018 Free Software Foundation, Inc.

This file is part of the GNU MP Library test suite.

The GNU MP Library test suite is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 3 of the License,
or (at your option) any later version.

The GNU MP Library test suite is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
Public License for more details.

You should have received a copy of the GNU General Public License along with
the GNU MP Library test suite.  If not, see https://www.gnu.org/licenses/.  */

#include "testutils.h"

/* Include it here, so we we could tweak, e.g., how MPZ_REALLOC
   works. */
#include "../mini-gmp.c"
#include "../mini-mpq.c"

static size_t total_alloc = 0;

/* Custom memory allocation to track memory usage, and add a small red
   zone.

   About alignment: In general, getting a block from malloc, and
   incrementing it by sizeof(size_t), like we do here, might give a
   pointer which is not properly aligned for all types. But the
   largest type we allocate space for is unsigned long (mp_limb_t),
   which shouldn't have stricter alignment requirements than
   size_t. */

static unsigned char block_end[8] =
  { 0x7c, 0x37, 0xd6, 0x12, 0xa8, 0x6c, 0x01, 0xd1 };

static void *
block_init (size_t *block, size_t size)
{
  char *p;
  *block++ = size;

  p = (char *) block;
  memcpy (p + size, block_end, sizeof(block_end));

  total_alloc += size;
  return p;
}

/* Check small redzone, return pointer to malloced block. */
static size_t *
block_check  (void *p)
{
  size_t *block = (size_t *) p - 1;
  size_t size = block[0];

  if (memcmp ((char *)p + size, block_end, sizeof(block_end)) != 0)
    {
      fprintf (stderr, "red zone overwritten.\n");
      abort ();
    }
  total_alloc -= size;
  return block;
}

static void *
tu_alloc (size_t size)
{
  size_t *block = (size_t *) malloc (sizeof(size_t) + size + sizeof(block_end));
  if (!block)
    {
      fprintf (stderr, "Virtual memory exhausted.\n");
      abort ();
    }

  return block_init (block, size);
}

static void *
tu_realloc (void *p, size_t old_size, size_t new_size)
{
  size_t *block;
  size_t *old_block = block_check (p);
  if (old_block[0] != old_size)
    {
      fprintf (stderr, "%s:%d: bad old_size: want %ld, got %ld.\n", __FILE__, __LINE__,
                 (long)old_block[0], (long)old_size);
      abort ();
    }

  block = (size_t *) realloc (old_block, sizeof(size_t) + new_size + sizeof(block_end));
  if (!block)
    {
      fprintf (stderr, "Virtual memory exhausted.\n");
      abort ();
    }

  return block_init (block, new_size);
}

static void
tu_free (void *p, size_t old_size)
{
  size_t *old_block = block_check (p);
  if (old_block[0] != old_size && old_size != 0)
    {
      fprintf (stderr, "%s:%d: bad old_size: want %ld, got %ld.\n", __FILE__, __LINE__,
                 (long)old_block[0], (long)old_size);
      abort ();
    }
  free (old_block);
}

/* Free memory allocated via mini-gmp allocation function. */
void
testfree (void *p, size_t size)
{
  void (*freefunc) (void *, size_t);
  mp_get_memory_functions (NULL, NULL, &freefunc);

  freefunc (p, size);
}

int
main (int argc, char **argv)
{
  hex_random_init ();

  mp_set_memory_functions (tu_alloc, tu_realloc, tu_free);

  /* Currently, t-comb seems to be the only program accepting any
     arguments. It might make sense to parse common arguments here. */
  testmain (argc, argv);

  if (total_alloc != 0)
    {
      fprintf (stderr, "Memory leaked: %lu bytes.\n",
	       (unsigned long) total_alloc);
      abort ();
    }
  return 0;
}

void
testhalves (int count, void (*tested_fun) (int))
{
  void (*freefunc) (void *, size_t);
  void *(*reallocfunc) (void *, size_t, size_t);
  void *(*allocfunc) (size_t);
  size_t initial_alloc;

  mp_get_memory_functions (&allocfunc, &reallocfunc, &freefunc);
  initial_alloc = total_alloc;
  (*tested_fun) (count / 2);
  if (initial_alloc != total_alloc)
    {
      fprintf (stderr, "First half, memory leaked: %lu bytes.\n",
	       (unsigned long) total_alloc - initial_alloc);
      abort ();
    }
  mp_set_memory_functions (NULL, NULL, NULL);
  (*tested_fun) (count / 2);
  mp_set_memory_functions (allocfunc, reallocfunc, freefunc);
}

void
dump (const char *label, const mpz_t x)
{
  char *buf = mpz_get_str (NULL, 16, x);
  fprintf (stderr, "%s: %s\n", label, buf);
  testfree (buf, strlen(buf) + 1);
}

void
mpz_set_str_or_abort (mpz_ptr z, const char *str, int base)
{
  if (mpz_set_str (z, str, base) != 0)
    {
      fprintf (stderr, "ERROR: mpz_set_str failed\n");
      fprintf (stderr, "   str  = \"%s\"\n", str);
      fprintf (stderr, "   base = %d\n", base);
      abort();
    }
}

int
mpz_lucas_mod (mpz_t V, mpz_t Qk, long Q,
	       mp_bitcnt_t b0, const mpz_t n)
{
  return gmp_lucas_mod (V, Qk, Q, b0, n);
}