mbtowc_test.c revision 106686
190792Sgshapiro/*-
2261194Sgshapiro * Copyright (c) 2002 Tim J. Robbins
390792Sgshapiro * All rights reserved.
490792Sgshapiro *
590792Sgshapiro * Redistribution and use in source and binary forms, with or without
690792Sgshapiro * modification, are permitted provided that the following conditions
790792Sgshapiro * are met:
890792Sgshapiro * 1. Redistributions of source code must retain the above copyright
9266527Sgshapiro *    notice, this list of conditions and the following disclaimer.
1090792Sgshapiro * 2. Redistributions in binary form must reproduce the above copyright
1190792Sgshapiro *    notice, this list of conditions and the following disclaimer in the
1290792Sgshapiro *    documentation and/or other materials provided with the distribution.
1390792Sgshapiro *
1490792Sgshapiro * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1590792Sgshapiro * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1690792Sgshapiro * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1790792Sgshapiro * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1890792Sgshapiro * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1990792Sgshapiro * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2090792Sgshapiro * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2190792Sgshapiro * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2290792Sgshapiro * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2390792Sgshapiro * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2490792Sgshapiro * SUCH DAMAGE.
2590792Sgshapiro */
2690792Sgshapiro
2790792Sgshapiro/*
2890792Sgshapiro * Test program for mbtowc(), as specified by IEEE Std. 1003.1-2001 and
2990792Sgshapiro * ISO/IEC 9899:1990.
3090792Sgshapiro *
3190792Sgshapiro * The function is tested with both the "C" ("POSIX") LC_CTYPE setting and
3290792Sgshapiro * "ja_JP.eucJP". Other encodings are not tested.
3390792Sgshapiro */
34102528Sgshapiro
3590792Sgshapiro#include <sys/cdefs.h>
3690792Sgshapiro__FBSDID("$FreeBSD: head/tools/regression/lib/libc/locale/test-mbtowc.c 106686 2002-11-09 04:33:02Z tjr $");
3790792Sgshapiro
3890792Sgshapiro#include <assert.h>
3990792Sgshapiro#include <limits.h>
4090792Sgshapiro#include <locale.h>
4190792Sgshapiro#include <stdio.h>
4290792Sgshapiro#include <stdlib.h>
4390792Sgshapiro#include <string.h>
4490792Sgshapiro
4590792Sgshapiroint
46main(int argc, char *argv[])
47{
48	size_t len;
49	wchar_t wc;
50	char buf[MB_LEN_MAX + 1];
51
52	/*
53	 * C/POSIX locale.
54	 */
55
56	assert(MB_CUR_MAX == 1);
57
58	/* No shift states in C locale. */
59	assert(mbtowc(NULL, NULL, 0) == 0);
60
61	/* Null wide character. */
62	wc = 0xcccc;
63	memset(buf, 0, sizeof(buf));
64	assert(mbtowc(&wc, buf, 1) == 0);
65	assert(wc == 0);
66
67	/* Latin letter A. */
68	buf[0] = 'A';
69	assert(mbtowc(&wc, buf, 1) == 1);
70	assert(wc == L'A');
71
72	/* Incomplete character sequence. */
73	wc = L'z';
74	buf[0] = '\0';
75	assert(mbtowc(&wc, buf, 0) == -1);
76	assert(wc == L'z');
77
78	/*
79	 * Japanese (EUC) locale.
80	 */
81
82	assert(strcmp(setlocale(LC_CTYPE, "ja_JP.eucJP"), "ja_JP.eucJP") == 0);
83	assert(MB_CUR_MAX > 1);
84
85	/* Null wide character */
86	memset(buf, 0xcc, sizeof(buf));
87	buf[0] = 0;
88	wc = 0xcccc;
89	assert(mbtowc(&wc, buf, 1) == 0);
90	assert(wc == 0);
91
92	/* Latin letter A. */
93	buf[0] = 'A';
94	assert(mbtowc(&wc, buf, 1) == 1);
95	assert(wc == L'A');
96
97	/* Incomplete character sequence (zero length). */
98	wc = L'z';
99	buf[0] = '\0';
100	assert(mbtowc(&wc, buf, 0) == -1);
101	assert(wc == L'z');
102
103	/* Incomplete character sequence (truncated double-byte). */
104	memset(buf, 0xcc, sizeof(buf));
105	buf[0] = 0xa3;
106	buf[1] = 0x00;
107	wc = L'z';
108	assert(mbtowc(&wc, buf, 1) == -1);
109	assert(wc == L'z');
110
111	/* Same as above, but complete. */
112	buf[1] = 0xc1;
113	assert(mbtowc(&wc, buf, 2) == 2);
114	assert(wc == 0xa3c1);
115
116	printf("PASS mbtowc()\n");
117
118	return (0);
119}
120