xref: /freebsd/lib/libc/tests/locale/iswctype_test.c (revision 1d386b48)
1 /*-
2  * Copyright (c) 2003 Tim J. Robbins
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 /*
28  * Test program for wctype() and iswctype() as specified by
29  * IEEE Std. 1003.1-2001 and ISO/IEC 9899:1999.
30  *
31  * The functions are tested in the "C" and "ja_JP.eucJP" locales.
32  */
33 
34 #include <sys/cdefs.h>
35 #include <sys/param.h>
36 #include <errno.h>
37 #include <locale.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <wchar.h>
41 #include <wctype.h>
42 
43 #include <atf-c.h>
44 
45 static void
46 require_lc_ctype(const char *locale_name)
47 {
48 	char *lc_ctype_set;
49 
50 	lc_ctype_set = setlocale(LC_CTYPE, locale_name);
51 	if (lc_ctype_set == NULL)
52 		atf_tc_fail("setlocale(LC_CTYPE, \"%s\") failed; errno=%d",
53 		    locale_name, errno);
54 
55 	ATF_REQUIRE(strcmp(lc_ctype_set, locale_name) == 0);
56 }
57 
58 static wctype_t t;
59 static int i, j;
60 static struct {
61 	const char *name;
62 	int (*func)(wint_t);
63 } cls[] = {
64 	{ "alnum", iswalnum },
65 	{ "alpha", iswalpha },
66 	{ "blank", iswblank },
67 	{ "cntrl", iswcntrl },
68 	{ "digit", iswdigit },
69 	{ "graph", iswgraph },
70 	{ "lower", iswlower },
71 	{ "print", iswprint },
72 	{ "punct", iswpunct },
73 	{ "space", iswspace },
74 	{ "upper", iswupper },
75 	{ "xdigit", iswxdigit }
76 };
77 
78 ATF_TC_WITHOUT_HEAD(iswctype_c_locale_test);
79 ATF_TC_BODY(iswctype_c_locale_test, tc)
80 {
81 
82 	require_lc_ctype("C");
83 	for (i = 0; i < nitems(cls); i++) {
84 		t = wctype(cls[i].name);
85 		ATF_REQUIRE(t != 0);
86 		for (j = 0; j < 256; j++)
87 			ATF_REQUIRE(cls[i].func(j) == iswctype(j, t));
88 	}
89 	t = wctype("elephant");
90 	ATF_REQUIRE(t == 0);
91 	for (i = 0; i < 256; i++)
92 		ATF_REQUIRE(iswctype(i, t) == 0);
93 }
94 
95 ATF_TC_WITHOUT_HEAD(iswctype_euc_jp_test);
96 ATF_TC_BODY(iswctype_euc_jp_test, tc)
97 {
98 
99 	require_lc_ctype("ja_JP.eucJP");
100 
101 	for (i = 0; i < nitems(cls); i++) {
102 		t = wctype(cls[i].name);
103 		ATF_REQUIRE(t != 0);
104 		for (j = 0; j < 65536; j++)
105 			ATF_REQUIRE(cls[i].func(j) == iswctype(j, t));
106 	}
107 	t = wctype("elephant");
108 	ATF_REQUIRE(t == 0);
109 	for (i = 0; i < 65536; i++)
110 		ATF_REQUIRE(iswctype(i, t) == 0);
111 }
112 
113 ATF_TP_ADD_TCS(tp)
114 {
115 
116 	ATF_TP_ADD_TC(tp, iswctype_c_locale_test);
117 	ATF_TP_ADD_TC(tp, iswctype_euc_jp_test);
118 
119 	return (atf_no_error());
120 }
121