1105973Stjr/*-
2105973Stjr * Copyright (c) 2002 Tim J. Robbins
3105973Stjr * All rights reserved.
4105973Stjr *
5105973Stjr * Redistribution and use in source and binary forms, with or without
6105973Stjr * modification, are permitted provided that the following conditions
7105973Stjr * are met:
8105973Stjr * 1. Redistributions of source code must retain the above copyright
9105973Stjr *    notice, this list of conditions and the following disclaimer.
10105973Stjr * 2. Redistributions in binary form must reproduce the above copyright
11105973Stjr *    notice, this list of conditions and the following disclaimer in the
12105973Stjr *    documentation and/or other materials provided with the distribution.
13105973Stjr *
14105973Stjr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15105973Stjr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16105973Stjr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17105973Stjr * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18105973Stjr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19105973Stjr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20105973Stjr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21105973Stjr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22105973Stjr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23105973Stjr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24105973Stjr * SUCH DAMAGE.
25105973Stjr */
26105973Stjr
27105973Stjr/*
28105973Stjr * Test program for wcrtomb(), as specified by IEEE Std. 1003.1-2001 and
29105973Stjr * ISO/IEC 9899:1999.
30105973Stjr *
31106495Stjr * The function is tested with both the "C" ("POSIX") LC_CTYPE setting and
32106495Stjr * "ja_JP.eucJP". Other encodings are not tested.
33105973Stjr */
34105973Stjr
35105973Stjr#include <sys/cdefs.h>
36105973Stjr__FBSDID("$FreeBSD$");
37105973Stjr
38105973Stjr#include <assert.h>
39105973Stjr#include <errno.h>
40105973Stjr#include <limits.h>
41105973Stjr#include <locale.h>
42106495Stjr#include <stdio.h>
43105973Stjr#include <stdlib.h>
44105973Stjr#include <string.h>
45105973Stjr#include <wchar.h>
46105973Stjr
47105973Stjrint
48105973Stjrmain(int argc, char *argv[])
49105973Stjr{
50105973Stjr	mbstate_t s;
51105973Stjr	size_t len;
52105973Stjr	char buf[MB_LEN_MAX + 1];
53105973Stjr
54105973Stjr	/*
55105973Stjr	 * C/POSIX locale.
56105973Stjr	 */
57105973Stjr
58137587Snik	printf("1..1\n");
59137587Snik
60105973Stjr	assert(MB_CUR_MAX == 1);
61105973Stjr
62108069Stjr	/*
63108069Stjr	 * If the buffer argument is NULL, wc is implicitly L'\0',
64108069Stjr	 * wcrtomb() resets its internal state.
65108069Stjr	 */
66105973Stjr	assert(wcrtomb(NULL, L'\0', NULL) == 1);
67108069Stjr	assert(wcrtomb(NULL, UCHAR_MAX + 1, NULL) == 1);
68105973Stjr
69105973Stjr	/* Null wide character. */
70105973Stjr	memset(&s, 0, sizeof(s));
71105973Stjr	memset(buf, 0xcc, sizeof(buf));
72105973Stjr	len = wcrtomb(buf, L'\0', &s);
73105973Stjr	assert(len == 1);
74105973Stjr	assert((unsigned char)buf[0] == 0 && (unsigned char)buf[1] == 0xcc);
75105973Stjr
76108069Stjr	/* Latin letter A, internal state. */
77108069Stjr	assert(wcrtomb(NULL, L'\0', NULL) == 1);
78105973Stjr	assert(wcrtomb(NULL, L'A', NULL) == 1);
79105973Stjr
80105973Stjr	/* Latin letter A. */
81105973Stjr	memset(&s, 0, sizeof(s));
82105973Stjr	memset(buf, 0xcc, sizeof(buf));
83105973Stjr	len = wcrtomb(buf, L'A', &s);
84105973Stjr	assert(len == 1);
85105973Stjr	assert((unsigned char)buf[0] == 'A' && (unsigned char)buf[1] == 0xcc);
86105973Stjr
87105973Stjr	/* Invalid code. */
88108069Stjr	assert(wcrtomb(buf, UCHAR_MAX + 1, NULL) == (size_t)-1);
89105973Stjr	assert(errno == EILSEQ);
90105973Stjr
91105973Stjr	/*
92105973Stjr	 * Japanese (EUC) locale.
93105973Stjr	 */
94105973Stjr
95105973Stjr	assert(strcmp(setlocale(LC_CTYPE, "ja_JP.eucJP"), "ja_JP.eucJP") == 0);
96105973Stjr	assert(MB_CUR_MAX == 3);
97105973Stjr
98108069Stjr	/*
99108069Stjr	 * If the buffer argument is NULL, wc is implicitly L'\0',
100108069Stjr	 * wcrtomb() resets its internal state.
101108069Stjr	 */
102108069Stjr	assert(wcrtomb(NULL, L'\0', NULL) == 1);
103105973Stjr
104105973Stjr	/* Null wide character. */
105105973Stjr	memset(&s, 0, sizeof(s));
106105973Stjr	memset(buf, 0xcc, sizeof(buf));
107105973Stjr	len = wcrtomb(buf, L'\0', &s);
108105973Stjr	assert(len == 1);
109105973Stjr	assert((unsigned char)buf[0] == 0 && (unsigned char)buf[1] == 0xcc);
110105973Stjr
111108069Stjr	/* Latin letter A, internal state. */
112108069Stjr	assert(wcrtomb(NULL, L'\0', NULL) == 1);
113105973Stjr	assert(wcrtomb(NULL, L'A', NULL) == 1);
114105973Stjr
115105973Stjr	/* Latin letter A. */
116105973Stjr	memset(&s, 0, sizeof(s));
117105973Stjr	memset(buf, 0xcc, sizeof(buf));
118105973Stjr	len = wcrtomb(buf, L'A', &s);
119105973Stjr	assert(len == 1);
120105973Stjr	assert((unsigned char)buf[0] == 'A' && (unsigned char)buf[1] == 0xcc);
121105973Stjr
122105973Stjr	/* Full width letter A. */
123105973Stjr	memset(&s, 0, sizeof(s));
124105973Stjr	memset(buf, 0xcc, sizeof(buf));
125105973Stjr	len = wcrtomb(buf, 0xa3c1, &s);
126105973Stjr	assert(len == 2);
127105973Stjr	assert((unsigned char)buf[0] == 0xa3 &&
128105973Stjr		(unsigned char)buf[1] == 0xc1 &&
129105973Stjr		(unsigned char)buf[2] == 0xcc);
130105973Stjr
131137587Snik	printf("ok 1 - wcrtomb()\n");
132105973Stjr
133105973Stjr	return (0);
134105973Stjr}
135