glibc/libio/tst-widetext.c
Arjun Shankar 29955b5d96 Modify several tests to use test-skeleton.c
This patch modifies several test cases to use test-skeleton.c.
It was generated by a bash script written for this purpose and
thus excludes several other tests which I deemed worth a visual
inspection before making the change.

I intend to follow up with individual patches to the tests
skipped by the script.

The script itself resides at http://git.io/WODAmg and should
reproduce this very patch when run against master.

ChangeLog:

2014-10-30  Arjun Shankar  <arjun.is@lostca.se>

	* catgets/test-gencat.c: Use test-skeleton.c.
	* catgets/tst-catgets.c: Likewise.
	* csu/tst-empty.c: Likewise.
	* elf/tst-audit2.c: Likewise.
	* elf/tst-global1.c: Likewise.
	* elf/tst-pathopt.c: Likewise.
	* elf/tst-piemod1.c: Likewise.
	* elf/tst-tls10.c: Likewise.
	* elf/tst-tls11.c: Likewise.
	* elf/tst-tls12.c: Likewise.
	* gnulib/tst-gcc.c: Likewise.
	* iconvdata/tst-e2big.c: Likewise.
	* iconvdata/tst-loading.c: Likewise.
	* iconv/tst-iconv1.c: Likewise.
	* iconv/tst-iconv2.c: Likewise.
	* inet/test-inet6_opt.c: Likewise.
	* inet/tst-gethnm.c: Likewise.
	* inet/tst-network.c: Likewise.
	* inet/tst-ntoa.c: Likewise.
	* intl/tst-codeset.c: Likewise.
	* intl/tst-gettext2.c: Likewise.
	* intl/tst-gettext3.c: Likewise.
	* intl/tst-ngettext.c: Likewise.
	* intl/tst-translit.c: Likewise.
	* io/test-stat.c: Likewise.
	* libio/test-fmemopen.c: Likewise.
	* libio/tst-freopen.c: Likewise.
	* libio/tst-sscanf.c: Likewise.
	* libio/tst-ungetwc1.c: Likewise.
	* libio/tst-ungetwc2.c: Likewise.
	* libio/tst-widetext.c: Likewise.
	* localedata/tst-ctype.c: Likewise.
	* localedata/tst-digits.c: Likewise.
	* localedata/tst-leaks.c: Likewise.
	* localedata/tst-mbswcs1.c: Likewise.
	* localedata/tst-mbswcs2.c: Likewise.
	* localedata/tst-mbswcs3.c: Likewise.
	* localedata/tst-mbswcs4.c: Likewise.
	* localedata/tst-mbswcs5.c: Likewise.
	* localedata/tst-setlocale.c: Likewise.
	* localedata/tst-trans.c: Likewise.
	* localedata/tst-wctype.c: Likewise.
	* localedata/tst-xlocale1.c: Likewise.
	* login/tst-grantpt.c: Likewise.
	* malloc/tst-calloc.c: Likewise.
	* malloc/tst-malloc.c: Likewise.
	* malloc/tst-mallocstate.c: Likewise.
	* malloc/tst-mcheck.c: Likewise.
	* malloc/tst-mtrace.c: Likewise.
	* malloc/tst-obstack.c: Likewise.
	* math/atest-exp2.c: Likewise.
	* math/atest-exp.c: Likewise.
	* math/atest-sincos.c: Likewise.
	* math/test-matherr.c: Likewise.
	* math/test-misc.c: Likewise.
	* math/test-powl.c: Likewise.
	* math/tst-definitions.c: Likewise.
	* misc/tst-dirname.c: Likewise.
	* misc/tst-efgcvt.c: Likewise.
	* misc/tst-fdset.c: Likewise.
	* misc/tst-hsearch.c: Likewise.
	* misc/tst-mntent2.c: Likewise.
	* nptl/tst-sem7.c: Likewise.
	* nptl/tst-sem8.c: Likewise.
	* nptl/tst-sem9.c: Likewise.
	* nss/test-netdb.c: Likewise.
	* posix/tst-fnmatch.c: Likewise.
	* posix/tst-getlogin.c: Likewise.
	* posix/tst-gnuglob.c: Likewise.
	* posix/tst-mmap.c: Likewise.
	* pwd/tst-getpw.c: Likewise.
	* resolv/tst-inet_ntop.c: Likewise.
	* rt/tst-timer.c: Likewise.
	* stdio-common/test-fseek.c: Likewise.
	* stdio-common/test-popen.c: Likewise.
	* stdio-common/test-vfprintf.c: Likewise.
	* stdio-common/tst-cookie.c: Likewise.
	* stdio-common/tst-fileno.c: Likewise.
	* stdio-common/tst-gets.c: Likewise.
	* stdio-common/tst-obprintf.c: Likewise.
	* stdio-common/tst-perror.c: Likewise.
	* stdio-common/tst-sprintf2.c: Likewise.
	* stdio-common/tst-sprintf3.c: Likewise.
	* stdio-common/tst-sprintf.c: Likewise.
	* stdio-common/tst-swprintf.c: Likewise.
	* stdio-common/tst-tmpnam.c: Likewise.
	* stdio-common/tst-unbputc.c: Likewise.
	* stdio-common/tst-wc-printf.c: Likewise.
	* stdlib/tst-environ.c: Likewise.
	* stdlib/tst-fmtmsg.c: Likewise.
	* stdlib/tst-limits.c: Likewise.
	* stdlib/tst-rand48-2.c: Likewise.
	* stdlib/tst-rand48.c: Likewise.
	* stdlib/tst-random2.c: Likewise.
	* stdlib/tst-random.c: Likewise.
	* stdlib/tst-strtol.c: Likewise.
	* stdlib/tst-strtoll.c: Likewise.
	* stdlib/tst-tls-atexit.c: Likewise.
	* stdlib/tst-xpg-basename.c: Likewise.
	* string/test-ffs.c: Likewise.
	* string/tst-bswap.c: Likewise.
	* string/tst-inlcall.c: Likewise.
	* string/tst-strtok.c: Likewise.
	* string/tst-strxfrm.c: Likewise.
	* sysdeps/x86_64/tst-audit10.c: Likewise.
	* sysdeps/x86_64/tst-audit3.c: Likewise.
	* sysdeps/x86_64/tst-audit4.c: Likewise.
	* sysdeps/x86_64/tst-audit5.c: Likewise.
	* time/tst-ftime_l.c: Likewise.
	* time/tst-getdate.c: Likewise.
	* time/tst-mktime3.c: Likewise.
	* time/tst-mktime.c: Likewise.
	* time/tst-posixtz.c: Likewise.
	* time/tst-strptime2.c: Likewise.
	* time/tst-strptime3.c: Likewise.
	* wcsmbs/tst-btowc.c: Likewise.
	* wcsmbs/tst-mbrtowc.c: Likewise.
	* wcsmbs/tst-mbsrtowcs.c: Likewise.
	* wcsmbs/tst-wchar-h.c: Likewise.
	* wcsmbs/tst-wcpncpy.c: Likewise.
	* wcsmbs/tst-wcrtomb.c: Likewise.
	* wcsmbs/tst-wcsnlen.c: Likewise.
	* wcsmbs/tst-wcstof.c: Likewise.
