1272343Sngie/* $NetBSD: t_mbstowcs.c,v 1.1 2011/07/15 07:35:21 jruoho Exp $ */ 2272343Sngie 3272343Sngie/*- 4272343Sngie * Copyright (c) 2011 The NetBSD Foundation, Inc. 5272343Sngie * All rights reserved. 6272343Sngie * 7272343Sngie * Redistribution and use in source and binary forms, with or without 8272343Sngie * modification, are permitted provided that the following conditions 9272343Sngie * are met: 10272343Sngie * 1. Redistributions of source code must retain the above copyright 11272343Sngie * notice, this list of conditions and the following disclaimer. 12272343Sngie * 2. Redistributions in binary form must reproduce the above copyright 13272343Sngie * notice, this list of conditions and the following disclaimer in the 14272343Sngie * documentation and/or other materials provided with the distribution. 15272343Sngie * 16272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19272343Sngie * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26272343Sngie * POSSIBILITY OF SUCH DAMAGE. 27272343Sngie */ 28272343Sngie 29272343Sngie/*- 30272343Sngie * Copyright (c)2003 Citrus Project, 31272343Sngie * All rights reserved. 32272343Sngie * 33272343Sngie * Redistribution and use in source and binary forms, with or without 34272343Sngie * modification, are permitted provided that the following conditions 35272343Sngie * are met: 36272343Sngie * 1. Redistributions of source code must retain the above copyright 37272343Sngie * notice, this list of conditions and the following disclaimer. 38272343Sngie * 2. Redistributions in binary form must reproduce the above copyright 39272343Sngie * notice, this list of conditions and the following disclaimer in the 40272343Sngie * documentation and/or other materials provided with the distribution. 41272343Sngie * 42272343Sngie * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 43272343Sngie * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 44272343Sngie * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 45272343Sngie * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 46272343Sngie * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 47272343Sngie * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 48272343Sngie * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 49272343Sngie * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 50272343Sngie * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 51272343Sngie * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 52272343Sngie * SUCH DAMAGE. 53272343Sngie */ 54272343Sngie 55272343Sngie#include <sys/cdefs.h> 56272343Sngie__COPYRIGHT("@(#) Copyright (c) 2011\ 57272343Sngie The NetBSD Foundation, inc. All rights reserved."); 58272343Sngie__RCSID("$NetBSD: t_mbstowcs.c,v 1.1 2011/07/15 07:35:21 jruoho Exp $"); 59272343Sngie 60272343Sngie#include <errno.h> 61272343Sngie#include <locale.h> 62272343Sngie#include <stdio.h> 63272343Sngie#include <stdlib.h> 64272343Sngie#include <string.h> 65272343Sngie#include <vis.h> 66272343Sngie#include <wchar.h> 67272343Sngie 68272343Sngie#include <atf-c.h> 69272343Sngie 70272343Sngie#define REQUIRE_ERRNO(x, v) \ 71272343Sngie ATF_REQUIRE_MSG((x) != (v), "%s: %s", #x, strerror(errno)) 72272343Sngie 73272343Sngie#define SIZE 256 74272343Sngie 75272343Sngiestatic struct test { 76272343Sngie const char *locale; 77272343Sngie const char *data; 78272343Sngie wchar_t wchars[64]; 79272343Sngie int widths[64]; 80272343Sngie int width; 81272343Sngie} tests[] = { 82272343Sngie{ 83272343Sngie "en_US.UTF-8", 84272343Sngie "[\001\177][\302\200\337\277][\340\240\200\357\277\277][\360\220\200" 85287568Srodrigc "\200\364\217\277\277]", 86272343Sngie { 87272343Sngie 0x5B, 0x01, 0x7F, 0x5D, 0x5B, 0x80, 0x07FF, 0x5D, 0x5B, 0x0800, 88287568Srodrigc 0xFFFF, 0x5D, 0x5B, 0x10000, 0x10FFFF, 0x5D, 0x0A 89272343Sngie }, 90272343Sngie { 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 91272343Sngie 1, 1, -1, -1, 1, 1, -1, -1, 1, -1 92272343Sngie }, 93272343Sngie -1 94272343Sngie}, { 95272343Sngie "ja_JP.ISO2022-JP", 96272343Sngie "\033$B#J#I#S$G$9!#\033(Baaaa\033$B$\"$$$&$($*\033(B", 97272343Sngie { 98272343Sngie 0x4200234A, 0x42002349, 0x42002353, 0x42002447, 0x42002439, 99272343Sngie 0x42002123, 0x61, 0x61, 0x61, 0x61, 0x42002422, 0x42002424, 100272343Sngie 0x42002426, 0x42002428, 0x4200242A, 0x0A 101272343Sngie }, 102272343Sngie { 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 2, 2, -1 }, 103272343Sngie 26 104272343Sngie}, { 105272343Sngie "ja_JP.SJIS", 106272343Sngie "\202r\202i\202h\202r\202\305\202\267\201Baaaa\202\240\202\242" 107272343Sngie "\202\244\202\246\202\250", 108272343Sngie { 109272343Sngie 0x8272, 0x8269, 0x8268, 0x8272, 0x82C5, 0x82B7, 0x8142, 0x61, 110272343Sngie 0x61, 0x61, 0x61, 0x82A0, 0x82A2, 0x82A4, 0x82A6, 0x82A8, 0x0A 111272343Sngie }, 112272343Sngie { 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 2, 2, -1 }, 113272343Sngie 28 114272343Sngie}, { 115272343Sngie "ja_JP.eucJP", 116272343Sngie "\243\305\243\325\243\303\244\307\244\271\241\243aaaa\244\242\244" 117272343Sngie "\244\244\246\244\250\244\252", 118272343Sngie { 119272343Sngie 0xA3C5, 0xA3D5, 0xA3C3, 0xA4C7, 0xA4B9, 0xA1A3, 0x61, 0x61, 0x61, 120272343Sngie 0x61, 0xA4A2, 0xA4A4, 0xA4A6, 0xA4A8, 0xA4AA, 0x0A 121272343Sngie }, 122272343Sngie { 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 2, 2, -1 }, 123272343Sngie 26 124272343Sngie}, { 125272343Sngie NULL, 126272343Sngie NULL, 127272343Sngie {}, 128272343Sngie {}, 129272343Sngie 0 130272343Sngie} 131272343Sngie}; 132272343Sngie 133272343SngieATF_TC(mbstowcs_basic); 134272343SngieATF_TC_HEAD(mbstowcs_basic, tc) 135272343Sngie{ 136272343Sngie atf_tc_set_md_var(tc, "descr", 137272343Sngie "Checks wide character functions with different locales"); 138272343Sngie} 139272343SngieATF_TC_BODY(mbstowcs_basic, tc) 140272343Sngie{ 141272343Sngie struct test *t; 142272343Sngie 143272343Sngie for (t = &tests[0]; t->data != NULL; ++t) { 144272343Sngie wchar_t wbuf[SIZE]; 145272343Sngie char buf[SIZE]; 146272343Sngie char visbuf[SIZE]; 147272343Sngie char *str; 148272343Sngie int i; 149272343Sngie 150272343Sngie ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C"); 151276478Sngie#ifdef __NetBSD__ 152272343Sngie ATF_REQUIRE(setlocale(LC_CTYPE, t->locale) != NULL); 153276478Sngie#else 154276478Sngie if (setlocale(LC_CTYPE, t->locale) == NULL) { 155276478Sngie fprintf(stderr, "Locale %s not found.\n", t->locale); 156276478Sngie continue; 157276478Sngie } 158276478Sngie#endif 159272343Sngie 160272343Sngie (void)strvis(visbuf, t->data, VIS_WHITE | VIS_OCTAL); 161272343Sngie (void)printf("Checking string: \"%s\"\n", visbuf); 162272343Sngie 163272343Sngie ATF_REQUIRE((str = setlocale(LC_ALL, NULL)) != NULL); 164272343Sngie (void)printf("Using locale: %s\n", str); 165272343Sngie 166272343Sngie REQUIRE_ERRNO((ssize_t)mbstowcs(wbuf, t->data, SIZE-1), -1); 167272343Sngie REQUIRE_ERRNO((ssize_t)wcstombs(buf, wbuf, SIZE-1), -1); 168272343Sngie 169272343Sngie if (strcmp(buf, t->data) != 0) { 170272343Sngie (void)strvis(visbuf, buf, VIS_WHITE | VIS_OCTAL); 171272343Sngie (void)printf("Conversion to wcs and back failed: " 172272343Sngie "\"%s\"\n", visbuf); 173272343Sngie atf_tc_fail("Test failed"); 174272343Sngie } 175272343Sngie 176272343Sngie /* The output here is implementation-dependent. */ 177272343Sngie 178272343Sngie for (i = 0; wbuf[i] != 0; ++i) { 179272343Sngie if (wbuf[i] == t->wchars[i] && 180272343Sngie wcwidth(wbuf[i]) == t->widths[i]) 181272343Sngie continue; 182272343Sngie 183272343Sngie (void)printf("At position %d:\n", i); 184272343Sngie (void)printf(" expected: 0x%04X (%d)\n", 185272343Sngie t->wchars[i], t->widths[i]); 186272343Sngie (void)printf(" got : 0x%04X (%d)\n", wbuf[i], 187272343Sngie wcwidth(wbuf[i])); 188272343Sngie atf_tc_fail("Test failed"); 189272343Sngie } 190272343Sngie 191272343Sngie if (wcswidth(wbuf, SIZE-1) != t->width) { 192272343Sngie (void)printf("Incorrect wcswidth:\n"); 193272343Sngie (void)printf(" expected: %d\n", t->width); 194272343Sngie (void)printf(" got : %d\n", wcswidth(wbuf, SIZE-1)); 195272343Sngie atf_tc_fail("Test failed"); 196272343Sngie } 197272343Sngie 198272343Sngie (void)printf("Ok.\n"); 199272343Sngie } 200272343Sngie} 201272343Sngie 202272343SngieATF_TP_ADD_TCS(tp) 203272343Sngie{ 204272343Sngie ATF_TP_ADD_TC(tp, mbstowcs_basic); 205272343Sngie 206272343Sngie return atf_no_error(); 207272343Sngie} 208