[Haiku-commits] r31180 - haiku/trunk/src/tests/system/libroot/posix

zooey at BerliOS zooey at mail.berlios.de
Mon Jun 22 21:13:33 CEST 2009


Author: zooey
Date: 2009-06-22 21:13:32 +0200 (Mon, 22 Jun 2009)
New Revision: 31180
ViewCVS: http://svn.berlios.de/viewcvs/haiku?rev=31180&view=rev

Added:
   haiku/trunk/src/tests/system/libroot/posix/test_wcfuncs.c
   haiku/trunk/src/tests/system/libroot/posix/test_wctype.c
   haiku/trunk/src/tests/system/libroot/posix/tst-btowc.c
   haiku/trunk/src/tests/system/libroot/posix/tst-fgetws.c
   haiku/trunk/src/tests/system/libroot/posix/tst-getwc.c
   haiku/trunk/src/tests/system/libroot/posix/tst-getwc.input
   haiku/trunk/src/tests/system/libroot/posix/tst-mbrtowc.c
   haiku/trunk/src/tests/system/libroot/posix/tst-swprintf.c
   haiku/trunk/src/tests/system/libroot/posix/tst-swscanf.c
   haiku/trunk/src/tests/system/libroot/posix/tst-swscanf2.c
   haiku/trunk/src/tests/system/libroot/posix/tst-ungetwc1.c
   haiku/trunk/src/tests/system/libroot/posix/tst-ungetwc2.c
   haiku/trunk/src/tests/system/libroot/posix/tst-wcrtomb.c
   haiku/trunk/src/tests/system/libroot/posix/tst-wcsnlen.c
   haiku/trunk/src/tests/system/libroot/posix/tst-wcstof.c
   haiku/trunk/src/tests/system/libroot/posix/tst-wprintf.c
   haiku/trunk/src/tests/system/libroot/posix/tst-wprintf2.c
   haiku/trunk/src/tests/system/libroot/posix/tst-wscanf.c
   haiku/trunk/src/tests/system/libroot/posix/tst-wscanf.input
Modified:
   haiku/trunk/src/tests/system/libroot/posix/Jamfile
Log:
* 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 ...



Modified: haiku/trunk/src/tests/system/libroot/posix/Jamfile
===================================================================
--- haiku/trunk/src/tests/system/libroot/posix/Jamfile	2009-06-22 18:18:53 UTC (rev 31179)
+++ haiku/trunk/src/tests/system/libroot/posix/Jamfile	2009-06-22 19:13:32 UTC (rev 31180)
@@ -72,6 +72,74 @@
 	: 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

Added: haiku/trunk/src/tests/system/libroot/posix/test_wcfuncs.c
===================================================================
--- haiku/trunk/src/tests/system/libroot/posix/test_wcfuncs.c	2009-06-22 18:18:53 UTC (rev 31179)
+++ haiku/trunk/src/tests/system/libroot/posix/test_wcfuncs.c	2009-06-22 19:13:32 UTC (rev 31180)
@@ -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;
+}

Added: haiku/trunk/src/tests/system/libroot/posix/test_wctype.c
===================================================================
--- haiku/trunk/src/tests/system/libroot/posix/test_wctype.c	2009-06-22 18:18:53 UTC (rev 31179)
+++ haiku/trunk/src/tests/system/libroot/posix/test_wctype.c	2009-06-22 19:13:32 UTC (rev 31180)
@@ -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;
+}

Added: haiku/trunk/src/tests/system/libroot/posix/tst-btowc.c
===================================================================
--- haiku/trunk/src/tests/system/libroot/posix/tst-btowc.c	2009-06-22 18:18:53 UTC (rev 31179)
+++ haiku/trunk/src/tests/system/libroot/posix/tst-btowc.c	2009-06-22 19:13:32 UTC (rev 31180)
@@ -0,0 +1,204 @@
+/* Copyright (C) 2000 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Ulrich Drepper <drepper at 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 <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;
+}

Added: haiku/trunk/src/tests/system/libroot/posix/tst-fgetws.c
===================================================================
--- haiku/trunk/src/tests/system/libroot/posix/tst-fgetws.c	2009-06-22 18:18:53 UTC (rev 31179)
+++ haiku/trunk/src/tests/system/libroot/posix/tst-fgetws.c	2009-06-22 19:13:32 UTC (rev 31180)
@@ -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;
+}

Added: haiku/trunk/src/tests/system/libroot/posix/tst-getwc.c
===================================================================
--- haiku/trunk/src/tests/system/libroot/posix/tst-getwc.c	2009-06-22 18:18:53 UTC (rev 31179)
+++ haiku/trunk/src/tests/system/libroot/posix/tst-getwc.c	2009-06-22 19:13:32 UTC (rev 31180)
@@ -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 <drepper at cygnus.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 <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;
+}

Added: haiku/trunk/src/tests/system/libroot/posix/tst-getwc.input
===================================================================
--- haiku/trunk/src/tests/system/libroot/posix/tst-getwc.input	2009-06-22 18:18:53 UTC (rev 31179)
+++ haiku/trunk/src/tests/system/libroot/posix/tst-getwc.input	2009-06-22 19:13:32 UTC (rev 31180)
@@ -0,0 +1 @@
+This is a test of getwc

Added: haiku/trunk/src/tests/system/libroot/posix/tst-mbrtowc.c
===================================================================
--- haiku/trunk/src/tests/system/libroot/posix/tst-mbrtowc.c	2009-06-22 18:18:53 UTC (rev 31179)
+++ haiku/trunk/src/tests/system/libroot/posix/tst-mbrtowc.c	2009-06-22 19:13:32 UTC (rev 31180)
@@ -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 <drepper at 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.  */
+
+/* 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 <mkuhn at acm.org>.  */
+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 <aviro at redhat.com>.  */
+	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;
+}

Added: haiku/trunk/src/tests/system/libroot/posix/tst-swprintf.c
===================================================================
--- haiku/trunk/src/tests/system/libroot/posix/tst-swprintf.c	2009-06-22 18:18:53 UTC (rev 31179)
+++ haiku/trunk/src/tests/system/libroot/posix/tst-swprintf.c	2009-06-22 19:13:32 UTC (rev 31180)
@@ -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;
+}

Added: haiku/trunk/src/tests/system/libroot/posix/tst-swscanf.c
===================================================================

[... truncated: 660 lines follow ...]



More information about the Haiku-commits mailing list