1*81cc9994SLauri Tirkkonen /*
2*81cc9994SLauri Tirkkonen  * Copyright (c) 2014 Lauri Tirkkonen <lotheac@iki.fi>
3*81cc9994SLauri Tirkkonen  *
4*81cc9994SLauri Tirkkonen  * Permission to use, copy, modify, and distribute this software for any
5*81cc9994SLauri Tirkkonen  * purpose with or without fee is hereby granted, provided that the above
6*81cc9994SLauri Tirkkonen  * copyright notice and this permission notice appear in all copies.
7*81cc9994SLauri Tirkkonen  *
8*81cc9994SLauri Tirkkonen  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9*81cc9994SLauri Tirkkonen  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10*81cc9994SLauri Tirkkonen  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11*81cc9994SLauri Tirkkonen  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12*81cc9994SLauri Tirkkonen  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13*81cc9994SLauri Tirkkonen  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14*81cc9994SLauri Tirkkonen  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15*81cc9994SLauri Tirkkonen  */
16*81cc9994SLauri Tirkkonen 
17*81cc9994SLauri Tirkkonen /*
18*81cc9994SLauri Tirkkonen  * This program tests that the characters defined in the POSIX-1.2008 Portable
19*81cc9994SLauri Tirkkonen  * Character Set are classified correctly by the iswctype and isw* functions.
20*81cc9994SLauri Tirkkonen  */
21*81cc9994SLauri Tirkkonen 
22*81cc9994SLauri Tirkkonen #include <stdlib.h>
23*81cc9994SLauri Tirkkonen #include <stdio.h>
24*81cc9994SLauri Tirkkonen #include <wchar.h>
25*81cc9994SLauri Tirkkonen #include <wctype.h>
26*81cc9994SLauri Tirkkonen #include <locale.h>
27*81cc9994SLauri Tirkkonen #include <err.h>
28*81cc9994SLauri Tirkkonen #include "test_common.h"
29*81cc9994SLauri Tirkkonen 
30*81cc9994SLauri Tirkkonen wint_t upper_should[] = L"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
31*81cc9994SLauri Tirkkonen wint_t lower_should[] = L"abcdefghijklmnopqrstuvwxyz";
32*81cc9994SLauri Tirkkonen wint_t digit_should[] = L"0123456789";
33*81cc9994SLauri Tirkkonen wint_t space_should[] = L"\t\n\v\f\r ";
34*81cc9994SLauri Tirkkonen wint_t cntrl_should[] = L"\a\b\t\n\v\f\r\0\001\002\003\004\005\006\016\017\020"
35*81cc9994SLauri Tirkkonen "\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037";
36*81cc9994SLauri Tirkkonen wint_t punct_should[] = L"!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";
37*81cc9994SLauri Tirkkonen wint_t xdigit_should[] = L"0123456789ABCDEFabcdef";
38*81cc9994SLauri Tirkkonen wint_t blank_should[] = L" \t";
39*81cc9994SLauri Tirkkonen wint_t only_space_should[] = L" ";
40*81cc9994SLauri Tirkkonen 
41*81cc9994SLauri Tirkkonen #define	test_ctype_subset(x, y) do {\
42*81cc9994SLauri Tirkkonen 	test_t t = test_start(#x "_should is subset of " #y);\
43*81cc9994SLauri Tirkkonen 	wctype_t class = wctype(#y);\
44*81cc9994SLauri Tirkkonen 	if (!class) test_failed(t, "wctype(\"%s\") returned 0", #y);\
45*81cc9994SLauri Tirkkonen 	size_t nchars = (sizeof (x ## _should) / sizeof (*x ## _should)) - 1;\
46*81cc9994SLauri Tirkkonen 	for (wint_t *wc = x ## _should; wc < x ## _should + nchars; wc++) {\
47*81cc9994SLauri Tirkkonen 		if (!iswctype(*wc, class))\
48*81cc9994SLauri Tirkkonen 			test_failed(t, "iswctype(L'%lc', wctype(\"%s\"))"\
49*81cc9994SLauri Tirkkonen 				    "returned 0", *wc, #y);\
50*81cc9994SLauri Tirkkonen 		if (!isw ## y(*wc))\
51*81cc9994SLauri Tirkkonen 			test_failed(t, "isw%s(L'%lc') returned 0", #y, *wc);\
52*81cc9994SLauri Tirkkonen 	}\
53*81cc9994SLauri Tirkkonen 	test_passed(t);\
54*81cc9994SLauri Tirkkonen } while (*"\0")
55*81cc9994SLauri Tirkkonen 
56*81cc9994SLauri Tirkkonen #define	test_ctype(x) test_ctype_subset(x, x)
57*81cc9994SLauri Tirkkonen 
main(void)58*81cc9994SLauri Tirkkonen int main(void) {
59*81cc9994SLauri Tirkkonen 	if (!setlocale(LC_CTYPE, "POSIX"))
60*81cc9994SLauri Tirkkonen 		err(1, "setlocale POSIX failed");
61*81cc9994SLauri Tirkkonen 	test_ctype(upper);
62*81cc9994SLauri Tirkkonen 	test_ctype(lower);
63*81cc9994SLauri Tirkkonen 	test_ctype(digit);
64*81cc9994SLauri Tirkkonen 	test_ctype(space);
65*81cc9994SLauri Tirkkonen 	test_ctype(cntrl);
66*81cc9994SLauri Tirkkonen 	test_ctype(punct);
67*81cc9994SLauri Tirkkonen 	test_ctype(xdigit);
68*81cc9994SLauri Tirkkonen 	test_ctype(blank);
69*81cc9994SLauri Tirkkonen 
70*81cc9994SLauri Tirkkonen 	test_ctype_subset(upper, alpha);
71*81cc9994SLauri Tirkkonen 	test_ctype_subset(lower, alpha);
72*81cc9994SLauri Tirkkonen 	test_ctype_subset(upper, alnum);
73*81cc9994SLauri Tirkkonen 	test_ctype_subset(lower, alnum);
74*81cc9994SLauri Tirkkonen 	test_ctype_subset(digit, alnum);
75*81cc9994SLauri Tirkkonen 	test_ctype_subset(upper, print);
76*81cc9994SLauri Tirkkonen 	test_ctype_subset(lower, print);
77*81cc9994SLauri Tirkkonen 	test_ctype_subset(digit, print);
78*81cc9994SLauri Tirkkonen 	test_ctype_subset(punct, print);
79*81cc9994SLauri Tirkkonen 	test_ctype_subset(only_space, print);
80*81cc9994SLauri Tirkkonen 	test_ctype_subset(upper, graph);
81*81cc9994SLauri Tirkkonen 	test_ctype_subset(lower, graph);
82*81cc9994SLauri Tirkkonen 	test_ctype_subset(digit, graph);
83*81cc9994SLauri Tirkkonen 	test_ctype_subset(punct, graph);
84*81cc9994SLauri Tirkkonen 	return (0);
85*81cc9994SLauri Tirkkonen }
86