tests: skip fseek/ftell tests if ungetc is broken
[gnulib.git] / tests / test-ftell.c
1 /* Test of ftell() function.
2    Copyright (C) 2007, 2008, 2009 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>, 2007.  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23
24 #include "binary-io.h"
25
26 #define ASSERT(expr) \
27   do                                                                         \
28     {                                                                        \
29       if (!(expr))                                                           \
30         {                                                                    \
31           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
32           fflush (stderr);                                                   \
33           abort ();                                                          \
34         }                                                                    \
35     }                                                                        \
36   while (0)
37
38 #ifndef FUNC_UNGETC_BROKEN
39 # define FUNC_UNGETC_BROKEN 0
40 #endif
41
42 int
43 main (int argc, char **argv)
44 {
45   int ch;
46   /* Assume stdin is seekable iff argc > 1.  */
47   if (argc == 1)
48     {
49       ASSERT (ftell (stdin) == -1);
50       return 0;
51     }
52
53   /* mingw ftell is unreliable on text mode input.  */
54   SET_BINARY (0);
55
56   /* Simple tests.  */
57   ASSERT (ftell (stdin) == 0);
58
59   ch = fgetc (stdin);
60   ASSERT (ch == '#');
61   ASSERT (ftell (stdin) == 1);
62
63   /* Test ftell after ungetc of read input.  */
64   ch = ungetc ('#', stdin);
65   ASSERT (ch == '#');
66   ASSERT (ftell (stdin) == 0);
67
68   ch = fgetc (stdin);
69   ASSERT (ch == '#');
70   ASSERT (ftell (stdin) == 1);
71
72   /* Test ftell after fseek.  */
73   ASSERT (fseek (stdin, 2, SEEK_SET) == 0);
74   ASSERT (ftell (stdin) == 2);
75
76   /* Test ftell after random ungetc.  */
77   ch = fgetc (stdin);
78   ASSERT (ch == '/');
79   ch = ungetc ('@', stdin);
80   ASSERT (ch == '@');
81   ASSERT (ftell (stdin) == 2);
82
83   ch = fgetc (stdin);
84   ASSERT (ch == '@');
85   ASSERT (ftell (stdin) == 3);
86
87   if (2 < argc)
88     {
89       if (FUNC_UNGETC_BROKEN)
90         {
91           fputs ("Skipping test: ungetc cannot handle arbitrary bytes\n",
92                  stderr);
93           return 77;
94         }
95       /* Test ftell after ungetc without read.  */
96       ASSERT (fseek (stdin, 0, SEEK_CUR) == 0);
97       ASSERT (ftell (stdin) == 3);
98
99       ch = ungetc ('~', stdin);
100       ASSERT (ch == '~');
101       ASSERT (ftell (stdin) == 2);
102     }
103
104   /* Test ftell beyond end of file.  */
105   ASSERT (fseek (stdin, 0, SEEK_END) == 0);
106   ch = ftell (stdin);
107   ASSERT (fseek (stdin, 10, SEEK_END) == 0);
108   ASSERT (ftell (stdin) == ch + 10);
109
110   return 0;
111 }