* posix/execl.c: Fix last argument of memcpy. Reported by Brian
[glibc.git] / wcsmbs / tst-mbrtowc.c
blobe83ac6521a04580f3ee88e03277a5c919f74974f
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
18 02111-1307 USA. */
20 #include <assert.h>
21 #include <locale.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <wchar.h>
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>. */
32 static int
33 utf8_test_1 (void)
35 wchar_t wc;
36 mbstate_t s;
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 */
48 return 0;
51 /* Test for NUL byte processing via empty string. */
52 static int
53 utf8_test_2 (void)
55 wchar_t wc;
56 mbstate_t s;
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));
82 return 0;
85 /* Test for NUL byte processing via NULL string. */
86 static int
87 utf8_test_3 (void)
89 wchar_t wc;
90 mbstate_t s;
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));
116 return 0;
119 static int
120 utf8_test (void)
122 const char *locale = "de_DE.UTF-8";
123 int error = 0;
125 if (!setlocale (LC_CTYPE, locale))
127 fprintf (stderr, "locale '%s' not available!\n", locale);
128 exit (1);
131 error |= utf8_test_1 ();
132 error |= utf8_test_2 ();
133 error |= utf8_test_3 ();
135 return error;
140 main (void)
142 int result = 0;
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
146 the same value. */
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));
157 return result;
161 static int
162 check_ascii (const char *locname)
164 int c;
165 int res = 0;
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;
173 mbstate_t s;
174 size_t n;
175 int i;
177 for (i = 0; i < MB_CUR_MAX; ++i)
178 buf[i] = c + 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);
186 ++res;
188 else if (n == (size_t) -2)
190 printf ("%s: '\\x%x': incomplete character\n", locname, c);
191 ++res;
193 else if (n == 0 && c != 0)
195 printf ("%s: '\\x%x': 0 returned\n", locname, c);
196 ++res;
198 else if (n != 0 && c == 0)
200 printf ("%s: '\\x%x': not 0 returned\n", locname, c);
201 ++res;
203 else if (c != 0 && n != 1)
205 printf ("%s: '\\x%x': not 1 returned\n", locname, c);
206 ++res;
208 else if (wc != (wchar_t) c)
210 printf ("%s: '\\x%x': wc != L'\\x%x'\n", locname, c, c);
211 ++res;
215 printf (res == 1 ? "%d error\n" : "%d errors\n", res);
217 return res != 0;