summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorthevenyp <thevenyp@211d60ee-9f03-0410-a15a-8952a2c7a4e4>2009-03-20 15:07:03 +0000
committerthevenyp <thevenyp@211d60ee-9f03-0410-a15a-8952a2c7a4e4>2009-03-20 15:07:03 +0000
commit787eb286cd5e116b091c940f0e6d81fd5a12518f (patch)
treed902969b4e6a92bd52804b0c4f12fcd77072581f
parentf8ab5b5dc3961522317426ace0d50bd8603aed33 (diff)
downloadmpc-787eb286cd5e116b091c940f0e6d81fd5a12518f.tar.gz
tests/mpc-tests.h tests/read_data.c: factorize code reading data file, and use
environment variable srcdir defined by 'make check'. tests/tstrtoc.c: Simplify using new functions in read_data. git-svn-id: svn://scm.gforge.inria.fr/svn/mpc/trunk@491 211d60ee-9f03-0410-a15a-8952a2c7a4e4
-rw-r--r--tests/mpc-tests.h18
-rw-r--r--tests/read_data.c158
-rw-r--r--tests/tstrtoc.c143
3 files changed, 153 insertions, 166 deletions
diff --git a/tests/mpc-tests.h b/tests/mpc-tests.h
index 6589657..dc3be5d 100644
--- a/tests/mpc-tests.h
+++ b/tests/mpc-tests.h
@@ -20,13 +20,11 @@ the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA. */
#include <stdio.h>
-#include "mpc-impl.h"
+#include <ctype.h>
-#define TERNARY_NOT_CHECKED 255
- /* special value to indicate that the ternary value is not checked */
-#define TERNARY_ERROR 254
- /* special value to indicate that an error occurred in an mpc function */
+#include "mpc-impl.h"
+/** OUTPUT HELPER MACROS */
#define MPFR_OUT(X) \
printf (#X" [%ld]=", MPFR_PREC (X));\
mpfr_out_str (stdout, 2, 0, (X), GMP_RNDN);\
@@ -163,11 +161,21 @@ void tgeneric (mpc_function, mpfr_prec_t, mpfr_prec_t, mpfr_prec_t, mp_exp_t);
precomputed data in a file.*/
void data_check (mpc_function, const char *);
+FILE * open_data_file (const char *file_name);
+void close_data_file (FILE *fp);
+
/* helper file reading functions */
void skip_whitespace_comments (FILE *fp);
void read_ternary (FILE *fp, int* ternary);
void read_mpfr_rounding_mode (FILE *fp, mpfr_rnd_t* rnd);
void read_mpc_rounding_mode (FILE *fp, mpc_rnd_t* rnd);
mpfr_prec_t read_mpfr_prec (FILE *fp);
+void read_int (FILE *fp, int *n, const char *name);
+size_t read_string (FILE *fp, char **buffer_ptr, size_t buffer_length, const char *name);
void read_mpfr (FILE *fp, mpfr_ptr x, int *known_sign);
void read_mpc (FILE *fp, mpc_ptr z, known_signs_t *ks);
+
+#define TERNARY_NOT_CHECKED 255
+ /* special value to indicate that the ternary value is not checked */
+#define TERNARY_ERROR 254
+ /* special value to indicate that an error occurred in an mpc function */
diff --git a/tests/read_data.c b/tests/read_data.c
index ddd0462..bb245be 100644
--- a/tests/read_data.c
+++ b/tests/read_data.c
@@ -21,13 +21,8 @@ MA 02111-1307, USA. */
#include <stdlib.h>
#include <string.h>
-#include <ctype.h>
#include "mpc-tests.h"
-#ifndef __SRCDIR
-#define __SRCDIR .
-#endif
-
char *pathname;
unsigned long line_number;
/* file name with complete path and currently read line;
@@ -35,18 +30,19 @@ unsigned long line_number;
int nextchar;
/* character appearing next in the file, may be EOF */
-#define MPC_INEX_CMP(r, i, c) \
+#define MPC_INEX_CMP(r, i, c) \
(((r) == TERNARY_NOT_CHECKED || (r) == MPC_INEX_RE(c)) \
&& ((i) == TERNARY_NOT_CHECKED || (i) == MPC_INEX_IM (c)))
+
#define MPFR_INEX_STR(inex) \
- (inex) == TERNARY_NOT_CHECKED ? "?" \
+ (inex) == TERNARY_NOT_CHECKED ? "?" \
: (inex) == +1 ? "+1" \
: (inex) == -1 ? "-1" : "0"
static const char *mpfr_rnd_mode [] =
{ "GMP_RNDN", "GMP_RNDZ", "GMP_RNDU", "GMP_RNDD" };
-static const char *rnd_mode[] =
+const char *rnd_mode[] =
{ "MPC_RNDNN", "MPC_RNDZN", "MPC_RNDUN", "MPC_RNDDN",
"undefined", "undefined", "undefined", "undefined", "undefined",
"undefined", "undefined", "undefined", "undefined", "undefined",
@@ -65,6 +61,42 @@ static const char *rnd_mode[] =
"undefined", "undefined",
};
+/* file functions */
+FILE *
+open_data_file (const char *file_name)
+{
+ FILE *fp;
+ char *src_dir;
+
+ src_dir = getenv ("srcdir");
+ if (src_dir == NULL)
+ src_dir = ".";
+
+ pathname = malloc (((strlen (src_dir)) + strlen (file_name) + 2)
+ * sizeof (char));
+ if (pathname == NULL)
+ {
+ printf ("Cannot allocate memory\n");
+ exit (1);
+ }
+ sprintf (pathname, "%s/%s", src_dir, file_name);
+ fp = fopen (pathname, "r");
+ if (fp == NULL)
+ {
+ fprintf (stderr, "Unable to open %s\n", pathname);
+ exit (1);
+ }
+
+ return fp;
+}
+
+void
+close_data_file (FILE *fp)
+{
+ free (pathname);
+ fclose (fp);
+}
+
/* read primitives */
static void
skip_line (FILE *fp)
@@ -103,6 +135,67 @@ skip_whitespace_comments (FILE *fp)
}
}
+
+size_t
+read_string (FILE *fp, char **buffer_ptr, size_t buffer_length, const char *name)
+{
+ size_t pos;
+ char *buffer;
+
+ pos = 0;
+ buffer = *buffer_ptr;
+
+ if (nextchar == '"')
+ nextchar = getc (fp);
+ else
+ goto error;
+
+ while (nextchar != EOF && nextchar != '"')
+ {
+ if (nextchar == '\n')
+ line_number++;
+ if (pos + 1 > buffer_length)
+ {
+ buffer = realloc (buffer, 2 * buffer_length);
+ if (buffer == NULL)
+ {
+ printf ("Cannot allocate memory\n");
+ exit (1);
+ }
+ buffer_length *= 2;
+ }
+ buffer[pos++] = nextchar;
+ nextchar = getc (fp);
+ }
+
+ if (nextchar != '"')
+ goto error;
+
+ if (pos + 1 > buffer_length)
+ {
+ buffer = realloc (buffer, buffer_length + 1);
+ if (buffer == NULL)
+ {
+ printf ("Cannot allocate memory\n");
+ exit (1);
+ }
+ buffer_length *= 2;
+ }
+ buffer[pos] = '\0';
+
+ nextchar = getc (fp);
+ skip_whitespace_comments (fp);
+
+ buffer_ptr = &buffer;
+
+ return buffer_length;
+
+ error:
+ printf ("Error: Unable to read %s in file '%s' line '%lu'\n",
+ name, pathname, line_number);
+ exit (1);
+}
+
/* All following read routines skip over whitespace and comments; */
/* so after calling them, nextchar is either EOF or the beginning */
/* of a non-comment token. */
@@ -178,6 +271,30 @@ read_mpc_rounding_mode (FILE *fp, mpc_rnd_t* rnd)
*rnd = RNDC (re, im);
}
+void
+read_int (FILE *fp, int *nread, const char *name)
+{
+ int n = 0;
+
+ if (nextchar == EOF)
+ {
+ printf ("Error: Unexpected EOF when reading mpfr precision "
+ "in file '%s' line %lu\n",
+ pathname, line_number);
+ exit (1);
+ }
+ ungetc (nextchar, fp);
+ n = fscanf (fp, "%i", nread);
+ if (ferror (fp) || n == 0 || n == EOF)
+ {
+ printf ("Error: Cannot read %s in file '%s' line %lu\n",
+ name, pathname, line_number);
+ exit (1);
+ }
+ nextchar = getc (fp);
+ skip_whitespace_comments (fp);
+}
+
mpfr_prec_t
read_mpfr_prec (FILE *fp)
{
@@ -323,23 +440,9 @@ data_check (mpc_function function, const char *file_name)
known_signs_t signs;
int inex = 0;
- /* 1. open data file */
- pathname = (char *)malloc ((strlen (QUOTE (__SRCDIR)) + strlen (file_name)
- + 2) * sizeof (char));
- if (pathname == NULL)
- {
- printf ("Cannot allocate memory\n");
- exit (1);
- }
- sprintf (pathname, QUOTE (__SRCDIR)"/%s", file_name);
- fp = fopen (pathname, "r");
- if (fp == NULL)
- {
- fprintf (stderr, "Unable to open %s\n", pathname);
- exit (1);
- }
+ fp = open_data_file (file_name);
- /* 2. init needed variables */
+ /* 1. init needed variables */
mpc_init2 (z1, 2);
switch (function.type)
{
@@ -365,7 +468,7 @@ data_check (mpc_function function, const char *file_name)
;
}
- /* 3. read data file */
+ /* 2. read data file */
line_number = 1;
nextchar = getc (fp);
skip_whitespace_comments (fp);
@@ -555,7 +658,7 @@ data_check (mpc_function function, const char *file_name)
}
}
- /* 4. Clear used variables */
+ /* 3. Clear used variables */
mpc_clear (z1);
switch (function.type)
{
@@ -581,6 +684,5 @@ data_check (mpc_function function, const char *file_name)
;
}
- fclose (fp);
- free (pathname);
+ close_data_file (fp);
}
diff --git a/tests/tstrtoc.c b/tests/tstrtoc.c
index 84e3e66..40950c4 100644
--- a/tests/tstrtoc.c
+++ b/tests/tstrtoc.c
@@ -19,123 +19,17 @@ along with the MPC Library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA. */
-#include <ctype.h>
#include <string.h>
-#include <stdio.h>
#include <stdlib.h>
#include "mpc-tests.h"
-#ifndef __SRCDIR
-#define __SRCDIR .
-#endif
-
extern unsigned long line_number;
extern int nextchar;
extern char *pathname;
-static const char *rnd_mode[] =
- { "MPC_RNDNN", "MPC_RNDZN", "MPC_RNDUN", "MPC_RNDDN",
- "undefined", "undefined", "undefined", "undefined", "undefined",
- "undefined", "undefined", "undefined", "undefined", "undefined",
- "undefined", "undefined",
- "MPC_RNDNZ", "MPC_RNDZZ", "MPC_RNDUZ", "MPC_RNDDZ",
- "undefined", "undefined", "undefined", "undefined", "undefined",
- "undefined", "undefined", "undefined", "undefined", "undefined",
- "undefined", "undefined",
- "MPC_RNDNU", "MPC_RNDZU", "MPC_RNDUU", "MPC_RNDDU",
- "undefined", "undefined", "undefined", "undefined", "undefined",
- "undefined", "undefined", "undefined", "undefined", "undefined",
- "undefined", "undefined",
- "MPC_RNDND", "MPC_RNDZD", "MPC_RNDUD", "MPC_RNDDD",
- "undefined", "undefined", "undefined", "undefined", "undefined",
- "undefined", "undefined", "undefined", "undefined", "undefined",
- "undefined", "undefined",
- };
-
-static void
-read_int (FILE *fp, int *nread, const char *name)
-{
- int n = 0;
-
- if (nextchar == EOF)
- {
- printf ("Error: Unexpected EOF when reading mpfr precision "
- "in file '%s' line %lu\n",
- pathname, line_number);
- exit (1);
- }
- ungetc (nextchar, fp);
- n = fscanf (fp, "%i", nread);
- if (ferror (fp) || n == 0 || n == EOF)
- {
- printf ("Error: Cannot read %s in file '%s' line %lu\n",
- name, pathname, line_number);
- exit (1);
- }
- nextchar = getc (fp);
- skip_whitespace_comments (fp);
-}
-
-static size_t
-read_string (FILE *fp, char **buffer_ptr, size_t buffer_length, const char *name)
-{
- size_t pos;
- char *buffer;
-
- pos = 0;
- buffer = *buffer_ptr;
-
- if (nextchar == '"')
- nextchar = getc (fp);
- else
- goto error;
-
- while (nextchar != EOF && nextchar != '"')
- {
- if (nextchar == '\n')
- line_number++;
- if (pos + 1 > buffer_length)
- {
- buffer = realloc (buffer, 2 * buffer_length);
- if (buffer == NULL)
- {
- printf ("Cannot allocate memory\n");
- exit (1);
- }
- buffer_length *= 2;
- }
- buffer[pos++] = nextchar;
- nextchar = getc (fp);
- }
-
- if (nextchar != '"')
- goto error;
-
- if (pos + 1 > buffer_length)
- {
- buffer = realloc (buffer, buffer_length + 1);
- if (buffer == NULL)
- {
- printf ("Cannot allocate memory\n");
- exit (1);
- }
- buffer_length *= 2;
- }
- buffer[pos] = '\0';
-
- nextchar = getc (fp);
- skip_whitespace_comments (fp);
-
- buffer_ptr = &buffer;
-
- return buffer_length;
-
- error:
- printf ("Error: Unable to read %s in file '%s' line '%lu'\n",
- name, pathname, line_number);
- exit (1);
-}
+/* names of rounding modes */
+extern char *rnd_mode[];
static void
check_file (const char* file_name)
@@ -157,23 +51,9 @@ check_file (const char* file_name)
known_signs_t ks = {1, 1};
- /* 1. open data file */
- pathname = (char *)malloc ((strlen (QUOTE (__SRCDIR)) + strlen (file_name)
- + 2) * sizeof (char));
- if (pathname == NULL)
- {
- printf ("Cannot allocate memory\n");
- exit (1);
- }
- sprintf (pathname, QUOTE (__SRCDIR)"/%s", file_name);
- fp = fopen (pathname, "r");
- if (fp == NULL)
- {
- fprintf (stderr, "Unable to open %s\n", pathname);
- exit (1);
- }
-
- /* 2. init needed variables */
+ fp = open_data_file (file_name);
+
+ /* initializations */
str = (char *) malloc (str_len * sizeof (char));
if (str == NULL)
{
@@ -189,14 +69,14 @@ check_file (const char* file_name)
mpc_init2 (expected, 53);
mpc_init2 (got, 53);
- /* 3. read data file */
+ /* read data file */
line_number = 1;
nextchar = getc (fp);
while (nextchar != EOF)
{
skip_whitespace_comments (fp);
- /* 3.1 read a line of data: expected result, base, rounding mode */
+ /* 1. read a line of data: expected result, base, rounding mode */
read_ternary (fp, &inex_re);
read_ternary (fp, &inex_im);
read_mpc (fp, expected, NULL);
@@ -210,12 +90,12 @@ check_file (const char* file_name)
read_int (fp, &base, "base");
read_mpc_rounding_mode (fp, &rnd);
- /* 3.2 convert string at the same precision as the expected result */
+ /* 2. convert string at the same precision as the expected result */
mpfr_set_prec (MPC_RE (got), MPC_PREC_RE (expected));
mpfr_set_prec (MPC_IM (got), MPC_PREC_IM (expected));
inex = mpc_strtoc (got, str, &end, base, rnd);
- /* 3.3 compare this result with the expected one */
+ /* 3. compare this result with the expected one */
if (inex != inex_expected
|| !same_mpc_value (got, expected, ks)
|| strcmp (end, rstr) != 0)
@@ -244,16 +124,13 @@ check_file (const char* file_name)
end = NULL;
}
- fclose (fp);
-
- /* 4. Clear used variables */
mpc_clear (expected);
mpc_clear (got);
if (str != NULL)
free (str);
if (rstr != NULL)
free (rstr);
- free (pathname);
+ close_data_file (fp);
}
static void