Update to LGPL v2.1.
[platform/upstream/glibc.git] / wcsmbs / tst-mbrtowc.c
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.
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 #include <assert.h>
21 #include <locale.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <wchar.h>
26
27
28 static int check_ascii (const char *locname);
29
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)
34 {
35   wchar_t wc;
36   mbstate_t s;
37
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 */
47
48   return 0;
49 }
50
51 /* Test for NUL byte processing via empty string.  */
52 static int
53 utf8_test_2 (void)
54 {
55   wchar_t wc;
56   mbstate_t s;
57
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));
62
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 */
67
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 */
73
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));
81
82   return 0;
83 }
84
85 /* Test for NUL byte processing via NULL string.  */
86 static int
87 utf8_test_3 (void)
88 {
89   wchar_t wc;
90   mbstate_t s;
91
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));
96
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 */
101
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 */
107
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));
115
116   return 0;
117 }
118
119 static int
120 utf8_test (void)
121 {
122   const char *locale = "de_DE.UTF-8";
123   int error = 0;
124
125   if (!setlocale (LC_CTYPE, locale))
126     {
127       fprintf (stderr, "locale '%s' not available!\n", locale);
128       exit (1);
129     }
130
131   error |= utf8_test_1 ();
132   error |= utf8_test_2 ();
133   error |= utf8_test_3 ();
134
135   return error;
136 }
137
138
139 int
140 main (void)
141 {
142   int result = 0;
143
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));
149
150   setlocale (LC_ALL, "de_DE.UTF-8");
151   result |= check_ascii (setlocale (LC_ALL, NULL));
152   result |= utf8_test ();
153
154   setlocale (LC_ALL, "ja_JP.EUC-JP");
155   result |= check_ascii (setlocale (LC_ALL, NULL));
156
157   return result;
158 }
159
160
161 static int
162 check_ascii (const char *locname)
163 {
164   int c;
165   int res = 0;
166
167   printf ("Testing locale \"%s\":\n", locname);
168
169   for (c = 0; c <= 127; ++c)
170     {
171       char buf[MB_CUR_MAX];
172       wchar_t wc = 0xffffffff;
173       mbstate_t s;
174       size_t n;
175       int i;
176
177       for (i = 0; i < MB_CUR_MAX; ++i)
178         buf[i] = c + i;
179
180       memset (&s, '\0', sizeof (s));
181
182       n = mbrtowc (&wc, buf, MB_CUR_MAX, &s);
183       if (n == (size_t) -1)
184         {
185           printf ("%s: '\\x%x': encoding error\n", locname, c);
186           ++res;
187         }
188       else if (n == (size_t) -2)
189         {
190           printf ("%s: '\\x%x': incomplete character\n", locname, c);
191           ++res;
192         }
193       else if (n == 0 && c != 0)
194         {
195           printf ("%s: '\\x%x': 0 returned\n", locname, c);
196           ++res;
197         }
198       else if (n != 0 && c == 0)
199         {
200           printf ("%s: '\\x%x': not 0 returned\n", locname, c);
201           ++res;
202         }
203       else if (c != 0 && n != 1)
204         {
205           printf ("%s: '\\x%x': not 1 returned\n", locname, c);
206           ++res;
207         }
208       else if (wc != (wchar_t) c)
209         {
210           printf ("%s: '\\x%x': wc != L'\\x%x'\n", locname, c, c);
211           ++res;
212         }
213     }
214
215   printf (res == 1 ? "%d error\n" : "%d errors\n", res);
216
217   return res != 0;
218 }