.
[glibc/history.git] / libio / tst-fopenloc2.c
blob5ddd63446bb75f81273d7adcc44b63b65349aea9
1 #include <errno.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <wchar.h>
8 static const struct
10 const char *enc;
11 const char *data;
12 size_t datalen;
13 const wchar_t *expected;
14 size_t expectedlen;
15 } tests[] =
17 { "UCS-4LE", "a\0\0\0b\0\0\0", 8, L"ab", 2 },
18 { "UCS-4BE", "\0\0\0a\0\0\0b", 8, L"ab", 2 },
20 #define ntests (sizeof (tests) / sizeof (tests[0]))
23 static int do_test (void);
24 #define TEST_FUNCTION do_test ()
26 static void prepare (void);
27 #define PREPARE(argc, argv) prepare ();
29 #include "../test-skeleton.c"
32 static int fd;
33 static char *tmpname;
36 static void
37 prepare (void)
39 fd = create_temp_file ("tst-fopenloc2", &tmpname);
40 if (fd == -1)
42 puts ("cannot open temp file");
43 exit (1);
48 static int
49 do_test (void)
51 for (int i = 0; i < ntests; ++i)
53 if (ftruncate (fd, 0) != 0)
55 printf ("ftruncate in round %d failed\n", i + 1);
56 return 1;
59 if (TEMP_FAILURE_RETRY (write (fd, tests[i].data, tests[i].datalen))
60 != tests[i].datalen)
62 printf ("write in round %d failed\n", i + 1);
63 return 1;
66 if (lseek (fd, 0, SEEK_SET) != 0)
68 printf ("lseek in round %d failed\n", i + 1);
69 return 1;
72 char *ccs;
73 if (asprintf (&ccs, "r,ccs=%s", tests[i].enc) == -1)
75 printf ("asprintf in round %d failed\n", i + 1);
76 return 1;
79 FILE *fp = fopen (tmpname, ccs);
80 if (fp == NULL)
82 printf ("fopen in round %d failed\n", i + 1);
83 return 1;
86 #define LINELEN 100
87 wchar_t line[LINELEN];
88 if (fgetws (line, LINELEN, fp) != line)
90 printf ("fgetws in round %d failed\n", i + 1);
91 return 1;
94 if (wcslen (line) != tests[i].expectedlen)
96 printf ("round %d: expected length %zu, got length %zu\n",
97 i + 1, tests[i].expectedlen, wcslen (line));
98 return 1;
101 if (wcscmp (tests[i].expected, line) != 0)
103 printf ("round %d: expected L\"%ls\", got L\"%ls\"\n",
104 i + 1, tests[i].expected, line);
105 return 1;
108 fclose (fp);
110 free (ccs);
113 close (fd);
115 return 0;