Home | History | Annotate | Line # | Download | only in locale
      1  1.7  riastrad /*	$NetBSD: t_c8rtomb.c,v 1.7 2024/08/19 16:22:10 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*-
      4  1.1  riastrad  * Copyright (c) 2002 Tim J. Robbins
      5  1.1  riastrad  * All rights reserved.
      6  1.1  riastrad  *
      7  1.1  riastrad  * Copyright (c) 2013 Ed Schouten <ed (at) FreeBSD.org>
      8  1.1  riastrad  * All rights reserved.
      9  1.1  riastrad  *
     10  1.1  riastrad  * Redistribution and use in source and binary forms, with or without
     11  1.1  riastrad  * modification, are permitted provided that the following conditions
     12  1.1  riastrad  * are met:
     13  1.1  riastrad  * 1. Redistributions of source code must retain the above copyright
     14  1.1  riastrad  *    notice, this list of conditions and the following disclaimer.
     15  1.1  riastrad  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  riastrad  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  riastrad  *    documentation and/or other materials provided with the distribution.
     18  1.1  riastrad  *
     19  1.1  riastrad  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     20  1.1  riastrad  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  1.1  riastrad  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  1.1  riastrad  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     23  1.1  riastrad  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  1.1  riastrad  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  1.1  riastrad  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  1.1  riastrad  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  1.1  riastrad  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  1.1  riastrad  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  1.1  riastrad  * SUCH DAMAGE.
     30  1.1  riastrad  */
     31  1.1  riastrad /*
     32  1.1  riastrad  * Test program for c8rtomb() as specified by C23.
     33  1.1  riastrad  */
     34  1.1  riastrad 
     35  1.1  riastrad #include <sys/cdefs.h>
     36  1.7  riastrad __RCSID("$NetBSD: t_c8rtomb.c,v 1.7 2024/08/19 16:22:10 riastradh Exp $");
     37  1.1  riastrad 
     38  1.1  riastrad #include <errno.h>
     39  1.1  riastrad #include <limits.h>
     40  1.1  riastrad #include <locale.h>
     41  1.1  riastrad #include <stdio.h>
     42  1.1  riastrad #include <string.h>
     43  1.1  riastrad #include <uchar.h>
     44  1.1  riastrad 
     45  1.1  riastrad #include <atf-c.h>
     46  1.1  riastrad 
     47  1.1  riastrad static void
     48  1.1  riastrad require_lc_ctype(const char *locale_name)
     49  1.1  riastrad {
     50  1.1  riastrad 	char *lc_ctype_set;
     51  1.1  riastrad 
     52  1.1  riastrad 	lc_ctype_set = setlocale(LC_CTYPE, locale_name);
     53  1.1  riastrad 	if (lc_ctype_set == NULL)
     54  1.1  riastrad 		atf_tc_fail("setlocale(LC_CTYPE, \"%s\") failed; errno=%d",
     55  1.1  riastrad 		    locale_name, errno);
     56  1.1  riastrad 
     57  1.1  riastrad 	ATF_REQUIRE_EQ_MSG(strcmp(lc_ctype_set, locale_name), 0,
     58  1.1  riastrad 	    "lc_ctype_set=%s locale_name=%s", lc_ctype_set, locale_name);
     59  1.1  riastrad }
     60  1.1  riastrad 
     61  1.1  riastrad static mbstate_t s;
     62  1.4  riastrad static char buf[7*MB_LEN_MAX + 1];
     63  1.1  riastrad 
     64  1.1  riastrad ATF_TC_WITHOUT_HEAD(c8rtomb_c_locale_test);
     65  1.1  riastrad ATF_TC_BODY(c8rtomb_c_locale_test, tc)
     66  1.1  riastrad {
     67  1.1  riastrad 	size_t n;
     68  1.1  riastrad 
     69  1.1  riastrad 	require_lc_ctype("C");
     70  1.1  riastrad 
     71  1.1  riastrad 	/*
     72  1.1  riastrad 	 * If the buffer argument is NULL, c8 is implicitly 0,
     73  1.1  riastrad 	 * c8rtomb() resets its internal state.
     74  1.1  riastrad 	 */
     75  1.1  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, '\0', NULL)), 1, "n=%zu", n);
     76  1.1  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 0x80, NULL)), 1, "n=%zu", n);
     77  1.1  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 0xc0, NULL)), 1, "n=%zu", n);
     78  1.1  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 0xe0, NULL)), 1, "n=%zu", n);
     79  1.1  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 0xf0, NULL)), 1, "n=%zu", n);
     80  1.1  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 0xf8, NULL)), 1, "n=%zu", n);
     81  1.1  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 0xfc, NULL)), 1, "n=%zu", n);
     82  1.1  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 0xfe, NULL)), 1, "n=%zu", n);
     83  1.1  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 0xff, NULL)), 1, "n=%zu", n);
     84  1.1  riastrad 
     85  1.1  riastrad 	/* Null wide character. */
     86  1.1  riastrad 	memset(&s, 0, sizeof(s));
     87  1.1  riastrad 	memset(buf, 0xcc, sizeof(buf));
     88  1.1  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0, &s)), 1, "n=%zu", n);
     89  1.1  riastrad 	ATF_CHECK_MSG(((unsigned char)buf[0] == 0 &&
     90  1.1  riastrad 		(unsigned char)buf[1] == 0xcc),
     91  1.1  riastrad 	    "buf=[%02x %02x]", buf[0], buf[1]);
     92  1.1  riastrad 
     93  1.1  riastrad 	/* Latin letter A, internal state. */
     94  1.1  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, '\0', NULL)), 1, "n=%zu", n);
     95  1.1  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 'A', NULL)), 1, "n=%zu", n);
     96  1.1  riastrad 
     97  1.1  riastrad 	/* Latin letter A. */
     98  1.1  riastrad 	memset(&s, 0, sizeof(s));
     99  1.1  riastrad 	memset(buf, 0xcc, sizeof(buf));
    100  1.1  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 'A', &s)), 1, "n=%zu", n);
    101  1.1  riastrad 	ATF_CHECK_MSG(((unsigned char)buf[0] == 'A' &&
    102  1.1  riastrad 		(unsigned char)buf[1] == 0xcc),
    103  1.1  riastrad 	    "buf=[%02x %02x]", buf[0], buf[1]);
    104  1.1  riastrad 
    105  1.1  riastrad 	/* Unicode character 'Pile of poo'. */
    106  1.1  riastrad 	memset(&s, 0, sizeof(s));
    107  1.1  riastrad 	memset(buf, 0xcc, sizeof(buf));
    108  1.1  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xf0, &s)), 0, "n=%zu", n);
    109  1.1  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x9f, &s)), 0, "n=%zu", n);
    110  1.1  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x92, &s)), 0, "n=%zu", n);
    111  1.1  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xa9, &s)), (size_t)-1,
    112  1.1  riastrad 	    "n=%zu", n);
    113  1.1  riastrad 	ATF_CHECK_EQ_MSG(errno, EILSEQ, "errno=%d", errno);
    114  1.1  riastrad 	ATF_CHECK_EQ_MSG((unsigned char)buf[0], 0xcc, "buf=[%02x]", buf[0]);
    115  1.2  riastrad 
    116  1.2  riastrad 	/* Incomplete Unicode character 'Pile of poo', interrupted by NUL. */
    117  1.2  riastrad 	memset(&s, 0, sizeof(s));
    118  1.2  riastrad 	memset(buf, 0xcc, sizeof(buf));
    119  1.2  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xf0, &s)), 0, "n=%zu", n);
    120  1.2  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, '\0', &s)), 1, "n=%zu", n);
    121  1.2  riastrad 	ATF_CHECK_MSG(((unsigned char)buf[0] == '\0' &&
    122  1.2  riastrad 		(unsigned char)buf[1] == 0xcc),
    123  1.2  riastrad 	    "buf=[%02x %02x]", buf[0], buf[1]);
    124  1.2  riastrad 
    125  1.2  riastrad 	memset(&s, 0, sizeof(s));
    126  1.2  riastrad 	memset(buf, 0xcc, sizeof(buf));
    127  1.2  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xf0, &s)), 0, "n=%zu", n);
    128  1.2  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x9f, &s)), 0, "n=%zu", n);
    129  1.2  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, '\0', &s)), 1, "n=%zu", n);
    130  1.2  riastrad 	ATF_CHECK_MSG(((unsigned char)buf[0] == '\0' &&
    131  1.2  riastrad 		(unsigned char)buf[1] == 0xcc),
    132  1.2  riastrad 	    "buf=[%02x %02x]", buf[0], buf[1]);
    133  1.2  riastrad 
    134  1.2  riastrad 	memset(&s, 0, sizeof(s));
    135  1.2  riastrad 	memset(buf, 0xcc, sizeof(buf));
    136  1.2  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xf0, &s)), 0, "n=%zu", n);
    137  1.2  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x9f, &s)), 0, "n=%zu", n);
    138  1.2  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x92, &s)), 0, "n=%zu", n);
    139  1.2  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, '\0', &s)), 1, "n=%zu", n);
    140  1.2  riastrad 	ATF_CHECK_MSG(((unsigned char)buf[0] == '\0' &&
    141  1.2  riastrad 		(unsigned char)buf[1] == 0xcc),
    142  1.2  riastrad 	    "buf=[%02x %02x]", buf[0], buf[1]);
    143  1.1  riastrad }
    144  1.1  riastrad 
    145  1.4  riastrad ATF_TC_WITHOUT_HEAD(c8rtomb_iso2022jp_locale_test);
    146  1.4  riastrad ATF_TC_BODY(c8rtomb_iso2022jp_locale_test, tc)
    147  1.4  riastrad {
    148  1.4  riastrad 	char *p;
    149  1.4  riastrad 	size_t n;
    150  1.4  riastrad 
    151  1.4  riastrad 	require_lc_ctype("ja_JP.ISO-2022-JP");
    152  1.4  riastrad 
    153  1.4  riastrad 	/*
    154  1.4  riastrad 	 * If the buffer argument is NULL, c8 is implicitly 0,
    155  1.4  riastrad 	 * c8rtomb() resets its internal state.
    156  1.4  riastrad 	 */
    157  1.4  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, '\0', NULL)), 1, "n=%zu", n);
    158  1.4  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 0x80, NULL)), 1, "n=%zu", n);
    159  1.4  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 0xc0, NULL)), 1, "n=%zu", n);
    160  1.4  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 0xe0, NULL)), 1, "n=%zu", n);
    161  1.4  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 0xf0, NULL)), 1, "n=%zu", n);
    162  1.4  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 0xf8, NULL)), 1, "n=%zu", n);
    163  1.4  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 0xfc, NULL)), 1, "n=%zu", n);
    164  1.4  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 0xfe, NULL)), 1, "n=%zu", n);
    165  1.4  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 0xff, NULL)), 1, "n=%zu", n);
    166  1.4  riastrad 
    167  1.4  riastrad 	/* Null wide character. */
    168  1.4  riastrad 	memset(&s, 0, sizeof(s));
    169  1.4  riastrad 	memset(buf, 0xcc, sizeof(buf));
    170  1.4  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0, &s)), 1, "n=%zu", n);
    171  1.4  riastrad 	ATF_CHECK_MSG(((unsigned char)buf[0] == 0 &&
    172  1.4  riastrad 		(unsigned char)buf[1] == 0xcc),
    173  1.4  riastrad 	    "buf=[%02x %02x]", buf[0], buf[1]);
    174  1.4  riastrad 
    175  1.4  riastrad 	/* Latin letter A, internal state. */
    176  1.4  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, '\0', NULL)), 1, "n=%zu", n);
    177  1.4  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(NULL, 'A', NULL)), 1, "n=%zu", n);
    178  1.4  riastrad 
    179  1.4  riastrad 	/*
    180  1.4  riastrad 	 * 1. U+0042 LATIN CAPITAL LETTER A
    181  1.4  riastrad 	 * 2. U+00A5 YEN SIGN
    182  1.4  riastrad 	 * 3. U+00A5 YEN SIGN (again, no shift needed)
    183  1.4  riastrad 	 * 4. U+30A2 KATAKANA LETTER A
    184  1.4  riastrad 	 * 5. U+30A2 KATAKANA LETTER A (again, no shift needed)
    185  1.4  riastrad 	 * 6. incomplete UTF-8 multibyte sequence -- no output
    186  1.4  riastrad 	 * 7. U+0000 NUL (plus shift sequence to initial state)
    187  1.4  riastrad 	 */
    188  1.4  riastrad 	memset(&s, 0, sizeof(s));
    189  1.4  riastrad 	memset(buf, 0xcc, sizeof(buf));
    190  1.4  riastrad 	p = buf;
    191  1.4  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(p, 'A', &s)), 1, "n=%zu", n); /* 1 */
    192  1.4  riastrad 	p += 1;
    193  1.4  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(p, 0xc2, &s)), 0, "n=%zu", n); /* 2 */
    194  1.4  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(p, 0xa5, &s)), 4, "n=%zu", n);
    195  1.4  riastrad 	p += 4;
    196  1.4  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(p, 0xc2, &s)), 0, "n=%zu", n); /* 3 */
    197  1.4  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(p, 0xa5, &s)), 1, "n=%zu", n);
    198  1.4  riastrad 	p += 1;
    199  1.4  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(p, 0xe3, &s)), 0, "n=%zu", n); /* 4 */
    200  1.4  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(p, 0x82, &s)), 0, "n=%zu", n);
    201  1.5  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(p, 0xa2, &s)), 5, "n=%zu", n);
    202  1.4  riastrad 	p += 5;
    203  1.4  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(p, 0xe3, &s)), 0, "n=%zu", n); /* 5 */
    204  1.4  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(p, 0x82, &s)), 0, "n=%zu", n);
    205  1.4  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(p, 0xa2, &s)), 2, "n=%zu", n);
    206  1.4  riastrad 	p += 2;
    207  1.4  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(p, 0xe3, &s)), 0, "n=%zu", n); /* 6 */
    208  1.4  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(p, 0x82, &s)), 0, "n=%zu", n);
    209  1.4  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(p, '\0', &s)), 4, "n=%zu", n); /* 7 */
    210  1.4  riastrad 	p += 4;
    211  1.4  riastrad 	ATF_CHECK_MSG(((unsigned char)buf[0] == 'A' &&
    212  1.4  riastrad 		(unsigned char)buf[1] == 0x1b && /* shift ISO/IEC 646:JP */
    213  1.4  riastrad 		(unsigned char)buf[2] == '(' &&
    214  1.4  riastrad 		(unsigned char)buf[3] == 'J' &&
    215  1.4  riastrad 		(unsigned char)buf[4] == 0x5c && /* YEN SIGN */
    216  1.4  riastrad 		(unsigned char)buf[5] == 0x5c && /* YEN SIGN */
    217  1.6  riastrad 		(unsigned char)buf[6] == 0x1b && /* shift JIS X 0208 */
    218  1.4  riastrad 		(unsigned char)buf[7] == '$' &&
    219  1.4  riastrad 		(unsigned char)buf[8] == 'B' &&
    220  1.4  riastrad 		(unsigned char)buf[9] == 0x25 && /* KATAKANA LETTER A */
    221  1.4  riastrad 		(unsigned char)buf[10] == 0x22 &&
    222  1.4  riastrad 		(unsigned char)buf[11] == 0x25 && /* KATAKANA LETTER A */
    223  1.4  riastrad 		(unsigned char)buf[12] == 0x22 &&
    224  1.4  riastrad 		(unsigned char)buf[13] == 0x1b && /* shift US-ASCII */
    225  1.4  riastrad 		(unsigned char)buf[14] == '(' &&
    226  1.4  riastrad 		(unsigned char)buf[15] == 'B' &&
    227  1.4  riastrad 		(unsigned char)buf[16] == '\0' &&
    228  1.4  riastrad 		(unsigned char)buf[17] == 0xcc),
    229  1.4  riastrad 	    "buf=[%02x %02x %02x %02x  %02x %02x %02x %02x "
    230  1.4  riastrad 	    " %02x %02x %02x %02x  %02x %02x %02x %02x "
    231  1.4  riastrad 	    " %02x %02x]",
    232  1.4  riastrad 	    buf[0], buf[1], buf[2], buf[3],
    233  1.4  riastrad 	    buf[4], buf[5], buf[6], buf[7],
    234  1.4  riastrad 	    buf[8], buf[9], buf[10], buf[11],
    235  1.4  riastrad 	    buf[12], buf[13], buf[14], buf[15],
    236  1.4  riastrad 	    buf[16], buf[17]);
    237  1.4  riastrad }
    238  1.4  riastrad 
    239  1.1  riastrad ATF_TC_WITHOUT_HEAD(c8rtomb_iso_8859_1_test);
    240  1.1  riastrad ATF_TC_BODY(c8rtomb_iso_8859_1_test, tc)
    241  1.1  riastrad {
    242  1.1  riastrad 	size_t n;
    243  1.1  riastrad 
    244  1.1  riastrad 	require_lc_ctype("en_US.ISO8859-1");
    245  1.1  riastrad 
    246  1.1  riastrad 	/* Unicode character 'Euro sign'. */
    247  1.1  riastrad 	memset(&s, 0, sizeof(s));
    248  1.1  riastrad 	memset(buf, 0xcc, sizeof(buf));
    249  1.1  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xe2, &s)), 0, "n=%zu", n);
    250  1.1  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x82, &s)), 0, "n=%zu", n);
    251  1.1  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xac, &s)), (size_t)-1,
    252  1.1  riastrad 	    "n=%zu", n);
    253  1.1  riastrad 	ATF_CHECK_EQ_MSG(errno, EILSEQ, "errno=%d", errno);
    254  1.1  riastrad 	ATF_CHECK_EQ_MSG((unsigned char)buf[0], 0xcc, "buf=[%02x]", buf[0]);
    255  1.1  riastrad }
    256  1.1  riastrad 
    257  1.1  riastrad ATF_TC_WITHOUT_HEAD(c8rtomb_iso_8859_15_test);
    258  1.1  riastrad ATF_TC_BODY(c8rtomb_iso_8859_15_test, tc)
    259  1.1  riastrad {
    260  1.1  riastrad 	size_t n;
    261  1.1  riastrad 
    262  1.1  riastrad 	require_lc_ctype("en_US.ISO8859-15");
    263  1.1  riastrad 
    264  1.1  riastrad 	/* Unicode character 'Euro sign'. */
    265  1.1  riastrad 	memset(&s, 0, sizeof(s));
    266  1.1  riastrad 	memset(buf, 0xcc, sizeof(buf));
    267  1.1  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xe2, &s)), 0, "n=%zu", n);
    268  1.1  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x82, &s)), 0, "n=%zu", n);
    269  1.1  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xac, &s)), 1, "n=%zu", n);
    270  1.1  riastrad 	ATF_CHECK_MSG(((unsigned char)buf[0] == 0xa4 &&
    271  1.1  riastrad 		(unsigned char)buf[1] == 0xcc),
    272  1.1  riastrad 	    "buf=[%02x %02x]", buf[0], buf[1]);
    273  1.1  riastrad }
    274  1.1  riastrad 
    275  1.1  riastrad ATF_TC_WITHOUT_HEAD(c8rtomb_utf_8_test);
    276  1.1  riastrad ATF_TC_BODY(c8rtomb_utf_8_test, tc)
    277  1.1  riastrad {
    278  1.1  riastrad 	size_t n;
    279  1.1  riastrad 
    280  1.1  riastrad 	require_lc_ctype("en_US.UTF-8");
    281  1.1  riastrad 
    282  1.1  riastrad 	/* Unicode character 'Pile of poo'. */
    283  1.1  riastrad 	memset(&s, 0, sizeof(s));
    284  1.1  riastrad 	memset(buf, 0xcc, sizeof(buf));
    285  1.1  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xf0, &s)), 0, "n=%zu", n);
    286  1.1  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x9f, &s)), 0, "n=%zu", n);
    287  1.1  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x92, &s)), 0, "n=%zu", n);
    288  1.1  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xa9, &s)), 4, "n=%zu", n);
    289  1.1  riastrad 	ATF_CHECK_MSG(((unsigned char)buf[0] == 0xf0 &&
    290  1.1  riastrad 		(unsigned char)buf[1] == 0x9f &&
    291  1.1  riastrad 		(unsigned char)buf[2] == 0x92 &&
    292  1.1  riastrad 		(unsigned char)buf[3] == 0xa9 &&
    293  1.1  riastrad 		(unsigned char)buf[4] == 0xcc),
    294  1.1  riastrad 	    "buf=[%02x %02x %02x %02x %02x]",
    295  1.1  riastrad 	    buf[0], buf[1], buf[2], buf[3], buf[4]);
    296  1.1  riastrad 
    297  1.1  riastrad 	/* Invalid code; 'Pile of poo' without the last byte. */
    298  1.1  riastrad 	memset(&s, 0, sizeof(s));
    299  1.1  riastrad 	memset(buf, 0xcc, sizeof(buf));
    300  1.1  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xf0, &s)), 0, "n=%zu", n);
    301  1.1  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x9f, &s)), 0, "n=%zu", n);
    302  1.1  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x92, &s)), 0, "n=%zu", n);
    303  1.1  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 'A', &s)), (size_t)-1,
    304  1.1  riastrad 	    "n=%zu", n);
    305  1.1  riastrad 	ATF_CHECK_EQ_MSG(errno, EILSEQ, "errno=%d", errno);
    306  1.1  riastrad 	ATF_CHECK_EQ_MSG((unsigned char)buf[0], 0xcc, "buf=[%02x]", buf[0]);
    307  1.1  riastrad 
    308  1.1  riastrad 	/* Invalid code; 'Pile of poo' without the first byte. */
    309  1.1  riastrad 	memset(&s, 0, sizeof(s));
    310  1.1  riastrad 	memset(buf, 0xcc, sizeof(buf));
    311  1.1  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x9f, &s)), (size_t)-1,
    312  1.1  riastrad 	    "n=%zu", n);
    313  1.1  riastrad 	ATF_CHECK_EQ_MSG(errno, EILSEQ, "errno=%d", errno);
    314  1.1  riastrad 	ATF_CHECK_EQ_MSG((unsigned char)buf[0], 0xcc, "buf=[%02x]", buf[0]);
    315  1.2  riastrad 
    316  1.2  riastrad 	/* Incomplete Unicode character 'Pile of poo', interrupted by NUL. */
    317  1.2  riastrad 	memset(&s, 0, sizeof(s));
    318  1.2  riastrad 	memset(buf, 0xcc, sizeof(buf));
    319  1.2  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xf0, &s)), 0, "n=%zu", n);
    320  1.2  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, '\0', &s)), 1, "n=%zu", n);
    321  1.2  riastrad 	ATF_CHECK_MSG(((unsigned char)buf[0] == '\0' &&
    322  1.2  riastrad 		(unsigned char)buf[1] == 0xcc),
    323  1.2  riastrad 	    "buf=[%02x %02x]", buf[0], buf[1]);
    324  1.2  riastrad 
    325  1.2  riastrad 	memset(&s, 0, sizeof(s));
    326  1.2  riastrad 	memset(buf, 0xcc, sizeof(buf));
    327  1.2  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xf0, &s)), 0, "n=%zu", n);
    328  1.2  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x9f, &s)), 0, "n=%zu", n);
    329  1.2  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, '\0', &s)), 1, "n=%zu", n);
    330  1.2  riastrad 	ATF_CHECK_MSG(((unsigned char)buf[0] == '\0' &&
    331  1.2  riastrad 		(unsigned char)buf[1] == 0xcc),
    332  1.2  riastrad 	    "buf=[%02x %02x]", buf[0], buf[1]);
    333  1.2  riastrad 
    334  1.2  riastrad 	memset(&s, 0, sizeof(s));
    335  1.2  riastrad 	memset(buf, 0xcc, sizeof(buf));
    336  1.2  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0xf0, &s)), 0, "n=%zu", n);
    337  1.2  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x9f, &s)), 0, "n=%zu", n);
    338  1.2  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, 0x92, &s)), 0, "n=%zu", n);
    339  1.2  riastrad 	ATF_CHECK_EQ_MSG((n = c8rtomb(buf, '\0', &s)), 1, "n=%zu", n);
    340  1.2  riastrad 	ATF_CHECK_MSG(((unsigned char)buf[0] == '\0' &&
    341  1.2  riastrad 		(unsigned char)buf[1] == 0xcc),
    342  1.2  riastrad 	    "buf=[%02x %02x]", buf[0], buf[1]);
    343  1.1  riastrad }
    344  1.1  riastrad 
    345  1.1  riastrad ATF_TP_ADD_TCS(tp)
    346  1.1  riastrad {
    347  1.1  riastrad 
    348  1.1  riastrad 	ATF_TP_ADD_TC(tp, c8rtomb_c_locale_test);
    349  1.4  riastrad 	ATF_TP_ADD_TC(tp, c8rtomb_iso2022jp_locale_test);
    350  1.1  riastrad 	ATF_TP_ADD_TC(tp, c8rtomb_iso_8859_1_test);
    351  1.1  riastrad 	ATF_TP_ADD_TC(tp, c8rtomb_iso_8859_15_test);
    352  1.1  riastrad 	ATF_TP_ADD_TC(tp, c8rtomb_utf_8_test);
    353  1.1  riastrad 
    354  1.1  riastrad 	return (atf_no_error());
    355  1.1  riastrad }
    356