Home | History | Annotate | Line # | Download | only in locale
t_io.c revision 1.1
      1  1.1  christos /* $NetBSD: t_io.c,v 1.1 2013/02/28 21:52:02 christos 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.1  christos __RCSID("$NetBSD: t_io.c,v 1.1 2013/02/28 21:52:02 christos 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.1  christos 	wchar_t ibuf[] = { 0xcf10, 0 };
     57  1.1  christos 	setlocale(LC_CTYPE, "zh_TW.Big5");
     58  1.1  christos 	ATF_REQUIRE_EQ(wprintf(L"%ls\n", ibuf), -1);
     59  1.1  christos }
     60  1.1  christos 
     61  1.1  christos ATF_TC(bad_big5_swprintf);
     62  1.1  christos ATF_TC_HEAD(bad_big5_swprintf, tc)
     63  1.1  christos {
     64  1.1  christos 	atf_tc_set_md_var(tc, "descr", "Test bad big5 wchar swprintf");
     65  1.1  christos }
     66  1.1  christos 
     67  1.1  christos ATF_TC_BODY(bad_big5_swprintf, tc)
     68  1.1  christos {
     69  1.1  christos 	wchar_t ibuf[] = { 0xcf10, 0 };
     70  1.1  christos 	wchar_t obuf[20];
     71  1.1  christos 	setlocale(LC_CTYPE, "zh_TW.Big5");
     72  1.1  christos 	ATF_REQUIRE_EQ(swprintf(obuf, sizeof(obuf), L"%ls\n", ibuf), -1);
     73  1.1  christos }
     74  1.1  christos 
     75  1.1  christos ATF_TC(good_big5_wprintf);
     76  1.1  christos ATF_TC_HEAD(good_big5_wprintf, tc)
     77  1.1  christos {
     78  1.1  christos 	atf_tc_set_md_var(tc, "descr", "Test good big5 wchar wprintf");
     79  1.1  christos }
     80  1.1  christos 
     81  1.1  christos ATF_TC_BODY(good_big5_wprintf, tc)
     82  1.1  christos {
     83  1.1  christos 	wchar_t ibuf[] = { 0xcf40, 0 };
     84  1.1  christos 	setlocale(LC_CTYPE, "zh_TW.Big5");
     85  1.1  christos 	// WTF? swprintf() fails, wprintf succeeds?
     86  1.1  christos 	ATF_REQUIRE_EQ(wprintf(L"%ls\n", ibuf), 2);
     87  1.1  christos }
     88  1.1  christos 
     89  1.1  christos ATF_TC(good_big5_swprintf);
     90  1.1  christos ATF_TC_HEAD(good_big5_swprintf, tc)
     91  1.1  christos {
     92  1.1  christos 	atf_tc_set_md_var(tc, "descr", "Test good big5 wchar swprintf");
     93  1.1  christos }
     94  1.1  christos 
     95  1.1  christos ATF_TC_BODY(good_big5_swprintf, tc)
     96  1.1  christos {
     97  1.1  christos 	wchar_t ibuf[] = { 0xcf40, 0 };
     98  1.1  christos 	wchar_t obuf[20];
     99  1.1  christos 	setlocale(LC_CTYPE, "zh_TW.Big5");
    100  1.1  christos 	ATF_REQUIRE_EQ(swprintf(obuf, sizeof(obuf), L"%ls\n", ibuf), 2);
    101  1.1  christos }
    102  1.1  christos 
    103  1.1  christos static int readfn(void *p, char *buf, int len) {
    104  1.1  christos 	memcpy(buf, p, MIN(len, 2));
    105  1.1  christos 	return 2;
    106  1.1  christos }
    107  1.1  christos 
    108  1.1  christos ATF_TC(good_big5_getwc);
    109  1.1  christos ATF_TC_HEAD(good_big5_getwc, tc)
    110  1.1  christos {
    111  1.1  christos 	atf_tc_set_md_var(tc, "descr", "Test good big5 wchar getwc");
    112  1.1  christos }
    113  1.1  christos 
    114  1.1  christos ATF_TC_BODY(good_big5_getwc, tc)
    115  1.1  christos {
    116  1.1  christos 	char ibuf[] = { 0xcf, 0x40 };
    117  1.1  christos 	FILE *fp = funopen(ibuf, readfn, NULL, NULL, NULL);
    118  1.1  christos 
    119  1.1  christos 	ATF_REQUIRE(fp != NULL);
    120  1.1  christos 	setlocale(LC_CTYPE, "zh_TW.Big5");
    121  1.1  christos 	ATF_REQUIRE_EQ(getwc(fp), 0xcf40);
    122  1.1  christos 	fclose(fp);
    123  1.1  christos }
    124  1.1  christos 
    125  1.1  christos ATF_TC(bad_big5_getwc);
    126  1.1  christos ATF_TC_HEAD(bad_big5_getwc, tc)
    127  1.1  christos {
    128  1.1  christos 	atf_tc_set_md_var(tc, "descr", "Test bad big5 wchar getwc");
    129  1.1  christos }
    130  1.1  christos 
    131  1.1  christos ATF_TC_BODY(bad_big5_getwc, tc)
    132  1.1  christos {
    133  1.1  christos 	char ibuf[] = { 0xcf, 0x20 };
    134  1.1  christos 	FILE *fp = funopen(ibuf, readfn, NULL, NULL, NULL);
    135  1.1  christos 
    136  1.1  christos 	ATF_REQUIRE(fp != NULL);
    137  1.1  christos 	setlocale(LC_CTYPE, "zh_TW.Big5");
    138  1.1  christos 	ATF_REQUIRE_EQ(getwc(fp), WEOF);
    139  1.1  christos 	fclose(fp);
    140  1.1  christos }
    141  1.1  christos 
    142  1.1  christos ATF_TC(bad_eucJP_getwc);
    143  1.1  christos ATF_TC_HEAD(bad_eucJP_getwc, tc)
    144  1.1  christos {
    145  1.1  christos 	atf_tc_set_md_var(tc, "descr", "Test bad eucJP wchar getwc");
    146  1.1  christos }
    147  1.1  christos 
    148  1.1  christos ATF_TC_BODY(bad_eucJP_getwc, tc)
    149  1.1  christos {
    150  1.1  christos 	char ibuf[] = { 0xcf, 0x20 };
    151  1.1  christos 	FILE *fp = funopen(ibuf, readfn, NULL, NULL, NULL);
    152  1.1  christos 
    153  1.1  christos 	ATF_REQUIRE(fp != NULL);
    154  1.1  christos 	setlocale(LC_CTYPE, "ja_JP.eucJP");
    155  1.1  christos 	// WTF? Not even returning what it read?
    156  1.1  christos 	ATF_CHECK_EQ(getwc(fp), 0xcf20);
    157  1.1  christos 	ATF_REQUIRE_EQ(getwc(fp), WEOF);
    158  1.1  christos 	fclose(fp);
    159  1.1  christos }
    160  1.1  christos 
    161  1.1  christos ATF_TP_ADD_TCS(tp)
    162  1.1  christos {
    163  1.1  christos 	ATF_TP_ADD_TC(tp, bad_big5_wprintf);
    164  1.1  christos 	ATF_TP_ADD_TC(tp, bad_big5_swprintf);
    165  1.1  christos 	ATF_TP_ADD_TC(tp, good_big5_wprintf);
    166  1.1  christos 	ATF_TP_ADD_TC(tp, good_big5_swprintf);
    167  1.1  christos 	ATF_TP_ADD_TC(tp, good_big5_getwc);
    168  1.1  christos 	ATF_TP_ADD_TC(tp, bad_big5_getwc);
    169  1.1  christos 	ATF_TP_ADD_TC(tp, bad_eucJP_getwc);
    170  1.1  christos 
    171  1.1  christos 	return atf_no_error();
    172  1.1  christos }
    173