xref: /haiku/src/tests/system/libroot/posix/gnulib-test-btowc.c (revision ed24eb5ff12640d052171c6a7feba37fab8a75d1)
1 /* Test of conversion of unibyte character to wide character.
2    Copyright (C) 2008-2011 Free Software Foundation, Inc.
3 
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8 
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16 
17 /* Written by Bruno Haible <bruno@clisp.org>, 2008.  */
18 
19 #include <assert.h>
20 #include <locale.h>
21 #include <stdio.h>
22 #include <wchar.h>
23 
24 int
25 main (int argc, char *argv[])
26 {
27   int c, i;
28 
29   /* configure should already have checked that the locale is supported.  */
30   if (setlocale (LC_ALL, "") == NULL) {
31 	fprintf(stderr, "unable to set standard locale\n");
32     return 1;
33   }
34 
35   assert (btowc (EOF) == WEOF);
36 
37   for (i = '1'; i <= '2'; ++i) {
38     switch (i)
39       {
40       case '1':
41         /* Locale encoding is ISO-8859-1 or ISO-8859-15.  */
42     	printf("ISO8859-1 ...\n");
43 
44        	if (setlocale (LC_ALL, "en_US.ISO8859-1") == NULL) {
45        	  fprintf(stderr, "unable to set ISO8859-1 locale, skipping\n");
46        	  break;
47        	}
48 
49        	for (c = 0; c < 0x80; c++)
50           assert (btowc (c) == (wint_t)c);
51         for (c = 0xA0; c < 0x100; c++)
52           assert (btowc (c) != WEOF);
53         break;
54 
55       case '2':
56         /* Locale encoding is UTF-8.  */
57     	printf("UTF-8 ...\n");
58 
59        	if (setlocale (LC_ALL, "en_US.utf-8") == NULL) {
60        	  fprintf(stderr, "unable to set en_US.UTF-8 locale, skipping\n");
61        	  break;
62        	}
63 
64     	for (c = 0; c < 0x80; c++)
65           assert (btowc (c) == (wint_t)c);
66         for (c = 0x80; c < 0x100; c++)
67         	assert (btowc (c) == WEOF);
68         break;
69       }
70   }
71 
72   return 0;
73 }
74