usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / tests / test-freadptr.c
blob03ca9d7d8c23c55880b168cca744eaed489cb949
1 /* Test of freadptr() function.
2 Copyright (C) 2007-2024 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 <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2008. */
19 #include <config.h>
21 #include "freadptr.h"
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
27 #include "macros.h"
29 int
30 main (int argc, char **argv)
32 int nbytes = atoi (argv[1]);
34 void *buf = malloc (nbytes);
35 ASSERT (fread (buf, 1, nbytes, stdin) == nbytes);
36 free (buf);
39 if (lseek (0, 0, SEEK_CUR) == nbytes)
41 /* An unbuffered stdio, such as BeOS or on uClibc compiled without
42 __STDIO_BUFFERS. Or stdin is a pipe. */
43 size_t size;
44 ASSERT (freadptr (stdin, &size) == NULL);
46 else
48 /* Normal buffered stdio. */
49 const char stdin_contents[] =
50 "#!/bin/sh\n\n${CHECKER} ./test-freadptr${EXEEXT} 5 < \"$srcdir/test-freadptr.sh\" || exit 1\ncat \"$srcdir/test-freadptr.sh\" | ${CHECKER} ./test-freadptr${EXEEXT} 5 || exit 1\nexit 0\n";
51 const char *expected = stdin_contents + nbytes;
52 size_t available1;
53 size_t available2;
54 size_t available3;
56 /* Test normal behaviour. */
58 const char *ptr = freadptr (stdin, &available1);
60 ASSERT (ptr != NULL);
61 ASSERT (available1 != 0);
62 ASSERT (available1 <= strlen (expected));
63 ASSERT (memcmp (ptr, expected, available1) == 0);
66 /* Test behaviour after normal ungetc. */
67 ungetc (fgetc (stdin), stdin);
69 const char *ptr = freadptr (stdin, &available2);
71 if (ptr != NULL)
73 ASSERT (available2 == available1);
74 ASSERT (memcmp (ptr, expected, available2) == 0);
78 /* Test behaviour after arbitrary ungetc. */
79 fgetc (stdin);
80 ungetc ('@', stdin);
82 const char *ptr = freadptr (stdin, &available3);
84 if (ptr != NULL)
86 ASSERT (available3 == 1 || available3 == available1);
87 ASSERT (ptr[0] == '@');
88 if (available3 > 1)
90 ASSERT (memcmp (ptr + 1, expected + 1, available3 - 1) == 0);
96 /* Free memory allocated during ungetc(). */
97 fclose (stdin);
99 return test_exit_status;