Files
lotherk 83741ce6eb dtchat: add the conversation model and text helpers (T1.4)
conv.c: accounts, conversations, topics, idempotent add, a 2000-message ring buffer and unread counts that ignore our own/system messages. text.c: UTF-8 validation, C0 cleaning, mIRC formatting strip and iconv display with ASCII//TRANSLIT and '?' fallbacks. test_conv and test_text cover duplicate adds, ordering, the ring boundary, flags/unread, IRC codes, CR/CRLF and ISO-8859-1/ASCII output. make mcde-host exit=0; all six dtchat tests report 'all ok'; ASan/UBSan clean.
2026-11-16 17:18:12 +00:00

150 lines
2.9 KiB
C

/*
* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
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;
}