Debian Codegraph @

gnulib-tests/test-mbrtowc-w32utf8.c

raw
1 /* Test of conversion of multibyte character to wide character
2 on native Windows in the UTF-8 environment.
3 Copyright (C) 2024-2025 Free Software Foundation, Inc.
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program 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
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17
18 /* Written by Bruno Haible <[email protected]>, 2024. */
19
20 #include <config.h>
21
22 #include <wchar.h>
23
24 #include <errno.h>
25 #include <locale.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "macros.h"
31
32 int
33 main (void)
34 {
35 #ifdef _UCRT
36 /* Test that MB_CUR_MAX and mbrtowc() work as expected in a UTF-8 locale. */
37 mbstate_t state;
38 wchar_t wc;
39 size_t ret;
40
41 if (setlocale (LC_ALL, "") == NULL)
42 return 1;
43
44 ASSERT (MB_CUR_MAX >= 4);
45
46 {
47 char input[] = "B\303\274\303\237er"; /* "Büßer" */
48 memset (&state, '\0', sizeof (mbstate_t));
49
50 wc = (wchar_t) 0xBADFACE;
51 ret = mbrtowc (&wc, input, 1, &state);
52 ASSERT (ret == 1);
53 ASSERT (wc == 'B');
54 ASSERT (mbsinit (&state));
55 input[0] = '\0';
56
57 wc = (wchar_t) 0xBADFACE;
58 ret = mbrtowc (&wc, input + 1, 1, &state);
59 ASSERT (ret == (size_t)(-2));
60 ASSERT (wc == (wchar_t) 0xBADFACE);
61 ASSERT (!mbsinit (&state));
62 input[1] = '\0';
63
64 wc = (wchar_t) 0xBADFACE;
65 ret = mbrtowc (&wc, input + 2, 5, &state);
66 ASSERT (ret == 1);
67 ASSERT (wctob (wc) == EOF);
68 ASSERT (wc == 0x00FC);
69 ASSERT (mbsinit (&state));
70 input[2] = '\0';
71
72 /* Test support of NULL first argument. */
73 ret = mbrtowc (NULL, input + 3, 4, &state);
74 ASSERT (ret == 2);
75 ASSERT (mbsinit (&state));
76
77 wc = (wchar_t) 0xBADFACE;
78 ret = mbrtowc (&wc, input + 3, 4, &state);
79 ASSERT (ret == 2);
80 ASSERT (wctob (wc) == EOF);
81 ASSERT (wc == 0x00DF);
82 ASSERT (mbsinit (&state));
83 input[3] = '\0';
84 input[4] = '\0';
85
86 wc = (wchar_t) 0xBADFACE;
87 ret = mbrtowc (&wc, input + 5, 2, &state);
88 ASSERT (ret == 1);
89 ASSERT (wc == 'e');
90 ASSERT (mbsinit (&state));
91 input[5] = '\0';
92
93 wc = (wchar_t) 0xBADFACE;
94 ret = mbrtowc (&wc, input + 6, 1, &state);
95 ASSERT (ret == 1);
96 ASSERT (wc == 'r');
97 ASSERT (mbsinit (&state));
98
99 /* Test some invalid input. */
100 memset (&state, '\0', sizeof (mbstate_t));
101 wc = (wchar_t) 0xBADFACE;
102 ret = mbrtowc (&wc, "\377", 1, &state); /* 0xFF */
103 ASSERT (ret == (size_t)-1);
104 ASSERT (errno == EILSEQ);
105
106 memset (&state, '\0', sizeof (mbstate_t));
107 wc = (wchar_t) 0xBADFACE;
108 ret = mbrtowc (&wc, "\303\300", 2, &state); /* 0xC3 0xC0 */
109 ASSERT (ret == (size_t)-1);
110 ASSERT (errno == EILSEQ);
111
112 memset (&state, '\0', sizeof (mbstate_t));
113 wc = (wchar_t) 0xBADFACE;
114 ret = mbrtowc (&wc, "\343\300", 2, &state); /* 0xE3 0xC0 */
115 ASSERT (ret == (size_t)-1);
116 ASSERT (errno == EILSEQ);
117
118 memset (&state, '\0', sizeof (mbstate_t));
119 wc = (wchar_t) 0xBADFACE;
120 ret = mbrtowc (&wc, "\343\300\200", 3, &state); /* 0xE3 0xC0 0x80 */
121 ASSERT (ret == (size_t)-1);
122 ASSERT (errno == EILSEQ);
123
124 memset (&state, '\0', sizeof (mbstate_t));
125 wc = (wchar_t) 0xBADFACE;
126 ret = mbrtowc (&wc, "\343\200\300", 3, &state); /* 0xE3 0x80 0xC0 */
127 ASSERT (ret == (size_t)-1);
128 ASSERT (errno == EILSEQ);
129
130 memset (&state, '\0', sizeof (mbstate_t));
131 wc = (wchar_t) 0xBADFACE;
132 ret = mbrtowc (&wc, "\363\300", 2, &state); /* 0xF3 0xC0 */
133 ASSERT (ret == (size_t)-1);
134 ASSERT (errno == EILSEQ);
135
136 memset (&state, '\0', sizeof (mbstate_t));
137 wc = (wchar_t) 0xBADFACE;
138 ret = mbrtowc (&wc, "\363\300\200\200", 4, &state); /* 0xF3 0xC0 0x80 0x80 */
139 ASSERT (ret == (size_t)-1);
140 ASSERT (errno == EILSEQ);
141
142 memset (&state, '\0', sizeof (mbstate_t));
143 wc = (wchar_t) 0xBADFACE;
144 ret = mbrtowc (&wc, "\363\200\300", 3, &state); /* 0xF3 0x80 0xC0 */
145 ASSERT (ret == (size_t)-1);
146 ASSERT (errno == EILSEQ);
147
148 memset (&state, '\0', sizeof (mbstate_t));
149 wc = (wchar_t) 0xBADFACE;
150 ret = mbrtowc (&wc, "\363\200\300\200", 4, &state); /* 0xF3 0x80 0xC0 0x80 */
151 ASSERT (ret == (size_t)-1);
152 ASSERT (errno == EILSEQ);
153
154 memset (&state, '\0', sizeof (mbstate_t));
155 wc = (wchar_t) 0xBADFACE;
156 ret = mbrtowc (&wc, "\363\200\200\300", 4, &state); /* 0xF3 0x80 0x80 0xC0 */
157 ASSERT (ret == (size_t)-1);
158 ASSERT (errno == EILSEQ);
159 }
160
161 return test_exit_status;
162 #else
163 fputs ("Skipping test: not using the UCRT runtime\n", stderr);
164 return 77;
165 #endif
166 }