/* * splinter -- UTF-8 to ASCII for the screen. * * The vendored curses mangles anything above 0x7f, so text is transliterated * before drawing. The regression this test exists for: a hand-written table * turned every character it did not know into '?', so a German answer full of * typographic punctuation ("Gedankenstrich — Anfuehrungszeichen") arrived as a * row of question marks. */ #include #include #include "ui.h" static int failures; #define CHECK(cond) do { \ if (!(cond)) { \ fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \ failures++; \ } \ } while (0) static int no_questions(const char *s) { return strchr(s, '?') == NULL; } int main(void) { char out[256]; /* ASCII survives untouched */ CHECK(sp_ui_ascii("hallo welt", out, sizeof(out)) == 0); CHECK(strcmp(out, "hallo welt") == 0); /* umlauts become something readable, not blocks */ CHECK(sp_ui_ascii("grün weiß schön", out, sizeof(out)) == 0); CHECK(no_questions(out)); CHECK(strstr(out, "gr") != NULL && strstr(out, "sch") != NULL); /* the typographic punctuation that used to produce ????? */ CHECK(sp_ui_ascii("ein — Gedankenstrich, „Zitat“, Auslassung…", out, sizeof(out)) == 0); CHECK(no_questions(out)); CHECK(strstr(out, "Gedankenstrich") != NULL); CHECK(strstr(out, "Zitat") != NULL); /* an ellipsis is not dropped silently */ CHECK(sp_ui_ascii("warten…", out, sizeof(out)) == 0); CHECK(strlen(out) >= 6); /* something genuinely unrepresentable becomes one '?', not a crash */ CHECK(sp_ui_ascii("a\U0001F600b", out, sizeof(out)) == 0); CHECK(strlen(out) >= 2); CHECK(strchr(out, 'a') != NULL && strchr(out, 'b') != NULL); /* empty and NULL are handled */ CHECK(sp_ui_ascii("", out, sizeof(out)) == 0); CHECK(strcmp(out, "") == 0); CHECK(sp_ui_ascii(NULL, out, sizeof(out)) == -1); /* a too-small buffer reports failure instead of writing past its end */ { char small[8]; (void)sp_ui_ascii("abcdefghijklmnop", small, sizeof(small)); CHECK(strlen(small) < sizeof(small)); } /* * ---- one character at a time, as the history window draws it ---- * A three- or four-byte character used to be copied into a char[2]. * Under -fsanitize=address this block is what catches it. */ { const char *dash = "\xe2\x80\x94x"; /* em dash, 3 bytes */ const char *ell = "\xe2\x80\xa6"; /* ellipsis */ const char *emoji = "\xf0\x9f\x98\x80y"; /* U+1F600, 4 bytes */ const char *cut = "\xf0\x9f"; /* truncated */ char one[8]; CHECK(sp_ui_ascii_char(dash, one, sizeof(one)) == 3); CHECK(one[0] != '\0' && strlen(one) < sizeof(one)); CHECK(sp_ui_ascii_char(ell, one, sizeof(one)) == 3); CHECK(no_questions(one)); CHECK(sp_ui_ascii_char(emoji, one, sizeof(one)) == 4); CHECK(sp_ui_ascii_char(emoji + 4, one, sizeof(one)) == 1); CHECK(strcmp(one, "y") == 0); CHECK(sp_ui_ascii_char(cut, one, sizeof(one)) == 2); CHECK(sp_ui_ascii_char("", one, sizeof(one)) == 0); CHECK(sp_ui_ascii_char("\xc3\xbc", one, sizeof(one)) == 2); } if (failures == 0) printf("test_ascii: ok\n"); return failures == 0 ? 0 : 1; }