1 /* Copyright (C) 2000-2019 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2000.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 /* We always want assert to be fully defined. */
29 static int check_ascii (const char *locname
);
31 /* UTF-8 single byte feeding test for mbrtowc(),
32 contributed by Markus Kuhn <mkuhn@acm.org>. */
39 wc
= 42; /* arbitrary number */
40 memset (&s
, 0, sizeof (s
)); /* get s into initial state */
41 assert (mbrtowc (&wc
, "\xE2", 1, &s
) == (size_t) -2); /* 1st byte processed */
42 assert (mbrtowc (&wc
, "\x89", 1, &s
) == (size_t) -2); /* 2nd byte processed */
43 assert (wc
== 42); /* no value has not been stored into &wc yet */
44 assert (mbrtowc (&wc
, "\xA0", 1, &s
) == 1); /* 3nd byte processed */
45 assert (wc
== 0x2260); /* E2 89 A0 = U+2260 (not equal) decoded correctly */
46 assert (mbrtowc (&wc
, "", 1, &s
) == 0); /* test final byte processing */
47 assert (wc
== 0); /* test final byte decoding */
49 /* The following test is by Al Viro <aviro@redhat.com>. */
50 const char str
[] = "\xe0\xa0\x80";
52 wc
= 42; /* arbitrary number */
53 memset (&s
, 0, sizeof (s
)); /* get s into initial state */
54 assert (mbrtowc (&wc
, str
, 1, &s
) == -2);
55 assert (mbrtowc (&wc
, str
+ 1, 2, &s
) == 2);
58 wc
= 42; /* arbitrary number */
59 memset (&s
, 0, sizeof (s
)); /* get s into initial state */
60 assert (mbrtowc (&wc
, str
, 3, &s
) == 3);
66 /* Test for NUL byte processing via empty string. */
73 wc
= 42; /* arbitrary number */
74 memset (&s
, 0, sizeof (s
)); /* get s into initial state */
75 assert (mbrtowc (NULL
, "", 1, &s
) == 0); /* valid terminator */
76 assert (mbsinit (&s
));
78 wc
= 42; /* arbitrary number */
79 memset (&s
, 0, sizeof (s
)); /* get s into initial state */
80 assert (mbrtowc (&wc
, "\xE2", 1, &s
) == (size_t) -2); /* 1st byte processed */
81 assert (mbrtowc (NULL
, "", 1, &s
) == (size_t) -1); /* invalid terminator */
83 wc
= 42; /* arbitrary number */
84 memset (&s
, 0, sizeof (s
)); /* get s into initial state */
85 assert (mbrtowc (&wc
, "\xE2", 1, &s
) == (size_t) -2); /* 1st byte processed */
86 assert (mbrtowc (&wc
, "\x89", 1, &s
) == (size_t) -2); /* 2nd byte processed */
87 assert (mbrtowc (NULL
, "", 1, &s
) == (size_t) -1); /* invalid terminator */
89 wc
= 42; /* arbitrary number */
90 memset (&s
, 0, sizeof (s
)); /* get s into initial state */
91 assert (mbrtowc (&wc
, "\xE2", 1, &s
) == (size_t) -2); /* 1st byte processed */
92 assert (mbrtowc (&wc
, "\x89", 1, &s
) == (size_t) -2); /* 2nd byte processed */
93 assert (mbrtowc (&wc
, "\xA0", 1, &s
) == 1); /* 3nd byte processed */
94 assert (mbrtowc (NULL
, "", 1, &s
) == 0); /* valid terminator */
95 assert (mbsinit (&s
));
100 /* Test for NUL byte processing via NULL string. */
107 wc
= 42; /* arbitrary number */
108 memset (&s
, 0, sizeof (s
)); /* get s into initial state */
109 assert (mbrtowc (NULL
, NULL
, 0, &s
) == 0); /* valid terminator */
110 assert (mbsinit (&s
));
112 wc
= 42; /* arbitrary number */
113 memset (&s
, 0, sizeof (s
)); /* get s into initial state */
114 assert (mbrtowc (&wc
, "\xE2", 1, &s
) == (size_t) -2); /* 1st byte processed */
115 assert (mbrtowc (NULL
, NULL
, 0, &s
) == (size_t) -1); /* invalid terminator */
117 wc
= 42; /* arbitrary number */
118 memset (&s
, 0, sizeof (s
)); /* get s into initial state */
119 assert (mbrtowc (&wc
, "\xE2", 1, &s
) == (size_t) -2); /* 1st byte processed */
120 assert (mbrtowc (&wc
, "\x89", 1, &s
) == (size_t) -2); /* 2nd byte processed */
121 assert (mbrtowc (NULL
, NULL
, 0, &s
) == (size_t) -1); /* invalid terminator */
123 wc
= 42; /* arbitrary number */
124 memset (&s
, 0, sizeof (s
)); /* get s into initial state */
125 assert (mbrtowc (&wc
, "\xE2", 1, &s
) == (size_t) -2); /* 1st byte processed */
126 assert (mbrtowc (&wc
, "\x89", 1, &s
) == (size_t) -2); /* 2nd byte processed */
127 assert (mbrtowc (&wc
, "\xA0", 1, &s
) == 1); /* 3nd byte processed */
128 assert (mbrtowc (NULL
, NULL
, 0, &s
) == 0); /* valid terminator */
129 assert (mbsinit (&s
));
137 const char *locale
= "de_DE.UTF-8";
140 if (!setlocale (LC_CTYPE
, locale
))
142 fprintf (stderr
, "locale '%s' not available!\n", locale
);
146 error
|= utf8_test_1 ();
147 error
|= utf8_test_2 ();
148 error
|= utf8_test_3 ();
159 /* Check mapping of ASCII range for some character sets which have
160 ASCII as a subset. For those the wide char generated must have
162 setlocale (LC_ALL
, "C");
163 result
|= check_ascii (setlocale (LC_ALL
, NULL
));
165 setlocale (LC_ALL
, "de_DE.UTF-8");
166 result
|= check_ascii (setlocale (LC_ALL
, NULL
));
167 result
|= utf8_test ();
169 setlocale (LC_ALL
, "ja_JP.EUC-JP");
170 result
|= check_ascii (setlocale (LC_ALL
, NULL
));
177 check_ascii (const char *locname
)
182 printf ("Testing locale \"%s\":\n", locname
);
184 for (c
= 0; c
<= 127; ++c
)
186 char buf
[MB_CUR_MAX
];
187 wchar_t wc
= 0xffffffff;
191 for (i
= 0; i
< MB_CUR_MAX
; ++i
)
194 memset (&s
, '\0', sizeof (s
));
196 n
= mbrtowc (&wc
, buf
, MB_CUR_MAX
, &s
);
197 if (n
== (size_t) -1)
199 printf ("%s: '\\x%x': encoding error\n", locname
, c
);
202 else if (n
== (size_t) -2)
204 printf ("%s: '\\x%x': incomplete character\n", locname
, c
);
207 else if (n
== 0 && c
!= 0)
209 printf ("%s: '\\x%x': 0 returned\n", locname
, c
);
212 else if (n
!= 0 && c
== 0)
214 printf ("%s: '\\x%x': not 0 returned\n", locname
, c
);
217 else if (c
!= 0 && n
!= 1)
219 printf ("%s: '\\x%x': not 1 returned\n", locname
, c
);
222 else if (wc
!= (wchar_t) c
)
224 printf ("%s: '\\x%x': wc != L'\\x%x'\n", locname
, c
, c
);
229 printf (res
== 1 ? "%d error\n" : "%d errors\n", res
);
234 #define TEST_FUNCTION do_test ()
235 #include "../test-skeleton.c"