| 1 | /* Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
|
|---|
| 2 | This file is part of the GNU C Library.
|
|---|
| 3 | Contributed by Ulrich Drepper <drepper@redhat.com>, 2000.
|
|---|
| 4 |
|
|---|
| 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.
|
|---|
| 9 |
|
|---|
| 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.
|
|---|
| 14 |
|
|---|
| 15 | You should have received a copy of the GNU Lesser General Public
|
|---|
| 16 | License along with the GNU C Library; if not, write to the Free
|
|---|
| 17 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|---|
| 18 | 02111-1307 USA. */
|
|---|
| 19 |
|
|---|
| 20 | /* We always want assert to be fully defined. */
|
|---|
| 21 | #undef NDEBUG
|
|---|
| 22 | #include <assert.h>
|
|---|
| 23 | #include <locale.h>
|
|---|
| 24 | #include <stdio.h>
|
|---|
| 25 | #include <stdlib.h>
|
|---|
| 26 | #include <string.h>
|
|---|
| 27 | #include <wchar.h>
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 | static int check_ascii (const char *locname);
|
|---|
| 31 |
|
|---|
| 32 | /* UTF-8 single byte feeding test for mbrtowc(),
|
|---|
| 33 | contributed by Markus Kuhn <mkuhn@acm.org>. */
|
|---|
| 34 | static int
|
|---|
| 35 | utf8_test_1 (void)
|
|---|
| 36 | {
|
|---|
| 37 | wchar_t wc;
|
|---|
| 38 | mbstate_t s;
|
|---|
| 39 |
|
|---|
| 40 | wc = 42; /* arbitrary number */
|
|---|
| 41 | memset (&s, 0, sizeof (s)); /* get s into initial state */
|
|---|
| 42 | assert (mbrtowc (&wc, "\xE2", 1, &s) == (size_t) -2); /* 1st byte processed */
|
|---|
| 43 | assert (mbrtowc (&wc, "\x89", 1, &s) == (size_t) -2); /* 2nd byte processed */
|
|---|
| 44 | assert (wc == 42); /* no value has not been stored into &wc yet */
|
|---|
| 45 | assert (mbrtowc (&wc, "\xA0", 1, &s) == 1); /* 3nd byte processed */
|
|---|
| 46 | assert (wc == 0x2260); /* E2 89 A0 = U+2260 (not equal) decoded correctly */
|
|---|
| 47 | assert (mbrtowc (&wc, "", 1, &s) == 0); /* test final byte processing */
|
|---|
| 48 | assert (wc == 0); /* test final byte decoding */
|
|---|
| 49 |
|
|---|
| 50 | /* The following test is by Al Viro <aviro@redhat.com>. */
|
|---|
| 51 | const char str[] = "\xe0\xa0\x80";
|
|---|
| 52 |
|
|---|
| 53 | wc = 42; /* arbitrary number */
|
|---|
| 54 | memset (&s, 0, sizeof (s)); /* get s into initial state */
|
|---|
| 55 | assert (mbrtowc (&wc, str, 1, &s) == -2);
|
|---|
| 56 | assert (mbrtowc (&wc, str + 1, 2, &s) == 2);
|
|---|
| 57 | assert (wc == 0x800);
|
|---|
| 58 |
|
|---|
| 59 | wc = 42; /* arbitrary number */
|
|---|
| 60 | memset (&s, 0, sizeof (s)); /* get s into initial state */
|
|---|
| 61 | assert (mbrtowc (&wc, str, 3, &s) == 3);
|
|---|
| 62 | assert (wc == 0x800);
|
|---|
| 63 |
|
|---|
| 64 | return 0;
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | /* Test for NUL byte processing via empty string. */
|
|---|
| 68 | static int
|
|---|
| 69 | utf8_test_2 (void)
|
|---|
| 70 | {
|
|---|
| 71 | wchar_t wc;
|
|---|
| 72 | mbstate_t s;
|
|---|
| 73 |
|
|---|
| 74 | wc = 42; /* arbitrary number */
|
|---|
| 75 | memset (&s, 0, sizeof (s)); /* get s into initial state */
|
|---|
| 76 | assert (mbrtowc (NULL, "", 1, &s) == 0); /* valid terminator */
|
|---|
| 77 | assert (mbsinit (&s));
|
|---|
| 78 |
|
|---|
| 79 | wc = 42; /* arbitrary number */
|
|---|
| 80 | memset (&s, 0, sizeof (s)); /* get s into initial state */
|
|---|
| 81 | assert (mbrtowc (&wc, "\xE2", 1, &s) == (size_t) -2); /* 1st byte processed */
|
|---|
| 82 | assert (mbrtowc (NULL, "", 1, &s) == (size_t) -1); /* invalid terminator */
|
|---|
| 83 |
|
|---|
| 84 | wc = 42; /* arbitrary number */
|
|---|
| 85 | memset (&s, 0, sizeof (s)); /* get s into initial state */
|
|---|
| 86 | assert (mbrtowc (&wc, "\xE2", 1, &s) == (size_t) -2); /* 1st byte processed */
|
|---|
| 87 | assert (mbrtowc (&wc, "\x89", 1, &s) == (size_t) -2); /* 2nd byte processed */
|
|---|
| 88 | assert (mbrtowc (NULL, "", 1, &s) == (size_t) -1); /* invalid terminator */
|
|---|
| 89 |
|
|---|
| 90 | wc = 42; /* arbitrary number */
|
|---|
| 91 | memset (&s, 0, sizeof (s)); /* get s into initial state */
|
|---|
| 92 | assert (mbrtowc (&wc, "\xE2", 1, &s) == (size_t) -2); /* 1st byte processed */
|
|---|
| 93 | assert (mbrtowc (&wc, "\x89", 1, &s) == (size_t) -2); /* 2nd byte processed */
|
|---|
| 94 | assert (mbrtowc (&wc, "\xA0", 1, &s) == 1); /* 3nd byte processed */
|
|---|
| 95 | assert (mbrtowc (NULL, "", 1, &s) == 0); /* valid terminator */
|
|---|
| 96 | assert (mbsinit (&s));
|
|---|
| 97 |
|
|---|
| 98 | return 0;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | /* Test for NUL byte processing via NULL string. */
|
|---|
| 102 | static int
|
|---|
| 103 | utf8_test_3 (void)
|
|---|
| 104 | {
|
|---|
| 105 | wchar_t wc;
|
|---|
| 106 | mbstate_t s;
|
|---|
| 107 |
|
|---|
| 108 | wc = 42; /* arbitrary number */
|
|---|
| 109 | memset (&s, 0, sizeof (s)); /* get s into initial state */
|
|---|
| 110 | assert (mbrtowc (NULL, NULL, 0, &s) == 0); /* valid terminator */
|
|---|
| 111 | assert (mbsinit (&s));
|
|---|
| 112 |
|
|---|
| 113 | wc = 42; /* arbitrary number */
|
|---|
| 114 | memset (&s, 0, sizeof (s)); /* get s into initial state */
|
|---|
| 115 | assert (mbrtowc (&wc, "\xE2", 1, &s) == (size_t) -2); /* 1st byte processed */
|
|---|
| 116 | assert (mbrtowc (NULL, NULL, 0, &s) == (size_t) -1); /* invalid terminator */
|
|---|
| 117 |
|
|---|
| 118 | wc = 42; /* arbitrary number */
|
|---|
| 119 | memset (&s, 0, sizeof (s)); /* get s into initial state */
|
|---|
| 120 | assert (mbrtowc (&wc, "\xE2", 1, &s) == (size_t) -2); /* 1st byte processed */
|
|---|
| 121 | assert (mbrtowc (&wc, "\x89", 1, &s) == (size_t) -2); /* 2nd byte processed */
|
|---|
| 122 | assert (mbrtowc (NULL, NULL, 0, &s) == (size_t) -1); /* invalid terminator */
|
|---|
| 123 |
|
|---|
| 124 | wc = 42; /* arbitrary number */
|
|---|
| 125 | memset (&s, 0, sizeof (s)); /* get s into initial state */
|
|---|
| 126 | assert (mbrtowc (&wc, "\xE2", 1, &s) == (size_t) -2); /* 1st byte processed */
|
|---|
| 127 | assert (mbrtowc (&wc, "\x89", 1, &s) == (size_t) -2); /* 2nd byte processed */
|
|---|
| 128 | assert (mbrtowc (&wc, "\xA0", 1, &s) == 1); /* 3nd byte processed */
|
|---|
| 129 | assert (mbrtowc (NULL, NULL, 0, &s) == 0); /* valid terminator */
|
|---|
| 130 | assert (mbsinit (&s));
|
|---|
| 131 |
|
|---|
| 132 | return 0;
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | static int
|
|---|
| 136 | utf8_test (void)
|
|---|
| 137 | {
|
|---|
| 138 | const char *locale = "de_DE.UTF-8";
|
|---|
| 139 | int error = 0;
|
|---|
| 140 |
|
|---|
| 141 | if (!setlocale (LC_CTYPE, locale))
|
|---|
| 142 | {
|
|---|
| 143 | fprintf (stderr, "locale '%s' not available!\n", locale);
|
|---|
| 144 | exit (1);
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | error |= utf8_test_1 ();
|
|---|
| 148 | error |= utf8_test_2 ();
|
|---|
| 149 | error |= utf8_test_3 ();
|
|---|
| 150 |
|
|---|
| 151 | return error;
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 |
|
|---|
| 155 | int
|
|---|
| 156 | main (void)
|
|---|
| 157 | {
|
|---|
| 158 | int result = 0;
|
|---|
| 159 |
|
|---|
| 160 | /* Check mapping of ASCII range for some character sets which have
|
|---|
| 161 | ASCII as a subset. For those the wide char generated must have
|
|---|
| 162 | the same value. */
|
|---|
| 163 | setlocale (LC_ALL, "C");
|
|---|
| 164 | result |= check_ascii (setlocale (LC_ALL, NULL));
|
|---|
| 165 |
|
|---|
| 166 | setlocale (LC_ALL, "de_DE.UTF-8");
|
|---|
| 167 | result |= check_ascii (setlocale (LC_ALL, NULL));
|
|---|
| 168 | result |= utf8_test ();
|
|---|
| 169 |
|
|---|
| 170 | setlocale (LC_ALL, "ja_JP.EUC-JP");
|
|---|
| 171 | result |= check_ascii (setlocale (LC_ALL, NULL));
|
|---|
| 172 |
|
|---|
| 173 | return result;
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 |
|
|---|
| 177 | static int
|
|---|
| 178 | check_ascii (const char *locname)
|
|---|
| 179 | {
|
|---|
| 180 | int c;
|
|---|
| 181 | int res = 0;
|
|---|
| 182 |
|
|---|
| 183 | printf ("Testing locale \"%s\":\n", locname);
|
|---|
| 184 |
|
|---|
| 185 | for (c = 0; c <= 127; ++c)
|
|---|
| 186 | {
|
|---|
| 187 | char buf[MB_CUR_MAX];
|
|---|
| 188 | wchar_t wc = 0xffffffff;
|
|---|
| 189 | mbstate_t s;
|
|---|
| 190 | size_t n, i;
|
|---|
| 191 |
|
|---|
| 192 | for (i = 0; i < MB_CUR_MAX; ++i)
|
|---|
| 193 | buf[i] = c + i;
|
|---|
| 194 |
|
|---|
| 195 | memset (&s, '\0', sizeof (s));
|
|---|
| 196 |
|
|---|
| 197 | n = mbrtowc (&wc, buf, MB_CUR_MAX, &s);
|
|---|
| 198 | if (n == (size_t) -1)
|
|---|
| 199 | {
|
|---|
| 200 | printf ("%s: '\\x%x': encoding error\n", locname, c);
|
|---|
| 201 | ++res;
|
|---|
| 202 | }
|
|---|
| 203 | else if (n == (size_t) -2)
|
|---|
| 204 | {
|
|---|
| 205 | printf ("%s: '\\x%x': incomplete character\n", locname, c);
|
|---|
| 206 | ++res;
|
|---|
| 207 | }
|
|---|
| 208 | else if (n == 0 && c != 0)
|
|---|
| 209 | {
|
|---|
| 210 | printf ("%s: '\\x%x': 0 returned\n", locname, c);
|
|---|
| 211 | ++res;
|
|---|
| 212 | }
|
|---|
| 213 | else if (n != 0 && c == 0)
|
|---|
| 214 | {
|
|---|
| 215 | printf ("%s: '\\x%x': not 0 returned\n", locname, c);
|
|---|
| 216 | ++res;
|
|---|
| 217 | }
|
|---|
| 218 | else if (c != 0 && n != 1)
|
|---|
| 219 | {
|
|---|
| 220 | printf ("%s: '\\x%x': not 1 returned\n", locname, c);
|
|---|
| 221 | ++res;
|
|---|
| 222 | }
|
|---|
| 223 | else if (wc != (wchar_t) c)
|
|---|
| 224 | {
|
|---|
| 225 | printf ("%s: '\\x%x': wc != L'\\x%x'\n", locname, c, c);
|
|---|
| 226 | ++res;
|
|---|
| 227 | }
|
|---|
| 228 | }
|
|---|
| 229 |
|
|---|
| 230 | printf (res == 1 ? "%d error\n" : "%d errors\n", res);
|
|---|
| 231 |
|
|---|
| 232 | return res != 0;
|
|---|
| 233 | }
|
|---|