1570af302Sopenharmony_ci#include <stdio.h>
2570af302Sopenharmony_ci#include <string.h>
3570af302Sopenharmony_ci#include <wchar.h>
4570af302Sopenharmony_ci#include <stdlib.h>
5570af302Sopenharmony_ci#include <locale.h>
6570af302Sopenharmony_ci#include <langinfo.h>
7570af302Sopenharmony_ci#include "test.h"
8570af302Sopenharmony_ci
9570af302Sopenharmony_ci/*
10570af302Sopenharmony_ci * f = function call to test (or any expression)
11570af302Sopenharmony_ci * x = expected result
12570af302Sopenharmony_ci * m = message to print on failure
13570af302Sopenharmony_ci */
14570af302Sopenharmony_ci#define T(f, x, m) (void)( \
15570af302Sopenharmony_ci	memset(&st, 0, sizeof st), \
16570af302Sopenharmony_ci	(i = (f)) == (x) || \
17570af302Sopenharmony_ci		t_error("%s failed (%s) got %d want %d\n", #f, m, i, x) )
18570af302Sopenharmony_ci#define TCHAR(f, x, m) (void)( \
19570af302Sopenharmony_ci	memset(&st, 0, sizeof st), \
20570af302Sopenharmony_ci	(i = (f)) == (x) || \
21570af302Sopenharmony_ci		t_error("%s failed (%s) got 0x%04x want 0x%04x\n", #f, m, i, x) )
22570af302Sopenharmony_ci
23570af302Sopenharmony_ciint main(void)
24570af302Sopenharmony_ci{
25570af302Sopenharmony_ci	const char *cs;
26570af302Sopenharmony_ci	int i;
27570af302Sopenharmony_ci	mbstate_t st, st2;
28570af302Sopenharmony_ci	wchar_t wc, wcs[32];
29570af302Sopenharmony_ci
30570af302Sopenharmony_ci	(void)(
31570af302Sopenharmony_ci	setlocale(LC_CTYPE, "en_US.UTF-8") ||
32570af302Sopenharmony_ci	setlocale(LC_CTYPE, "en_GB.UTF-8") ||
33570af302Sopenharmony_ci	setlocale(LC_CTYPE, "en.UTF-8") ||
34570af302Sopenharmony_ci	setlocale(LC_CTYPE, "POSIX.UTF-8") ||
35570af302Sopenharmony_ci	setlocale(LC_CTYPE, "C.UTF-8") ||
36570af302Sopenharmony_ci	setlocale(LC_CTYPE, "UTF-8") ||
37570af302Sopenharmony_ci	setlocale(LC_CTYPE, "") );
38570af302Sopenharmony_ci
39570af302Sopenharmony_ci	T(mbsrtowcs(wcs, (cs="abcdef",&cs), 3, &st), 3, "wrong semantics for wcs buf len");
40570af302Sopenharmony_ci	T(mbsrtowcs(wcs, (cs="abcdef",&cs), 8, &st), 6, "wrong semantics for wcs buf len");
41570af302Sopenharmony_ci	T(mbsrtowcs(NULL, (cs="abcdef",&cs), 2, &st), 6, "wrong semantics for NULL wcs");
42570af302Sopenharmony_ci
43570af302Sopenharmony_ci	if (strcmp(nl_langinfo(CODESET), "UTF-8"))
44570af302Sopenharmony_ci		return t_error("cannot set UTF-8 locale for test (codeset=%s)\n", nl_langinfo(CODESET));
45570af302Sopenharmony_ci
46570af302Sopenharmony_ci	T(mbrtowc(&wc, "\x80", 1, &st), -1, "failed to catch error");
47570af302Sopenharmony_ci	T(mbrtowc(&wc, "\xc0", 1, &st), -1, "failed to catch illegal initial");
48570af302Sopenharmony_ci
49570af302Sopenharmony_ci	T(mbrtowc(&wc, "\xc0\x80", 2, &st), -1, "aliasing nul");
50570af302Sopenharmony_ci	T(mbrtowc(&wc, "\xc0\xaf", 2, &st), -1, "aliasing slash");
51570af302Sopenharmony_ci	T(mbrtowc(&wc, "\xe0\x80\xaf", 3, &st), -1, "aliasing slash");
52570af302Sopenharmony_ci	T(mbrtowc(&wc, "\xf0\x80\x80\xaf", 4, &st), -1, "aliasing slash");
53570af302Sopenharmony_ci	T(mbrtowc(&wc, "\xf8\x80\x80\x80\xaf", 5, &st), -1, "aliasing slash");
54570af302Sopenharmony_ci	T(mbrtowc(&wc, "\xfc\x80\x80\x80\x80\xaf", 6, &st), -1, "aliasing slash");
55570af302Sopenharmony_ci	T(mbrtowc(&wc, "\xe0\x82\x80", 3, &st), -1, "aliasing U+0080");
56570af302Sopenharmony_ci	T(mbrtowc(&wc, "\xe0\x9f\xbf", 3, &st), -1, "aliasing U+07FF");
57570af302Sopenharmony_ci	T(mbrtowc(&wc, "\xf0\x80\xa0\x80", 4, &st), -1, "aliasing U+0800");
58570af302Sopenharmony_ci	T(mbrtowc(&wc, "\xf0\x8f\xbf\xbd", 4, &st), -1, "aliasing U+FFFD");
59570af302Sopenharmony_ci
60570af302Sopenharmony_ci	T(mbrtowc(&wc, "\xed\xa0\x80", 3, &st), -1, "failed to catch surrogate");
61570af302Sopenharmony_ci	T(mbrtowc(&wc, "\xef\xbf\xbe", 3, &st), 3, "failed to accept U+FFFE");
62570af302Sopenharmony_ci	T(mbrtowc(&wc, "\xef\xbf\xbf", 3, &st), 3, "failed to accept U+FFFF");
63570af302Sopenharmony_ci	T(mbrtowc(&wc, "\xf4\x8f\xbf\xbe", 4, &st), 4, "failed to accept U+10FFFE");
64570af302Sopenharmony_ci	T(mbrtowc(&wc, "\xf4\x8f\xbf\xbf", 4, &st), 4, "failed to accept U+10FFFF");
65570af302Sopenharmony_ci
66570af302Sopenharmony_ci	T(mbrtowc(&wc, "\xc2\x80", 2, &st), 2, "wrong length");
67570af302Sopenharmony_ci	TCHAR((mbrtowc(&wc, "\xc2\x80", 2, &st),wc), 0x80, "wrong char");
68570af302Sopenharmony_ci	T(mbrtowc(&wc, "\xe0\xa0\x80", 3, &st), 3, "wrong length");
69570af302Sopenharmony_ci	TCHAR((mbrtowc(&wc, "\xe0\xa0\x80", 3, &st),wc), 0x800, "wrong char");
70570af302Sopenharmony_ci	T(mbrtowc(&wc, "\xf0\x90\x80\x80", 4, &st), 4, "wrong length");
71570af302Sopenharmony_ci	TCHAR((mbrtowc(&wc, "\xf0\x90\x80\x80", 4, &st),wc), 0x10000, "wrong char");
72570af302Sopenharmony_ci
73570af302Sopenharmony_ci	memset(&st2, 0, sizeof st2);
74570af302Sopenharmony_ci	T(mbrtowc(&wc, "\xc2", 1, &st2), -2, "failed to accept initial byte");
75570af302Sopenharmony_ci	T(mbrtowc(&wc, "\x80", 1, &st2), 1, "failed to resume");
76570af302Sopenharmony_ci	TCHAR(wc, 0x80, "wrong char");
77570af302Sopenharmony_ci
78570af302Sopenharmony_ci	memset(&st2, 0, sizeof st2);
79570af302Sopenharmony_ci	T(mbrtowc(&wc, "\xc2", 1, &st2), -2, "failed to accept initial byte");
80570af302Sopenharmony_ci	T(mbsrtowcs(wcs, (cs="\xa0""abc",&cs), 32, &st2), 4, "failed to resume");
81570af302Sopenharmony_ci	TCHAR(wcs[0], 0xa0, "wrong char");
82570af302Sopenharmony_ci	TCHAR(wcs[1], 'a', "wrong char");
83570af302Sopenharmony_ci	T(!cs, 1, "wrong final position");
84570af302Sopenharmony_ci	return t_status;
85570af302Sopenharmony_ci}
86