t_mbsnrtowcs.c revision 272345
158551Skris/* $NetBSD: t_mbsnrtowcs.c,v 1.2 2014/05/06 00:41:26 yamt Exp $ */ 2228060Sbapt 3228060Sbapt/*- 4228060Sbapt * Copyright (c) 2013 The NetBSD Foundation, Inc. 558551Skris * All rights reserved. 6228060Sbapt * 758551Skris * This code is derived from software contributed to The NetBSD Foundation 8228060Sbapt * by Joerg Sonnenberger. 9228060Sbapt * 10228060Sbapt * Redistribution and use in source and binary forms, with or without 11228060Sbapt * modification, are permitted provided that the following conditions 1258551Skris * are met: 13228060Sbapt * 1. Redistributions of source code must retain the above copyright 14228060Sbapt * notice, this list of conditions and the following disclaimer. 15228060Sbapt * 2. Redistributions in binary form must reproduce the above copyright 16228060Sbapt * notice, this list of conditions and the following disclaimer in the 1758551Skris * documentation and/or other materials provided with the distribution. 18228060Sbapt * 19228060Sbapt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20228060Sbapt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21228060Sbapt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 2258551Skris * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23228060Sbapt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2458551Skris * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2558551Skris * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2658551Skris * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2758551Skris * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2858551Skris * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 2958551Skris * POSSIBILITY OF SUCH DAMAGE. 3058551Skris */ 31228060Sbapt 32228060Sbapt#include <sys/cdefs.h> 33228060Sbapt__RCSID("$NetBSD: t_mbsnrtowcs.c,v 1.2 2014/05/06 00:41:26 yamt Exp $"); 3458551Skris 35228060Sbapt#include <locale.h> 36228060Sbapt#include <string.h> 37228060Sbapt#include <wchar.h> 38228060Sbapt 39228060Sbapt#include <atf-c.h> 40228060Sbapt 41228060Sbaptstatic const struct test { 42228060Sbapt const char *locale; 43228060Sbapt const char *data; 44228060Sbapt size_t limit; 45228060Sbapt const wchar_t output1[64]; 4658551Skris size_t output1_len; 47228060Sbapt const wchar_t output2[64]; 48228060Sbapt size_t output2_len; 49228060Sbapt} tests[] = { 50228060Sbapt { "C", "ABCD0123", 4, { 0x41, 0x42, 0x43, 0x44 }, 4, 51228060Sbapt { 0x30, 0x31, 0x32, 0x33, 0x0 }, 5 }, 52228060Sbapt { "en_US.UTF-8", "ABCD0123", 4, { 0x41, 0x42, 0x43, 0x44 }, 4, 53228060Sbapt { 0x30, 0x31, 0x32, 0x33, 0x0 }, 5 }, 54228060Sbapt { "en_US.UTF-8", "ABC\303\2440123", 4, { 0x41, 0x42, 0x43, }, 3, 55228060Sbapt { 0xe4, 0x30, 0x31, 0x32, 0x33, 0x0 }, 6 }, 56228060Sbapt}; 57228060Sbapt 58228060SbaptATF_TC(mbsnrtowcs); 59228060SbaptATF_TC_HEAD(mbsnrtowcs, tc) 60228060Sbapt{ 61228060Sbapt atf_tc_set_md_var(tc, "descr", 62228060Sbapt "Checks mbsnrtowc(3) with different locales"); 63228060Sbapt} 64228060SbaptATF_TC_BODY(mbsnrtowcs, tc) 65228060Sbapt{ 66228060Sbapt size_t i; 67228060Sbapt const struct test *t; 68228060Sbapt mbstate_t state; 69228060Sbapt wchar_t buf[64]; 70228060Sbapt const char *src; 71228060Sbapt size_t len; 72228060Sbapt 73228060Sbapt for (i = 0; i < __arraycount(tests); ++i) { 74228060Sbapt t = &tests[i]; 75228060Sbapt ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C"); 76228060Sbapt ATF_REQUIRE(setlocale(LC_CTYPE, t->locale) != NULL); 77228060Sbapt memset(&state, 0, sizeof(state)); 78228060Sbapt src = t->data; 79228060Sbapt len = mbsnrtowcs(buf, &src, t->limit, 80228060Sbapt __arraycount(buf), &state); 81228060Sbapt ATF_REQUIRE_EQ(src, t->data + t->limit); 82228060Sbapt ATF_REQUIRE_EQ(len, t->output1_len); 8358551Skris ATF_REQUIRE(wmemcmp(t->output1, buf, len) == 0); 8458551Skris len = mbsnrtowcs(buf, &src, strlen(src) + 1, 85228060Sbapt __arraycount(buf), &state); 86228060Sbapt ATF_REQUIRE_EQ(len, strlen(t->data) - t->limit); 8758551Skris ATF_REQUIRE(wmemcmp(t->output2, buf, len + 1) == 0); 88228060Sbapt ATF_REQUIRE_EQ(src, NULL); 89228060Sbapt } 9058551Skris} 91228060Sbapt 92228060SbaptATF_TP_ADD_TCS(tp) 93228060Sbapt{ 94228060Sbapt 95228060Sbapt ATF_TP_ADD_TC(tp, mbsnrtowcs); 9658551Skris 97228060Sbapt return atf_no_error(); 98228060Sbapt} 99228060Sbapt