mirror of
https://github.com/zaerl/mojibake.git
synced 2026-11-30 16:48:26 +01:00
312 lines
15 KiB
C++
312 lines
15 KiB
C++
/**
|
|
* The Mojibake library
|
|
*
|
|
* This file is distributed under the MIT License. See LICENSE for details.
|
|
*/
|
|
|
|
#include "../../src/cpp/mojibake.hpp"
|
|
#include "../test.h"
|
|
|
|
#include <stdexcept>
|
|
#include <string>
|
|
|
|
ATT_TEST(cpp_mojibake) {
|
|
ATT_ASSERT(mjb::property_binary(U'A', MJB_PR_ALPHABETIC).value_or(false), true,
|
|
"C++ typed binary property");
|
|
ATT_ASSERT(mjb::property_int(U'A', MJB_PR_SCRIPT).value_or(0), MJB_SC_LATN,
|
|
"C++ typed enumerated property");
|
|
|
|
const auto script_extensions = mjb::script_extensions(U'\u30FC');
|
|
|
|
ATT_ASSERT(script_extensions.size(), 2u, "C++ Script_Extensions count");
|
|
ATT_ASSERT((int)script_extensions[0], MJB_SC_HIRA, "C++ Script_Extensions Hiragana");
|
|
ATT_ASSERT((int)script_extensions[1], MJB_SC_KANA, "C++ Script_Extensions Katakana");
|
|
|
|
#if MJB_FEATURE_SECURITY
|
|
const auto resolved_latin = mjb::resolved_script_set("hello123!");
|
|
ATT_ASSERT((int)resolved_latin.kind, MJB_SCRIPT_SET_RESOLVED,
|
|
"C++ resolved script set kind");
|
|
ATT_ASSERT(resolved_latin.scripts.size(), 1u, "C++ resolved script count");
|
|
ATT_ASSERT((int)resolved_latin.scripts[0], MJB_SC_LATN, "C++ resolved Latin script");
|
|
|
|
const auto resolved_mixed = mjb::resolved_script_set("h\xD0\xB5llo");
|
|
ATT_ASSERT(resolved_mixed.is_mixed_script(), true, "C++ mixed script set");
|
|
ATT_ASSERT(resolved_mixed.scripts.empty(), true, "C++ mixed script set has no scripts");
|
|
|
|
const auto resolved_all = mjb::resolved_script_set("123!");
|
|
ATT_ASSERT(resolved_all.is_all(), true, "C++ Common-only script set is ALL");
|
|
|
|
const auto resolved_japanese = mjb::resolved_script_set("\xE3\x81\xAD\xE3\x82\xAC");
|
|
ATT_ASSERT((int)resolved_japanese.kind, MJB_SCRIPT_SET_RESOLVED,
|
|
"C++ Japanese script set kind");
|
|
ATT_ASSERT(resolved_japanese.scripts.size(), 1u, "C++ Japanese script count");
|
|
ATT_ASSERT((int)resolved_japanese.scripts[0], MJB_SC_JPAN,
|
|
"C++ Japanese writing system");
|
|
#else
|
|
bool resolved_scripts_disabled = false;
|
|
|
|
try {
|
|
(void)mjb::resolved_script_set("hello");
|
|
} catch(const mjb::LibraryError &error) {
|
|
resolved_scripts_disabled = error.status() == MJB_STATUS_FEATURE_NOT_ENABLED;
|
|
}
|
|
|
|
ATT_ASSERT(resolved_scripts_disabled, true,
|
|
"C++ disabled resolved scripts report feature status");
|
|
#endif
|
|
|
|
// U+1F642 = 🙂
|
|
auto character = mjb::Character((mjb_codepoint)0x1F642);
|
|
// U+1F643 = 🙃
|
|
auto character2 = mjb::Character((mjb_codepoint)0x1F643);
|
|
// U+1F642 = 🙂
|
|
auto character3 = mjb::Character((mjb_codepoint)0x1F642);
|
|
|
|
#if MJB_FEATURE_CHARACTER_NAMES
|
|
ATT_ASSERT(std::string(character.name()), std::string("SLIGHTLY SMILING FACE"),
|
|
"Character::name");
|
|
#else
|
|
ATT_ASSERT(std::string(character.name()), std::string("Codepoint U+1F642"),
|
|
"Character::name placeholder without character names");
|
|
#endif
|
|
ATT_ASSERT((int)character.category(), MJB_CATEGORY_SO, "Character::category");
|
|
|
|
ATT_ASSERT(mjb::Character::from(0x1F642).has_value(), true, "Character::from valid codepoint");
|
|
ATT_ASSERT(mjb::Character::from(MJB_CODEPOINT_NOT_VALID).has_value(), false,
|
|
"Character::from invalid codepoint");
|
|
ATT_ASSERT(mjb::Character::from(0x1F642)->codepoint(), (mjb_codepoint)0x1F642,
|
|
"Character::from codepoint accessor");
|
|
|
|
ATT_ASSERT(character == character2, false, "Character::operator==");
|
|
ATT_ASSERT(character == character3, true, "Character::operator==");
|
|
ATT_ASSERT(character3 == character, true, "Character::operator==");
|
|
ATT_ASSERT(character3 != character2, true, "Character::operator!=");
|
|
|
|
// Identifier properties are free functions of the codepoint
|
|
// U+0041 = 'A': valid ID_Start and ID_Continue
|
|
ATT_ASSERT(mjb::is_id_start('A'), true, "A: is_id_start");
|
|
ATT_ASSERT(mjb::is_id_continue('A'), true, "A: is_id_continue");
|
|
ATT_ASSERT(mjb::is_xid_start('A'), true, "A: is_xid_start");
|
|
ATT_ASSERT(mjb::is_xid_continue('A'), true, "A: is_xid_continue");
|
|
ATT_ASSERT(mjb::is_pattern_syntax('A'), false, "A: is_pattern_syntax");
|
|
ATT_ASSERT(mjb::is_pattern_white_space('A'), false, "A: is_pattern_white_space");
|
|
|
|
// U+0021 = '!': pattern syntax
|
|
ATT_ASSERT(mjb::is_id_start('!'), false, "!: is_id_start");
|
|
ATT_ASSERT(mjb::is_pattern_syntax('!'), true, "!: is_pattern_syntax");
|
|
|
|
// is_identifier free function
|
|
ATT_ASSERT(mjb::is_identifier("hello"), true, "is_identifier: hello");
|
|
ATT_ASSERT(mjb::is_identifier("hello world"), false, "is_identifier: hello world");
|
|
ATT_ASSERT(mjb::is_identifier(""), false, "is_identifier: empty");
|
|
|
|
#if MJB_FEATURE_SECURITY
|
|
// is_confusable free function
|
|
ATT_ASSERT(mjb::confusable_match("\xD0\x90", "A"), true, "is_confusable: Cyrillic A / Latin A");
|
|
ATT_ASSERT(mjb::confusable_match("hello", "hello"), true, "is_confusable: same string");
|
|
ATT_ASSERT(mjb::confusable_match("a", "b"), false, "is_confusable: a / b");
|
|
ATT_ASSERT(mjb::confusable_skeleton("h\xD0\xB5llo"), std::string("hello"),
|
|
"confusable_skeleton");
|
|
#else
|
|
bool confusables_disabled = false;
|
|
|
|
try {
|
|
(void)mjb::confusable_match("a", "b");
|
|
} catch(const mjb::LibraryError &error) {
|
|
confusables_disabled = error.status() == MJB_STATUS_FEATURE_NOT_ENABLED;
|
|
}
|
|
|
|
ATT_ASSERT(confusables_disabled, true, "C++ disabled confusables report feature status");
|
|
#endif
|
|
|
|
#if MJB_FEATURE_IDNA
|
|
const auto idna_ascii = mjb::idna_to_ascii("b\xC3\xBC"
|
|
"cher.example");
|
|
ATT_ASSERT(idna_ascii.valid(), true, "C++ IDNA ToASCII valid");
|
|
ATT_ASSERT(idna_ascii.text, std::string("xn--bcher-kva.example"), "C++ IDNA ToASCII");
|
|
|
|
const auto idna_unicode = mjb::idna_to_unicode("xn--bcher-kva.example");
|
|
ATT_ASSERT(idna_unicode.valid(), true, "C++ IDNA ToUnicode valid");
|
|
ATT_ASSERT(idna_unicode.text, std::string("b\xC3\xBC"
|
|
"cher.example"),
|
|
"C++ IDNA ToUnicode");
|
|
|
|
const auto idna_invalid = mjb::idna_to_ascii("a..b");
|
|
ATT_ASSERT(idna_invalid.valid(), false, "C++ IDNA reports invalid domain");
|
|
ATT_ASSERT(idna_invalid.has_error(MJB_IDNA_ERROR_EMPTY_LABEL), true,
|
|
"C++ IDNA exposes error flags");
|
|
#else
|
|
bool idna_disabled = false;
|
|
|
|
try {
|
|
(void)mjb::idna_to_ascii("example.com");
|
|
} catch(const mjb::LibraryError &error) {
|
|
idna_disabled = error.status() == MJB_STATUS_FEATURE_NOT_ENABLED;
|
|
}
|
|
|
|
ATT_ASSERT(idna_disabled, true, "C++ disabled IDNA reports feature status");
|
|
#endif
|
|
|
|
// NumericValue and codepoint_numeric_value
|
|
// U+0031 = '1'
|
|
auto num1 = mjb::numeric_value('1');
|
|
ATT_ASSERT(num1.decimal().has_value(), true, "numeric_value '1': decimal has_value");
|
|
ATT_ASSERT(*num1.decimal(), 1, "numeric_value '1': decimal");
|
|
ATT_ASSERT(*num1.digit(), 1, "numeric_value '1': digit");
|
|
ATT_ASSERT(std::string(num1.numeric()), std::string("1"), "numeric_value '1': numeric");
|
|
|
|
// U+00BD = '½': only numeric string
|
|
auto num_half = mjb::numeric_value(0x00BD);
|
|
ATT_ASSERT(num_half.decimal().has_value(), false, "numeric_value ½: no decimal");
|
|
ATT_ASSERT(std::string(num_half.numeric()), std::string("1/2"), "numeric_value ½: numeric");
|
|
|
|
#if MJB_FEATURE_COLLATION
|
|
// collation_key
|
|
auto key_a = mjb::collation_key("a");
|
|
auto key_b = mjb::collation_key("b");
|
|
ATT_ASSERT(key_a.empty(), false, "collation_key: a not empty");
|
|
ATT_ASSERT(key_a < key_b, true, "collation_key: a < b");
|
|
ATT_ASSERT(mjb::collation_key(""), std::string(""), "collation_key: empty");
|
|
ATT_ASSERT(mjb::collation_key("\xE2\x80\x8B"), mjb::collation_key(""),
|
|
"collation_key: completely ignorable equals empty");
|
|
ATT_ASSERT(mjb::collation_key("\xCC\x81", { .strength = mjb::CollationStrength::Primary }),
|
|
mjb::collation_key("", { .strength = mjb::CollationStrength::Primary }),
|
|
"collation_key: primary-ignorable accent equals empty");
|
|
ATT_ASSERT(mjb::collation_key("A", { .strength = mjb::CollationStrength::Secondary }),
|
|
mjb::collation_key("a", { .strength = mjb::CollationStrength::Secondary }),
|
|
"collation_key: secondary ignores case");
|
|
#else
|
|
bool collation_disabled = false;
|
|
|
|
try {
|
|
(void)mjb::collation_key("a");
|
|
} catch(const mjb::LibraryError &error) {
|
|
collation_disabled = error.status() == MJB_STATUS_FEATURE_NOT_ENABLED;
|
|
}
|
|
|
|
ATT_ASSERT(collation_disabled, true, "C++ disabled collation reports feature status");
|
|
#endif
|
|
|
|
// truncate
|
|
ATT_ASSERT(std::string(mjb::grapheme_truncate("hello", 3)), std::string("hel"), "truncate: 3 graphemes");
|
|
ATT_ASSERT(std::string(mjb::grapheme_truncate("hello", 10)), std::string("hello"),
|
|
"truncate: beyond length");
|
|
ATT_ASSERT(std::string(mjb::word_truncate("hello world", 1)), std::string("hello"),
|
|
"word_truncate: 1 segment");
|
|
|
|
ATT_ASSERT(mjb::grapheme_count("hello"), (size_t)5, "grapheme_count: ASCII");
|
|
ATT_ASSERT(mjb::grapheme_count(""), (size_t)0, "grapheme_count: empty");
|
|
ATT_ASSERT(mjb::sentence_count("Hello. How are you? Fine!"), (size_t)3,
|
|
"sentence_count: three sentences");
|
|
ATT_ASSERT(mjb::sentence_count(""), (size_t)0, "sentence_count: empty");
|
|
ATT_ASSERT(mjb::word_count("Hello, world! It works."), (size_t)4, "word_count: four words");
|
|
ATT_ASSERT(mjb::word_count(""), (size_t)0, "word_count: empty");
|
|
ATT_ASSERT(mjb::grapheme_count("a\xCC\x81"), (size_t)1, "grapheme_count: combining mark");
|
|
ATT_ASSERT(mjb::grapheme_count("\xF0\x9F\x87\xAE\xF0\x9F\x87\xB9"), (size_t)1,
|
|
"grapheme_count: flag emoji");
|
|
|
|
ATT_ASSERT(std::string(mjb::property_name(MJB_PR_ALPHABETIC)), std::string("Alphabetic"),
|
|
"property_name");
|
|
ATT_ASSERT((int)mjb::script('A'), MJB_SC_LATN, "script");
|
|
|
|
const auto block = mjb::block_info('A');
|
|
ATT_ASSERT(block.has_value(), true, "block_info has value");
|
|
ATT_ASSERT(std::string(block->name()), std::string("Basic Latin"), "block_info name");
|
|
ATT_ASSERT(block->contains('Z'), true, "block_info contains");
|
|
|
|
const auto emoji = mjb::emoji_properties(0x1F642);
|
|
ATT_ASSERT(emoji.has_value(), true, "emoji_properties has value");
|
|
ATT_ASSERT(emoji->is_emoji(), true, "emoji_properties is_emoji");
|
|
ATT_ASSERT(emoji->is_extended_pictographic(), true,
|
|
"emoji_properties is_extended_pictographic");
|
|
|
|
ATT_ASSERT(mjb::is_valid('A'), true, "is_valid codepoint");
|
|
ATT_ASSERT(mjb::is_graphic('A'), true, "is_graphic");
|
|
ATT_ASSERT(mjb::is_combining(0x0301), true, "is_combining");
|
|
ATT_ASSERT(mjb::category_is_graphic(MJB_CATEGORY_LL), true, "category_is_graphic");
|
|
ATT_ASSERT(mjb::category_is_combining(MJB_CATEGORY_MN), true, "category_is_combining");
|
|
ATT_ASSERT(mjb::is_hangul_leading_jamo(0x1100), true, "is_hangul_l");
|
|
ATT_ASSERT(mjb::is_hangul_vowel_jamo(0x1161), true, "is_hangul_v");
|
|
ATT_ASSERT(mjb::is_hangul_trailing_jamo(0x11A8), true, "is_hangul_t");
|
|
ATT_ASSERT(mjb::is_hangul_jamo(0x1100), true, "is_hangul_jamo");
|
|
ATT_ASSERT(mjb::is_hangul_syllable(0xAC00), true, "is_hangul_syllable");
|
|
ATT_ASSERT(mjb::is_cjk_ideograph(0x4E00), true, "is_cjk_ideograph");
|
|
ATT_ASSERT(mjb::is_cjk_extension_ideograph(0x3400), true, "is_cjk_extension");
|
|
ATT_ASSERT(mjb::is_emoji(0x1F642), true, "is_emoji codepoint");
|
|
ATT_ASSERT(mjb::is_emoji_presentation(0x1F642), true, "is_emoji_presentation");
|
|
ATT_ASSERT(mjb::is_extended_pictographic(0x1F642), true,
|
|
"is_extended_pictographic");
|
|
ATT_ASSERT(mjb::is_id_start('A'), true, "is_id_start codepoint");
|
|
ATT_ASSERT(mjb::is_id_continue('1'), true, "is_id_continue codepoint");
|
|
ATT_ASSERT(mjb::is_pattern_syntax('!'), true, "is_pattern_syntax codepoint");
|
|
|
|
ATT_ASSERT((int)mjb::plane('A'), MJB_PLANE_BMP, "plane");
|
|
ATT_ASSERT(mjb::is_valid(MJB_PLANE_BMP), true, "is_valid plane");
|
|
ATT_ASSERT(std::string(mjb::plane_name(MJB_PLANE_BMP, true)), std::string("BMP"),
|
|
"plane_name");
|
|
ATT_ASSERT((int)mjb::plane(0xE0001), MJB_PLANE_SSP, "plane SSP");
|
|
ATT_ASSERT(std::string(mjb::plane_name(MJB_PLANE_PUA_A, false)),
|
|
std::string("Supplementary Private Use Area-A"), "plane_name PUA-A");
|
|
ATT_ASSERT((int)mjb::east_asian_width('A'), MJB_EAW_NARROW, "east_asian_width");
|
|
ATT_ASSERT(mjb::encode(0x1F642), std::string("\xF0\x9F\x99\x82"), "encode");
|
|
|
|
std::u32string visited;
|
|
ATT_ASSERT_STATUS(mjb::for_each_codepoint("A\xC3\xA9" "B",
|
|
[&visited](const mjb_character &each, mjb_character_position) {
|
|
visited += static_cast<char32_t>(each.codepoint);
|
|
|
|
return true;
|
|
}),
|
|
MJB_STATUS_OK, "for_each_codepoint with a capturing lambda");
|
|
ATT_ASSERT(visited == U"A\u00E9B", true, "for_each_codepoint lambda sees every codepoint");
|
|
|
|
size_t stopped_after = 0;
|
|
ATT_ASSERT_STATUS(mjb::for_each_codepoint("ABC",
|
|
[&stopped_after](const mjb_character &, mjb_character_position) { return ++stopped_after < 2;
|
|
}),
|
|
MJB_STATUS_CALLBACK_STOPPED, "for_each_codepoint lambda can stop the iteration");
|
|
ATT_ASSERT(stopped_after, 2u, "for_each_codepoint stops when the lambda returns false");
|
|
|
|
bool rethrown = false;
|
|
size_t calls_before_throw = 0;
|
|
|
|
try {
|
|
(void)mjb::for_each_codepoint("ABC",
|
|
[&calls_before_throw](const mjb_character &, mjb_character_position) -> bool {
|
|
++calls_before_throw;
|
|
throw std::runtime_error("stop here");
|
|
});
|
|
} catch(const std::runtime_error &error) {
|
|
rethrown = std::string(error.what()) == "stop here";
|
|
}
|
|
|
|
ATT_ASSERT(rethrown, true, "for_each_codepoint rethrows the lambda exception");
|
|
ATT_ASSERT(calls_before_throw, 1u, "for_each_codepoint stops at the lambda exception");
|
|
|
|
const auto sequence = mjb::emoji_sequence("\xF0\x9F\x99\x82");
|
|
ATT_ASSERT(sequence.has_value(), true, "emoji_sequence has value");
|
|
ATT_ASSERT(sequence->size(), 1u, "emoji_sequence size");
|
|
ATT_ASSERT(mjb::is_emoji_sequence("\xF0\x9F\x99\x82"), true, "is_emoji_sequence");
|
|
ATT_ASSERT(mjb::is_rgi_emoji("\xF0\x9F\x99\x82"), true, "is_rgi_emoji");
|
|
ATT_ASSERT(mjb::terminal_width("abc"), 3u, "terminal_width");
|
|
ATT_ASSERT(mjb::terminal_width("\xE2\x9D\xA4\xEF\xB8\x8F"), 2u,
|
|
"terminal_width emoji sequence");
|
|
|
|
ATT_ASSERT(mjb::hangul_syllable_name(0xAC00), std::string("HANGUL SYLLABLE GA"),
|
|
"hangul_syllable_name");
|
|
const auto decomposition = mjb::hangul_syllable_decompose(0xAC00);
|
|
ATT_ASSERT(decomposition[0], (mjb_codepoint)0x1100, "hangul decomposition L");
|
|
ATT_ASSERT(decomposition[1], (mjb_codepoint)0x1161, "hangul decomposition V");
|
|
ATT_ASSERT(decomposition[2], (mjb_codepoint)0, "hangul decomposition T");
|
|
|
|
ATT_ASSERT(mjb::hangul_syllable_compose(0x1100, 0x1161), (mjb_codepoint)0xAC00,
|
|
"hangul composition LV");
|
|
ATT_ASSERT(mjb::hangul_syllable_compose(0x1100, 0x1161, 0x11A8), (mjb_codepoint)0xAC01,
|
|
"hangul composition LVT");
|
|
|
|
ATT_ASSERT(std::string(mjb::version()), std::string(MJB_VERSION), "version");
|
|
ATT_ASSERT(mjb::version_number(), (unsigned int)MJB_VERSION_NUMBER, "version_number");
|
|
ATT_ASSERT(std::string(mjb::unicode_version()), std::string(MJB_UNICODE_VERSION),
|
|
"unicode_version");
|
|
}
|