cmake/Tests/CMakeLib/testUTF8.cxx

189 lines
4.7 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
2023-12-07 09:12:54 +01:00
#include <cm/string_view>
2010-03-17 14:00:29 +02:00
#include <stdio.h>
2020-02-01 23:06:01 +01:00
#include <cm_utf8.h>
2023-12-07 09:12:54 +01:00
using test_utf8_char = const cm::string_view;
2010-03-17 14:00:29 +02:00
2023-12-07 09:12:54 +01:00
static void byte_array_print(test_utf8_char s)
2019-11-11 23:01:05 +01:00
{
bool started = false;
printf("[");
2023-12-07 09:12:54 +01:00
for (char c : s) {
2019-11-11 23:01:05 +01:00
if (started) {
printf(",");
}
started = true;
2023-12-07 09:12:54 +01:00
printf("0x%02X", static_cast<unsigned char>(c));
2019-11-11 23:01:05 +01:00
}
printf("]");
}
2010-03-17 14:00:29 +02:00
struct test_utf8_entry
{
test_utf8_char str;
unsigned int chr;
};
static test_utf8_entry const good_entry[] = {
2023-12-07 09:12:54 +01:00
{ "\x20", 0x0020 }, /* Space. */
{ "\xC2\xA9", 0x00A9 }, /* Copyright. */
{ "\xE2\x80\x98", 0x2018 }, /* Open-single-quote. */
{ "\xE2\x80\x99", 0x2019 }, /* Close-single-quote. */
{ "\xF0\xA3\x8E\xB4", 0x233B4 }, /* Example from RFC 3629. */
{ "\xED\x80\x80", 0xD000 }, /* Valid 0xED prefixed codepoint. */
{ "\xF4\x8F\xBF\xBF", 0x10FFFF }, /* Highest valid RFC codepoint. */
{ {}, 0 }
2010-03-17 14:00:29 +02:00
};
static test_utf8_char const bad_chars[] = {
2023-12-07 09:12:54 +01:00
"\x80", /* Leading continuation byte. */
"\xC0\x80", /* Overlong encoding. */
"\xC1\x80", /* Overlong encoding. */
"\xC2", /* Missing continuation byte. */
"\xE0", /* Missing continuation bytes. */
"\xE0\x80\x80", /* Overlong encoding. */
2019-11-11 23:01:05 +01:00
"\xF0\x80\x80\x80", /* Overlong encoding. */
2023-12-07 09:12:54 +01:00
"\xED\xA0\x80", /* UTF-16 surrogate half. */
"\xED\xBF\xBF", /* UTF-16 surrogate half. */
2019-11-11 23:01:05 +01:00
"\xF4\x90\x80\x80", /* Lowest out-of-range codepoint. */
"\xF5\x80\x80\x80", /* Prefix forces out-of-range codepoints. */
2023-12-07 09:12:54 +01:00
{}
2019-11-11 23:01:05 +01:00
};
static char const* good_strings[] = { "", "ASCII", "\xC2\xA9 Kitware", 0 };
static char const* bad_strings[] = {
"\xC0\x80", /* Modified UTF-8 for embedded 0-byte. */
0
2010-03-17 14:00:29 +02:00
};
2023-12-07 09:12:54 +01:00
static void report_good(bool passed, test_utf8_char c)
2010-03-17 14:00:29 +02:00
{
2016-07-09 11:21:54 +02:00
printf("%s: decoding good ", passed ? "pass" : "FAIL");
2023-12-07 09:12:54 +01:00
byte_array_print(c);
printf(" (%s) ", c.data());
2010-03-17 14:00:29 +02:00
}
2023-12-07 09:12:54 +01:00
static void report_bad(bool passed, test_utf8_char c)
2010-03-17 14:00:29 +02:00
{
2016-07-09 11:21:54 +02:00
printf("%s: decoding bad ", passed ? "pass" : "FAIL");
2023-12-07 09:12:54 +01:00
byte_array_print(c);
2010-03-17 14:00:29 +02:00
printf(" ");
}
2023-12-07 09:12:54 +01:00
static bool decode_good(test_utf8_entry const& entry)
2010-03-17 14:00:29 +02:00
{
2023-12-07 09:12:54 +01:00
const auto& s = entry.str;
2010-03-17 14:00:29 +02:00
unsigned int uc;
2016-07-09 11:21:54 +02:00
if (const char* e =
2023-12-07 09:12:54 +01:00
cm_utf8_decode_character(s.data(), s.data() + s.size(), &uc)) {
int used = static_cast<int>(e - s.data());
2016-07-09 11:21:54 +02:00
if (uc != entry.chr) {
2023-12-07 09:12:54 +01:00
report_good(false, s);
2010-03-17 14:00:29 +02:00
printf("expected 0x%04X, got 0x%04X\n", entry.chr, uc);
return false;
2016-07-09 11:21:54 +02:00
}
2023-12-07 09:12:54 +01:00
if (used != int(s.size())) {
report_good(false, s);
printf("had %d bytes, used %d\n", int(s.size()), used);
2010-03-17 14:00:29 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
2023-12-07 09:12:54 +01:00
report_good(true, s);
2010-03-17 14:00:29 +02:00
printf("got 0x%04X\n", uc);
return true;
2016-07-09 11:21:54 +02:00
}
2023-12-07 09:12:54 +01:00
report_good(false, s);
2010-03-17 14:00:29 +02:00
printf("failed\n");
return false;
}
2023-12-07 09:12:54 +01:00
static bool decode_bad(test_utf8_char s)
2010-03-17 14:00:29 +02:00
{
unsigned int uc = 0xFFFFu;
2023-12-07 09:12:54 +01:00
const char* e = cm_utf8_decode_character(s.data(), s.data() + s.size(), &uc);
2016-07-09 11:21:54 +02:00
if (e) {
2010-03-17 14:00:29 +02:00
report_bad(false, s);
printf("expected failure, got 0x%04X\n", uc);
return false;
2016-07-09 11:21:54 +02:00
}
2010-03-17 14:00:29 +02:00
report_bad(true, s);
printf("failed as expected\n");
return true;
}
2023-12-07 09:12:54 +01:00
static void report_valid(bool passed, test_utf8_char s)
2019-11-11 23:01:05 +01:00
{
printf("%s: validity good ", passed ? "pass" : "FAIL");
byte_array_print(s);
2023-12-07 09:12:54 +01:00
printf(" (%s) ", s.data());
2019-11-11 23:01:05 +01:00
}
2023-12-07 09:12:54 +01:00
static void report_invalid(bool passed, test_utf8_char s)
2019-11-11 23:01:05 +01:00
{
printf("%s: validity bad ", passed ? "pass" : "FAIL");
byte_array_print(s);
printf(" ");
}
2023-12-07 09:12:54 +01:00
static bool is_valid(test_utf8_char s)
2019-11-11 23:01:05 +01:00
{
2023-12-07 09:12:54 +01:00
bool valid = cm_utf8_is_valid(s.data()) != 0;
2019-11-11 23:01:05 +01:00
if (!valid) {
report_valid(false, s);
printf("expected valid, reported as invalid\n");
return false;
}
report_valid(true, s);
printf("valid as expected\n");
return true;
}
2023-12-07 09:12:54 +01:00
static bool is_invalid(test_utf8_char s)
2019-11-11 23:01:05 +01:00
{
2023-12-07 09:12:54 +01:00
bool valid = cm_utf8_is_valid(s.data()) != 0;
2019-11-11 23:01:05 +01:00
if (valid) {
report_invalid(false, s);
printf("expected invalid, reported as valid\n");
return false;
}
report_invalid(true, s);
printf("invalid as expected\n");
return true;
}
2023-05-23 16:38:00 +02:00
int testUTF8(int /*unused*/, char* /*unused*/[])
2010-03-17 14:00:29 +02:00
{
int result = 0;
2023-12-07 09:12:54 +01:00
for (test_utf8_entry const* e = good_entry; !e->str.empty(); ++e) {
2016-07-09 11:21:54 +02:00
if (!decode_good(*e)) {
2010-03-17 14:00:29 +02:00
result = 1;
}
2019-11-11 23:01:05 +01:00
if (!is_valid(e->str)) {
result = 1;
}
2016-07-09 11:21:54 +02:00
}
2023-12-07 09:12:54 +01:00
for (test_utf8_char* c = bad_chars; !(*c).empty(); ++c) {
2016-07-09 11:21:54 +02:00
if (!decode_bad(*c)) {
2010-03-17 14:00:29 +02:00
result = 1;
}
2019-11-11 23:01:05 +01:00
if (!is_invalid(*c)) {
result = 1;
}
}
for (char const** s = good_strings; *s; ++s) {
if (!is_valid(*s)) {
result = 1;
}
}
for (char const** s = bad_strings; *s; ++s) {
if (!is_invalid(*s)) {
result = 1;
}
2016-07-09 11:21:54 +02:00
}
2010-03-17 14:00:29 +02:00
return result;
}