1105980Stjr/*-
2105980Stjr * Copyright (c) 2002 Tim J. Robbins
3105980Stjr * All rights reserved.
4105980Stjr *
5105980Stjr * Redistribution and use in source and binary forms, with or without
6105980Stjr * modification, are permitted provided that the following conditions
7105980Stjr * are met:
8105980Stjr * 1. Redistributions of source code must retain the above copyright
9105980Stjr *    notice, this list of conditions and the following disclaimer.
10105980Stjr * 2. Redistributions in binary form must reproduce the above copyright
11105980Stjr *    notice, this list of conditions and the following disclaimer in the
12105980Stjr *    documentation and/or other materials provided with the distribution.
13105980Stjr *
14105980Stjr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15105980Stjr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16105980Stjr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17105980Stjr * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18105980Stjr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19105980Stjr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20105980Stjr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21105980Stjr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22105980Stjr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23105980Stjr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24105980Stjr * SUCH DAMAGE.
25105980Stjr */
26105980Stjr
27105980Stjr/*
28105980Stjr * Test program for mbrlen(), as specified by IEEE Std. 1003.1-2001 and
29105980Stjr * ISO/IEC 9899:1999.
30105980Stjr *
31105980Stjr * The function is tested with both the "C" ("POSIX") LC_CTYPE setting and
32105980Stjr * "ja_JP.eucJP". Other encodings are not tested.
33105980Stjr */
34105980Stjr
35105980Stjr#include <sys/cdefs.h>
36105980Stjr__FBSDID("$FreeBSD$");
37105980Stjr
38105980Stjr#include <assert.h>
39105980Stjr#include <errno.h>
40105980Stjr#include <limits.h>
41105980Stjr#include <locale.h>
42105980Stjr#include <stdio.h>
43105980Stjr#include <stdlib.h>
44105980Stjr#include <string.h>
45105980Stjr#include <wchar.h>
46105980Stjr
47105980Stjrint
48105980Stjrmain(int argc, char *argv[])
49105980Stjr{
50105980Stjr	mbstate_t s;
51105980Stjr	size_t len;
52105980Stjr	char buf[MB_LEN_MAX + 1];
53105980Stjr
54105980Stjr	/*
55105980Stjr	 * C/POSIX locale.
56105980Stjr	 */
57137587Snik
58137587Snik	printf("1..1\n");
59105980Stjr
60105980Stjr	assert(MB_CUR_MAX == 1);
61105980Stjr
62105980Stjr	/* Null wide character, internal state. */
63105980Stjr	memset(buf, 0xcc, sizeof(buf));
64105980Stjr	buf[0] = 0;
65105980Stjr	assert(mbrlen(buf, 1, NULL) == 0);
66105980Stjr
67105980Stjr	/* Null wide character. */
68105980Stjr	memset(&s, 0, sizeof(s));
69105980Stjr	assert(mbrlen(buf, 1, &s) == 0);
70105980Stjr
71105980Stjr	/* Latin letter A, internal state. */
72105980Stjr	assert(mbrlen(NULL, 0, NULL) == 0);
73105980Stjr	buf[0] = 'A';
74105980Stjr	assert(mbrlen(buf, 1, NULL) == 1);
75105980Stjr
76105980Stjr	/* Latin letter A. */
77105980Stjr	memset(&s, 0, sizeof(s));
78105980Stjr	assert(mbrlen(buf, 1, &s) == 1);
79105980Stjr
80105980Stjr	/* Incomplete character sequence. */
81105980Stjr	memset(&s, 0, sizeof(s));
82105980Stjr	assert(mbrlen(buf, 0, &s) == (size_t)-2);
83105980Stjr
84105980Stjr	/*
85105980Stjr	 * Japanese (EUC) locale.
86105980Stjr	 */
87105980Stjr
88105980Stjr	assert(strcmp(setlocale(LC_CTYPE, "ja_JP.eucJP"), "ja_JP.eucJP") == 0);
89105980Stjr	assert(MB_CUR_MAX > 1);
90105980Stjr
91105980Stjr	/* Null wide character, internal state. */
92105980Stjr	assert(mbrlen(NULL, 0, NULL) == 0);
93105980Stjr	memset(buf, 0xcc, sizeof(buf));
94105980Stjr	buf[0] = 0;
95105980Stjr	assert(mbrlen(buf, 1, NULL) == 0);
96105980Stjr
97105980Stjr	/* Null wide character. */
98105980Stjr	memset(&s, 0, sizeof(s));
99105980Stjr	assert(mbrlen(buf, 1, &s) == 0);
100105980Stjr
101105980Stjr	/* Latin letter A, internal state. */
102105980Stjr	assert(mbrlen(NULL, 0, NULL) == 0);
103105980Stjr	buf[0] = 'A';
104105980Stjr	assert(mbrlen(buf, 1, NULL) == 1);
105105980Stjr
106105980Stjr	/* Latin letter A. */
107105980Stjr	memset(&s, 0, sizeof(s));
108105980Stjr	assert(mbrlen(buf, 1, &s) == 1);
109105980Stjr
110105980Stjr	/* Incomplete character sequence (zero length). */
111105980Stjr	memset(&s, 0, sizeof(s));
112105980Stjr	assert(mbrlen(buf, 0, &s) == (size_t)-2);
113105980Stjr
114105980Stjr	/* Incomplete character sequence (truncated double-byte). */
115105980Stjr	memset(buf, 0xcc, sizeof(buf));
116105980Stjr	buf[0] = 0xa3;
117105980Stjr	buf[1] = 0x00;
118105980Stjr	memset(&s, 0, sizeof(s));
119105980Stjr	assert(mbrlen(buf, 1, &s) == (size_t)-2);
120105980Stjr
121105980Stjr	/* Same as above, but complete. */
122105980Stjr	buf[1] = 0xc1;
123105980Stjr	memset(&s, 0, sizeof(s));
124105980Stjr	assert(mbrlen(buf, 2, &s) == 2);
125105980Stjr
126137587Snik	printf("ok 1 - mbrlen()\n");
127105980Stjr
128105980Stjr	return (0);
129105980Stjr}
130