mirror of
https://github.com/zaerl/mojibake.git
synced 2026-11-30 16:48:26 +01:00
* Fix: quadratic strlen with MJB_NUL_TERMINATED Signed-off-by: Francesco Bigiarini <francesco.bigiarini@gmail.com> * Fix: implicit declaration of mjb_nul_terminated_length Signed-off-by: Francesco Bigiarini <francesco.bigiarini@gmail.com> --------- Signed-off-by: Francesco Bigiarini <francesco.bigiarini@gmail.com>
980 lines
50 KiB
C
980 lines
50 KiB
C
/**
|
||
* The Mojibake library
|
||
*
|
||
* This file is distributed under the MIT License. See LICENSE for details.
|
||
*/
|
||
|
||
#include "test.h"
|
||
|
||
// Return the codepoint count, or SIZE_MAX when mjb_codepoint_count reports a failure.
|
||
static size_t count_codepoints(const char *buffer, size_t byte_length, mjb_encoding encoding) {
|
||
size_t count = 0;
|
||
mjb_text text = mjb_text_make(buffer, byte_length, encoding);
|
||
|
||
if(mjb_codepoint_count(&text, MJB_MALFORMED_STOP, &count, NULL) != MJB_STATUS_OK) {
|
||
return SIZE_MAX;
|
||
}
|
||
|
||
return count;
|
||
}
|
||
|
||
static void assert_encoding_conversion(const char *input, size_t input_size,
|
||
mjb_encoding input_encoding, mjb_encoding output_encoding, const char *expected,
|
||
size_t expected_size, const char *message) {
|
||
mjb_result result;
|
||
|
||
mjb_text text = mjb_text_make(input, input_size, input_encoding);
|
||
ATT_ASSERT_STATUS(mjb_convert_encoding(&text, MJB_MALFORMED_STOP,
|
||
output_encoding, &result, NULL),
|
||
MJB_STATUS_OK, message);
|
||
ATT_ASSERT(result.output_size, expected_size, message);
|
||
ATT_ASSERT(memcmp(result.output, expected, expected_size), 0, message);
|
||
|
||
if(result.owned) {
|
||
(void)mjb_result_free(&result);
|
||
}
|
||
}
|
||
|
||
// See API.md for the example.
|
||
static mjb_status api_md_examples(void) {
|
||
mjb_text input = mjb_text_utf8("caf\xC3\xA9", MJB_NUL_TERMINATED);
|
||
mjb_buffer into = mjb_buffer_make(NULL, 0, MJB_ENC_UTF_16LE);
|
||
mjb_status status;
|
||
|
||
// Get the length.
|
||
status = mjb_convert_encoding_into(&input, MJB_MALFORMED_STOP, &into, NULL);
|
||
|
||
if(status != MJB_STATUS_OK) {
|
||
return status;
|
||
}
|
||
|
||
size_t required = into.size;
|
||
char *output = (char *)malloc(required);
|
||
|
||
if(output == NULL) {
|
||
return MJB_STATUS_NO_MEMORY;
|
||
}
|
||
|
||
into.data = output;
|
||
status = mjb_convert_encoding_into(&input, MJB_MALFORMED_STOP, &into, NULL);
|
||
|
||
if(status != MJB_STATUS_OK) {
|
||
free(output);
|
||
|
||
return status;
|
||
}
|
||
|
||
MJB_TEST_COVERAGE(mjb_convert_encoding_into);
|
||
|
||
ATT_ASSERT(into.size, required, "mjb_convert_encoding_into API.md example");
|
||
ATT_ASSERT(into.size, 8, "mjb_convert_encoding_into API.md example capacity");
|
||
|
||
free(output);
|
||
|
||
return MJB_STATUS_OK;
|
||
}
|
||
|
||
static void test_validation_and_decoding(void) {
|
||
mjb_diagnostic diagnostic = { MJB_TEXT_ERROR_OUT_OF_RANGE, 99, 99, 99 };
|
||
|
||
MJB_TEST_COVERAGE(mjb_validate_encoding);
|
||
mjb_text text = mjb_text_utf8(NULL, 0);
|
||
ATT_ASSERT_STATUS(mjb_validate_encoding(&text, &diagnostic), MJB_STATUS_OK,
|
||
"NULL with zero size is well-formed");
|
||
ATT_ASSERT((unsigned int)diagnostic.error, (unsigned int)MJB_TEXT_ERROR_NONE,
|
||
"Successful validation clears the diagnostic");
|
||
text = mjb_text_make(NULL, 0, MJB_ENC_UNKNOWN);
|
||
ATT_ASSERT_STATUS(mjb_validate_encoding(&text, &diagnostic), MJB_STATUS_INVALID_ENCODING,
|
||
"Validation rejects an invalid encoding for empty input");
|
||
text = mjb_text_utf8(NULL, 1);
|
||
ATT_ASSERT_STATUS(mjb_validate_encoding(&text, &diagnostic), MJB_STATUS_INVALID_ARGUMENT,
|
||
"Validation rejects a NULL buffer with a non-zero size");
|
||
diagnostic.error = MJB_TEXT_ERROR_OUT_OF_RANGE;
|
||
ATT_ASSERT_STATUS(mjb_validate_encoding(NULL, &diagnostic), MJB_STATUS_INVALID_ARGUMENT,
|
||
"Validation rejects a NULL text");
|
||
ATT_ASSERT((unsigned int)diagnostic.error, (unsigned int)MJB_TEXT_ERROR_NONE,
|
||
"Validation of a NULL text clears the diagnostic");
|
||
ATT_ASSERT_STATUS(mjb_validate_encoding(NULL, NULL), MJB_STATUS_INVALID_ARGUMENT,
|
||
"Validation accepts a NULL diagnostic");
|
||
|
||
const char valid_utf8[] = { 'A', '\xC3', '\xA9', '\xF0', '\x9F', '\x99', '\x82' };
|
||
size_t offset = 0;
|
||
mjb_codepoint codepoint = MJB_CODEPOINT_NOT_VALID;
|
||
|
||
MJB_TEST_COVERAGE(mjb_decode_next);
|
||
text = mjb_text_utf8(valid_utf8, sizeof(valid_utf8));
|
||
ATT_ASSERT_STATUS(mjb_decode_next(&text, MJB_MALFORMED_STOP, &offset, &codepoint, &diagnostic),
|
||
MJB_STATUS_OK, "Decode next ASCII");
|
||
ATT_ASSERT((unsigned int)codepoint, 0x41u, "Decode next ASCII codepoint");
|
||
ATT_ASSERT(offset, (size_t)1, "Decode next ASCII offset");
|
||
text = mjb_text_utf8(valid_utf8, sizeof(valid_utf8));
|
||
ATT_ASSERT_STATUS(mjb_decode_next(&text, MJB_MALFORMED_STOP, &offset, &codepoint, &diagnostic),
|
||
MJB_STATUS_OK, "Decode next two-byte UTF-8");
|
||
ATT_ASSERT((unsigned int)codepoint, 0xE9u, "Decode next two-byte codepoint");
|
||
ATT_ASSERT(offset, (size_t)3, "Decode next two-byte offset");
|
||
text = mjb_text_utf8(valid_utf8, sizeof(valid_utf8));
|
||
ATT_ASSERT_STATUS(mjb_decode_next(&text, MJB_MALFORMED_STOP, &offset, &codepoint, &diagnostic),
|
||
MJB_STATUS_OK, "Decode next four-byte UTF-8");
|
||
ATT_ASSERT((unsigned int)codepoint, 0x1F642u, "Decode next four-byte codepoint");
|
||
ATT_ASSERT(offset, sizeof(valid_utf8), "Decode next four-byte offset");
|
||
text = mjb_text_utf8(valid_utf8, sizeof(valid_utf8));
|
||
ATT_ASSERT_STATUS(mjb_decode_next(&text, MJB_MALFORMED_STOP, &offset, &codepoint, &diagnostic),
|
||
MJB_STATUS_END_OF_INPUT, "Decode next distinguishes end of input");
|
||
|
||
MJB_TEST_COVERAGE(mjb_decode_previous);
|
||
text = mjb_text_utf8(valid_utf8, sizeof(valid_utf8));
|
||
ATT_ASSERT_STATUS(mjb_decode_previous(&text,
|
||
MJB_MALFORMED_STOP, &offset, &codepoint, &diagnostic),
|
||
MJB_STATUS_OK, "Decode previous four-byte UTF-8");
|
||
ATT_ASSERT((unsigned int)codepoint, 0x1F642u, "Decode previous four-byte codepoint");
|
||
ATT_ASSERT(offset, (size_t)3, "Decode previous four-byte offset");
|
||
text = mjb_text_utf8(valid_utf8, sizeof(valid_utf8));
|
||
ATT_ASSERT_STATUS(mjb_decode_previous(&text,
|
||
MJB_MALFORMED_STOP, &offset, &codepoint, &diagnostic),
|
||
MJB_STATUS_OK, "Decode previous two-byte UTF-8");
|
||
ATT_ASSERT((unsigned int)codepoint, 0xE9u, "Decode previous two-byte codepoint");
|
||
ATT_ASSERT(offset, (size_t)1, "Decode previous two-byte offset");
|
||
text = mjb_text_utf8(valid_utf8, sizeof(valid_utf8));
|
||
ATT_ASSERT_STATUS(mjb_decode_previous(&text,
|
||
MJB_MALFORMED_STOP, &offset, &codepoint, &diagnostic),
|
||
MJB_STATUS_OK, "Decode previous ASCII");
|
||
ATT_ASSERT((unsigned int)codepoint, 0x41u, "Decode previous ASCII codepoint");
|
||
ATT_ASSERT(offset, (size_t)0, "Decode previous ASCII offset");
|
||
text = mjb_text_utf8(valid_utf8, sizeof(valid_utf8));
|
||
ATT_ASSERT_STATUS(mjb_decode_previous(&text,
|
||
MJB_MALFORMED_STOP, &offset, &codepoint, &diagnostic),
|
||
MJB_STATUS_END_OF_INPUT, "Decode previous distinguishes start of input");
|
||
|
||
const char malformed_utf8[] = { 'A', '\x80', 'B' };
|
||
offset = 1;
|
||
text = mjb_text_utf8(malformed_utf8, sizeof(malformed_utf8));
|
||
ATT_ASSERT_STATUS(mjb_decode_next(&text, MJB_MALFORMED_STOP, &offset, &codepoint, &diagnostic),
|
||
MJB_STATUS_MALFORMED_INPUT, "Stop policy reports malformed UTF-8");
|
||
ATT_ASSERT((unsigned int)diagnostic.error,
|
||
(unsigned int)MJB_TEXT_ERROR_UNEXPECTED_CONTINUATION,
|
||
"Stop policy reports unexpected continuation");
|
||
ATT_ASSERT(diagnostic.byte_offset, (size_t)1, "Stop diagnostic byte offset");
|
||
ATT_ASSERT(diagnostic.byte_length, (size_t)1, "Stop diagnostic byte length");
|
||
ATT_ASSERT(diagnostic.code_unit_offset, (size_t)1, "Stop diagnostic code-unit offset");
|
||
ATT_ASSERT(offset, (size_t)2, "Stop policy advances over malformed subsequence");
|
||
|
||
offset = 1;
|
||
text = mjb_text_utf8(malformed_utf8, sizeof(malformed_utf8));
|
||
ATT_ASSERT_STATUS(mjb_decode_next(&text,
|
||
MJB_MALFORMED_REPLACE, &offset, &codepoint, &diagnostic),
|
||
MJB_STATUS_OK, "Replace policy decodes malformed UTF-8");
|
||
ATT_ASSERT((unsigned int)codepoint, (unsigned int)MJB_CODEPOINT_REPLACEMENT,
|
||
"Replace policy emits U+FFFD");
|
||
ATT_ASSERT((unsigned int)diagnostic.error,
|
||
(unsigned int)MJB_TEXT_ERROR_UNEXPECTED_CONTINUATION,
|
||
"Replace policy retains the diagnostic");
|
||
|
||
offset = 1;
|
||
text = mjb_text_utf8(malformed_utf8, sizeof(malformed_utf8));
|
||
ATT_ASSERT_STATUS(mjb_decode_next(&text, MJB_MALFORMED_SKIP, &offset, &codepoint, &diagnostic),
|
||
MJB_STATUS_OK, "Skip policy resumes after malformed UTF-8");
|
||
ATT_ASSERT((unsigned int)codepoint, (unsigned int)'B', "Skip policy returns the next codepoint");
|
||
ATT_ASSERT(offset, sizeof(malformed_utf8), "Skip policy advances through the next codepoint");
|
||
ATT_ASSERT((unsigned int)diagnostic.error,
|
||
(unsigned int)MJB_TEXT_ERROR_UNEXPECTED_CONTINUATION,
|
||
"Skip policy retains the diagnostic");
|
||
|
||
offset = sizeof(malformed_utf8);
|
||
text = mjb_text_utf8(malformed_utf8, sizeof(malformed_utf8));
|
||
ATT_ASSERT_STATUS(mjb_decode_previous(&text,
|
||
MJB_MALFORMED_STOP, &offset, &codepoint, &diagnostic),
|
||
MJB_STATUS_OK, "Decode previous after malformed UTF-8");
|
||
ATT_ASSERT((unsigned int)codepoint, (unsigned int)'B', "Decode previous trailing codepoint");
|
||
text = mjb_text_utf8(malformed_utf8, sizeof(malformed_utf8));
|
||
ATT_ASSERT_STATUS(mjb_decode_previous(&text,
|
||
MJB_MALFORMED_REPLACE, &offset, &codepoint, &diagnostic),
|
||
MJB_STATUS_OK, "Decode previous replaces malformed UTF-8");
|
||
ATT_ASSERT((unsigned int)codepoint, (unsigned int)MJB_CODEPOINT_REPLACEMENT,
|
||
"Decode previous replacement codepoint");
|
||
ATT_ASSERT(offset, (size_t)1, "Decode previous malformed offset");
|
||
|
||
const char truncated_utf8[] = { '\xE2', '\x82' };
|
||
offset = sizeof(truncated_utf8);
|
||
text = mjb_text_utf8(truncated_utf8, sizeof(truncated_utf8));
|
||
ATT_ASSERT_STATUS(mjb_decode_previous(&text,
|
||
MJB_MALFORMED_REPLACE, &offset, &codepoint, &diagnostic),
|
||
MJB_STATUS_OK, "Decode previous replaces a complete truncated subsequence");
|
||
ATT_ASSERT((unsigned int)codepoint, (unsigned int)MJB_CODEPOINT_REPLACEMENT,
|
||
"Decode previous truncated replacement codepoint");
|
||
ATT_ASSERT(offset, (size_t)0, "Decode previous consumes the complete truncated subsequence");
|
||
ATT_ASSERT(diagnostic.byte_length, sizeof(truncated_utf8),
|
||
"Decode previous reports the complete truncated subsequence");
|
||
|
||
struct malformed_case {
|
||
const char *input;
|
||
size_t size;
|
||
mjb_encoding encoding;
|
||
mjb_text_error error;
|
||
size_t error_size;
|
||
} malformed_cases[] = {
|
||
{ "\x80", 1, MJB_ENC_ASCII, MJB_TEXT_ERROR_NON_ASCII, 1 },
|
||
{ "\xFF", 1, MJB_ENC_UTF_8, MJB_TEXT_ERROR_INVALID_LEADING_BYTE, 1 },
|
||
{ "\xE2(A", 3, MJB_ENC_UTF_8, MJB_TEXT_ERROR_MISSING_CONTINUATION, 1 },
|
||
{ "\xE0\x80\x80", 3, MJB_ENC_UTF_8, MJB_TEXT_ERROR_OVERLONG_SEQUENCE, 1 },
|
||
{ "\xED\xA0\x80", 3, MJB_ENC_UTF_8, MJB_TEXT_ERROR_SURROGATE, 1 },
|
||
{ "\xF4\x90\x80\x80", 4, MJB_ENC_UTF_8, MJB_TEXT_ERROR_OUT_OF_RANGE, 1 },
|
||
{ "\xE2\x82", 2, MJB_ENC_UTF_8, MJB_TEXT_ERROR_TRUNCATED_SEQUENCE, 2 },
|
||
{ "A", 1, MJB_ENC_UTF_16LE, MJB_TEXT_ERROR_TRUNCATED_CODE_UNIT, 1 },
|
||
{ "\x00\xD8", 2, MJB_ENC_UTF_16LE, MJB_TEXT_ERROR_UNPAIRED_SURROGATE, 2 },
|
||
{ "\x00\xD8\x00\x00", 4, MJB_ENC_UTF_32LE, MJB_TEXT_ERROR_SURROGATE, 4 },
|
||
{ "\x00\x00\x11\x00", 4, MJB_ENC_UTF_32LE, MJB_TEXT_ERROR_OUT_OF_RANGE, 4 },
|
||
};
|
||
|
||
for(size_t i = 0; i < sizeof(malformed_cases) / sizeof(malformed_cases[0]); ++i) {
|
||
diagnostic.error = MJB_TEXT_ERROR_NONE;
|
||
text = mjb_text_make(malformed_cases[i].input, malformed_cases[i].size,
|
||
malformed_cases[i].encoding);
|
||
ATT_ASSERT_STATUS(mjb_validate_encoding(&text, &diagnostic),
|
||
MJB_STATUS_MALFORMED_INPUT, "Validation rejects malformed encoding case");
|
||
ATT_ASSERT((unsigned int)diagnostic.error, (unsigned int)malformed_cases[i].error,
|
||
"Validation reports precise malformed encoding kind");
|
||
ATT_ASSERT(diagnostic.byte_length, malformed_cases[i].error_size,
|
||
"Validation reports malformed subsequence size");
|
||
}
|
||
|
||
const char generic_utf16[] = { '\xFF', '\xFE', 'A', '\0' };
|
||
offset = 0;
|
||
text = mjb_text_make(generic_utf16, sizeof(generic_utf16), MJB_ENC_UTF_16);
|
||
ATT_ASSERT_STATUS(mjb_decode_next(&text, MJB_MALFORMED_STOP, &offset, &codepoint, &diagnostic),
|
||
MJB_STATUS_OK, "Generic UTF-16 decoder consumes BOM");
|
||
ATT_ASSERT((unsigned int)codepoint, (unsigned int)'A', "Generic UTF-16 decoded codepoint");
|
||
ATT_ASSERT(offset, sizeof(generic_utf16), "Generic UTF-16 decoded offset");
|
||
offset = 1;
|
||
text = mjb_text_make(generic_utf16, sizeof(generic_utf16), MJB_ENC_UTF_16);
|
||
ATT_ASSERT_STATUS(mjb_decode_next(&text, MJB_MALFORMED_STOP, &offset, &codepoint, &diagnostic),
|
||
MJB_STATUS_INVALID_ARGUMENT, "Generic UTF-16 rejects an offset inside its BOM");
|
||
|
||
size_t count = 99;
|
||
mjb_text malformed_text = mjb_text_utf8(malformed_utf8, sizeof(malformed_utf8));
|
||
ATT_ASSERT_STATUS(mjb_codepoint_count(&malformed_text, MJB_MALFORMED_STOP, &count, &diagnostic),
|
||
MJB_STATUS_MALFORMED_INPUT, "Codepoint count stop policy");
|
||
ATT_ASSERT(count, (size_t)0, "Codepoint count is reset after malformed input");
|
||
ATT_ASSERT_STATUS(mjb_codepoint_count(&malformed_text, MJB_MALFORMED_REPLACE, &count,
|
||
&diagnostic),
|
||
MJB_STATUS_OK, "Codepoint count replace policy");
|
||
ATT_ASSERT(count, (size_t)3, "Replacement counts as a codepoint");
|
||
ATT_ASSERT_STATUS(mjb_codepoint_count(&malformed_text, MJB_MALFORMED_SKIP, &count, &diagnostic),
|
||
MJB_STATUS_OK, "Codepoint count skip policy");
|
||
ATT_ASSERT(count, (size_t)2, "Skipped malformed input is not counted");
|
||
|
||
mjb_result converted;
|
||
text = mjb_text_utf8(malformed_utf8, sizeof(malformed_utf8));
|
||
ATT_ASSERT_STATUS(mjb_convert_encoding(&text,
|
||
MJB_MALFORMED_STOP, MJB_ENC_UTF_8, &converted, &diagnostic),
|
||
MJB_STATUS_MALFORMED_INPUT, "Same-encoding conversion validates in stop mode");
|
||
text = mjb_text_utf8(malformed_utf8, sizeof(malformed_utf8));
|
||
ATT_ASSERT_STATUS(mjb_convert_encoding(&text,
|
||
MJB_MALFORMED_REPLACE, MJB_ENC_UTF_8, &converted, &diagnostic),
|
||
MJB_STATUS_OK, "Same-encoding conversion replaces malformed input");
|
||
ATT_ASSERT(converted.output_size, (size_t)5, "Replacement conversion output size");
|
||
ATT_ASSERT(memcmp(converted.output, "A\xEF\xBF\xBD"
|
||
"B",
|
||
converted.output_size),
|
||
0, "Replacement conversion output");
|
||
(void)mjb_result_free(&converted);
|
||
|
||
text = mjb_text_utf8(malformed_utf8, sizeof(malformed_utf8));
|
||
ATT_ASSERT_STATUS(mjb_convert_encoding(&text,
|
||
MJB_MALFORMED_SKIP, MJB_ENC_UTF_8, &converted, &diagnostic),
|
||
MJB_STATUS_OK, "Same-encoding conversion skips malformed input");
|
||
ATT_ASSERT(converted.output_size, (size_t)2, "Skip conversion output size");
|
||
ATT_ASSERT(memcmp(converted.output, "AB", converted.output_size), 0,
|
||
"Skip conversion output");
|
||
(void)mjb_result_free(&converted);
|
||
}
|
||
|
||
ATT_TEST(encoding) {
|
||
{
|
||
size_t offset = 0;
|
||
mjb_codepoint codepoint = 0;
|
||
|
||
ATT_ASSERT_STATUS(mjb_decode_next(NULL, MJB_MALFORMED_STOP, &offset, &codepoint, NULL),
|
||
MJB_STATUS_INVALID_ARGUMENT, "Decode next rejects NULL text");
|
||
ATT_ASSERT_STATUS(
|
||
mjb_decode_previous(NULL, MJB_MALFORMED_STOP, &offset, &codepoint, NULL),
|
||
MJB_STATUS_INVALID_ARGUMENT, "Decode previous rejects NULL text");
|
||
|
||
// A NUL-terminated text would be rescanned on every call. The constructors resolve the
|
||
// length, so build the texts directly.
|
||
mjb_text text = { "A\xC3\xA9", MJB_NUL_TERMINATED, MJB_ENC_UTF_8 };
|
||
ATT_ASSERT_STATUS(mjb_decode_next(&text, MJB_MALFORMED_STOP, &offset, &codepoint, NULL),
|
||
MJB_STATUS_INVALID_ARGUMENT, "Decode next rejects a NUL-terminated text");
|
||
ATT_ASSERT(offset, (size_t)0, "Decode next leaves the offset of a NUL-terminated text");
|
||
ATT_ASSERT((unsigned int)codepoint, (unsigned int)MJB_CODEPOINT_NOT_VALID,
|
||
"Decode next invalidates the codepoint of a NUL-terminated text");
|
||
|
||
mjb_text utf16 = { "A\0\0\0", MJB_NUL_TERMINATED, MJB_ENC_UTF_16LE };
|
||
ATT_ASSERT_STATUS(mjb_decode_next(&utf16, MJB_MALFORMED_REPLACE, &offset, &codepoint,
|
||
NULL),
|
||
MJB_STATUS_INVALID_ARGUMENT, "Decode next rejects a NUL-terminated UTF-16 text");
|
||
|
||
offset = 1;
|
||
codepoint = 0;
|
||
ATT_ASSERT_STATUS(
|
||
mjb_decode_previous(&text, MJB_MALFORMED_STOP, &offset, &codepoint, NULL),
|
||
MJB_STATUS_INVALID_ARGUMENT, "Decode previous rejects a NUL-terminated text");
|
||
ATT_ASSERT(offset, (size_t)1, "Decode previous leaves the offset of a NUL-terminated text");
|
||
ATT_ASSERT((unsigned int)codepoint, (unsigned int)MJB_CODEPOINT_NOT_VALID,
|
||
"Decode previous invalidates the codepoint of a NUL-terminated text");
|
||
}
|
||
|
||
{
|
||
size_t byte_length = 0;
|
||
|
||
MJB_TEST_COVERAGE(mjb_nul_terminated_length);
|
||
ATT_ASSERT_STATUS(mjb_nul_terminated_length("A\xC3\xA9", MJB_ENC_UTF_8, &byte_length),
|
||
MJB_STATUS_OK, "NUL-terminated length of UTF-8");
|
||
ATT_ASSERT(byte_length, (size_t)3, "NUL-terminated length of UTF-8 bytes");
|
||
ATT_ASSERT_STATUS(mjb_nul_terminated_length("", MJB_ENC_ASCII, &byte_length),
|
||
MJB_STATUS_OK, "NUL-terminated length of an empty string");
|
||
ATT_ASSERT(byte_length, (size_t)0, "NUL-terminated length of an empty string bytes");
|
||
// Zero bytes inside a code unit do not terminate the string.
|
||
ATT_ASSERT_STATUS(mjb_nul_terminated_length("H\0i\0\0\0", MJB_ENC_UTF_16LE, &byte_length),
|
||
MJB_STATUS_OK, "NUL-terminated length of UTF-16LE");
|
||
ATT_ASSERT(byte_length, (size_t)4, "NUL-terminated length of UTF-16LE bytes");
|
||
ATT_ASSERT_STATUS(mjb_nul_terminated_length("\xFE\xFF\0H\0\0", MJB_ENC_UTF_16,
|
||
&byte_length),
|
||
MJB_STATUS_OK, "NUL-terminated length of UTF-16 with a byte order mark");
|
||
ATT_ASSERT(byte_length, (size_t)4, "NUL-terminated length of UTF-16 bytes");
|
||
ATT_ASSERT_STATUS(mjb_nul_terminated_length("H\0\0\0\0\0\0\0", MJB_ENC_UTF_32LE,
|
||
&byte_length),
|
||
MJB_STATUS_OK, "NUL-terminated length of UTF-32LE");
|
||
ATT_ASSERT(byte_length, (size_t)4, "NUL-terminated length of UTF-32LE bytes");
|
||
|
||
ATT_ASSERT_STATUS(mjb_nul_terminated_length(NULL, MJB_ENC_UTF_8, &byte_length),
|
||
MJB_STATUS_INVALID_ARGUMENT, "NUL-terminated length rejects a NULL buffer");
|
||
ATT_ASSERT(byte_length, (size_t)MJB_NUL_TERMINATED,
|
||
"NUL-terminated length of a NULL buffer keeps the sentinel");
|
||
byte_length = 0;
|
||
ATT_ASSERT_STATUS(mjb_nul_terminated_length("A", MJB_ENC_UNKNOWN, &byte_length),
|
||
MJB_STATUS_INVALID_ENCODING, "NUL-terminated length rejects an invalid encoding");
|
||
ATT_ASSERT(byte_length, (size_t)MJB_NUL_TERMINATED,
|
||
"NUL-terminated length of an invalid encoding keeps the sentinel");
|
||
ATT_ASSERT_STATUS(mjb_nul_terminated_length("A", MJB_ENC_UTF_8, NULL),
|
||
MJB_STATUS_INVALID_ARGUMENT, "NUL-terminated length rejects a NULL length");
|
||
|
||
// The constructors resolve the length once.
|
||
mjb_text text = mjb_text_utf8("A\xC3\xA9", MJB_NUL_TERMINATED);
|
||
ATT_ASSERT(text.byte_length, (size_t)3, "Text make resolves a UTF-8 length");
|
||
text = mjb_text_utf16le("H\0i\0\0\0", MJB_NUL_TERMINATED);
|
||
ATT_ASSERT(text.byte_length, (size_t)4, "Text make resolves a UTF-16LE length");
|
||
text = mjb_text_utf32be("\0\0\0H\0\0\0\0", MJB_NUL_TERMINATED);
|
||
ATT_ASSERT(text.byte_length, (size_t)4, "Text make resolves a UTF-32BE length");
|
||
text = mjb_text_make("A", 1, MJB_ENC_UTF_8);
|
||
ATT_ASSERT(text.byte_length, (size_t)1, "Text make keeps an explicit length");
|
||
text = mjb_text_make(NULL, MJB_NUL_TERMINATED, MJB_ENC_UTF_8);
|
||
ATT_ASSERT(text.byte_length, (size_t)MJB_NUL_TERMINATED,
|
||
"Text make keeps the sentinel of a NULL buffer");
|
||
ATT_ASSERT_STATUS(mjb_validate_encoding(&text, NULL), MJB_STATUS_INVALID_ARGUMENT,
|
||
"A NULL NUL-terminated text is reported by the function taking it");
|
||
text = mjb_text_make("A", MJB_NUL_TERMINATED, MJB_ENC_UNKNOWN);
|
||
ATT_ASSERT(text.byte_length, (size_t)MJB_NUL_TERMINATED,
|
||
"Text make keeps the sentinel of an invalid encoding");
|
||
|
||
// A resolved text can be iterated.
|
||
text = mjb_text_utf8("A\xC3\xA9", MJB_NUL_TERMINATED);
|
||
size_t offset = 0;
|
||
size_t count = 0;
|
||
mjb_codepoint codepoint;
|
||
|
||
while(mjb_decode_next(&text, MJB_MALFORMED_STOP, &offset, &codepoint, NULL) ==
|
||
MJB_STATUS_OK) {
|
||
++count;
|
||
}
|
||
|
||
ATT_ASSERT(count, (size_t)2, "Decode next iterates a resolved NUL-terminated text");
|
||
}
|
||
|
||
test_validation_and_decoding();
|
||
|
||
ATT_ASSERT((unsigned int)mjb_detect_encoding(0, 10), (unsigned int)MJB_ENC_UNKNOWN,
|
||
"Void unknown string");
|
||
ATT_ASSERT((unsigned int)mjb_detect_encoding("", 0), (unsigned int)MJB_ENC_UNKNOWN,
|
||
"Void unknown length");
|
||
|
||
ATT_ASSERT((unsigned int)mjb_detect_encoding(0, 0), (unsigned int)MJB_ENC_UNKNOWN,
|
||
"Void unknown string and length");
|
||
|
||
const char *test1 = "The quick brown fox jumps over the lazy dog";
|
||
|
||
ATT_ASSERT((unsigned int)mjb_detect_encoding(test1, 43),
|
||
(unsigned int)(MJB_ENC_ASCII | MJB_ENC_UTF_8), "Plain ASCII (and UTF-8)");
|
||
const char *test2 = "\xEF\xBB\xBFThe quick brown fox jumps over the lazy dog";
|
||
|
||
ATT_ASSERT((unsigned int)mjb_detect_encoding(test2, 43 + 3), (unsigned int)MJB_ENC_UTF_8,
|
||
"UTF-8 BOM");
|
||
const char *test3 = "\xFE\xFFThe quick brown fox jumps over the lazy dog";
|
||
|
||
ATT_ASSERT((unsigned int)mjb_detect_encoding(test3, 43 + 2),
|
||
(unsigned int)(MJB_ENC_UTF_16 | MJB_ENC_UTF_16BE), "UTF-16-BE BOM");
|
||
|
||
const char *test4 = "\xFF\xFEThe quick brown fox jumps over the lazy dog";
|
||
ATT_ASSERT((unsigned int)mjb_detect_encoding(test4, 43 + 2),
|
||
(unsigned int)(MJB_ENC_UTF_16 | MJB_ENC_UTF_16LE), "UTF-16-LE BOM");
|
||
|
||
const char *test5 = "\x00\x00\xFE\xFFThe quick brown fox jumps over the lazy dog";
|
||
ATT_ASSERT((unsigned int)mjb_detect_encoding(test5, 43 + 4),
|
||
(unsigned int)MJB_ENC_UTF_32 | MJB_ENC_UTF_32BE, "UTF-32-BE BOM");
|
||
|
||
const char *test6 = "\xFF\xFE\x00\x00The quick brown fox jumps over the lazy dog";
|
||
ATT_ASSERT((unsigned int)mjb_detect_encoding(test6, 43 + 4),
|
||
(unsigned int)(MJB_ENC_UTF_32 | MJB_ENC_UTF_32LE | MJB_ENC_UTF_16 | MJB_ENC_UTF_16LE),
|
||
"UTF-32-LE BOM");
|
||
|
||
ATT_ASSERT(mjb_is_ascii("", 0), true, "Empty ASCII string");
|
||
ATT_ASSERT(mjb_is_ascii(0, 0), true, "NULL with zero size is empty ASCII");
|
||
|
||
const char *test10 = "The quick brown fox jumps over the lazy dog";
|
||
ATT_ASSERT(mjb_is_ascii(test10, 43), true, "Valid string and length");
|
||
|
||
const unsigned char ascii_null_invalid[] = { 'A', '\0', 0x80 };
|
||
ATT_ASSERT(mjb_is_ascii((const char *)ascii_null_invalid, sizeof(ascii_null_invalid)),
|
||
false, "ASCII rejects non-ASCII after embedded NULL");
|
||
ATT_ASSERT(mjb_is_ascii((const char *)ascii_null_invalid, MJB_NUL_TERMINATED), true,
|
||
"NUL-terminated ASCII excludes bytes after the terminator");
|
||
|
||
// \xF0\x9F\x99\x82 = 🙂
|
||
const char *test11 = "\xF0\x9F\x99\x82";
|
||
ATT_ASSERT(mjb_is_ascii(test11, 5), false, "String with emoji");
|
||
|
||
const char *test12 = "\x80";
|
||
ATT_ASSERT(mjb_is_ascii(test12, 2), false, "Lone continuation byte");
|
||
|
||
const char *test13 = "\xC0";
|
||
ATT_ASSERT(mjb_is_ascii(test13, 2), false, "Lone first 2-bytes sequence");
|
||
|
||
const char *test14 = "\xE0";
|
||
ATT_ASSERT(mjb_is_ascii(test14, 2), false, "Lone first 3-bytes sequence");
|
||
|
||
const char *test15 = "\xF0";
|
||
ATT_ASSERT(mjb_is_ascii(test15, 2), false, "Lone first 4-bytes sequence");
|
||
|
||
const char *utf8_test = "";
|
||
|
||
ATT_ASSERT(mjb_is_utf8(NULL, 0), true, "NULL with zero size is empty UTF-8");
|
||
ATT_ASSERT(mjb_is_utf8("", 0), true, "Empty UTF-8 \"\" string");
|
||
ATT_ASSERT(mjb_is_utf8(utf8_test, strlen(utf8_test)), true, "Empty UTF-8 string");
|
||
|
||
utf8_test = "Hello, world!";
|
||
ATT_ASSERT(mjb_is_utf8(utf8_test, strlen(utf8_test)), true, "Simple ASCII");
|
||
|
||
utf8_test = "Hell\xC3\xB6 w\xC3\xB6rld";
|
||
ATT_ASSERT(mjb_is_utf8(utf8_test, strlen(utf8_test)), true, "Hell[o] wörld");
|
||
|
||
utf8_test = "\xE3\x81\x93\xE3\x82\x93\xE3\x81\xAB\xE3\x81\xA1\xE3\x81\xAF\xE4\xB8\x96\xE7\x95"
|
||
"\x8C"; // こんにちは世界
|
||
ATT_ASSERT(mjb_is_utf8(utf8_test, strlen(utf8_test)), true, "Japanese");
|
||
|
||
utf8_test = "Hello \xF0\x9F\x8C\x8D";
|
||
ATT_ASSERT(mjb_is_utf8(utf8_test, strlen(utf8_test)), true, "Hello (world)");
|
||
|
||
utf8_test = "a\xC2\xA2\xE2\x82\xAC\xF0\x90\x8D\x88";
|
||
ATT_ASSERT(mjb_is_utf8(utf8_test, strlen(utf8_test)), true,
|
||
"1-byte, 2-byte, 3-byte, and 4-byte characters");
|
||
|
||
utf8_test = "\xF4\x8F\xBF\xBF";
|
||
ATT_ASSERT(mjb_is_utf8(utf8_test, strlen(utf8_test)), true,
|
||
"U+10FFFF, maximum code point");
|
||
|
||
utf8_test = "\xEF\xBB\xBF"
|
||
"Hello";
|
||
ATT_ASSERT(mjb_is_utf8(utf8_test, strlen(utf8_test)), true,
|
||
"BOM (Byte Order Mark) followed by \"Hello\"");
|
||
|
||
utf8_test = "Hello\xC2\xA0World\xE2\x80\x83Test\xE2\x80\x8B";
|
||
ATT_ASSERT(mjb_is_utf8(utf8_test, strlen(utf8_test)), true,
|
||
"Various Unicode spaces and invisible characters");
|
||
|
||
utf8_test = "n\xCC\x83";
|
||
ATT_ASSERT(mjb_is_utf8(utf8_test, strlen(utf8_test)), true, "N combined with tilde");
|
||
|
||
utf8_test = "A\xCE\x91\xE2\x98\x83\xF0\x9D\x84\x9E";
|
||
ATT_ASSERT(mjb_is_utf8(utf8_test, strlen(utf8_test)), true,
|
||
"Characters from various Unicode planes");
|
||
|
||
utf8_test = "Hello\0World";
|
||
ATT_ASSERT(mjb_is_utf8(utf8_test, 11), true, "String with NULL character");
|
||
|
||
const unsigned char utf8_null_invalid[] = { 'A', '\0', 0xFF };
|
||
ATT_ASSERT(mjb_is_utf8((const char *)utf8_null_invalid, sizeof(utf8_null_invalid)),
|
||
false, "UTF-8 rejects invalid byte after embedded NULL");
|
||
ATT_ASSERT((unsigned int)mjb_detect_encoding((const char *)utf8_null_invalid,
|
||
sizeof(utf8_null_invalid)),
|
||
(unsigned int)MJB_ENC_UNKNOWN, "Encoding rejects invalid byte after embedded NULL");
|
||
ATT_ASSERT(mjb_is_utf8((const char *)utf8_null_invalid, MJB_NUL_TERMINATED), true,
|
||
"NUL-terminated UTF-8 excludes bytes after the terminator");
|
||
|
||
utf8_test = "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14"
|
||
"\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F\x7F";
|
||
ATT_ASSERT(mjb_is_utf8(utf8_test, strlen(utf8_test)), true,
|
||
"All ASCII control characters");
|
||
|
||
utf8_test = "Hello\xE2\x80\x94World\xE2\x80\xA2Test\xE2\x99\xA5Unicode\xE2\x98\xAE";
|
||
ATT_ASSERT(mjb_is_utf8(utf8_test, strlen(utf8_test)), true,
|
||
"Various Unicode punctuation and symbols");
|
||
|
||
// UTF-16 tests
|
||
ATT_ASSERT(mjb_is_utf16(NULL, 0), true, "NULL with zero size is empty UTF-16");
|
||
ATT_ASSERT(mjb_is_utf16("", 0), true, "Empty UTF-16 string");
|
||
ATT_ASSERT(mjb_is_utf16("A", 1), false, "Odd-length UTF-16 string");
|
||
ATT_ASSERT(mjb_is_utf16("", 1), false, "Odd-length empty UTF-16 string");
|
||
|
||
// UTF-16BE tests (Big Endian)
|
||
const char *utf16be_hello = "\x00H\x00e\x00l\x00l\x00o"; // "Hello" in UTF-16BE
|
||
ATT_ASSERT(mjb_is_utf16(utf16be_hello, 10), true, "UTF-16BE Hello");
|
||
|
||
const char *utf16be_with_bmp = "\x00A\x03\x91\x00Z"; // "AΑZ" (A, Greek Alpha, Z) in UTF-16BE
|
||
ATT_ASSERT(mjb_is_utf16(utf16be_with_bmp, 6), true, "UTF-16BE with BMP characters");
|
||
|
||
// UTF-16LE tests (Little Endian)
|
||
const char *utf16le_hello = "H\x00e\x00l\x00l\x00o\x00"; // "Hello" in UTF-16LE
|
||
ATT_ASSERT(mjb_is_utf16(utf16le_hello, 10), true, "UTF-16LE Hello");
|
||
|
||
const char utf16le_terminated[] = { 'H', '\0', 'i', '\0', '\0', '\0' };
|
||
ATT_ASSERT(mjb_is_utf16(utf16le_terminated, MJB_NUL_TERMINATED), true,
|
||
"NUL-terminated UTF-16LE");
|
||
|
||
const char *utf16le_with_bmp = "A\x00\x91\x03Z\x00"; // "AΑZ" (A, Greek Alpha, Z) in UTF-16LE
|
||
ATT_ASSERT(mjb_is_utf16(utf16le_with_bmp, 6), true, "UTF-16LE with BMP characters");
|
||
|
||
// UTF-16BE with surrogate pairs (emoji: 🙂 U+1F642)
|
||
const char *utf16be_emoji = "\xD8\x3D\xDE\x42"; // 🙂 in UTF-16BE surrogate pair
|
||
ATT_ASSERT(mjb_is_utf16(utf16be_emoji, 4), true, "UTF-16BE with surrogate pair");
|
||
|
||
// UTF-16LE with surrogate pairs (emoji: 🙂 U+1F642)
|
||
const char *utf16le_emoji = "\x3D\xD8\x42\xDE"; // 🙂 in UTF-16LE surrogate pair
|
||
ATT_ASSERT(mjb_is_utf16(utf16le_emoji, 4), true, "UTF-16LE with surrogate pair");
|
||
|
||
// UTF-16 with BOM markers
|
||
const char *utf16be_bom = "\xFE\xFF\x00H\x00i"; // BOM + "Hi" in UTF-16BE
|
||
ATT_ASSERT(mjb_is_utf16(utf16be_bom, 6), true, "UTF-16BE with BOM");
|
||
|
||
const char *utf16le_bom = "\xFF\xFEH\x00i\x00"; // BOM + "Hi" in UTF-16LE
|
||
ATT_ASSERT(mjb_is_utf16(utf16le_bom, 6), true, "UTF-16LE with BOM");
|
||
|
||
// Edge cases - maximum valid codepoints
|
||
const char *utf16be_max = "\xDB\xFF\xDF\xFF"; // U+10FFFF in UTF-16BE
|
||
ATT_ASSERT(mjb_is_utf16(utf16be_max, 4), true, "UTF-16BE maximum codepoint");
|
||
|
||
const char *utf16le_max = "\xFF\xDB\xFF\xDF"; // U+10FFFF in UTF-16LE
|
||
ATT_ASSERT(mjb_is_utf16(utf16le_max, 4), true, "UTF-16LE maximum codepoint");
|
||
|
||
ATT_ASSERT(mjb_codepoint_encode(0, (char *)0, 0, MJB_ENC_UTF_8), 0, "Void buffer");
|
||
ATT_ASSERT(mjb_codepoint_encode(0, (char *)1, 1, MJB_ENC_UTF_8), 0, "Wrong size");
|
||
|
||
ATT_ASSERT(mjb_codepoint_encode(0, (char *)0, 0, MJB_ENC_UTF_16LE), 0, "Void buffer UTF-16LE");
|
||
ATT_ASSERT(mjb_codepoint_encode(0, (char *)1, 1, MJB_ENC_UTF_16LE), 0, "Wrong size UTF-16LE");
|
||
|
||
ATT_ASSERT(mjb_codepoint_encode(0, (char *)0, 0, MJB_ENC_UTF_16BE), 0, "Void buffer UTF-16BE");
|
||
ATT_ASSERT(mjb_codepoint_encode(0, (char *)1, 1, MJB_ENC_UTF_16BE), 0, "Wrong size UTF-16BE");
|
||
|
||
ATT_ASSERT(mjb_codepoint_encode(0, (char *)1, 4, MJB_ENC_UTF_32), 0, "Invalid encoding");
|
||
|
||
char buffer[5];
|
||
ATT_ASSERT(mjb_codepoint_encode(MJB_CODEPOINT_MAX + 1, (char *)buffer, 5, MJB_ENC_UTF_8), 0,
|
||
"Noncharacter max");
|
||
ATT_ASSERT(mjb_codepoint_encode(MJB_CODEPOINT_MIN - 1, (char *)buffer, 5, MJB_ENC_UTF_8), 0,
|
||
"Noncharacter min");
|
||
ATT_ASSERT(mjb_codepoint_encode(0xD800, (char *)buffer, 5, MJB_ENC_UTF_8), 0,
|
||
"High surrogate UTF-8");
|
||
ATT_ASSERT(mjb_codepoint_encode(0xDFFF, (char *)buffer, 5, MJB_ENC_UTF_8), 0,
|
||
"Low surrogate UTF-8");
|
||
|
||
ATT_ASSERT(mjb_codepoint_encode(MJB_CODEPOINT_MAX + 1, (char *)buffer, 5, MJB_ENC_UTF_16LE), 0,
|
||
"Noncharacter max UTF-16LE");
|
||
ATT_ASSERT(mjb_codepoint_encode(MJB_CODEPOINT_MIN - 1, (char *)buffer, 5, MJB_ENC_UTF_16LE), 0,
|
||
"Noncharacter min UTF-16LE");
|
||
ATT_ASSERT(mjb_codepoint_encode(0xD800, (char *)buffer, 5, MJB_ENC_UTF_16LE), 0,
|
||
"High surrogate UTF-16LE");
|
||
ATT_ASSERT(mjb_codepoint_encode(0xDFFF, (char *)buffer, 5, MJB_ENC_UTF_16LE), 0,
|
||
"Low surrogate UTF-16LE");
|
||
|
||
ATT_ASSERT(mjb_codepoint_encode(MJB_CODEPOINT_MAX + 1, (char *)buffer, 5, MJB_ENC_UTF_16BE), 0,
|
||
"Noncharacter max UTF-16BE");
|
||
ATT_ASSERT(mjb_codepoint_encode(MJB_CODEPOINT_MIN - 1, (char *)buffer, 5, MJB_ENC_UTF_16BE), 0,
|
||
"Noncharacter min UTF-16BE");
|
||
ATT_ASSERT(mjb_codepoint_encode(0xD800, (char *)buffer, 5, MJB_ENC_UTF_16BE), 0,
|
||
"High surrogate UTF-16BE");
|
||
ATT_ASSERT(mjb_codepoint_encode(0xDFFF, (char *)buffer, 5, MJB_ENC_UTF_16BE), 0,
|
||
"Low surrogate UTF-16BE");
|
||
|
||
ATT_ASSERT(mjb_codepoint_encode(0, (char *)0, 0, MJB_ENC_UTF_32LE), 0, "Void buffer UTF-32LE");
|
||
ATT_ASSERT(mjb_codepoint_encode(0, (char *)1, 4, MJB_ENC_UTF_32LE), 0, "Wrong size UTF-32LE");
|
||
|
||
ATT_ASSERT(mjb_codepoint_encode(0, (char *)0, 0, MJB_ENC_UTF_32BE), 0, "Void buffer UTF-32BE");
|
||
ATT_ASSERT(mjb_codepoint_encode(0, (char *)1, 4, MJB_ENC_UTF_32BE), 0, "Wrong size UTF-32BE");
|
||
|
||
ATT_ASSERT(mjb_codepoint_encode(MJB_CODEPOINT_MAX + 1, (char *)buffer, 5, MJB_ENC_UTF_32LE), 0,
|
||
"Noncharacter max UTF-32LE");
|
||
ATT_ASSERT(mjb_codepoint_encode(MJB_CODEPOINT_MIN - 1, (char *)buffer, 5, MJB_ENC_UTF_32LE), 0,
|
||
"Noncharacter min UTF-32LE");
|
||
ATT_ASSERT(mjb_codepoint_encode(0xD800, (char *)buffer, 5, MJB_ENC_UTF_32LE), 0,
|
||
"High surrogate UTF-32LE");
|
||
ATT_ASSERT(mjb_codepoint_encode(0xDFFF, (char *)buffer, 5, MJB_ENC_UTF_32LE), 0,
|
||
"Low surrogate UTF-32LE");
|
||
|
||
ATT_ASSERT(mjb_codepoint_encode(MJB_CODEPOINT_MAX + 1, (char *)buffer, 5, MJB_ENC_UTF_32BE), 0,
|
||
"Noncharacter max UTF-32BE");
|
||
ATT_ASSERT(mjb_codepoint_encode(MJB_CODEPOINT_MIN - 1, (char *)buffer, 5, MJB_ENC_UTF_32BE), 0,
|
||
"Noncharacter min UTF-32BE");
|
||
ATT_ASSERT(mjb_codepoint_encode(0xD800, (char *)buffer, 5, MJB_ENC_UTF_32BE), 0,
|
||
"High surrogate UTF-32BE");
|
||
ATT_ASSERT(mjb_codepoint_encode(0xDFFF, (char *)buffer, 5, MJB_ENC_UTF_32BE), 0,
|
||
"Low surrogate UTF-32BE");
|
||
|
||
ATT_ASSERT(mjb_codepoint_encode(0x0000, (char *)buffer, 5, MJB_ENC_UTF_8), 1, "0x0000 UTF-8");
|
||
ATT_ASSERT(buffer[0], 0, "0x0000 UTF-8");
|
||
|
||
ATT_ASSERT(mjb_codepoint_encode(0x0000, (char *)buffer, 5, MJB_ENC_UTF_16LE), 2,
|
||
"0x0000 UTF-16LE");
|
||
ATT_ASSERT(buffer[0], 0, "0x0000 UTF-16LE");
|
||
|
||
ATT_ASSERT(mjb_codepoint_encode(0x0000, (char *)buffer, 5, MJB_ENC_UTF_16BE), 2,
|
||
"0x0000 UTF-16BE");
|
||
ATT_ASSERT(buffer[0], 0, "0x0000 UTF-16BE");
|
||
|
||
ATT_ASSERT(mjb_codepoint_encode(0x0000, (char *)buffer, 5, MJB_ENC_UTF_32LE), 4,
|
||
"0x0000 UTF-32LE");
|
||
ATT_ASSERT(buffer[0], 0, "0x0000 UTF-32LE");
|
||
|
||
ATT_ASSERT(mjb_codepoint_encode(0x0000, (char *)buffer, 5, MJB_ENC_UTF_32BE), 4,
|
||
"0x0000 UTF-32BE");
|
||
ATT_ASSERT(buffer[0], 0, "0x0000 UTF-32BE");
|
||
|
||
ATT_ASSERT(mjb_codepoint_encode(0x007F, (char *)buffer, 5, MJB_ENC_ASCII), 1,
|
||
"ASCII encode limit");
|
||
ATT_ASSERT(buffer[0], (char)0x7F, "ASCII encode limit value");
|
||
|
||
ATT_ASSERT(mjb_codepoint_encode(0x0080, (char *)buffer, 5, MJB_ENC_ASCII), 0,
|
||
"ASCII encode rejects non-ASCII");
|
||
ATT_ASSERT(mjb_codepoint_encode(0x00E9, (char *)buffer, 5, MJB_ENC_ASCII), 0,
|
||
"ASCII encode rejects Latin-1");
|
||
|
||
mjb_encoding encoding;
|
||
|
||
#define TEST_UTF(CHAR, STR, RES, COMMENT) \
|
||
ATT_ASSERT(mjb_codepoint_encode(CHAR, (char *)buffer, 5, encoding), RES, COMMENT); \
|
||
ATT_ASSERT((const char *)buffer, STR, COMMENT)
|
||
|
||
encoding = MJB_ENC_UTF_8;
|
||
|
||
// UTF-8 tests
|
||
TEST_UTF(0x007F, "\x7F", 1, "ASCII limit");
|
||
TEST_UTF(0x07FF, "\xDF\xBF", 2, "2-bytes limit");
|
||
TEST_UTF(0x1E0A, "\xE1\xB8\x8A", 3, "LATIN CAPITAL LETTER D WITH DOT ABOVE");
|
||
TEST_UTF(0xFFFD, "\xEF\xBF\xBD", 3, "3-bytes limit");
|
||
TEST_UTF(0x10FFFE, "\xF4\x8F\xBF\xBE", 4, "4-bytes limit");
|
||
TEST_UTF(0x1F642, "\xF0\x9F\x99\x82", 4, "SLIGHTLY SMILING FACE");
|
||
|
||
encoding = MJB_ENC_UTF_16LE;
|
||
|
||
TEST_UTF(0x007F, "\x7F\x00", 2, "ASCII limit UTF-16LE");
|
||
TEST_UTF(0x07FF, "\xFF\x07", 2, "2-bytes limit UTF-16LE");
|
||
TEST_UTF(0x1E0A, "\x0A\x1E", 2, "LATIN CAPITAL LETTER D WITH DOT ABOVE UTF-16LE");
|
||
TEST_UTF(0xFFFD, "\xFD\xFF", 2, "3-bytes limit UTF-16LE");
|
||
TEST_UTF(0x10FFFE, "\xFF\xDB\xFE\xDF", 4, "4-bytes limit UTF-16LE");
|
||
TEST_UTF(0x1F642, "\x3D\xD8\x42\xDE", 4, "SLIGHTLY SMILING FACE UTF-16LE");
|
||
|
||
encoding = MJB_ENC_UTF_16BE;
|
||
|
||
TEST_UTF(0x007F, "\x00\x7F", 2, "ASCII limit UTF-16BE");
|
||
TEST_UTF(0x07FF, "\x07\xFF", 2, "2-bytes limit UTF-16BE");
|
||
TEST_UTF(0x1E0A, "\x1E\x0A", 2, "LATIN CAPITAL LETTER D WITH DOT ABOVE UTF-16BE");
|
||
TEST_UTF(0xFFFD, "\xFF\xFD", 2, "3-bytes limit UTF-16BE");
|
||
TEST_UTF(0x10FFFE, "\xDB\xFF\xDF\xFE", 4, "4-bytes limit UTF-16BE");
|
||
TEST_UTF(0x1F642, "\xD8\x3D\xDE\x42", 4, "SLIGHTLY SMILING FACE UTF-16BE");
|
||
|
||
encoding = MJB_ENC_UTF_32LE;
|
||
|
||
TEST_UTF(0x007F, "\x7F\x00\x00\x00", 4, "ASCII limit UTF-32LE");
|
||
TEST_UTF(0x07FF, "\xFF\x07\x00\x00", 4, "2-bytes limit UTF-32LE");
|
||
TEST_UTF(0x1E0A, "\x0A\x1E\x00\x00", 4, "LATIN CAPITAL LETTER D WITH DOT ABOVE UTF-32LE");
|
||
TEST_UTF(0xFFFD, "\xFD\xFF\x00\x00", 4, "3-bytes limit UTF-32LE");
|
||
TEST_UTF(0x10FFFE, "\xFE\xFF\x10\x00", 4, "4-bytes limit UTF-32LE");
|
||
TEST_UTF(0x1F642, "\x42\xF6\x01\x00", 4, "SLIGHTLY SMILING FACE UTF-32LE");
|
||
|
||
encoding = MJB_ENC_UTF_32BE;
|
||
|
||
TEST_UTF(0x007F, "\x00\x00\x00\x7F", 4, "ASCII limit UTF-32BE");
|
||
TEST_UTF(0x07FF, "\x00\x00\x07\xFF", 4, "2-bytes limit UTF-32BE");
|
||
TEST_UTF(0x1E0A, "\x00\x00\x1E\x0A", 4, "LATIN CAPITAL LETTER D WITH DOT ABOVE UTF-32BE");
|
||
TEST_UTF(0xFFFD, "\x00\x00\xFF\xFD", 4, "3-bytes limit UTF-32BE");
|
||
TEST_UTF(0x10FFFE, "\x00\x10\xFF\xFE", 4, "4-bytes limit UTF-32BE");
|
||
TEST_UTF(0x1F642, "\x00\x01\xF6\x42", 4, "SLIGHTLY SMILING FACE UTF-32BE");
|
||
|
||
#undef TEST_UTF
|
||
|
||
const char *hello_strings[] = {
|
||
"Hello", // UTF-8
|
||
"\x48\x00\x65\x00\x6C\x00\x6C\x00\x6F\x00", // UTF-16LE
|
||
"\x00\x48\x00\x65\x00\x6C\x00\x6C\x00\x6F", // UTF-16BE
|
||
"\x48\x00\x00\x00\x65\x00\x00\x00\x6C\x00\x00\x00\x6C\x00\x00\x00\x6F\x00\x00"
|
||
"\x00", // UTF-32LE
|
||
"\x00\x00\x00\x48\x00\x00\x00\x65\x00\x00\x00\x6C\x00\x00\x00\x6C\x00\x00\x00"
|
||
"\x6F" // UTF-32BE
|
||
};
|
||
size_t hello_strings_sizes[] = { 5, 10, 10, 20, 20 };
|
||
mjb_encoding encodings[] = { MJB_ENC_UTF_8, MJB_ENC_UTF_16LE, MJB_ENC_UTF_16BE,
|
||
MJB_ENC_UTF_32LE, MJB_ENC_UTF_32BE };
|
||
const char *output_encodings[] = { "UTF-8", "UTF-16LE", "UTF-16BE", "UTF-32LE", "UTF-32BE" };
|
||
char test_description[64];
|
||
mjb_result boundary_result;
|
||
|
||
mjb_text text = mjb_text_utf8(NULL, 1);
|
||
ATT_ASSERT_STATUS(mjb_convert_encoding(&text, MJB_MALFORMED_STOP,
|
||
MJB_ENC_UTF_16LE, &boundary_result, NULL),
|
||
MJB_STATUS_INVALID_ARGUMENT, "Convert encoding rejects NULL buffer");
|
||
text = mjb_text_utf8("", 0);
|
||
ATT_ASSERT_STATUS(mjb_convert_encoding(&text, MJB_MALFORMED_STOP,
|
||
MJB_ENC_UTF_16LE, NULL, NULL),
|
||
MJB_STATUS_INVALID_ARGUMENT, "Convert encoding rejects NULL result");
|
||
mjb_result null_text_result;
|
||
ATT_ASSERT_STATUS(mjb_convert_encoding(NULL, MJB_MALFORMED_STOP, MJB_ENC_UTF_16LE,
|
||
&null_text_result, NULL),
|
||
MJB_STATUS_INVALID_ARGUMENT, "Convert encoding rejects NULL text");
|
||
|
||
MJB_TEST_COVERAGE(mjb_convert_encoding_into);
|
||
|
||
mjb_text into_text = mjb_text_utf8(NULL, 1);
|
||
mjb_buffer into = mjb_buffer_make(NULL, 123, MJB_ENC_UTF_16LE);
|
||
|
||
ATT_ASSERT_STATUS(mjb_convert_encoding_into(&into_text, MJB_MALFORMED_STOP, &into, NULL),
|
||
MJB_STATUS_INVALID_ARGUMENT, "Convert encoding into rejects NULL buffer");
|
||
ATT_ASSERT(into.size, (size_t)0, "Convert encoding into resets size for invalid input");
|
||
into = mjb_buffer_make(NULL, 123, MJB_ENC_UTF_16LE);
|
||
ATT_ASSERT_STATUS(mjb_convert_encoding_into(NULL, MJB_MALFORMED_STOP, &into, NULL),
|
||
MJB_STATUS_INVALID_ARGUMENT, "Convert encoding into rejects NULL text");
|
||
ATT_ASSERT(into.size, (size_t)0, "Convert encoding into resets size for NULL text");
|
||
into_text = mjb_text_utf8("A", 1);
|
||
ATT_ASSERT_STATUS(mjb_convert_encoding_into(&into_text, MJB_MALFORMED_STOP, NULL, NULL),
|
||
MJB_STATUS_INVALID_ARGUMENT, "Convert encoding into rejects NULL output");
|
||
into = mjb_buffer_make(NULL, 123, MJB_ENC_UTF_16);
|
||
ATT_ASSERT_STATUS(mjb_convert_encoding_into(&into_text, MJB_MALFORMED_STOP, &into, NULL),
|
||
MJB_STATUS_INVALID_ENCODING, "Convert encoding into rejects output without byte order");
|
||
ATT_ASSERT(into.size, (size_t)0, "Convert encoding into resets size for invalid encoding");
|
||
|
||
const char into_input[] = "caf\xC3\xA9";
|
||
const unsigned char into_expected[] = { 'c', 0, 'a', 0, 'f', 0, 0xE9, 0 };
|
||
|
||
into_text = mjb_text_utf8(into_input, strlen(into_input));
|
||
into = mjb_buffer_make(NULL, 0, MJB_ENC_UTF_16LE);
|
||
ATT_ASSERT_STATUS(mjb_convert_encoding_into(&into_text, MJB_MALFORMED_STOP, &into, NULL),
|
||
MJB_STATUS_OK, "Query encoding conversion output size");
|
||
ATT_ASSERT(into.size, sizeof(into_expected), "Encoding conversion required payload size");
|
||
|
||
unsigned char into_output[sizeof(into_expected) + 1];
|
||
memset(into_output, 0xA5, sizeof(into_output));
|
||
size_t small_capacity = sizeof(into_expected) - 1;
|
||
into = mjb_buffer_make(into_output, small_capacity, MJB_ENC_UTF_16LE);
|
||
|
||
ATT_ASSERT_STATUS(mjb_convert_encoding_into(&into_text, MJB_MALFORMED_STOP, &into, NULL),
|
||
MJB_STATUS_OUTPUT_TOO_SMALL, "Convert encoding reports a small output buffer");
|
||
ATT_ASSERT(into.size, sizeof(into_expected), "Small output buffer reports required size");
|
||
|
||
unsigned char untouched_output[sizeof(into_output)];
|
||
memset(untouched_output, 0xA5, sizeof(untouched_output));
|
||
ATT_ASSERT(memcmp(into_output + small_capacity, untouched_output + small_capacity,
|
||
sizeof(into_output) - small_capacity),
|
||
0, "Small output buffer is not written past its capacity");
|
||
|
||
into = mjb_buffer_make(into_output, sizeof(into_expected), MJB_ENC_UTF_16LE);
|
||
ATT_ASSERT_STATUS(mjb_convert_encoding_into(&into_text, MJB_MALFORMED_STOP, &into, NULL),
|
||
MJB_STATUS_OK, "Convert encoding into exact-size output buffer");
|
||
ATT_ASSERT(into.size, sizeof(into_expected), "Convert encoding into written payload size");
|
||
ATT_ASSERT(memcmp(into_output, into_expected, sizeof(into_expected)), 0,
|
||
"Convert encoding into output bytes");
|
||
ATT_ASSERT((unsigned int)into_output[sizeof(into_expected)], 0xA5u,
|
||
"Convert encoding into does not write a terminator");
|
||
|
||
unsigned char same_encoding_output[] = { 0, 0xA5 };
|
||
into_text = mjb_text_utf8("A", 1);
|
||
into = mjb_buffer_make(same_encoding_output, 1, MJB_ENC_UTF_8);
|
||
ATT_ASSERT_STATUS(mjb_convert_encoding_into(&into_text, MJB_MALFORMED_STOP, &into, NULL),
|
||
MJB_STATUS_OK, "Convert encoding into copies unchanged encoding");
|
||
ATT_ASSERT((unsigned int)same_encoding_output[0], (unsigned int)'A',
|
||
"Convert encoding into unchanged byte");
|
||
ATT_ASSERT((unsigned int)same_encoding_output[1], 0xA5u,
|
||
"Unchanged encoding does not write a terminator");
|
||
|
||
const char utf16le_ascii[] = { 'e', '\0', 'n', '\0' };
|
||
mjb_result ascii_result;
|
||
|
||
text = mjb_text_make(utf16le_ascii, sizeof(utf16le_ascii), MJB_ENC_UTF_16LE);
|
||
ATT_ASSERT_STATUS(mjb_convert_encoding(&text, MJB_MALFORMED_STOP, MJB_ENC_ASCII, &ascii_result,
|
||
NULL),
|
||
MJB_STATUS_OK, "Convert UTF-16LE ASCII text to ASCII");
|
||
ATT_ASSERT(ascii_result.owned, true, "Convert UTF-16LE ASCII text is owned");
|
||
ATT_ASSERT(ascii_result.output_size, (size_t)2, "Convert UTF-16LE ASCII text size");
|
||
ATT_ASSERT(ascii_result.output, "en", "Convert UTF-16LE ASCII text output");
|
||
(void)mjb_result_free(&ascii_result);
|
||
|
||
text = mjb_text_utf8("\xC3\xA9", 2);
|
||
ATT_ASSERT_STATUS(mjb_convert_encoding(&text, MJB_MALFORMED_STOP,
|
||
MJB_ENC_ASCII, &ascii_result, NULL),
|
||
MJB_STATUS_UNSUPPORTED, "Convert UTF-8 non-ASCII text to ASCII");
|
||
|
||
const char utf16le_smile[] = { '\x3D', '\xD8', '\x42', '\xDE' };
|
||
const char utf16be_smile[] = { '\xD8', '\x3D', '\xDE', '\x42' };
|
||
|
||
text = mjb_text_make(utf16le_smile, sizeof(utf16le_smile), MJB_ENC_UTF_16LE);
|
||
ATT_ASSERT_STATUS(mjb_convert_encoding(&text, MJB_MALFORMED_STOP, MJB_ENC_UTF_8, &ascii_result,
|
||
NULL),
|
||
MJB_STATUS_OK, "Convert UTF-16LE surrogate pair to UTF-8");
|
||
ATT_ASSERT(ascii_result.output_size, (size_t)4, "Convert UTF-16LE surrogate pair to UTF-8 size");
|
||
ATT_ASSERT(memcmp(ascii_result.output, "\xF0\x9F\x99\x82", ascii_result.output_size), 0,
|
||
"Convert UTF-16LE surrogate pair to UTF-8 output");
|
||
(void)mjb_result_free(&ascii_result);
|
||
|
||
text = mjb_text_make(utf16be_smile, sizeof(utf16be_smile), MJB_ENC_UTF_16BE);
|
||
ATT_ASSERT_STATUS(mjb_convert_encoding(&text, MJB_MALFORMED_STOP, MJB_ENC_UTF_8, &ascii_result,
|
||
NULL),
|
||
MJB_STATUS_OK, "Convert UTF-16BE surrogate pair to UTF-8");
|
||
ATT_ASSERT(ascii_result.output_size, (size_t)4, "Convert UTF-16BE surrogate pair to UTF-8 size");
|
||
ATT_ASSERT(memcmp(ascii_result.output, "\xF0\x9F\x99\x82", ascii_result.output_size), 0,
|
||
"Convert UTF-16BE surrogate pair to UTF-8 output");
|
||
(void)mjb_result_free(&ascii_result);
|
||
|
||
const char utf8_bom_a[] = { '\xEF', '\xBB', '\xBF', 'A' };
|
||
const char utf16be_bom_a[] = { '\xFE', '\xFF', '\x00', 'A' };
|
||
const char utf16le_bom_a[] = { '\xFF', '\xFE', 'A', '\x00' };
|
||
const char utf16be_bom_a_inner_bom_b[] = {
|
||
'\xFE',
|
||
'\xFF',
|
||
'\x00',
|
||
'A',
|
||
'\xFE',
|
||
'\xFF',
|
||
'\x00',
|
||
'B',
|
||
};
|
||
const char utf32be_bom_a[] = {
|
||
'\x00',
|
||
'\x00',
|
||
'\xFE',
|
||
'\xFF',
|
||
'\x00',
|
||
'\x00',
|
||
'\x00',
|
||
'A',
|
||
};
|
||
const char utf32le_bom_a[] = {
|
||
'\xFF',
|
||
'\xFE',
|
||
'\x00',
|
||
'\x00',
|
||
'A',
|
||
'\x00',
|
||
'\x00',
|
||
'\x00',
|
||
};
|
||
const char utf16be_plain_a[] = { '\x00', 'A' };
|
||
|
||
mjb_encoding detected_utf16be = mjb_detect_encoding(utf16be_bom_a, sizeof(utf16be_bom_a));
|
||
mjb_encoding detected_utf16le = mjb_detect_encoding(utf16le_bom_a, sizeof(utf16le_bom_a));
|
||
mjb_encoding detected_utf32be = mjb_detect_encoding(utf32be_bom_a, sizeof(utf32be_bom_a));
|
||
mjb_encoding detected_utf32le = mjb_detect_encoding(utf32le_bom_a, sizeof(utf32le_bom_a));
|
||
|
||
assert_encoding_conversion(utf16be_bom_a, sizeof(utf16be_bom_a), detected_utf16be,
|
||
MJB_ENC_UTF_8, "A", 1, "Convert detected UTF-16BE BOM consumes signature");
|
||
assert_encoding_conversion(utf16le_bom_a, sizeof(utf16le_bom_a), detected_utf16le,
|
||
MJB_ENC_UTF_8, "A", 1, "Convert detected UTF-16LE BOM consumes signature");
|
||
assert_encoding_conversion(utf32be_bom_a, sizeof(utf32be_bom_a), detected_utf32be,
|
||
MJB_ENC_UTF_8, "A", 1, "Convert detected UTF-32BE BOM consumes signature");
|
||
assert_encoding_conversion(utf32le_bom_a, sizeof(utf32le_bom_a), detected_utf32le,
|
||
MJB_ENC_UTF_8, "A", 1, "Convert detected UTF-32LE BOM consumes signature");
|
||
|
||
assert_encoding_conversion(utf16be_bom_a, sizeof(utf16be_bom_a), MJB_ENC_UTF_16, MJB_ENC_UTF_8,
|
||
"A", 1, "Convert generic UTF-16BE BOM consumes signature");
|
||
assert_encoding_conversion(utf16le_bom_a, sizeof(utf16le_bom_a), MJB_ENC_UTF_16, MJB_ENC_UTF_8,
|
||
"A", 1, "Convert generic UTF-16LE BOM consumes signature");
|
||
assert_encoding_conversion(utf16be_bom_a_inner_bom_b, sizeof(utf16be_bom_a_inner_bom_b),
|
||
MJB_ENC_UTF_16, MJB_ENC_UTF_8,
|
||
"A\xEF\xBB\xBF"
|
||
"B",
|
||
5, "Convert generic UTF-16 preserves inner U+FEFF");
|
||
assert_encoding_conversion(utf32be_bom_a, sizeof(utf32be_bom_a), MJB_ENC_UTF_32, MJB_ENC_UTF_8,
|
||
"A", 1, "Convert generic UTF-32BE BOM consumes signature");
|
||
assert_encoding_conversion(utf32le_bom_a, sizeof(utf32le_bom_a), MJB_ENC_UTF_32, MJB_ENC_UTF_8,
|
||
"A", 1, "Convert generic UTF-32LE BOM consumes signature");
|
||
|
||
assert_encoding_conversion(utf16be_bom_a, sizeof(utf16be_bom_a), MJB_ENC_UTF_16BE,
|
||
MJB_ENC_UTF_8, utf8_bom_a, sizeof(utf8_bom_a),
|
||
"Convert explicit UTF-16BE preserves U+FEFF");
|
||
assert_encoding_conversion(utf16le_bom_a, sizeof(utf16le_bom_a), MJB_ENC_UTF_16LE,
|
||
MJB_ENC_UTF_8, utf8_bom_a, sizeof(utf8_bom_a),
|
||
"Convert explicit UTF-16LE preserves U+FEFF");
|
||
assert_encoding_conversion(utf32be_bom_a, sizeof(utf32be_bom_a), MJB_ENC_UTF_32BE,
|
||
MJB_ENC_UTF_8, utf8_bom_a, sizeof(utf8_bom_a),
|
||
"Convert explicit UTF-32BE preserves U+FEFF");
|
||
assert_encoding_conversion(utf32le_bom_a, sizeof(utf32le_bom_a), MJB_ENC_UTF_32LE,
|
||
MJB_ENC_UTF_8, utf8_bom_a, sizeof(utf8_bom_a),
|
||
"Convert explicit UTF-32LE preserves U+FEFF");
|
||
|
||
MJB_TEST_COVERAGE(mjb_codepoint_count);
|
||
ATT_ASSERT(count_codepoints(utf16be_bom_a, sizeof(utf16be_bom_a), MJB_ENC_UTF_16), (size_t)1,
|
||
"Length generic UTF-16BE BOM consumes signature");
|
||
ATT_ASSERT(count_codepoints(utf16be_bom_a, sizeof(utf16be_bom_a), MJB_ENC_UTF_16BE), (size_t)2,
|
||
"Length explicit UTF-16BE preserves U+FEFF");
|
||
ATT_ASSERT(count_codepoints(utf32le_bom_a, sizeof(utf32le_bom_a), MJB_ENC_UTF_32), (size_t)1,
|
||
"Length generic UTF-32LE BOM consumes signature");
|
||
|
||
size_t rejected_count = 6251;
|
||
mjb_text detected_text = mjb_text_make(utf32le_bom_a, sizeof(utf32le_bom_a), detected_utf32le);
|
||
ATT_ASSERT_STATUS(mjb_codepoint_count(&detected_text, MJB_MALFORMED_STOP, &rejected_count, NULL),
|
||
MJB_STATUS_INVALID_ENCODING,
|
||
"Codepoint count rejects a multi-encoding detection mask");
|
||
ATT_ASSERT(rejected_count, (size_t)0, "Codepoint count is zero after a detection mask");
|
||
ATT_ASSERT(count_codepoints(utf32le_bom_a, sizeof(utf32le_bom_a), MJB_ENC_UTF_32LE), (size_t)2,
|
||
"Length explicit UTF-32LE preserves U+FEFF");
|
||
|
||
text = mjb_text_make(utf16be_plain_a, sizeof(utf16be_plain_a), MJB_ENC_UTF_16);
|
||
ATT_ASSERT_STATUS(mjb_convert_encoding(&text, MJB_MALFORMED_STOP, MJB_ENC_UTF_8, &ascii_result, NULL),
|
||
MJB_STATUS_INVALID_ENCODING,
|
||
"Convert generic UTF-16 without BOM rejects unknown byte order");
|
||
text = mjb_text_utf8("A", 1);
|
||
ATT_ASSERT_STATUS(mjb_convert_encoding(&text, MJB_MALFORMED_STOP,
|
||
MJB_ENC_UTF_16, &ascii_result, NULL),
|
||
MJB_STATUS_INVALID_ENCODING, "Convert generic UTF-16 output rejects unknown byte order");
|
||
|
||
MJB_TEST_COVERAGE(mjb_convert_encoding);
|
||
for(size_t from = 0; from < 5; ++from) {
|
||
for(size_t to = 0; to < 5; ++to) {
|
||
mjb_result convert_result;
|
||
text = mjb_text_make(hello_strings[from], hello_strings_sizes[from], encodings[from]);
|
||
mjb_status status = mjb_convert_encoding(&text, MJB_MALFORMED_STOP, encodings[to],
|
||
&convert_result, NULL);
|
||
|
||
snprintf(test_description, 64, "%s to %s", output_encodings[from],
|
||
output_encodings[to]);
|
||
ATT_ASSERT_STATUS(status, MJB_STATUS_OK, test_description);
|
||
|
||
if(from == to) {
|
||
snprintf(test_description, 64, "%s to %s is not owned", output_encodings[from],
|
||
output_encodings[to]);
|
||
ATT_ASSERT(convert_result.owned, false, test_description);
|
||
ATT_ASSERT((void *)convert_result.output, (void *)hello_strings[from],
|
||
test_description);
|
||
ATT_ASSERT(convert_result.output_size, hello_strings_sizes[from], test_description);
|
||
} else {
|
||
snprintf(test_description, 64, "%s to %s is owned", output_encodings[from],
|
||
output_encodings[to]);
|
||
ATT_ASSERT(convert_result.owned, true, test_description);
|
||
ATT_ASSERT(memcmp(convert_result.output, hello_strings[to],
|
||
convert_result.output_size),
|
||
0, test_description);
|
||
}
|
||
|
||
if(convert_result.owned) {
|
||
(void)mjb_result_free(&convert_result);
|
||
}
|
||
}
|
||
}
|
||
|
||
ATT_ASSERT_STATUS(api_md_examples(), MJB_STATUS_OK, "API MD examples");
|
||
}
|