1 /*
2 
3 Copyright 2013 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 <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 
24 #include "testutils.h"
25 
26 #define MAX_WORDS 20
27 #define MAX_WORD_SIZE 10
28 
29 static void
dump_bytes(const char * label,const unsigned char * s,size_t n)30 dump_bytes (const char *label, const unsigned char *s, size_t n)
31 {
32   size_t i;
33   fprintf (stderr, "%s:", label);
34   for (i = 0; i < n; i++)
35     {
36       if (i && (i % 16) == 0)
37 	fprintf (stderr, "\n");
38       fprintf (stderr, " %02x", s[i]);
39     }
40   fprintf (stderr, "\n");
41 }
42 
43 /* Tests both mpz_import and mpz_export. */
44 void
testmain(int argc,char ** argv)45 testmain (int argc, char **argv)
46 {
47   unsigned char input[MAX_WORDS * MAX_WORD_SIZE];
48   unsigned char output[MAX_WORDS * MAX_WORD_SIZE + 2];
49   size_t count, in_count, out_count, size;
50   int endian, order;
51 
52   mpz_t a, res;
53 
54   mpz_init (a);
55   mpz_init (res);
56 
57   for (size = 0; size <= MAX_WORD_SIZE; size++)
58     for (count = 0; count <= MAX_WORDS; count++)
59       for (endian = -1; endian <= 1; endian++)
60 	for (order = -1; order <= 1; order += 2)
61 	  {
62 	    mini_rrandomb_export (a, input, &in_count,
63 				  order, size, endian, size*count * 8);
64 	    mpz_import (res, in_count, order, size, endian, 0, input);
65 	    if (mpz_cmp (a, res))
66 	      {
67 		fprintf (stderr, "mpz_import failed:\n"
68 			 "in_count %lu, out_count %lu, endian = %d, order = %d\n",
69 			 (unsigned long) in_count, (unsigned long) out_count, endian, order);
70 		dump ("a", a);
71 		dump ("res", res);
72 		abort ();
73 	      }
74 	    output[0] = 17;
75 	    output[1+in_count*size] = 17;
76 
77 	    mpz_export (output+1, &out_count, order, size, endian, 0, a);
78 	    if (out_count != in_count
79 		|| memcmp (output+1, input, in_count * size)
80 		|| output[0] != 17
81 		|| output[1+in_count*size] != 17)
82 	      {
83 		fprintf (stderr, "mpz_export failed:\n"
84 			 "in_count %lu, out_count %lu, endian = %d, order = %d\n",
85 			 (unsigned long) in_count, (unsigned long) out_count, endian, order);
86 		dump_bytes ("input", input, in_count * size);
87 		dump_bytes ("output", output+1, out_count * size);
88 		if (output[0] != 17)
89 		  fprintf (stderr, "Overwrite at -1, value %02x\n", output[0]);
90 		if (output[1+in_count*size] != 17)
91 		  fprintf (stderr, "Overwrite at %lu, value %02x\n",
92 			   (unsigned long) (in_count*size), output[1+in_count*size]);
93 
94 		abort ();
95 	      }
96 	  }
97   mpz_clear (a);
98   mpz_clear (res);
99 }
100