using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; using Tessera.Unicode; using UnitTestSharp; namespace Tessera.UnitTests.Unicode { public class UnicodePropertiesTests : TestFixture { /// /// Where the build unpacks 3rdParty\Unicode\UCD-*.7z, next to this binary. /// public static readonly string DataDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Unicode"); public const int CodePointCount = UnicodeProperties.MaxCodePoint + 1; public const int DottedCircle = 0x25CC; public const int SoftHyphen = 0x00AD; public static string[] ReadProperty(string fileName, string valueWhereUnlisted) { var returnMe = new string[CodePointCount]; for (int codePoint = 0; codePoint < CodePointCount; ++codePoint) { returnMe[codePoint] = valueWhereUnlisted; } foreach ((int first, int last, string value) in ReadRanges(fileName)) { for (int codePoint = first; codePoint <= last; ++codePoint) { returnMe[codePoint] = value; } } return returnMe; } public static IEnumerable<(int First, int Last, string Value)> ReadRanges(string fileName) { foreach (string line in File.ReadLines(Path.Combine(DataDirectory, fileName))) { string data = line.Split('#')[0].Trim(); if (data.Length == 0) { continue; } string[] fields = data.Split(';'); string[] range = fields[0].Trim().Split(new[] { ".." }, StringSplitOptions.None); yield return (int.Parse(range[0], NumberStyles.HexNumber), int.Parse(range[range.Length - 1], NumberStyles.HexNumber), fields[1].Trim()); } } // Enum.Parse for each code point would dominate an every-code-point comparison. public static readonly Dictionary LineBreakClassesByName = Enum.GetNames(typeof(LineBreakClass)).ToDictionary(name => name, name => (LineBreakClass)Enum.Parse(typeof(LineBreakClass), name)); /// /// LB1's default resolutions, plus QU split by whether it's initial or final punctuation. /// public static LineBreakClass ResolvedClass(string lineBreak, string generalCategory) { switch (lineBreak) { case "AI": case "SG": case "XX": return LineBreakClass.AL; case "SA": return generalCategory == "Mn" || generalCategory == "Mc" ? LineBreakClass.CM : LineBreakClass.AL; case "CJ": return LineBreakClass.NS; case "QU" when generalCategory == "Pi": return LineBreakClass.QU_Pi; case "QU" when generalCategory == "Pf": return LineBreakClass.QU_Pf; default: return LineBreakClassesByName[lineBreak]; } } public static string Describe(CharacterProperties properties) { return $"{properties.LineBreakClass()}" + (properties.IsEastAsian() ? " east-asian" : "") + (properties.IsUnassignedExtendedPictographic() ? " unassigned-pictographic" : ""); } /// /// Maps each UCD value name to the enum member named the same once its underscores are dropped. /// public static Dictionary ByUcdName(IEnumerable ucdNames) { return ucdNames.Distinct().ToDictionary(name => name, name => (EnumType)Enum.Parse(typeof(EnumType), name.Replace("_", ""))); } public static string Describe(SegmentationProperties properties) { return $"{properties.GraphemeBreak()} {properties.WordBreak()} {properties.IndicConjunctBreak()}" + (properties.IsExtendedPictographic() ? " pictographic" : "") + (properties.IsLetterOrNumber() ? " letter-or-number" : ""); } public class OfTests : TestFixture { [CpuTimeout(3000)] public void EveryCodePoint_MatchesTheCharacterDatabase() { string[] lineBreaks = ReadProperty("LineBreak.txt", valueWhereUnlisted: "XX"); string[] widths = ReadProperty("EastAsianWidth.txt", valueWhereUnlisted: "N"); string[] categories = ReadProperty("DerivedGeneralCategory.txt", valueWhereUnlisted: "Cn"); var pictographic = new bool[CodePointCount]; foreach ((int first, int last, string value) in ReadRanges("emoji-data.txt")) { for (int codePoint = first; codePoint <= last; ++codePoint) { pictographic[codePoint] |= value == "Extended_Pictographic"; } } var mismatches = new List(); for (int codePoint = 0; codePoint < CodePointCount; ++codePoint) { var expected = (CharacterProperties)(codePoint == DottedCircle ? LineBreakClass.DottedCircle : codePoint == SoftHyphen ? LineBreakClass.SoftHyphen : ResolvedClass(lineBreaks[codePoint], categories[codePoint])); if (widths[codePoint] == "F" || widths[codePoint] == "W" || widths[codePoint] == "H") { expected |= CharacterProperties.EastAsian; } if (pictographic[codePoint] && categories[codePoint] == "Cn") { expected |= CharacterProperties.UnassignedExtendedPictographic; } CharacterProperties actual = UnicodeProperties.Of(codePoint); if (actual != expected && mismatches.Count < 10) { mismatches.Add($"U+{codePoint:X4}: expected {Describe(expected)}, was {Describe(actual)}"); } } CheckEqual("", string.Join("; ", mismatches)); } public void Letter_IsAlphabeticAndNotEastAsian() { CheckEqual("AL", Describe(UnicodeProperties.Of('A'))); } public void Ideograph_IsIdeographicAndEastAsian() { CheckEqual("ID east-asian", Describe(UnicodeProperties.Of(0x5B57))); } public void FullwidthParenthesis_IsEastAsian_WhereTheAsciiOneIsNot() { CheckEqual("OP east-asian", Describe(UnicodeProperties.Of(0xFF08))); CheckEqual("OP", Describe(UnicodeProperties.Of('('))); } public void ComplexContextLetter_ResolvesToAlphabetic() { CheckEqual(LineBreakClass.AL, UnicodeProperties.Of(0x0E01).LineBreakClass()); } public void ComplexContextMark_ResolvesToCombiningMark() { CheckEqual(LineBreakClass.CM, UnicodeProperties.Of(0x0E31).LineBreakClass()); } public void AmbiguousCharacter_ResolvesToAlphabetic() { CheckEqual(LineBreakClass.AL, UnicodeProperties.Of(0x00A7).LineBreakClass()); } public void ConditionalJapaneseStarter_ResolvesToNonstarter() { CheckEqual(LineBreakClass.NS, UnicodeProperties.Of(0x3041).LineBreakClass()); } public void UnassignedCodePoint_ResolvesToAlphabetic() { CheckEqual(LineBreakClass.AL, UnicodeProperties.Of(0x0378).LineBreakClass()); } public void Surrogate_ResolvesToAlphabetic() { CheckEqual(LineBreakClass.AL, UnicodeProperties.Of(0xD800).LineBreakClass()); } public void QuotationMarks_SplitByInitialAndFinalPunctuation() { char[] quotes = { '"', (char)0x00AB, (char)0x00BB }; CheckEqual(new[] { LineBreakClass.QU, LineBreakClass.QU_Pi, LineBreakClass.QU_Pf }, quotes.Select(quote => UnicodeProperties.Of(quote).LineBreakClass())); } public void DottedCircle_HasAClassOfItsOwn() { CheckEqual("DottedCircle", Describe(UnicodeProperties.Of(DottedCircle))); } public void SoftHyphen_HasAClassOfItsOwn() { CheckEqual("SoftHyphen", Describe(UnicodeProperties.Of(SoftHyphen))); } public void UnassignedPictograph_IsFlagged() { CheckTrue(UnicodeProperties.Of(0x1F02C).IsUnassignedExtendedPictographic()); } public void AssignedEmoji_IsNotFlaggedAsUnassigned() { CheckFalse(UnicodeProperties.Of(0x1F600).IsUnassignedExtendedPictographic()); } public void LastCodePoint_IsLookedUp() { CheckEqual(LineBreakClass.AL, UnicodeProperties.Of(UnicodeProperties.MaxCodePoint).LineBreakClass()); } public void NegativeValue_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); UnicodeProperties.Of(-1); } public void ValuePastTheLastCodePoint_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); UnicodeProperties.Of(UnicodeProperties.MaxCodePoint + 1); } } public class IsDefaultIgnorableTests : TestFixture { public void EveryCodePoint_MatchesTheCharacterDatabase() { var expected = new bool[CodePointCount]; foreach ((int first, int last, string value) in ReadRanges("DerivedCoreProperties.txt")) { for (int codePoint = first; codePoint <= last; ++codePoint) { expected[codePoint] |= value == "Default_Ignorable_Code_Point"; } } var mismatches = new List(); for (int codePoint = 0; codePoint < CodePointCount; ++codePoint) { if (UnicodeProperties.IsDefaultIgnorable(codePoint) != expected[codePoint] && mismatches.Count < 10) { mismatches.Add($"U+{codePoint:X4}: expected {expected[codePoint]}"); } } CheckEqual("", string.Join("; ", mismatches)); } public void NegativeValue_IsNot() { CheckFalse(UnicodeProperties.IsDefaultIgnorable(-1)); } } public class IsSurrogateTests : TestFixture { public void EveryCodeUnit_MatchesTheFramework() { CheckEqual("", CodeUnitSamples.SurrogateMismatches(UnicodeProperties.IsSurrogate, char.IsSurrogate)); } } public class IsHighSurrogateTests : TestFixture { public void EveryCodeUnit_MatchesTheFramework() { CheckEqual("", CodeUnitSamples.SurrogateMismatches(UnicodeProperties.IsHighSurrogate, char.IsHighSurrogate)); } } public class IsLowSurrogateTests : TestFixture { public void EveryCodeUnit_MatchesTheFramework() { CheckEqual("", CodeUnitSamples.SurrogateMismatches(UnicodeProperties.IsLowSurrogate, char.IsLowSurrogate)); } } public class SpaceSeparatorsTests : TestFixture { public void MatchesTheCharacterDatabase() { IEnumerable spaceSeparators = ReadRanges("DerivedGeneralCategory.txt") .Where(range => range.Value == "Zs") .SelectMany(range => Enumerable.Range(range.First, range.Last - range.First + 1)) .OrderBy(codePoint => codePoint); CheckEqual(spaceSeparators, UnicodeProperties.SpaceSeparators); } } public class OfDecodedTests : TestFixture { public void EveryCodePoint_MatchesOf() { int mismatches = 0; for (int codePoint = 0; codePoint <= UnicodeProperties.MaxCodePoint; ++codePoint) { if (UnicodeProperties.OfDecoded(codePoint) != UnicodeProperties.Of(codePoint)) { ++mismatches; } } CheckEqual(0, mismatches); } } public class SegmentationOfTests : TestFixture { [CpuTimeout(3000)] public void EveryCodePoint_MatchesTheCharacterDatabase() { string[] graphemeBreaks = ReadProperty("GraphemeBreakProperty.txt", valueWhereUnlisted: "Other"); string[] wordBreaks = ReadProperty("WordBreakProperty.txt", valueWhereUnlisted: "Other"); string[] categories = ReadProperty("DerivedGeneralCategory.txt", valueWhereUnlisted: "Cn"); var indicConjunctBreaks = new IndicConjunctBreak[CodePointCount]; var pictographic = new bool[CodePointCount]; foreach (string line in File.ReadLines(Path.Combine(DataDirectory, "DerivedCoreProperties.txt"))) { string[] fields = line.Split('#')[0].Split(';'); if (fields.Length == 3 && fields[1].Trim() == "InCB") { string[] range = fields[0].Trim().Split(new[] { ".." }, StringSplitOptions.None); int first = int.Parse(range[0], NumberStyles.HexNumber); int last = int.Parse(range[range.Length - 1], NumberStyles.HexNumber); var value = (IndicConjunctBreak)Enum.Parse(typeof(IndicConjunctBreak), fields[2].Trim()); for (int codePoint = first; codePoint <= last; ++codePoint) { indicConjunctBreaks[codePoint] = value; } } } foreach ((int first, int last, string value) in ReadRanges("emoji-data.txt")) { for (int codePoint = first; codePoint <= last; ++codePoint) { pictographic[codePoint] |= value == "Extended_Pictographic"; } } Dictionary graphemeBreaksByName = ByUcdName(graphemeBreaks); Dictionary wordBreaksByName = ByUcdName(wordBreaks); var mismatches = new List(); for (int codePoint = 0; codePoint < CodePointCount; ++codePoint) { SegmentationProperties actual = UnicodeProperties.SegmentationOf(codePoint); string category = categories[codePoint]; bool letterOrNumber = category[0] == 'L' || category == "Nd" || category == "Nl" || category == "No"; if (actual.GraphemeBreak() != graphemeBreaksByName[graphemeBreaks[codePoint]] || actual.WordBreak() != wordBreaksByName[wordBreaks[codePoint]] || actual.IndicConjunctBreak() != indicConjunctBreaks[codePoint] || actual.IsExtendedPictographic() != pictographic[codePoint] || actual.IsLetterOrNumber() != letterOrNumber) { if (mismatches.Count < 10) { mismatches.Add($"U+{codePoint:X4}: was {Describe(actual)}"); } } } CheckEqual("", string.Join("; ", mismatches)); } public void Letter_IsALetterAndLetterOrNumber() { CheckEqual("Other ALetter None letter-or-number", Describe(UnicodeProperties.SegmentationOf('A'))); } public void Ideograph_IsOtherButLetterOrNumber() { CheckEqual("Other Other None letter-or-number", Describe(UnicodeProperties.SegmentationOf(0x4E00))); } public void Emoji_IsPictographicAndNotLetterOrNumber() { CheckEqual("Other Other None pictographic", Describe(UnicodeProperties.SegmentationOf(0x1F600))); } public void Virama_ExtendsAndLinks() { CheckEqual("Extend Extend Linker", Describe(UnicodeProperties.SegmentationOf(0x094D))); } public void ArabicNumberSign_PrependsAndIsNumeric() { CheckEqual("Prepend Numeric None", Describe(UnicodeProperties.SegmentationOf(0x0600))); } public void HangulJamoAndSyllables_HaveTheirSyllableTypes() { int[] hangul = { 0x1100, 0x1161, 0x11A8, 0xAC00, 0xAC01 }; CheckEqual( new[] { GraphemeBreak.L, GraphemeBreak.V, GraphemeBreak.T, GraphemeBreak.LV, GraphemeBreak.LVT }, hangul.Select(codePoint => UnicodeProperties.SegmentationOf(codePoint).GraphemeBreak())); } public void UnassignedCodePoint_PacksAsZero() { CheckEqual((SegmentationProperties)0, UnicodeProperties.SegmentationOf(0x0378)); } public void LastCodePoint_IsLookedUp() { CheckEqual((SegmentationProperties)0, UnicodeProperties.SegmentationOf(UnicodeProperties.MaxCodePoint)); } public void NegativeValue_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); UnicodeProperties.SegmentationOf(-1); } public void ValuePastTheLastCodePoint_Throws() { CheckThrow(typeof(ArgumentOutOfRangeException)); UnicodeProperties.SegmentationOf(UnicodeProperties.MaxCodePoint + 1); } } public class SegmentationOfDecodedTests : TestFixture { public void EveryCodePoint_MatchesSegmentationOf() { int mismatches = 0; for (int codePoint = 0; codePoint <= UnicodeProperties.MaxCodePoint; ++codePoint) { if (UnicodeProperties.SegmentationOfDecoded(codePoint) != UnicodeProperties.SegmentationOf(codePoint)) { ++mismatches; } } CheckEqual(0, mismatches); } } } public class CharacterPropertiesExtensionsTests : TestFixture { public static readonly CharacterProperties BothFlags = CharacterProperties.EastAsian | CharacterProperties.UnassignedExtendedPictographic; public class LineBreakClassTests : TestFixture { public void IgnoresBothFlags() { CheckEqual(LineBreakClass.OP, ((CharacterProperties)LineBreakClass.OP | BothFlags).LineBreakClass()); } } public class IsEastAsianTests : TestFixture { public void ReadsOnlyItsOwnFlag() { CheckTrue(CharacterProperties.EastAsian.IsEastAsian()); CheckFalse((CharacterProperties.UnassignedExtendedPictographic | CharacterProperties.LineBreakClassMask) .IsEastAsian()); } } public class IsUnassignedExtendedPictographicTests : TestFixture { public void ReadsOnlyItsOwnFlag() { CheckTrue(CharacterProperties.UnassignedExtendedPictographic.IsUnassignedExtendedPictographic()); CheckFalse((CharacterProperties.EastAsian | CharacterProperties.LineBreakClassMask) .IsUnassignedExtendedPictographic()); } } } }