1106686Stjr/*-
2106686Stjr * Copyright (c) 2002 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 mbstowcs(), 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#include <wchar.h>
46106686Stjr
47106686Stjrint
48106686Stjrmain(int argc, char *argv[])
49106686Stjr{
50106686Stjr	char srcbuf[128];
51106686Stjr	wchar_t dstbuf[128];
52106686Stjr
53106686Stjr	/*
54106686Stjr	 * C/POSIX locale.
55106686Stjr	 */
56106686Stjr
57137587Snik	printf("1..1\n");
58137587Snik
59106686Stjr	/* Simple null terminated string. */
60106686Stjr	memset(srcbuf, 0xcc, sizeof(srcbuf));
61106686Stjr	strcpy(srcbuf, "hello");
62106686Stjr	wmemset(dstbuf, 0xcccc, sizeof(dstbuf) / sizeof(*dstbuf));
63106686Stjr	assert(mbstowcs(dstbuf, srcbuf, sizeof(dstbuf) / sizeof(*dstbuf)) == 5);
64106686Stjr	assert(wcscmp(dstbuf, L"hello") == 0);
65106686Stjr	assert(dstbuf[6] == 0xcccc);
66106686Stjr
67106686Stjr	/* Not enough space in destination buffer. */
68106686Stjr	memset(srcbuf, 0xcc, sizeof(srcbuf));
69106686Stjr	strcpy(srcbuf, "hello");
70106686Stjr	wmemset(dstbuf, 0xcccc, sizeof(dstbuf) / sizeof(*dstbuf));
71106686Stjr	assert(mbstowcs(dstbuf, srcbuf, 4) == 4);
72106686Stjr	assert(wmemcmp(dstbuf, L"hell", 4) == 0);
73106686Stjr	assert(dstbuf[5] == 0xcccc);
74106686Stjr
75106686Stjr	/* Null terminated string, internal dest. buffer (XSI extension) */
76106686Stjr	memset(srcbuf, 0xcc, sizeof(srcbuf));
77106686Stjr	strcpy(srcbuf, "hello");
78106686Stjr	assert(mbstowcs(NULL, srcbuf, 0) == 5);
79106686Stjr
80106686Stjr	/* Empty source buffer. */
81106686Stjr	memset(srcbuf, 0xcc, sizeof(srcbuf));
82106686Stjr	srcbuf[0] = '\0';
83106686Stjr	wmemset(dstbuf, 0xcccc, sizeof(dstbuf) / sizeof(*dstbuf));
84106686Stjr	assert(mbstowcs(dstbuf, srcbuf, 1) == 0);
85106686Stjr	assert(dstbuf[0] == 0);
86106686Stjr	assert(dstbuf[1] == 0xcccc);
87106686Stjr
88106686Stjr	/* Zero length destination buffer. */
89106686Stjr	memset(srcbuf, 0xcc, sizeof(srcbuf));
90106686Stjr	strcpy(srcbuf, "hello");
91106686Stjr	wmemset(dstbuf, 0xcccc, sizeof(dstbuf) / sizeof(*dstbuf));
92106686Stjr	assert(mbstowcs(dstbuf, srcbuf, 0) == 0);
93106686Stjr	assert(dstbuf[0] == 0xcccc);
94106686Stjr
95106686Stjr	/*
96106686Stjr	 * Japanese (EUC) locale.
97106686Stjr	 */
98106686Stjr
99106686Stjr	assert(strcmp(setlocale(LC_CTYPE, "ja_JP.eucJP"), "ja_JP.eucJP") == 0);
100106686Stjr	assert(MB_CUR_MAX > 1);
101106686Stjr
102106686Stjr	memset(srcbuf, 0xcc, sizeof(srcbuf));
103106686Stjr	strcpy(srcbuf, "\xA3\xC1 B \xA3\xC3");
104106686Stjr	wmemset(dstbuf, 0xcccc, sizeof(dstbuf) / sizeof(*dstbuf));
105106686Stjr	assert(mbstowcs(dstbuf, srcbuf, sizeof(dstbuf) / sizeof(*dstbuf)) == 5);
106106686Stjr	assert(dstbuf[0] == 0xA3C1 && dstbuf[1] == 0x20 && dstbuf[2] == 0x42 &&
107106686Stjr	    dstbuf[3] == 0x20 && dstbuf[4] == 0xA3C3 && dstbuf[5] == 0);
108106686Stjr
109137587Snik	printf("ok 1 - mbstowcs()\n");
110106686Stjr
111106686Stjr	return (0);
112106686Stjr}
113