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