(root)/
gmp-6.3.0/
tests/
mpf/
t-inp_str.c
       1  /* Test mpf_inp_str.
       2  
       3  Copyright 2001, 2002 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 "config.h"
      21  
      22  #include <stdio.h>
      23  #include <stdlib.h>
      24  #include <string.h>
      25  #if HAVE_UNISTD_H
      26  #include <unistd.h>		/* for unlink */
      27  #endif
      28  
      29  #include "gmp-impl.h"
      30  #include "tests.h"
      31  
      32  
      33  #define FILENAME  "t-inp_str.tmp"
      34  
      35  
      36  void
      37  check_data (void)
      38  {
      39    static const struct {
      40      const char  *inp;
      41      int         base;
      42      const char  *want;
      43      int         want_nread;
      44  
      45    } data[] = {
      46  
      47      { "0",   10, "0", 1 },
      48  
      49      { "abc", 10, "0", 0 },
      50      { "ghi", 16, "0", 0 },
      51  
      52      { "125",    10, "125",  3 },
      53      { "125e1",  10, "1250", 5 },
      54      { "12e+2",  10, "1200", 5 },
      55      { "125e-1", 10, "12.5", 6 },
      56  
      57      {  "ff", 16,  "255", 2 },
      58      { "-ff", 16, "-255", 3 },
      59      {  "FF", 16,  "255", 2 },
      60      { "-FF", 16, "-255", 3 },
      61  
      62      { "100",     16, "256",  3 },
      63      { "100@1",   16, "4096", 5 },
      64      { "100@10",  16, "4722366482869645213696", 6 },
      65      { "100@10", -16, "281474976710656",        6 },
      66      { "100@-1",  16, "16",   6 },
      67      { "10000000000000000@-10",  16, "1", 21 },
      68      { "10000000000@-10",       -16, "1", 15 },
      69  
      70      { "z", 36, "35", 1 },
      71      { "Z", 36, "35", 1 },
      72      { "z@1", 36, "1260", 3 },
      73      { "Z@1", 36, "1260", 3 },
      74  
      75      {  "0",      0,   "0", 1 },
      76    };
      77  
      78    mpf_t  got, want;
      79    long   ftell_nread;
      80    int    i, pre, post, j, got_nread, want_nread;
      81    FILE   *fp;
      82  
      83    mpf_init (got);
      84    mpf_init (want);
      85  
      86    for (i = 0; i < numberof (data); i++)
      87      {
      88        for (pre = 0; pre <= 3; pre++)
      89          {
      90            for (post = 0; post <= 2; post++)
      91              {
      92                mpf_set_str_or_abort (want, data[i].want, 10);
      93                MPF_CHECK_FORMAT (want);
      94  
      95                /* create the file new each time to ensure its length is what
      96                   we want */
      97                fp = fopen (FILENAME, "w+");
      98                ASSERT_ALWAYS (fp != NULL);
      99                for (j = 0; j < pre; j++)
     100                  putc (' ', fp);
     101                fputs (data[i].inp, fp);
     102                for (j = 0; j < post; j++)
     103                  putc (' ', fp);
     104                fflush (fp);
     105                ASSERT_ALWAYS (! ferror(fp));
     106  
     107                rewind (fp);
     108                got_nread = mpf_inp_str (got, fp, data[i].base);
     109  
     110                if (got_nread != 0)
     111                  {
     112                    ftell_nread = ftell (fp);
     113                    if (got_nread != ftell_nread)
     114                      {
     115                        printf ("mpf_inp_str nread wrong\n");
     116                        printf ("  inp          \"%s\"\n", data[i].inp);
     117                        printf ("  base         %d\n", data[i].base);
     118                        printf ("  pre          %d\n", pre);
     119                        printf ("  post         %d\n", post);
     120                        printf ("  got_nread    %d\n", got_nread);
     121                        printf ("  ftell_nread  %ld\n", ftell_nread);
     122                        abort ();
     123                      }
     124                  }
     125  
     126                /* if data[i].inp is a whole string to read and there's no post
     127                   whitespace then expect to have EOF */
     128                if (post == 0 && data[i].want_nread == strlen(data[i].inp))
     129                  {
     130                    int  c = getc(fp);
     131                    if (c != EOF)
     132                      {
     133                        printf ("mpf_inp_str didn't read to EOF\n");
     134                        printf ("  inp   \"%s\"\n", data[i].inp);
     135                        printf ("  base  %d\n", data[i].base);
     136                        printf ("  pre   %d\n", pre);
     137                        printf ("  post  %d\n", post);
     138                        printf ("  c     '%c' %#x\n", c, c);
     139                        abort ();
     140                      }
     141                  }
     142  
     143                /* only expect "pre" included in the count when non-zero */
     144                want_nread = data[i].want_nread;
     145                if (want_nread != 0)
     146                  want_nread += pre;
     147  
     148                if (got_nread != want_nread)
     149                  {
     150                    printf ("mpf_inp_str nread wrong\n");
     151                    printf ("  inp         \"%s\"\n", data[i].inp);
     152                    printf ("  base        %d\n", data[i].base);
     153                    printf ("  pre         %d\n", pre);
     154                    printf ("  post        %d\n", post);
     155                    printf ("  got_nread   %d\n", got_nread);
     156                    printf ("  want_nread  %d\n", want_nread);
     157                    abort ();
     158                  }
     159  
     160                MPF_CHECK_FORMAT (got);
     161  
     162                if (mpf_cmp (got, want) != 0)
     163                  {
     164                    printf ("mpf_inp_str wrong result\n");
     165                    printf ("  inp   \"%s\"\n", data[i].inp);
     166                    printf ("  base  %d\n", data[i].base);
     167                    mpf_trace ("  got ",  got);
     168                    mpf_trace ("  want", want);
     169                    abort ();
     170                  }
     171  
     172                ASSERT_ALWAYS (fclose (fp) == 0);
     173              }
     174          }
     175      }
     176  
     177    mpf_clear (got);
     178    mpf_clear (want);
     179  }
     180  
     181  int
     182  main (void)
     183  {
     184    tests_start ();
     185  
     186    check_data ();
     187  
     188    unlink (FILENAME);
     189    tests_end ();
     190    exit (0);
     191  }