1 /* Test of conversion of 32-bit wide string to string.
2 Copyright (C) 2008-2020 Free Software Foundation, Inc.
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.
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.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2008. */
23 #include "signature.h"
24 SIGNATURE_CHECK (c32snrtombs
, size_t,
25 (char *, const char32_t
**, size_t, size_t, mbstate_t *));
34 main (int argc
, char *argv
[])
36 /* configure should already have checked that the locale is supported. */
37 if (setlocale (LC_ALL
, "") == NULL
)
51 for (i
= 0; i
< BUFSIZE
; i
++)
58 /* Locale encoding is ISO-8859-1 or ISO-8859-15. */
60 const char original
[] = "B\374\337er"; /* "Büßer" */
62 ret
= mbstoc32s (input
, original
, 10);
65 for (n
= 0; n
< 10; n
++)
68 ret
= c32snrtombs (NULL
, &src
, 6, n
, NULL
);
70 ASSERT (src
== input
);
73 ret
= c32snrtombs (buf
, &src
, 6, n
, NULL
);
74 ASSERT (ret
== (n
<= 5 ? n
: 5));
75 ASSERT (src
== (n
<= 5 ? input
+ n
: NULL
));
76 ASSERT (memcmp (buf
, original
, ret
) == 0);
78 ASSERT (buf
[ret
] == '\0');
79 ASSERT (buf
[ret
+ (src
== NULL
) + 0] == '_');
80 ASSERT (buf
[ret
+ (src
== NULL
) + 1] == '_');
81 ASSERT (buf
[ret
+ (src
== NULL
) + 2] == '_');
87 /* Locale encoding is UTF-8. */
89 const char original
[] = "s\303\274\303\237\360\237\230\213!"; /* "süß😋!" */
91 ret
= mbstoc32s (input
, original
, 10);
94 for (n
= 0; n
< 15; n
++)
97 ret
= c32snrtombs (NULL
, &src
, 6, n
, NULL
);
99 ASSERT (src
== input
);
102 ret
= c32snrtombs (buf
, &src
, 6, n
, NULL
);
103 ASSERT (ret
== (n
< 1 ? n
:
108 ASSERT (src
== (n
< 1 ? input
+ n
:
112 n
<= 10 ? input
+ (n
- 5) : NULL
));
113 ASSERT (memcmp (buf
, original
, ret
) == 0);
115 ASSERT (buf
[ret
] == '\0');
116 ASSERT (buf
[ret
+ (src
== NULL
) + 0] == '_');
117 ASSERT (buf
[ret
+ (src
== NULL
) + 1] == '_');
118 ASSERT (buf
[ret
+ (src
== NULL
) + 2] == '_');
124 /* Locale encoding is EUC-JP. */
126 const char original
[] = "<\306\374\313\334\270\354>"; /* "<日本語>" */
128 ret
= mbstoc32s (input
, original
, 10);
131 for (n
= 0; n
< 10; n
++)
134 ret
= c32snrtombs (NULL
, &src
, 6, n
, NULL
);
136 ASSERT (src
== input
);
139 ret
= c32snrtombs (buf
, &src
, 6, n
, NULL
);
140 ASSERT (ret
== (n
< 1 ? n
:
145 ASSERT (src
== (n
< 1 ? input
+ n
:
149 n
<= 8 ? input
+ (n
- 3) : NULL
));
150 ASSERT (memcmp (buf
, original
, ret
) == 0);
152 ASSERT (buf
[ret
] == '\0');
153 ASSERT (buf
[ret
+ (src
== NULL
) + 0] == '_');
154 ASSERT (buf
[ret
+ (src
== NULL
) + 1] == '_');
155 ASSERT (buf
[ret
+ (src
== NULL
) + 2] == '_');
162 /* Locale encoding is GB18030. */
164 const char original
[] = "s\250\271\201\060\211\070\224\071\375\067!"; /* "süß😋!" */
166 ret
= mbstoc32s (input
, original
, 10);
169 for (n
= 0; n
< 15; n
++)
172 ret
= c32snrtombs (NULL
, &src
, 6, n
, NULL
);
174 ASSERT (src
== input
);
177 ret
= c32snrtombs (buf
, &src
, 6, n
, NULL
);
178 ASSERT (ret
== (n
< 1 ? n
:
183 ASSERT (src
== (n
< 1 ? input
+ n
:
187 n
<= 12 ? input
+ (n
- 7) : NULL
));
188 ASSERT (memcmp (buf
, original
, ret
) == 0);
190 ASSERT (buf
[ret
] == '\0');
191 ASSERT (buf
[ret
+ (src
== NULL
) + 0] == '_');
192 ASSERT (buf
[ret
+ (src
== NULL
) + 1] == '_');
193 ASSERT (buf
[ret
+ (src
== NULL
) + 2] == '_');