* added a couple of wchar-related tests (from glibc-2.3.2) that expose

several problems in our wchar-support - the worst of which being that
  our compilers define wchar_t as short while the glibc-implementation
  is relying on it being 32-bits wide ...



git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@31180 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Oliver Tappe
2009-06-22 19:13:32 +00:00
parent 77fdfdfe5f
commit 8ce2ca0fc7
20 changed files with 1534 additions and 0 deletions
+68
View File
@@ -72,6 +72,74 @@ SimpleTest mbtest
: mbtest.c
;
SimpleTest tst-btowc
: tst-btowc.c
;
SimpleTest tst-fgetws
: tst-fgetws.c
;
SimpleTest tst-getwc
: tst-getwc.c
;
SimpleTest tst-mbrtowc
: tst-mbrtowc.c
;
SimpleTest tst-swprintf
: tst-swprintf.c
;
#SimpleTest tst-swcanf
# : tst-swscanf.c
#;
#SimpleTest tst-swscanf2
# : tst-swscanf2.c
#;
SimpleTest tst-ungetwc1
: tst-ungetwc1.c
;
SimpleTest tst-ungetwc2
: tst-ungetwc2.c
;
SimpleTest tst-wcrtomb
: tst-wcrtomb.c
;
SimpleTest tst-wcsnlen
: tst-wcsnlen.c
;
SimpleTest tst-wcstof
: tst-wcstof.c
;
SimpleTest tst-wprintf
: tst-wprintf.c
;
SimpleTest tst-wprintf2
: tst-wprintf2.c
;
#SimpleTest tst-wscanf
# : tst-wscanf.c
#;
SimpleTest test_wcfuncs
: test_wcfuncs.c
;
SimpleTest test_wctype
: test_wctype.c
;
# Tell Jam where to find these sources
SEARCH on [ FGristFiles
syslog.cpp
@@ -0,0 +1,83 @@
/* Copyright (C) 1999, 2000 Free Software Foundation, Inc.
This file is part of the GNU C Library.
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, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA. */
#include <stdio.h>
#include <stdlib.h>
#include <wctype.h>
int
main(int argc, char *argv[])
{
int result = 0;
wint_t ch;
for (ch = 0; ch < 128; ++ch) {
if (iswlower(ch)) {
/* Get corresponding upper case character. */
wint_t up = towupper(ch);
/* This should have no effect. */
wint_t low = towlower(ch);
if ((ch != low) || (up == ch) || (up == low)) {
printf(
"iswlower/towupper/towlower for character \\%x failed\n",
ch);
result++;
}
}
if (iswupper(ch)) {
/* Get corresponding lower case character. */
wint_t low = towlower(ch);
/* This should have no effect. */
wint_t up = towupper(ch);
if ((ch != up) || (low == ch) || (up == low)) {
printf(
"iswupper/towlower/towupper for character \\%x failed\n",
ch);
result++;
}
}
}
/* Finally some specific tests. */
ch = L'A';
if (!iswupper(ch) || iswlower(ch)) {
printf("!iswupper/iswlower (L'A') failed\n");
result++;
}
ch = L'a';
if (iswupper(ch) || !iswlower(ch)) {
printf("iswupper/!iswlower (L'a') failed\n");
result++;
}
if (towlower(L'A') != L'a') {
printf("towlower(L'A') failed\n");
result++;
}
if (towupper(L'a') != L'A') {
printf("towupper(L'a') failed\n");
result++;
}
if (result == 0)
puts("All test successful!");
return result != 0;
}
@@ -0,0 +1,78 @@
/* Copyright (C) 1996, 1997, 2000 Free Software Foundation, Inc.
This file is part of the GNU C Library.
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, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA. */
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <wctype.h>
#define TEST(test) \
do { \
if ((is##test (ch) == 0) != (iswctype (ch, bit_##test) == 0)) { \
printf("`iswctype' class `%s' test for character \\%o failed\n",\
#test, ch); \
result = 1; \
} \
if ((is##test (ch) == 0) != (isw##test (ch) == 0)) { \
printf ("`isw%s' test for character \\%o failed\n", #test, ch); \
result = 1; \
} \
} while (0)
int
main(int argc, char *argv[])
{
int result = 0;
wctype_t bit_alnum = wctype("alnum");
wctype_t bit_alpha = wctype("alpha");
wctype_t bit_cntrl = wctype("cntrl");
wctype_t bit_digit = wctype("digit");
wctype_t bit_graph = wctype("graph");
wctype_t bit_lower = wctype("lower");
wctype_t bit_print = wctype("print");
wctype_t bit_punct = wctype("punct");
wctype_t bit_space = wctype("space");
wctype_t bit_upper = wctype("upper");
wctype_t bit_xdigit = wctype("xdigit");
int ch;
if (wctype("does not exist") != 0) {
puts("wctype return value != 0 for non existing property");
result = 1;
}
for (ch = 0; ch < 256; ++ch) {
TEST (alnum);
TEST (alpha);
TEST (cntrl);
TEST (digit);
TEST (graph);
TEST (lower);
TEST (print);
TEST (punct);
TEST (space);
TEST (upper);
TEST (xdigit);
}
if (result == 0)
puts("All tests successful!");
return result;
}
+204
View File
@@ -0,0 +1,204 @@
/* Copyright (C) 2000 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Ulrich Drepper <[email protected]>, 2000.
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, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA. */
#include <locale.h>
#include <stdio.h>
#include <wchar.h>
/* Currently selected locale. */
static const char *current_locale;
/* Test which should succeed. */
static int
ok_test(int c, wint_t expwc)
{
wint_t wc = btowc(c);
if (wc != expwc) {
printf(
"%s: btowc('%c') failed, returned L'\\x%x' instead of L'\\x%x'\n",
current_locale, c, wc, expwc);
return 1;
}
return 0;
}
/* Test which should fail. */
static int
fail_test(int c)
{
wint_t wc = btowc(c);
if (wc != WEOF) {
printf("%s: btowc('%c') succeded, returned L'\\x%x' instead of WEOF\n",
current_locale, c, wc);
return 1;
}
return 0;
}
/* Test EOF handling. */
static int
eof_test(void)
{
wint_t wc = btowc(EOF);
if (wc != WEOF) {
printf("%s: btowc(EOF) returned L'\\x%x', not WEOF\n", current_locale,
wc);
}
return 0;
}
/* Test the btowc() function for a few locales with known character sets. */
int
main(void)
{
int totalResult = 0;
printf("WEOF=0x%08x\n", WEOF);
printf("WCHAR_MIN=0x%08lx\n", WCHAR_MIN);
printf("WCHAR_MAX=0x%08lx\n", WCHAR_MAX);
current_locale = setlocale(LC_ALL, "C");
if (current_locale == NULL) {
puts("cannot set locale \"C\"");
totalResult = 1;
} else {
int c;
int result = 0;
for (c = 0; c < 128; ++c)
result |= ok_test(c, c);
for (c = 128; c < 256; ++c)
result |= fail_test(c);
result |= eof_test();
totalResult |= result;
if (result == 0)
printf("locale '%s' ok\n", current_locale);
}
current_locale = setlocale(LC_ALL, "en_US.ANSI_X3.4-1968");
if (current_locale == NULL) {
puts("cannot set locale \"en_US.ANSI_X3.4-1968\"");
totalResult = 1;
} else {
int c;
int result = 0;
for (c = 0; c < 128; ++c)
result |= ok_test(c, c);
for (c = 128; c < 256; ++c)
result |= fail_test(c);
result |= eof_test();
totalResult |= result;
if (result == 0)
printf("locale '%s' ok\n", current_locale);
}
current_locale = setlocale(LC_ALL, "de_DE.ISO-8859-1");
if (current_locale == NULL) {
puts("cannot set locale \"de_DE.ISO-8859-1\"");
totalResult = 1;
} else {
int c;
int result = 0;
for (c = 0; c < 256; ++c)
result |= ok_test(c, c);
result |= eof_test();
totalResult |= result;
if (result == 0)
printf("locale '%s' ok\n", current_locale);
}
current_locale = setlocale(LC_ALL, "de_DE.UTF-8");
if (current_locale == NULL) {
puts("cannot set locale \"de_DE.UTF-8\"");
totalResult = 1;
} else {
int c;
int result = 0;
for (c = 0; c < 128; ++c)
result |= ok_test(c, c);
for (c = 128; c < 256; ++c)
result |= fail_test(c);
result |= eof_test();
totalResult |= result;
if (result == 0)
printf("locale '%s' ok\n", current_locale);
}
current_locale = setlocale(LC_ALL, "hr_HR.ISO-8859-2");
if (current_locale == NULL) {
puts("cannot set locale \"hr_HR.ISO-8859-2\"");
totalResult = 1;
} else {
static const wint_t upper_half[] = { 0x0104, 0x02D8, 0x0141, 0x00A4,
0x013D, 0x015A, 0x00A7, 0x00A8, 0x0160, 0x015E, 0x0164, 0x0179,
0x00AD, 0x017D, 0x017B, 0x00B0, 0x0105, 0x02DB, 0x0142, 0x00B4,
0x013E, 0x015B, 0x02C7, 0x00B8, 0x0161, 0x015F, 0x0165, 0x017A,
0x02DD, 0x017E, 0x017C, 0x0154, 0x00C1, 0x00C2, 0x0102, 0x00C4,
0x0139, 0x0106, 0x00C7, 0x010C, 0x00C9, 0x0118, 0x00CB, 0x011A,
0x00CD, 0x00CE, 0x010E, 0x0110, 0x0143, 0x0147, 0x00D3, 0x00D4,
0x0150, 0x00D6, 0x00D7, 0x0158, 0x016E, 0x00DA, 0x0170, 0x00DC,
0x00DD, 0x0162, 0x00DF, 0x0155, 0x00E1, 0x00E2, 0x0103, 0x00E4,
0x013A, 0x0107, 0x00E7, 0x010D, 0x00E9, 0x0119, 0x00EB, 0x011B,
0x00ED, 0x00EE, 0x010F, 0x0111, 0x0144, 0x0148, 0x00F3, 0x00F4,
0x0151, 0x00F6, 0x00F7, 0x0159, 0x016F, 0x00FA, 0x0171, 0x00FC,
0x00FD, 0x0163, 0x02D9 };
int c;
int result = 0;
for (c = 0; c < 161; ++c)
result |= ok_test(c, c);
for (c = 161; c < 256; ++c)
result |= ok_test(c, upper_half[c - 161]);
result |= eof_test();
totalResult |= result;
if (result == 0)
printf("locale '%s' ok\n", current_locale);
}
if (totalResult == 0)
puts("all OK");
return totalResult;
}
+162
View File
@@ -0,0 +1,162 @@
/* Taken from the Li18nux base test suite. */
#define _XOPEN_SOURCE 500
#include <errno.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <wchar.h>
#define WIDE_STR_LEN 32
int
main(int argc, char *argv[])
{
size_t i;
FILE *fp;
wchar_t *ret, wcs[WIDE_STR_LEN];
int result = 0;
const char il_str1[] = { 0xe3, 0x81, '\0' };
const char il_str2[] = { '0', '\n', 'A', 'B', 0xe3, 0x81, 'E', '\0' };
char name1[] = "/tmp/tst-fgetws.out.XXXXXX";
char name2[] = "/tmp/tst-fgetws.out.XXXXXX";
int fd;
puts("This program runs on de_DE.UTF-8 locale.");
if (setlocale(LC_ALL, "de_DE.UTF-8") == NULL) {
fprintf(stderr, "Err: Cannot run on the de_DE.UTF-8 locale");
exit(EXIT_FAILURE);
}
/* Make a file `il_str1'. */
fd = mkstemp(name1);
if (fd == -1) {
printf("cannot open temp file: %m\n");
exit(EXIT_FAILURE);
}
if ((fp = fdopen(fd, "w")) == NULL) {
printf("Can't open %s.\n", argv[1]);
exit(EXIT_FAILURE);
}
fwrite(il_str1, sizeof(char), sizeof(il_str1), fp);
fclose(fp);
/* Make a file `il_str2'. */
fd = mkstemp(name2);
if (fd == -1) {
printf("cannot open temp file: %m\n");
exit(EXIT_FAILURE);
}
if ((fp = fdopen(fd, "w")) == NULL) {
fprintf(stderr, "Can't open %s.\n", argv[1]);
exit(EXIT_FAILURE);
}
fwrite(il_str2, sizeof(char), sizeof(il_str2), fp);
fclose(fp);
/* Test for il_str1. */
if ((fp = fopen(name1, "r")) == NULL) {
fprintf(stderr, "Can't open %s.\n", argv[1]);
exit(EXIT_FAILURE);
}
puts("--");
puts("Read a byte sequence which is invalid as a wide character string.");
puts(" bytes: 0xe3, 0x81, '\\0'");
errno = 0;
ret = fgetws(wcs, WIDE_STR_LEN, fp);
if (ret == NULL) {
puts("Return Value: NULL");
if (errno == EILSEQ)
puts("errno = EILSEQ");
else {
printf("errno = %d\n", errno);
result = 1;
}
} else {
printf("Return Value: %p\n", ret);
for (i = 0; i < wcslen(wcs) + 1; i++)
printf(" wcs[%zd] = %04x", i, (unsigned int) wcs[i]);
printf("\n");
result = 1;
}
/* Test for il_str2. */
if ((fp = fopen(name2, "r")) == NULL) {
fprintf(stderr, "Can't open %s.\n", argv[1]);
exit(EXIT_FAILURE);
}
puts("--");
puts("Read a byte sequence which is invalid as a wide character string.");
puts(" bytes: '0', '\\n', 'A', 'B', 0xe3, 0x81, 'c', '\\0'");
errno = 0;
ret = fgetws(wcs, WIDE_STR_LEN, fp);
if (ret == NULL) {
puts("Return Value: NULL");
if (errno == EILSEQ)
puts("errno = EILSEQ");
else
printf("errno = %d\n", errno);
result = 1;
} else {
size_t i;
printf("Return Value: %p\n", ret);
for (i = 0; i < wcslen(wcs) + 1; i++)
printf(" wcs[%zd] = 0x%04x", i, (unsigned int) wcs[i]);
printf("\n");
for (i = 0; il_str2[i] != '\n'; ++i)
if ((wchar_t) il_str2[i] != wcs[i]) {
puts("read string not correct");
result = 1;
break;
}
if (il_str2[i] == '\n') {
if (wcs[i] != L'\n') {
puts("newline missing");
result = 1;
} else if (wcs[i + 1] != L'\0') {
puts("read string not NUL-terminated");
result = 1;
}
}
}
puts("\nsecond line");
errno = 0;
ret = fgetws(wcs, WIDE_STR_LEN, fp);
if (ret == NULL) {
puts("Return Value: NULL");
if (errno == EILSEQ)
puts("errno = EILSEQ");
else {
printf("errno = %d\n", errno);
result = 1;
}
} else {
printf("Return Value: %p\n", ret);
for (i = 0; i < wcslen(wcs) + 1; i++)
printf(" wcs[%zd] = 0x%04x", i, (unsigned int) wcs[i]);
printf("\n");
}
fclose(fp);
unlink(name1);
unlink(name2);
return result;
}
@@ -0,0 +1,41 @@
/* Simple test of getwc in the C locale.
Copyright (C) 2000 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Ulrich Drepper <[email protected]>, 2000.
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, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA. */
#include <stdio.h>
#include <wchar.h>
int
main(void)
{
wchar_t buf[100];
size_t n = 0;
wmemset(buf, L'\0', sizeof(buf) / sizeof(buf[0]));
while (!feof(stdin) && n < sizeof(buf) - 1) {
buf[n] = getwc(stdin);
if (buf[n] == WEOF)
break;
++n;
}
buf[n] = L'\0';
return wcscmp(buf,L"This is a test of getwc\n" ) != 0;
}
@@ -0,0 +1 @@
This is a test of getwc
@@ -0,0 +1,223 @@
/* Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Ulrich Drepper <[email protected]>, 2000.
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, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA. */
/* We always want assert to be fully defined. */
#undef NDEBUG
#include <assert.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
static int check_ascii(const char *locname);
/* UTF-8 single byte feeding test for mbrtowc(),
contributed by Markus Kuhn <[email protected]>. */
static int
utf8_test_1(void)
{
wchar_t wc;
mbstate_t s;
const char str[] = "\xe0\xa0\x80";
wc = 42; /* arbitrary number */
memset(&s, 0, sizeof(s)); /* get s into initial state */
assert (mbrtowc (&wc, "\xE2", 1, &s) == (size_t) -2); /* 1st byte processed */
assert (mbrtowc (&wc, "\x89", 1, &s) == (size_t) -2); /* 2nd byte processed */
assert (wc == 42); /* no value has not been stored into &wc yet */
assert (mbrtowc (&wc, "\xA0", 1, &s) == 1); /* 3nd byte processed */
assert (wc == 0x2260); /* E2 89 A0 = U+2260 (not equal) decoded correctly */
assert (mbrtowc (&wc, "", 1, &s) == 0); /* test final byte processing */
assert (wc == 0); /* test final byte decoding */
/* The following test is by Al Viro <[email protected]>. */
wc = 42; /* arbitrary number */
memset(&s, 0, sizeof(s)); /* get s into initial state */
assert (mbrtowc (&wc, str, 1, &s) == (size_t)-2);
assert (mbrtowc (&wc, str + 1, 2, &s) == 2);
assert (wc == 0x800);
wc = 42; /* arbitrary number */
memset(&s, 0, sizeof(s)); /* get s into initial state */
assert (mbrtowc (&wc, str, 3, &s) == 3);
assert (wc == 0x800);
return 0;
}
/* Test for NUL byte processing via empty string. */
static int
utf8_test_2(void)
{
wchar_t wc;
mbstate_t s;
wc = 42; /* arbitrary number */
memset(&s, 0, sizeof(s)); /* get s into initial state */
assert (mbrtowc (NULL, "", 1, &s) == 0); /* valid terminator */
assert (mbsinit (&s));
wc = 42; /* arbitrary number */
memset(&s, 0, sizeof(s)); /* get s into initial state */
assert (mbrtowc (&wc, "\xE2", 1, &s) == (size_t) -2); /* 1st byte processed */
assert (mbrtowc (NULL, "", 1, &s) == (size_t) -1); /* invalid terminator */
wc = 42; /* arbitrary number */
memset(&s, 0, sizeof(s)); /* get s into initial state */
assert (mbrtowc (&wc, "\xE2", 1, &s) == (size_t) -2); /* 1st byte processed */
assert (mbrtowc (&wc, "\x89", 1, &s) == (size_t) -2); /* 2nd byte processed */
assert (mbrtowc (NULL, "", 1, &s) == (size_t) -1); /* invalid terminator */
wc = 42; /* arbitrary number */
memset(&s, 0, sizeof(s)); /* get s into initial state */
assert (mbrtowc (&wc, "\xE2", 1, &s) == (size_t) -2); /* 1st byte processed */
assert (mbrtowc (&wc, "\x89", 1, &s) == (size_t) -2); /* 2nd byte processed */
assert (mbrtowc (&wc, "\xA0", 1, &s) == 1); /* 3nd byte processed */
assert (mbrtowc (NULL, "", 1, &s) == 0); /* valid terminator */
assert (mbsinit (&s));
return 0;
}
/* Test for NUL byte processing via NULL string. */
static int
utf8_test_3(void)
{
wchar_t wc;
mbstate_t s;
wc = 42; /* arbitrary number */
memset(&s, 0, sizeof(s)); /* get s into initial state */
assert (mbrtowc (NULL, NULL, 0, &s) == 0); /* valid terminator */
assert (mbsinit (&s));
wc = 42; /* arbitrary number */
memset(&s, 0, sizeof(s)); /* get s into initial state */
assert (mbrtowc (&wc, "\xE2", 1, &s) == (size_t) -2); /* 1st byte processed */
assert (mbrtowc (NULL, NULL, 0, &s) == (size_t) -1); /* invalid terminator */
wc = 42; /* arbitrary number */
memset(&s, 0, sizeof(s)); /* get s into initial state */
assert (mbrtowc (&wc, "\xE2", 1, &s) == (size_t) -2); /* 1st byte processed */
assert (mbrtowc (&wc, "\x89", 1, &s) == (size_t) -2); /* 2nd byte processed */
assert (mbrtowc (NULL, NULL, 0, &s) == (size_t) -1); /* invalid terminator */
wc = 42; /* arbitrary number */
memset(&s, 0, sizeof(s)); /* get s into initial state */
assert (mbrtowc (&wc, "\xE2", 1, &s) == (size_t) -2); /* 1st byte processed */
assert (mbrtowc (&wc, "\x89", 1, &s) == (size_t) -2); /* 2nd byte processed */
assert (mbrtowc (&wc, "\xA0", 1, &s) == 1); /* 3nd byte processed */
assert (mbrtowc (NULL, NULL, 0, &s) == 0); /* valid terminator */
assert (mbsinit (&s));
return 0;
}
static int
utf8_test(void)
{
const char *locale = "de_DE.UTF-8";
int error = 0;
if (!setlocale(LC_CTYPE, locale)) {
fprintf(stderr, "locale '%s' not available!\n", locale);
exit(1);
}
error |= utf8_test_1();
error |= utf8_test_2();
error |= utf8_test_3();
return error;
}
int
main(void)
{
int result = 0;
/* Check mapping of ASCII range for some character sets which have
ASCII as a subset. For those the wide char generated must have
the same value. */
setlocale(LC_ALL, "C");
result |= check_ascii(setlocale(LC_ALL, NULL));
setlocale(LC_ALL, "de_DE.UTF-8");
result |= check_ascii(setlocale(LC_ALL, NULL));
result |= utf8_test();
setlocale(LC_ALL, "ja_JP.EUC-JP");
result |= check_ascii(setlocale(LC_ALL, NULL));
return result;
}
static int
check_ascii(const char *locname)
{
int c;
int res = 0;
printf("Testing locale \"%s\":\n", locname);
for (c = 0; c <= 127; ++c) {
char buf[MB_CUR_MAX];
wchar_t wc = (wchar_t) 0xffffffff;
mbstate_t s;
size_t n, i;
for (i = 0; i < MB_CUR_MAX; ++i)
buf[i] = c + i;
memset(&s, '\0', sizeof(s));
n = mbrtowc(&wc, buf, MB_CUR_MAX, &s);
if (n == (size_t) - 1) {
printf("%s: '\\x%x': encoding error\n", locname, c);
++res;
} else if (n == (size_t) - 2) {
printf("%s: '\\x%x': incomplete character\n", locname, c);
++res;
} else if (n == 0 && c != 0) {
printf("%s: '\\x%x': 0 returned\n", locname, c);
++res;
} else if (n != 0 && c == 0) {
printf("%s: '\\x%x': not 0 returned\n", locname, c);
++res;
} else if (c != 0 && n != 1) {
printf("%s: '\\x%x': not 1 returned\n", locname, c);
++res;
} else if (wc != (wchar_t) c) {
printf("%s: '\\x%x': wc != L'\\x%x'\n", locname, c, c);
++res;
}
}
printf(res == 1 ? "%d error\n" : "%d errors\n", res);
return res != 0;
}
@@ -0,0 +1,78 @@
#include <stdio.h>
#include <wchar.h>
#include <sys/types.h>
static wchar_t buf[100];
#define nbuf (sizeof (buf) / sizeof (buf[0]))
static const struct {
size_t n;
const char *str;
ssize_t exp;
} tests[] = {
{ nbuf, "hello world", 11 },
{ 0, "hello world", -1 },
{ 0, "", -1 },
{ nbuf, "", 0 }
};
int
main(int argc, char *argv[])
{
size_t n;
int result = 0;
puts("test 1");
n = swprintf(buf, nbuf,L"Hello %s" , "world");
if (n != 11)
{
printf ("incorrect return value: %zd instead of 11\n", n);
result = 1;
}
else if (wcscmp(buf, L"Hello world") != 0)
{
printf("incorrect string: L\"%ls\" instead of L\"Hello world\"\n", buf);
result = 1;
}
puts ("test 2");
n = swprintf(buf, nbuf, L"Is this >%g< 3.1?", 3.1);
if (n != 18) {
printf("incorrect return value: %zd instead of 18\n", n);
result = 1;
} else if (wcscmp (buf, L"Is this >3.1< 3.1?") != 0) {
printf("incorrect string: L\"%ls\" instead of L\"Is this >3.1< 3.1?\"\n",
buf);
result = 1;
}
for (n = 0; n < sizeof(tests) / sizeof(tests[0]); ++n) {
ssize_t res = swprintf(buf, tests[n].n, L"%s", tests[n].str);
if (tests[n].exp < 0 && res >= 0) {
printf("swprintf (buf, %Zu, L\"%%s\", \"%s\") expected to fail\n",
tests[n].n, tests[n].str);
result = 1;
} else if (tests[n].exp >= 0 && tests[n].exp != res) {
printf("swprintf (buf, %Zu, L\"%%s\", \"%s\") expected to return %Zd, but got %Zd\n",
tests[n].n, tests[n].str, tests[n].exp, res);
result = 1;
} else {
printf("swprintf (buf, %Zu, L\"%%s\", \"%s\") OK\n", tests[n].n,
tests[n].str);
}
}
if (swprintf(buf, nbuf, L"%.0s", "foo") != 0 || wcslen(buf) != 0) {
printf("swprintf (buf, %Zu, L\"%%.0s\", \"foo\") create some output\n",
nbuf);
result = 1;
}
if (swprintf(buf, nbuf, L"%.0ls", L"foo") != 0 || wcslen(buf) != 0) {
printf("swprintf (buf, %Zu, L\"%%.0ls\", L\"foo\") create some output\n",
nbuf);
result = 1;
}
return result;
}
@@ -0,0 +1,89 @@
#include <locale.h>
#include <stdio.h>
#include <string.h>
#include <wchar.h>
static int do_test(const char *loc);
int
main(void)
{
int result;
result = do_test("C");
result |= do_test("de_DE.ISO-8859-1");
result |= do_test("de_DE.UTF-8");
result |= do_test("ja_JP.EUC-JP");
return result;
}
static const struct {
const wchar_t *fmt;
const wchar_t *wfmt;
const wchar_t *arg;
int retval;
const char *res;
const wchar_t *wres;
int only_C_locale;
} tests[] = {
{ L"%[abc]", L"%l[abc]", L"aabbccddaabb", 1 ,"aabbcc", L"aabbcc", 0 },
{ L"%[^def]", L"%l[^def]", L"aabbccddaabb", 1, "aabbcc", L"aabbcc", 0 },
{ L"%[^abc]", L"%l[^abc]", L"aabbccddaabb", 0, "", L"", 0 },
{ L"%[a-c]", L"%l[a-c]", L"aabbccddaabb", 1, "aabbcc", L"aabbcc", 1 },
{ L"%[^d-f]", L"%l[^d-f]", L"aabbccddaabb", 1, "aabbcc", L"aabbcc", 1 },
{ L"%[^a-c]", L"%l[^a-c]", L"aabbccddaabb", 0, "", L"", 1 },
{ L"%[^a-c]", L"%l[^a-c]", L"bbccddaabb", 0, "", L"", 1 }
};
static int
do_test(const char *loc)
{
size_t n;
int result = 0;
if (setlocale(LC_ALL, loc) == NULL) {
printf("cannot set locale \"%s\": %m\n", loc);
return 1;
}
printf("\nnew locale: \"%s\"\n", loc);
for (n = 0; n < sizeof(tests) / sizeof(tests[0]); ++n) {
char buf[100];
wchar_t wbuf[100];
if (tests[n].only_C_locale && strcmp(loc, "C") != 0)
continue;
if (swscanf(tests[n].arg, tests[n].fmt, buf) != tests[n].retval) {
printf("swscanf (\"%S\", \"%S\", ...) failed\n", tests[n].arg,
tests[n].fmt);
result = 1;
} else if (tests[n].retval != 0 && strcmp(buf, tests[n].res) != 0) {
printf(
"swscanf (\"%S\", \"%S\", ...) return \"%s\", expected \"%s\"\n",
tests[n].arg, tests[n].fmt, buf, tests[n].res);
result = 1;
} else
printf("swscanf (\"%S\", \"%S\", ...) OK\n", tests[n].arg,
tests[n].fmt);
if (swscanf(tests[n].arg, tests[n].wfmt, wbuf) != tests[n].retval) {
printf("swscanf (\"%S\", \"%S\", ...) failed\n", tests[n].arg,
tests[n].wfmt);
result = 1;
} else if (tests[n].retval != 0 && wcscmp(wbuf, tests[n].wres) != 0) {
printf(
"swscanf (\"%S\", \"%S\", ...) return \"%S\", expected \"%S\"\n",
tests[n].arg, tests[n].wfmt, wbuf, tests[n].wres);
result = 1;
} else
printf("swscanf (\"%S\", \"%S\", ...) OK\n", tests[n].arg,
tests[n].wfmt);
}
return result;
}
@@ -0,0 +1,38 @@
#include <stdio.h>
#include <string.h>
#include <wchar.h>
int
main(int argc, char *argv[])
{
const wchar_t in[] =L"7 + 35 is 42" ;
size_t n;
int a, b, c;
int result = 0;
char buf1[20];
wchar_t wbuf2[20];
char buf3[20];
char c4;
wchar_t wc5;
puts ("Test 1");
a = b = c = 0;
n = swscanf (in, L"%d + %d is %d", &a, &b, &c);
if (n != 3 || a + b != c || c != 42) {
printf ("*** FAILED, n = %Zu, a = %d, b = %d, c = %d\n", n, a, b, c);
result = 1;
}
puts ("Test 2");
n = swscanf (L"one two three !!", L"%s %S %s %c%C",
buf1, wbuf2, buf3, &c4, &wc5);
if (n != 5 || strcmp (buf1, "one") != 0 || wcscmp (wbuf2, L"two") != 0
|| strcmp (buf3, "three") != 0 || c4 != '!' || wc5 != L'!') {
printf ("*** FAILED, n = %Zu, buf1 = \"%s\", wbuf2 = L\"%S\", buf3 = \"%s\", c4 = '%c', wc5 = L'%C'\n",
n, buf1, wbuf2, buf3, c4, (wint_t) wc5);
result = 1;
}
return result;
}
@@ -0,0 +1,74 @@
/* Taken from the Li18nux base test suite. */
#define _XOPEN_SOURCE 500
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <wchar.h>
int
main(void)
{
FILE *fp;
const char *str = "abcdef";
wint_t ret, wc, ungetone = 0x00E4; /* 0x00E4 means `a umlaut'. */
char fname[] = "/tmp/tst-ungetwc1.out.XXXXXX";
int fd;
int result = 0;
puts("This program runs on de_DE.UTF-8 locale.");
if (setlocale(LC_ALL, "de_DE.UTF-8") == NULL) {
fprintf(stderr, "Err: Cannot run on the de_DE.UTF-8 locale");
exit(EXIT_FAILURE);
}
fd = mkstemp(fname);
if (fd == -1) {
printf("cannot open temp file: %m\n");
exit(EXIT_FAILURE);
}
/* Write some characters to `testfile'. */
if ((fp = fdopen(fd, "w")) == NULL) {
fprintf(stderr, "Cannot open 'testfile'.");
exit(EXIT_FAILURE);
}
fputs(str, fp);
fclose(fp);
/* Open `testfile'. */
if ((fp = fopen(fname, "r")) == NULL) {
fprintf(stderr, "Cannot open 'testfile'.");
exit(EXIT_FAILURE);
}
/* Unget a character. */
ret = ungetwc(ungetone, fp);
printf("Unget a character (0x%04x)\n", (unsigned int) ungetone);
fflush(stdout);
if (ret == WEOF) {
puts("ungetwc() returns NULL.");
exit(EXIT_SUCCESS);
}
/* Reget a character. */
wc = getwc(fp);
printf("Reget a character (0x%04x)\n", (unsigned int) wc);
fflush(stdout);
if (wc == ungetone) {
puts("The ungotten character is equal to the regotten character.");
fflush(stdout);
} else {
puts("The ungotten character is not equal to the regotten character.");
printf("ungotten one: %04x, regetone: %04x", ungetone, wc);
fflush(stdout);
result = 1;
}
fclose(fp);
unlink(fname);
return result;
}
@@ -0,0 +1,77 @@
/* Taken from the Li18nux base test suite. */
#define _XOPEN_SOURCE 500
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <wchar.h>
int
main(void)
{
FILE *fp;
const char *str = "abcdef";
wint_t ret, wc;
char fname[] = "/tmp/tst-ungetwc2.out.XXXXXX";
int fd;
long int pos;
int result = 0;
puts("This program runs on de_DE.UTF-8 locale.");
if (setlocale(LC_ALL, "de_DE.UTF-8") == NULL) {
fprintf(stderr, "Err: Cannot run on the de_DE.UTF-8 locale\n");
exit(EXIT_FAILURE);
}
/* Write some characters to `testfile'. */
fd = mkstemp(fname);
if (fd == -1) {
printf("cannot open temp file: %m\n");
exit(EXIT_FAILURE);
}
if ((fp = fdopen(fd, "w")) == NULL) {
fprintf(stderr, "Cannot open 'testfile'.\n");
exit(EXIT_FAILURE);
}
fputs(str, fp);
fclose(fp);
/* Open `testfile'. */
if ((fp = fopen(fname, "r")) == NULL) {
fprintf(stderr, "Cannot open 'testfile'.");
exit(EXIT_FAILURE);
}
/* Get a character. */
wc = getwc(fp);
pos = ftell(fp);
printf("After get a character: %ld\n", pos);
if (pos != 1)
result = 1;
/* Unget a character. */
ret = ungetwc(wc, fp);
if (ret == WEOF) {
fprintf(stderr, "ungetwc() returns NULL.");
exit(EXIT_FAILURE);
}
pos = ftell(fp);
printf("After unget a character: %ld\n", pos);
if (pos != 0)
result = 1;
/* Reget a character. */
wc = getwc(fp);
pos = ftell(fp);
printf("After reget a character: %ld\n", pos);
if (pos != 1)
result = 1;
fclose(fp);
unlink(fname);
return result;
}
@@ -0,0 +1,86 @@
/* Copyright (C) 2000 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Ulrich Drepper <drepper@redhat.com>, 2000.
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, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA. */
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
static int check_ascii(const char *locname);
int
main(void)
{
int result = 0;
/* Check mapping of ASCII range for some character sets which have
ASCII as a subset. For those the wide char generated must have
the same value. */
setlocale(LC_ALL, "C");
result |= check_ascii(setlocale(LC_ALL, NULL));
setlocale(LC_ALL, "de_DE.UTF-8");
result |= check_ascii(setlocale(LC_ALL, NULL));
setlocale(LC_ALL, "ja_JP.EUC-JP");
result |= check_ascii(setlocale(LC_ALL, NULL));
return result;
}
static int
check_ascii(const char *locname)
{
wchar_t wc;
int res = 0;
printf("Testing locale \"%s\":\n", locname);
for (wc = 0; wc <= 127; ++wc) {
char buf[2 * MB_CUR_MAX];
mbstate_t s;
size_t n;
memset(buf, '\xff', sizeof(buf));
memset(&s, '\0', sizeof(s));
n = wcrtomb(buf, wc, &s);
if (n == (size_t) - 1) {
printf("%s: '\\x%x': encoding error\n", locname, (int) wc);
++res;
} else if (n == 0) {
printf("%s: '\\x%x': 0 returned\n", locname, (int) wc);
++res;
} else if (n != 1) {
printf("%s: '\\x%x': not 1 returned\n", locname, (int) wc);
++res;
} else if (wc != (wchar_t) buf[0]) {
printf("%s: L'\\x%x': buf[0] != '\\x%x'\n", locname, (int) wc,
(int) wc);
++res;
}
}
printf(res == 1 ? "%d error\n" : "%d errors\n", res);
return res != 0;
}
@@ -0,0 +1,55 @@
/* Copyright (C) 1999 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Ulrich Drepper <drepper@cygnus.com>, 1999.
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, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA. */
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
#define TEST(Str, Max, Exp) \
n = wcsnlen (L##Str, Max); \
if (n != Exp) { \
result = 1; \
printf ("wcsnlen (L\"%s\", %d) = %d, not %d\n", Str, Max, n, Exp); \
}
int
main(void)
{
int result = 0;
int n;
TEST ("0123456789", 0, 0);
TEST ("0123456789", 1, 1);
TEST ("0123456789", 2, 2);
TEST ("0123456789", 3, 3);
TEST ("0123456789", 4, 4);
TEST ("0123456789", 5, 5);
TEST ("0123456789", 6, 6);
TEST ("0123456789", 7, 7);
TEST ("0123456789", 8, 8);
TEST ("0123456789", 9, 9);
TEST ("01234", 9, 5);
if (result == 0)
printf("all ok.\n");
return result;
}
@@ -0,0 +1,24 @@
#define _GNU_SOURCE 1
#include <wchar.h>
#include <stdio.h>
#include <string.h>
#include <wctype.h>
int
main(void)
{
int result = 0;
char buf[100];
wchar_t tmp[3];
tmp[0] = '8';
tmp[1] = '1';
tmp[2] = 0;
snprintf(buf, 100, "%S = %f", tmp, wcstof(tmp, NULL));
printf("\"%s\" -> %s\n", buf, strcmp(buf, "81 = 81.000000") == 0 ? "okay"
: "buggy");
result |= strcmp(buf, "81 = 81.000000") != 0;
return result;
}
@@ -0,0 +1,12 @@
#include <stdio.h>
#include <wchar.h>
int
main(int argc, char *argv[])
{
fputws(L"Hello world!\n", stdout);
wprintf (L"This %s a %ls string: %d\n", "is", L"mixed", 42);
wprintf (L"%Iu\n", 0xfeedbeef);
return 0;
}
@@ -0,0 +1,111 @@
/* Test case by Yoshito Kawada <[email protected]>. */
#include <errno.h>
#include <fcntl.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wchar.h>
void error(int exitCode, int, const char* message);
int
main(int argc, char *argv[])
{
int a = 3;
int fd;
char name[] = "/tmp/wprintf.out.XXXXXX";
FILE *fp;
char buf[100];
size_t len;
int res = 0;
fd = mkstemp(name);
if (fd == -1)
error(EXIT_FAILURE, errno, "cannot open temporary file");
unlink(name);
setlocale(LC_ALL, "");
fp = fdopen(dup(fd), "w");
if (fp == NULL)
error(EXIT_FAILURE, errno, "fdopen(,\"w\")");
fwprintf(fp,L"test start" );
fwprintf(fp, L" int %d\n", a);
/* String with precision. */
fwprintf(fp, L"1[%6.3s]\n", argv[1]);
fclose(fp);
fp = fdopen(dup (fd), "a");
if (fp == NULL)
error(EXIT_FAILURE, errno, "fdopen(,\"a\")");
setvbuf(fp, NULL, _IONBF, 0);
/* fwprintf to unbuffered stream. */
fwprintf(fp, L"hello.\n");
fclose(fp);
/* Now read it back in. This time using multibyte functions. */
lseek(fd, SEEK_SET, 0);
fp = fdopen(fd, "r");
if (fp == NULL)
error(EXIT_FAILURE, errno, "fdopen(,\"r\")");
if (fgets(buf, sizeof buf, fp) != buf)
error(EXIT_FAILURE, errno, "first fgets");
len = strlen(buf);
if (buf[len - 1] == '\n')
--len;
else {
puts ("newline missing after first line");
res = 1;
}
printf("1st line: \"%.*s\" -> %s\n", (int) len, buf,
strncmp(buf, "test start int 3", len) == 0 ? "OK" : "FAIL");
res |= strncmp(buf, "test start int 3", len) != 0;
if (fgets(buf, sizeof(buf), fp) != buf)
error(EXIT_FAILURE, errno, "second fgets");
len = strlen(buf);
if (buf[len - 1] == '\n')
--len;
else {
puts("newline missing after second line");
res = 1;
}
printf ("2nd line: \"%.*s\" -> %s\n", (int) len, buf,
strncmp(buf, "1[ Som]", len) == 0 ? "OK" : "FAIL");
res |= strncmp(buf, "1[ Som]", len) != 0;
if (fgets(buf, sizeof(buf), fp) != buf)
error(EXIT_FAILURE, errno, "third fgets");
len = strlen(buf);
if (buf[len - 1] == '\n')
--len;
else {
puts("newline missing after third line");
res = 1;
}
printf("3rd line: \"%.*s\" -> %s\n", (int) len, buf,
strncmp(buf, "hello.", len) == 0 ? "OK" : "FAIL");
res |= strncmp(buf, "hello.", len) != 0;
return res;
}
void
error(int exitCode, int errorCode, const char* message)
{
perror(message);
exit(exitCode);
}
@@ -0,0 +1,29 @@
#include <stdio.h>
#include <string.h>
#include <wchar.h>
int
main(int argc, char *argv[])
{
int n;
int result = 0;
char buf1[20];
wchar_t wbuf2[20];
char c3;
wchar_t wc4;
int d;
puts("Test 1");
n = wscanf(L"%s %S %c%C %d", buf1, wbuf2, &c3, &wc4, &d);
if (n != 5 || strcmp (buf1, "Hello") != 0 || wcscmp (wbuf2, L"World") != 0
|| c3 != '!' || wc4 != L'!' || d != 42) {
printf ("*** FAILED, n = %d, buf1 = \"%s\", wbuf2 = L\"%S\", c3 = '%c', wc4 = L'%C', d = %d\n",
n, buf1, wbuf2, c3, (wint_t) wc4, d);
result = 1;
}
return result;
}
@@ -0,0 +1 @@
Hello World !! 42