github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/tests/mpz/t-import.c (about)

     1  /* Test mpz_import.
     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.h"
    24  #include "gmp-impl.h"
    25  #include "tests.h"
    26  
    27  
    28  void
    29  check_data (void)
    30  {
    31    static const struct {
    32      const char  *want;
    33      size_t      count;
    34      int         order;
    35      size_t      size;
    36      int         endian;
    37      int         nail;
    38      char        src[64];
    39  
    40    } data[] = {
    41  
    42      { "0", 0,1, 1,1, 0 },
    43      { "0", 1,1, 0,1, 0 },
    44  
    45      { "0x12345678", 4,1,  1,1, 0, { '\22', '\64', '\126', '\170' } },
    46      { "0x12345678", 1,1,  4,1, 0, { '\22', '\64', '\126', '\170' } },
    47      { "0x12345678", 1,-1, 4,1, 0, { '\22', '\64', '\126', '\170' } },
    48  
    49      { "0x12345678", 4,-1, 1,-1, 0, { '\170', '\126', '\064', '\22' } },
    50      { "0x12345678", 1,1,  4,-1, 0, { '\170', '\126', '\064', '\22' } },
    51      { "0x12345678", 1,-1, 4,-1, 0, { '\170', '\126', '\064', '\22' } },
    52  
    53      { "0",    5,1,  1,1, 7, { '\376', '\376', '\376', '\376', '\376' } },
    54      { "0",    5,-1, 1,1, 7, { '\376', '\376', '\376', '\376', '\376' } },
    55      { "0x15", 5,1,  1,1, 7, { '\377', '\376', '\377', '\376', '\377' } },
    56  
    57      { "0",    3,1,  2,1,   1, { '\200','\000', '\200','\000', '\200','\000' }},
    58      { "0",    3,1,  2,-1,  1, { '\000','\200', '\000','\200', '\000','\200' }},
    59      { "0",    3,1,  2,1,  15, { '\377','\376', '\377','\376', '\377','\376' }},
    60  
    61      { "0x2A", 3,1,  2,1, 14, { '\377','\376', '\377','\376', '\377','\376' } },
    62      { "0x06", 3,1,  2,1, 14, { '\377','\374', '\377','\375', '\377','\376' } },
    63      { "0x24", 3,-1, 2,1, 14, { '\377','\374', '\377','\375', '\377','\376' } },
    64  
    65      { "0x123456789ABC", 3,1,  2,1,  0, {
    66          '\022','\064', '\126','\170', '\232','\274' } },
    67      { "0x123456789ABC", 3,-1, 2,1,  0, {
    68          '\232','\274', '\126','\170', '\022','\064' } },
    69      { "0x123456789ABC", 3,1,  2,-1, 0, {
    70          '\064','\022', '\170','\126', '\274','\232' } },
    71      { "0x123456789ABC", 3,-1, 2,-1, 0, {
    72          '\274','\232', '\170','\126', '\064','\022' } },
    73  
    74      { "0x112233445566778899AABBCC", 3,1,  4,1,  0,
    75        { '\021','\042','\063','\104',
    76          '\125','\146','\167','\210',
    77          '\231','\252','\273','\314' } },
    78      { "0x112233445566778899AABBCC", 3,-1, 4,1,  0,
    79        { '\231','\252','\273','\314',
    80          '\125','\146','\167','\210',
    81          '\021','\042','\063','\104' } },
    82      { "0x112233445566778899AABBCC", 3,1,  4,-1, 0,
    83        { '\104','\063','\042','\021',
    84          '\210','\167','\146','\125',
    85          '\314','\273','\252','\231' } },
    86      { "0x112233445566778899AABBCC", 3,-1, 4,-1, 0,
    87        { '\314','\273','\252','\231',
    88          '\210','\167','\146','\125',
    89          '\104','\063','\042','\021' } },
    90  
    91      { "0x100120023003400450056006700780089009A00AB00BC00C", 3,1,  8,1,  0,
    92        { '\020','\001','\040','\002','\060','\003','\100','\004',
    93          '\120','\005','\140','\006','\160','\007','\200','\010',
    94          '\220','\011','\240','\012','\260','\013','\300','\014' } },
    95      { "0x100120023003400450056006700780089009A00AB00BC00C", 3,-1, 8,1,  0,
    96        { '\220','\011','\240','\012','\260','\013','\300','\014',
    97          '\120','\005','\140','\006','\160','\007','\200','\010',
    98          '\020','\001','\040','\002','\060','\003','\100','\004' } },
    99      { "0x100120023003400450056006700780089009A00AB00BC00C", 3,1,  8,-1, 0,
   100        { '\004','\100','\003','\060','\002','\040','\001','\020',
   101          '\010','\200','\007','\160','\006','\140','\005','\120',
   102          '\014','\300','\013','\260','\012','\240','\011','\220' } },
   103      { "0x100120023003400450056006700780089009A00AB00BC00C", 3,-1, 8,-1, 0,
   104        { '\014','\300','\013','\260','\012','\240','\011','\220',
   105          '\010','\200','\007','\160','\006','\140','\005','\120',
   106          '\004','\100','\003','\060','\002','\040','\001','\020' } },
   107  
   108      { "0x155555555555555555555555", 3,1,  4,1,  1,
   109        { '\325','\125','\125','\125',
   110          '\252','\252','\252','\252',
   111          '\325','\125','\125','\125' } },
   112      { "0x155555555555555555555555", 3,-1,  4,1,  1,
   113        { '\325','\125','\125','\125',
   114          '\252','\252','\252','\252',
   115          '\325','\125','\125','\125' } },
   116      { "0x155555555555555555555555", 3,1,  4,-1,  1,
   117        { '\125','\125','\125','\325',
   118          '\252','\252','\252','\252',
   119          '\125','\125','\125','\325' } },
   120      { "0x155555555555555555555555", 3,-1,  4,-1,  1,
   121        { '\125','\125','\125','\325',
   122          '\252','\252','\252','\252',
   123          '\125','\125','\125','\325' } },
   124    };
   125  
   126    char    buf[sizeof(data[0].src) + sizeof (mp_limb_t)];
   127    char    *src;
   128    size_t  align;
   129    int     i;
   130    mpz_t   got, want;
   131  
   132    mpz_init (got);
   133    mpz_init (want);
   134  
   135    for (i = 0; i < numberof (data); i++)
   136      {
   137        for (align = 0; align < sizeof (mp_limb_t); align++)
   138          {
   139            mpz_set_str_or_abort (want, data[i].want, 0);
   140            src = buf + align;
   141            memcpy (src, data[i].src, data[i].count * data[i].size);
   142  
   143            mpz_set_ui (got, 0L);
   144            mpz_import (got, data[i].count, data[i].order,
   145                        data[i].size, data[i].endian, data[i].nail, src);
   146  
   147            MPZ_CHECK_FORMAT (got);
   148            if (mpz_cmp (got, want) != 0)
   149              {
   150                printf ("wrong at data[%d]\n", i);
   151                printf ("    count=%lu order=%d  size=%lu endian=%d nail=%u  align=%lu\n",
   152                        (unsigned long) data[i].count, data[i].order,
   153                        (unsigned long) data[i].size, data[i].endian, data[i].nail,
   154                        (unsigned long) align);
   155                mpz_trace ("    got ", got);
   156                mpz_trace ("    want", want);
   157                abort ();
   158              }
   159          }
   160      }
   161    mpz_clear (got);
   162    mpz_clear (want);
   163  }
   164  
   165  
   166  int
   167  main (void)
   168  {
   169    tests_start ();
   170  
   171    mp_trace_base = -16;
   172    check_data ();
   173  
   174    tests_end ();
   175    exit (0);
   176  }