xref: /illumos-gate/usr/src/test/libc-tests/tests/stdio/test_mbrtowc.c (revision 8459c777fc1aaabb2f7dad05de1313aa169417cd)
1 /*
2  * Copyright (c) 2016 Ingo Schwarze <schwarze@openbsd.org>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #include <sys/types.h>
18 #include <err.h>
19 #include <errno.h>
20 #include <locale.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <wchar.h>
24 
25 static mbstate_t	 mbs;
26 
27 void
28 onetest(const char *name, const char *in, size_t insz,
29     int outerr, size_t outsz, wint_t out)
30 {
31 	wchar_t		 wc;
32 	size_t		 sz;
33 
34 	sz = mbrtowc(&wc, in, insz, &mbs);
35 	if (errno != outerr)
36 		err(1, "%zu %s(%zd)", MB_CUR_MAX, name, insz);
37 	if (sz != outsz || (out != WEOF && wc != out))
38 		errx(1, "%zu %s(%zd) = (%zd, %d) != (%zd, %d)",
39 		    MB_CUR_MAX, name, insz, sz, wc, outsz, out);
40 	if (mbsinit(&mbs) == (insz && outsz == (size_t)-2))
41 		errx(1, "%zu %s(%zd) mbsinit", MB_CUR_MAX, name, insz);
42 	if (errno == 0 && outerr == 0)
43 		return;
44 	errno = 0;
45 	memset(&mbs, 0, sizeof (mbs));
46 }
47 
48 int
49 main(void)
50 {
51 	onetest("NUL", "", 0, 0, -2, WEOF);
52 	onetest("NUL", "", 2, 0, 0, L'\0');
53 	onetest("BEL", "\a", 2, 0, 1, L'\a');
54 	onetest("A", "A", 2, 0, 1, L'A');
55 	onetest("DEL", "\177", 2, 0, 1, L'\177');
56 	onetest("CSI", "\233", 2, 0, 1, L'\233');
57 
58 	if (setlocale(LC_CTYPE, "en_US.UTF-8") == NULL)
59 		errx(1, "setlocale(UTF-8) failed");
60 
61 	onetest("NUL", "", 0, 0, -2, WEOF);
62 	onetest("NUL", "", 8, 0, 0, L'\0');
63 	onetest("BEL", "\a", 8, 0, 1, L'\a');
64 	onetest("A", "A", 8, 0, 1, L'A');
65 	onetest("DEL", "\177", 8, 0, 1, L'\177');
66 	onetest("0x80", "\200", 8, EILSEQ, -1, WEOF);
67 	onetest("0xc3", "\303", 1, 0, -2, WEOF);
68 	onetest("U+00E9", "\251", 8, 0, 1, 0xe9);
69 	onetest("0xec", "\354", 1, 0, -2, WEOF);
70 	onetest("0xecbf", "\277", 1, 0, -2, WEOF);
71 	onetest("U+CFFF", "\277", 8, 0, 1, 0xcfff);
72 
73 	if (setlocale(LC_CTYPE, "POSIX") == NULL)
74 		errx(1, "setlocale(POSIX) failed");
75 
76 	onetest("0xff", "\277", 2, 0, 1, L'\277');
77 
78 	if (setlocale(LC_CTYPE, "en_US.UTF-8") == NULL)
79 		errx(1, "second setlocale(UTF-8) failed");
80 
81 	onetest("U+13000", "\360\223\200\200", 8, 0, 4, 0x13000);
82 
83 	return (0);
84 }
85