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 wctomb(), as specified by IEEE Std. 1003.1-2001 and
29106686Stjr * ISO/IEC 9899:1999.
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 <errno.h>
40106686Stjr#include <limits.h>
41106686Stjr#include <locale.h>
42106686Stjr#include <stdio.h>
43106686Stjr#include <stdlib.h>
44106686Stjr#include <string.h>
45106686Stjr
46106686Stjrint
47106686Stjrmain(int argc, char *argv[])
48106686Stjr{
49106686Stjr	size_t len;
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(wctomb(NULL, L'\0') == 0);
62106686Stjr
63106686Stjr	/* Null wide character. */
64106686Stjr	memset(buf, 0xcc, sizeof(buf));
65106686Stjr	len = wctomb(buf, L'\0');
66106686Stjr	assert(len == 1);
67106686Stjr	assert((unsigned char)buf[0] == 0 && (unsigned char)buf[1] == 0xcc);
68106686Stjr
69106686Stjr	/* Latin letter A. */
70106686Stjr	memset(buf, 0xcc, sizeof(buf));
71106686Stjr	len = wctomb(buf, L'A');
72106686Stjr	assert(len == 1);
73106686Stjr	assert((unsigned char)buf[0] == 'A' && (unsigned char)buf[1] == 0xcc);
74106686Stjr
75106686Stjr	/* Invalid code. */
76106686Stjr	assert(wctomb(buf, UCHAR_MAX + 1) == -1);
77128005Stjr	assert(wctomb(NULL, 0) == 0);
78106686Stjr
79106686Stjr	/*
80106686Stjr	 * Japanese (EUC) locale.
81106686Stjr	 */
82106686Stjr
83106686Stjr	assert(strcmp(setlocale(LC_CTYPE, "ja_JP.eucJP"), "ja_JP.eucJP") == 0);
84106686Stjr	assert(MB_CUR_MAX == 3);
85106686Stjr
86106686Stjr	/* No shift states in EUC encoding. */
87106686Stjr	assert(wctomb(NULL, L'\0') == 0);
88106686Stjr
89106686Stjr	/* Null wide character. */
90106686Stjr	memset(buf, 0xcc, sizeof(buf));
91106686Stjr	len = wctomb(buf, L'\0');
92106686Stjr	assert(len == 1);
93106686Stjr	assert((unsigned char)buf[0] == 0 && (unsigned char)buf[1] == 0xcc);
94106686Stjr
95106686Stjr	/* Latin letter A. */
96106686Stjr	memset(buf, 0xcc, sizeof(buf));
97106686Stjr	len = wctomb(buf, L'A');
98106686Stjr	assert(len == 1);
99106686Stjr	assert((unsigned char)buf[0] == 'A' && (unsigned char)buf[1] == 0xcc);
100106686Stjr
101106686Stjr	/* Full width letter A. */
102106686Stjr	memset(buf, 0xcc, sizeof(buf));
103106686Stjr	len = wctomb(buf, 0xa3c1);
104106686Stjr	assert(len == 2);
105106686Stjr	assert((unsigned char)buf[0] == 0xa3 &&
106106686Stjr		(unsigned char)buf[1] == 0xc1 &&
107106686Stjr		(unsigned char)buf[2] == 0xcc);
108106686Stjr
109137587Snik	printf("ok 1 - wctomb()\n");
110106686Stjr
111106686Stjr	return (0);
112106686Stjr}
113