sys_ioctl: Simplify.
[gnulib.git] / tests / test-c32stombs.c
blobebf542c9be033083ddaf0083ae9d82aaf722b23a
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. */
19 #include <config.h>
21 #include <uchar.h>
23 #include "signature.h"
24 SIGNATURE_CHECK (c32stombs, size_t,
25 (char *, const char32_t *, size_t));
27 #include <locale.h>
28 #include <stdlib.h>
29 #include <string.h>
31 #include "macros.h"
33 int
34 main (int argc, char *argv[])
36 /* configure should already have checked that the locale is supported. */
37 if (setlocale (LC_ALL, "") == NULL)
38 return 1;
40 if (argc > 1)
42 char32_t input[10];
43 size_t n;
44 #define BUFSIZE 20
45 char buf[BUFSIZE];
46 size_t ret;
49 size_t i;
50 for (i = 0; i < BUFSIZE; i++)
51 buf[i] = '_';
54 switch (argv[1][0])
56 case '1':
57 /* Locale encoding is ISO-8859-1 or ISO-8859-15. */
59 const char original[] = "B\374\337er"; /* "Büßer" */
61 ret = mbstoc32s (input, original, 10);
62 ASSERT (ret == 5);
64 for (n = 0; n < 10; n++)
66 ret = c32stombs (NULL, input, n);
67 ASSERT (ret == 5);
69 ret = c32stombs (buf, input, n);
70 ASSERT (ret == (n <= 5 ? n : 5));
71 ASSERT (memcmp (buf, original, ret) == 0);
72 if (n > 5)
73 ASSERT (buf[ret] == '\0');
74 ASSERT (buf[ret + (n > 5) + 0] == '_');
75 ASSERT (buf[ret + (n > 5) + 1] == '_');
76 ASSERT (buf[ret + (n > 5) + 2] == '_');
79 break;
81 case '2':
82 /* Locale encoding is UTF-8. */
84 const char original[] = "s\303\274\303\237\360\237\230\213!"; /* "süß😋!" */
86 ret = mbstoc32s (input, original, 10);
87 ASSERT (ret == 5);
89 for (n = 0; n < 15; n++)
91 ret = c32stombs (NULL, input, n);
92 ASSERT (ret == 10);
94 ret = c32stombs (buf, input, n);
95 ASSERT (ret == (n < 1 ? n :
96 n < 3 ? 1 :
97 n < 5 ? 3 :
98 n < 9 ? 5 :
99 n <= 10 ? n : 10));
100 ASSERT (memcmp (buf, original, ret) == 0);
101 if (n > 10)
102 ASSERT (buf[ret] == '\0');
103 ASSERT (buf[ret + (n > 10) + 0] == '_');
104 ASSERT (buf[ret + (n > 10) + 1] == '_');
105 ASSERT (buf[ret + (n > 10) + 2] == '_');
108 break;
110 case '3':
111 /* Locale encoding is EUC-JP. */
113 const char original[] = "<\306\374\313\334\270\354>"; /* "<日本語>" */
115 ret = mbstoc32s (input, original, 10);
116 ASSERT (ret == 5);
118 for (n = 0; n < 10; n++)
120 ret = c32stombs (NULL, input, n);
121 ASSERT (ret == 8);
123 ret = c32stombs (buf, input, n);
124 ASSERT (ret == (n < 1 ? n :
125 n < 3 ? 1 :
126 n < 5 ? 3 :
127 n < 7 ? 5 :
128 n <= 8 ? n : 8));
129 ASSERT (memcmp (buf, original, ret) == 0);
130 if (n > 8)
131 ASSERT (buf[ret] == '\0');
132 ASSERT (buf[ret + (n > 8) + 0] == '_');
133 ASSERT (buf[ret + (n > 8) + 1] == '_');
134 ASSERT (buf[ret + (n > 8) + 2] == '_');
137 break;
140 case '4':
141 /* Locale encoding is GB18030. */
143 const char original[] = "s\250\271\201\060\211\070\224\071\375\067!"; /* "süß😋!" */
145 ret = mbstoc32s (input, original, 10);
146 ASSERT (ret == 5);
148 for (n = 0; n < 15; n++)
150 ret = c32stombs (NULL, input, n);
151 ASSERT (ret == 12);
153 ret = c32stombs (buf, input, n);
154 ASSERT (ret == (n < 1 ? n :
155 n < 3 ? 1 :
156 n < 7 ? 3 :
157 n < 11 ? 7 :
158 n <= 12 ? n : 12));
159 ASSERT (memcmp (buf, original, ret) == 0);
160 if (n > 12)
161 ASSERT (buf[ret] == '\0');
162 ASSERT (buf[ret + (n > 12) + 0] == '_');
163 ASSERT (buf[ret + (n > 12) + 1] == '_');
164 ASSERT (buf[ret + (n > 12) + 2] == '_');
167 break;
169 default:
170 return 1;
173 return 0;
176 return 1;