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 mblen(), as specified by IEEE Std. 1003.1-2001 and
29106686Stjr * ISO/IEC 9899:1990.
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 <limits.h>
40106686Stjr#include <locale.h>
41106686Stjr#include <stdio.h>
42106686Stjr#include <stdlib.h>
43106686Stjr#include <string.h>
44106686Stjr
45106686Stjrint
46106686Stjrmain(int argc, char *argv[])
47106686Stjr{
48106686Stjr	size_t len;
49106686Stjr	char buf[MB_LEN_MAX + 1];
50106686Stjr
51106686Stjr	/*
52106686Stjr	 * C/POSIX locale.
53106686Stjr	 */
54106686Stjr
55137587Snik	printf("1..1\n");
56137587Snik
57106686Stjr	assert(MB_CUR_MAX == 1);
58106686Stjr
59106686Stjr	/* No shift states in C locale. */
60106686Stjr	assert(mblen(NULL, 0) == 0);
61106686Stjr
62106686Stjr	/* Null wide character. */
63106686Stjr	memset(buf, 0xcc, sizeof(buf));
64106686Stjr	buf[0] = '\0';
65106686Stjr	assert(mblen(buf, 1) == 0);
66106686Stjr
67106686Stjr	/* Latin letter A. */
68106686Stjr	buf[0] = 'A';
69106686Stjr	assert(mblen(buf, 1) == 1);
70106686Stjr
71106686Stjr	/* Incomplete character sequence. */
72106686Stjr	buf[0] = '\0';
73106686Stjr	assert(mblen(buf, 0) == -1);
74128005Stjr	assert(mblen(NULL, 0) == 0);
75106686Stjr
76106686Stjr	/*
77106686Stjr	 * Japanese (EUC) locale.
78106686Stjr	 */
79106686Stjr
80106686Stjr	assert(strcmp(setlocale(LC_CTYPE, "ja_JP.eucJP"), "ja_JP.eucJP") == 0);
81106686Stjr	assert(MB_CUR_MAX > 1);
82106686Stjr
83106686Stjr	/* No shift states in EUC. */
84106686Stjr	assert(mblen(NULL, 0) == 0);
85106686Stjr
86106686Stjr	/* Null wide character. */
87106686Stjr	memset(buf, 0xcc, sizeof(buf));
88106686Stjr	buf[0] = '\0';
89106686Stjr	assert(mblen(buf, 1) == 0);
90106686Stjr
91106686Stjr	/* Latin letter A. */
92106686Stjr	buf[0] = 'A';
93106686Stjr	assert(mblen(buf, 1) == 1);
94106686Stjr
95106686Stjr	/* Incomplete character sequence. */
96106686Stjr	buf[0] = '\0';
97106686Stjr	assert(mblen(buf, 0) == -1);
98128005Stjr	assert(mblen(NULL, 0) == 0);
99106686Stjr
100106686Stjr	/* Incomplete character sequence (truncated double-byte). */
101106686Stjr	memset(buf, 0xcc, sizeof(buf));
102106686Stjr	buf[0] = 0xa3;
103106686Stjr	buf[1] = 0x00;
104106686Stjr	assert(mblen(buf, 1) == -1);
105128005Stjr	assert(mblen(NULL, 0) == 0);
106106686Stjr
107106686Stjr	/* Same as above, but complete. */
108106686Stjr	buf[1] = 0xc1;
109106686Stjr	assert(mblen(buf, 2) == 2);
110106686Stjr
111137587Snik	printf("ok 1 - mblen()\n");
112106686Stjr
113106686Stjr	return (0);
114106686Stjr}
115