/** * The Mojibake library * * This file is distributed under the MIT License. See LICENSE for details. */ #include #include #if defined(MJB_SHARED) #include "../src/mojibake.h" #else #include "../src/mojibake-internal.h" #endif #include "test.h" #if !defined(MJB_SHARED) static mjb_status test_output_writer(mjb_output *output, const void *context) { (void)context; mjb_status status = mjb_output_write(output, "abc", 3); if(status != MJB_STATUS_OK) { return status; } return mjb_output_codepoint(output, 0xE9, MJB_ENC_UTF_8); } // Write a chunk that overflows a capacity of 4 bytes, then one that would still fit. static mjb_status test_output_resume_writer(mjb_output *output, const void *context) { (void)context; mjb_status status = mjb_output_write(output, "ab", 2); if(status == MJB_STATUS_OK) { status = mjb_output_write(output, "cde", 3); } if(status == MJB_STATUS_OK) { status = mjb_output_write(output, "f", 1); } return status; } static mjb_status test_output_failure_writer(mjb_output *output, const void *context) { (void)output; (void)context; return MJB_STATUS_INVALID_ENCODING; } #endif // 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; } ATT_TEST(string) { mjb_encoding enc = MJB_ENC_UTF_8; #if !defined(MJB_SHARED) size_t output_index = 0; size_t output_size = 0; char output_input[] = "A"; ATT_ASSERT(mjb_string_output(NULL, NULL, 1, &output_index, &output_size), (char *)NULL, "String output rejects NULL input"); ATT_ASSERT(mjb_string_output(NULL, output_input, 1, NULL, &output_size), (char *)NULL, "String output rejects NULL output index"); ATT_ASSERT(mjb_string_output(NULL, output_input, 1, &output_index, NULL), (char *)NULL, "String output rejects NULL output size"); char overflow_output[1] = { 0 }; output_index = SIZE_MAX; output_size = sizeof(overflow_output); ATT_ASSERT(mjb_string_output(overflow_output, output_input, 1, &output_index, &output_size), (char *)NULL, "String output rejects an overflowing index"); output_index = 0; ATT_ASSERT(mjb_string_output(overflow_output, output_input, SIZE_MAX, &output_index, &output_size), (char *)NULL, "String output rejects an overflowing input size"); size_t into_size = 0; ATT_ASSERT_STATUS(mjb_output_into(NULL, &into_size, test_output_writer, NULL), MJB_STATUS_OK, "Output sink measures a transformation"); ATT_ASSERT(into_size, (size_t)5, "Output sink reports the required size"); char into_output[6] = { '#', '#', '#', '#', '#', '#' }; into_size = 4; ATT_ASSERT_STATUS(mjb_output_into(into_output, &into_size, test_output_writer, NULL), MJB_STATUS_OUTPUT_TOO_SMALL, "Output sink rejects insufficient capacity"); ATT_ASSERT(into_size, (size_t)5, "Output sink preserves the required size"); ATT_ASSERT(memcmp(into_output, "abc###", sizeof(into_output)), 0, "Output sink writes a prefix and does not split a codepoint"); char resumed_output[6] = { '#', '#', '#', '#', '#', '#' }; into_size = 4; ATT_ASSERT_STATUS(mjb_output_into(resumed_output, &into_size, test_output_resume_writer, NULL), MJB_STATUS_OUTPUT_TOO_SMALL, "Output sink rejects a chunk past its capacity"); ATT_ASSERT(into_size, (size_t)6, "Output sink counts every chunk after overflowing"); ATT_ASSERT(memcmp(resumed_output, "ab####", sizeof(resumed_output)), 0, "Output sink does not resume writing after overflowing"); into_size = 5; ATT_ASSERT_STATUS(mjb_output_into(into_output, &into_size, test_output_writer, NULL), MJB_STATUS_OK, "Output sink writes into exact capacity"); ATT_ASSERT(into_size, (size_t)5, "Output sink reports the written size"); ATT_ASSERT(memcmp(into_output, "abc\xC3\xA9", into_size), 0, "Output sink writes bytes and encoded codepoints"); ATT_ASSERT(into_output[5], '#', "Output sink does not write a terminator"); into_size = sizeof(into_output); ATT_ASSERT_STATUS(mjb_output_into(into_output, &into_size, test_output_failure_writer, NULL), MJB_STATUS_INVALID_ENCODING, "Output sink propagates writer errors"); ATT_ASSERT(into_size, (size_t)0, "Output sink clears the size after a writer error"); ATT_ASSERT_STATUS(mjb_output_into(NULL, NULL, test_output_writer, NULL), MJB_STATUS_INVALID_ARGUMENT, "Output sink rejects a NULL size"); ATT_ASSERT_STATUS(mjb_output_into(NULL, &into_size, NULL, NULL), MJB_STATUS_INVALID_ARGUMENT, "Output sink rejects a NULL writer"); mjb_output measured = { NULL, SIZE_MAX, 0, MJB_OUTPUT_MEASURE }; ATT_ASSERT_STATUS(mjb_output_write(&measured, "a", 1), MJB_STATUS_OVERFLOW, "Output sink detects size overflow"); measured.size = 0; ATT_ASSERT_STATUS(mjb_output_codepoint(&measured, 0xE9, MJB_ENC_ASCII), MJB_STATUS_UNSUPPORTED, "Output sink rejects an unrepresentable codepoint"); #endif MJB_TEST_COVERAGE(mjb_codepoint_count); ATT_ASSERT(count_codepoints("Hello", 5, enc), 5, "UTF-8 length: Hello"); ATT_ASSERT(count_codepoints("Hello", 4, enc), 4, "UTF-8 length: Hello"); ATT_ASSERT(count_codepoints("Hello", 3, enc), 3, "UTF-8 length: Hello"); ATT_ASSERT(count_codepoints("Hello", 2, enc), 2, "UTF-8 length: Hello"); ATT_ASSERT(count_codepoints("Hello", 1, enc), 1, "UTF-8 length: Hello"); ATT_ASSERT(count_codepoints("Hello", 0, enc), 0, "UTF-8 length: Hello"); ATT_ASSERT(count_codepoints(NULL, 0, enc), 0, "UTF-8 length: NULL"); ATT_ASSERT(count_codepoints("Héllö", 7, enc), 5, "UTF-8 length: Héllö"); ATT_ASSERT(count_codepoints("Héllö", 4, enc), 3, "UTF-8 length: Héllö"); ATT_ASSERT(count_codepoints("Héllö", 2, enc), SIZE_MAX, "UTF-8 length rejects a truncated sequence"); ATT_ASSERT(count_codepoints("Héllö", 0, enc), 0, "UTF-8 length: Héllö"); ATT_ASSERT(count_codepoints("Hèllõ ツ", 11, enc), 7, "UTF-8 length: Hèllõ ツ"); ATT_ASSERT(count_codepoints("Hèllõ ツ", 5, enc), 4, "UTF-8 length: Hèllõ ツ"); ATT_ASSERT(count_codepoints("こんにちは", 15, enc), 5, "UTF-8 length: こんにちは"); ATT_ASSERT(count_codepoints("Γειά σου", 15, enc), 8, "UTF-8 length: Γειά σου"); ATT_ASSERT(count_codepoints("Héllö", 1, enc), 1, "UTF-8 length: Héllö (1 max value)"); ATT_ASSERT(count_codepoints("Hello", 5, MJB_ENC_ASCII), 5, "ASCII length: Hello"); size_t codepoint_count = 6251; mjb_text count_text = mjb_text_utf8("A", 1); ATT_ASSERT_STATUS(mjb_codepoint_count(&count_text, MJB_MALFORMED_STOP, NULL, NULL), MJB_STATUS_INVALID_ARGUMENT, "Codepoint count rejects NULL count"); count_text = mjb_text_utf8(NULL, 1); ATT_ASSERT_STATUS(mjb_codepoint_count(&count_text, MJB_MALFORMED_STOP, &codepoint_count, NULL), MJB_STATUS_INVALID_ARGUMENT, "Codepoint count rejects NULL buffer"); ATT_ASSERT(codepoint_count, (size_t)0, "Codepoint count is zero after NULL buffer"); codepoint_count = 6251; ATT_ASSERT_STATUS(mjb_codepoint_count(NULL, MJB_MALFORMED_STOP, &codepoint_count, NULL), MJB_STATUS_INVALID_ARGUMENT, "Codepoint count rejects NULL text"); ATT_ASSERT(codepoint_count, (size_t)0, "Codepoint count is zero after NULL text"); codepoint_count = 6251; count_text = mjb_text_utf8("A", 1); ATT_ASSERT_STATUS(mjb_codepoint_count(&count_text, (mjb_malformed_policy)99, &codepoint_count, NULL), MJB_STATUS_INVALID_ARGUMENT, "Codepoint count rejects an invalid malformed policy"); codepoint_count = 6251; count_text = mjb_text_make("Hello", 5, MJB_ENC_UNKNOWN); ATT_ASSERT_STATUS(mjb_codepoint_count(&count_text, MJB_MALFORMED_STOP, &codepoint_count, NULL), MJB_STATUS_INVALID_ENCODING, "Codepoint count rejects an unknown encoding"); ATT_ASSERT(codepoint_count, (size_t)0, "Codepoint count is zero after invalid encoding"); enc = MJB_ENC_UTF_16LE; const char utf16le_hello[] = "H\0e\0l\0l\0o\0"; ATT_ASSERT(count_codepoints(utf16le_hello, 10, enc), 5, "UTF-16LE length: Hello"); ATT_ASSERT(count_codepoints(utf16le_hello, 8, enc), 4, "UTF-16LE length: Hello"); ATT_ASSERT(count_codepoints(utf16le_hello, 6, enc), 3, "UTF-16LE length: Hello"); ATT_ASSERT(count_codepoints(utf16le_hello, 4, enc), 2, "UTF-16LE length: Hello"); ATT_ASSERT(count_codepoints(utf16le_hello, 2, enc), 1, "UTF-16LE length: Hello"); ATT_ASSERT(count_codepoints(utf16le_hello, 0, enc), 0, "UTF-16LE length: Hello"); ATT_ASSERT(count_codepoints(NULL, 0, enc), 0, "UTF-16LE length: NULL"); const char utf16le_hello_accents[] = "H\0\xE9\0l\0l\0\xF6\0"; ATT_ASSERT(count_codepoints(utf16le_hello_accents, 10, enc), 5, "UTF-16LE length: Héllö"); ATT_ASSERT(count_codepoints(utf16le_hello_accents, 8, enc), 4, "UTF-16LE length: Héllö"); ATT_ASSERT(count_codepoints(utf16le_hello_accents, 4, enc), 2, "UTF-16LE length: Héllö"); ATT_ASSERT(count_codepoints(utf16le_hello_accents, 0, enc), 0, "UTF-16LE length: Héllö"); const char utf16le_hello_katakana[] = "H\0\xE8\0l\0l\0\xF5\0 \0\x30\x30"; ATT_ASSERT(count_codepoints(utf16le_hello_katakana, 14, enc), 7, "UTF-16LE length: Hèllõ ツ"); ATT_ASSERT(count_codepoints(utf16le_hello_katakana, 10, enc), 5, "UTF-16LE length: Hèllõ ツ"); const char utf16le_konnichiwa[] = "\x93\x30\x93\x30\x6B\x30\x61\x30\x6F\x30"; ATT_ASSERT(count_codepoints(utf16le_konnichiwa, 10, enc), 5, "UTF-16LE length: こんにちは"); const char utf16le_geia_sou[] = "\x93\x03\x65\x03\x69\x03\x3F\x03 \0\xC3\x03\x6F\x03\x75\x03"; ATT_ASSERT(count_codepoints(utf16le_geia_sou, 16, enc), 8, "UTF-16LE length: Γειά σου"); ATT_ASSERT(count_codepoints(utf16le_hello_accents, 2, enc), 1, "UTF-16LE length: Héllö (1 max value)"); enc = MJB_ENC_UTF_16BE; const char utf16be_hello[] = "\0H\0e\0l\0l\0o"; ATT_ASSERT(count_codepoints(utf16be_hello, 10, enc), 5, "UTF-16BE length: Hello"); ATT_ASSERT(count_codepoints(utf16be_hello, 8, enc), 4, "UTF-16BE length: Hello"); ATT_ASSERT(count_codepoints(utf16be_hello, 6, enc), 3, "UTF-16BE length: Hello"); ATT_ASSERT(count_codepoints(utf16be_hello, 4, enc), 2, "UTF-16BE length: Hello"); ATT_ASSERT(count_codepoints(utf16be_hello, 2, enc), 1, "UTF-16BE length: Hello"); ATT_ASSERT(count_codepoints(utf16be_hello, 0, enc), 0, "UTF-16BE length: Hello"); ATT_ASSERT(count_codepoints(NULL, 0, enc), 0, "UTF-16BE length: NULL"); const char utf16be_hello_accents[] = "\0H\0\xE9\0l\0l\0\xF6"; ATT_ASSERT(count_codepoints(utf16be_hello_accents, 10, enc), 5, "UTF-16BE length: Héllö"); ATT_ASSERT(count_codepoints(utf16be_hello_accents, 8, enc), 4, "UTF-16BE length: Héllö"); ATT_ASSERT(count_codepoints(utf16be_hello_accents, 4, enc), 2, "UTF-16BE length: Héllö"); ATT_ASSERT(count_codepoints(utf16be_hello_accents, 0, enc), 0, "UTF-16BE length: Héllö"); const char utf16be_hello_katakana[] = "\0H\0\xE8\0l\0l\0\xF5\0 \x30\x30"; ATT_ASSERT(count_codepoints(utf16be_hello_katakana, 14, enc), 7, "UTF-16BE length: Hèllõ ツ"); ATT_ASSERT(count_codepoints(utf16be_hello_katakana, 10, enc), 5, "UTF-16BE length: Hèllõ ツ"); const char utf16be_konnichiwa[] = "\x30\x93\x30\x93\x30\x6B\x30\x61\x30\x6F"; ATT_ASSERT(count_codepoints(utf16be_konnichiwa, 10, enc), 5, "UTF-16BE length: こんにちは"); const char utf16be_geia_sou[] = "\x03\x93\x03\x65\x03\x69\x03\x3F\0 \x03\xC3\x03\x6F\x03\x75"; ATT_ASSERT(count_codepoints(utf16be_geia_sou, 16, enc), 8, "UTF-16BE length: Γειά σου"); ATT_ASSERT(count_codepoints(utf16be_hello_accents, 2, enc), 1, "UTF-16BE length: Héllö (1 max value)"); enc = MJB_ENC_UTF_32LE; const char utf32le_hello[] = "H\0\0\0e\0\0\0l\0\0\0l\0\0\0o\0\0\0"; ATT_ASSERT(count_codepoints(utf32le_hello, 20, enc), 5, "UTF-32LE length: Hello"); ATT_ASSERT(count_codepoints(utf32le_hello, 16, enc), 4, "UTF-32LE length: Hello"); ATT_ASSERT(count_codepoints(utf32le_hello, 12, enc), 3, "UTF-32LE length: Hello"); ATT_ASSERT(count_codepoints(utf32le_hello, 8, enc), 2, "UTF-32LE length: Hello"); ATT_ASSERT(count_codepoints(utf32le_hello, 4, enc), 1, "UTF-32LE length: Hello"); ATT_ASSERT(count_codepoints(utf32le_hello, 0, enc), 0, "UTF-32LE length: Hello"); ATT_ASSERT(count_codepoints(NULL, 0, enc), 0, "UTF-32LE length: NULL"); const char utf32le_hello_accents[] = "H\0\0\0\xE9\0\0\0l\0\0\0l\0\0\0\xF6\0\0\0"; ATT_ASSERT(count_codepoints(utf32le_hello_accents, 20, enc), 5, "UTF-32LE length: Héllö"); ATT_ASSERT(count_codepoints(utf32le_hello_accents, 16, enc), 4, "UTF-32LE length: Héllö"); ATT_ASSERT(count_codepoints(utf32le_hello_accents, 8, enc), 2, "UTF-32LE length: Héllö"); ATT_ASSERT(count_codepoints(utf32le_hello_accents, 0, enc), 0, "UTF-32LE length: Héllö"); const char utf32le_hello_katakana[] = "H\0\0\0\xE8\0\0\0l\0\0\0l\0\0\0\xF5\0\0\0 \0\0\0\x30\x30\0\0"; ATT_ASSERT(count_codepoints(utf32le_hello_katakana, 28, enc), 7, "UTF-32LE length: Hèllõ ツ"); ATT_ASSERT(count_codepoints(utf32le_hello_katakana, 20, enc), 5, "UTF-32LE length: Hèllõ ツ"); const char utf32le_konnichiwa[] = "\x93\x30\0\0\x93\x30\0\0\x6B\x30\0\0\x61\x30\0\0\x6F\x30\0\0"; ATT_ASSERT(count_codepoints(utf32le_konnichiwa, 20, enc), 5, "UTF-32LE length: こんにちは"); const char utf32le_geia_sou[] = "\x93\x03\0\0\x65\x03\0\0\x69\x03\0\0\x3F\x03\0\0 \0\0\0" "\xC3\x03\0\0\x6F\x03\0\0\x75\x03\0\0"; ATT_ASSERT(count_codepoints(utf32le_geia_sou, 32, enc), 8, "UTF-32LE length: Γειά σου"); ATT_ASSERT(count_codepoints(utf32le_hello_accents, 4, enc), 1, "UTF-32LE length: Héllö (1 max value)"); enc = MJB_ENC_UTF_32BE; const char utf32be_hello[] = "\0\0\0H\0\0\0e\0\0\0l\0\0\0l\0\0\0o"; ATT_ASSERT(count_codepoints(utf32be_hello, 20, enc), 5, "UTF-32BE length: Hello"); ATT_ASSERT(count_codepoints(utf32be_hello, 16, enc), 4, "UTF-32BE length: Hello"); ATT_ASSERT(count_codepoints(utf32be_hello, 12, enc), 3, "UTF-32BE length: Hello"); ATT_ASSERT(count_codepoints(utf32be_hello, 8, enc), 2, "UTF-32BE length: Hello"); ATT_ASSERT(count_codepoints(utf32be_hello, 4, enc), 1, "UTF-32BE length: Hello"); ATT_ASSERT(count_codepoints(utf32be_hello, 0, enc), 0, "UTF-32BE length: Hello"); ATT_ASSERT(count_codepoints(NULL, 0, enc), 0, "UTF-32BE length: NULL"); const char utf32be_hello_accents[] = "\0\0\0H\0\0\0\xE9\0\0\0l\0\0\0l\0\0\0\xF6"; ATT_ASSERT(count_codepoints(utf32be_hello_accents, 20, enc), 5, "UTF-32BE length: Héllö"); ATT_ASSERT(count_codepoints(utf32be_hello_accents, 16, enc), 4, "UTF-32BE length: Héllö"); ATT_ASSERT(count_codepoints(utf32be_hello_accents, 8, enc), 2, "UTF-32BE length: Héllö"); ATT_ASSERT(count_codepoints(utf32be_hello_accents, 0, enc), 0, "UTF-32BE length: Héllö"); const char utf32be_hello_katakana[] = "\0\0\0H\0\0\0\xE8\0\0\0l\0\0\0l\0\0\0\xF5\0\0\0 \0\0\x30\x30"; ATT_ASSERT(count_codepoints(utf32be_hello_katakana, 28, enc), 7, "UTF-32BE length: Hèllõ ツ"); ATT_ASSERT(count_codepoints(utf32be_hello_katakana, 20, enc), 5, "UTF-32BE length: Hèllõ ツ"); const char utf32be_konnichiwa[] = "\0\0\x30\x93\0\0\x30\x93\0\0\x30\x6B\0\0\x30\x61\0\0\x30\x6F"; ATT_ASSERT(count_codepoints(utf32be_konnichiwa, 20, enc), 5, "UTF-32BE length: こんにちは"); const char utf32be_geia_sou[] = "\0\0\x03\x93\0\0\x03\x65\0\0\x03\x69\0\0\x03\x3F\0\0\0 " "\0\0\x03\xC3\0\0\x03\x6F\0\0\x03\x75"; ATT_ASSERT(count_codepoints(utf32be_geia_sou, 32, enc), 8, "UTF-32BE length: Γειά σου"); ATT_ASSERT(count_codepoints(utf32be_hello_accents, 4, enc), 1, "UTF-32BE length: Héllö (1 max value)"); }