Home | History | Annotate | Line # | Download | only in locale
t_btowc.c revision 1.1
      1  1.1  perseant /* $NetBSD: t_btowc.c,v 1.1 2017/06/01 15:45:02 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.1  perseant __RCSID("$NetBSD: t_btowc.c,v 1.1 2017/06/01 15:45:02 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.1  perseant #include <string.h>
     41  1.1  perseant #include <wchar.h>
     42  1.1  perseant 
     43  1.1  perseant #include <atf-c.h>
     44  1.1  perseant 
     45  1.1  perseant struct test {
     46  1.1  perseant 	const char *locale;
     47  1.1  perseant 	const char *illegal; /* Illegal single-byte characters, if any */
     48  1.1  perseant 	const char *legal;   /* Legal single-byte characters */
     49  1.1  perseant 	/* The next two are only used if __STDC_ISO_10646__ is defined */
     50  1.1  perseant 	const wchar_t wlegal[8]; /* The same characters, but in ISO-10646 */
     51  1.1  perseant 	const wchar_t willegal[8]; /* ISO-10646 that do not map into charset */
     52  1.1  perseant } tests[] = {
     53  1.1  perseant 	{
     54  1.1  perseant 		"C",
     55  1.1  perseant 		"\377",
     56  1.1  perseant 		"ABC123@\t",
     57  1.1  perseant 		{ 'A', 'B', 'C', '1', '2', '3', '@', '\t' },
     58  1.1  perseant 		{ 0x0430, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}
     59  1.1  perseant 	},
     60  1.1  perseant 	{
     61  1.1  perseant 		"en_US.UTF-8",
     62  1.1  perseant 		"\200",
     63  1.1  perseant 		"ABC123@\t",
     64  1.1  perseant 		{ 'A', 'B', 'C', '1', '2', '3', '@', '\t' },
     65  1.1  perseant 		{ 0xfdd0, 0x10fffe, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}
     66  1.1  perseant 	},
     67  1.1  perseant 	{
     68  1.1  perseant                 "ru_RU.KOI8-R",
     69  1.1  perseant 		"", /* No illegal characters in KOI8-R */
     70  1.1  perseant                 "A\xc2\xd7\xc7\xc4\xc5\xa3",
     71  1.1  perseant 		{ 'A', 0x0431, 0x432, 0x0433, 0x0434, 0x0435, 0x0451, 0x0 },
     72  1.1  perseant 		{ 0x00c5, 0x00e6, 0x00fe, 0x0630, 0x06fc, 0x56cd, 0x0, 0x0 }
     73  1.1  perseant 	},
     74  1.1  perseant 	{
     75  1.1  perseant 		NULL,
     76  1.1  perseant                 NULL,
     77  1.1  perseant                 NULL,
     78  1.1  perseant 		{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 },
     79  1.1  perseant 		{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }
     80  1.1  perseant 	},
     81  1.1  perseant };
     82  1.1  perseant 
     83  1.1  perseant #ifdef __STDC_ISO_10646__
     84  1.1  perseant static void
     85  1.1  perseant h_iso10646(struct test *t)
     86  1.1  perseant {
     87  1.1  perseant 	const char *cp;
     88  1.1  perseant 	unsigned char c;
     89  1.1  perseant 	char *str;
     90  1.1  perseant 	const wchar_t *wcp;
     91  1.1  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.1  perseant 		c = (unsigned char)*cp;
     95  1.1  perseant 		printf("Checking legal character 0x%x\n", c);
     96  1.1  perseant 
     97  1.1  perseant 		/* It should map to the known Unicode equivalent */
     98  1.1  perseant 		printf("btowc(0x%2.2x) = 0x%x, expecting 0x%x\n",
     99  1.1  perseant 			c, btowc(c), *wcp);
    100  1.1  perseant 		ATF_REQUIRE(btowc(c) == *wcp);
    101  1.1  perseant 	}
    102  1.1  perseant 
    103  1.1  perseant 	/* These are invalid characters in the target set */
    104  1.1  perseant 	for (wcp = t->willegal; *wcp != '\0'; ++wcp) {
    105  1.1  perseant 		printf("Checking illegal wide character 0x%lx\n",
    106  1.1  perseant 			(unsigned long)*wcp);
    107  1.1  perseant 		ATF_REQUIRE_EQ(wctob(*wcp), EOF);
    108  1.1  perseant 	}
    109  1.1  perseant }
    110  1.1  perseant #endif
    111  1.1  perseant 
    112  1.1  perseant static void
    113  1.1  perseant h_btowc(struct test *t)
    114  1.1  perseant {
    115  1.1  perseant 	const char *cp;
    116  1.1  perseant 	unsigned char c;
    117  1.1  perseant 	char *str;
    118  1.1  perseant 	const wchar_t *wcp;
    119  1.1  perseant 
    120  1.1  perseant 	ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
    121  1.1  perseant 	printf("Trying locale: %s\n", t->locale);
    122  1.1  perseant 	ATF_REQUIRE(setlocale(LC_CTYPE, t->locale) != NULL);
    123  1.1  perseant 
    124  1.1  perseant 	/* btowc(EOF) -> WEOF */
    125  1.1  perseant 	ATF_REQUIRE_EQ(btowc(EOF), WEOF);
    126  1.1  perseant 
    127  1.1  perseant 	/* wctob(WEOF) -> EOF */
    128  1.1  perseant 	ATF_REQUIRE_EQ(wctob(WEOF), EOF);
    129  1.1  perseant 
    130  1.1  perseant 	/* Invalid in initial shift state -> WEOF */
    131  1.1  perseant 	for (cp = t->illegal; *cp != '\0'; ++cp) {
    132  1.1  perseant 		printf("Checking illegal character 0x%x\n",
    133  1.1  perseant 			(unsigned char)*cp);
    134  1.1  perseant 		ATF_REQUIRE_EQ(btowc(*cp), WEOF);
    135  1.1  perseant 	}
    136  1.1  perseant 
    137  1.1  perseant 	/* These should have valid wchar representations */
    138  1.1  perseant 	for (cp = t->legal; *cp != '\0'; ++cp) {
    139  1.1  perseant 		c = (unsigned char)*cp;
    140  1.1  perseant 		printf("Checking legal character 0x%x\n", c);
    141  1.1  perseant 
    142  1.1  perseant 		/* A legal character never maps to EOF */
    143  1.1  perseant 		ATF_REQUIRE(btowc(c) != WEOF);
    144  1.1  perseant 
    145  1.1  perseant 		/* And the mapping should be reversible */
    146  1.1  perseant 		printf("0x%x -> wide 0x%x -> 0x%x\n",
    147  1.1  perseant 			c, btowc(c), (unsigned char)wctob(btowc(c)));
    148  1.1  perseant 		ATF_REQUIRE_EQ(wctob(btowc(c)), c);
    149  1.1  perseant 	}
    150  1.1  perseant }
    151  1.1  perseant 
    152  1.1  perseant ATF_TC(btowc);
    153  1.1  perseant ATF_TC_HEAD(btowc, tc)
    154  1.1  perseant {
    155  1.1  perseant 	atf_tc_set_md_var(tc, "descr", "Checks btowc(3) and wctob(3)");
    156  1.1  perseant }
    157  1.1  perseant ATF_TC_BODY(btowc, tc)
    158  1.1  perseant {
    159  1.1  perseant 	struct test *t;
    160  1.1  perseant 
    161  1.1  perseant 	for (t = tests; t->locale != NULL; ++t)
    162  1.1  perseant 		h_btowc(t);
    163  1.1  perseant }
    164  1.1  perseant 
    165  1.1  perseant ATF_TC(stdc_iso_10646);
    166  1.1  perseant ATF_TC_HEAD(stdc_iso_10646, tc)
    167  1.1  perseant {
    168  1.1  perseant 	atf_tc_set_md_var(tc, "descr",
    169  1.1  perseant 		"Checks btowc(3) conversion to ISO10646");
    170  1.1  perseant }
    171  1.1  perseant ATF_TC_BODY(stdc_iso_10646, tc)
    172  1.1  perseant {
    173  1.1  perseant 	struct test *t;
    174  1.1  perseant 
    175  1.1  perseant #ifdef __STDC_ISO_10646__
    176  1.1  perseant 	for (t = tests; t->locale != NULL; ++t)
    177  1.1  perseant 		h_iso10646(t);
    178  1.1  perseant #else /* ! __STDC_ISO_10646__ */
    179  1.1  perseant 	atf_tc_skip("__STDC_ISO_10646__ not defined");
    180  1.1  perseant #endif /* ! __STDC_ISO_10646__ */
    181  1.1  perseant }
    182  1.1  perseant 
    183  1.1  perseant ATF_TP_ADD_TCS(tp)
    184  1.1  perseant {
    185  1.1  perseant 	ATF_TP_ADD_TC(tp, btowc);
    186  1.1  perseant 	ATF_TP_ADD_TC(tp, stdc_iso_10646);
    187  1.1  perseant 
    188  1.1  perseant 	return atf_no_error();
    189  1.1  perseant }
    190