summaryrefslogtreecommitdiff
path: root/tests/lib/libc
diff options
context:
space:
mode:
authorjoerg <joerg@NetBSD.org>2013-05-28 16:57:56 +0000
committerjoerg <joerg@NetBSD.org>2013-05-28 16:57:56 +0000
commitbf2f4b3ea18e80dbcc4508cb29f4ee30be2fc330 (patch)
treee588b602aa734c3ccb80e52852c894f39d18b2e6 /tests/lib/libc
parentd4b28bc86dff2e3b37b6056d53eeb426d81502fd (diff)
Add mbsnrtowcs and wcsnrtombs. Approved by core.
Diffstat (limited to 'tests/lib/libc')
-rw-r--r--tests/lib/libc/locale/Makefile3
-rw-r--r--tests/lib/libc/locale/t_mbsnrtowcs.c97
2 files changed, 99 insertions, 1 deletions
diff --git a/tests/lib/libc/locale/Makefile b/tests/lib/libc/locale/Makefile
index c47925756bc..cf20e3baf93 100644
--- a/tests/lib/libc/locale/Makefile
+++ b/tests/lib/libc/locale/Makefile
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.5 2013/02/28 21:52:02 christos Exp $
+# $NetBSD: Makefile,v 1.6 2013/05/28 16:57:56 joerg Exp $
.include <bsd.own.mk>
@@ -6,6 +6,7 @@ TESTSDIR= ${TESTSBASE}/lib/libc/locale
TESTS_C+= t_mbrtowc
TESTS_C+= t_mbstowcs
+TESTS_C+= t_mbsnrtowcs
TESTS_C+= t_mbtowc
TESTS_C+= t_wcscspn
TESTS_C+= t_wcspbrk
diff --git a/tests/lib/libc/locale/t_mbsnrtowcs.c b/tests/lib/libc/locale/t_mbsnrtowcs.c
new file mode 100644
index 00000000000..4631dc73067
--- /dev/null
+++ b/tests/lib/libc/locale/t_mbsnrtowcs.c
@@ -0,0 +1,97 @@
+/* $NetBSD: t_mbsnrtowcs.c,v 1.1 2013/05/28 16:57:56 joerg Exp $ */
+
+/*-
+ * Copyright (c) 2013 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Joerg Sonnenberger.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__RCSID("$NetBSD: t_mbsnrtowcs.c,v 1.1 2013/05/28 16:57:56 joerg Exp $");
+
+#include <locale.h>
+#include <wchar.h>
+
+#include <atf-c.h>
+
+static const struct test {
+ const char *locale;
+ const char *data;
+ size_t limit;
+ const wchar_t output1[64];
+ size_t output1_len;
+ const wchar_t output2[64];
+ size_t output2_len;
+} tests[] = {
+ { "C", "ABCD0123", 4, { 0x41, 0x42, 0x43, 0x44 }, 4,
+ { 0x30, 0x31, 0x32, 0x33, 0x0 }, 5 },
+ { "en_US.UTF-8", "ABCD0123", 4, { 0x41, 0x42, 0x43, 0x44 }, 4,
+ { 0x30, 0x31, 0x32, 0x33, 0x0 }, 5 },
+ { "en_US.UTF-8", "ABC\303\2440123", 4, { 0x41, 0x42, 0x43, }, 3,
+ { 0xe4, 0x30, 0x31, 0x32, 0x33, 0x0 }, 6 },
+};
+
+ATF_TC(mbsnrtowcs);
+ATF_TC_HEAD(mbsnrtowcs, tc)
+{
+ atf_tc_set_md_var(tc, "descr",
+ "Checks mbsnrtowc(3) with different locales");
+}
+ATF_TC_BODY(mbsnrtowcs, tc)
+{
+ size_t i;
+ const struct test *t;
+ mbstate_t state;
+ wchar_t buf[64];
+ const char *src;
+ size_t len;
+
+ for (i = 0; i < __arraycount(tests); ++i) {
+ t = &tests[i];
+ ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
+ ATF_REQUIRE(setlocale(LC_CTYPE, t->locale) != NULL);
+ memset(&state, 0, sizeof(state));
+ src = t->data;
+ len = mbsnrtowcs(buf, &src, t->limit,
+ __arraycount(buf), &state);
+ ATF_REQUIRE_EQ(src, t->data + t->limit);
+ ATF_REQUIRE_EQ(len, t->output1_len);
+ ATF_REQUIRE(wmemcmp(t->output1, buf, len) == 0);
+ len = mbsnrtowcs(buf, &src, strlen(src) + 1,
+ __arraycount(buf), &state);
+ ATF_REQUIRE_EQ(len, strlen(t->data) - t->limit);
+ ATF_REQUIRE(wmemcmp(t->output2, buf, len + 1) == 0);
+ ATF_REQUIRE_EQ(src, NULL);
+ }
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+
+ ATF_TP_ADD_TC(tp, mbsnrtowcs);
+
+ return atf_no_error();
+}