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