2000-07-26 05:09:46 +08:00
|
|
|
/* Test program for the wide character stream functions handling larger
|
|
|
|
amounts of text.
|
2020-01-01 08:14:33 +08:00
|
|
|
Copyright (C) 2000-2020 Free Software Foundation, Inc.
|
2000-07-26 05:09:46 +08:00
|
|
|
This file is part of the GNU C Library.
|
|
|
|
Contributed by Ulrich Drepper <drepper@cygnus.com>.
|
|
|
|
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
2001-07-06 12:58:11 +08:00
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
2000-07-26 05:09:46 +08:00
|
|
|
|
|
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2001-07-06 12:58:11 +08:00
|
|
|
Lesser General Public License for more details.
|
2000-07-26 05:09:46 +08:00
|
|
|
|
2001-07-06 12:58:11 +08:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
2012-02-10 07:18:22 +08:00
|
|
|
License along with the GNU C Library; if not, see
|
Prefer https to http for gnu.org and fsf.org URLs
Also, change sources.redhat.com to sourceware.org.
This patch was automatically generated by running the following shell
script, which uses GNU sed, and which avoids modifying files imported
from upstream:
sed -ri '
s,(http|ftp)(://(.*\.)?(gnu|fsf|sourceware)\.org($|[^.]|\.[^a-z])),https\2,g
s,(http|ftp)(://(.*\.)?)sources\.redhat\.com($|[^.]|\.[^a-z]),https\2sourceware.org\4,g
' \
$(find $(git ls-files) -prune -type f \
! -name '*.po' \
! -name 'ChangeLog*' \
! -path COPYING ! -path COPYING.LIB \
! -path manual/fdl-1.3.texi ! -path manual/lgpl-2.1.texi \
! -path manual/texinfo.tex ! -path scripts/config.guess \
! -path scripts/config.sub ! -path scripts/install-sh \
! -path scripts/mkinstalldirs ! -path scripts/move-if-change \
! -path INSTALL ! -path locale/programs/charmap-kw.h \
! -path po/libc.pot ! -path sysdeps/gnu/errlist.c \
! '(' -name configure \
-execdir test -f configure.ac -o -f configure.in ';' ')' \
! '(' -name preconfigure \
-execdir test -f preconfigure.ac ';' ')' \
-print)
and then by running 'make dist-prepare' to regenerate files built
from the altered files, and then executing the following to cleanup:
chmod a+x sysdeps/unix/sysv/linux/riscv/configure
# Omit irrelevant whitespace and comment-only changes,
# perhaps from a slightly-different Autoconf version.
git checkout -f \
sysdeps/csky/configure \
sysdeps/hppa/configure \
sysdeps/riscv/configure \
sysdeps/unix/sysv/linux/csky/configure
# Omit changes that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/powerpc/powerpc64/ppc-mcount.S: trailing lines
git checkout -f \
sysdeps/powerpc/powerpc64/ppc-mcount.S \
sysdeps/unix/sysv/linux/s390/s390-64/syscall.S
# Omit change that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S: last line does not end in newline
git checkout -f sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S
2019-09-07 13:40:42 +08:00
|
|
|
<https://www.gnu.org/licenses/>. */
|
2000-07-26 05:09:46 +08:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <iconv.h>
|
|
|
|
#include <locale.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <wchar.h>
|
|
|
|
|
|
|
|
/* Approximate size of the file (must be larger). */
|
|
|
|
#define SIZE 210000
|
|
|
|
|
|
|
|
|
2014-11-05 17:54:08 +08:00
|
|
|
static int
|
|
|
|
do_test (void)
|
2000-07-26 05:09:46 +08:00
|
|
|
{
|
|
|
|
char name[] = "/tmp/widetext.out.XXXXXX";
|
|
|
|
char mbbuf[SIZE];
|
|
|
|
char mb2buf[SIZE];
|
|
|
|
wchar_t wcbuf[SIZE];
|
|
|
|
wchar_t wc2buf[SIZE];
|
|
|
|
size_t mbsize;
|
|
|
|
size_t wcsize;
|
|
|
|
int fd;
|
|
|
|
FILE *fp;
|
|
|
|
size_t n;
|
|
|
|
int res;
|
|
|
|
int status = 0;
|
|
|
|
wchar_t *wcp;
|
|
|
|
|
|
|
|
setlocale (LC_ALL, "de_DE.UTF-8");
|
|
|
|
printf ("locale used: %s\n\n", setlocale (LC_ALL, NULL));
|
|
|
|
|
|
|
|
/* Read the file into memory. */
|
|
|
|
mbsize = fread (mbbuf, 1, SIZE, stdin);
|
|
|
|
if (mbsize == 0)
|
|
|
|
{
|
2002-08-25 16:44:43 +08:00
|
|
|
printf ("%u: cannot read input file from standard input: %m\n",
|
2000-07-26 05:09:46 +08:00
|
|
|
__LINE__);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf ("INFO: input file has %Zd bytes\n", mbsize);
|
|
|
|
|
|
|
|
/* First convert the text to wide characters. We use iconv here. */
|
|
|
|
{
|
|
|
|
iconv_t cd;
|
|
|
|
char *inbuf = mbbuf;
|
|
|
|
size_t inleft = mbsize;
|
|
|
|
char *outbuf = (char *) wcbuf;
|
|
|
|
size_t outleft = sizeof (wcbuf);
|
|
|
|
size_t nonr;
|
|
|
|
|
|
|
|
cd = iconv_open ("WCHAR_T", "UTF-8");
|
|
|
|
if (cd == (iconv_t) -1)
|
|
|
|
{
|
2002-08-25 16:44:43 +08:00
|
|
|
printf ("%u: cannot get iconv descriptor for conversion to UCS4\n",
|
2000-07-26 05:09:46 +08:00
|
|
|
__LINE__);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We must need only one call and there must be no losses. */
|
|
|
|
nonr = iconv (cd, &inbuf, &inleft, &outbuf, &outleft);
|
|
|
|
if (nonr != 0 && nonr != (size_t) -1)
|
|
|
|
{
|
2002-08-25 16:44:43 +08:00
|
|
|
printf ("%u: iconv performed %Zd nonreversible conversions\n",
|
2000-07-26 05:09:46 +08:00
|
|
|
__LINE__, nonr);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
2002-09-24 12:24:25 +08:00
|
|
|
if (nonr == (size_t) -1)
|
2000-07-26 05:09:46 +08:00
|
|
|
{
|
|
|
|
printf ("\
|
2002-08-25 16:44:43 +08:00
|
|
|
%u: iconv returned with %Zd and errno = %m (inleft: %Zd, outleft: %Zd)\n",
|
2000-07-26 05:09:46 +08:00
|
|
|
__LINE__, nonr, inleft, outleft);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inleft != 0)
|
|
|
|
{
|
2002-08-25 16:44:43 +08:00
|
|
|
printf ("%u: iconv didn't convert all input\n", __LINE__);
|
2000-07-26 05:09:46 +08:00
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
iconv_close (cd);
|
|
|
|
|
|
|
|
if ((sizeof (wcbuf) - outleft) % sizeof (wchar_t) != 0)
|
|
|
|
{
|
2002-08-25 16:44:43 +08:00
|
|
|
printf ("%u: iconv converted not complete wchar_t\n", __LINE__);
|
2000-07-26 05:09:46 +08:00
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
wcsize = (sizeof (wcbuf) - outleft) / sizeof (wchar_t);
|
|
|
|
assert (wcsize + 1 <= SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now that we finished the preparations, run the first test. We
|
|
|
|
are writing the wide char data out and read it back in. We write
|
|
|
|
and read single characters. */
|
|
|
|
|
|
|
|
fd = mkstemp (name);
|
|
|
|
if (fd == -1)
|
|
|
|
{
|
2002-08-25 16:44:43 +08:00
|
|
|
printf ("%u: cannot open temporary file: %m\n", __LINE__);
|
2000-07-26 05:09:46 +08:00
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
unlink (name);
|
|
|
|
|
|
|
|
fp = fdopen (dup (fd), "w");
|
|
|
|
if (fp == NULL)
|
|
|
|
{
|
2002-08-25 16:44:43 +08:00
|
|
|
printf ("%u: fdopen of temp file for writing failed: %m\n", __LINE__);
|
2000-07-26 05:09:46 +08:00
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (n = 0; n < wcsize; ++n)
|
|
|
|
{
|
|
|
|
if (fputwc (wcbuf[n], fp) == WEOF)
|
|
|
|
{
|
2002-08-25 16:44:43 +08:00
|
|
|
printf ("%u: fputwc failed: %m\n", __LINE__);
|
2000-07-26 05:09:46 +08:00
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res = fclose (fp);
|
|
|
|
if (res != 0)
|
|
|
|
{
|
2002-08-25 16:44:43 +08:00
|
|
|
printf ("%u: fclose after single-character writing failed (%d): %m\n",
|
2000-07-26 05:09:46 +08:00
|
|
|
__LINE__, res);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
lseek (fd, SEEK_SET, 0);
|
|
|
|
fp = fdopen (dup (fd), "r");
|
|
|
|
if (fp == NULL)
|
|
|
|
{
|
2002-08-25 16:44:43 +08:00
|
|
|
printf ("%u: fdopen of temp file for reading failed: %m\n", __LINE__);
|
2000-07-26 05:09:46 +08:00
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (n = 0; n < wcsize; ++n)
|
|
|
|
{
|
|
|
|
wint_t wch = fgetwc (fp);
|
|
|
|
if (wch == WEOF)
|
|
|
|
{
|
2002-08-25 16:44:43 +08:00
|
|
|
printf ("%u: fgetwc failed (idx %Zd): %m\n", __LINE__, n);
|
2000-07-26 05:09:46 +08:00
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
wc2buf[n] = wch;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* There should be nothing else. */
|
|
|
|
if (fgetwc (fp) != WEOF)
|
|
|
|
{
|
2002-08-25 16:44:43 +08:00
|
|
|
printf ("%u: too many characters available with fgetwc\n", __LINE__);
|
2000-07-26 05:09:46 +08:00
|
|
|
status = 1;
|
|
|
|
}
|
|
|
|
else if (wmemcmp (wcbuf, wc2buf, wcsize) != 0)
|
|
|
|
{
|
2002-08-25 16:44:43 +08:00
|
|
|
printf ("%u: buffer read with fgetwc differs\n", __LINE__);
|
2000-07-26 05:09:46 +08:00
|
|
|
status = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = fclose (fp);
|
|
|
|
if (res != 0)
|
|
|
|
{
|
2002-08-25 16:44:43 +08:00
|
|
|
printf ("%u: fclose after single-character reading failed (%d): %m\n",
|
2000-07-26 05:09:46 +08:00
|
|
|
__LINE__, res);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Just make sure there are no two errors which hide each other, read the
|
|
|
|
file using the `char' functions. */
|
|
|
|
|
|
|
|
lseek (fd, SEEK_SET, 0);
|
|
|
|
fp = fdopen (fd, "r");
|
|
|
|
if (fp == NULL)
|
|
|
|
{
|
2002-08-25 16:44:43 +08:00
|
|
|
printf ("%u: fdopen of temp file for reading failed: %m\n", __LINE__);
|
2000-07-26 05:09:46 +08:00
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fread (mb2buf, 1, mbsize, fp) != mbsize)
|
|
|
|
{
|
2002-08-25 16:44:43 +08:00
|
|
|
printf ("%u: cannot read all of the temp file\n", __LINE__);
|
2000-07-26 05:09:46 +08:00
|
|
|
status = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Make sure there is nothing left. */
|
|
|
|
if (fgetc (fp) != EOF)
|
|
|
|
{
|
2002-08-25 16:44:43 +08:00
|
|
|
printf ("%u: more input available\n", __LINE__);
|
2000-07-26 05:09:46 +08:00
|
|
|
status = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (memcmp (mb2buf, mbbuf, mbsize) != 0)
|
|
|
|
{
|
2002-08-25 16:44:43 +08:00
|
|
|
printf ("%u: buffer written with fputwc differs\n", __LINE__);
|
2000-07-26 05:09:46 +08:00
|
|
|
status = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res = fclose (fp);
|
|
|
|
if (res != 0)
|
|
|
|
{
|
2002-08-25 16:44:43 +08:00
|
|
|
printf ("%u: fclose after single-character reading failed (%d): %m\n",
|
2000-07-26 05:09:46 +08:00
|
|
|
__LINE__, res);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now to reading and writing line-wise. */
|
|
|
|
|
|
|
|
fd = mkstemp (strcpy (name, "/tmp/widetext.out.XXXXXX"));
|
|
|
|
if (fd == -1)
|
|
|
|
{
|
2002-08-25 16:44:43 +08:00
|
|
|
printf ("%u: cannot open temporary file: %m\n", __LINE__);
|
2000-07-26 05:09:46 +08:00
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
unlink (name);
|
|
|
|
|
|
|
|
fp = fdopen (dup (fd), "w");
|
|
|
|
if (fp == NULL)
|
|
|
|
{
|
2002-08-25 16:44:43 +08:00
|
|
|
printf ("%u: fdopen of temp file for writing failed: %m\n", __LINE__);
|
2000-07-26 05:09:46 +08:00
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (wcp = wcbuf; wcp < &wcbuf[wcsize]; )
|
|
|
|
{
|
|
|
|
wchar_t *wendp = wcschr (wcp, L'\n');
|
|
|
|
|
|
|
|
if (wendp != NULL)
|
|
|
|
{
|
|
|
|
/* Temporarily NUL terminate the line. */
|
|
|
|
wchar_t save = wendp[1];
|
|
|
|
wendp[1] = L'\0';
|
|
|
|
|
|
|
|
fputws (wcp, fp);
|
|
|
|
|
|
|
|
wendp[1] = save;
|
|
|
|
wcp = &wendp[1];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fputws (wcp, fp);
|
|
|
|
wcp = wcschr (wcp, L'\0');
|
|
|
|
assert (wcp == &wcbuf[wcsize]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res = fclose (fp);
|
|
|
|
if (res != 0)
|
|
|
|
{
|
2002-08-25 16:44:43 +08:00
|
|
|
printf ("%u: fclose after line-wise writing failed (%d): %m\n",
|
2000-07-26 05:09:46 +08:00
|
|
|
__LINE__, res);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
lseek (fd, SEEK_SET, 0);
|
|
|
|
fp = fdopen (dup (fd), "r");
|
|
|
|
if (fp == NULL)
|
|
|
|
{
|
2002-08-25 16:44:43 +08:00
|
|
|
printf ("%u: fdopen of temp file for reading failed: %m\n", __LINE__);
|
2000-07-26 05:09:46 +08:00
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (wcp = wc2buf; wcp < &wc2buf[wcsize]; )
|
|
|
|
{
|
|
|
|
if (fgetws (wcp, &wc2buf[wcsize] - wcp + 1, fp) == NULL)
|
|
|
|
{
|
2014-12-17 23:42:51 +08:00
|
|
|
printf ("%u: short read using fgetws (only %td of %Zd)\n",
|
2000-07-26 05:09:46 +08:00
|
|
|
__LINE__, wcp - wc2buf, wcsize);
|
|
|
|
status = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
wcp = wcschr (wcp, L'\0');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wcp > &wc2buf[wcsize])
|
|
|
|
{
|
2002-08-25 16:44:43 +08:00
|
|
|
printf ("%u: fgetws read too much\n", __LINE__);
|
2000-07-26 05:09:46 +08:00
|
|
|
status = 1;
|
|
|
|
}
|
|
|
|
else if (fgetwc (fp) != WEOF)
|
|
|
|
{
|
|
|
|
/* There should be nothing else. */
|
2002-08-25 16:44:43 +08:00
|
|
|
printf ("%u: too many characters available with fgetws\n", __LINE__);
|
2000-07-26 05:09:46 +08:00
|
|
|
status = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wcp >= &wc2buf[wcsize] && wmemcmp (wcbuf, wc2buf, wcsize) != 0)
|
|
|
|
{
|
2002-08-25 16:44:43 +08:00
|
|
|
printf ("%u: buffer read with fgetws differs\n", __LINE__);
|
2000-07-26 05:09:46 +08:00
|
|
|
status = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = fclose (fp);
|
|
|
|
if (res != 0)
|
|
|
|
{
|
2002-08-25 16:44:43 +08:00
|
|
|
printf ("%u: fclose after single-character reading failed (%d): %m\n",
|
2000-07-26 05:09:46 +08:00
|
|
|
__LINE__, res);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Just make sure there are no two errors which hide each other, read the
|
|
|
|
file using the `char' functions. */
|
|
|
|
|
|
|
|
lseek (fd, SEEK_SET, 0);
|
|
|
|
fp = fdopen (fd, "r");
|
|
|
|
if (fp == NULL)
|
|
|
|
{
|
2002-08-25 16:44:43 +08:00
|
|
|
printf ("%u: fdopen of temp file for reading failed: %m\n", __LINE__);
|
2000-07-26 05:09:46 +08:00
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fread (mb2buf, 1, mbsize, fp) != mbsize)
|
|
|
|
{
|
2002-08-25 16:44:43 +08:00
|
|
|
printf ("%u: cannot read all of the temp file\n", __LINE__);
|
2000-07-26 05:09:46 +08:00
|
|
|
status = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Make sure there is nothing left. */
|
|
|
|
if (fgetc (fp) != EOF)
|
|
|
|
{
|
2002-08-25 16:44:43 +08:00
|
|
|
printf ("%u: more input available\n", __LINE__);
|
2000-07-26 05:09:46 +08:00
|
|
|
status = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (memcmp (mb2buf, mbbuf, mbsize) != 0)
|
|
|
|
{
|
2002-08-25 16:44:43 +08:00
|
|
|
printf ("%u: buffer written with fputws differs\n", __LINE__);
|
2000-07-26 05:09:46 +08:00
|
|
|
status = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res = fclose (fp);
|
|
|
|
if (res != 0)
|
|
|
|
{
|
2002-08-25 16:44:43 +08:00
|
|
|
printf ("%u: fclose after single-character reading failed (%d): %m\n",
|
2000-07-26 05:09:46 +08:00
|
|
|
__LINE__, res);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
2014-11-05 17:54:08 +08:00
|
|
|
|
|
|
|
#define TEST_FUNCTION do_test ()
|
|
|
|
#include "../test-skeleton.c"
|