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