1 | /* Test program for fsetpos on a wide character stream. */ |
---|---|
2 | |
3 | #include <assert.h> |
4 | #include <stdio.h> |
5 | #include <wchar.h> |
6 | |
7 | static void do_prepare (void); |
8 | #define PREPARE(argc, argv) do_prepare () |
9 | static int do_test (void); |
10 | #define TEST_FUNCTION do_test () |
11 | #include <test-skeleton.c> |
12 | |
13 | static const char pattern[] = "12345"; |
14 | static char *temp_file; |
15 | |
16 | static void |
17 | do_prepare (void) |
18 | { |
19 | int fd = create_temp_file (base: "bug-wsetpos.", filename: &temp_file); |
20 | if (fd == -1) |
21 | { |
22 | printf (format: "cannot create temporary file: %m\n"); |
23 | exit (1); |
24 | } |
25 | write (fd, pattern, sizeof (pattern)); |
26 | close (fd: fd); |
27 | } |
28 | |
29 | static int |
30 | do_test (void) |
31 | { |
32 | FILE *fp = fopen (temp_file, "r"); |
33 | fpos_t pos; |
34 | wchar_t c; |
35 | |
36 | if (fp == NULL) |
37 | { |
38 | printf (format: "fdopen: %m\n"); |
39 | return 1; |
40 | } |
41 | |
42 | c = fgetwc (stream: fp); assert (c == L'1'); |
43 | c = fgetwc (stream: fp); assert (c == L'2'); |
44 | |
45 | if (fgetpos (fp, &pos) == EOF) |
46 | { |
47 | printf (format: "fgetpos: %m\n"); |
48 | return 1; |
49 | } |
50 | |
51 | rewind (fp); |
52 | if (ferror (stream: fp)) |
53 | { |
54 | printf (format: "rewind: %m\n"); |
55 | return 1; |
56 | } |
57 | |
58 | c = fgetwc (stream: fp); assert (c == L'1'); |
59 | |
60 | if (fsetpos (fp, &pos) == EOF) |
61 | { |
62 | printf (format: "fsetpos: %m\n"); |
63 | return 1; |
64 | } |
65 | |
66 | c = fgetwc (stream: fp); |
67 | if (c != L'3') |
68 | { |
69 | puts (s: "fsetpos failed"); |
70 | return 1; |
71 | } |
72 | |
73 | puts (s: "Test succeeded."); |
74 | return 0; |
75 | } |
76 |