xref: /netbsd-src/external/lgpl3/mpc/dist/tests/tstrtoc.c (revision 39f28e1e142c5bfb6be935a49cb55e2287fec7ea)
18fa80f29Smrg /* tstrtoc -- test file for mpc_strtoc.
28fa80f29Smrg 
38fa80f29Smrg Copyright (C) 2009, 2011 INRIA
48fa80f29Smrg 
58fa80f29Smrg This file is part of GNU MPC.
68fa80f29Smrg 
78fa80f29Smrg GNU MPC is free software; you can redistribute it and/or modify it under
88fa80f29Smrg the terms of the GNU Lesser General Public License as published by the
98fa80f29Smrg Free Software Foundation; either version 3 of the License, or (at your
108fa80f29Smrg option) any later version.
118fa80f29Smrg 
128fa80f29Smrg GNU MPC is distributed in the hope that it will be useful, but WITHOUT ANY
138fa80f29Smrg WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
148fa80f29Smrg FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
158fa80f29Smrg more details.
168fa80f29Smrg 
178fa80f29Smrg You should have received a copy of the GNU Lesser General Public License
188fa80f29Smrg along with this program. If not, see http://www.gnu.org/licenses/ .
198fa80f29Smrg */
208fa80f29Smrg 
218fa80f29Smrg #include <string.h>
228fa80f29Smrg #include <stdlib.h>
238fa80f29Smrg 
248fa80f29Smrg #include "mpc-tests.h"
258fa80f29Smrg 
268fa80f29Smrg extern unsigned long line_number;
278fa80f29Smrg extern int nextchar;
288fa80f29Smrg extern char *pathname;
298fa80f29Smrg 
308fa80f29Smrg /* names of rounding modes */
31*39f28e1eSmrg extern const char *mpc_rnd_mode[];
328fa80f29Smrg 
338fa80f29Smrg static void
check_file(const char * file_name)348fa80f29Smrg check_file (const char* file_name)
358fa80f29Smrg {
368fa80f29Smrg   FILE *fp;
378fa80f29Smrg   unsigned long test_line_number;
388fa80f29Smrg 
398fa80f29Smrg   size_t str_len = 255;
408fa80f29Smrg   char *str = NULL;
418fa80f29Smrg   size_t rstr_len = 255;
428fa80f29Smrg   char *rstr = NULL;
438fa80f29Smrg   char *end = NULL;
448fa80f29Smrg 
458fa80f29Smrg   int base;
468fa80f29Smrg   int inex_re;
478fa80f29Smrg   int inex_im;
488fa80f29Smrg   mpc_t expected, got;
498fa80f29Smrg   mpc_rnd_t rnd = MPC_RNDNN;
508fa80f29Smrg   int inex = 0, inex_expected;
518fa80f29Smrg   known_signs_t ks = {1, 1};
528fa80f29Smrg 
538fa80f29Smrg 
548fa80f29Smrg   fp = open_data_file (file_name);
558fa80f29Smrg 
568fa80f29Smrg   /* initializations */
578fa80f29Smrg   str = (char *) malloc (str_len);
588fa80f29Smrg   if (str == NULL)
598fa80f29Smrg     {
608fa80f29Smrg       printf ("Cannot allocate memory\n");
618fa80f29Smrg       exit (1);
628fa80f29Smrg     }
638fa80f29Smrg   rstr = (char *) malloc (rstr_len);
648fa80f29Smrg   if (rstr == NULL)
658fa80f29Smrg     {
668fa80f29Smrg       printf ("Cannot allocate memory\n");
678fa80f29Smrg       exit (1);
688fa80f29Smrg     }
698fa80f29Smrg   mpc_init2 (expected, 53);
708fa80f29Smrg   mpc_init2 (got, 53);
718fa80f29Smrg 
728fa80f29Smrg   /* read data file */
738fa80f29Smrg   line_number = 1;
748fa80f29Smrg   nextchar = getc (fp);
758fa80f29Smrg   while (nextchar != EOF)
768fa80f29Smrg     {
778fa80f29Smrg       skip_whitespace_comments (fp);
788fa80f29Smrg 
798fa80f29Smrg       /* 1. read a line of data: expected result, base, rounding mode */
808fa80f29Smrg       test_line_number = line_number;
818fa80f29Smrg       read_ternary (fp, &inex_re);
828fa80f29Smrg       read_ternary (fp, &inex_im);
838fa80f29Smrg       read_mpc (fp, expected, NULL);
848fa80f29Smrg       if (inex_re == TERNARY_ERROR || inex_im == TERNARY_ERROR)
858fa80f29Smrg          inex_expected = -1;
868fa80f29Smrg       else
878fa80f29Smrg          inex_expected = MPC_INEX (inex_re, inex_im);
888fa80f29Smrg 
898fa80f29Smrg       str_len = read_string (fp, &str, str_len, "number string");
908fa80f29Smrg       rstr_len = read_string (fp, &rstr, rstr_len, "string remainder");
918fa80f29Smrg       read_int (fp, &base, "base");
928fa80f29Smrg       read_mpc_rounding_mode (fp, &rnd);
938fa80f29Smrg 
948fa80f29Smrg       /* 2. convert string at the same precision as the expected result */
958fa80f29Smrg       mpfr_set_prec (mpc_realref (got), MPC_PREC_RE (expected));
968fa80f29Smrg       mpfr_set_prec (mpc_imagref (got), MPC_PREC_IM (expected));
978fa80f29Smrg       inex = mpc_strtoc (got, str, &end, base, rnd);
988fa80f29Smrg 
998fa80f29Smrg       /* 3. compare this result with the expected one */
1008fa80f29Smrg       if (inex != inex_expected
1018fa80f29Smrg           || !same_mpc_value (got, expected, ks)
1028fa80f29Smrg           || strcmp (end, rstr) != 0)
1038fa80f29Smrg         {
1048fa80f29Smrg           printf ("mpc_strtoc(str) failed (line %lu)\nwith base=%d and "
1058fa80f29Smrg                   "rounding mode %s\n", test_line_number, base,
106*39f28e1eSmrg                   mpc_rnd_mode[rnd]);
1078fa80f29Smrg           if (inex != MPC_INEX (inex_re, inex_im))
1088fa80f29Smrg             printf ("ternary value: got %s, expected (%s, %s)\n",
1098fa80f29Smrg                     MPC_INEX_STR (inex),
1108fa80f29Smrg                     (inex_re == +1 ? "+1" : (inex_re == -1 ? "-1" : "0")),
1118fa80f29Smrg                     (inex_im == +1 ? "+1" : (inex_im == -1 ? "-1" : "0")));
1128fa80f29Smrg           printf ("str = \"%s\"\n", str);
1138fa80f29Smrg           if (strcmp (end, rstr) != 0)
1148fa80f29Smrg             printf ("string remainder expected \"%s\"\n"
1158fa80f29Smrg                     "                 got      \"%s\"\n",
1168fa80f29Smrg                     rstr, end);
1178fa80f29Smrg           else
1188fa80f29Smrg             {
1198fa80f29Smrg               printf ("     ");
1208fa80f29Smrg               MPC_OUT (got);
1218fa80f29Smrg               MPC_OUT (expected);
1228fa80f29Smrg             }
1238fa80f29Smrg           exit (1);
1248fa80f29Smrg         }
1258fa80f29Smrg 
1268fa80f29Smrg       end = NULL;
1278fa80f29Smrg     }
1288fa80f29Smrg 
1298fa80f29Smrg   mpc_clear (expected);
1308fa80f29Smrg   mpc_clear (got);
1318fa80f29Smrg   if (str != NULL)
1328fa80f29Smrg     free (str);
1338fa80f29Smrg   if (rstr != NULL)
1348fa80f29Smrg     free (rstr);
1358fa80f29Smrg   close_data_file (fp);
1368fa80f29Smrg }
1378fa80f29Smrg 
1388fa80f29Smrg static void
check_null(void)1398fa80f29Smrg check_null (void)
1408fa80f29Smrg {
1418fa80f29Smrg   int inex;
1428fa80f29Smrg   char *end;
1438fa80f29Smrg   mpc_t z;
1448fa80f29Smrg 
1458fa80f29Smrg   mpc_init2 (z, 53);
1468fa80f29Smrg 
1478fa80f29Smrg   inex = mpc_strtoc (z, NULL, &end, 10, MPC_RNDNN);
1488fa80f29Smrg   if (end != NULL || inex != -1 || mpfr_nan_p (mpc_realref (z)) == 0
1498fa80f29Smrg       || mpfr_nan_p (mpc_imagref (z)) == 0)
1508fa80f29Smrg     {
1518fa80f29Smrg       printf ("Error: mpc_strtoc(z, NULL) with a NULL pointer should fail"
1528fa80f29Smrg               " and the z value should be set to NaN +I*NaN\ngot ");
1538fa80f29Smrg       MPC_OUT (z);
1548fa80f29Smrg       exit (1);
1558fa80f29Smrg     }
1568fa80f29Smrg 
1578fa80f29Smrg   mpc_clear (z);
1588fa80f29Smrg }
1598fa80f29Smrg 
1608fa80f29Smrg int
main(void)1618fa80f29Smrg main (void)
1628fa80f29Smrg {
1638fa80f29Smrg   check_null ();
1648fa80f29Smrg   check_file ("strtoc.dat");
1658fa80f29Smrg   return 0;
1668fa80f29Smrg }
167