xref: /netbsd-src/tests/lib/libc/locale/t_mbsnrtowcs.c (revision d1d388317bda7ffaa5379c0cc3e8b5f09554892d)
1*d1d38831Syamt /* $NetBSD: t_mbsnrtowcs.c,v 1.2 2014/05/06 00:41:26 yamt Exp $ */
285a67e61Sjoerg 
385a67e61Sjoerg /*-
485a67e61Sjoerg  * Copyright (c) 2013 The NetBSD Foundation, Inc.
585a67e61Sjoerg  * All rights reserved.
685a67e61Sjoerg  *
785a67e61Sjoerg  * This code is derived from software contributed to The NetBSD Foundation
885a67e61Sjoerg  * by Joerg Sonnenberger.
985a67e61Sjoerg  *
1085a67e61Sjoerg  * Redistribution and use in source and binary forms, with or without
1185a67e61Sjoerg  * modification, are permitted provided that the following conditions
1285a67e61Sjoerg  * are met:
1385a67e61Sjoerg  * 1. Redistributions of source code must retain the above copyright
1485a67e61Sjoerg  *    notice, this list of conditions and the following disclaimer.
1585a67e61Sjoerg  * 2. Redistributions in binary form must reproduce the above copyright
1685a67e61Sjoerg  *    notice, this list of conditions and the following disclaimer in the
1785a67e61Sjoerg  *    documentation and/or other materials provided with the distribution.
1885a67e61Sjoerg  *
1985a67e61Sjoerg  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2085a67e61Sjoerg  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2185a67e61Sjoerg  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2285a67e61Sjoerg  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2385a67e61Sjoerg  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2485a67e61Sjoerg  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2585a67e61Sjoerg  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2685a67e61Sjoerg  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2785a67e61Sjoerg  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2885a67e61Sjoerg  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2985a67e61Sjoerg  * POSSIBILITY OF SUCH DAMAGE.
3085a67e61Sjoerg  */
3185a67e61Sjoerg 
3285a67e61Sjoerg #include <sys/cdefs.h>
33*d1d38831Syamt __RCSID("$NetBSD: t_mbsnrtowcs.c,v 1.2 2014/05/06 00:41:26 yamt Exp $");
3485a67e61Sjoerg 
3585a67e61Sjoerg #include <locale.h>
36*d1d38831Syamt #include <string.h>
3785a67e61Sjoerg #include <wchar.h>
3885a67e61Sjoerg 
3985a67e61Sjoerg #include <atf-c.h>
4085a67e61Sjoerg 
4185a67e61Sjoerg static const struct test {
4285a67e61Sjoerg 	const char *locale;
4385a67e61Sjoerg 	const char *data;
4485a67e61Sjoerg 	size_t limit;
4585a67e61Sjoerg 	const wchar_t output1[64];
4685a67e61Sjoerg 	size_t output1_len;
4785a67e61Sjoerg 	const wchar_t output2[64];
4885a67e61Sjoerg 	size_t output2_len;
4985a67e61Sjoerg } tests[] = {
5085a67e61Sjoerg 	{ "C", "ABCD0123", 4, { 0x41, 0x42, 0x43, 0x44 }, 4,
5185a67e61Sjoerg 			    { 0x30, 0x31, 0x32, 0x33, 0x0 }, 5 },
5285a67e61Sjoerg 	{ "en_US.UTF-8", "ABCD0123", 4, { 0x41, 0x42, 0x43, 0x44 }, 4,
5385a67e61Sjoerg 			    { 0x30, 0x31, 0x32, 0x33, 0x0 }, 5 },
5485a67e61Sjoerg 	{ "en_US.UTF-8", "ABC\303\2440123", 4, { 0x41, 0x42, 0x43, }, 3,
5585a67e61Sjoerg 			    { 0xe4, 0x30, 0x31, 0x32, 0x33, 0x0 }, 6 },
5685a67e61Sjoerg };
5785a67e61Sjoerg 
5885a67e61Sjoerg ATF_TC(mbsnrtowcs);
ATF_TC_HEAD(mbsnrtowcs,tc)5985a67e61Sjoerg ATF_TC_HEAD(mbsnrtowcs, tc)
6085a67e61Sjoerg {
6185a67e61Sjoerg 	atf_tc_set_md_var(tc, "descr",
6285a67e61Sjoerg 		"Checks mbsnrtowc(3) with different locales");
6385a67e61Sjoerg }
ATF_TC_BODY(mbsnrtowcs,tc)6485a67e61Sjoerg ATF_TC_BODY(mbsnrtowcs, tc)
6585a67e61Sjoerg {
6685a67e61Sjoerg 	size_t i;
6785a67e61Sjoerg 	const struct test *t;
6885a67e61Sjoerg 	mbstate_t state;
6985a67e61Sjoerg 	wchar_t buf[64];
7085a67e61Sjoerg 	const char *src;
7185a67e61Sjoerg 	size_t len;
7285a67e61Sjoerg 
7385a67e61Sjoerg 	for (i = 0; i < __arraycount(tests); ++i) {
7485a67e61Sjoerg 		t = &tests[i];
7585a67e61Sjoerg 		ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
7685a67e61Sjoerg 		ATF_REQUIRE(setlocale(LC_CTYPE, t->locale) != NULL);
7785a67e61Sjoerg 		memset(&state, 0, sizeof(state));
7885a67e61Sjoerg 		src = t->data;
7985a67e61Sjoerg 		len = mbsnrtowcs(buf, &src, t->limit,
8085a67e61Sjoerg 		    __arraycount(buf), &state);
8185a67e61Sjoerg 		ATF_REQUIRE_EQ(src, t->data + t->limit);
8285a67e61Sjoerg 		ATF_REQUIRE_EQ(len, t->output1_len);
8385a67e61Sjoerg 		ATF_REQUIRE(wmemcmp(t->output1, buf, len) == 0);
8485a67e61Sjoerg 		len = mbsnrtowcs(buf, &src, strlen(src) + 1,
8585a67e61Sjoerg 		    __arraycount(buf), &state);
8685a67e61Sjoerg 		ATF_REQUIRE_EQ(len, strlen(t->data) - t->limit);
8785a67e61Sjoerg 		ATF_REQUIRE(wmemcmp(t->output2, buf, len + 1) == 0);
8885a67e61Sjoerg 		ATF_REQUIRE_EQ(src, NULL);
8985a67e61Sjoerg 	}
9085a67e61Sjoerg }
9185a67e61Sjoerg 
ATF_TP_ADD_TCS(tp)9285a67e61Sjoerg ATF_TP_ADD_TCS(tp)
9385a67e61Sjoerg {
9485a67e61Sjoerg 
9585a67e61Sjoerg 	ATF_TP_ADD_TC(tp, mbsnrtowcs);
9685a67e61Sjoerg 
9785a67e61Sjoerg 	return atf_no_error();
9885a67e61Sjoerg }
99