t_mbsnrtowcs.c revision 1.1
1/* $NetBSD: t_mbsnrtowcs.c,v 1.1 2013/05/28 16:57:56 joerg 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.1 2013/05/28 16:57:56 joerg Exp $");
34
35#include <locale.h>
36#include <wchar.h>
37
38#include <atf-c.h>
39
40static const struct test {
41	const char *locale;
42	const char *data;
43	size_t limit;
44	const wchar_t output1[64];
45	size_t output1_len;
46	const wchar_t output2[64];
47	size_t output2_len;
48} tests[] = {
49	{ "C", "ABCD0123", 4, { 0x41, 0x42, 0x43, 0x44 }, 4,
50			    { 0x30, 0x31, 0x32, 0x33, 0x0 }, 5 },
51	{ "en_US.UTF-8", "ABCD0123", 4, { 0x41, 0x42, 0x43, 0x44 }, 4,
52			    { 0x30, 0x31, 0x32, 0x33, 0x0 }, 5 },
53	{ "en_US.UTF-8", "ABC\303\2440123", 4, { 0x41, 0x42, 0x43, }, 3,
54			    { 0xe4, 0x30, 0x31, 0x32, 0x33, 0x0 }, 6 },
55};
56
57ATF_TC(mbsnrtowcs);
58ATF_TC_HEAD(mbsnrtowcs, tc)
59{
60	atf_tc_set_md_var(tc, "descr",
61		"Checks mbsnrtowc(3) with different locales");
62}
63ATF_TC_BODY(mbsnrtowcs, tc)
64{
65	size_t i;
66	const struct test *t;
67	mbstate_t state;
68	wchar_t buf[64];
69	const char *src;
70	size_t len;
71
72	for (i = 0; i < __arraycount(tests); ++i) {
73		t = &tests[i];
74		ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
75		ATF_REQUIRE(setlocale(LC_CTYPE, t->locale) != NULL);
76		memset(&state, 0, sizeof(state));
77		src = t->data;
78		len = mbsnrtowcs(buf, &src, t->limit,
79		    __arraycount(buf), &state);
80		ATF_REQUIRE_EQ(src, t->data + t->limit);
81		ATF_REQUIRE_EQ(len, t->output1_len);
82		ATF_REQUIRE(wmemcmp(t->output1, buf, len) == 0);
83		len = mbsnrtowcs(buf, &src, strlen(src) + 1,
84		    __arraycount(buf), &state);
85		ATF_REQUIRE_EQ(len, strlen(t->data) - t->limit);
86		ATF_REQUIRE(wmemcmp(t->output2, buf, len + 1) == 0);
87		ATF_REQUIRE_EQ(src, NULL);
88	}
89}
90
91ATF_TP_ADD_TCS(tp)
92{
93
94	ATF_TP_ADD_TC(tp, mbsnrtowcs);
95
96	return atf_no_error();
97}
98