Ticket #3145: fseek-ftell-bug.c

File fseek-ftell-bug.c, 1.2 KB (added by bhaible, 15 years ago)

a second test case

Line 
1/* Test of ftell function. */
2
3#include <stdio.h>
4#include <stdlib.h>
5
6#define ASSERT(expr) \
7 do \
8 { \
9 if (!(expr)) \
10 { \
11 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
12 fflush (stderr); \
13 abort (); \
14 } \
15 } \
16 while (0)
17
18int
19main ()
20{
21 int ch;
22
23 /* Simple tests. */
24 ASSERT (ftell (stdin) == 0);
25
26 ch = fgetc (stdin);
27 ASSERT (ch == '/');
28 ASSERT (ftell (stdin) == 1);
29
30 /* Test ftell after ungetc of read input. */
31 ch = ungetc ('/', stdin);
32 ASSERT (ch == '/');
33 ASSERT (ftell (stdin) == 0);
34
35 ch = fgetc (stdin);
36 ASSERT (ch == '/');
37 ASSERT (ftell (stdin) == 1);
38
39 /* Test ftell after fseek. */
40 ASSERT (fseek (stdin, 2, SEEK_SET) == 0);
41 ASSERT (ftell (stdin) == 2);
42
43 /* Test ftell after random ungetc. */
44 ch = fgetc (stdin);
45 ASSERT (ch == ' ');
46 ch = ungetc ('@', stdin);
47 ASSERT (ch == '@');
48 ASSERT (ftell (stdin) == 2);
49
50 ch = fgetc (stdin);
51 ASSERT (ch == '@');
52 ASSERT (ftell (stdin) == 3);
53
54 /* Test ftell after ungetc without read. */
55 ASSERT (fseek (stdin, 0, SEEK_CUR) == 0);
56 ASSERT (ftell (stdin) == 3);
57
58 return 0;
59}