Home | History | Annotate | Line # | Download | only in locale
t_c16rtomb.c revision 1.2
      1  1.2  riastrad /*	$NetBSD: t_c16rtomb.c,v 1.2 2024/08/17 21:31:22 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 c16rtomb() as specified by ISO/IEC 9899:2011.
     33  1.1  riastrad  */
     34  1.1  riastrad 
     35  1.1  riastrad #include <sys/cdefs.h>
     36  1.2  riastrad __RCSID("$NetBSD: t_c16rtomb.c,v 1.2 2024/08/17 21:31:22 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.1  riastrad static char buf[MB_LEN_MAX + 1];
     63  1.1  riastrad 
     64  1.1  riastrad ATF_TC_WITHOUT_HEAD(c16rtomb_c_locale_test);
     65  1.1  riastrad ATF_TC_BODY(c16rtomb_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, c16 is implicitly 0,
     73  1.1  riastrad 	 * c16rtomb() resets its internal state.
     74  1.1  riastrad 	 */
     75  1.1  riastrad 	ATF_CHECK_EQ_MSG((n = c16rtomb(NULL, L'\0', NULL)), 1, "n=%zu", n);
     76  1.1  riastrad 	ATF_CHECK_EQ_MSG((n = c16rtomb(NULL, 0xdc00, NULL)), 1, "n=%zu", n);
     77  1.1  riastrad 
     78  1.1  riastrad 	/* Null wide character. */
     79  1.1  riastrad 	memset(&s, 0, sizeof(s));
     80  1.1  riastrad 	memset(buf, 0xcc, sizeof(buf));
     81  1.1  riastrad 	ATF_CHECK_EQ_MSG((n = c16rtomb(buf, 0, &s)), 1, "n=%zu", n);
     82  1.1  riastrad 	ATF_CHECK_MSG(((unsigned char)buf[0] == 0 &&
     83  1.1  riastrad 		(unsigned char)buf[1] == 0xcc),
     84  1.1  riastrad 	    "buf=[%02x %02x]", buf[0], buf[1]);
     85  1.1  riastrad 
     86  1.1  riastrad 	/* Latin letter A, internal state. */
     87  1.1  riastrad 	ATF_CHECK_EQ_MSG((n = c16rtomb(NULL, L'\0', NULL)), 1, "n=%zu", n);
     88  1.1  riastrad 	ATF_CHECK_EQ_MSG((n = c16rtomb(NULL, L'A', NULL)), 1, "n=%zu", n);
     89  1.1  riastrad 
     90  1.1  riastrad 	/* Latin letter A. */
     91  1.1  riastrad 	memset(&s, 0, sizeof(s));
     92  1.1  riastrad 	memset(buf, 0xcc, sizeof(buf));
     93  1.1  riastrad 	ATF_CHECK_EQ_MSG((n = c16rtomb(buf, L'A', &s)), 1, "n=%zu", n);
     94  1.1  riastrad 	ATF_CHECK_MSG(((unsigned char)buf[0] == 'A' &&
     95  1.1  riastrad 		(unsigned char)buf[1] == 0xcc),
     96  1.1  riastrad 	    "buf=[%02x %02x]", buf[0], buf[1]);
     97  1.1  riastrad 
     98  1.1  riastrad 	/* Unicode character 'Pile of poo'. */
     99  1.1  riastrad 	memset(&s, 0, sizeof(s));
    100  1.1  riastrad 	memset(buf, 0xcc, sizeof(buf));
    101  1.1  riastrad 	ATF_CHECK_EQ_MSG((n = c16rtomb(buf, 0xd83d, &s)), 0, "n=%zu", n);
    102  1.1  riastrad 	ATF_CHECK_EQ_MSG((n = c16rtomb(buf, 0xdca9, &s)), (size_t)-1,
    103  1.1  riastrad 	    "n=%zu", n);
    104  1.1  riastrad 	ATF_CHECK_EQ_MSG(errno, EILSEQ, "errno=%d", errno);
    105  1.1  riastrad 	ATF_CHECK_EQ_MSG((unsigned char)buf[0], 0xcc, "buf=[%02x]", buf[0]);
    106  1.2  riastrad 
    107  1.2  riastrad 	/* Incomplete Unicode character 'Pile of poo', interrupted by NUL. */
    108  1.2  riastrad 	memset(&s, 0, sizeof(s));
    109  1.2  riastrad 	memset(buf, 0xcc, sizeof(buf));
    110  1.2  riastrad 	ATF_CHECK_EQ_MSG((n = c16rtomb(buf, 0xd83d, &s)), 0, "n=%zu", n);
    111  1.2  riastrad 	atf_tc_expect_fail("PR lib/58615:"
    112  1.2  riastrad 	    " incomplete c8rtomb, c16rtomb handles NUL termination wrong");
    113  1.2  riastrad 	ATF_CHECK_EQ_MSG((n = c16rtomb(buf, L'\0', &s)), 1, "n=%zu", n);
    114  1.2  riastrad 	ATF_CHECK_MSG(((unsigned char)buf[0] == '\0' &&
    115  1.2  riastrad 		(unsigned char)buf[1] == 0xcc),
    116  1.2  riastrad 	    "buf=[%02x %02x]", buf[0], buf[1]);
    117  1.1  riastrad }
    118  1.1  riastrad 
    119  1.1  riastrad ATF_TC_WITHOUT_HEAD(c16rtomb_iso_8859_1_test);
    120  1.1  riastrad ATF_TC_BODY(c16rtomb_iso_8859_1_test, tc)
    121  1.1  riastrad {
    122  1.1  riastrad 	size_t n;
    123  1.1  riastrad 
    124  1.1  riastrad 	require_lc_ctype("en_US.ISO8859-1");
    125  1.1  riastrad 
    126  1.1  riastrad 	/* Unicode character 'Euro sign'. */
    127  1.1  riastrad 	memset(&s, 0, sizeof(s));
    128  1.1  riastrad 	memset(buf, 0xcc, sizeof(buf));
    129  1.1  riastrad 	ATF_CHECK_EQ_MSG((n = c16rtomb(buf, 0x20ac, &s)), (size_t)-1,
    130  1.1  riastrad 	    "n=%zu", n);
    131  1.1  riastrad 	ATF_CHECK_EQ_MSG(errno, EILSEQ, "errno=%d", errno);
    132  1.1  riastrad 	ATF_CHECK_EQ_MSG((unsigned char)buf[0], 0xcc, "buf=[%02x]", buf[0]);
    133  1.1  riastrad }
    134  1.1  riastrad 
    135  1.1  riastrad ATF_TC_WITHOUT_HEAD(c16rtomb_iso_8859_15_test);
    136  1.1  riastrad ATF_TC_BODY(c16rtomb_iso_8859_15_test, tc)
    137  1.1  riastrad {
    138  1.1  riastrad 	size_t n;
    139  1.1  riastrad 
    140  1.1  riastrad 	require_lc_ctype("en_US.ISO8859-15");
    141  1.1  riastrad 
    142  1.1  riastrad 	/* Unicode character 'Euro sign'. */
    143  1.1  riastrad 	memset(&s, 0, sizeof(s));
    144  1.1  riastrad 	memset(buf, 0xcc, sizeof(buf));
    145  1.1  riastrad 	ATF_CHECK_EQ_MSG((n = c16rtomb(buf, 0x20ac, &s)), 1, "n=%zu", n);
    146  1.1  riastrad 	ATF_CHECK_MSG(((unsigned char)buf[0] == 0xa4 &&
    147  1.1  riastrad 		(unsigned char)buf[1] == 0xcc),
    148  1.1  riastrad 	    "buf=[%02x %02x]", buf[0], buf[1]);
    149  1.1  riastrad }
    150  1.1  riastrad 
    151  1.1  riastrad ATF_TC_WITHOUT_HEAD(c16rtomb_utf_8_test);
    152  1.1  riastrad ATF_TC_BODY(c16rtomb_utf_8_test, tc)
    153  1.1  riastrad {
    154  1.1  riastrad 	size_t n;
    155  1.1  riastrad 
    156  1.1  riastrad 	require_lc_ctype("en_US.UTF-8");
    157  1.1  riastrad 
    158  1.1  riastrad 	/* Unicode character 'Pile of poo'. */
    159  1.1  riastrad 	memset(&s, 0, sizeof(s));
    160  1.1  riastrad 	memset(buf, 0xcc, sizeof(buf));
    161  1.1  riastrad 	ATF_CHECK_EQ_MSG((n = c16rtomb(buf, 0xd83d, &s)), 0, "n=%zu", n);
    162  1.1  riastrad 	ATF_CHECK_EQ_MSG((n = c16rtomb(buf, 0xdca9, &s)), 4, "n=%zu", n);
    163  1.1  riastrad 	ATF_CHECK_MSG(((unsigned char)buf[0] == 0xf0 &&
    164  1.1  riastrad 		(unsigned char)buf[1] == 0x9f &&
    165  1.1  riastrad 		(unsigned char)buf[2] == 0x92 &&
    166  1.1  riastrad 		(unsigned char)buf[3] == 0xa9 &&
    167  1.1  riastrad 		(unsigned char)buf[4] == 0xcc),
    168  1.1  riastrad 	    "buf=[%02x %02x %02x %02x %02x]",
    169  1.1  riastrad 	    buf[0], buf[1], buf[2], buf[3], buf[4]);
    170  1.1  riastrad 
    171  1.1  riastrad 	/* Invalid code; 'Pile of poo' without the trail surrogate. */
    172  1.1  riastrad 	memset(&s, 0, sizeof(s));
    173  1.1  riastrad 	memset(buf, 0xcc, sizeof(buf));
    174  1.1  riastrad 	ATF_CHECK_EQ_MSG((n = c16rtomb(buf, 0xd83d, &s)), 0, "n=%zu", n);
    175  1.1  riastrad 	ATF_CHECK_EQ_MSG((n = c16rtomb(buf, L'A', &s)), (size_t)-1,
    176  1.1  riastrad 	    "n=%zu", n);
    177  1.1  riastrad 	ATF_CHECK_EQ_MSG(errno, EILSEQ, "errno=%d", errno);
    178  1.1  riastrad 	ATF_CHECK_EQ_MSG((unsigned char)buf[0], 0xcc, "buf=[%02x]", buf[0]);
    179  1.1  riastrad 
    180  1.1  riastrad 	/* Invalid code; 'Pile of poo' without the lead surrogate. */
    181  1.1  riastrad 	memset(&s, 0, sizeof(s));
    182  1.1  riastrad 	memset(buf, 0xcc, sizeof(buf));
    183  1.1  riastrad 	ATF_CHECK_EQ_MSG((n = c16rtomb(buf, 0xdca9, &s)), (size_t)-1,
    184  1.1  riastrad 	    "n=%zu", n);
    185  1.1  riastrad 	ATF_CHECK_EQ_MSG(errno, EILSEQ, "errno=%d", errno);
    186  1.1  riastrad 	ATF_CHECK_EQ_MSG((unsigned char)buf[0], 0xcc, "buf=[%02x]", buf[0]);
    187  1.2  riastrad 
    188  1.2  riastrad 	/* Incomplete Unicode character 'Pile of poo', interrupted by NUL. */
    189  1.2  riastrad 	memset(&s, 0, sizeof(s));
    190  1.2  riastrad 	memset(buf, 0xcc, sizeof(buf));
    191  1.2  riastrad 	ATF_CHECK_EQ_MSG((n = c16rtomb(buf, 0xd83d, &s)), 0, "n=%zu", n);
    192  1.2  riastrad 	atf_tc_expect_fail("PR lib/58615:"
    193  1.2  riastrad 	    " incomplete c8rtomb, c16rtomb handles NUL termination wrong");
    194  1.2  riastrad 	ATF_CHECK_EQ_MSG((n = c16rtomb(buf, L'\0', &s)), 1,
    195  1.2  riastrad 	    "n=%zu", n);
    196  1.2  riastrad 	ATF_CHECK_MSG(((unsigned char)buf[0] == '\0' &&
    197  1.2  riastrad 		(unsigned char)buf[1] == 0xcc),
    198  1.2  riastrad 	    "buf=[%02x %02x]", buf[0], buf[1]);
    199  1.1  riastrad }
    200  1.1  riastrad 
    201  1.1  riastrad ATF_TP_ADD_TCS(tp)
    202  1.1  riastrad {
    203  1.1  riastrad 
    204  1.1  riastrad 	ATF_TP_ADD_TC(tp, c16rtomb_c_locale_test);
    205  1.1  riastrad 	ATF_TP_ADD_TC(tp, c16rtomb_iso_8859_1_test);
    206  1.1  riastrad 	ATF_TP_ADD_TC(tp, c16rtomb_iso_8859_15_test);
    207  1.1  riastrad 	ATF_TP_ADD_TC(tp, c16rtomb_utf_8_test);
    208  1.1  riastrad 
    209  1.1  riastrad 	return (atf_no_error());
    210  1.1  riastrad }
    211