Home | History | Annotate | Line # | Download | only in locale
t_io.c revision 1.4.20.1
      1  1.4.20.1    martin /* $NetBSD: t_io.c,v 1.4.20.1 2018/03/15 09:55:23 martin Exp $ */
      2       1.1  christos 
      3       1.1  christos /*-
      4       1.1  christos  * Copyright (c) 2013 The NetBSD Foundation, Inc.
      5       1.1  christos  * All rights reserved.
      6       1.1  christos  *
      7       1.1  christos  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1  christos  * by Christos Zoulas.
      9       1.1  christos  *
     10       1.1  christos  * Redistribution and use in source and binary forms, with or without
     11       1.1  christos  * modification, are permitted provided that the following conditions
     12       1.1  christos  * are met:
     13       1.1  christos  * 1. Redistributions of source code must retain the above copyright
     14       1.1  christos  *    notice, this list of conditions and the following disclaimer.
     15       1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     17       1.1  christos  *    documentation and/or other materials provided with the distribution.
     18       1.1  christos  *
     19       1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.1  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.1  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1  christos  */
     31       1.1  christos 
     32       1.1  christos #include <sys/cdefs.h>
     33       1.1  christos __COPYRIGHT("@(#) Copyright (c) 2011\
     34       1.1  christos  The NetBSD Foundation, inc. All rights reserved.");
     35  1.4.20.1    martin __RCSID("$NetBSD: t_io.c,v 1.4.20.1 2018/03/15 09:55:23 martin Exp $");
     36       1.1  christos 
     37       1.1  christos #include <sys/param.h>
     38       1.1  christos #include <errno.h>
     39       1.1  christos #include <locale.h>
     40       1.1  christos #include <stdio.h>
     41       1.1  christos #include <stdlib.h>
     42       1.1  christos #include <string.h>
     43       1.1  christos #include <wchar.h>
     44       1.1  christos 
     45       1.1  christos #include <atf-c.h>
     46       1.1  christos 
     47       1.1  christos 
     48       1.1  christos ATF_TC(bad_big5_wprintf);
     49       1.1  christos ATF_TC_HEAD(bad_big5_wprintf, tc)
     50       1.1  christos {
     51       1.1  christos 	atf_tc_set_md_var(tc, "descr", "Test bad big5 wchar wprintf");
     52       1.1  christos }
     53       1.1  christos 
     54       1.1  christos ATF_TC_BODY(bad_big5_wprintf, tc)
     55       1.1  christos {
     56  1.4.20.1    martin 	wchar_t ibuf[] = {
     57  1.4.20.1    martin #ifdef __STDC_ISO_10646__
     58  1.4.20.1    martin 		0x0978, 0  /* An arbitrarily chosen Devangari symbol */
     59  1.4.20.1    martin #else
     60  1.4.20.1    martin 		/* XXX implementation detail knowledge (wchar_t encoding) */
     61  1.4.20.1    martin 		0xcf10, 0
     62  1.4.20.1    martin #endif
     63  1.4.20.1    martin 	};
     64       1.1  christos 	setlocale(LC_CTYPE, "zh_TW.Big5");
     65       1.3      yamt 	ATF_REQUIRE_ERRNO(EILSEQ, wprintf(L"%ls\n", ibuf) < 0);
     66       1.3      yamt 	ATF_REQUIRE(ferror(stdout));
     67       1.1  christos }
     68       1.1  christos 
     69       1.1  christos ATF_TC(bad_big5_swprintf);
     70       1.1  christos ATF_TC_HEAD(bad_big5_swprintf, tc)
     71       1.1  christos {
     72       1.1  christos 	atf_tc_set_md_var(tc, "descr", "Test bad big5 wchar swprintf");
     73       1.1  christos }
     74       1.1  christos 
     75       1.1  christos ATF_TC_BODY(bad_big5_swprintf, tc)
     76       1.1  christos {
     77  1.4.20.1    martin 	wchar_t ibuf[] = {
     78  1.4.20.1    martin #ifdef __STDC_ISO_10646__
     79  1.4.20.1    martin 		0x0978, 0  /* An arbitrarily chosen Devangari symbol */
     80  1.4.20.1    martin #else
     81  1.4.20.1    martin 		/* XXX implementation detail knowledge (wchar_t encoding) */
     82  1.4.20.1    martin 		0xcf10, 0
     83  1.4.20.1    martin #endif
     84  1.4.20.1    martin 	};
     85       1.1  christos 	wchar_t obuf[20];
     86       1.1  christos 	setlocale(LC_CTYPE, "zh_TW.Big5");
     87       1.3      yamt 	ATF_REQUIRE_ERRNO(EILSEQ,
     88       1.3      yamt 			  swprintf(obuf, sizeof(obuf), L"%ls\n", ibuf) < 0);
     89       1.1  christos }
     90       1.1  christos 
     91       1.1  christos ATF_TC(good_big5_wprintf);
     92       1.1  christos ATF_TC_HEAD(good_big5_wprintf, tc)
     93       1.1  christos {
     94       1.1  christos 	atf_tc_set_md_var(tc, "descr", "Test good big5 wchar wprintf");
     95       1.1  christos }
     96       1.1  christos 
     97       1.1  christos ATF_TC_BODY(good_big5_wprintf, tc)
     98       1.1  christos {
     99  1.4.20.1    martin 	wchar_t ibuf[] = {
    100  1.4.20.1    martin #ifdef __STDC_ISO_10646__
    101  1.4.20.1    martin 		0x67DC, 0
    102  1.4.20.1    martin #else
    103  1.4.20.1    martin 		/* XXX implementation detail knowledge (wchar_t encoding) */
    104  1.4.20.1    martin 		0xcf40, 0
    105  1.4.20.1    martin #endif
    106  1.4.20.1    martin 	};
    107       1.1  christos 	setlocale(LC_CTYPE, "zh_TW.Big5");
    108       1.1  christos 	ATF_REQUIRE_EQ(wprintf(L"%ls\n", ibuf), 2);
    109       1.1  christos }
    110       1.1  christos 
    111       1.1  christos ATF_TC(good_big5_swprintf);
    112       1.1  christos ATF_TC_HEAD(good_big5_swprintf, tc)
    113       1.1  christos {
    114       1.1  christos 	atf_tc_set_md_var(tc, "descr", "Test good big5 wchar swprintf");
    115       1.1  christos }
    116       1.1  christos 
    117       1.1  christos ATF_TC_BODY(good_big5_swprintf, tc)
    118       1.1  christos {
    119  1.4.20.1    martin 	wchar_t ibuf[] = {
    120  1.4.20.1    martin #ifdef __STDC_ISO_10646__
    121  1.4.20.1    martin 		0x67DC, 0
    122  1.4.20.1    martin #else
    123  1.4.20.1    martin 		/* XXX implementation detail knowledge (wchar_t encoding) */
    124  1.4.20.1    martin 		0xcf40, 0
    125  1.4.20.1    martin #endif
    126  1.4.20.1    martin 	};
    127       1.1  christos 	wchar_t obuf[20];
    128       1.1  christos 	setlocale(LC_CTYPE, "zh_TW.Big5");
    129       1.1  christos 	ATF_REQUIRE_EQ(swprintf(obuf, sizeof(obuf), L"%ls\n", ibuf), 2);
    130       1.1  christos }
    131       1.1  christos 
    132       1.3      yamt struct ibuf {
    133       1.3      yamt 	off_t off;
    134       1.3      yamt 	size_t buflen;
    135       1.3      yamt 	const char *buf;
    136       1.3      yamt };
    137       1.3      yamt 
    138       1.3      yamt static int
    139       1.3      yamt readfn(void *vp, char *buf, int len)
    140       1.3      yamt {
    141       1.3      yamt 	struct ibuf *ib = vp;
    142       1.3      yamt 	size_t todo = MIN((size_t)len, ib->buflen - ib->off);
    143       1.3      yamt 
    144       1.3      yamt 	memcpy(buf, ib->buf + ib->off, todo);
    145       1.3      yamt 	ib->off += todo;
    146       1.3      yamt 	return todo;
    147       1.1  christos }
    148       1.1  christos 
    149       1.1  christos ATF_TC(good_big5_getwc);
    150       1.1  christos ATF_TC_HEAD(good_big5_getwc, tc)
    151       1.1  christos {
    152       1.1  christos 	atf_tc_set_md_var(tc, "descr", "Test good big5 wchar getwc");
    153       1.1  christos }
    154       1.1  christos 
    155       1.1  christos ATF_TC_BODY(good_big5_getwc, tc)
    156       1.1  christos {
    157       1.3      yamt 	const char buf[] = { 0xcf, 0x40 };
    158       1.3      yamt 	struct ibuf ib = {
    159       1.3      yamt 		.buf = buf,
    160       1.3      yamt 		.buflen = sizeof(buf),
    161       1.3      yamt 	};
    162       1.3      yamt 	FILE *fp = funopen(&ib, readfn, NULL, NULL, NULL);
    163       1.1  christos 
    164       1.1  christos 	ATF_REQUIRE(fp != NULL);
    165       1.1  christos 	setlocale(LC_CTYPE, "zh_TW.Big5");
    166  1.4.20.1    martin 	ATF_REQUIRE_EQ(getwc(fp),
    167  1.4.20.1    martin #ifdef __STDC_ISO_10646__
    168  1.4.20.1    martin 		       0x67DC
    169  1.4.20.1    martin #else
    170  1.4.20.1    martin 		       /* XXX implementation detail knowledge (wchar_t encoding) */
    171  1.4.20.1    martin 		       0xcf40
    172  1.4.20.1    martin #endif
    173  1.4.20.1    martin 		);
    174       1.1  christos 	fclose(fp);
    175       1.1  christos }
    176       1.1  christos 
    177       1.1  christos ATF_TC(bad_big5_getwc);
    178       1.1  christos ATF_TC_HEAD(bad_big5_getwc, tc)
    179       1.1  christos {
    180       1.1  christos 	atf_tc_set_md_var(tc, "descr", "Test bad big5 wchar getwc");
    181       1.1  christos }
    182       1.1  christos 
    183       1.1  christos ATF_TC_BODY(bad_big5_getwc, tc)
    184       1.1  christos {
    185       1.3      yamt 	const char buf[] = { 0xcf, 0x20 };
    186       1.3      yamt 	struct ibuf ib = {
    187       1.3      yamt 		.buf = buf,
    188       1.3      yamt 		.buflen = sizeof(buf),
    189       1.3      yamt 	};
    190       1.3      yamt 	FILE *fp = funopen(&ib, readfn, NULL, NULL, NULL);
    191       1.1  christos 
    192       1.1  christos 	ATF_REQUIRE(fp != NULL);
    193       1.1  christos 	setlocale(LC_CTYPE, "zh_TW.Big5");
    194       1.1  christos 	ATF_REQUIRE_EQ(getwc(fp), WEOF);
    195       1.1  christos 	fclose(fp);
    196       1.1  christos }
    197       1.1  christos 
    198       1.1  christos ATF_TP_ADD_TCS(tp)
    199       1.1  christos {
    200       1.1  christos 	ATF_TP_ADD_TC(tp, bad_big5_wprintf);
    201       1.1  christos 	ATF_TP_ADD_TC(tp, bad_big5_swprintf);
    202       1.1  christos 	ATF_TP_ADD_TC(tp, good_big5_wprintf);
    203       1.1  christos 	ATF_TP_ADD_TC(tp, good_big5_swprintf);
    204       1.1  christos 	ATF_TP_ADD_TC(tp, good_big5_getwc);
    205       1.1  christos 	ATF_TP_ADD_TC(tp, bad_big5_getwc);
    206       1.1  christos 
    207       1.1  christos 	return atf_no_error();
    208       1.1  christos }
    209