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 #include "freadahead.h"
30 #define ASSERT(expr) \
35 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
42 main (int argc, char **argv)
44 int nbytes = atoi (argv[1]);
45 void *buf = malloc (nbytes);
46 ASSERT (fread (buf, 1, nbytes, stdin) == nbytes);
48 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. */
51 ASSERT (freadahead (stdin) == 0);
54 /* Normal buffered stdio. */
55 const char stdin_contents[] =
56 "#!/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";
57 const char *expected = stdin_contents + nbytes;
62 /* Test normal behaviour. */
63 available = freadahead (stdin);
64 ASSERT (available != 0);
65 ASSERT (available <= strlen (expected));
67 const char *ptr = freadptr (stdin);
70 ASSERT (memcmp (ptr, expected, available) == 0);
73 /* Test behaviour after normal ungetc. */
74 ungetc (fgetc (stdin), stdin);
75 available2 = freadahead (stdin);
76 ASSERT (/* available2 == available - 1 || */ available2 == available);
78 if (available2 == available - 1)
80 ASSERT (freadptr (stdin) == NULL);
85 const char *ptr = freadptr (stdin);
88 ASSERT (memcmp (ptr, expected, available) == 0);
91 /* Test behaviour after arbitrary ungetc. */
94 available3 = freadahead (stdin);
95 ASSERT (available3 == 0 || available3 == 1 || /* available3 == available - 1 || */ available3 == available);
98 else if (available3 == 1)
100 const char *ptr = freadptr (stdin);
104 ASSERT (ptr[0] == '@');
108 else if (available3 == available - 1)
110 ASSERT (freadptr (stdin) == NULL);
115 const char *ptr = freadptr (stdin);
119 ASSERT (ptr[0] == '@');
120 ASSERT (memcmp (ptr + 1, expected + 1, available - 1) == 0);