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