e25bc029f5ae5fe81b6063c760a36cd56fe1a919
[gnulib.git] / tests / test-ftell.c
1 /* Test of ftell() 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       return 0;
46     }
47
48   /* mingw ftell is unreliable on text mode input.  */
49   SET_BINARY (0);
50
51   /* Simple tests.  */
52   ASSERT (ftell (stdin) == 0);
53
54   ch = fgetc (stdin);
55   ASSERT (ch == '#');
56   ASSERT (ftell (stdin) == 1);
57
58   /* Test ftell after ungetc of read input.  */
59   ch = ungetc ('#', stdin);
60   ASSERT (ch == '#');
61   ASSERT (ftell (stdin) == 0);
62
63   ch = fgetc (stdin);
64   ASSERT (ch == '#');
65   ASSERT (ftell (stdin) == 1);
66
67   /* Test ftell after fseek.  */
68   ASSERT (fseek (stdin, 2, SEEK_SET) == 0);
69   ASSERT (ftell (stdin) == 2);
70
71   /* Test ftell after random ungetc.  */
72   ch = fgetc (stdin);
73   ASSERT (ch == '/');
74   ch = ungetc ('@', stdin);
75   ASSERT (ch == '@');
76   ASSERT (ftell (stdin) == 2);
77
78   ch = fgetc (stdin);
79   ASSERT (ch == '@');
80   ASSERT (ftell (stdin) == 3);
81
82   /* Test ftell after ungetc without read.  */
83   ASSERT (fseek (stdin, 0, SEEK_CUR) == 0);
84   ASSERT (ftell (stdin) == 3);
85
86   ch = ungetc ('~', stdin);
87   ASSERT (ch == '~');
88   ASSERT (ftell (stdin) == 2);
89
90   /* Test ftell beyond end of file.  */
91   ASSERT (fseek (stdin, 0, SEEK_END) == 0);
92   ch = ftell (stdin);
93   ASSERT (fseek (stdin, 10, SEEK_END) == 0);
94   ASSERT (ftell (stdin) == ch + 10);
95
96   return 0;
97 }