1*9da7d79fSEnji Cooper /*-
2*9da7d79fSEnji Cooper * Copyright (c) 2002 Tim J. Robbins
3*9da7d79fSEnji Cooper * All rights reserved.
4*9da7d79fSEnji Cooper *
5*9da7d79fSEnji Cooper * Redistribution and use in source and binary forms, with or without
6*9da7d79fSEnji Cooper * modification, are permitted provided that the following conditions
7*9da7d79fSEnji Cooper * are met:
8*9da7d79fSEnji Cooper * 1. Redistributions of source code must retain the above copyright
9*9da7d79fSEnji Cooper * notice, this list of conditions and the following disclaimer.
10*9da7d79fSEnji Cooper * 2. Redistributions in binary form must reproduce the above copyright
11*9da7d79fSEnji Cooper * notice, this list of conditions and the following disclaimer in the
12*9da7d79fSEnji Cooper * documentation and/or other materials provided with the distribution.
13*9da7d79fSEnji Cooper *
14*9da7d79fSEnji Cooper * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*9da7d79fSEnji Cooper * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*9da7d79fSEnji Cooper * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*9da7d79fSEnji Cooper * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*9da7d79fSEnji Cooper * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*9da7d79fSEnji Cooper * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*9da7d79fSEnji Cooper * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*9da7d79fSEnji Cooper * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*9da7d79fSEnji Cooper * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*9da7d79fSEnji Cooper * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*9da7d79fSEnji Cooper * SUCH DAMAGE.
25*9da7d79fSEnji Cooper */
26*9da7d79fSEnji Cooper
27*9da7d79fSEnji Cooper /*
28*9da7d79fSEnji Cooper * Test program for mbrlen(), as specified by IEEE Std. 1003.1-2001 and
29*9da7d79fSEnji Cooper * ISO/IEC 9899:1999.
30*9da7d79fSEnji Cooper *
31*9da7d79fSEnji Cooper * The function is tested with both the "C" ("POSIX") LC_CTYPE setting and
32*9da7d79fSEnji Cooper * "ja_JP.eucJP". Other encodings are not tested.
33*9da7d79fSEnji Cooper */
34*9da7d79fSEnji Cooper
35*9da7d79fSEnji Cooper #include <errno.h>
36*9da7d79fSEnji Cooper #include <limits.h>
37*9da7d79fSEnji Cooper #include <locale.h>
38*9da7d79fSEnji Cooper #include <stdio.h>
39*9da7d79fSEnji Cooper #include <stdlib.h>
40*9da7d79fSEnji Cooper #include <string.h>
41*9da7d79fSEnji Cooper #include <wchar.h>
42*9da7d79fSEnji Cooper
43*9da7d79fSEnji Cooper #include <atf-c.h>
44*9da7d79fSEnji Cooper
45*9da7d79fSEnji Cooper ATF_TC_WITHOUT_HEAD(mbrlen_test);
ATF_TC_BODY(mbrlen_test,tc)46*9da7d79fSEnji Cooper ATF_TC_BODY(mbrlen_test, tc)
47*9da7d79fSEnji Cooper {
48*9da7d79fSEnji Cooper mbstate_t s;
49*9da7d79fSEnji Cooper char buf[MB_LEN_MAX + 1];
50*9da7d79fSEnji Cooper
51*9da7d79fSEnji Cooper /* C/POSIX locale. */
52*9da7d79fSEnji Cooper ATF_REQUIRE(MB_CUR_MAX == 1);
53*9da7d79fSEnji Cooper
54*9da7d79fSEnji Cooper /* Null wide character, internal state. */
55*9da7d79fSEnji Cooper memset(buf, 0xcc, sizeof(buf));
56*9da7d79fSEnji Cooper buf[0] = 0;
57*9da7d79fSEnji Cooper ATF_REQUIRE(mbrlen(buf, 1, NULL) == 0);
58*9da7d79fSEnji Cooper
59*9da7d79fSEnji Cooper /* Null wide character. */
60*9da7d79fSEnji Cooper memset(&s, 0, sizeof(s));
61*9da7d79fSEnji Cooper ATF_REQUIRE(mbrlen(buf, 1, &s) == 0);
62*9da7d79fSEnji Cooper
63*9da7d79fSEnji Cooper /* Latin letter A, internal state. */
64*9da7d79fSEnji Cooper ATF_REQUIRE(mbrlen(NULL, 0, NULL) == 0);
65*9da7d79fSEnji Cooper buf[0] = 'A';
66*9da7d79fSEnji Cooper ATF_REQUIRE(mbrlen(buf, 1, NULL) == 1);
67*9da7d79fSEnji Cooper
68*9da7d79fSEnji Cooper /* Latin letter A. */
69*9da7d79fSEnji Cooper memset(&s, 0, sizeof(s));
70*9da7d79fSEnji Cooper ATF_REQUIRE(mbrlen(buf, 1, &s) == 1);
71*9da7d79fSEnji Cooper
72*9da7d79fSEnji Cooper /* Incomplete character sequence. */
73*9da7d79fSEnji Cooper memset(&s, 0, sizeof(s));
74*9da7d79fSEnji Cooper ATF_REQUIRE(mbrlen(buf, 0, &s) == (size_t)-2);
75*9da7d79fSEnji Cooper
76*9da7d79fSEnji Cooper /* Japanese (EUC) locale. */
77*9da7d79fSEnji Cooper
78*9da7d79fSEnji Cooper ATF_REQUIRE(strcmp(setlocale(LC_CTYPE, "ja_JP.eucJP"), "ja_JP.eucJP") == 0);
79*9da7d79fSEnji Cooper ATF_REQUIRE(MB_CUR_MAX > 1);
80*9da7d79fSEnji Cooper
81*9da7d79fSEnji Cooper /* Null wide character, internal state. */
82*9da7d79fSEnji Cooper ATF_REQUIRE(mbrlen(NULL, 0, NULL) == 0);
83*9da7d79fSEnji Cooper memset(buf, 0xcc, sizeof(buf));
84*9da7d79fSEnji Cooper buf[0] = 0;
85*9da7d79fSEnji Cooper ATF_REQUIRE(mbrlen(buf, 1, NULL) == 0);
86*9da7d79fSEnji Cooper
87*9da7d79fSEnji Cooper /* Null wide character. */
88*9da7d79fSEnji Cooper memset(&s, 0, sizeof(s));
89*9da7d79fSEnji Cooper ATF_REQUIRE(mbrlen(buf, 1, &s) == 0);
90*9da7d79fSEnji Cooper
91*9da7d79fSEnji Cooper /* Latin letter A, internal state. */
92*9da7d79fSEnji Cooper ATF_REQUIRE(mbrlen(NULL, 0, NULL) == 0);
93*9da7d79fSEnji Cooper buf[0] = 'A';
94*9da7d79fSEnji Cooper ATF_REQUIRE(mbrlen(buf, 1, NULL) == 1);
95*9da7d79fSEnji Cooper
96*9da7d79fSEnji Cooper /* Latin letter A. */
97*9da7d79fSEnji Cooper memset(&s, 0, sizeof(s));
98*9da7d79fSEnji Cooper ATF_REQUIRE(mbrlen(buf, 1, &s) == 1);
99*9da7d79fSEnji Cooper
100*9da7d79fSEnji Cooper /* Incomplete character sequence (zero length). */
101*9da7d79fSEnji Cooper memset(&s, 0, sizeof(s));
102*9da7d79fSEnji Cooper ATF_REQUIRE(mbrlen(buf, 0, &s) == (size_t)-2);
103*9da7d79fSEnji Cooper
104*9da7d79fSEnji Cooper /* Incomplete character sequence (truncated double-byte). */
105*9da7d79fSEnji Cooper memset(buf, 0xcc, sizeof(buf));
106*9da7d79fSEnji Cooper buf[0] = 0xa3;
107*9da7d79fSEnji Cooper buf[1] = 0x00;
108*9da7d79fSEnji Cooper memset(&s, 0, sizeof(s));
109*9da7d79fSEnji Cooper ATF_REQUIRE(mbrlen(buf, 1, &s) == (size_t)-2);
110*9da7d79fSEnji Cooper
111*9da7d79fSEnji Cooper /* Same as above, but complete. */
112*9da7d79fSEnji Cooper buf[1] = 0xc1;
113*9da7d79fSEnji Cooper memset(&s, 0, sizeof(s));
114*9da7d79fSEnji Cooper ATF_REQUIRE(mbrlen(buf, 2, &s) == 2);
115*9da7d79fSEnji Cooper }
116*9da7d79fSEnji Cooper
ATF_TP_ADD_TCS(tp)117*9da7d79fSEnji Cooper ATF_TP_ADD_TCS(tp)
118*9da7d79fSEnji Cooper {
119*9da7d79fSEnji Cooper
120*9da7d79fSEnji Cooper ATF_TP_ADD_TC(tp, mbrlen_test);
121*9da7d79fSEnji Cooper
122*9da7d79fSEnji Cooper return (atf_no_error());
123*9da7d79fSEnji Cooper }
124