Merge branch 'master' of ssh://sources.redhat.com/git/glibc
[platform/upstream/glibc.git] / wcsmbs / tst-btowc.c
1 /* Copyright (C) 2000 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 <locale.h>
21 #include <stdio.h>
22 #include <wchar.h>
23
24
25 /* Currently selected locale.  */
26 static const char *current_locale;
27
28
29 /* Test which should succeed.  */
30 static int
31 ok_test (int c, wint_t expwc)
32 {
33   wint_t wc = btowc (c);
34
35   if (wc != expwc)
36     {
37       printf ("%s: btowc('%c') failed, returned L'\\x%x' instead of L'\\x%x'\n",
38               current_locale, c, wc, expwc);
39       return 1;
40     }
41
42   return 0;
43 }
44
45 /* Test which should fail.  */
46 static int
47 fail_test (int c)
48 {
49   wint_t wc = btowc (c);
50
51   if (wc != WEOF)
52     {
53       printf ("%s: btowc('%c') succeded, returned L'\\x%x' instead of WEOF\n",
54               current_locale, c, wc);
55       return 1;
56     }
57
58   return 0;
59 }
60
61
62 /* Test EOF handling.  */
63 static int
64 eof_test (void)
65 {
66   wint_t wc = btowc (EOF);
67   if (wc != WEOF)
68     {
69       printf ("%s: btowc(EOF) returned L'\\x%x', not WEOF\n",
70               current_locale, wc);
71     }
72
73   return 0;
74 }
75
76
77 /* Test the btowc() function for a few locales with known character sets.  */
78 int
79 main (void)
80 {
81   int result = 0;
82
83   current_locale = setlocale (LC_ALL, "en_US.ANSI_X3.4-1968");
84   if (current_locale == NULL)
85     {
86       puts ("cannot set locale \"en_US.ANSI_X3.4-1968\"");
87       result = 1;
88     }
89   else
90     {
91       int c;
92
93       for (c = 0; c < 128; ++c)
94         result |= ok_test (c, c);
95
96       for (c = 128; c < 256; ++c)
97         result |= fail_test (c);
98
99       result |= eof_test ();
100     }
101
102   current_locale = setlocale (LC_ALL, "de_DE.ISO-8859-1");
103   if (current_locale == NULL)
104     {
105       puts ("cannot set locale \"de_DE.ISO-8859-1\"");
106       result = 1;
107     }
108   else
109     {
110       int c;
111
112       for (c = 0; c < 256; ++c)
113         result |= ok_test (c, c);
114
115       result |= eof_test ();
116     }
117
118   current_locale = setlocale (LC_ALL, "de_DE.UTF-8");
119   if (current_locale == NULL)
120     {
121       puts ("cannot set locale \"de_DE.UTF-8\"");
122       result = 1;
123     }
124   else
125     {
126       int c;
127
128       for (c = 0; c < 128; ++c)
129         result |= ok_test (c, c);
130
131       for (c = 128; c < 256; ++c)
132         result |= fail_test (c);
133
134       result |= eof_test ();
135     }
136
137   current_locale = setlocale (LC_ALL, "hr_HR.ISO-8859-2");
138   if (current_locale == NULL)
139     {
140       puts ("cannot set locale \"hr_HR.ISO-8859-2\"");
141       result = 1;
142     }
143   else
144     {
145       static const wint_t upper_half[] =
146       {
147         0x0104, 0x02D8, 0x0141, 0x00A4, 0x013D, 0x015A, 0x00A7, 0x00A8,
148         0x0160, 0x015E, 0x0164, 0x0179, 0x00AD, 0x017D, 0x017B, 0x00B0,
149         0x0105, 0x02DB, 0x0142, 0x00B4, 0x013E, 0x015B, 0x02C7, 0x00B8,
150         0x0161, 0x015F, 0x0165, 0x017A, 0x02DD, 0x017E, 0x017C, 0x0154,
151         0x00C1, 0x00C2, 0x0102, 0x00C4, 0x0139, 0x0106, 0x00C7, 0x010C,
152         0x00C9, 0x0118, 0x00CB, 0x011A, 0x00CD, 0x00CE, 0x010E, 0x0110,
153         0x0143, 0x0147, 0x00D3, 0x00D4, 0x0150, 0x00D6, 0x00D7, 0x0158,
154         0x016E, 0x00DA, 0x0170, 0x00DC, 0x00DD, 0x0162, 0x00DF, 0x0155,
155         0x00E1, 0x00E2, 0x0103, 0x00E4, 0x013A, 0x0107, 0x00E7, 0x010D,
156         0x00E9, 0x0119, 0x00EB, 0x011B, 0x00ED, 0x00EE, 0x010F, 0x0111,
157         0x0144, 0x0148, 0x00F3, 0x00F4, 0x0151, 0x00F6, 0x00F7, 0x0159,
158         0x016F, 0x00FA, 0x0171, 0x00FC, 0x00FD, 0x0163, 0x02D9
159       };
160       int c;
161
162       for (c = 0; c < 161; ++c)
163         result |= ok_test (c, c);
164
165       for (c = 161; c < 256; ++c)
166         result |= ok_test (c, upper_half[c - 161]);
167
168       result |= eof_test ();
169     }
170
171   if (result == 0)
172     puts ("all OK");
173
174   return result;
175 }