5c69bbe8fde2be6c770481ca5f3719ade8d55a70
[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 #include "freadahead.h"
29
30 #define ASSERT(expr) \
31   do                                                                         \
32     {                                                                        \
33       if (!(expr))                                                           \
34         {                                                                    \
35           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
36           abort ();                                                          \
37         }                                                                    \
38     }                                                                        \
39   while (0)
40
41 int
42 main (int argc, char **argv)
43 {
44   int nbytes = atoi (argv[1]);
45   void *buf = malloc (nbytes);
46   ASSERT (fread (buf, 1, nbytes, stdin) == nbytes);
47
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);
52   else
53     {
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;
58       size_t available;
59       size_t available2;
60       size_t available3;
61
62       /* Test normal behaviour.  */
63       available = freadahead (stdin);
64       ASSERT (available != 0);
65       ASSERT (available <= strlen (expected));
66       {
67         const char *ptr = freadptr (stdin);
68
69         ASSERT (ptr != NULL);
70         ASSERT (memcmp (ptr, expected, available) == 0);
71       }
72
73       /* Test behaviour after normal ungetc.  */
74       ungetc (fgetc (stdin), stdin);
75       available2 = freadahead (stdin);
76       ASSERT (/* available2 == available - 1 || */ available2 == available);
77 #if 0
78       if (available2 == available - 1)
79         {
80           ASSERT (freadptr (stdin) == NULL);
81         }
82       else
83 #endif
84         {
85           const char *ptr = freadptr (stdin);
86
87           ASSERT (ptr != NULL);
88           ASSERT (memcmp (ptr, expected, available) == 0);
89         }
90
91       /* Test behaviour after arbitrary ungetc.  */
92       fgetc (stdin);
93       ungetc ('@', stdin);
94       available3 = freadahead (stdin);
95       ASSERT (available3 == 0 || available3 == 1 || /* available3 == available - 1 || */ available3 == available);
96       if (available3 == 0)
97         ;
98       else if (available3 == 1)
99         {
100           const char *ptr = freadptr (stdin);
101
102           if (ptr != NULL)
103             {
104               ASSERT (ptr[0] == '@');
105             }
106         }
107 #if 0
108       else if (available3 == available - 1)
109         {
110           ASSERT (freadptr (stdin) == NULL);
111         }
112 #endif
113       else
114         {
115           const char *ptr = freadptr (stdin);
116
117           if (ptr != NULL)
118             {
119               ASSERT (ptr[0] == '@');
120               ASSERT (memcmp (ptr + 1, expected + 1, available - 1) == 0);
121             }
122         }
123     }
124
125   return 0;
126 }