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