Flush the standard error stream before aborting.
[gnulib.git] / tests / test-freadptr.c
1 /* Test of freadptr() function.
2    Copyright (C) 2007-2008 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>.  */
16
17 /* Written by Bruno Haible <bruno@clisp.org>, 2008.  */
18
19 #include <config.h>
20
21 #include "freadptr.h"
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27
28 #define ASSERT(expr) \
29   do                                                                         \
30     {                                                                        \
31       if (!(expr))                                                           \
32         {                                                                    \
33           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
34           fflush (stderr);                                                   \
35           abort ();                                                          \
36         }                                                                    \
37     }                                                                        \
38   while (0)
39
40 int
41 main (int argc, char **argv)
42 {
43   int nbytes = atoi (argv[1]);
44   void *buf = malloc (nbytes);
45   ASSERT (fread (buf, 1, nbytes, stdin) == nbytes);
46
47   if (lseek (0, 0, SEEK_CUR) == nbytes)
48     {
49       /* An unbuffered stdio, such as BeOS or on uClibc compiled without
50          __STDIO_BUFFERS.  Or stdin is a pipe.  */
51       size_t size;
52       ASSERT (freadptr (stdin, &size) == NULL);
53     }
54   else
55     {
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;
60       size_t available1;
61       size_t available2;
62       size_t available3;
63
64       /* Test normal behaviour.  */
65       {
66         const char *ptr = freadptr (stdin, &available1);
67
68         ASSERT (ptr != NULL);
69         ASSERT (available1 != 0);
70         ASSERT (available1 <= strlen (expected));
71         ASSERT (memcmp (ptr, expected, available1) == 0);
72       }
73
74       /* Test behaviour after normal ungetc.  */
75       ungetc (fgetc (stdin), stdin);
76       {
77         const char *ptr = freadptr (stdin, &available2);
78
79         if (ptr != NULL)
80           {
81             ASSERT (available2 == available1);
82             ASSERT (memcmp (ptr, expected, available2) == 0);
83           }
84       }
85
86       /* Test behaviour after arbitrary ungetc.  */
87       fgetc (stdin);
88       ungetc ('@', stdin);
89       {
90         const char *ptr = freadptr (stdin, &available3);
91
92         if (ptr != NULL)
93           {
94             ASSERT (available3 == 1 || available3 == available1);
95             ASSERT (ptr[0] == '@');
96             if (available3 > 1)
97               {
98                 ASSERT (memcmp (ptr + 1, expected + 1, available3 - 1) == 0);
99               }
100           }
101       }
102     }
103
104   return 0;
105 }