Permalink
Browse files

Merge pull request #4 from TomMD/trunk

Make nmod_poly_fread set the mod to the value from the file (re-init).
  • Loading branch information...
2 parents 24be8a7 + 4331c2d commit a9b98bf6c71c3b92164c5b2a14e5ece038eed305 @fredrik-johansson fredrik-johansson committed Mar 31, 2012
Showing with 327 additions and 1 deletion.
  1. +2 −0 fmpz_mod_poly.h
  2. +69 −0 fmpz_mod_poly/fread.c
  3. +253 −0 fmpz_mod_poly/test/t-print_read.c
  4. +3 −1 nmod_poly/fread.c
View
2 fmpz_mod_poly.h
@@ -514,6 +514,8 @@ int _fmpz_mod_poly_fprint(FILE * file, const fmpz *poly, long len,
int fmpz_mod_poly_fprint(FILE * file, const fmpz_mod_poly_t poly);
+int fmpz_mod_poly_fread(FILE * file, fmpz_mod_poly_t poly);
+
static __inline__
int fmpz_mod_poly_fprint_pretty(FILE * file,
const fmpz_mod_poly_t poly, const char * x)
View
69 fmpz_mod_poly/fread.c
@@ -0,0 +1,69 @@
+/*=============================================================================
+
+ This file is part of FLINT.
+
+ FLINT is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ FLINT is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with FLINT; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+
+=============================================================================*/
+/******************************************************************************
+
+ Copyright (C) 2007, David Howden.
+ Copyright (C) 2010 William Hart
+
+******************************************************************************/
+
+#include <mpir.h>
+#include <stdio.h>
+#include "flint.h"
+#include "fmpz_mod_poly.h"
+#include "fmpz.h"
+
+int fmpz_mod_poly_fread(FILE * f, fmpz_mod_poly_t poly)
+{
+ long i, length;
+ fmpz_t coeff;
+ unsigned long res;
+
+ fmpz_init(coeff);
+ if (fscanf(f, "%ld", &length) != 1) {
+ fmpz_clear(coeff);
+ return 0;
+ }
+
+ fmpz_fread(f,coeff);
+ fmpz_mod_poly_clear(poly);
+ fmpz_mod_poly_init(poly, coeff);
+ fmpz_mod_poly_fit_length(poly, length);
+ poly->length = length;
+
+ for (i = 0; i < length; i++)
+ {
+ res = fmpz_fread(f, coeff);
+ fmpz_mod_poly_set_coeff_fmpz(poly,i,coeff);
+
+ if (!res)
+ {
+ poly->length = i;
+ fmpz_clear(coeff);
+ return 0;
+ }
+ }
+
+ fmpz_clear(coeff);
+ _fmpz_mod_poly_normalise(poly);
+
+ return 1;
+}
+
View
253 fmpz_mod_poly/test/t-print_read.c
@@ -0,0 +1,253 @@
+/*=============================================================================
+
+ This file is part of FLINT.
+
+ FLINT is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ FLINT is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with FLINT; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+
+=============================================================================*/
+/******************************************************************************
+
+ Copyright (C) 2010 Sebastian Pancratz
+
+******************************************************************************/
+
+
+#include <sys/types.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <mpir.h>
+
+#include "flint.h"
+#include "fmpz.h"
+#include "fmpz_mod_poly.h"
+
+/*
+ The function fdopen is declared in stdio.h. It is POSIX.1 compliant,
+ but not ANSI compliant. The following line enables compilation with
+ the "-ansi" flag.
+ */
+extern FILE * fdopen(int fildes, const char *mode);
+
+int main(void)
+{
+ int i, j, n = 1000, result;
+ flint_rand_t state;
+
+ FILE *in, *out;
+ int fd[2];
+ pid_t childpid;
+ fmpz_t two;
+ fmpz_init(two);
+ fmpz_set_ui(two,2);
+
+ printf("print/ read....");
+ fflush(stdout);
+
+ flint_randinit(state);
+
+ /* Randomise n polynomials, write to and read from a pipe */
+ {
+ fmpz_mod_poly_t *a;
+
+ a = malloc(n * sizeof(fmpz_mod_poly_t));
+ for (i = 0; i < n; i++)
+ {
+ fmpz_mod_poly_init(a[i],two);
+ fmpz_mod_poly_randtest(a[i], state, n_randint(state, 100));
+ }
+
+ if (pipe(fd))
+ {
+ printf("FAIL:\n");
+ printf("Failed to set-up the pipe.\n");
+ abort();
+ }
+
+ if((childpid = fork()) == -1)
+ {
+ printf("FAIL:\n");
+ printf("Failed to fork the process.\n");
+ abort();
+ }
+
+ if(childpid == 0) /* Child process */
+ {
+ int r;
+
+ close(fd[0]);
+ out = fdopen(fd[1], "w");
+ if (out == NULL)
+ {
+ printf("FAIL:\n");
+ printf("Could not open output file at the pipe.\n");
+ abort();
+ }
+
+ for (j = 0; j < n; j++)
+ {
+ r = fmpz_mod_poly_fprint(out, a[j]);
+ if ((j < n - 1) && (r > 0))
+ r = fprintf(out, "\n");
+
+ if (r <= 0)
+ {
+ printf("FAIL:\n");
+ printf("Write error.\n");
+ abort();
+ }
+ }
+
+ fclose(out);
+ exit(0);
+ }
+ else /* Parent process */
+ {
+ int r;
+ fmpz_mod_poly_t t;
+
+ close(fd[1]);
+ in = fdopen(fd[0], "r");
+ if (in == NULL)
+ {
+ printf("FAIL:\n");
+ printf("Could not open input file at the pipe.\n");
+ abort();
+ }
+
+ fmpz_mod_poly_init(t,two);
+
+ i = 0;
+ while (!feof(in))
+ {
+ r = fmpz_mod_poly_fread(in, t);
+ if (r <= 0)
+ {
+ printf("FAIL:\n");
+ printf("Read error.\n");
+ abort();
+ }
+
+ result = fmpz_mod_poly_equal(t, a[i]);
+ if (!result)
+ {
+ printf("FAIL:\n");
+ printf("a[i] = "), fmpz_mod_poly_print(a[i]), printf("\n");
+ printf("t = "), fmpz_mod_poly_print(t), printf("\n");
+ abort();
+ }
+
+ ++i;
+ }
+
+ fmpz_mod_poly_clear(t);
+ fclose(in);
+ }
+
+ if (i != n)
+ {
+ printf("FAIL:\n");
+ printf("Only %d out of %d objects were processed.\n", i, n);
+ abort();
+ }
+
+ for (i = 0; i < n; i++)
+ fmpz_mod_poly_clear(a[i]);
+ free(a);
+ }
+
+ /* Write bad data to a pipe and read it */
+ {
+ char str[5] = {'b', 'l', 'a', 'h', '\0'};
+
+ if (pipe(fd))
+ {
+ printf("FAIL:\n");
+ printf("Failed to set-up the pipe.\n");
+ abort();
+ }
+
+ if((childpid = fork()) == -1)
+ {
+ printf("FAIL:\n");
+ printf("Failed to fork the process.\n");
+ abort();
+ }
+
+ if(childpid == 0) /* Child process */
+ {
+ int r;
+
+ close(fd[0]);
+ out = fdopen(fd[1], "w");
+ if (out == NULL)
+ {
+ printf("FAIL:\n");
+ printf("Could not open output file at the pipe.\n");
+ abort();
+ }
+
+ r = fprintf(out, "blah");
+ if (r <= 0)
+ {
+ printf("FAIL:\n");
+ printf("Write error.\n");
+ abort();
+ }
+
+ fclose(out);
+ exit(0);
+ }
+ else /* Parent process */
+ {
+ int r;
+ fmpz_mod_poly_t t;
+
+ close(fd[1]);
+ in = fdopen(fd[0], "r");
+ if (in == NULL)
+ {
+ printf("FAIL:\n");
+ printf("Could not open input file at the pipe.\n");
+ abort();
+ }
+
+ fmpz_mod_poly_init(t,two);
+
+ i = 0;
+ // Only four junk bytes are sent and our read
+ // doesn't consume invalid bytes, so eof is never reached
+ for(i=0; i<500; i++)
+ {
+ r = fmpz_mod_poly_fread(in, t);
+ if (r > 0)
+ {
+ printf("FAIL:\n");
+ printf("r = %d\n", r);
+ abort();
+ }
+ }
+
+ fmpz_mod_poly_clear(t);
+ fclose(in);
+ }
+ }
+
+ fmpz_clear(two);
+ flint_randclear(state);
+ _fmpz_cleanup();
+ printf("PASS\n");
+ return EXIT_SUCCESS;
+}
View
4 nmod_poly/fread.c
@@ -36,7 +36,9 @@ int nmod_poly_fread(FILE * f, nmod_poly_t poly)
if (fscanf(f, "%ld %lu", &length, &n) != 2)
return 0;
-
+
+ nmod_poly_clear(poly);
+ nmod_poly_init(poly,n);
nmod_poly_fit_length(poly, length);
poly->length = length;

0 comments on commit a9b98bf

Please sign in to comment.