1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
|
/*=============================================================================
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 <string.h>
#include <stdlib.h>
#include <gmp.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpz_poly.h"
#if !defined (__WIN32) || defined(__CYGWIN__)
/*
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;
FILE *in, *out;
int fd[2];
pid_t childpid;
FLINT_TEST_INIT(state);
flint_printf("print/ read_pretty....");
fflush(stdout);
/* Randomise n polynomials, write to and read from a pipe */
{
fmpz_poly_t *a;
char *var = "x";
a = flint_malloc(n * sizeof(fmpz_poly_t));
for (i = 0; i < n; i++)
{
fmpz_poly_init(a[i]);
fmpz_poly_randtest(a[i], state, 100, 100);
}
if (pipe(fd))
{
flint_printf("FAIL:\n");
flint_printf("Failed to set-up the pipe.\n");
abort();
}
if((childpid = fork()) == -1)
{
flint_printf("FAIL:\n");
flint_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)
{
flint_printf("FAIL:\n");
flint_printf("Could not open output file at the pipe.\n");
abort();
}
for (j = 0; j < n; j++)
{
r = fmpz_poly_fprint_pretty(out, a[j], var);
if ((j < n - 1) && (r > 0))
r = flint_fprintf(out, "\n");
if (r <= 0)
{
flint_printf("FAIL:\n");
flint_printf("Write error.\n");
abort();
}
}
fclose(out);
exit(0);
}
else /* Parent process */
{
int r;
fmpz_poly_t t;
char *rvar;
close(fd[1]);
in = fdopen(fd[0], "r");
if (in == NULL)
{
flint_printf("FAIL:\n");
flint_printf("Could not open input file at the pipe.\n");
abort();
}
fmpz_poly_init(t);
i = 0;
while (!feof(in))
{
r = fmpz_poly_fread_pretty(in, t, &rvar);
if (r <= 0)
{
flint_printf("FAIL:\n");
flint_printf("Read error.\n");
abort();
}
result = fmpz_poly_equal(t, a[i]) &&
(t->length <= 1 || (strcmp(var, rvar) == 0));
if (!result)
{
flint_printf("FAIL:\n");
flint_printf("a[i] = "), fmpz_poly_print_pretty(a[i], var), flint_printf("\n");
flint_printf("t = "), fmpz_poly_print_pretty(t, rvar), flint_printf("\n");
flint_printf("rvar = %s\n", rvar);
abort();
}
flint_free(rvar);
++i;
}
fmpz_poly_clear(t);
fclose(in);
}
if (i != n)
{
flint_printf("FAIL:\n");
flint_printf("Only %d out of %d objects were processed.\n", i, n);
abort();
}
for (i = 0; i < n; i++)
fmpz_poly_clear(a[i]);
flint_free(a);
}
/* Write "blah" to the pipe and see it read as a variable */
{
char str[5] = {'b', 'l', 'a', 'h', '\0'};
if (pipe(fd))
{
flint_printf("FAIL:\n");
flint_printf("Failed to set-up the pipe.\n");
abort();
}
if((childpid = fork()) == -1)
{
flint_printf("FAIL:\n");
flint_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)
{
flint_printf("FAIL:\n");
flint_printf("Could not open output file at the pipe.\n");
abort();
}
r = fputs(str, out);
if (r == EOF)
{
flint_printf("FAIL:\n");
flint_printf("Write error.\n");
abort();
}
fclose(out);
exit(0);
}
else /* Parent process */
{
char *rvar = NULL;
int r;
fmpz_poly_t t;
close(fd[1]);
in = fdopen(fd[0], "r");
if (in == NULL)
{
flint_printf("FAIL:\n");
flint_printf("Could not open input file at the pipe.\n");
abort();
}
fmpz_poly_init(t);
while (!feof(in))
{
r = fmpz_poly_fread_pretty(in, t, &rvar);
result = (r > 0) && rvar && (strcmp(str, rvar) == 0) &&
(t->length == 2) && (t->coeffs[0] == WORD(0)) &&
(t->coeffs[1] == WORD(1));
if (!result)
{
flint_printf("FAIL:\n");
flint_printf("r = %d\n", r);
flint_printf("str = {%s}\n", str);
flint_printf("rvar = {%s}\n", rvar);
flint_printf("t = "), fmpz_poly_print(t), flint_printf("\n");
abort();
}
if (rvar)
flint_free(rvar);
}
fmpz_poly_clear(t);
fclose(in);
}
}
FLINT_TEST_CLEANUP(state);
flint_printf("PASS\n");
return EXIT_SUCCESS;
}
#else
int main(void)
{
flint_printf("print/ read....");
fflush(stdout);
flint_printf("SKIPPED\n");
return EXIT_SUCCESS;
}
#endif
|