1106686Stjr/*-
2128005Stjr * Copyright (c) 2002-2004 Tim J. Robbins
3106686Stjr * All rights reserved.
4106686Stjr *
5106686Stjr * Redistribution and use in source and binary forms, with or without
6106686Stjr * modification, are permitted provided that the following conditions
7106686Stjr * are met:
8106686Stjr * 1. Redistributions of source code must retain the above copyright
9106686Stjr *    notice, this list of conditions and the following disclaimer.
10106686Stjr * 2. Redistributions in binary form must reproduce the above copyright
11106686Stjr *    notice, this list of conditions and the following disclaimer in the
12106686Stjr *    documentation and/or other materials provided with the distribution.
13106686Stjr *
14106686Stjr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15106686Stjr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16106686Stjr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17106686Stjr * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18106686Stjr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19106686Stjr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20106686Stjr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21106686Stjr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22106686Stjr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23106686Stjr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24106686Stjr * SUCH DAMAGE.
25106686Stjr */
26106686Stjr
27106686Stjr/*
28106686Stjr * Test program for mbtowc(), as specified by IEEE Std. 1003.1-2001 and
29106686Stjr * ISO/IEC 9899:1990.
30106686Stjr *
31106686Stjr * The function is tested with both the "C" ("POSIX") LC_CTYPE setting and
32106686Stjr * "ja_JP.eucJP". Other encodings are not tested.
33106686Stjr */
34106686Stjr
35106686Stjr#include <sys/cdefs.h>
36106686Stjr__FBSDID("$FreeBSD$");
37106686Stjr
38106686Stjr#include <assert.h>
39106686Stjr#include <limits.h>
40106686Stjr#include <locale.h>
41106686Stjr#include <stdio.h>
42106686Stjr#include <stdlib.h>
43106686Stjr#include <string.h>
44106686Stjr
45106686Stjrint
46106686Stjrmain(int argc, char *argv[])
47106686Stjr{
48106686Stjr	size_t len;
49106686Stjr	wchar_t wc;
50106686Stjr	char buf[MB_LEN_MAX + 1];
51106686Stjr
52106686Stjr	/*
53106686Stjr	 * C/POSIX locale.
54106686Stjr	 */
55106686Stjr
56137587Snik	printf("1..1\n");
57137587Snik
58106686Stjr	assert(MB_CUR_MAX == 1);
59106686Stjr
60106686Stjr	/* No shift states in C locale. */
61106686Stjr	assert(mbtowc(NULL, NULL, 0) == 0);
62106686Stjr
63106686Stjr	/* Null wide character. */
64106686Stjr	wc = 0xcccc;
65106686Stjr	memset(buf, 0, sizeof(buf));
66106686Stjr	assert(mbtowc(&wc, buf, 1) == 0);
67106686Stjr	assert(wc == 0);
68106686Stjr
69106686Stjr	/* Latin letter A. */
70106686Stjr	buf[0] = 'A';
71106686Stjr	assert(mbtowc(&wc, buf, 1) == 1);
72106686Stjr	assert(wc == L'A');
73106686Stjr
74106686Stjr	/* Incomplete character sequence. */
75106686Stjr	wc = L'z';
76106686Stjr	buf[0] = '\0';
77106686Stjr	assert(mbtowc(&wc, buf, 0) == -1);
78106686Stjr	assert(wc == L'z');
79128005Stjr	assert(mbtowc(NULL, NULL, 0) == 0);
80106686Stjr
81106686Stjr	/*
82106686Stjr	 * Japanese (EUC) locale.
83106686Stjr	 */
84106686Stjr
85106686Stjr	assert(strcmp(setlocale(LC_CTYPE, "ja_JP.eucJP"), "ja_JP.eucJP") == 0);
86106686Stjr	assert(MB_CUR_MAX > 1);
87106686Stjr
88106686Stjr	/* Null wide character */
89106686Stjr	memset(buf, 0xcc, sizeof(buf));
90106686Stjr	buf[0] = 0;
91106686Stjr	wc = 0xcccc;
92106686Stjr	assert(mbtowc(&wc, buf, 1) == 0);
93106686Stjr	assert(wc == 0);
94106686Stjr
95106686Stjr	/* Latin letter A. */
96106686Stjr	buf[0] = 'A';
97106686Stjr	assert(mbtowc(&wc, buf, 1) == 1);
98106686Stjr	assert(wc == L'A');
99106686Stjr
100106686Stjr	/* Incomplete character sequence (zero length). */
101106686Stjr	wc = L'z';
102106686Stjr	buf[0] = '\0';
103106686Stjr	assert(mbtowc(&wc, buf, 0) == -1);
104106686Stjr	assert(wc == L'z');
105128005Stjr	assert(mbtowc(NULL, NULL, 0) == 0);
106106686Stjr
107106686Stjr	/* Incomplete character sequence (truncated double-byte). */
108106686Stjr	memset(buf, 0xcc, sizeof(buf));
109106686Stjr	buf[0] = 0xa3;
110106686Stjr	buf[1] = 0x00;
111106686Stjr	wc = L'z';
112106686Stjr	assert(mbtowc(&wc, buf, 1) == -1);
113106686Stjr	assert(wc == L'z');
114128005Stjr	assert(mbtowc(NULL, NULL, 0) == 0);
115106686Stjr
116106686Stjr	/* Same as above, but complete. */
117106686Stjr	buf[1] = 0xc1;
118106686Stjr	assert(mbtowc(&wc, buf, 2) == 2);
119106686Stjr	assert(wc == 0xa3c1);
120106686Stjr
121137587Snik	printf("ok 1 - mbtowc()\n");
122106686Stjr
123106686Stjr	return (0);
124106686Stjr}
125