2014-11-05 15:24:08 +05:30

373 lines
8.5 KiB
C

/* Test program for the wide character stream functions handling larger
amounts of text.
Copyright (C) 2000-2014 Free Software Foundation, Inc.
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
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.
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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<http://www.gnu.org/licenses/>. */
#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
static int
do_test (void)
{
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)
{
printf ("%u: cannot read input file from standard input: %m\n",
__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)
{
printf ("%u: cannot get iconv descriptor for conversion to UCS4\n",
__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)
{
printf ("%u: iconv performed %Zd nonreversible conversions\n",
__LINE__, nonr);
exit (1);
}
if (nonr == (size_t) -1)
{
printf ("\
%u: iconv returned with %Zd and errno = %m (inleft: %Zd, outleft: %Zd)\n",
__LINE__, nonr, inleft, outleft);
exit (1);
}
if (inleft != 0)
{
printf ("%u: iconv didn't convert all input\n", __LINE__);
exit (1);
}
iconv_close (cd);
if ((sizeof (wcbuf) - outleft) % sizeof (wchar_t) != 0)
{
printf ("%u: iconv converted not complete wchar_t\n", __LINE__);
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)
{
printf ("%u: cannot open temporary file: %m\n", __LINE__);
exit (1);
}
unlink (name);
fp = fdopen (dup (fd), "w");
if (fp == NULL)
{
printf ("%u: fdopen of temp file for writing failed: %m\n", __LINE__);
exit (1);
}
for (n = 0; n < wcsize; ++n)
{
if (fputwc (wcbuf[n], fp) == WEOF)
{
printf ("%u: fputwc failed: %m\n", __LINE__);
exit (1);
}
}
res = fclose (fp);
if (res != 0)
{
printf ("%u: fclose after single-character writing failed (%d): %m\n",
__LINE__, res);
exit (1);
}
lseek (fd, SEEK_SET, 0);
fp = fdopen (dup (fd), "r");
if (fp == NULL)
{
printf ("%u: fdopen of temp file for reading failed: %m\n", __LINE__);
exit (1);
}
for (n = 0; n < wcsize; ++n)
{
wint_t wch = fgetwc (fp);
if (wch == WEOF)
{
printf ("%u: fgetwc failed (idx %Zd): %m\n", __LINE__, n);
exit (1);
}
wc2buf[n] = wch;
}
/* There should be nothing else. */
if (fgetwc (fp) != WEOF)
{
printf ("%u: too many characters available with fgetwc\n", __LINE__);
status = 1;
}
else if (wmemcmp (wcbuf, wc2buf, wcsize) != 0)
{
printf ("%u: buffer read with fgetwc differs\n", __LINE__);
status = 1;
}
res = fclose (fp);
if (res != 0)
{
printf ("%u: fclose after single-character reading failed (%d): %m\n",
__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)
{
printf ("%u: fdopen of temp file for reading failed: %m\n", __LINE__);
exit (1);
}
if (fread (mb2buf, 1, mbsize, fp) != mbsize)
{
printf ("%u: cannot read all of the temp file\n", __LINE__);
status = 1;
}
else
{
/* Make sure there is nothing left. */
if (fgetc (fp) != EOF)
{
printf ("%u: more input available\n", __LINE__);
status = 1;
}
if (memcmp (mb2buf, mbbuf, mbsize) != 0)
{
printf ("%u: buffer written with fputwc differs\n", __LINE__);
status = 1;
}
}
res = fclose (fp);
if (res != 0)
{
printf ("%u: fclose after single-character reading failed (%d): %m\n",
__LINE__, res);
exit (1);
}
/* Now to reading and writing line-wise. */
fd = mkstemp (strcpy (name, "/tmp/widetext.out.XXXXXX"));
if (fd == -1)
{
printf ("%u: cannot open temporary file: %m\n", __LINE__);
exit (1);
}
unlink (name);
fp = fdopen (dup (fd), "w");
if (fp == NULL)
{
printf ("%u: fdopen of temp file for writing failed: %m\n", __LINE__);
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)
{
printf ("%u: fclose after line-wise writing failed (%d): %m\n",
__LINE__, res);
exit (1);
}
lseek (fd, SEEK_SET, 0);
fp = fdopen (dup (fd), "r");
if (fp == NULL)
{
printf ("%u: fdopen of temp file for reading failed: %m\n", __LINE__);
exit (1);
}
for (wcp = wc2buf; wcp < &wc2buf[wcsize]; )
{
if (fgetws (wcp, &wc2buf[wcsize] - wcp + 1, fp) == NULL)
{
printf ("%u: short read using fgetws (only %Zd of %Zd)\n",
__LINE__, wcp - wc2buf, wcsize);
status = 1;
break;
}
wcp = wcschr (wcp, L'\0');
}
if (wcp > &wc2buf[wcsize])
{
printf ("%u: fgetws read too much\n", __LINE__);
status = 1;
}
else if (fgetwc (fp) != WEOF)
{
/* There should be nothing else. */
printf ("%u: too many characters available with fgetws\n", __LINE__);
status = 1;
}
if (wcp >= &wc2buf[wcsize] && wmemcmp (wcbuf, wc2buf, wcsize) != 0)
{
printf ("%u: buffer read with fgetws differs\n", __LINE__);
status = 1;
}
res = fclose (fp);
if (res != 0)
{
printf ("%u: fclose after single-character reading failed (%d): %m\n",
__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)
{
printf ("%u: fdopen of temp file for reading failed: %m\n", __LINE__);
exit (1);
}
if (fread (mb2buf, 1, mbsize, fp) != mbsize)
{
printf ("%u: cannot read all of the temp file\n", __LINE__);
status = 1;
}
else
{
/* Make sure there is nothing left. */
if (fgetc (fp) != EOF)
{
printf ("%u: more input available\n", __LINE__);
status = 1;
}
if (memcmp (mb2buf, mbbuf, mbsize) != 0)
{
printf ("%u: buffer written with fputws differs\n", __LINE__);
status = 1;
}
}
res = fclose (fp);
if (res != 0)
{
printf ("%u: fclose after single-character reading failed (%d): %m\n",
__LINE__, res);
exit (1);
}
return status;
}
#define TEST_FUNCTION do_test ()
#include "../test-skeleton.c"