|
Packit |
5c3484 |
/*
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
Copyright 2013 Free Software Foundation, Inc.
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
This file is part of the GNU MP Library test suite.
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
The GNU MP Library test suite is free software; you can redistribute it
|
|
Packit |
5c3484 |
and/or modify it under the terms of the GNU General Public License as
|
|
Packit |
5c3484 |
published by the Free Software Foundation; either version 3 of the License,
|
|
Packit |
5c3484 |
or (at your option) any later version.
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
The GNU MP Library test suite is distributed in the hope that it will be
|
|
Packit |
5c3484 |
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
5c3484 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
Packit |
5c3484 |
Public License for more details.
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
You should have received a copy of the GNU General Public License along with
|
|
Packit |
5c3484 |
the GNU MP Library test suite. If not, see https://www.gnu.org/licenses/. */
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
#include <stdlib.h>
|
|
Packit |
5c3484 |
#include <stdio.h>
|
|
Packit |
5c3484 |
#include <string.h>
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
#include "testutils.h"
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
#define MAX_WORDS 20
|
|
Packit |
5c3484 |
#define MAX_WORD_SIZE 10
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
static void
|
|
Packit |
5c3484 |
dump_bytes (const char *label, const unsigned char *s, size_t n)
|
|
Packit |
5c3484 |
{
|
|
Packit |
5c3484 |
size_t i;
|
|
Packit |
5c3484 |
fprintf (stderr, "%s:", label);
|
|
Packit |
5c3484 |
for (i = 0; i < n; i++)
|
|
Packit |
5c3484 |
{
|
|
Packit |
5c3484 |
if (i && (i % 16) == 0)
|
|
Packit |
5c3484 |
fprintf (stderr, "\n");
|
|
Packit |
5c3484 |
fprintf (stderr, " %02x", s[i]);
|
|
Packit |
5c3484 |
}
|
|
Packit |
5c3484 |
fprintf (stderr, "\n");
|
|
Packit |
5c3484 |
}
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
/* Tests both mpz_import and mpz_export. */
|
|
Packit |
5c3484 |
void
|
|
Packit |
5c3484 |
testmain (int argc, char **argv)
|
|
Packit |
5c3484 |
{
|
|
Packit |
5c3484 |
unsigned char input[MAX_WORDS * MAX_WORD_SIZE];
|
|
Packit |
5c3484 |
unsigned char output[MAX_WORDS * MAX_WORD_SIZE + 2];
|
|
Packit |
5c3484 |
size_t count, in_count, out_count, size;
|
|
Packit |
5c3484 |
int endian, order;
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
mpz_t a, res;
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
mpz_init (a);
|
|
Packit |
5c3484 |
mpz_init (res);
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
for (size = 0; size <= MAX_WORD_SIZE; size++)
|
|
Packit |
5c3484 |
for (count = 0; count <= MAX_WORDS; count++)
|
|
Packit |
5c3484 |
for (endian = -1; endian <= 1; endian++)
|
|
Packit |
5c3484 |
for (order = -1; order <= 1; order += 2)
|
|
Packit |
5c3484 |
{
|
|
Packit |
5c3484 |
mini_rrandomb_export (a, input, &in_count,
|
|
Packit |
5c3484 |
order, size, endian, size*count * 8);
|
|
Packit |
5c3484 |
mpz_import (res, in_count, order, size, endian, 0, input);
|
|
Packit |
5c3484 |
if (mpz_cmp (a, res))
|
|
Packit |
5c3484 |
{
|
|
Packit |
5c3484 |
fprintf (stderr, "mpz_import failed:\n"
|
|
Packit |
5c3484 |
"in_count %lu, out_count %lu, endian = %d, order = %d\n",
|
|
Packit |
5c3484 |
(unsigned long) in_count, (unsigned long) out_count, endian, order);
|
|
Packit |
5c3484 |
dump ("a", a);
|
|
Packit |
5c3484 |
dump ("res", res);
|
|
Packit |
5c3484 |
abort ();
|
|
Packit |
5c3484 |
}
|
|
Packit |
5c3484 |
output[0] = 17;
|
|
Packit |
5c3484 |
output[1+in_count*size] = 17;
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
mpz_export (output+1, &out_count, order, size, endian, 0, a);
|
|
Packit |
5c3484 |
if (out_count != in_count
|
|
Packit |
5c3484 |
|| memcmp (output+1, input, in_count * size)
|
|
Packit |
5c3484 |
|| output[0] != 17
|
|
Packit |
5c3484 |
|| output[1+in_count*size] != 17)
|
|
Packit |
5c3484 |
{
|
|
Packit |
5c3484 |
fprintf (stderr, "mpz_export failed:\n"
|
|
Packit |
5c3484 |
"in_count %lu, out_count %lu, endian = %d, order = %d\n",
|
|
Packit |
5c3484 |
(unsigned long) in_count, (unsigned long) out_count, endian, order);
|
|
Packit |
5c3484 |
dump_bytes ("input", input, in_count * size);
|
|
Packit |
5c3484 |
dump_bytes ("output", output+1, out_count * size);
|
|
Packit |
5c3484 |
if (output[0] != 17)
|
|
Packit |
5c3484 |
fprintf (stderr, "Overwrite at -1, value %02x\n", output[0]);
|
|
Packit |
5c3484 |
if (output[1+in_count*size] != 17)
|
|
Packit |
5c3484 |
fprintf (stderr, "Overwrite at %lu, value %02x\n",
|
|
Packit |
5c3484 |
(unsigned long) (in_count*size), output[1+in_count*size]);
|
|
Packit |
5c3484 |
|
|
Packit |
5c3484 |
abort ();
|
|
Packit |
5c3484 |
}
|
|
Packit |
5c3484 |
}
|
|
Packit |
5c3484 |
mpz_clear (a);
|
|
Packit |
5c3484 |
mpz_clear (res);
|
|
Packit |
5c3484 |
}
|