1 /* Test of freadseek() function.
2 Copyright (C) 2007-2009 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2008. */
21 #include "freadseek.h"
28 #define ASSERT(expr) \
33 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
41 main (int argc, char **argv)
43 static const char stdin_contents[] =
44 "#!/bin/sh\n\n./test-freadseek${EXEEXT} 5 19 6 7 18 9 19 < \"$srcdir/test-freadseek.sh\" || exit 1\ncat \"$srcdir/test-freadseek.sh\" | ./test-freadseek${EXEEXT} 5 19 6 7 18 9 19 || exit 1\nexit 0\n";
45 int nbytes1 = atoi (argv[1]);
46 int nbytes2 = atoi (argv[2]);
47 int nbytes3 = atoi (argv[3]);
48 int nbytes4 = atoi (argv[4]);
49 int nbytes5 = atoi (argv[5]);
50 int nbytes6 = atoi (argv[6]);
51 int nbytes7 = atoi (argv[7]);
52 void *buf1 = malloc (nbytes1);
53 void *buf3 = malloc (nbytes3);
54 void *buf5 = malloc (nbytes5);
55 void *buf7 = malloc (nbytes7);
56 /* A private variable to keep track of the position. */
59 ASSERT (fread (buf1, 1, nbytes1, stdin) == nbytes1);
60 ASSERT (memcmp (buf1, stdin_contents + position, nbytes1) == 0);
63 /* Test normal behaviour. */
64 ASSERT (freadseek (stdin, nbytes2) == 0);
67 ASSERT (fread (buf3, 1, nbytes3, stdin) == nbytes3);
68 ASSERT (memcmp (buf3, stdin_contents + position, nbytes3) == 0);
71 /* Test behaviour after normal ungetc. */
72 ungetc (fgetc (stdin), stdin);
73 ASSERT (freadseek (stdin, nbytes4) == 0);
76 ASSERT (fread (buf5, 1, nbytes5, stdin) == nbytes5);
77 ASSERT (memcmp (buf5, stdin_contents + position, nbytes5) == 0);
80 /* Test behaviour after arbitrary ungetc. */
83 ASSERT (freadseek (stdin, nbytes6) == 0);
86 ASSERT (fread (buf7, 1, nbytes7, stdin) == nbytes7);
87 ASSERT (memcmp (buf7, stdin_contents + position, nbytes7) == 0);
90 /* Test move to end of file. */
91 ASSERT (freadseek (stdin, strlen (stdin_contents) - position) == 0);
92 ASSERT (fgetc (stdin) == EOF);
93 ASSERT (!ferror (stdin));
95 #if !defined __MINT__ /* FreeMiNT has problems seeking past end of file */
96 /* Test move beyond end of file. */
97 ASSERT (freadseek (stdin, 1000000) == 0);
98 ASSERT (fgetc (stdin) == EOF);
99 ASSERT (!ferror (stdin));