iswctype_test.c revision 250989
1189251Ssam/*-
2214734Srpaulo * Copyright (c) 2003 Tim J. Robbins
3252726Srpaulo * All rights reserved.
4189251Ssam *
5252726Srpaulo * Redistribution and use in source and binary forms, with or without
6252726Srpaulo * modification, are permitted provided that the following conditions
7189251Ssam * are met:
8189251Ssam * 1. Redistributions of source code must retain the above copyright
9189251Ssam *    notice, this list of conditions and the following disclaimer.
10189251Ssam * 2. Redistributions in binary form must reproduce the above copyright
11189251Ssam *    notice, this list of conditions and the following disclaimer in the
12189251Ssam *    documentation and/or other materials provided with the distribution.
13189251Ssam *
14189251Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15189251Ssam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16189251Ssam * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17189251Ssam * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18189251Ssam * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19189251Ssam * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20252726Srpaulo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21252726Srpaulo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22189251Ssam * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23189251Ssam * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24189251Ssam * SUCH DAMAGE.
25189251Ssam */
26189251Ssam
27189251Ssam/*
28189251Ssam * Test program for wctype() and iswctype() as specified by
29189251Ssam * IEEE Std. 1003.1-2001 and ISO/IEC 9899:1999.
30189251Ssam *
31189251Ssam * The functions are tested in the "C" and "ja_JP.eucJP" locales.
32189251Ssam */
33189251Ssam
34189251Ssam#include <sys/cdefs.h>
35189251Ssam__FBSDID("$FreeBSD: head/tools/regression/lib/libc/locale/test-iswctype.c 250989 2013-05-25 18:04:24Z ed $");
36189251Ssam
37189251Ssam#include <assert.h>
38189251Ssam#include <locale.h>
39189251Ssam#include <stdio.h>
40189251Ssam#include <string.h>
41189251Ssam#include <wchar.h>
42189251Ssam#include <wctype.h>
43189251Ssam
44189251Ssamint
45189251Ssammain(int argc, char *argv[])
46189251Ssam{
47189251Ssam	wctype_t t;
48189251Ssam	int i, j;
49189251Ssam	struct {
50189251Ssam		const char *name;
51189251Ssam		int (*func)(wint_t);
52189251Ssam	} cls[] = {
53189251Ssam		{ "alnum", iswalnum },
54189251Ssam		{ "alpha", iswalpha },
55189251Ssam		{ "blank", iswblank },
56189251Ssam		{ "cntrl", iswcntrl },
57189251Ssam		{ "digit", iswdigit },
58252726Srpaulo		{ "graph", iswgraph },
59252726Srpaulo		{ "lower", iswlower },
60252726Srpaulo		{ "print", iswprint },
61189251Ssam		{ "punct", iswpunct },
62189251Ssam		{ "space", iswspace },
63189251Ssam		{ "upper", iswupper },
64189251Ssam		{ "xdigit", iswxdigit }
65189251Ssam	};
66189251Ssam
67189251Ssam	printf("1..2\n");
68189251Ssam
69189251Ssam	/*
70189251Ssam	 * C/POSIX locale.
71189251Ssam	 */
72189251Ssam	for (i = 0; i < sizeof(cls) / sizeof(*cls); i++) {
73189251Ssam		t = wctype(cls[i].name);
74189251Ssam		assert(t != 0);
75189251Ssam		for (j = 0; j < 256; j++)
76189251Ssam			assert(cls[i].func(j) == iswctype(j, t));
77189251Ssam	}
78189251Ssam	t = wctype("elephant");
79189251Ssam	assert(t == 0);
80189251Ssam	for (i = 0; i < 256; i++)
81189251Ssam		assert(iswctype(i, t) == 0);
82189251Ssam
83189251Ssam	/*
84189251Ssam	 * Japanese (EUC) locale.
85189251Ssam	 */
86189251Ssam	assert(strcmp(setlocale(LC_CTYPE, "ja_JP.eucJP"), "ja_JP.eucJP") == 0);
87189251Ssam	for (i = 0; i < sizeof(cls) / sizeof(*cls); i++) {
88189251Ssam		t = wctype(cls[i].name);
89189251Ssam		assert(t != 0);
90189251Ssam		for (j = 0; j < 65536; j++)
91189251Ssam			assert(cls[i].func(j) == iswctype(j, t));
92189251Ssam	}
93214734Srpaulo	t = wctype("elephant");
94214734Srpaulo	assert(t == 0);
95214734Srpaulo	for (i = 0; i < 65536; i++)
96189251Ssam		assert(iswctype(i, t) == 0);
97189251Ssam
98189251Ssam	printf("ok 1 - iswctype()\n");
99189251Ssam	printf("ok 2 - wctype()\n");
100189251Ssam
101189251Ssam	return (0);
102189251Ssam}
103189251Ssam