Home | History | Annotate | Line # | Download | only in tests
t-import.c revision 1.1.1.1
      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 http://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
     30 dump (const char *label, const mpz_t x)
     31 {
     32   char *buf = mpz_get_str (NULL, 16, x);
     33   fprintf (stderr, "%s: %s\n", label, buf);
     34   testfree (buf);
     35 }
     36 
     37 static void
     38 dump_bytes (const char *label, const unsigned char *s, size_t n)
     39 {
     40   size_t i;
     41   fprintf (stderr, "%s:", label);
     42   for (i = 0; i < n; i++)
     43     {
     44       if (i && (i % 16) == 0)
     45 	fprintf (stderr, "\n");
     46       fprintf (stderr, " %02x", s[i]);
     47     }
     48   fprintf (stderr, "\n");
     49 }
     50 
     51 /* Tests both mpz_import and mpz_export. */
     52 void
     53 testmain (int argc, char **argv)
     54 {
     55   unsigned char input[MAX_WORDS * MAX_WORD_SIZE];
     56   unsigned char output[MAX_WORDS * MAX_WORD_SIZE + 2];
     57   size_t count, in_count, out_count, size;
     58   int endian, order;
     59 
     60   mpz_t a, res;
     61 
     62   mpz_init (a);
     63   mpz_init (res);
     64 
     65   for (size = 0; size <= MAX_WORD_SIZE; size++)
     66     for (count = 0; count <= MAX_WORDS; count++)
     67       for (endian = -1; endian <= 1; endian++)
     68 	for (order = -1; order <= 1; order += 2)
     69 	  {
     70 	    mini_rrandomb_export (a, input, &in_count,
     71 				  order, size, endian, size*count * 8);
     72 	    mpz_import (res, in_count, order, size, endian, 0, input);
     73 	    if (mpz_cmp (a, res))
     74 	      {
     75 		fprintf (stderr, "mpz_import failed:\n"
     76 			 "in_count %lu, out_count %lu, endian = %d, order = %d\n",
     77 			 (unsigned long) in_count, (unsigned long) out_count, endian, order);
     78 		dump ("a", a);
     79 		dump ("res", res);
     80 		abort ();
     81 	      }
     82 	    output[0] = 17;
     83 	    output[1+in_count*size] = 17;
     84 
     85 	    mpz_export (output+1, &out_count, order, size, endian, 0, a);
     86 	    if (out_count != in_count
     87 		|| memcmp (output+1, input, in_count * size)
     88 		|| output[0] != 17
     89 		|| output[1+in_count*size] != 17)
     90 	      {
     91 		fprintf (stderr, "mpz_export failed:\n"
     92 			 "in_count %lu, out_count %lu, endian = %d, order = %d\n",
     93 			 (unsigned long) in_count, (unsigned long) out_count, endian, order);
     94 		dump_bytes ("input", input, in_count * size);
     95 		dump_bytes ("output", output+1, out_count * size);
     96 		if (output[0] != 17)
     97 		  fprintf (stderr, "Overwrite at -1, value %02x\n", output[0]);
     98 		if (output[1+in_count*size] != 17)
     99 		  fprintf (stderr, "Overwrite at %lu, value %02x\n",
    100 			   (unsigned long) (in_count*size), output[1+in_count*size]);
    101 
    102 		abort ();
    103 	      }
    104 	  }
    105   mpz_clear (a);
    106   mpz_clear (res);
    107 }
    108