11590Srgrimes/*-
21590Srgrimes * Copyright (c) 2002-2004 Tim J. Robbins
31590Srgrimes * All rights reserved.
41590Srgrimes *
51590Srgrimes * Redistribution and use in source and binary forms, with or without
61590Srgrimes * modification, are permitted provided that the following conditions
71590Srgrimes * are met:
81590Srgrimes * 1. Redistributions of source code must retain the above copyright
91590Srgrimes *    notice, this list of conditions and the following disclaimer.
101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111590Srgrimes *    notice, this list of conditions and the following disclaimer in the
121590Srgrimes *    documentation and/or other materials provided with the distribution.
131590Srgrimes *
141590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
151590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
161590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
171590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
181590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
191590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
201590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
211590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
221590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
231590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
241590Srgrimes * SUCH DAMAGE.
251590Srgrimes */
261590Srgrimes
271590Srgrimes/*
281590Srgrimes * Test program for mbtowc(), as specified by IEEE Std. 1003.1-2001 and
291590Srgrimes * ISO/IEC 9899:1990.
301590Srgrimes *
311590Srgrimes * The function is tested with both the "C" ("POSIX") LC_CTYPE setting and
321590Srgrimes * "ja_JP.eucJP". Other encodings are not tested.
331590Srgrimes */
341590Srgrimes
351590Srgrimes#include <sys/cdefs.h>
361590Srgrimes__FBSDID("$FreeBSD$");
371590Srgrimes
3827270Scharnier#include <assert.h>
391590Srgrimes#include <limits.h>
401590Srgrimes#include <locale.h>
411590Srgrimes#include <stdio.h>
421590Srgrimes#include <stdlib.h>
431590Srgrimes#include <string.h>
4427270Scharnier
451590Srgrimesint
4627270Scharniermain(int argc, char *argv[])
471590Srgrimes{
481590Srgrimes	size_t len;
4987751Scharnier	wchar_t wc;
5087751Scharnier	char buf[MB_LEN_MAX + 1];
5187751Scharnier
5227270Scharnier	/*
5394978Stjr	 * C/POSIX locale.
5495033Sache	 */
551590Srgrimes
5627270Scharnier	printf("1..1\n");
57200462Sdelphij
5827270Scharnier	assert(MB_CUR_MAX == 1);
59131056Stjr
60131056Stjr	/* No shift states in C locale. */
611590Srgrimes	assert(mbtowc(NULL, NULL, 0) == 0);
621590Srgrimes
631590Srgrimes	/* Null wide character. */
6492920Simp	wc = 0xcccc;
65131056Stjr	memset(buf, 0, sizeof(buf));
6692920Simp	assert(mbtowc(&wc, buf, 1) == 0);
6727270Scharnier	assert(wc == 0);
6894978Stjr
6994978Stjr	/* Latin letter A. */
7094978Stjr	buf[0] = 'A';
7127270Scharnier	assert(mbtowc(&wc, buf, 1) == 1);
72102944Sdwmalone	assert(wc == L'A');
731590Srgrimes
74102944Sdwmalone	/* Incomplete character sequence. */
7597266Stjr	wc = L'z';
761590Srgrimes	buf[0] = '\0';
771590Srgrimes	assert(mbtowc(&wc, buf, 0) == -1);
7895033Sache	assert(wc == L'z');
7995033Sache	assert(mbtowc(NULL, NULL, 0) == 0);
801590Srgrimes
8194978Stjr	/*
821590Srgrimes	 * Japanese (EUC) locale.
8394978Stjr	 */
8494978Stjr
8594978Stjr	assert(strcmp(setlocale(LC_CTYPE, "ja_JP.eucJP"), "ja_JP.eucJP") == 0);
8694978Stjr	assert(MB_CUR_MAX > 1);
8794978Stjr
8894978Stjr	/* Null wide character */
891590Srgrimes	memset(buf, 0xcc, sizeof(buf));
901590Srgrimes	buf[0] = 0;
9127270Scharnier	wc = 0xcccc;
921590Srgrimes	assert(mbtowc(&wc, buf, 1) == 0);
931590Srgrimes	assert(wc == 0);
941590Srgrimes
951590Srgrimes	/* Latin letter A. */
961590Srgrimes	buf[0] = 'A';
971590Srgrimes	assert(mbtowc(&wc, buf, 1) == 1);
981590Srgrimes	assert(wc == L'A');
991590Srgrimes
1001590Srgrimes	/* Incomplete character sequence (zero length). */
1011590Srgrimes	wc = L'z';
1021590Srgrimes	buf[0] = '\0';
1031590Srgrimes	assert(mbtowc(&wc, buf, 0) == -1);
1041590Srgrimes	assert(wc == L'z');
10527270Scharnier	assert(mbtowc(NULL, NULL, 0) == 0);
1061590Srgrimes
1071590Srgrimes	/* Incomplete character sequence (truncated double-byte). */
1081590Srgrimes	memset(buf, 0xcc, sizeof(buf));
1091590Srgrimes	buf[0] = 0xa3;
1101590Srgrimes	buf[1] = 0x00;
1111590Srgrimes	wc = L'z';
11297266Stjr	assert(mbtowc(&wc, buf, 1) == -1);
1131590Srgrimes	assert(wc == L'z');
1141590Srgrimes	assert(mbtowc(NULL, NULL, 0) == 0);
1151590Srgrimes
1161590Srgrimes	/* Same as above, but complete. */
11797266Stjr	buf[1] = 0xc1;
11897266Stjr	assert(mbtowc(&wc, buf, 2) == 2);
1191590Srgrimes	assert(wc == 0xa3c1);
1201590Srgrimes
12197266Stjr	printf("ok 1 - mbtowc()\n");
1221590Srgrimes
1231590Srgrimes	return (0);
12427270Scharnier}
125102944Sdwmalone