Use spaces for indentation, not tabs.
[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 /* 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       return 0;
57     }
58
59   /* mingw ftell is unreliable on text mode input.  */
60   SET_BINARY (0);
61
62   /* Simple tests.  */
63   ASSERT (ftell (stdin) == 0);
64
65   ch = fgetc (stdin);
66   ASSERT (ch == '#');
67   ASSERT (ftell (stdin) == 1);
68
69   /* Test ftell after ungetc of read input.  */
70   ch = ungetc ('#', stdin);
71   ASSERT (ch == '#');
72   ASSERT (ftell (stdin) == 0);
73
74   ch = fgetc (stdin);
75   ASSERT (ch == '#');
76   ASSERT (ftell (stdin) == 1);
77
78   /* Test ftell after fseek.  */
79   ASSERT (fseek (stdin, 2, SEEK_SET) == 0);
80   ASSERT (ftell (stdin) == 2);
81
82   /* Test ftell after random ungetc.  */
83   ch = fgetc (stdin);
84   ASSERT (ch == '/');
85   ch = ungetc ('@', stdin);
86   ASSERT (ch == '@');
87   ASSERT (ftell (stdin) == 2);
88
89   ch = fgetc (stdin);
90   ASSERT (ch == '@');
91   ASSERT (ftell (stdin) == 3);
92
93   if (2 < argc)
94     {
95       if (FUNC_UNGETC_BROKEN)
96         {
97           fputs ("Skipping test: ungetc cannot handle arbitrary bytes\n",
98                  stderr);
99           return 77;
100         }
101       /* Test ftell after ungetc without read.  */
102       ASSERT (fseek (stdin, 0, SEEK_CUR) == 0);
103       ASSERT (ftell (stdin) == 3);
104
105       ch = ungetc ('~', stdin);
106       ASSERT (ch == '~');
107       ASSERT (ftell (stdin) == 2);
108     }
109
110 #if !defined __MINT__ /* FreeMiNT has problems seeking past end of file */
111   /* Test ftell beyond end of file.  */
112   ASSERT (fseek (stdin, 0, SEEK_END) == 0);
113   ch = ftell (stdin);
114   ASSERT (fseek (stdin, 10, SEEK_END) == 0);
115   ASSERT (ftell (stdin) == ch + 10);
116 #endif
117
118   return 0;
119 }