/* * test_text.c -- unit tests for UTF-8, IRC formatting and display conversion * * License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. */ #include "dc_internal.h" #include #include #include static int fails; #define CHECK(cond) do { \ if (!(cond)) { \ fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, \ #cond); \ fails++; \ } \ } while (0) static int all_ascii(const char *s) { while (*s != '\0') { if ((unsigned char)*s >= 0x80) return 0; s++; } return 1; } static void test_utf8(void) { CHECK(dc_text_valid_utf8("hello")); CHECK(dc_text_valid_utf8("Gr\xc3\xbc\xc3\x9f" "e")); CHECK(!dc_text_valid_utf8("\xc0\xaf")); CHECK(!dc_text_valid_utf8("\xed\xa0\x80")); CHECK(!dc_text_valid_utf8("\xc3")); CHECK(dc_text_valid_utf8("")); } static void test_irc(void) { char *s; s = dc_text_irc_strip("\x02" "bold" "\x02" " " "\x1d" "ital" "\x1d" " " "\x1f" "under" "\x1f" " " "\x16" "rev" "\x16" " " "\x0f" "reset"); CHECK(s != NULL); if (s != NULL) { CHECK(strcmp(s, "bold ital under rev reset") == 0); free(s); } s = dc_text_irc_strip("a\x03" "0,1b\x03" "c\x03" "12,34d\x03"); CHECK(s != NULL); if (s != NULL) { CHECK(strcmp(s, "abcd") == 0); free(s); } s = dc_text_irc_strip("\x02Gr\xc3\xbc\xc3\x9f" "e\x02"); CHECK(s != NULL); if (s != NULL) { CHECK(strcmp(s, "Gr\xc3\xbc\xc3\x9f" "e") == 0); free(s); } s = dc_text_irc_strip("\x03"); CHECK(s != NULL); if (s != NULL) { CHECK(strcmp(s, "") == 0); free(s); } CHECK(dc_text_irc_strip(NULL) == NULL); } static void test_clean(void) { char *s = dc_text_clean("a\r\nb\rc\x07" "d\te\x7f"); CHECK(s != NULL); if (s != NULL) { CHECK(strcmp(s, "a\nb\ncd\te") == 0); free(s); } CHECK(dc_text_clean(NULL) == NULL); } static void test_display(void) { char *s; s = dc_text_to_display("hello", "UTF-8"); CHECK(s != NULL); if (s != NULL) { CHECK(strcmp(s, "hello") == 0); free(s); } s = dc_text_to_display("Gr\xc3\xbc\xc3\x9f" "e", "ISO-8859-1"); CHECK(s != NULL); if (s != NULL) { CHECK(strcmp(s, "Gr\xfc\xdf" "e") == 0); free(s); } s = dc_text_to_display("Gr\xc3\xbc\xc3\x9f" "e", "ASCII"); CHECK(s != NULL); if (s != NULL) { CHECK(all_ascii(s)); free(s); } s = dc_text_to_display("Gr\xc3\xbc\xc3\x9f" "e", "NO-SUCH-CHARSET"); CHECK(s != NULL); if (s != NULL) { CHECK(all_ascii(s)); free(s); } s = dc_text_to_display("\xc0\xaf", "UTF-8"); CHECK(s != NULL); if (s != NULL) { CHECK(strcmp(s, "?") == 0); free(s); } s = dc_text_to_display(NULL, "UTF-8"); CHECK(s == NULL); s = dc_text_to_display("x", NULL); CHECK(s != NULL); if (s != NULL) { CHECK(strcmp(s, "x") == 0); free(s); } } int main(void) { test_utf8(); test_irc(); test_clean(); test_display(); if (fails != 0) { fprintf(stderr, "%d test(s) failed\n", fails); return 1; } printf("test_text: all ok\n"); return 0; }