Home | History | Annotate | Line # | Download | only in locale
t_c16rtomb.c revision 1.2
      1 /*	$NetBSD: t_c16rtomb.c,v 1.2 2024/08/17 21:31:22 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2002 Tim J. Robbins
      5  * All rights reserved.
      6  *
      7  * Copyright (c) 2013 Ed Schouten <ed (at) FreeBSD.org>
      8  * All rights reserved.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 /*
     32  * Test program for c16rtomb() as specified by ISO/IEC 9899:2011.
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 __RCSID("$NetBSD: t_c16rtomb.c,v 1.2 2024/08/17 21:31:22 riastradh Exp $");
     37 
     38 #include <errno.h>
     39 #include <limits.h>
     40 #include <locale.h>
     41 #include <stdio.h>
     42 #include <string.h>
     43 #include <uchar.h>
     44 
     45 #include <atf-c.h>
     46 
     47 static void
     48 require_lc_ctype(const char *locale_name)
     49 {
     50 	char *lc_ctype_set;
     51 
     52 	lc_ctype_set = setlocale(LC_CTYPE, locale_name);
     53 	if (lc_ctype_set == NULL)
     54 		atf_tc_fail("setlocale(LC_CTYPE, \"%s\") failed; errno=%d",
     55 		    locale_name, errno);
     56 
     57 	ATF_REQUIRE_EQ_MSG(strcmp(lc_ctype_set, locale_name), 0,
     58 	    "lc_ctype_set=%s locale_name=%s", lc_ctype_set, locale_name);
     59 }
     60 
     61 static mbstate_t s;
     62 static char buf[MB_LEN_MAX + 1];
     63 
     64 ATF_TC_WITHOUT_HEAD(c16rtomb_c_locale_test);
     65 ATF_TC_BODY(c16rtomb_c_locale_test, tc)
     66 {
     67 	size_t n;
     68 
     69 	require_lc_ctype("C");
     70 
     71 	/*
     72 	 * If the buffer argument is NULL, c16 is implicitly 0,
     73 	 * c16rtomb() resets its internal state.
     74 	 */
     75 	ATF_CHECK_EQ_MSG((n = c16rtomb(NULL, L'\0', NULL)), 1, "n=%zu", n);
     76 	ATF_CHECK_EQ_MSG((n = c16rtomb(NULL, 0xdc00, NULL)), 1, "n=%zu", n);
     77 
     78 	/* Null wide character. */
     79 	memset(&s, 0, sizeof(s));
     80 	memset(buf, 0xcc, sizeof(buf));
     81 	ATF_CHECK_EQ_MSG((n = c16rtomb(buf, 0, &s)), 1, "n=%zu", n);
     82 	ATF_CHECK_MSG(((unsigned char)buf[0] == 0 &&
     83 		(unsigned char)buf[1] == 0xcc),
     84 	    "buf=[%02x %02x]", buf[0], buf[1]);
     85 
     86 	/* Latin letter A, internal state. */
     87 	ATF_CHECK_EQ_MSG((n = c16rtomb(NULL, L'\0', NULL)), 1, "n=%zu", n);
     88 	ATF_CHECK_EQ_MSG((n = c16rtomb(NULL, L'A', NULL)), 1, "n=%zu", n);
     89 
     90 	/* Latin letter A. */
     91 	memset(&s, 0, sizeof(s));
     92 	memset(buf, 0xcc, sizeof(buf));
     93 	ATF_CHECK_EQ_MSG((n = c16rtomb(buf, L'A', &s)), 1, "n=%zu", n);
     94 	ATF_CHECK_MSG(((unsigned char)buf[0] == 'A' &&
     95 		(unsigned char)buf[1] == 0xcc),
     96 	    "buf=[%02x %02x]", buf[0], buf[1]);
     97 
     98 	/* Unicode character 'Pile of poo'. */
     99 	memset(&s, 0, sizeof(s));
    100 	memset(buf, 0xcc, sizeof(buf));
    101 	ATF_CHECK_EQ_MSG((n = c16rtomb(buf, 0xd83d, &s)), 0, "n=%zu", n);
    102 	ATF_CHECK_EQ_MSG((n = c16rtomb(buf, 0xdca9, &s)), (size_t)-1,
    103 	    "n=%zu", n);
    104 	ATF_CHECK_EQ_MSG(errno, EILSEQ, "errno=%d", errno);
    105 	ATF_CHECK_EQ_MSG((unsigned char)buf[0], 0xcc, "buf=[%02x]", buf[0]);
    106 
    107 	/* Incomplete Unicode character 'Pile of poo', interrupted by NUL. */
    108 	memset(&s, 0, sizeof(s));
    109 	memset(buf, 0xcc, sizeof(buf));
    110 	ATF_CHECK_EQ_MSG((n = c16rtomb(buf, 0xd83d, &s)), 0, "n=%zu", n);
    111 	atf_tc_expect_fail("PR lib/58615:"
    112 	    " incomplete c8rtomb, c16rtomb handles NUL termination wrong");
    113 	ATF_CHECK_EQ_MSG((n = c16rtomb(buf, L'\0', &s)), 1, "n=%zu", n);
    114 	ATF_CHECK_MSG(((unsigned char)buf[0] == '\0' &&
    115 		(unsigned char)buf[1] == 0xcc),
    116 	    "buf=[%02x %02x]", buf[0], buf[1]);
    117 }
    118 
    119 ATF_TC_WITHOUT_HEAD(c16rtomb_iso_8859_1_test);
    120 ATF_TC_BODY(c16rtomb_iso_8859_1_test, tc)
    121 {
    122 	size_t n;
    123 
    124 	require_lc_ctype("en_US.ISO8859-1");
    125 
    126 	/* Unicode character 'Euro sign'. */
    127 	memset(&s, 0, sizeof(s));
    128 	memset(buf, 0xcc, sizeof(buf));
    129 	ATF_CHECK_EQ_MSG((n = c16rtomb(buf, 0x20ac, &s)), (size_t)-1,
    130 	    "n=%zu", n);
    131 	ATF_CHECK_EQ_MSG(errno, EILSEQ, "errno=%d", errno);
    132 	ATF_CHECK_EQ_MSG((unsigned char)buf[0], 0xcc, "buf=[%02x]", buf[0]);
    133 }
    134 
    135 ATF_TC_WITHOUT_HEAD(c16rtomb_iso_8859_15_test);
    136 ATF_TC_BODY(c16rtomb_iso_8859_15_test, tc)
    137 {
    138 	size_t n;
    139 
    140 	require_lc_ctype("en_US.ISO8859-15");
    141 
    142 	/* Unicode character 'Euro sign'. */
    143 	memset(&s, 0, sizeof(s));
    144 	memset(buf, 0xcc, sizeof(buf));
    145 	ATF_CHECK_EQ_MSG((n = c16rtomb(buf, 0x20ac, &s)), 1, "n=%zu", n);
    146 	ATF_CHECK_MSG(((unsigned char)buf[0] == 0xa4 &&
    147 		(unsigned char)buf[1] == 0xcc),
    148 	    "buf=[%02x %02x]", buf[0], buf[1]);
    149 }
    150 
    151 ATF_TC_WITHOUT_HEAD(c16rtomb_utf_8_test);
    152 ATF_TC_BODY(c16rtomb_utf_8_test, tc)
    153 {
    154 	size_t n;
    155 
    156 	require_lc_ctype("en_US.UTF-8");
    157 
    158 	/* Unicode character 'Pile of poo'. */
    159 	memset(&s, 0, sizeof(s));
    160 	memset(buf, 0xcc, sizeof(buf));
    161 	ATF_CHECK_EQ_MSG((n = c16rtomb(buf, 0xd83d, &s)), 0, "n=%zu", n);
    162 	ATF_CHECK_EQ_MSG((n = c16rtomb(buf, 0xdca9, &s)), 4, "n=%zu", n);
    163 	ATF_CHECK_MSG(((unsigned char)buf[0] == 0xf0 &&
    164 		(unsigned char)buf[1] == 0x9f &&
    165 		(unsigned char)buf[2] == 0x92 &&
    166 		(unsigned char)buf[3] == 0xa9 &&
    167 		(unsigned char)buf[4] == 0xcc),
    168 	    "buf=[%02x %02x %02x %02x %02x]",
    169 	    buf[0], buf[1], buf[2], buf[3], buf[4]);
    170 
    171 	/* Invalid code; 'Pile of poo' without the trail surrogate. */
    172 	memset(&s, 0, sizeof(s));
    173 	memset(buf, 0xcc, sizeof(buf));
    174 	ATF_CHECK_EQ_MSG((n = c16rtomb(buf, 0xd83d, &s)), 0, "n=%zu", n);
    175 	ATF_CHECK_EQ_MSG((n = c16rtomb(buf, L'A', &s)), (size_t)-1,
    176 	    "n=%zu", n);
    177 	ATF_CHECK_EQ_MSG(errno, EILSEQ, "errno=%d", errno);
    178 	ATF_CHECK_EQ_MSG((unsigned char)buf[0], 0xcc, "buf=[%02x]", buf[0]);
    179 
    180 	/* Invalid code; 'Pile of poo' without the lead surrogate. */
    181 	memset(&s, 0, sizeof(s));
    182 	memset(buf, 0xcc, sizeof(buf));
    183 	ATF_CHECK_EQ_MSG((n = c16rtomb(buf, 0xdca9, &s)), (size_t)-1,
    184 	    "n=%zu", n);
    185 	ATF_CHECK_EQ_MSG(errno, EILSEQ, "errno=%d", errno);
    186 	ATF_CHECK_EQ_MSG((unsigned char)buf[0], 0xcc, "buf=[%02x]", buf[0]);
    187 
    188 	/* Incomplete Unicode character 'Pile of poo', interrupted by NUL. */
    189 	memset(&s, 0, sizeof(s));
    190 	memset(buf, 0xcc, sizeof(buf));
    191 	ATF_CHECK_EQ_MSG((n = c16rtomb(buf, 0xd83d, &s)), 0, "n=%zu", n);
    192 	atf_tc_expect_fail("PR lib/58615:"
    193 	    " incomplete c8rtomb, c16rtomb handles NUL termination wrong");
    194 	ATF_CHECK_EQ_MSG((n = c16rtomb(buf, L'\0', &s)), 1,
    195 	    "n=%zu", n);
    196 	ATF_CHECK_MSG(((unsigned char)buf[0] == '\0' &&
    197 		(unsigned char)buf[1] == 0xcc),
    198 	    "buf=[%02x %02x]", buf[0], buf[1]);
    199 }
    200 
    201 ATF_TP_ADD_TCS(tp)
    202 {
    203 
    204 	ATF_TP_ADD_TC(tp, c16rtomb_c_locale_test);
    205 	ATF_TP_ADD_TC(tp, c16rtomb_iso_8859_1_test);
    206 	ATF_TP_ADD_TC(tp, c16rtomb_iso_8859_15_test);
    207 	ATF_TP_ADD_TC(tp, c16rtomb_utf_8_test);
    208 
    209 	return (atf_no_error());
    210 }
    211