(elf_machine_rela): Add missing comma.
[glibc.git] / wcsmbs / tst-mbrtowc.c
blob5b6a412d479276a07a2f446f8ab4442f7817a769
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.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <assert.h>
21 #include <locale.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <wchar.h>
28 static int check_ascii (const char *locname);
30 /* Test for mbrtowc, contributed by Markus Kuhn <mkuhn@acm.org>. */
31 static int
32 utf8_test (void)
34 /* UTF-8 single byte feeding test for mbrtowc(). */
35 wchar_t wc;
36 mbstate_t s;
37 const char *locale = "de_DE.UTF-8";
39 if (!setlocale (LC_CTYPE, locale))
41 fprintf (stderr, "locale '%s' not available!\n", locale);
42 exit (1);
44 wc = 42; /* arbitrary number */
45 memset (&s, 0, sizeof (s)); /* get s into initial state */
46 assert (mbrtowc (&wc, "\xE2", 1, &s) == (size_t) - 2); /* 1st byte processed */
47 assert (mbrtowc (&wc, "\x89", 1, &s) == (size_t) - 2); /* 2nd byte processed */
48 assert (wc == 42); /* no value has not been stored into &wc yet */
49 assert (mbrtowc (&wc, "\xA0", 1, &s) == 1); /* 3nd byte processed */
50 assert (wc == 0x2260); /* E2 89 A0 = U+2260 (not equal) decoded correctly */
51 assert (mbrtowc (&wc, "", 1, &s) == 0); /* test final byte processing */
52 assert (wc == 0); /* test final byte decoding */
54 return 0;
58 int
59 main (void)
61 int result = 0;
63 /* Check mapping of ASCII range for some character sets which have
64 ASCII as a subset. For those the wide char generated must have
65 the same value. */
66 setlocale (LC_ALL, "C");
67 result |= check_ascii (setlocale (LC_ALL, NULL));
69 setlocale (LC_ALL, "de_DE.UTF-8");
70 result |= check_ascii (setlocale (LC_ALL, NULL));
71 result |= utf8_test ();
73 setlocale (LC_ALL, "ja_JP.EUC-JP");
74 result |= check_ascii (setlocale (LC_ALL, NULL));
76 return result;
80 static int
81 check_ascii (const char *locname)
83 int c;
84 int res = 0;
86 printf ("Testing locale \"%s\":\n", locname);
88 for (c = 0; c <= 127; ++c)
90 char buf[MB_CUR_MAX];
91 wchar_t wc = 0xffffffff;
92 mbstate_t s;
93 size_t n;
94 int i;
96 for (i = 0; i < MB_CUR_MAX; ++i)
97 buf[i] = c + i;
99 memset (&s, '\0', sizeof (s));
101 n = mbrtowc (&wc, buf, MB_CUR_MAX, &s);
102 if (n == (size_t) -1)
104 printf ("%s: '\\x%x': encoding error\n", locname, c);
105 ++res;
107 else if (n == (size_t) -2)
109 printf ("%s: '\\x%x': incomplete character\n", locname, c);
110 ++res;
112 else if (n == 0 && c != 0)
114 printf ("%s: '\\x%x': 0 returned\n", locname, c);
115 ++res;
117 else if (n != 0 && c == 0)
119 printf ("%s: '\\x%x': not 0 returned\n", locname, c);
120 ++res;
122 else if (c != 0 && n != 1)
124 printf ("%s: '\\x%x': not 1 returned\n", locname, c);
125 ++res;
127 else if (wc != (wchar_t) c)
129 printf ("%s: '\\x%x': wc != L'\\x%x'\n", locname, c, c);
130 ++res;
134 printf (res == 1 ? "%d error\n" : "%d errors\n", res);
136 return res != 0;