From f1b3dff4c2075d9304dd4c298db433c7d404f3a3 Mon Sep 17 00:00:00 2001 From: Paul LeoNerd Evans Date: Thu, 31 Mar 2011 23:42:52 +0100 Subject: Extract UTF-8 sequence parser into its own function, fix bugs, apply unit tests --- t/03utf8.c | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 92 insertions(+), 1 deletion(-) (limited to 't') diff --git a/t/03utf8.c b/t/03utf8.c index d4d8da3..34fce6f 100644 --- a/t/03utf8.c +++ b/t/03utf8.c @@ -7,7 +7,7 @@ int main(int argc, char *argv[]) TermKey *tk; TermKeyKey key; - plan_tests(21); + plan_tests(57); pipe(fd); @@ -72,6 +72,97 @@ int main(int argc, char *argv[]) is_int(key.type, TERMKEY_TYPE_UNICODE, "key.type UTF-8 4 high"); is_int(key.code.number, 0x10FFFF, "key.code.number UTF-8 4 high"); + /* Invalid continuations */ + + write(fd[1], "\xC2!", 2); + + termkey_advisereadable(tk); + is_int(termkey_getkey(tk, &key), TERMKEY_RES_KEY, "getkey yields RES_KEY UTF-8 2 invalid cont"); + is_int(key.code.number, 0xFFFD, "key.code.number UTF-8 2 invalid cont"); + is_int(termkey_getkey(tk, &key), TERMKEY_RES_KEY, "getkey yields RES_KEY UTF-8 2 invalid after"); + is_int(key.code.number, '!', "key.code.number UTF-8 2 invalid after"); + + write(fd[1], "\xE0!", 2); + + termkey_advisereadable(tk); + is_int(termkey_getkey(tk, &key), TERMKEY_RES_KEY, "getkey yields RES_KEY UTF-8 3 invalid cont"); + is_int(key.code.number, 0xFFFD, "key.code.number UTF-8 3 invalid cont"); + is_int(termkey_getkey(tk, &key), TERMKEY_RES_KEY, "getkey yields RES_KEY UTF-8 3 invalid after"); + is_int(key.code.number, '!', "key.code.number UTF-8 3 invalid after"); + + write(fd[1], "\xE0\xA0!", 3); + + termkey_advisereadable(tk); + is_int(termkey_getkey(tk, &key), TERMKEY_RES_KEY, "getkey yields RES_KEY UTF-8 3 invalid cont 2"); + is_int(key.code.number, 0xFFFD, "key.code.number UTF-8 3 invalid cont 2"); + is_int(termkey_getkey(tk, &key), TERMKEY_RES_KEY, "getkey yields RES_KEY UTF-8 3 invalid after"); + is_int(key.code.number, '!', "key.code.number UTF-8 3 invalid after"); + + write(fd[1], "\xF0!", 2); + + termkey_advisereadable(tk); + is_int(termkey_getkey(tk, &key), TERMKEY_RES_KEY, "getkey yields RES_KEY UTF-8 4 invalid cont"); + is_int(key.code.number, 0xFFFD, "key.code.number UTF-8 4 invalid cont"); + is_int(termkey_getkey(tk, &key), TERMKEY_RES_KEY, "getkey yields RES_KEY UTF-8 4 invalid after"); + is_int(key.code.number, '!', "key.code.number UTF-8 4 invalid after"); + + write(fd[1], "\xF0\x90!", 3); + + termkey_advisereadable(tk); + is_int(termkey_getkey(tk, &key), TERMKEY_RES_KEY, "getkey yields RES_KEY UTF-8 4 invalid cont 2"); + is_int(key.code.number, 0xFFFD, "key.code.number UTF-8 4 invalid cont 2"); + is_int(termkey_getkey(tk, &key), TERMKEY_RES_KEY, "getkey yields RES_KEY UTF-8 4 invalid after"); + is_int(key.code.number, '!', "key.code.number UTF-8 4 invalid after"); + + write(fd[1], "\xF0\x90\x80!", 4); + + termkey_advisereadable(tk); + is_int(termkey_getkey(tk, &key), TERMKEY_RES_KEY, "getkey yields RES_KEY UTF-8 4 invalid cont 3"); + is_int(key.code.number, 0xFFFD, "key.code.number UTF-8 4 invalid cont 3"); + is_int(termkey_getkey(tk, &key), TERMKEY_RES_KEY, "getkey yields RES_KEY UTF-8 4 invalid after"); + is_int(key.code.number, '!', "key.code.number UTF-8 4 invalid after"); + + /* Partials */ + + write(fd[1], "\xC2", 1); + termkey_advisereadable(tk); + is_int(termkey_getkey(tk, &key), TERMKEY_RES_AGAIN, "getkey yields RES_AGAIN UTF-8 2 partial"); + + write(fd[1], "\xA0", 1); + termkey_advisereadable(tk); + is_int(termkey_getkey(tk, &key), TERMKEY_RES_KEY, "getkey yields RES_KEY UTF-8 2 partial"); + is_int(key.code.number, 0x00A0, "key.code.number UTF-8 2 partial"); + + write(fd[1], "\xE0", 1); + termkey_advisereadable(tk); + is_int(termkey_getkey(tk, &key), TERMKEY_RES_AGAIN, "getkey yields RES_AGAIN UTF-8 3 partial"); + + write(fd[1], "\xA0", 1); + termkey_advisereadable(tk); + is_int(termkey_getkey(tk, &key), TERMKEY_RES_AGAIN, "getkey yields RES_AGAIN UTF-8 3 partial"); + + write(fd[1], "\x80", 1); + termkey_advisereadable(tk); + is_int(termkey_getkey(tk, &key), TERMKEY_RES_KEY, "getkey yields RES_KEY UTF-8 3 partial"); + is_int(key.code.number, 0x0800, "key.code.number UTF-8 3 partial"); + + write(fd[1], "\xF0", 1); + termkey_advisereadable(tk); + is_int(termkey_getkey(tk, &key), TERMKEY_RES_AGAIN, "getkey yields RES_AGAIN UTF-8 4 partial"); + + write(fd[1], "\x90", 1); + termkey_advisereadable(tk); + is_int(termkey_getkey(tk, &key), TERMKEY_RES_AGAIN, "getkey yields RES_AGAIN UTF-8 4 partial"); + + write(fd[1], "\x80", 1); + termkey_advisereadable(tk); + is_int(termkey_getkey(tk, &key), TERMKEY_RES_AGAIN, "getkey yields RES_AGAIN UTF-8 4 partial"); + + write(fd[1], "\x80", 1); + termkey_advisereadable(tk); + is_int(termkey_getkey(tk, &key), TERMKEY_RES_KEY, "getkey yields RES_KEY UTF-8 4 partial"); + is_int(key.code.number, 0x10000, "key.code.number UTF-8 4 partial"); + termkey_destroy(tk); return exit_status(); -- cgit v1.2.3-70-g09d2