1272343Sngie/* $NetBSD: t_mbsnrtowcs.c,v 1.2 2014/05/06 00:41:26 yamt Exp $ */
2272343Sngie
3272343Sngie/*-
4272343Sngie * Copyright (c) 2013 The NetBSD Foundation, Inc.
5272343Sngie * All rights reserved.
6272343Sngie *
7272343Sngie * This code is derived from software contributed to The NetBSD Foundation
8272343Sngie * by Joerg Sonnenberger.
9272343Sngie *
10272343Sngie * Redistribution and use in source and binary forms, with or without
11272343Sngie * modification, are permitted provided that the following conditions
12272343Sngie * are met:
13272343Sngie * 1. Redistributions of source code must retain the above copyright
14272343Sngie *    notice, this list of conditions and the following disclaimer.
15272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
16272343Sngie *    notice, this list of conditions and the following disclaimer in the
17272343Sngie *    documentation and/or other materials provided with the distribution.
18272343Sngie *
19272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22272343Sngie * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29272343Sngie * POSSIBILITY OF SUCH DAMAGE.
30272343Sngie */
31272343Sngie
32272343Sngie#include <sys/cdefs.h>
33272343Sngie__RCSID("$NetBSD: t_mbsnrtowcs.c,v 1.2 2014/05/06 00:41:26 yamt Exp $");
34272343Sngie
35272343Sngie#include <locale.h>
36272343Sngie#include <string.h>
37272343Sngie#include <wchar.h>
38272343Sngie
39272343Sngie#include <atf-c.h>
40272343Sngie
41272343Sngiestatic const struct test {
42272343Sngie	const char *locale;
43272343Sngie	const char *data;
44272343Sngie	size_t limit;
45272343Sngie	const wchar_t output1[64];
46272343Sngie	size_t output1_len;
47272343Sngie	const wchar_t output2[64];
48272343Sngie	size_t output2_len;
49272343Sngie} tests[] = {
50272343Sngie	{ "C", "ABCD0123", 4, { 0x41, 0x42, 0x43, 0x44 }, 4,
51272343Sngie			    { 0x30, 0x31, 0x32, 0x33, 0x0 }, 5 },
52272343Sngie	{ "en_US.UTF-8", "ABCD0123", 4, { 0x41, 0x42, 0x43, 0x44 }, 4,
53272343Sngie			    { 0x30, 0x31, 0x32, 0x33, 0x0 }, 5 },
54272343Sngie	{ "en_US.UTF-8", "ABC\303\2440123", 4, { 0x41, 0x42, 0x43, }, 3,
55272343Sngie			    { 0xe4, 0x30, 0x31, 0x32, 0x33, 0x0 }, 6 },
56272343Sngie};
57272343Sngie
58272343SngieATF_TC(mbsnrtowcs);
59272343SngieATF_TC_HEAD(mbsnrtowcs, tc)
60272343Sngie{
61272343Sngie	atf_tc_set_md_var(tc, "descr",
62272343Sngie		"Checks mbsnrtowc(3) with different locales");
63272343Sngie}
64272343SngieATF_TC_BODY(mbsnrtowcs, tc)
65272343Sngie{
66272343Sngie	size_t i;
67272343Sngie	const struct test *t;
68272343Sngie	mbstate_t state;
69272343Sngie	wchar_t buf[64];
70272343Sngie	const char *src;
71272343Sngie	size_t len;
72272343Sngie
73272343Sngie	for (i = 0; i < __arraycount(tests); ++i) {
74272343Sngie		t = &tests[i];
75272343Sngie		ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
76272343Sngie		ATF_REQUIRE(setlocale(LC_CTYPE, t->locale) != NULL);
77272343Sngie		memset(&state, 0, sizeof(state));
78272343Sngie		src = t->data;
79272343Sngie		len = mbsnrtowcs(buf, &src, t->limit,
80272343Sngie		    __arraycount(buf), &state);
81272343Sngie		ATF_REQUIRE_EQ(src, t->data + t->limit);
82272343Sngie		ATF_REQUIRE_EQ(len, t->output1_len);
83272343Sngie		ATF_REQUIRE(wmemcmp(t->output1, buf, len) == 0);
84272343Sngie		len = mbsnrtowcs(buf, &src, strlen(src) + 1,
85272343Sngie		    __arraycount(buf), &state);
86272343Sngie		ATF_REQUIRE_EQ(len, strlen(t->data) - t->limit);
87272343Sngie		ATF_REQUIRE(wmemcmp(t->output2, buf, len + 1) == 0);
88272343Sngie		ATF_REQUIRE_EQ(src, NULL);
89272343Sngie	}
90272343Sngie}
91272343Sngie
92272343SngieATF_TP_ADD_TCS(tp)
93272343Sngie{
94272343Sngie
95272343Sngie	ATF_TP_ADD_TC(tp, mbsnrtowcs);
96272343Sngie
97272343Sngie	return atf_no_error();
98272343Sngie}
99