1/* $NetBSD: t_mbsnrtowcs.c,v 1.2 2014/05/06 00:41:26 yamt Exp $ */
2
3/*-
4 * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Joerg Sonnenberger.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33__RCSID("$NetBSD: t_mbsnrtowcs.c,v 1.2 2014/05/06 00:41:26 yamt Exp $");
34
35#include <locale.h>
36#include <string.h>
37#include <wchar.h>
38
39#include <atf-c.h>
40
41static const struct test {
42	const char *locale;
43	const char *data;
44	size_t limit;
45	const wchar_t output1[64];
46	size_t output1_len;
47	const wchar_t output2[64];
48	size_t output2_len;
49} tests[] = {
50	{ "C", "ABCD0123", 4, { 0x41, 0x42, 0x43, 0x44 }, 4,
51			    { 0x30, 0x31, 0x32, 0x33, 0x0 }, 5 },
52	{ "en_US.UTF-8", "ABCD0123", 4, { 0x41, 0x42, 0x43, 0x44 }, 4,
53			    { 0x30, 0x31, 0x32, 0x33, 0x0 }, 5 },
54	{ "en_US.UTF-8", "ABC\303\2440123", 4, { 0x41, 0x42, 0x43, }, 3,
55			    { 0xe4, 0x30, 0x31, 0x32, 0x33, 0x0 }, 6 },
56};
57
58ATF_TC(mbsnrtowcs);
59ATF_TC_HEAD(mbsnrtowcs, tc)
60{
61	atf_tc_set_md_var(tc, "descr",
62		"Checks mbsnrtowc(3) with different locales");
63}
64ATF_TC_BODY(mbsnrtowcs, tc)
65{
66	size_t i;
67	const struct test *t;
68	mbstate_t state;
69	wchar_t buf[64];
70	const char *src;
71	size_t len;
72
73	for (i = 0; i < __arraycount(tests); ++i) {
74		t = &tests[i];
75		ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
76		ATF_REQUIRE(setlocale(LC_CTYPE, t->locale) != NULL);
77		memset(&state, 0, sizeof(state));
78		src = t->data;
79		len = mbsnrtowcs(buf, &src, t->limit,
80		    __arraycount(buf), &state);
81		ATF_REQUIRE_EQ(src, t->data + t->limit);
82		ATF_REQUIRE_EQ(len, t->output1_len);
83		ATF_REQUIRE(wmemcmp(t->output1, buf, len) == 0);
84		len = mbsnrtowcs(buf, &src, strlen(src) + 1,
85		    __arraycount(buf), &state);
86		ATF_REQUIRE_EQ(len, strlen(t->data) - t->limit);
87		ATF_REQUIRE(wmemcmp(t->output2, buf, len + 1) == 0);
88		ATF_REQUIRE_EQ(src, NULL);
89	}
90}
91
92ATF_TP_ADD_TCS(tp)
93{
94
95	ATF_TP_ADD_TC(tp, mbsnrtowcs);
96
97	return atf_no_error();
98}
99