1145516Sdarrenr/*-
2145516Sdarrenr * Copyright (c) 2002-2004 Tim J. Robbins
3145516Sdarrenr * All rights reserved.
4145516Sdarrenr *
5145516Sdarrenr * Redistribution and use in source and binary forms, with or without
6145516Sdarrenr * modification, are permitted provided that the following conditions
7145516Sdarrenr * are met:
8145516Sdarrenr * 1. Redistributions of source code must retain the above copyright
9145516Sdarrenr *    notice, this list of conditions and the following disclaimer.
10172776Sdarrenr * 2. Redistributions in binary form must reproduce the above copyright
11145516Sdarrenr *    notice, this list of conditions and the following disclaimer in the
12145516Sdarrenr *    documentation and/or other materials provided with the distribution.
13145516Sdarrenr *
14145516Sdarrenr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15145516Sdarrenr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16145516Sdarrenr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17145516Sdarrenr * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18145516Sdarrenr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19145516Sdarrenr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20145516Sdarrenr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21145516Sdarrenr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22145516Sdarrenr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23145516Sdarrenr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24145516Sdarrenr * SUCH DAMAGE.
25145516Sdarrenr */
26145516Sdarrenr
27145516Sdarrenr/*
28145516Sdarrenr * Test program for wctomb(), as specified by IEEE Std. 1003.1-2001 and
29145516Sdarrenr * ISO/IEC 9899:1999.
30145516Sdarrenr *
31145516Sdarrenr * The function is tested with both the "C" ("POSIX") LC_CTYPE setting and
32145516Sdarrenr * "ja_JP.eucJP". Other encodings are not tested.
33145516Sdarrenr */
34145516Sdarrenr
35145516Sdarrenr#include <sys/cdefs.h>
36145516Sdarrenr__FBSDID("$FreeBSD$");
37145516Sdarrenr
38145516Sdarrenr#include <assert.h>
39145516Sdarrenr#include <errno.h>
40145516Sdarrenr#include <limits.h>
41145516Sdarrenr#include <locale.h>
42145516Sdarrenr#include <stdio.h>
43145516Sdarrenr#include <stdlib.h>
44145516Sdarrenr#include <string.h>
45145516Sdarrenr
46145516Sdarrenrint
47145516Sdarrenrmain(int argc, char *argv[])
48145516Sdarrenr{
49145516Sdarrenr	size_t len;
50145516Sdarrenr	char buf[MB_LEN_MAX + 1];
51145516Sdarrenr
52145516Sdarrenr	/*
53145516Sdarrenr	 * C/POSIX locale.
54145516Sdarrenr	 */
55145516Sdarrenr
56145516Sdarrenr	printf("1..1\n");
57145516Sdarrenr
58145516Sdarrenr	assert(MB_CUR_MAX == 1);
59145516Sdarrenr
60170268Sdarrenr	/* No shift states in C locale. */
61170268Sdarrenr	assert(wctomb(NULL, L'\0') == 0);
62170268Sdarrenr
63170268Sdarrenr	/* Null wide character. */
64170268Sdarrenr	memset(buf, 0xcc, sizeof(buf));
65145516Sdarrenr	len = wctomb(buf, L'\0');
66145516Sdarrenr	assert(len == 1);
67145516Sdarrenr	assert((unsigned char)buf[0] == 0 && (unsigned char)buf[1] == 0xcc);
68145516Sdarrenr
69170268Sdarrenr	/* Latin letter A. */
70170268Sdarrenr	memset(buf, 0xcc, sizeof(buf));
71170268Sdarrenr	len = wctomb(buf, L'A');
72145516Sdarrenr	assert(len == 1);
73145516Sdarrenr	assert((unsigned char)buf[0] == 'A' && (unsigned char)buf[1] == 0xcc);
74145516Sdarrenr
75145516Sdarrenr	/* Invalid code. */
76145516Sdarrenr	assert(wctomb(buf, UCHAR_MAX + 1) == -1);
77145516Sdarrenr	assert(wctomb(NULL, 0) == 0);
78145516Sdarrenr
79145516Sdarrenr	/*
80145516Sdarrenr	 * Japanese (EUC) locale.
81145516Sdarrenr	 */
82145516Sdarrenr
83145516Sdarrenr	assert(strcmp(setlocale(LC_CTYPE, "ja_JP.eucJP"), "ja_JP.eucJP") == 0);
84145516Sdarrenr	assert(MB_CUR_MAX == 3);
85145516Sdarrenr
86145516Sdarrenr	/* No shift states in EUC encoding. */
87145516Sdarrenr	assert(wctomb(NULL, L'\0') == 0);
88145516Sdarrenr
89145516Sdarrenr	/* Null wide character. */
90145516Sdarrenr	memset(buf, 0xcc, sizeof(buf));
91145516Sdarrenr	len = wctomb(buf, L'\0');
92145516Sdarrenr	assert(len == 1);
93145516Sdarrenr	assert((unsigned char)buf[0] == 0 && (unsigned char)buf[1] == 0xcc);
94145516Sdarrenr
95145516Sdarrenr	/* Latin letter A. */
96145516Sdarrenr	memset(buf, 0xcc, sizeof(buf));
97145516Sdarrenr	len = wctomb(buf, L'A');
98145516Sdarrenr	assert(len == 1);
99145516Sdarrenr	assert((unsigned char)buf[0] == 'A' && (unsigned char)buf[1] == 0xcc);
100145516Sdarrenr
101145516Sdarrenr	/* Full width letter A. */
102145516Sdarrenr	memset(buf, 0xcc, sizeof(buf));
103145516Sdarrenr	len = wctomb(buf, 0xa3c1);
104145516Sdarrenr	assert(len == 2);
105145516Sdarrenr	assert((unsigned char)buf[0] == 0xa3 &&
106145516Sdarrenr		(unsigned char)buf[1] == 0xc1 &&
107145516Sdarrenr		(unsigned char)buf[2] == 0xcc);
108145516Sdarrenr
109145516Sdarrenr	printf("ok 1 - wctomb()\n");
110145516Sdarrenr
111145516Sdarrenr	return (0);
112145516Sdarrenr}
113145516Sdarrenr