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