Avoid some warnings from "gcc -Wshadow".
[gnulib.git] / tests / test-ftello.c
1 /* Test of ftello() 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>, 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           abort ();                                                          \
33         }                                                                    \
34     }                                                                        \
35   while (0)
36
37 int
38 main (int argc, char **argv)
39 {
40   int ch;
41   /* Assume stdin is seekable iff argc > 1.  */
42   if (argc == 1)
43     {
44       ASSERT (ftell (stdin) == -1);
45       ASSERT (ftello (stdin) == -1);
46       return 0;
47     }
48
49   /* mingw ftell is unreliable on text mode input.  */
50   SET_BINARY (0);
51
52   /* Simple tests.  For each test, make sure ftell and ftello agree.  */
53   ASSERT (ftell (stdin) == 0);
54   ASSERT (ftello (stdin) == 0);
55
56   ch = fgetc (stdin);
57   ASSERT (ch == '#');
58   ASSERT (ftell (stdin) == 1);
59   ASSERT (ftello (stdin) == 1);
60
61   /* Test ftell after ungetc of read input.  */
62   ch = ungetc ('#', stdin);
63   ASSERT (ch == '#');
64   ASSERT (ftell (stdin) == 0);
65   ASSERT (ftello (stdin) == 0);
66
67   ch = fgetc (stdin);
68   ASSERT (ch == '#');
69   ASSERT (ftell (stdin) == 1);
70   ASSERT (ftello (stdin) == 1);
71
72   /* Test ftell after fseek.  */
73   ASSERT (fseek (stdin, 2, SEEK_SET) == 0);
74   ASSERT (ftell (stdin) == 2);
75   ASSERT (ftello (stdin) == 2);
76
77   /* Test ftell after random ungetc.  */
78   ch = fgetc (stdin);
79   ASSERT (ch == '/');
80   ch = ungetc ('@', stdin);
81   ASSERT (ch == '@');
82   ASSERT (ftell (stdin) == 2);
83   ASSERT (ftello (stdin) == 2);
84
85   ch = fgetc (stdin);
86   ASSERT (ch == '@');
87   ASSERT (ftell (stdin) == 3);
88   ASSERT (ftello (stdin) == 3);
89
90   /* Test ftell after ungetc without read.  */
91   ASSERT (fseek (stdin, 0, SEEK_CUR) == 0);
92   ASSERT (ftell (stdin) == 3);
93   ASSERT (ftello (stdin) == 3);
94
95   ch = ungetc ('~', stdin);
96   ASSERT (ch == '~');
97   ASSERT (ftell (stdin) == 2);
98   ASSERT (ftello (stdin) == 2);
99
100   /* Test ftell beyond end of file.  */
101   ASSERT (fseek (stdin, 0, SEEK_END) == 0);
102   ch = ftello (stdin);
103   ASSERT (fseek (stdin, 10, SEEK_END) == 0);
104   ASSERT (ftell (stdin) == ch + 10);
105   ASSERT (ftello (stdin) == ch + 10);
106
107   return 0;
108 }