From 9c8a4ff7e117ba052b2957c7e3f2e8751e8f8970 Mon Sep 17 00:00:00 2001 From: John-Mark Bell Date: Sun, 13 Jan 2013 02:05:46 +0000 Subject: Transliteration fixes: * Clear any substitution if codec reset has been requested. * Don't report memory exhaustion when failing to allocate space for the test conversion in translit_try_sequence: there's nothing the caller can do, so treat it as if the substitution cannot be converted to the target character set. * Correctly report success if we run out of input immediately following a flush of a substitution. Additional tests for transliteration. --- test/translit.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) (limited to 'test/translit.c') diff --git a/test/translit.c b/test/translit.c index 8f17889..240f5e2 100644 --- a/test/translit.c +++ b/test/translit.c @@ -20,7 +20,15 @@ typedef struct translit_testcase { } translit_testcase; static const translit_testcase tests[] = { + /* Trivial */ { "iso-8859-1//TRANSLIT", "\xe2\x80\x93", "-" }, + /* Multi-character replacements */ + { "iso-8859-2//TRANSLIT", "\xc2\xa9", "(c)" }, + { "iso-8859-3//TRANSLIT", "\xc2\xab", "<<" }, + /* Multiple choices */ + { "iso-8859-4//TRANSLIT", "\xef\xac\x85", "st" }, + /* Default fallback */ + { "iso-8859-1//TRANSLIT", "\xef\xac\x87", "?" }, { NULL, NULL, NULL } }; @@ -53,6 +61,50 @@ static void run_tests(void) } } +static void test_translit_buffer_boundary(void) +{ + iconv_t cd; + char out[128]; + char *inp = (char *) "\xc2\xa9", *outp = out; + size_t inlen = strlen(inp), outlen; + size_t read; + + cd = iconv_open("iso-8859-2//TRANSLIT", "utf-8"); + assert(cd != (iconv_t) -1); + + outlen = 1; + read = iconv(cd, &inp, &inlen, &outp, &outlen); + assert(read == (size_t) -1); + assert(errno == E2BIG); + + /* Expect ( to appear in output */ + assert(outlen == 0); + assert(out[0] == '('); + + /* Try to write next output character */ + outlen = 1; + read = iconv(cd, &inp, &inlen, &outp, &outlen); + assert(read == (size_t) -1); + assert(errno == E2BIG); + + /* Expect "(c" in output */ + assert(outlen == 0); + assert(out[0] == '('); + assert(out[1] == 'c'); + + /* Flush through last character */ + outlen = 1; + read = iconv(cd, &inp, &inlen, &outp, &outlen); + assert(read == 0); + + /* Expect "(c)" in output, and all input read */ + assert(outlen == 0); + assert(memcmp(out, "(c)", 3) == 0); + assert(inlen == 0); + + iconv_close(cd); +} + int main(int argc, char **argv) { const char *ucpath; @@ -84,6 +136,7 @@ int main(int argc, char **argv) assert(iconv_initialise(aliases) == 1); run_tests(); + test_translit_buffer_boundary(); iconv_finalise(); -- cgit v1.2.3