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. */
28 static int check_ascii (const char *locname
);
30 /* Test for mbrtowc, contributed by Markus Kuhn <mkuhn@acm.org>. */
34 /* UTF-8 single byte feeding test for mbrtowc(). */
37 const char *locale
= "de_DE.UTF-8";
39 if (!setlocale (LC_CTYPE
, locale
))
41 fprintf (stderr
, "locale '%s' not available!\n", locale
);
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 */
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
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
));
81 check_ascii (const char *locname
)
86 printf ("Testing locale \"%s\":\n", locname
);
88 for (c
= 0; c
<= 127; ++c
)
91 wchar_t wc
= 0xffffffff;
96 for (i
= 0; i
< MB_CUR_MAX
; ++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
);
107 else if (n
== (size_t) -2)
109 printf ("%s: '\\x%x': incomplete character\n", locname
, c
);
112 else if (n
== 0 && c
!= 0)
114 printf ("%s: '\\x%x': 0 returned\n", locname
, c
);
117 else if (n
!= 0 && c
== 0)
119 printf ("%s: '\\x%x': not 0 returned\n", locname
, c
);
122 else if (c
!= 0 && n
!= 1)
124 printf ("%s: '\\x%x': not 1 returned\n", locname
, c
);
127 else if (wc
!= (wchar_t) c
)
129 printf ("%s: '\\x%x': wc != L'\\x%x'\n", locname
, c
, c
);
134 printf (res
== 1 ? "%d error\n" : "%d errors\n", res
);