1 /* Copyright (C) 2000, 2001 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
28 static int check_ascii (const char *locname
);
30 /* UTF-8 single byte feeding test for mbrtowc(),
31 contributed by Markus Kuhn <mkuhn@acm.org>. */
38 wc
= 42; /* arbitrary number */
39 memset (&s
, 0, sizeof (s
)); /* get s into initial state */
40 assert (mbrtowc (&wc
, "\xE2", 1, &s
) == (size_t) -2); /* 1st byte processed */
41 assert (mbrtowc (&wc
, "\x89", 1, &s
) == (size_t) -2); /* 2nd byte processed */
42 assert (wc
== 42); /* no value has not been stored into &wc yet */
43 assert (mbrtowc (&wc
, "\xA0", 1, &s
) == 1); /* 3nd byte processed */
44 assert (wc
== 0x2260); /* E2 89 A0 = U+2260 (not equal) decoded correctly */
45 assert (mbrtowc (&wc
, "", 1, &s
) == 0); /* test final byte processing */
46 assert (wc
== 0); /* test final byte decoding */
51 /* Test for NUL byte processing via empty string. */
58 wc
= 42; /* arbitrary number */
59 memset (&s
, 0, sizeof (s
)); /* get s into initial state */
60 assert (mbrtowc (NULL
, "", 1, &s
) == 0); /* valid terminator */
61 assert (mbsinit (&s
));
63 wc
= 42; /* arbitrary number */
64 memset (&s
, 0, sizeof (s
)); /* get s into initial state */
65 assert (mbrtowc (&wc
, "\xE2", 1, &s
) == (size_t) -2); /* 1st byte processed */
66 assert (mbrtowc (NULL
, "", 1, &s
) == (size_t) -1); /* invalid terminator */
68 wc
= 42; /* arbitrary number */
69 memset (&s
, 0, sizeof (s
)); /* get s into initial state */
70 assert (mbrtowc (&wc
, "\xE2", 1, &s
) == (size_t) -2); /* 1st byte processed */
71 assert (mbrtowc (&wc
, "\x89", 1, &s
) == (size_t) -2); /* 2nd byte processed */
72 assert (mbrtowc (NULL
, "", 1, &s
) == (size_t) -1); /* invalid terminator */
74 wc
= 42; /* arbitrary number */
75 memset (&s
, 0, sizeof (s
)); /* get s into initial state */
76 assert (mbrtowc (&wc
, "\xE2", 1, &s
) == (size_t) -2); /* 1st byte processed */
77 assert (mbrtowc (&wc
, "\x89", 1, &s
) == (size_t) -2); /* 2nd byte processed */
78 assert (mbrtowc (&wc
, "\xA0", 1, &s
) == 1); /* 3nd byte processed */
79 assert (mbrtowc (NULL
, "", 1, &s
) == 0); /* valid terminator */
80 assert (mbsinit (&s
));
85 /* Test for NUL byte processing via NULL string. */
92 wc
= 42; /* arbitrary number */
93 memset (&s
, 0, sizeof (s
)); /* get s into initial state */
94 assert (mbrtowc (NULL
, NULL
, 0, &s
) == 0); /* valid terminator */
95 assert (mbsinit (&s
));
97 wc
= 42; /* arbitrary number */
98 memset (&s
, 0, sizeof (s
)); /* get s into initial state */
99 assert (mbrtowc (&wc
, "\xE2", 1, &s
) == (size_t) -2); /* 1st byte processed */
100 assert (mbrtowc (NULL
, NULL
, 0, &s
) == (size_t) -1); /* invalid terminator */
102 wc
= 42; /* arbitrary number */
103 memset (&s
, 0, sizeof (s
)); /* get s into initial state */
104 assert (mbrtowc (&wc
, "\xE2", 1, &s
) == (size_t) -2); /* 1st byte processed */
105 assert (mbrtowc (&wc
, "\x89", 1, &s
) == (size_t) -2); /* 2nd byte processed */
106 assert (mbrtowc (NULL
, NULL
, 0, &s
) == (size_t) -1); /* invalid terminator */
108 wc
= 42; /* arbitrary number */
109 memset (&s
, 0, sizeof (s
)); /* get s into initial state */
110 assert (mbrtowc (&wc
, "\xE2", 1, &s
) == (size_t) -2); /* 1st byte processed */
111 assert (mbrtowc (&wc
, "\x89", 1, &s
) == (size_t) -2); /* 2nd byte processed */
112 assert (mbrtowc (&wc
, "\xA0", 1, &s
) == 1); /* 3nd byte processed */
113 assert (mbrtowc (NULL
, NULL
, 0, &s
) == 0); /* valid terminator */
114 assert (mbsinit (&s
));
122 const char *locale
= "de_DE.UTF-8";
125 if (!setlocale (LC_CTYPE
, locale
))
127 fprintf (stderr
, "locale '%s' not available!\n", locale
);
131 error
|= utf8_test_1 ();
132 error
|= utf8_test_2 ();
133 error
|= utf8_test_3 ();
144 /* Check mapping of ASCII range for some character sets which have
145 ASCII as a subset. For those the wide char generated must have
147 setlocale (LC_ALL
, "C");
148 result
|= check_ascii (setlocale (LC_ALL
, NULL
));
150 setlocale (LC_ALL
, "de_DE.UTF-8");
151 result
|= check_ascii (setlocale (LC_ALL
, NULL
));
152 result
|= utf8_test ();
154 setlocale (LC_ALL
, "ja_JP.EUC-JP");
155 result
|= check_ascii (setlocale (LC_ALL
, NULL
));
162 check_ascii (const char *locname
)
167 printf ("Testing locale \"%s\":\n", locname
);
169 for (c
= 0; c
<= 127; ++c
)
171 char buf
[MB_CUR_MAX
];
172 wchar_t wc
= 0xffffffff;
177 for (i
= 0; i
< MB_CUR_MAX
; ++i
)
180 memset (&s
, '\0', sizeof (s
));
182 n
= mbrtowc (&wc
, buf
, MB_CUR_MAX
, &s
);
183 if (n
== (size_t) -1)
185 printf ("%s: '\\x%x': encoding error\n", locname
, c
);
188 else if (n
== (size_t) -2)
190 printf ("%s: '\\x%x': incomplete character\n", locname
, c
);
193 else if (n
== 0 && c
!= 0)
195 printf ("%s: '\\x%x': 0 returned\n", locname
, c
);
198 else if (n
!= 0 && c
== 0)
200 printf ("%s: '\\x%x': not 0 returned\n", locname
, c
);
203 else if (c
!= 0 && n
!= 1)
205 printf ("%s: '\\x%x': not 1 returned\n", locname
, c
);
208 else if (wc
!= (wchar_t) c
)
210 printf ("%s: '\\x%x': wc != L'\\x%x'\n", locname
, c
, c
);
215 printf (res
== 1 ? "%d error\n" : "%d errors\n", res
);