TEST(iconv, iconv_open_comparator) { // Examples from http://www.unicode.org/reports/tr22/#Charset_Alias_Matching: // "For example, the following names should match: "UTF-8", "utf8", "u.t.f-008", ..."
iconv_t c;
ASSERT_NE(INVALID_ICONV_T, c = iconv_open("UTF-8", "utf8"));
ASSERT_EQ(0, iconv_close(c));
ASSERT_NE(INVALID_ICONV_T, c = iconv_open("UTF-8", "u.t.f-008"));
ASSERT_EQ(0, iconv_close(c));
// "...but not "utf-80" or "ut8"."
ASSERT_ERRNO_FAILURE(EINVAL, INVALID_ICONV_T, iconv_open("UTF-8", "utf-80"));
ASSERT_ERRNO_FAILURE(EINVAL, INVALID_ICONV_T, iconv_open("UTF-8", "ut80"));
}
iconv_t c = iconv_open("ASCII//TRANSLIT", "UTF-8");
ASSERT_NE(INVALID_ICONV_T, c);
char* in = const_cast<char*>(utf8);
size_t in_bytes = strlen(in);
char* out = buf;
size_t out_bytes = sizeof(buf);
// Two of the input characters (5 input bytes) aren't representable as ASCII. // With "//TRANSLIT", we use a replacement character, and report the number // of replacements.
EXPECT_EQ(2U, iconv(c, &in, &in_bytes, &out, &out_bytes));
iconv_t c = iconv_open("ASCII//IGNORE", "UTF-8");
ASSERT_NE(INVALID_ICONV_T, c);
char* in = const_cast<char*>(utf8);
size_t in_bytes = strlen(in);
char* out = buf;
size_t out_bytes = sizeof(buf);
// Two of the input characters (5 input bytes) aren't representable as ASCII. // With "//IGNORE", we just skip them (but return failure).
EXPECT_ERRNO_FAILURE(EILSEQ, static_cast<size_t>(-1), iconv(c, &in, &in_bytes, &out, &out_bytes));
iconv_t c = iconv_open("ASCII", "UTF-8");
ASSERT_NE(INVALID_ICONV_T, c);
char* in = const_cast<char*>(utf8);
size_t in_bytes = strlen(in);
char* out = buf;
size_t out_bytes = sizeof(buf);
// The second input character isn't representable as ASCII, so we stop there.
EXPECT_ERRNO_FAILURE(EILSEQ, static_cast<size_t>(-1), iconv(c, &in, &in_bytes, &out, &out_bytes));
EXPECT_EQ('a', buf[0]);
EXPECT_EQ(0, buf[1]);
EXPECT_EQ(6U, in_bytes); // Two bytes for ٦, three bytes for ᄀ, and one byte for z.
EXPECT_EQ(sizeof(buf) - 1, out_bytes);
ASSERT_EQ(0, iconv_close(c));
}
TEST(iconv, iconv_malformed_sequence_EILSEQ) { constchar* utf8 = "a\xd9z"; // 0xd9 is the first byte of the two-byte U+0666 ٦. char buf[BUFSIZ] = {};
iconv_t c = iconv_open("UTF-8", "UTF-8");
ASSERT_NE(INVALID_ICONV_T, c);
char* in = const_cast<char*>(utf8);
size_t in_bytes = strlen(in);
char* out = buf;
size_t out_bytes = sizeof(buf);
// The second input byte is a malformed character, so we stop there.
EXPECT_ERRNO_FAILURE(EILSEQ, static_cast<size_t>(-1), iconv(c, &in, &in_bytes, &out, &out_bytes));
EXPECT_EQ('\xd9', *in); // *in is left pointing to the start of the invalid sequence.
++in;
--in_bytes;
EXPECT_ERRNO_SUCCESS(0, 0U, iconv(c, &in, &in_bytes, &out, &out_bytes));
TEST(iconv, iconv_incomplete_sequence_EINVAL) { constchar* utf8 = "a\xd9"; // 0xd9 is the first byte of the two-byte U+0666 ٦. char buf[BUFSIZ] = {};
iconv_t c = iconv_open("UTF-8", "UTF-8");
ASSERT_NE(INVALID_ICONV_T, c);
char* in = const_cast<char*>(utf8);
size_t in_bytes = strlen(in);
char* out = buf;
size_t out_bytes = sizeof(buf);
// The second input byte is just the start of a character, and we don't have any more bytes.
EXPECT_ERRNO_FAILURE(EINVAL, static_cast<size_t>(-1), iconv(c, &in, &in_bytes, &out, &out_bytes));
EXPECT_EQ('\xd9', *in); // *in is left pointing to the start of the incomplete sequence.
iconv_t c = iconv_open("UTF-8", "UTF-8");
ASSERT_NE(INVALID_ICONV_T, c);
char* in = const_cast<char*>(utf8);
size_t in_bytes = strlen(in);
char* out = buf;
size_t out_bytes = 1;
// We need three bytes, so one isn't enough (but we will make progress).
out_bytes = 1;
EXPECT_ERRNO_FAILURE(E2BIG, static_cast<size_t>(-1), iconv(c, &in, &in_bytes, &out, &out_bytes));
EXPECT_EQ(2U, in_bytes);
EXPECT_EQ(0U, out_bytes);
// Two bytes left, so zero isn't enough (and we can't even make progress).
out_bytes = 0;
EXPECT_ERRNO_FAILURE(E2BIG, static_cast<size_t>(-1), iconv(c, &in, &in_bytes, &out, &out_bytes));
EXPECT_EQ(2U, in_bytes);
EXPECT_EQ(0U, out_bytes);
// Two bytes left, so one isn't enough (but we will make progress).
out_bytes = 1;
EXPECT_ERRNO_FAILURE(E2BIG, static_cast<size_t>(-1), iconv(c, &in, &in_bytes, &out, &out_bytes));
EXPECT_EQ(1U, in_bytes);
EXPECT_EQ(0U, out_bytes);
// One byte left, so one byte is now enough.
out_bytes = 1;
EXPECT_ERRNO_SUCCESS(0, 0U, iconv(c, &in, &in_bytes, &out, &out_bytes));
EXPECT_EQ(0U, in_bytes);
EXPECT_EQ(0U, out_bytes);
// Check we got the bytes we were expecting. for (size_t i = 0; i < n; ++i) {
EXPECT_EQ(expected_bytes[i], buf[i]) << i << ' '<< dst_enc;
}
ASSERT_EQ(0, iconv_close(c));
// We can't round-trip if there were replacements. if (strstr(dst_enc, "ascii")) {
GTEST_LOG_(INFO) << "can't round-trip " << dst_enc << "\n"; return;
}
ASSERT_EQ(0U, replacement_count);
c = iconv_open("UTF-8", dst_enc);
ASSERT_NE(INVALID_ICONV_T, c) << dst_enc;
TEST(iconv, iconv_initial_shift_state) { // POSIX: "For state-dependent encodings, the conversion descriptor // cd is placed into its initial shift state by a call for which inbuf // is a null pointer, or for which inbuf points to a null pointer."
iconv_t c = iconv_open("utf8", "utf8"); char* in = nullptr;
size_t in_bytes = 0; wchar_t out_buf[16];
size_t out_bytes = sizeof(out_buf); char* out = reinterpret_cast<char*>(out_buf);
// Points to a null pointer...
ASSERT_ERRNO_SUCCESS(0, static_cast<size_t>(0), iconv(c, &in, &in_bytes, &out, &out_bytes));
EXPECT_EQ(sizeof(out_buf), out_bytes);
// Is a null pointer...
ASSERT_ERRNO_SUCCESS(0, static_cast<size_t>(0), iconv(c, nullptr, &in_bytes, &out, &out_bytes));
EXPECT_EQ(sizeof(out_buf), out_bytes);
// Is a null pointer and so is in_bytes. This isn't specified by POSIX, but // glibc and macOS both allow that, where Android historically didn't. // https://issuetracker.google.com/180598400
ASSERT_ERRNO_SUCCESS(0, static_cast<size_t>(0), iconv(c, nullptr, nullptr, &out, &out_bytes));
EXPECT_EQ(sizeof(out_buf), out_bytes);
EXPECT_EQ(0, iconv_close(c));
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.13 Sekunden
(vorverarbeitet am 2026-06-28)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.