|
| 1 | +/** |
| 2 | + * @file test-bom-state.cpp |
| 3 | + * @brief Checks the behaviour of iconv() with suffix //IGNORE. |
| 4 | + * @author Bruno Haible, ChenPi11 |
| 5 | + * @copyright Copyright (C) 2024 Free Software Foundation, Inc. |
| 6 | + */ |
| 7 | +/* |
| 8 | + * This file is part of the cppp-reiconv Library. |
| 9 | + * |
| 10 | + * The cppp-reiconv Library is free software; you can redistribute it |
| 11 | + * and/or modify it under the terms of the GNU Lesser General Public |
| 12 | + * License as published by the Free Software Foundation; either version 3 |
| 13 | + * of the License, or (at your option) any later version. |
| 14 | + * |
| 15 | + * The cppp-reiconv Library is distributed in the hope that it will be |
| 16 | + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | + * Lesser General Public License for more details. |
| 19 | + * |
| 20 | + * You should have received a copy of the GNU Lesser General Public |
| 21 | + * License along with the cppp-reiconv Library; see the file LICENSE. |
| 22 | + * If not, see <https://www.gnu.org/licenses/>. |
| 23 | + */ |
| 24 | + |
| 25 | +#include "iconv.h" |
| 26 | + |
| 27 | +#include <cerrno> |
| 28 | +#include <cstdio> |
| 29 | +#include <errno.h> |
| 30 | +#include <stdlib.h> |
| 31 | + |
| 32 | +static const char input1[8] = "3\xd4\xe2\x84\x83\xc3\x9f"; |
| 33 | +static const char input2[8] = "3\xe2\x84\x83\xd4\xc3\x9f"; |
| 34 | + |
| 35 | +static void test_default(::reiconv_t cd) |
| 36 | +{ |
| 37 | + char output[10]; |
| 38 | + char *inbuf; |
| 39 | + std::size_t inbytesleft; |
| 40 | + char *outbuf; |
| 41 | + std::size_t outbytesleft; |
| 42 | + std::size_t ret; |
| 43 | + |
| 44 | + inbuf = (char *)input1; |
| 45 | + inbytesleft = sizeof(input1) - 1; |
| 46 | + outbuf = output; |
| 47 | + outbytesleft = sizeof(output); |
| 48 | + ret = ::reiconv_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft); |
| 49 | + if (ret != (size_t)(-1) || errno != EILSEQ || sizeof(input1) - 1 - inbytesleft != 1) |
| 50 | + { |
| 51 | + std::abort(); |
| 52 | + } |
| 53 | + if (sizeof(output) - outbytesleft != 1 || output[0] != '3') |
| 54 | + { |
| 55 | + std::abort(); |
| 56 | + } |
| 57 | + |
| 58 | + inbuf = (char *)input2; |
| 59 | + inbytesleft = sizeof(input2) - 1; |
| 60 | + outbuf = output; |
| 61 | + outbytesleft = sizeof(output); |
| 62 | + ret = iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft); |
| 63 | + if (ret != (std::size_t)(-1) || errno != EILSEQ || sizeof(input2) - 1 - inbytesleft != 1) |
| 64 | + { |
| 65 | + std::abort(); |
| 66 | + } |
| 67 | + if (sizeof(output) - outbytesleft != 1 || output[0] != '3') |
| 68 | + { |
| 69 | + std::abort(); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +static void test_translit(::reiconv_t cd) |
| 74 | +{ |
| 75 | + char output[10]; |
| 76 | + char *inbuf; |
| 77 | + std::size_t inbytesleft; |
| 78 | + char *outbuf; |
| 79 | + std::size_t outbytesleft; |
| 80 | + std::size_t ret; |
| 81 | + |
| 82 | + inbuf = (char *)input1; |
| 83 | + inbytesleft = sizeof(input1) - 1; |
| 84 | + outbuf = output; |
| 85 | + outbytesleft = sizeof(output); |
| 86 | + ret = ::reiconv_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft); |
| 87 | + if (ret != (std::size_t)(-1) || errno != EILSEQ || sizeof(input1) - 1 - inbytesleft != 1) |
| 88 | + { |
| 89 | + std::abort(); |
| 90 | + } |
| 91 | + if (sizeof(output) - outbytesleft != 1 || output[0] != '3') |
| 92 | + { |
| 93 | + std::abort(); |
| 94 | + } |
| 95 | + |
| 96 | + inbuf = (char *)input2; |
| 97 | + inbytesleft = sizeof(input2) - 1; |
| 98 | + outbuf = output; |
| 99 | + outbytesleft = sizeof(output); |
| 100 | + ret = iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft); |
| 101 | + if (ret != (std::size_t)(-1) || errno != EILSEQ || sizeof(input2) - 1 - inbytesleft != 4) |
| 102 | + { |
| 103 | + std::abort(); |
| 104 | + } |
| 105 | + if (sizeof(output) - outbytesleft != 3 || output[0] != '3' || output[1] != '\xb0' || output[2] != 'C') |
| 106 | + { |
| 107 | + std::abort(); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +static void test_ignore(::reiconv_t cd) |
| 112 | +{ |
| 113 | + char output[10]; |
| 114 | + char *inbuf; |
| 115 | + std::size_t inbytesleft; |
| 116 | + char *outbuf; |
| 117 | + std::size_t outbytesleft; |
| 118 | + std::size_t ret; |
| 119 | + |
| 120 | + inbuf = (char *)input1; |
| 121 | + inbytesleft = sizeof(input1) - 1; |
| 122 | + outbuf = output; |
| 123 | + outbytesleft = sizeof(output) - 1; |
| 124 | + ret = ::reiconv_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft); |
| 125 | + if (ret != 1 || inbytesleft != 0) |
| 126 | + { |
| 127 | + std::abort(); |
| 128 | + } |
| 129 | + if (sizeof(output) - outbytesleft != 3 || output[0] != '3' || output[1] != '\xdf') |
| 130 | + { |
| 131 | + std::abort(); |
| 132 | + } |
| 133 | + |
| 134 | + inbuf = (char *)input2; |
| 135 | + inbytesleft = sizeof(input2) - 1; |
| 136 | + outbuf = output; |
| 137 | + outbytesleft = sizeof(output) - 1; |
| 138 | + ret = ::reiconv_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft); |
| 139 | + if (ret != 1 || inbytesleft != 0) |
| 140 | + { |
| 141 | + std::abort(); |
| 142 | + } |
| 143 | + if (sizeof(output) - outbytesleft != 3 || output[0] != '3' || output[1] != '\xdf') |
| 144 | + { |
| 145 | + std::abort(); |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +static void test_ignore_translit(::reiconv_t cd) |
| 150 | +{ |
| 151 | + char output[10]; |
| 152 | + char *inbuf; |
| 153 | + std::size_t inbytesleft; |
| 154 | + char *outbuf; |
| 155 | + std::size_t outbytesleft; |
| 156 | + std::size_t ret; |
| 157 | + |
| 158 | + inbuf = (char *)input1; |
| 159 | + inbytesleft = sizeof(input1) - 1; |
| 160 | + outbuf = output; |
| 161 | + outbytesleft = sizeof(output); |
| 162 | + ret = ::reiconv_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft); |
| 163 | + if (ret != 1 || inbytesleft != 0) |
| 164 | + { |
| 165 | + std::abort(); |
| 166 | + } |
| 167 | + if (sizeof(output) - outbytesleft != 4 || output[0] != '3' || output[1] != '\xb0' || output[2] != 'C' || |
| 168 | + output[3] != '\xdf') |
| 169 | + { |
| 170 | + std::abort(); |
| 171 | + } |
| 172 | + |
| 173 | + inbuf = (char *)input2; |
| 174 | + inbytesleft = sizeof(input2) - 1; |
| 175 | + outbuf = output; |
| 176 | + outbytesleft = sizeof(output); |
| 177 | + ret = ::reiconv_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft); |
| 178 | + if (ret != 1 || inbytesleft != 0) |
| 179 | + { |
| 180 | + std::abort(); |
| 181 | + } |
| 182 | + if (sizeof(output) - outbytesleft != 4 || output[0] != '3' || output[1] != '\xb0' || output[2] != 'C' || |
| 183 | + output[3] != '\xdf') |
| 184 | + { |
| 185 | + std::abort(); |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +int main() |
| 190 | +{ |
| 191 | + ::reiconv_t cd; |
| 192 | + |
| 193 | +#pragma region Default conversion |
| 194 | + |
| 195 | + cd = ::reiconv_open("ISO-8859-1", "UTF-8"); |
| 196 | + test_default(cd); |
| 197 | + ::reiconv_handle_close(cd); |
| 198 | + |
| 199 | + cd = ::reiconv_open_from_index(ENCODING_UTF8, ENCODING_ISO8859_1, 0); |
| 200 | + test_default(cd); |
| 201 | + ::reiconv_handle_close(cd); |
| 202 | + |
| 203 | + cd = ::reiconv_open_from_codepage(65001, 28591, 0); |
| 204 | + test_default(cd); |
| 205 | + ::reiconv_handle_close(cd); |
| 206 | + |
| 207 | +#pragma endregion |
| 208 | + |
| 209 | +#pragma region Ignore conversion |
| 210 | + |
| 211 | + cd = ::reiconv_open("ISO-8859-1//IGNORE", "UTF-8"); |
| 212 | + test_ignore(cd); |
| 213 | + ::reiconv_handle_close(cd); |
| 214 | + |
| 215 | + cd = ::reiconv_open("ISO-8859-1", "UTF-8//IGNORE"); |
| 216 | + test_ignore(cd); |
| 217 | + ::reiconv_handle_close(cd); |
| 218 | + |
| 219 | + cd = ::reiconv_open_from_index(ENCODING_UTF8, ENCODING_ISO8859_1, 1); |
| 220 | + test_ignore(cd); |
| 221 | + ::reiconv_handle_close(cd); |
| 222 | + |
| 223 | + cd = ::reiconv_open_from_codepage(65001, 28591, 1); |
| 224 | + test_ignore(cd); |
| 225 | + ::reiconv_handle_close(cd); |
| 226 | + |
| 227 | +#pragma endregion |
| 228 | + |
| 229 | +#if TEST_TRANSLIT |
| 230 | +#pragma region Translit conversion |
| 231 | + cd = ::reiconv_open("ISO-8859-1//TRANSLIT", "UTF-8"); |
| 232 | + test_translit(cd); |
| 233 | + ::reiconv_handle_close(cd); |
| 234 | + |
| 235 | + cd = ::reiconv_open("ISO-8859-1", "UTF-8//TRANSLIT"); |
| 236 | + test_translit(cd); |
| 237 | + ::reiconv_handle_close(cd); |
| 238 | + |
| 239 | + // TODO: Translit support. |
| 240 | + |
| 241 | +#pragma endregion |
| 242 | +#endif |
| 243 | + |
| 244 | +#if TEST_TRANSLIT |
| 245 | + { |
| 246 | + iconv_t cd = iconv_open("ISO-8859-1//IGNORE//TRANSLIT", "UTF-8"); |
| 247 | + test_ignore_translit(cd); |
| 248 | + iconv_close(cd); |
| 249 | + } |
| 250 | + { |
| 251 | + iconv_t cd = iconv_open("ISO-8859-1//TRANSLIT//IGNORE", "UTF-8"); |
| 252 | + test_ignore_translit(cd); |
| 253 | + iconv_close(cd); |
| 254 | + } |
| 255 | +#endif |
| 256 | + |
| 257 | + return EXIT_SUCCESS; |
| 258 | +} |
0 commit comments