1 /* Test of conversion of wide string to string.
2 Copyright (C) 2008-2017 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 <http://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2008. */
23 #include "signature.h"
24 SIGNATURE_CHECK (wcsnrtombs
, size_t, (char *, wchar_t const **, size_t, size_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
= mbstowcs (input
, original
, 10);
65 for (n
= 0; n
< 10; n
++)
68 ret
= wcsnrtombs (NULL
, &src
, 6, n
, NULL
);
70 ASSERT (src
== input
);
73 ret
= wcsnrtombs (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
[] = "B\303\274\303\237er"; /* "Büßer" */
91 ret
= mbstowcs (input
, original
, 10);
94 for (n
= 0; n
< 10; n
++)
97 ret
= wcsnrtombs (NULL
, &src
, 6, n
, NULL
);
99 ASSERT (src
== input
);
102 ret
= wcsnrtombs (buf
, &src
, 6, n
, NULL
);
103 ASSERT (ret
== (n
< 1 ? n
:
107 ASSERT (src
== (n
< 1 ? input
+ n
:
110 n
<= 7 ? input
+ (n
- 2) : NULL
));
111 ASSERT (memcmp (buf
, original
, ret
) == 0);
113 ASSERT (buf
[ret
] == '\0');
114 ASSERT (buf
[ret
+ (src
== NULL
) + 0] == '_');
115 ASSERT (buf
[ret
+ (src
== NULL
) + 1] == '_');
116 ASSERT (buf
[ret
+ (src
== NULL
) + 2] == '_');
122 /* Locale encoding is EUC-JP. */
124 const char original
[] = "<\306\374\313\334\270\354>"; /* "<日本語>" */
126 ret
= mbstowcs (input
, original
, 10);
129 for (n
= 0; n
< 10; n
++)
132 ret
= wcsnrtombs (NULL
, &src
, 6, n
, NULL
);
134 ASSERT (src
== input
);
137 ret
= wcsnrtombs (buf
, &src
, 6, n
, NULL
);
138 ASSERT (ret
== (n
< 1 ? n
:
143 ASSERT (src
== (n
< 1 ? input
+ n
:
147 n
<= 8 ? input
+ (n
- 3) : NULL
));
148 ASSERT (memcmp (buf
, original
, ret
) == 0);
150 ASSERT (buf
[ret
] == '\0');
151 ASSERT (buf
[ret
+ (src
== NULL
) + 0] == '_');
152 ASSERT (buf
[ret
+ (src
== NULL
) + 1] == '_');
153 ASSERT (buf
[ret
+ (src
== NULL
) + 2] == '_');
160 /* Locale encoding is GB18030. */
162 const char original
[] = "B\250\271\201\060\211\070er"; /* "Büßer" */
164 ret
= mbstowcs (input
, original
, 10);
167 for (n
= 0; n
< 10; n
++)
170 ret
= wcsnrtombs (NULL
, &src
, 6, n
, NULL
);
172 ASSERT (src
== input
);
175 ret
= wcsnrtombs (buf
, &src
, 6, n
, NULL
);
176 ASSERT (ret
== (n
< 1 ? n
:
180 ASSERT (src
== (n
< 1 ? input
+ n
:
183 n
<= 9 ? input
+ (n
- 4) : NULL
));
184 ASSERT (memcmp (buf
, original
, ret
) == 0);
186 ASSERT (buf
[ret
] == '\0');
187 ASSERT (buf
[ret
+ (src
== NULL
) + 0] == '_');
188 ASSERT (buf
[ret
+ (src
== NULL
) + 1] == '_');
189 ASSERT (buf
[ret
+ (src
== NULL
) + 2] == '_');