Use spaces for indentation, not tabs.
[gnulib.git] / tests / test-ftello.c
1 /* Test of ftello() 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 /* None of the files accessed by this test are large, so disable the
27    fseek link warning if we are not using the gnulib fseek module.  */
28 #if !GNULIB_FSEEK
29 # undef fseek
30 #endif
31
32 #define ASSERT(expr) \
33   do                                                                         \
34     {                                                                        \
35       if (!(expr))                                                           \
36         {                                                                    \
37           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
38           fflush (stderr);                                                   \
39           abort ();                                                          \
40         }                                                                    \
41     }                                                                        \
42   while (0)
43
44 #ifndef FUNC_UNGETC_BROKEN
45 # define FUNC_UNGETC_BROKEN 0
46 #endif
47
48 int
49 main (int argc, char **argv)
50 {
51   int ch;
52   /* Assume stdin is seekable iff argc > 1.  */
53   if (argc == 1)
54     {
55       ASSERT (ftell (stdin) == -1);
56       ASSERT (ftello (stdin) == -1);
57       return 0;
58     }
59
60   /* mingw ftell is unreliable on text mode input.  */
61   SET_BINARY (0);
62
63   /* Simple tests.  For each test, make sure ftell and ftello agree.  */
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 ungetc of read input.  */
73   ch = ungetc ('#', stdin);
74   ASSERT (ch == '#');
75   ASSERT (ftell (stdin) == 0);
76   ASSERT (ftello (stdin) == 0);
77
78   ch = fgetc (stdin);
79   ASSERT (ch == '#');
80   ASSERT (ftell (stdin) == 1);
81   ASSERT (ftello (stdin) == 1);
82
83   /* Test ftell after fseek.  */
84   ASSERT (fseek (stdin, 2, SEEK_SET) == 0);
85   ASSERT (ftell (stdin) == 2);
86   ASSERT (ftello (stdin) == 2);
87
88   /* Test ftell after random ungetc.  */
89   ch = fgetc (stdin);
90   ASSERT (ch == '/');
91   ch = ungetc ('@', stdin);
92   ASSERT (ch == '@');
93   ASSERT (ftell (stdin) == 2);
94   ASSERT (ftello (stdin) == 2);
95
96   ch = fgetc (stdin);
97   ASSERT (ch == '@');
98   ASSERT (ftell (stdin) == 3);
99   ASSERT (ftello (stdin) == 3);
100
101   if (2 < argc)
102     {
103       if (FUNC_UNGETC_BROKEN)
104         {
105           fputs ("Skipping test: ungetc cannot handle arbitrary bytes\n",
106                  stderr);
107           return 77;
108         }
109       /* Test ftell after ungetc without read.  */
110       ASSERT (fseek (stdin, 0, SEEK_CUR) == 0);
111       ASSERT (ftell (stdin) == 3);
112       ASSERT (ftello (stdin) == 3);
113
114       ch = ungetc ('~', stdin);
115       ASSERT (ch == '~');
116       ASSERT (ftell (stdin) == 2);
117       ASSERT (ftello (stdin) == 2);
118     }
119
120 #if !defined __MINT__ /* FreeMiNT has problems seeking past end of file */
121   /* Test ftell beyond end of file.  */
122   ASSERT (fseek (stdin, 0, SEEK_END) == 0);
123   ch = ftello (stdin);
124   ASSERT (fseek (stdin, 10, SEEK_END) == 0);
125   ASSERT (ftell (stdin) == ch + 10);
126   ASSERT (ftello (stdin) == ch + 10);
127 #endif
128
129   return 0;
130 }