[BZ #4726]
[glibc.git] / wcsmbs / tst-btowc.c
blobf7b3ae95f3fa32b9cd7253b722d8a3c831607b3c
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.
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 <locale.h>
21 #include <stdio.h>
22 #include <wchar.h>
25 /* Currently selected locale. */
26 static const char *current_locale;
29 /* Test which should succeed. */
30 static int
31 ok_test (int c, wint_t expwc)
33 wint_t wc = btowc (c);
35 if (wc != expwc)
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;
42 return 0;
45 /* Test which should fail. */
46 static int
47 fail_test (int c)
49 wint_t wc = btowc (c);
51 if (wc != WEOF)
53 printf ("%s: btowc('%c') succeded, returned L'\\x%x' instead of WEOF\n",
54 current_locale, c, wc);
55 return 1;
58 return 0;
62 /* Test EOF handling. */
63 static int
64 eof_test (void)
66 wint_t wc = btowc (EOF);
67 if (wc != WEOF)
69 printf ("%s: btowc(EOF) returned L'\\x%x', not WEOF\n",
70 current_locale, wc);
73 return 0;
77 /* Test the btowc() function for a few locales with known character sets. */
78 int
79 main (void)
81 int result = 0;
83 current_locale = setlocale (LC_ALL, "en_US.ANSI_X3.4-1968");
84 if (current_locale == NULL)
86 puts ("cannot set locale \"en_US.ANSI_X3.4-1968\"");
87 result = 1;
89 else
91 int c;
93 for (c = 0; c < 128; ++c)
94 result |= ok_test (c, c);
96 for (c = 128; c < 256; ++c)
97 result |= fail_test (c);
99 result |= eof_test ();
102 current_locale = setlocale (LC_ALL, "de_DE.ISO-8859-1");
103 if (current_locale == NULL)
105 puts ("cannot set locale \"de_DE.ISO-8859-1\"");
106 result = 1;
108 else
110 int c;
112 for (c = 0; c < 256; ++c)
113 result |= ok_test (c, c);
115 result |= eof_test ();
118 current_locale = setlocale (LC_ALL, "de_DE.UTF-8");
119 if (current_locale == NULL)
121 puts ("cannot set locale \"de_DE.UTF-8\"");
122 result = 1;
124 else
126 int c;
128 for (c = 0; c < 128; ++c)
129 result |= ok_test (c, c);
131 for (c = 128; c < 256; ++c)
132 result |= fail_test (c);
134 result |= eof_test ();
137 current_locale = setlocale (LC_ALL, "hr_HR.ISO-8859-2");
138 if (current_locale == NULL)
140 puts ("cannot set locale \"hr_HR.ISO-8859-2\"");
141 result = 1;
143 else
145 static const wint_t upper_half[] =
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
160 int c;
162 for (c = 0; c < 161; ++c)
163 result |= ok_test (c, c);
165 for (c = 161; c < 256; ++c)
166 result |= ok_test (c, upper_half[c - 161]);
168 result |= eof_test ();
171 if (result == 0)
172 puts ("all OK");
174 return result;