1 /* Test program for the wide character stream functions handling larger
3 Copyright (C) 2000-2024 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <https://www.gnu.org/licenses/>. */
29 /* Approximate size of the file (must be larger). */
36 char name
[] = "/tmp/widetext.out.XXXXXX";
50 setlocale (LC_ALL
, "de_DE.UTF-8");
51 printf ("locale used: %s\n\n", setlocale (LC_ALL
, NULL
));
53 /* Read the file into memory. */
54 mbsize
= fread (mbbuf
, 1, SIZE
, stdin
);
57 printf ("%u: cannot read input file from standard input: %m\n",
62 printf ("INFO: input file has %zd bytes\n", mbsize
);
64 /* First convert the text to wide characters. We use iconv here. */
68 size_t inleft
= mbsize
;
69 char *outbuf
= (char *) wcbuf
;
70 size_t outleft
= sizeof (wcbuf
);
73 cd
= iconv_open ("WCHAR_T", "UTF-8");
74 if (cd
== (iconv_t
) -1)
76 printf ("%u: cannot get iconv descriptor for conversion to UCS4\n",
81 /* We must need only one call and there must be no losses. */
82 nonr
= iconv (cd
, &inbuf
, &inleft
, &outbuf
, &outleft
);
83 if (nonr
!= 0 && nonr
!= (size_t) -1)
85 printf ("%u: iconv performed %zd nonreversible conversions\n",
90 if (nonr
== (size_t) -1)
93 %u: iconv returned with %zd and errno = %m (inleft: %zd, outleft: %zd)\n",
94 __LINE__
, nonr
, inleft
, outleft
);
100 printf ("%u: iconv didn't convert all input\n", __LINE__
);
106 if ((sizeof (wcbuf
) - outleft
) % sizeof (wchar_t) != 0)
108 printf ("%u: iconv converted not complete wchar_t\n", __LINE__
);
112 wcsize
= (sizeof (wcbuf
) - outleft
) / sizeof (wchar_t);
113 assert (wcsize
+ 1 <= SIZE
);
116 /* Now that we finished the preparations, run the first test. We
117 are writing the wide char data out and read it back in. We write
118 and read single characters. */
123 printf ("%u: cannot open temporary file: %m\n", __LINE__
);
129 fp
= fdopen (dup (fd
), "w");
132 printf ("%u: fdopen of temp file for writing failed: %m\n", __LINE__
);
136 for (n
= 0; n
< wcsize
; ++n
)
138 if (fputwc (wcbuf
[n
], fp
) == WEOF
)
140 printf ("%u: fputwc failed: %m\n", __LINE__
);
148 printf ("%u: fclose after single-character writing failed (%d): %m\n",
153 lseek (fd
, SEEK_SET
, 0);
154 fp
= fdopen (dup (fd
), "r");
157 printf ("%u: fdopen of temp file for reading failed: %m\n", __LINE__
);
161 for (n
= 0; n
< wcsize
; ++n
)
163 wint_t wch
= fgetwc (fp
);
166 printf ("%u: fgetwc failed (idx %zd): %m\n", __LINE__
, n
);
172 /* There should be nothing else. */
173 if (fgetwc (fp
) != WEOF
)
175 printf ("%u: too many characters available with fgetwc\n", __LINE__
);
178 else if (wmemcmp (wcbuf
, wc2buf
, wcsize
) != 0)
180 printf ("%u: buffer read with fgetwc differs\n", __LINE__
);
187 printf ("%u: fclose after single-character reading failed (%d): %m\n",
192 /* Just make sure there are no two errors which hide each other, read the
193 file using the `char' functions. */
195 lseek (fd
, SEEK_SET
, 0);
196 fp
= fdopen (fd
, "r");
199 printf ("%u: fdopen of temp file for reading failed: %m\n", __LINE__
);
203 if (fread (mb2buf
, 1, mbsize
, fp
) != mbsize
)
205 printf ("%u: cannot read all of the temp file\n", __LINE__
);
210 /* Make sure there is nothing left. */
211 if (fgetc (fp
) != EOF
)
213 printf ("%u: more input available\n", __LINE__
);
217 if (memcmp (mb2buf
, mbbuf
, mbsize
) != 0)
219 printf ("%u: buffer written with fputwc differs\n", __LINE__
);
227 printf ("%u: fclose after single-character reading failed (%d): %m\n",
232 /* Now to reading and writing line-wise. */
234 fd
= mkstemp (strcpy (name
, "/tmp/widetext.out.XXXXXX"));
237 printf ("%u: cannot open temporary file: %m\n", __LINE__
);
243 fp
= fdopen (dup (fd
), "w");
246 printf ("%u: fdopen of temp file for writing failed: %m\n", __LINE__
);
250 for (wcp
= wcbuf
; wcp
< &wcbuf
[wcsize
]; )
252 wchar_t *wendp
= wcschr (wcp
, L
'\n');
256 /* Temporarily NUL terminate the line. */
257 wchar_t save
= wendp
[1];
268 wcp
= wcschr (wcp
, L
'\0');
269 assert (wcp
== &wcbuf
[wcsize
]);
276 printf ("%u: fclose after line-wise writing failed (%d): %m\n",
281 lseek (fd
, SEEK_SET
, 0);
282 fp
= fdopen (dup (fd
), "r");
285 printf ("%u: fdopen of temp file for reading failed: %m\n", __LINE__
);
289 for (wcp
= wc2buf
; wcp
< &wc2buf
[wcsize
]; )
291 if (fgetws (wcp
, &wc2buf
[wcsize
] - wcp
+ 1, fp
) == NULL
)
293 printf ("%u: short read using fgetws (only %td of %zd)\n",
294 __LINE__
, wcp
- wc2buf
, wcsize
);
298 wcp
= wcschr (wcp
, L
'\0');
301 if (wcp
> &wc2buf
[wcsize
])
303 printf ("%u: fgetws read too much\n", __LINE__
);
306 else if (fgetwc (fp
) != WEOF
)
308 /* There should be nothing else. */
309 printf ("%u: too many characters available with fgetws\n", __LINE__
);
313 if (wcp
>= &wc2buf
[wcsize
] && wmemcmp (wcbuf
, wc2buf
, wcsize
) != 0)
315 printf ("%u: buffer read with fgetws differs\n", __LINE__
);
322 printf ("%u: fclose after single-character reading failed (%d): %m\n",
327 /* Just make sure there are no two errors which hide each other, read the
328 file using the `char' functions. */
330 lseek (fd
, SEEK_SET
, 0);
331 fp
= fdopen (fd
, "r");
334 printf ("%u: fdopen of temp file for reading failed: %m\n", __LINE__
);
338 if (fread (mb2buf
, 1, mbsize
, fp
) != mbsize
)
340 printf ("%u: cannot read all of the temp file\n", __LINE__
);
345 /* Make sure there is nothing left. */
346 if (fgetc (fp
) != EOF
)
348 printf ("%u: more input available\n", __LINE__
);
352 if (memcmp (mb2buf
, mbbuf
, mbsize
) != 0)
354 printf ("%u: buffer written with fputws differs\n", __LINE__
);
362 printf ("%u: fclose after single-character reading failed (%d): %m\n",
370 #define TEST_FUNCTION do_test ()
371 #include "../test-skeleton.c"