Home | History | Annotate | Line # | Download | only in mpf
t-inp_str.c revision 1.1.1.3
      1      1.1  mrg /* Test mpf_inp_str.
      2      1.1  mrg 
      3      1.1  mrg Copyright 2001, 2002 Free Software Foundation, Inc.
      4      1.1  mrg 
      5  1.1.1.2  mrg This file is part of the GNU MP Library test suite.
      6      1.1  mrg 
      7  1.1.1.2  mrg The GNU MP Library test suite is free software; you can redistribute it
      8  1.1.1.2  mrg and/or modify it under the terms of the GNU General Public License as
      9  1.1.1.2  mrg published by the Free Software Foundation; either version 3 of the License,
     10  1.1.1.2  mrg or (at your option) any later version.
     11  1.1.1.2  mrg 
     12  1.1.1.2  mrg The GNU MP Library test suite is distributed in the hope that it will be
     13  1.1.1.2  mrg useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  1.1.1.2  mrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
     15  1.1.1.2  mrg Public License for more details.
     16      1.1  mrg 
     17  1.1.1.2  mrg You should have received a copy of the GNU General Public License along with
     18  1.1.1.3  mrg the GNU MP Library test suite.  If not, see https://www.gnu.org/licenses/.  */
     19      1.1  mrg 
     20      1.1  mrg #include "config.h"
     21      1.1  mrg 
     22      1.1  mrg #include <stdio.h>
     23      1.1  mrg #include <stdlib.h>
     24      1.1  mrg #include <string.h>
     25      1.1  mrg #if HAVE_UNISTD_H
     26      1.1  mrg #include <unistd.h>		/* for unlink */
     27      1.1  mrg #endif
     28      1.1  mrg 
     29      1.1  mrg #include "gmp.h"
     30      1.1  mrg #include "gmp-impl.h"
     31      1.1  mrg #include "tests.h"
     32      1.1  mrg 
     33      1.1  mrg 
     34      1.1  mrg #define FILENAME  "t-inp_str.tmp"
     35      1.1  mrg 
     36      1.1  mrg 
     37      1.1  mrg void
     38      1.1  mrg check_data (void)
     39      1.1  mrg {
     40      1.1  mrg   static const struct {
     41      1.1  mrg     const char  *inp;
     42      1.1  mrg     int         base;
     43      1.1  mrg     const char  *want;
     44      1.1  mrg     int         want_nread;
     45      1.1  mrg 
     46      1.1  mrg   } data[] = {
     47      1.1  mrg 
     48      1.1  mrg     { "0",   10, "0", 1 },
     49      1.1  mrg 
     50      1.1  mrg     { "abc", 10, "0", 0 },
     51      1.1  mrg     { "ghi", 16, "0", 0 },
     52      1.1  mrg 
     53      1.1  mrg     { "125",    10, "125",  3 },
     54      1.1  mrg     { "125e1",  10, "1250", 5 },
     55  1.1.1.2  mrg     { "12e+2",  10, "1200", 5 },
     56      1.1  mrg     { "125e-1", 10, "12.5", 6 },
     57      1.1  mrg 
     58      1.1  mrg     {  "ff", 16,  "255", 2 },
     59      1.1  mrg     { "-ff", 16, "-255", 3 },
     60      1.1  mrg     {  "FF", 16,  "255", 2 },
     61      1.1  mrg     { "-FF", 16, "-255", 3 },
     62      1.1  mrg 
     63      1.1  mrg     { "100",     16, "256",  3 },
     64      1.1  mrg     { "100@1",   16, "4096", 5 },
     65      1.1  mrg     { "100@10",  16, "4722366482869645213696", 6 },
     66      1.1  mrg     { "100@10", -16, "281474976710656",        6 },
     67      1.1  mrg     { "100@-1",  16, "16",   6 },
     68      1.1  mrg     { "10000000000000000@-10",  16, "1", 21 },
     69      1.1  mrg     { "10000000000@-10",       -16, "1", 15 },
     70      1.1  mrg 
     71      1.1  mrg     { "z", 36, "35", 1 },
     72      1.1  mrg     { "Z", 36, "35", 1 },
     73      1.1  mrg     { "z@1", 36, "1260", 3 },
     74      1.1  mrg     { "Z@1", 36, "1260", 3 },
     75      1.1  mrg 
     76      1.1  mrg     {  "0",      0,   "0", 1 },
     77      1.1  mrg   };
     78      1.1  mrg 
     79      1.1  mrg   mpf_t  got, want;
     80      1.1  mrg   long   ftell_nread;
     81      1.1  mrg   int    i, pre, post, j, got_nread, want_nread;
     82      1.1  mrg   FILE   *fp;
     83      1.1  mrg 
     84      1.1  mrg   mpf_init (got);
     85      1.1  mrg   mpf_init (want);
     86      1.1  mrg 
     87      1.1  mrg   for (i = 0; i < numberof (data); i++)
     88      1.1  mrg     {
     89      1.1  mrg       for (pre = 0; pre <= 3; pre++)
     90      1.1  mrg         {
     91      1.1  mrg           for (post = 0; post <= 2; post++)
     92      1.1  mrg             {
     93      1.1  mrg               mpf_set_str_or_abort (want, data[i].want, 10);
     94      1.1  mrg               MPF_CHECK_FORMAT (want);
     95      1.1  mrg 
     96      1.1  mrg               /* create the file new each time to ensure its length is what
     97      1.1  mrg                  we want */
     98      1.1  mrg               fp = fopen (FILENAME, "w+");
     99      1.1  mrg               ASSERT_ALWAYS (fp != NULL);
    100      1.1  mrg               for (j = 0; j < pre; j++)
    101      1.1  mrg                 putc (' ', fp);
    102      1.1  mrg               fputs (data[i].inp, fp);
    103      1.1  mrg               for (j = 0; j < post; j++)
    104      1.1  mrg                 putc (' ', fp);
    105      1.1  mrg               fflush (fp);
    106      1.1  mrg               ASSERT_ALWAYS (! ferror(fp));
    107      1.1  mrg 
    108      1.1  mrg               rewind (fp);
    109      1.1  mrg               got_nread = mpf_inp_str (got, fp, data[i].base);
    110      1.1  mrg 
    111      1.1  mrg               if (got_nread != 0)
    112      1.1  mrg                 {
    113      1.1  mrg                   ftell_nread = ftell (fp);
    114      1.1  mrg                   if (got_nread != ftell_nread)
    115      1.1  mrg                     {
    116      1.1  mrg                       printf ("mpf_inp_str nread wrong\n");
    117      1.1  mrg                       printf ("  inp          \"%s\"\n", data[i].inp);
    118      1.1  mrg                       printf ("  base         %d\n", data[i].base);
    119      1.1  mrg                       printf ("  pre          %d\n", pre);
    120      1.1  mrg                       printf ("  post         %d\n", post);
    121      1.1  mrg                       printf ("  got_nread    %d\n", got_nread);
    122      1.1  mrg                       printf ("  ftell_nread  %ld\n", ftell_nread);
    123      1.1  mrg                       abort ();
    124      1.1  mrg                     }
    125      1.1  mrg                 }
    126      1.1  mrg 
    127      1.1  mrg               /* if data[i].inp is a whole string to read and there's no post
    128      1.1  mrg                  whitespace then expect to have EOF */
    129      1.1  mrg               if (post == 0 && data[i].want_nread == strlen(data[i].inp))
    130      1.1  mrg                 {
    131      1.1  mrg                   int  c = getc(fp);
    132      1.1  mrg                   if (c != EOF)
    133      1.1  mrg                     {
    134      1.1  mrg                       printf ("mpf_inp_str didn't read to EOF\n");
    135      1.1  mrg                       printf ("  inp   \"%s\"\n", data[i].inp);
    136      1.1  mrg                       printf ("  base  %d\n", data[i].base);
    137      1.1  mrg                       printf ("  pre   %d\n", pre);
    138      1.1  mrg                       printf ("  post  %d\n", post);
    139      1.1  mrg                       printf ("  c     '%c' %#x\n", c, c);
    140      1.1  mrg                       abort ();
    141      1.1  mrg                     }
    142      1.1  mrg                 }
    143      1.1  mrg 
    144      1.1  mrg               /* only expect "pre" included in the count when non-zero */
    145      1.1  mrg               want_nread = data[i].want_nread;
    146      1.1  mrg               if (want_nread != 0)
    147      1.1  mrg                 want_nread += pre;
    148      1.1  mrg 
    149      1.1  mrg               if (got_nread != want_nread)
    150      1.1  mrg                 {
    151      1.1  mrg                   printf ("mpf_inp_str nread wrong\n");
    152      1.1  mrg                   printf ("  inp         \"%s\"\n", data[i].inp);
    153      1.1  mrg                   printf ("  base        %d\n", data[i].base);
    154      1.1  mrg                   printf ("  pre         %d\n", pre);
    155      1.1  mrg                   printf ("  post        %d\n", post);
    156      1.1  mrg                   printf ("  got_nread   %d\n", got_nread);
    157      1.1  mrg                   printf ("  want_nread  %d\n", want_nread);
    158      1.1  mrg                   abort ();
    159      1.1  mrg                 }
    160      1.1  mrg 
    161      1.1  mrg               MPF_CHECK_FORMAT (got);
    162      1.1  mrg 
    163      1.1  mrg               if (mpf_cmp (got, want) != 0)
    164      1.1  mrg                 {
    165      1.1  mrg                   printf ("mpf_inp_str wrong result\n");
    166      1.1  mrg                   printf ("  inp   \"%s\"\n", data[i].inp);
    167      1.1  mrg                   printf ("  base  %d\n", data[i].base);
    168      1.1  mrg                   mpf_trace ("  got ",  got);
    169      1.1  mrg                   mpf_trace ("  want", want);
    170      1.1  mrg                   abort ();
    171      1.1  mrg                 }
    172      1.1  mrg 
    173      1.1  mrg               ASSERT_ALWAYS (fclose (fp) == 0);
    174      1.1  mrg             }
    175      1.1  mrg         }
    176      1.1  mrg     }
    177      1.1  mrg 
    178      1.1  mrg   mpf_clear (got);
    179      1.1  mrg   mpf_clear (want);
    180      1.1  mrg }
    181      1.1  mrg 
    182      1.1  mrg int
    183      1.1  mrg main (void)
    184      1.1  mrg {
    185      1.1  mrg   tests_start ();
    186      1.1  mrg 
    187      1.1  mrg   check_data ();
    188      1.1  mrg 
    189      1.1  mrg   unlink (FILENAME);
    190      1.1  mrg   tests_end ();
    191      1.1  mrg   exit (0);
    192      1.1  mrg }
    193