1 /* Test of freadptr() function.
2 Copyright (C) 2007-2008 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. */
28 #define ASSERT(expr) \
33 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
41 main (int argc, char **argv)
43 int nbytes = atoi (argv[1]);
44 void *buf = malloc (nbytes);
45 ASSERT (fread (buf, 1, nbytes, stdin) == nbytes);
47 if (lseek (0, 0, SEEK_CUR) == nbytes)
49 /* An unbuffered stdio, such as BeOS or on uClibc compiled without
50 __STDIO_BUFFERS. Or stdin is a pipe. */
52 ASSERT (freadptr (stdin, &size) == NULL);
56 /* Normal buffered stdio. */
57 const char stdin_contents[] =
58 "#!/bin/sh\n\n./test-freadptr${EXEEXT} 5 < \"$srcdir/test-freadptr.sh\" || exit 1\ncat \"$srcdir/test-freadptr.sh\" | ./test-freadptr${EXEEXT} 5 || exit 1\nexit 0\n";
59 const char *expected = stdin_contents + nbytes;
64 /* Test normal behaviour. */
66 const char *ptr = freadptr (stdin, &available1);
69 ASSERT (available1 != 0);
70 ASSERT (available1 <= strlen (expected));
71 ASSERT (memcmp (ptr, expected, available1) == 0);
74 /* Test behaviour after normal ungetc. */
75 ungetc (fgetc (stdin), stdin);
77 const char *ptr = freadptr (stdin, &available2);
81 ASSERT (available2 == available1);
82 ASSERT (memcmp (ptr, expected, available2) == 0);
86 /* Test behaviour after arbitrary ungetc. */
90 const char *ptr = freadptr (stdin, &available3);
94 ASSERT (available3 == 1 || available3 == available1);
95 ASSERT (ptr[0] == '@');
98 ASSERT (memcmp (ptr + 1, expected + 1, available3 - 1) == 0);