Home | History | Annotate | Line # | Download | only in locale
      1  1.3  perseant /* $NetBSD: t_btowc.c,v 1.3 2017/08/10 19:08:43 perseant Exp $ */
      2  1.1  perseant 
      3  1.1  perseant /*-
      4  1.1  perseant  * Copyright (c) 2017 The NetBSD Foundation, Inc.
      5  1.1  perseant  * All rights reserved.
      6  1.1  perseant  *
      7  1.1  perseant  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  perseant  * by Konrad Schroder.
      9  1.1  perseant  *
     10  1.1  perseant  * Redistribution and use in source and binary forms, with or without
     11  1.1  perseant  * modification, are permitted provided that the following conditions
     12  1.1  perseant  * are met:
     13  1.1  perseant  * 1. Redistributions of source code must retain the above copyright
     14  1.1  perseant  *    notice, this list of conditions and the following disclaimer.
     15  1.1  perseant  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  perseant  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  perseant  *    documentation and/or other materials provided with the distribution.
     18  1.1  perseant  *
     19  1.1  perseant  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  perseant  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  perseant  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  perseant  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  perseant  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  perseant  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  perseant  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  perseant  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  perseant  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  perseant  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  perseant  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  perseant  */
     31  1.1  perseant 
     32  1.1  perseant #include <sys/cdefs.h>
     33  1.1  perseant __COPYRIGHT("@(#) Copyright (c) 2017\
     34  1.1  perseant  The NetBSD Foundation, inc. All rights reserved.");
     35  1.3  perseant __RCSID("$NetBSD: t_btowc.c,v 1.3 2017/08/10 19:08:43 perseant Exp $");
     36  1.1  perseant 
     37  1.1  perseant #include <locale.h>
     38  1.1  perseant #include <stdio.h>
     39  1.1  perseant #include <stdlib.h>
     40  1.2  perseant #include <errno.h>
     41  1.1  perseant #include <string.h>
     42  1.1  perseant #include <wchar.h>
     43  1.1  perseant 
     44  1.1  perseant #include <atf-c.h>
     45  1.1  perseant 
     46  1.1  perseant struct test {
     47  1.1  perseant 	const char *locale;
     48  1.1  perseant 	const char *illegal; /* Illegal single-byte characters, if any */
     49  1.1  perseant 	const char *legal;   /* Legal single-byte characters */
     50  1.1  perseant 	/* The next two are only used if __STDC_ISO_10646__ is defined */
     51  1.1  perseant 	const wchar_t wlegal[8]; /* The same characters, but in ISO-10646 */
     52  1.1  perseant 	const wchar_t willegal[8]; /* ISO-10646 that do not map into charset */
     53  1.1  perseant } tests[] = {
     54  1.1  perseant 	{
     55  1.1  perseant 		"en_US.UTF-8",
     56  1.1  perseant 		"\200",
     57  1.1  perseant 		"ABC123@\t",
     58  1.1  perseant 		{ 'A', 'B', 'C', '1', '2', '3', '@', '\t' },
     59  1.1  perseant 		{ 0xfdd0, 0x10fffe, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}
     60  1.1  perseant 	},
     61  1.1  perseant 	{
     62  1.1  perseant                 "ru_RU.KOI8-R",
     63  1.1  perseant 		"", /* No illegal characters in KOI8-R */
     64  1.1  perseant                 "A\xc2\xd7\xc7\xc4\xc5\xa3",
     65  1.1  perseant 		{ 'A', 0x0431, 0x432, 0x0433, 0x0434, 0x0435, 0x0451, 0x0 },
     66  1.1  perseant 		{ 0x00c5, 0x00e6, 0x00fe, 0x0630, 0x06fc, 0x56cd, 0x0, 0x0 }
     67  1.1  perseant 	},
     68  1.1  perseant 	{
     69  1.1  perseant 		NULL,
     70  1.1  perseant                 NULL,
     71  1.1  perseant                 NULL,
     72  1.1  perseant 		{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 },
     73  1.1  perseant 		{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }
     74  1.1  perseant 	},
     75  1.1  perseant };
     76  1.1  perseant 
     77  1.1  perseant #ifdef __STDC_ISO_10646__
     78  1.1  perseant static void
     79  1.1  perseant h_iso10646(struct test *t)
     80  1.1  perseant {
     81  1.1  perseant 	const char *cp;
     82  1.2  perseant 	int c, wc;
     83  1.1  perseant 	char *str;
     84  1.1  perseant 	const wchar_t *wcp;
     85  1.1  perseant 
     86  1.2  perseant 	ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
     87  1.2  perseant 	printf("Trying locale: %s\n", t->locale);
     88  1.2  perseant 	ATF_REQUIRE(setlocale(LC_CTYPE, t->locale) != NULL);
     89  1.2  perseant 	ATF_REQUIRE((str = setlocale(LC_ALL, NULL)) != NULL);
     90  1.2  perseant 	(void)printf("Using locale: %s\n", str);
     91  1.2  perseant 
     92  1.1  perseant 	/* These should have valid wchar representations */
     93  1.1  perseant 	for (cp = t->legal, wcp = t->wlegal; *cp != '\0'; ++cp, ++wcp) {
     94  1.2  perseant 		c = (int)(unsigned char)*cp;
     95  1.1  perseant 		printf("Checking legal character 0x%x\n", c);
     96  1.2  perseant 		wc = btowc(c);
     97  1.2  perseant 
     98  1.2  perseant 		if (errno != 0)
     99  1.2  perseant 			printf(" btowc() failed with errno=%d\n", errno);
    100  1.1  perseant 
    101  1.1  perseant 		/* It should map to the known Unicode equivalent */
    102  1.1  perseant 		printf("btowc(0x%2.2x) = 0x%x, expecting 0x%x\n",
    103  1.2  perseant 		       c, wc, *wcp);
    104  1.1  perseant 		ATF_REQUIRE(btowc(c) == *wcp);
    105  1.1  perseant 	}
    106  1.1  perseant 
    107  1.1  perseant 	/* These are invalid characters in the target set */
    108  1.1  perseant 	for (wcp = t->willegal; *wcp != '\0'; ++wcp) {
    109  1.1  perseant 		printf("Checking illegal wide character 0x%lx\n",
    110  1.1  perseant 			(unsigned long)*wcp);
    111  1.1  perseant 		ATF_REQUIRE_EQ(wctob(*wcp), EOF);
    112  1.1  perseant 	}
    113  1.1  perseant }
    114  1.1  perseant #endif
    115  1.1  perseant 
    116  1.1  perseant static void
    117  1.1  perseant h_btowc(struct test *t)
    118  1.1  perseant {
    119  1.1  perseant 	const char *cp;
    120  1.1  perseant 	unsigned char c;
    121  1.1  perseant 	char *str;
    122  1.1  perseant 	const wchar_t *wcp;
    123  1.1  perseant 
    124  1.1  perseant 	ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
    125  1.1  perseant 	printf("Trying locale: %s\n", t->locale);
    126  1.1  perseant 	ATF_REQUIRE(setlocale(LC_CTYPE, t->locale) != NULL);
    127  1.2  perseant 	ATF_REQUIRE((str = setlocale(LC_ALL, NULL)) != NULL);
    128  1.2  perseant 	(void)printf("Using locale: %s\n", str);
    129  1.1  perseant 
    130  1.1  perseant 	/* btowc(EOF) -> WEOF */
    131  1.1  perseant 	ATF_REQUIRE_EQ(btowc(EOF), WEOF);
    132  1.1  perseant 
    133  1.1  perseant 	/* wctob(WEOF) -> EOF */
    134  1.1  perseant 	ATF_REQUIRE_EQ(wctob(WEOF), EOF);
    135  1.1  perseant 
    136  1.1  perseant 	/* Invalid in initial shift state -> WEOF */
    137  1.1  perseant 	for (cp = t->illegal; *cp != '\0'; ++cp) {
    138  1.1  perseant 		printf("Checking illegal character 0x%x\n",
    139  1.1  perseant 			(unsigned char)*cp);
    140  1.1  perseant 		ATF_REQUIRE_EQ(btowc(*cp), WEOF);
    141  1.1  perseant 	}
    142  1.1  perseant 
    143  1.1  perseant 	/* These should have valid wchar representations */
    144  1.1  perseant 	for (cp = t->legal; *cp != '\0'; ++cp) {
    145  1.1  perseant 		c = (unsigned char)*cp;
    146  1.1  perseant 		printf("Checking legal character 0x%x\n", c);
    147  1.1  perseant 
    148  1.1  perseant 		/* A legal character never maps to EOF */
    149  1.1  perseant 		ATF_REQUIRE(btowc(c) != WEOF);
    150  1.1  perseant 
    151  1.1  perseant 		/* And the mapping should be reversible */
    152  1.1  perseant 		printf("0x%x -> wide 0x%x -> 0x%x\n",
    153  1.1  perseant 			c, btowc(c), (unsigned char)wctob(btowc(c)));
    154  1.1  perseant 		ATF_REQUIRE_EQ(wctob(btowc(c)), c);
    155  1.1  perseant 	}
    156  1.1  perseant }
    157  1.1  perseant 
    158  1.1  perseant ATF_TC(btowc);
    159  1.1  perseant ATF_TC_HEAD(btowc, tc)
    160  1.1  perseant {
    161  1.1  perseant 	atf_tc_set_md_var(tc, "descr", "Checks btowc(3) and wctob(3)");
    162  1.1  perseant }
    163  1.1  perseant ATF_TC_BODY(btowc, tc)
    164  1.1  perseant {
    165  1.1  perseant 	struct test *t;
    166  1.1  perseant 
    167  1.1  perseant 	for (t = tests; t->locale != NULL; ++t)
    168  1.1  perseant 		h_btowc(t);
    169  1.1  perseant }
    170  1.1  perseant 
    171  1.1  perseant ATF_TC(stdc_iso_10646);
    172  1.1  perseant ATF_TC_HEAD(stdc_iso_10646, tc)
    173  1.1  perseant {
    174  1.1  perseant 	atf_tc_set_md_var(tc, "descr",
    175  1.1  perseant 		"Checks btowc(3) conversion to ISO10646");
    176  1.1  perseant }
    177  1.1  perseant ATF_TC_BODY(stdc_iso_10646, tc)
    178  1.1  perseant {
    179  1.1  perseant 	struct test *t;
    180  1.1  perseant 
    181  1.1  perseant #ifdef __STDC_ISO_10646__
    182  1.1  perseant 	for (t = tests; t->locale != NULL; ++t)
    183  1.1  perseant 		h_iso10646(t);
    184  1.1  perseant #else /* ! __STDC_ISO_10646__ */
    185  1.1  perseant 	atf_tc_skip("__STDC_ISO_10646__ not defined");
    186  1.1  perseant #endif /* ! __STDC_ISO_10646__ */
    187  1.1  perseant }
    188  1.1  perseant 
    189  1.3  perseant ATF_TC(btowc_posix);
    190  1.3  perseant ATF_TC_HEAD(btowc_posix, tc)
    191  1.3  perseant {
    192  1.3  perseant 	atf_tc_set_md_var(tc, "descr", "Checks btowc(3) and wctob(3) for POSIX locale");
    193  1.3  perseant }
    194  1.3  perseant ATF_TC_BODY(btowc_posix, tc)
    195  1.3  perseant {
    196  1.3  perseant 	const char *cp;
    197  1.3  perseant 	unsigned char c;
    198  1.3  perseant 	char *str;
    199  1.3  perseant 	const wchar_t *wcp;
    200  1.3  perseant 	int i;
    201  1.3  perseant 
    202  1.3  perseant 	ATF_REQUIRE_STREQ(setlocale(LC_ALL, "POSIX"), "POSIX");
    203  1.3  perseant 
    204  1.3  perseant 	/* btowc(EOF) -> WEOF */
    205  1.3  perseant 	ATF_REQUIRE_EQ(btowc(EOF), WEOF);
    206  1.3  perseant 
    207  1.3  perseant 	/* wctob(WEOF) -> EOF */
    208  1.3  perseant 	ATF_REQUIRE_EQ(wctob(WEOF), EOF);
    209  1.3  perseant 
    210  1.3  perseant 	/* All characters from 0 to 255, inclusive, map
    211  1.3  perseant 	   onto their unsigned char equivalent */
    212  1.3  perseant 	for (i = 0; i <= 255; i++) {
    213  1.3  perseant 		ATF_REQUIRE_EQ(btowc(i), (wchar_t)(unsigned char)(i));
    214  1.3  perseant 		ATF_REQUIRE_EQ((unsigned char)wctob(i), (wchar_t)i);
    215  1.3  perseant 	}
    216  1.3  perseant }
    217  1.3  perseant 
    218  1.1  perseant ATF_TP_ADD_TCS(tp)
    219  1.1  perseant {
    220  1.1  perseant 	ATF_TP_ADD_TC(tp, btowc);
    221  1.3  perseant 	ATF_TP_ADD_TC(tp, btowc_posix);
    222  1.1  perseant 	ATF_TP_ADD_TC(tp, stdc_iso_10646);
    223  1.1  perseant 
    224  1.1  perseant 	return atf_no_error();
    225  1.1  perseant }
    226