| 1 |
/* Test of conversion of wide character to multibyte character. |
| 2 |
Copyright (C) 2008-2025 Free Software Foundation, Inc. |
| 3 |
|
| 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. |
| 8 |
|
| 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. |
| 13 |
|
| 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/>. */ |
| 16 |
|
| 17 |
/* Written by Bruno Haible <[email protected]>, 2008. */ |
| 18 |
|
| 19 |
#include <config.h> |
| 20 |
|
| 21 |
#include <wchar.h> |
| 22 |
|
| 23 |
#include "signature.h" |
| 24 |
SIGNATURE_CHECK (wcrtomb, size_t, (char *, wchar_t, mbstate_t *)); |
| 25 |
|
| 26 |
#include <locale.h> |
| 27 |
#include <stdlib.h> |
| 28 |
#include <string.h> |
| 29 |
|
| 30 |
#include "macros.h" |
| 31 |
|
| 32 |
/* Check the multibyte character s[0..n-1]. */ |
| 33 |
static void |
| 34 |
check_character (const char *s, size_t n) |
| 35 |
{ |
| 36 |
wchar_t wc; |
| 37 |
char buf[64]; |
| 38 |
int iret; |
| 39 |
size_t ret; |
| 40 |
|
| 41 |
wc = (wchar_t) 0xBADFACE; |
| 42 |
iret = mbtowc (&wc, s, n); |
| 43 |
ASSERT (iret == n); |
| 44 |
|
| 45 |
ret = wcrtomb (buf, wc, NULL); |
| 46 |
ASSERT (ret == n); |
| 47 |
ASSERT (memcmp (buf, s, n) == 0); |
| 48 |
|
| 49 |
/* Test special calling convention, passing a NULL pointer. */ |
| 50 |
ret = wcrtomb (NULL, wc, NULL); |
| 51 |
ASSERT (ret == 1); |
| 52 |
} |
| 53 |
|
| 54 |
int |
| 55 |
main (int argc, char *argv[]) |
| 56 |
{ |
| 57 |
char buf[64]; |
| 58 |
size_t ret; |
| 59 |
|
| 60 |
/* configure should already have checked that the locale is supported. */ |
| 61 |
if (setlocale (LC_ALL, "") == NULL) |
| 62 |
return 1; |
| 63 |
|
| 64 |
/* Test NUL character. */ |
| 65 |
{ |
| 66 |
buf[0] = 'x'; |
| 67 |
ret = wcrtomb (buf, 0, NULL); |
| 68 |
ASSERT (ret == 1); |
| 69 |
ASSERT (buf[0] == '\0'); |
| 70 |
} |
| 71 |
|
| 72 |
/* Test single bytes. */ |
| 73 |
{ |
| 74 |
int c; |
| 75 |
|
| 76 |
for (c = 0; c < 0x100; c++) |
| 77 |
switch (c) |
| 78 |
{ |
| 79 |
case '\t': case '\v': case '\f': |
| 80 |
case ' ': case '!': case '"': case '#': case '%': |
| 81 |
case '&': case '\'': case '(': case ')': case '*': |
| 82 |
case '+': case ',': case '-': case '.': case '/': |
| 83 |
case '0': case '1': case '2': case '3': case '4': |
| 84 |
case '5': case '6': case '7': case '8': case '9': |
| 85 |
case ':': case ';': case '<': case '=': case '>': |
| 86 |
case '?': |
| 87 |
case 'A': case 'B': case 'C': case 'D': case 'E': |
| 88 |
case 'F': case 'G': case 'H': case 'I': case 'J': |
| 89 |
case 'K': case 'L': case 'M': case 'N': case 'O': |
| 90 |
case 'P': case 'Q': case 'R': case 'S': case 'T': |
| 91 |
case 'U': case 'V': case 'W': case 'X': case 'Y': |
| 92 |
case 'Z': |
| 93 |
case '[': case '\\': case ']': case '^': case '_': |
| 94 |
case 'a': case 'b': case 'c': case 'd': case 'e': |
| 95 |
case 'f': case 'g': case 'h': case 'i': case 'j': |
| 96 |
case 'k': case 'l': case 'm': case 'n': case 'o': |
| 97 |
case 'p': case 'q': case 'r': case 's': case 't': |
| 98 |
case 'u': case 'v': case 'w': case 'x': case 'y': |
| 99 |
case 'z': case '{': case '|': case '}': case '~': |
| 100 |
/* c is in the ISO C "basic character set". */ |
| 101 |
ret = wcrtomb (buf, btowc (c), NULL); |
| 102 |
ASSERT (ret == 1); |
| 103 |
ASSERT (buf[0] == (char) c); |
| 104 |
break; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
/* Test special calling convention, passing a NULL pointer. */ |
| 109 |
{ |
| 110 |
ret = wcrtomb (NULL, '\0', NULL); |
| 111 |
ASSERT (ret == 1); |
| 112 |
ret = wcrtomb (NULL, btowc ('x'), NULL); |
| 113 |
ASSERT (ret == 1); |
| 114 |
} |
| 115 |
|
| 116 |
if (argc > 1) |
| 117 |
switch (argv[1][0]) |
| 118 |
{ |
| 119 |
case '1': |
| 120 |
/* C locale; tested above. */ |
| 121 |
return test_exit_status; |
| 122 |
|
| 123 |
case '2': |
| 124 |
/* Locale encoding is ISO-8859-1 or ISO-8859-15. */ |
| 125 |
{ |
| 126 |
const char input[] = "B\374\337er"; /* "Büßer" */ |
| 127 |
|
| 128 |
check_character (input + 1, 1); |
| 129 |
check_character (input + 2, 1); |
| 130 |
} |
| 131 |
return test_exit_status; |
| 132 |
|
| 133 |
case '3': |
| 134 |
/* Locale encoding is UTF-8. */ |
| 135 |
{ |
| 136 |
const char input[] = "B\303\274\303\237er"; /* "Büßer" */ |
| 137 |
|
| 138 |
check_character (input + 1, 2); |
| 139 |
check_character (input + 3, 2); |
| 140 |
} |
| 141 |
return test_exit_status; |
| 142 |
|
| 143 |
case '4': |
| 144 |
/* Locale encoding is EUC-JP. */ |
| 145 |
{ |
| 146 |
const char input[] = "<\306\374\313\334\270\354>"; /* "<日本語>" */ |
| 147 |
|
| 148 |
check_character (input + 1, 2); |
| 149 |
check_character (input + 3, 2); |
| 150 |
check_character (input + 5, 2); |
| 151 |
} |
| 152 |
return test_exit_status; |
| 153 |
|
| 154 |
case '5': |
| 155 |
/* Locale encoding is GB18030. */ |
| 156 |
{ |
| 157 |
const char input[] = "B\250\271\201\060\211\070er"; /* "Büßer" */ |
| 158 |
|
| 159 |
check_character (input + 1, 2); |
| 160 |
check_character (input + 3, 4); |
| 161 |
} |
| 162 |
return test_exit_status; |
| 163 |
} |
| 164 |
|
| 165 |
return 1; |
| 166 |
} |