1163516Simp/* Test mpf_inp_str. 2163516Simp 3163516SimpCopyright 2001, 2002 Free Software Foundation, Inc. 4163516Simp 5163516SimpThis file is part of the GNU MP Library. 6163516Simp 7163516SimpThe GNU MP Library is free software; you can redistribute it and/or modify 8163516Simpit under the terms of the GNU Lesser General Public License as published by 9163516Simpthe Free Software Foundation; either version 3 of the License, or (at your 10163516Simpoption) any later version. 11163516Simp 12163516SimpThe GNU MP Library is distributed in the hope that it will be useful, but 13163516SimpWITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 14163516Simpor FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 15163516SimpLicense for more details. 16163516Simp 17163516SimpYou should have received a copy of the GNU Lesser General Public License 18163516Simpalong with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */ 19163516Simp 20163516Simp#include "config.h" 21163516Simp 22163516Simp#include <stdio.h> 23163516Simp#include <stdlib.h> 24170002Simp#include <string.h> 25170002Simp#if HAVE_UNISTD_H 26170002Simp#include <unistd.h> /* for unlink */ 27170002Simp#endif 28170002Simp 29170002Simp#include "gmp.h" 30170002Simp#include "gmp-impl.h" 31170002Simp#include "tests.h" 32170002Simp 33170002Simp 34170002Simp#define FILENAME "t-inp_str.tmp" 35170002Simp 36170002Simp 37170002Simpvoid 38170002Simpcheck_data (void) 39170002Simp{ 40170002Simp static const struct { 41170002Simp const char *inp; 42170002Simp int base; 43170002Simp const char *want; 44170002Simp int want_nread; 45170002Simp 46170002Simp } data[] = { 47170002Simp 48170002Simp { "0", 10, "0", 1 }, 49170002Simp 50170002Simp { "abc", 10, "0", 0 }, 51163516Simp { "ghi", 16, "0", 0 }, 52163516Simp 53163516Simp { "125", 10, "125", 3 }, 54163516Simp { "125e1", 10, "1250", 5 }, 55163516Simp { "125e-1", 10, "12.5", 6 }, 56163516Simp 57163516Simp { "ff", 16, "255", 2 }, 58163516Simp { "-ff", 16, "-255", 3 }, 59163516Simp { "FF", 16, "255", 2 }, 60163516Simp { "-FF", 16, "-255", 3 }, 61163516Simp 62163516Simp { "100", 16, "256", 3 }, 63163516Simp { "100@1", 16, "4096", 5 }, 64163516Simp { "100@10", 16, "4722366482869645213696", 6 }, 65163516Simp { "100@10", -16, "281474976710656", 6 }, 66163516Simp { "100@-1", 16, "16", 6 }, 67163516Simp { "10000000000000000@-10", 16, "1", 21 }, 68163516Simp { "10000000000@-10", -16, "1", 15 }, 69163516Simp 70163516Simp { "z", 36, "35", 1 }, 71163516Simp { "Z", 36, "35", 1 }, 72163516Simp { "z@1", 36, "1260", 3 }, 73163516Simp { "Z@1", 36, "1260", 3 }, 74163516Simp 75163516Simp { "0", 0, "0", 1 }, 76163516Simp }; 77163516Simp 78163516Simp mpf_t got, want; 79163516Simp long ftell_nread; 80163516Simp int i, pre, post, j, got_nread, want_nread; 81163516Simp FILE *fp; 82163516Simp 83163516Simp mpf_init (got); 84163516Simp mpf_init (want); 85163516Simp 86163516Simp for (i = 0; i < numberof (data); i++) 87163516Simp { 88297329Skan for (pre = 0; pre <= 3; pre++) 89297329Skan { 90297329Skan for (post = 0; post <= 2; post++) 91297329Skan { 92297329Skan mpf_set_str_or_abort (want, data[i].want, 10); 93163516Simp MPF_CHECK_FORMAT (want); 94163516Simp 95163516Simp /* create the file new each time to ensure its length is what 96163516Simp we want */ 97163516Simp fp = fopen (FILENAME, "w+"); 98163516Simp ASSERT_ALWAYS (fp != NULL); 99163516Simp for (j = 0; j < pre; j++) 100163516Simp putc (' ', fp); 101163516Simp fputs (data[i].inp, fp); 102183468Simp for (j = 0; j < post; j++) 103188044Simp putc (' ', fp); 104163516Simp fflush (fp); 105163516Simp ASSERT_ALWAYS (! ferror(fp)); 106163516Simp 107163516Simp rewind (fp); 108163516Simp got_nread = mpf_inp_str (got, fp, data[i].base); 109163516Simp 110163516Simp if (got_nread != 0) 111163516Simp { 112163516Simp ftell_nread = ftell (fp); 113163516Simp if (got_nread != ftell_nread) 114163516Simp { 115163516Simp printf ("mpf_inp_str nread wrong\n"); 116163516Simp printf (" inp \"%s\"\n", data[i].inp); 117163516Simp printf (" base %d\n", data[i].base); 118163516Simp printf (" pre %d\n", pre); 119163516Simp printf (" post %d\n", post); 120163516Simp printf (" got_nread %d\n", got_nread); 121163516Simp printf (" ftell_nread %ld\n", ftell_nread); 122163516Simp abort (); 123163516Simp } 124163516Simp } 125163516Simp 126163516Simp /* if data[i].inp is a whole string to read and there's no post 127163516Simp whitespace then expect to have EOF */ 128163516Simp if (post == 0 && data[i].want_nread == strlen(data[i].inp)) 129163516Simp { 130163516Simp int c = getc(fp); 131163516Simp if (c != EOF) 132163516Simp { 133163516Simp printf ("mpf_inp_str didn't read to EOF\n"); 134163516Simp printf (" inp \"%s\"\n", data[i].inp); 135163516Simp printf (" base %d\n", data[i].base); 136163516Simp printf (" pre %d\n", pre); 137163516Simp printf (" post %d\n", post); 138163516Simp printf (" c '%c' %#x\n", c, c); 139163516Simp abort (); 140163516Simp } 141163516Simp } 142163516Simp 143163516Simp /* only expect "pre" included in the count when non-zero */ 144163516Simp want_nread = data[i].want_nread; 145234524Smarius if (want_nread != 0) 146234524Smarius want_nread += pre; 147163516Simp 148163516Simp if (got_nread != want_nread) 149163516Simp { 150163516Simp printf ("mpf_inp_str nread wrong\n"); 151163516Simp printf (" inp \"%s\"\n", data[i].inp); 152163516Simp printf (" base %d\n", data[i].base); 153163516Simp printf (" pre %d\n", pre); 154163516Simp printf (" post %d\n", post); 155163516Simp printf (" got_nread %d\n", got_nread); 156163516Simp printf (" want_nread %d\n", want_nread); 157163516Simp abort (); 158163516Simp } 159163516Simp 160163516Simp MPF_CHECK_FORMAT (got); 161163516Simp 162163516Simp if (mpf_cmp (got, want) != 0) 163163516Simp { 164163516Simp printf ("mpf_inp_str wrong result\n"); 165163516Simp printf (" inp \"%s\"\n", data[i].inp); 166163516Simp printf (" base %d\n", data[i].base); 167163516Simp mpf_trace (" got ", got); 168163516Simp mpf_trace (" want", want); 169163516Simp abort (); 170163516Simp } 171163516Simp 172163516Simp ASSERT_ALWAYS (fclose (fp) == 0); 173163516Simp } 174163516Simp } 175163516Simp } 176163516Simp 177163516Simp mpf_clear (got); 178163516Simp mpf_clear (want); 179163516Simp} 180163516Simp 181163516Simpint 182163516Simpmain (void) 183163516Simp{ 184163516Simp tests_start (); 185163516Simp 186163516Simp check_data (); 187163516Simp 188183704Smav unlink (FILENAME); 189183704Smav tests_end (); 190183704Smav exit (0); 191183704Smav} 192183704Smav