(root)/
gmp-6.3.0/
tests/
mpz/
t-export.c
       1  /* Test mpz_export.
       2  
       3  Copyright 2002, 2003 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 <stdio.h>
      21  #include <stdlib.h>
      22  #include <string.h>
      23  #include "gmp-impl.h"
      24  #include "tests.h"
      25  
      26  
      27  void
      28  check_data (void)
      29  {
      30    static const struct {
      31      const char  *src;
      32      size_t      want_count;
      33      int         order;
      34      size_t      size;
      35      int         endian;
      36      int         nail;
      37      char        want_data[64];
      38  
      39    } data[] = {
      40  
      41      { "0", 0,1, 1,1, 0 },
      42      { "0", 0,1, 2,1, 0 },
      43      { "0", 0,1, 3,1, 0 },
      44  
      45      { "0x12345678", 4,1,  1,1, 0, { '\022', '\064', '\126', '\170' } },
      46      { "0x12345678", 1,1,  4,1, 0, { '\022', '\064', '\126', '\170' } },
      47      { "0x12345678", 1,-1, 4,1, 0, { '\022', '\064', '\126', '\170' } },
      48  
      49      { "0x12345678", 4,-1, 1,-1, 0, { '\170', '\126', '\064', '\022' } },
      50      { "0x12345678", 1,1,  4,-1, 0, { '\170', '\126', '\064', '\022' } },
      51      { "0x12345678", 1,-1, 4,-1, 0, { '\170', '\126', '\064', '\022' } },
      52  
      53      { "0x15", 5,1,  1,1, 7, { '\001', '\000', '\001', '\000', '\001' } },
      54  
      55      { "0x1FFFFFFFFFFF", 3,1,  2,1,   1, {
      56  	'\177','\377', '\177','\377', '\177','\377' } },
      57      { "0x1FFFFFFFFFFF", 3,1,  2,-1,  1, {
      58  	'\377','\177', '\377','\177', '\377','\177' } },
      59      { "0x7",            3,1,  2,1,  15, {
      60  	'\000','\001', '\000','\001', '\000','\001' } },
      61      { "0x7",            3,1,  2,-1, 15, {
      62  	'\001','\000', '\001','\000', '\001','\000' } },
      63  
      64      { "0x24", 3,1,  2,1,  14, { '\000','\002', '\000','\001', '\000','\000' }},
      65      { "0x24", 3,1,  2,-1, 14, { '\002','\000', '\001','\000', '\000','\000' }},
      66      { "0x24", 3,-1, 2,-1, 14, { '\000','\000', '\001','\000', '\002','\000' }},
      67      { "0x24", 3,-1, 2,1,  14, { '\000','\000', '\000','\001', '\000','\002' }},
      68  
      69      { "0x123456789ABC", 3,1,  2,1,  0, {
      70  	'\022','\064', '\126','\170', '\232','\274' } },
      71      { "0x123456789ABC", 3,-1, 2,1,  0, {
      72  	'\232','\274', '\126','\170', '\022','\064' } },
      73      { "0x123456789ABC", 3,1,  2,-1, 0, {
      74  	'\064','\022', '\170','\126', '\274','\232' } },
      75      { "0x123456789ABC", 3,-1, 2,-1, 0, {
      76  	'\274','\232', '\170','\126', '\064','\022' } },
      77  
      78      { "0x112233445566778899AABBCC", 3,1,  4,1,  0,
      79        { '\021','\042','\063','\104',
      80  	'\125','\146','\167','\210',
      81  	'\231','\252','\273','\314' } },
      82      { "0x112233445566778899AABBCC", 3,-1, 4,1,  0,
      83        { '\231','\252','\273','\314',
      84  	'\125','\146','\167','\210',
      85  	'\021','\042','\063','\104' } },
      86      { "0x112233445566778899AABBCC", 3,1,  4,-1, 0,
      87        { '\104','\063','\042','\021',
      88  	'\210','\167','\146','\125',
      89  	'\314','\273','\252','\231' } },
      90      { "0x112233445566778899AABBCC", 3,-1, 4,-1, 0,
      91        { '\314','\273','\252','\231',
      92  	'\210','\167','\146','\125',
      93  	'\104','\063','\042','\021' } },
      94  
      95      { "0x100120023003400450056006700780089009A00AB00BC00C", 3,1,  8,1,  0,
      96        { '\020','\001','\040','\002','\060','\003','\100','\004',
      97  	'\120','\005','\140','\006','\160','\007','\200','\010',
      98  	'\220','\011','\240','\012','\260','\013','\300','\014' } },
      99      { "0x100120023003400450056006700780089009A00AB00BC00C", 3,-1, 8,1,  0,
     100        { '\220','\011','\240','\012','\260','\013','\300','\014',
     101  	'\120','\005','\140','\006','\160','\007','\200','\010',
     102  	'\020','\001','\040','\002','\060','\003','\100','\004' } },
     103      { "0x100120023003400450056006700780089009A00AB00BC00C", 3,1,  8,-1, 0,
     104        { '\004','\100','\003','\060','\002','\040','\001','\020',
     105  	'\010','\200','\007','\160','\006','\140','\005','\120',
     106  	'\014','\300','\013','\260','\012','\240','\011','\220' } },
     107      { "0x100120023003400450056006700780089009A00AB00BC00C", 3,-1, 8,-1, 0,
     108        { '\014','\300','\013','\260','\012','\240','\011','\220',
     109  	'\010','\200','\007','\160','\006','\140','\005','\120',
     110  	'\004','\100','\003','\060','\002','\040','\001','\020' } },
     111  
     112      { "0x155555555555555555555555", 3,1,  4,1,  1,
     113        { '\125','\125','\125','\125',
     114  	'\052','\252','\252','\252',
     115  	'\125','\125','\125','\125' } },
     116      { "0x155555555555555555555555", 3,-1,  4,1,  1,
     117        { '\125','\125','\125','\125',
     118  	'\052','\252','\252','\252',
     119  	'\125','\125','\125','\125' } },
     120      { "0x155555555555555555555555", 3,1,  4,-1,  1,
     121        { '\125','\125','\125','\125',
     122  	'\252','\252','\252','\052',
     123  	'\125','\125','\125','\125' } },
     124      { "0x155555555555555555555555", 3,-1,  4,-1,  1,
     125        { '\125','\125','\125','\125',
     126  	'\252','\252','\252','\052',
     127  	'\125','\125','\125','\125' } },
     128    };
     129  
     130    char    buf[sizeof(data[0].src) + sizeof (mp_limb_t) + 128];
     131    char    *got_data;
     132    void    *ret;
     133    size_t  align, got_count, j;
     134    int     i, error = 0;
     135    mpz_t   src;
     136  
     137    mpz_init (src);
     138  
     139    for (i = 0; i < numberof (data); i++)
     140      {
     141        for (align = 0; align < sizeof (mp_limb_t); align++)
     142  	{
     143  	  mpz_set_str_or_abort (src, data[i].src, 0);
     144  	  MPZ_CHECK_FORMAT (src);
     145  	  got_data = buf + align;
     146  
     147  	  ASSERT_ALWAYS (data[i].want_count * data[i].size + align
     148  			 <= sizeof (buf));
     149  
     150  	  memset (got_data, '\0', data[i].want_count * data[i].size);
     151  	  ret = mpz_export (got_data, &got_count, data[i].order,
     152  			    data[i].size, data[i].endian, data[i].nail, src);
     153  
     154  	  if (ret != got_data)
     155  	    {
     156  	      printf ("return doesn't equal given pointer\n");
     157  	      error = 1;
     158  	    }
     159  	  if (got_count != data[i].want_count)
     160  	    {
     161  	      printf ("wrong count\n");
     162  	      error = 1;
     163  	    }
     164  	  if (memcmp (got_data, data[i].want_data, got_count * data[i].size) != 0)
     165  	    {
     166  	      printf ("wrong result data\n");
     167  	      error = 1;
     168  	    }
     169  	  if (error)
     170  	    {
     171  	      printf ("    at data[%d]  align=%d\n", i, (int) align);
     172  	      printf ("    src \"%s\"\n", data[i].src);
     173  	      mpz_trace ("    src", src);
     174  	      printf ("    order=%d  size=%lu endian=%d nail=%u\n",
     175  		      data[i].order,
     176  		      (unsigned long) data[i].size, data[i].endian, data[i].nail);
     177  	      printf ("    want count %lu\n", (unsigned long) data[i].want_count);
     178  	      printf ("    got count  %lu\n", (unsigned long) got_count);
     179  	      printf ("    want");
     180  	      for (j = 0; j < data[i].want_count*data[i].size; j++)
     181  		printf (" 0x%02X,", (unsigned) (unsigned char) data[i].want_data[j]);
     182  	      printf ("\n");
     183  	      printf ("    got ");
     184  	      for (j = 0; j < got_count*data[i].size; j++)
     185  		printf (" 0x%02X,", (unsigned) (unsigned char) got_data[j]);
     186  	      printf ("\n");
     187  	      abort ();
     188  	    }
     189  	}
     190      }
     191    mpz_clear (src);
     192  }
     193  
     194  
     195  int
     196  main (void)
     197  {
     198    tests_start ();
     199  
     200    mp_trace_base = -16;
     201    check_data ();
     202  
     203    tests_end ();
     204    exit (0);
     205  }