t_mbsnrtowcs.c revision 1.2
11.2Syamt/* $NetBSD: t_mbsnrtowcs.c,v 1.2 2014/05/06 00:41:26 yamt Exp $ */ 21.1Sjoerg 31.1Sjoerg/*- 41.1Sjoerg * Copyright (c) 2013 The NetBSD Foundation, Inc. 51.1Sjoerg * All rights reserved. 61.1Sjoerg * 71.1Sjoerg * This code is derived from software contributed to The NetBSD Foundation 81.1Sjoerg * by Joerg Sonnenberger. 91.1Sjoerg * 101.1Sjoerg * Redistribution and use in source and binary forms, with or without 111.1Sjoerg * modification, are permitted provided that the following conditions 121.1Sjoerg * are met: 131.1Sjoerg * 1. Redistributions of source code must retain the above copyright 141.1Sjoerg * notice, this list of conditions and the following disclaimer. 151.1Sjoerg * 2. Redistributions in binary form must reproduce the above copyright 161.1Sjoerg * notice, this list of conditions and the following disclaimer in the 171.1Sjoerg * documentation and/or other materials provided with the distribution. 181.1Sjoerg * 191.1Sjoerg * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 201.1Sjoerg * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 211.1Sjoerg * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 221.1Sjoerg * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 231.1Sjoerg * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 241.1Sjoerg * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 251.1Sjoerg * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 261.1Sjoerg * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 271.1Sjoerg * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 281.1Sjoerg * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 291.1Sjoerg * POSSIBILITY OF SUCH DAMAGE. 301.1Sjoerg */ 311.1Sjoerg 321.1Sjoerg#include <sys/cdefs.h> 331.2Syamt__RCSID("$NetBSD: t_mbsnrtowcs.c,v 1.2 2014/05/06 00:41:26 yamt Exp $"); 341.1Sjoerg 351.1Sjoerg#include <locale.h> 361.2Syamt#include <string.h> 371.1Sjoerg#include <wchar.h> 381.1Sjoerg 391.1Sjoerg#include <atf-c.h> 401.1Sjoerg 411.1Sjoergstatic const struct test { 421.1Sjoerg const char *locale; 431.1Sjoerg const char *data; 441.1Sjoerg size_t limit; 451.1Sjoerg const wchar_t output1[64]; 461.1Sjoerg size_t output1_len; 471.1Sjoerg const wchar_t output2[64]; 481.1Sjoerg size_t output2_len; 491.1Sjoerg} tests[] = { 501.1Sjoerg { "C", "ABCD0123", 4, { 0x41, 0x42, 0x43, 0x44 }, 4, 511.1Sjoerg { 0x30, 0x31, 0x32, 0x33, 0x0 }, 5 }, 521.1Sjoerg { "en_US.UTF-8", "ABCD0123", 4, { 0x41, 0x42, 0x43, 0x44 }, 4, 531.1Sjoerg { 0x30, 0x31, 0x32, 0x33, 0x0 }, 5 }, 541.1Sjoerg { "en_US.UTF-8", "ABC\303\2440123", 4, { 0x41, 0x42, 0x43, }, 3, 551.1Sjoerg { 0xe4, 0x30, 0x31, 0x32, 0x33, 0x0 }, 6 }, 561.1Sjoerg}; 571.1Sjoerg 581.1SjoergATF_TC(mbsnrtowcs); 591.1SjoergATF_TC_HEAD(mbsnrtowcs, tc) 601.1Sjoerg{ 611.1Sjoerg atf_tc_set_md_var(tc, "descr", 621.1Sjoerg "Checks mbsnrtowc(3) with different locales"); 631.1Sjoerg} 641.1SjoergATF_TC_BODY(mbsnrtowcs, tc) 651.1Sjoerg{ 661.1Sjoerg size_t i; 671.1Sjoerg const struct test *t; 681.1Sjoerg mbstate_t state; 691.1Sjoerg wchar_t buf[64]; 701.1Sjoerg const char *src; 711.1Sjoerg size_t len; 721.1Sjoerg 731.1Sjoerg for (i = 0; i < __arraycount(tests); ++i) { 741.1Sjoerg t = &tests[i]; 751.1Sjoerg ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C"); 761.1Sjoerg ATF_REQUIRE(setlocale(LC_CTYPE, t->locale) != NULL); 771.1Sjoerg memset(&state, 0, sizeof(state)); 781.1Sjoerg src = t->data; 791.1Sjoerg len = mbsnrtowcs(buf, &src, t->limit, 801.1Sjoerg __arraycount(buf), &state); 811.1Sjoerg ATF_REQUIRE_EQ(src, t->data + t->limit); 821.1Sjoerg ATF_REQUIRE_EQ(len, t->output1_len); 831.1Sjoerg ATF_REQUIRE(wmemcmp(t->output1, buf, len) == 0); 841.1Sjoerg len = mbsnrtowcs(buf, &src, strlen(src) + 1, 851.1Sjoerg __arraycount(buf), &state); 861.1Sjoerg ATF_REQUIRE_EQ(len, strlen(t->data) - t->limit); 871.1Sjoerg ATF_REQUIRE(wmemcmp(t->output2, buf, len + 1) == 0); 881.1Sjoerg ATF_REQUIRE_EQ(src, NULL); 891.1Sjoerg } 901.1Sjoerg} 911.1Sjoerg 921.1SjoergATF_TP_ADD_TCS(tp) 931.1Sjoerg{ 941.1Sjoerg 951.1Sjoerg ATF_TP_ADD_TC(tp, mbsnrtowcs); 961.1Sjoerg 971.1Sjoerg return atf_no_error(); 981.1Sjoerg} 99