Ticket #3145: fseek-bug.c

File fseek-bug.c, 900 bytes (added by bhaible, 15 years ago)

test case

Line 
1/* Test of fseek() 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 ASSERT (fseek (stdin, 0, SEEK_CUR) == 0);
23 /* Test that fseek discards previously read ungetc data. */
24 ch = fgetc (stdin);
25 ASSERT (ch == '/');
26 ASSERT (ungetc (ch, stdin) == ch);
27 ASSERT (fseek (stdin, 2, SEEK_SET) == 0);
28 /* Test that fseek discards random ungetc data. */
29 ch = fgetc (stdin);
30 ASSERT (ch == ' ');
31 ASSERT (ungetc (ch ^ 0xff, stdin) == (ch ^ 0xff));
32 ASSERT (fseek (stdin, 0, SEEK_END) == 0);
33 ASSERT (fgetc (stdin) == EOF);
34 return 0;
35}