Blame src/general/mbc.c

nsz 97c71e
#include <stdio.h>
nsz 97c71e
#include <string.h>
nsz 97c71e
#include <wchar.h>
nsz 97c71e
#include <stdlib.h>
nsz 97c71e
#include <locale.h>
nsz 97c71e
#include <langinfo.h>
nsz 97c71e
#include "test.h"
nsz 97c71e
nsz 462b4f
/*
nsz 97c71e
 * f = function call to test (or any expression)
nsz 97c71e
 * x = expected result
nsz 462b4f
 * m = message to print on failure
nsz 462b4f
 */
nsz 462b4f
#define T(f, x, m) (void)( \
nsz 97c71e
	memset(&st, 0, sizeof st), \
nsz 462b4f
	(i = (f)) == (x) || \
nsz 462b4f
		error("%s failed (%s) got %d want %d\n", #f, m, i, x) )
nsz 462b4f
#define TCHAR(f, x, m) (void)( \
nsz 462b4f
	memset(&st, 0, sizeof st), \
nsz 462b4f
	(i = (f)) == (x) || \
nsz 462b4f
		error("%s failed (%s) got 0x%04x want 0x%04x\n", #f, m, i, x) )
nsz 97c71e
nsz 462b4f
int main(void)
nsz 462b4f
{
nsz 97c71e
	const char *cs;
nsz 97c71e
	int i;
nsz 97c71e
	mbstate_t st, st2;
nsz 97c71e
	wchar_t wc, wcs[32];
nsz 462b4f
nsz 462b4f
	(void)(
nsz 97c71e
	setlocale(LC_CTYPE, "en_US.UTF-8") ||
nsz 97c71e
	setlocale(LC_CTYPE, "en_GB.UTF-8") ||
nsz 97c71e
	setlocale(LC_CTYPE, "en.UTF-8") ||
nsz 97c71e
	setlocale(LC_CTYPE, "POSIX.UTF-8") ||
nsz 97c71e
	setlocale(LC_CTYPE, "C.UTF-8") ||
nsz 97c71e
	setlocale(LC_CTYPE, "UTF-8") ||
nsz 462b4f
	setlocale(LC_CTYPE, "") );
nsz 462b4f
nsz 462b4f
	T(mbsrtowcs(wcs, (cs="abcdef",&cs), 3, &st), 3, "wrong semantics for wcs buf len");
nsz 462b4f
	T(mbsrtowcs(wcs, (cs="abcdef",&cs), 8, &st), 6, "wrong semantics for wcs buf len");
nsz 462b4f
	T(mbsrtowcs(NULL, (cs="abcdef",&cs), 2, &st), 6, "wrong semantics for NULL wcs");
nsz 97c71e
nsz 462b4f
	if (strcmp(nl_langinfo(CODESET), "UTF-8"))
nsz 462b4f
		return error("cannot set UTF-8 locale for test (codeset=%s)\n", nl_langinfo(CODESET));
nsz 97c71e
nsz 462b4f
	T(mbrtowc(&wc, "\x80", 1, &st), -1, "failed to catch error");
nsz 462b4f
	T(mbrtowc(&wc, "\xc0", 1, &st), -1, "failed to catch illegal initial");
nsz 97c71e
nsz 462b4f
	T(mbrtowc(&wc, "\xc0\x80", 2, &st), -1, "aliasing nul");
nsz 462b4f
	T(mbrtowc(&wc, "\xc0\xaf", 2, &st), -1, "aliasing slash");
nsz 462b4f
	T(mbrtowc(&wc, "\xe0\x80\xaf", 3, &st), -1, "aliasing slash");
nsz 462b4f
	T(mbrtowc(&wc, "\xf0\x80\x80\xaf", 4, &st), -1, "aliasing slash");
nsz 462b4f
	T(mbrtowc(&wc, "\xf8\x80\x80\x80\xaf", 5, &st), -1, "aliasing slash");
nsz 462b4f
	T(mbrtowc(&wc, "\xfc\x80\x80\x80\x80\xaf", 6, &st), -1, "aliasing slash");
nsz 462b4f
	T(mbrtowc(&wc, "\xe0\x82\x80", 3, &st), -1, "aliasing U+0080");
nsz 462b4f
	T(mbrtowc(&wc, "\xe0\x9f\xbf", 3, &st), -1, "aliasing U+07FF");
nsz 462b4f
	T(mbrtowc(&wc, "\xf0\x80\xa0\x80", 4, &st), -1, "aliasing U+0800");
nsz 462b4f
	T(mbrtowc(&wc, "\xf0\x8f\xbf\xbd", 4, &st), -1, "aliasing U+FFFD");
nsz 97c71e
nsz 462b4f
	T(mbrtowc(&wc, "\xed\xa0\x80", 3, &st), -1, "failed to catch surrogate");
nsz 462b4f
	T(mbrtowc(&wc, "\xef\xbf\xbe", 3, &st), 3, "failed to accept U+FFFE");
nsz 462b4f
	T(mbrtowc(&wc, "\xef\xbf\xbf", 3, &st), 3, "failed to accept U+FFFF");
nsz 462b4f
	T(mbrtowc(&wc, "\xf4\x8f\xbf\xbe", 4, &st), 4, "failed to accept U+10FFFE");
nsz 462b4f
	T(mbrtowc(&wc, "\xf4\x8f\xbf\xbf", 4, &st), 4, "failed to accept U+10FFFF");
nsz 97c71e
nsz 462b4f
	T(mbrtowc(&wc, "\xc2\x80", 2, &st), 2, "wrong length");
nsz 462b4f
	TCHAR((mbrtowc(&wc, "\xc2\x80", 2, &st),wc), 0x80, "wrong char");
nsz 462b4f
	T(mbrtowc(&wc, "\xe0\xa0\x80", 3, &st), 3, "wrong length");
nsz 462b4f
	TCHAR((mbrtowc(&wc, "\xe0\xa0\x80", 3, &st),wc), 0x800, "wrong char");
nsz 462b4f
	T(mbrtowc(&wc, "\xf0\x90\x80\x80", 4, &st), 4, "wrong length");
nsz 462b4f
	TCHAR((mbrtowc(&wc, "\xf0\x90\x80\x80", 4, &st),wc), 0x10000, "wrong char");
nsz 97c71e
nsz 97c71e
	memset(&st2, 0, sizeof st2);
nsz 462b4f
	T(mbrtowc(&wc, "\xc2", 1, &st2), -2, "failed to accept initial byte");
nsz 462b4f
	T(mbrtowc(&wc, "\x80", 1, &st2), 1, "failed to resume");
nsz 462b4f
	TCHAR(wc, 0x80, "wrong char");
nsz 97c71e
nsz 97c71e
	memset(&st2, 0, sizeof st2);
nsz 462b4f
	T(mbrtowc(&wc, "\xc2", 1, &st2), -2, "failed to accept initial byte");
nsz 462b4f
	T(mbsrtowcs(wcs, (cs="\xa0""abc",&cs), 32, &st2), 4, "failed to resume");
nsz 462b4f
	TCHAR(wcs[0], 0xa0, "wrong char");
nsz 462b4f
	TCHAR(wcs[1], 'a', "wrong char");
nsz 462b4f
	T(!cs, 1, "wrong final position");
nsz 462b4f
	return test_status;
nsz 97c71e
}