]> git.ipfire.org Git - thirdparty/glibc.git/blob - wcsmbs/tst-wcrtomb.c
Update copyright dates with scripts/update-copyrights
[thirdparty/glibc.git] / wcsmbs / tst-wcrtomb.c
1 /* Copyright (C) 2000-2021 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, see
17 <https://www.gnu.org/licenses/>. */
18
19 #include <locale.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <wchar.h>
24
25
26 static int check_ascii (const char *locname);
27
28
29 static int
30 do_test (void)
31 {
32 int result = 0;
33
34 /* Check mapping of ASCII range for some character sets which have
35 ASCII as a subset. For those the wide char generated must have
36 the same value. */
37 setlocale (LC_ALL, "C");
38 result |= check_ascii (setlocale (LC_ALL, NULL));
39
40 setlocale (LC_ALL, "de_DE.UTF-8");
41 result |= check_ascii (setlocale (LC_ALL, NULL));
42
43 setlocale (LC_ALL, "ja_JP.EUC-JP");
44 result |= check_ascii (setlocale (LC_ALL, NULL));
45
46 return result;
47 }
48
49
50 static int
51 check_ascii (const char *locname)
52 {
53 wchar_t wc;
54 int res = 0;
55
56 printf ("Testing locale \"%s\":\n", locname);
57
58 for (wc = 0; wc <= 127; ++wc)
59 {
60 char buf[2 * MB_CUR_MAX];
61 mbstate_t s;
62 size_t n;
63
64 memset (buf, '\xff', sizeof (buf));
65 memset (&s, '\0', sizeof (s));
66
67 n = wcrtomb (buf, wc, &s);
68 if (n == (size_t) -1)
69 {
70 printf ("%s: '\\x%x': encoding error\n", locname, (int) wc);
71 ++res;
72 }
73 else if (n == 0)
74 {
75 printf ("%s: '\\x%x': 0 returned\n", locname, (int) wc);
76 ++res;
77 }
78 else if (n != 1)
79 {
80 printf ("%s: '\\x%x': not 1 returned\n", locname, (int) wc);
81 ++res;
82 }
83 else if (wc != (wchar_t) buf[0])
84 {
85 printf ("%s: L'\\x%x': buf[0] != '\\x%x'\n", locname, (int) wc,
86 (int) wc);
87 ++res;
88 }
89 }
90
91 printf (res == 1 ? "%d error\n" : "%d errors\n", res);
92
93 return res != 0;
94 }
95
96 #include <support/test-driver.c>