// test.c: the boundaries of Unicode table 3-7, for both versions. // // build/test.exe #include #include #include int utf8_valid_scalar(const uint8_t *buf, size_t len); int utf8_valid_avx2(const uint8_t *buf, size_t len); static const struct { const char *bytes; int valid; const char *what; } samples[] = { { "", 1, "nothing at all" }, { "plain ASCII", 1, "ASCII" }, { "\xC2\x80", 1, "U+0080, the first two-byte code point" }, { "\xDF\xBF", 1, "U+07FF, the last" }, { "\xE0\xA0\x80", 1, "U+0800, the first three-byte code point" }, { "\xED\x9F\xBF", 1, "U+D7FF, just below the surrogates" }, { "\xEE\x80\x80", 1, "U+E000, just above them" }, { "\xEF\xBF\xBF", 1, "U+FFFF" }, { "\xF0\x90\x80\x80", 1, "U+10000, the first four-byte code point" }, { "\xF4\x8F\xBF\xBF", 1, "U+10FFFF, the last code point" }, { "\xC0\x80", 0, "an overlong NUL" }, { "\xC1\xBF", 0, "an overlong U+007F" }, { "\xE0\x9F\xBF", 0, "an overlong U+07FF" }, { "\xED\xA0\x80", 0, "U+D800, a surrogate" }, { "\xED\xBF\xBF", 0, "U+DFFF, a surrogate" }, { "\xF0\x8F\xBF\xBF", 0, "an overlong U+FFFF" }, { "\xF4\x90\x80\x80", 0, "U+110000, past the end of Unicode" }, { "\xF5\x80\x80\x80", 0, "F5, which never appears" }, { "\xFF", 0, "FF, which never appears" }, { "\x80", 0, "a continuation with no lead" }, { "\xC3\xA9\xA9", 0, "one continuation too many" }, { "\xC3", 0, "a two-byte lead at the very end" }, { "\xC3" "A", 0, "a two-byte lead followed by ASCII" }, }; #define SAMPLES (sizeof samples / sizeof samples[0]) int main(void) { if (!__builtin_cpu_supports("avx2")) { puts("this CPU has no AVX2; nothing to test"); return 2; } int wrong = 0; for (size_t i = 0; i < SAMPLES; i++) { const uint8_t *p = (const uint8_t *)samples[i].bytes; size_t n = strlen(samples[i].bytes); int a = utf8_valid_scalar(p, n), b = utf8_valid_avx2(p, n); if (a == samples[i].valid && b == samples[i].valid) continue; wrong++; printf("wrong: %s: scalar %d, avx2 %d\n", samples[i].what, a, b); } printf("cases: %zu samples, %d wrong\n", SAMPLES, wrong); return wrong != 0; }