summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorBruno Haible <bruno@clisp.org>2023-04-03 14:24:48 +0200
committerBruno Haible <bruno@clisp.org>2023-04-03 14:24:48 +0200
commit52178721fa208bade898c3d14aed806e87bce642 (patch)
treed9f7026ad97ea11a2080f8f23e7597dbea7864f2 /tests
parentdb6135d5d5bc642bb8c95c384512f3f1aff68ebb (diff)
downloadgnulib-52178721fa208bade898c3d14aed806e87bce642.tar.gz
mbsnrtoc32s tests: Check behaviour in the C locale.
* tests/test-mbsnrtoc32s.c (main): Test behaviour in the C locale. Based on tests/test-mbsnrtowcs.c. * tests/test-mbsnrtoc32s-5.sh: New file, based on tests/test-mbsrtowcs5.sh. * modules/mbsnrtoc32s-tests (Files): Add it. (Depends-on): Add btoc32. (Makefile.am): Run test-mbsnrtoc32s-5.sh.
Diffstat (limited to 'tests')
-rwxr-xr-xtests/test-mbsnrtoc32s-5.sh9
-rw-r--r--tests/test-mbsnrtoc32s.c77
2 files changed, 86 insertions, 0 deletions
diff --git a/tests/test-mbsnrtoc32s-5.sh b/tests/test-mbsnrtoc32s-5.sh
new file mode 100755
index 0000000000..768d10b1e8
--- /dev/null
+++ b/tests/test-mbsnrtoc32s-5.sh
@@ -0,0 +1,9 @@
+#!/bin/sh
+
+# Test whether the POSIX locale has encoding errors.
+LC_ALL=C \
+${CHECKER} ./test-mbsnrtoc32s${EXEEXT} 5 || exit 1
+LC_ALL=POSIX \
+${CHECKER} ./test-mbsnrtoc32s${EXEEXT} 5 || exit 1
+
+exit 0
diff --git a/tests/test-mbsnrtoc32s.c b/tests/test-mbsnrtoc32s.c
index 88c11f5e47..c18b66ab06 100644
--- a/tests/test-mbsnrtoc32s.c
+++ b/tests/test-mbsnrtoc32s.c
@@ -72,6 +72,15 @@ main (int argc, char *argv[])
ASSERT (mbsinit (&state));
}
+#ifdef __ANDROID__
+ /* On Android ≥ 5.0, the default locale is the "C.UTF-8" locale, not the
+ "C" locale. Furthermore, when you attempt to set the "C" or "POSIX"
+ locale via setlocale(), what you get is a "C" locale with UTF-8 encoding,
+ that is, effectively the "C.UTF-8" locale. */
+ if (argc > 1 && strcmp (argv[1], "5") == 0 && MB_CUR_MAX > 1)
+ argv[1] = "2";
+#endif
+
if (argc > 1)
{
int unlimited;
@@ -281,6 +290,74 @@ main (int argc, char *argv[])
}
break;
+ case '5':
+ /* C or POSIX locale. */
+ {
+ char input[] = "n/a";
+ memset (&state, '\0', sizeof (mbstate_t));
+
+ src = input;
+ temp_state = state;
+ ret = mbsnrtoc32s (NULL, &src, 4, unlimited ? BUFSIZE : 1, &temp_state);
+ ASSERT (ret == 3);
+ ASSERT (src == input);
+ ASSERT (mbsinit (&state));
+
+ src = input;
+ ret = mbsnrtoc32s (buf, &src, 4, unlimited ? BUFSIZE : 1, &state);
+ ASSERT (ret == (unlimited ? 3 : 1));
+ ASSERT (src == (unlimited ? NULL : input + 1));
+ ASSERT (buf[0] == 'n');
+ if (unlimited)
+ {
+ ASSERT (buf[1] == '/');
+ ASSERT (buf[2] == 'a');
+ ASSERT (buf[3] == 0);
+ ASSERT (buf[4] == (char32_t) 0xBADFACE);
+ }
+ else
+ ASSERT (buf[1] == (char32_t) 0xBADFACE);
+ ASSERT (mbsinit (&state));
+ }
+ {
+ int c;
+ char input[2];
+
+ memset (&state, '\0', sizeof (mbstate_t));
+ for (c = 0; c < 0x100; c++)
+ if (c != 0)
+ {
+ /* We are testing all nonnull bytes. */
+ input[0] = c;
+ input[1] = '\0';
+
+ src = input;
+ ret = mbsnrtoc32s (NULL, &src, 2, unlimited ? BUFSIZE : 1, &state);
+ ASSERT (ret == 1);
+ ASSERT (src == input);
+ ASSERT (mbsinit (&state));
+
+ buf[0] = buf[1] = (char32_t) 0xBADFACE;
+ src = input;
+ ret = mbsnrtoc32s (buf, &src, 2, unlimited ? BUFSIZE : 1, &state);
+ /* POSIX:2018 says regarding mbsnrtowcs: "In the POSIX locale an
+ [EILSEQ] error cannot occur since all byte values are valid
+ characters." It is reasonable to expect mbsnrtoc32s to behave
+ in the same way. */
+ ASSERT (ret == 1);
+ ASSERT (src == (unlimited ? NULL : input + 1));
+ if (c < 0x80)
+ /* c is an ASCII character. */
+ ASSERT (buf[0] == c);
+ else
+ /* On most platforms, the bytes 0x80..0xFF map to U+0080..U+00FF.
+ But on musl libc, the bytes 0x80..0xFF map to U+DF80..U+DFFF. */
+ ASSERT (buf[0] == (btoc32 (c) == 0xDF00 + c ? btoc32 (c) : c));
+ ASSERT (mbsinit (&state));
+ }
+ }
+ break;
+
default:
return 1;
}