using System; using System.Collections.Generic; using System.Linq; using System.Text; using Amphora; using Tessera.Unicode; namespace Tessera.UnitTests.Unicode { /// /// What the line break tests, and the tests of what is built on line breaking, share. /// public static class LineBreakSamples { /// /// The Unicode Consortium's UAX #14 conformance file. /// public static class LineBreakTestFile { /// /// Holds every case in the file, read once for all the tests that walk it. /// // Only tests with a raised CPU budget touch this, so the read lands in one of them. public static readonly BoundaryTestFile.Case[] Cases = BoundaryTestFile.ReadCases("LineBreakTest.txt"); /// /// Returns everything wrapping or placing `text` can tell about it, as a string. A character counts by its /// ASCII identity, whether it's a hard break, a combining mark, a soft hyphen or default-ignorable, and /// whether it's an astral pair, and the text by the breaks its line breaker finds. /// public static string ShapeOf(string text) { var returnMe = new StringBuilder(); for (int i = 0; i < text.Length; ++i) { int codePoint = text[i]; if (char.IsHighSurrogate(text[i]) && i + 1 < text.Length && char.IsLowSurrogate(text[i + 1])) { codePoint = char.ConvertToUtf32(text[i], text[i + 1]); returnMe.Append('A'); ++i; } else if (char.IsSurrogate(text[i])) { returnMe.Append('s'); } else if (HardBreaks.IsHardBreak(text[i])) { returnMe.Append('H'); } if (UnicodeProperties.IsDefaultIgnorable(codePoint)) { returnMe.Append('i'); } if (UnicodeProperties.Of(codePoint).LineBreakClass().Is(LineBreakClassSets.CombiningMark)) { returnMe.Append('M'); } if (text[i] == LineBreakIterator.SoftHyphen) { returnMe.Append('~'); } returnMe.Append(codePoint >= ' ' && codePoint <= '~' ? (char)codePoint : '_').Append(','); } foreach (LineBreak lineBreak in new LineBreakIterator(text)) { returnMe.Append(lineBreak.Position).Append(lineBreak.IsMandatory ? '!' : '.') .Append(lineBreak.TrailingSpaces).Append('e').Append((int)lineBreak.Ending).Append(','); } return returnMe.ToString(); } /// /// Returns the first case of each . /// public static BoundaryTestFile.Case[] DistinctShapes(BoundaryTestFile.Case[] cases) { var seen = new HashSet(); var returnMe = new List(); foreach (BoundaryTestFile.Case testCase in cases) { if (seen.Add(ShapeOf(testCase.Text))) { returnMe.Add(testCase); } } return returnMe.ToArray(); } /// /// One case for each distinct shape, which is all a test needs that compares two ways of wrapping or /// placing, since neither can tell two cases of a shape apart. /// public static readonly BoundaryTestFile.Case[] Shapes = DistinctShapes(Cases); } public static List BreaksIn(LineBreakIterator iterator) { var returnMe = new List(); foreach (LineBreak lineBreak in iterator) { returnMe.Add(lineBreak); } return returnMe; } public static List BreaksIn(StringView text) => BreaksIn(new LineBreakIterator(text)); public static int[] PositionsIn(StringView text) => BreaksIn(text).Select(each => each.Position).ToArray(); public static string CodePointsOf(string text) { var codePoints = new List(); for (int i = 0; i < text.Length; i += char.IsSurrogatePair(text, i) ? 2 : 1) { codePoints.Add(char.ConvertToUtf32(text, i).ToString("X4")); } return string.Join(" ", codePoints); } public static string Describe(IEnumerable breaks) { return string.Join(",", breaks.Select(each => each.Position + (each.IsMandatory ? "!" : ""))); } /// /// Where `iteratorFor` disagrees with the Unicode Consortium's LineBreakTest.txt, or "" where it doesn't. /// public static string DisagreementsWithLineBreakTest(System.Func iteratorFor) { int mismatchCount = 0; var firstMismatches = new List(); foreach (BoundaryTestFile.Case testCase in LineBreakTestFile.Cases) { var expected = new HashSet(testCase.Boundaries.Where(each => each.Breaks) .Select(each => each.Index)) { testCase.Text.Length }; var actual = new HashSet(BreaksIn(iteratorFor(testCase.Text)).Select(each => each.Position)); if (expected.SetEquals(actual)) { continue; } ++mismatchCount; if (firstMismatches.Count < 10) { firstMismatches.Add($"[{CodePointsOf(testCase.Text)}] expected breaks at " + $"{string.Join(",", expected.OrderBy(each => each))} but got " + $"{string.Join(",", actual.OrderBy(each => each))}"); } } return mismatchCount == 0 ? "" : $"{mismatchCount} cases differ, including: " + string.Join("; ", firstMismatches); } public static bool IsHardBreak(char character) => UnicodeProperties.Of(character).LineBreakClass().Is(LineBreakClassSets.HardBreak); public static bool IsSpace(char character) => UnicodeProperties.Of(character).LineBreakClass() == LineBreakClass.SP; /// /// Where the spaces and the hard break before a break at `position` begin, worked out from each character's /// class rather than the way the iterator does. /// public static (int ContentEnd, int SpacesEnd) ExpectedTrailing(string text, int position) { int spacesEnd = position; if (spacesEnd > 0 && IsHardBreak(text[spacesEnd - 1])) { --spacesEnd; if (text[spacesEnd] == '\n' && spacesEnd > 0 && text[spacesEnd - 1] == '\r') { --spacesEnd; } } int contentEnd = spacesEnd; while (contentEnd > 0 && IsSpace(text[contentEnd - 1])) { --contentEnd; } return (contentEnd, spacesEnd); } /// /// Where the iterator's trailing positions disagree with on LineBreakTest.txt. /// public static string TrailingDisagreementsWithLineBreakTest() { var firstMismatches = new List(); foreach (BoundaryTestFile.Case testCase in LineBreakTestFile.Cases) { string text = testCase.Text; var iterator = new LineBreakIterator(text); int previousEnd = 0; while (iterator.MoveNext()) { (int contentEnd, int spacesEnd) = ExpectedTrailing(text, iterator.Current.Position); LineBreak lineBreak = iterator.Current; bool staysInTheWord = lineBreak.ContentEnd >= previousEnd; if ((lineBreak.ContentEnd != contentEnd || lineBreak.SpacesEnd != spacesEnd || !staysInTheWord) && firstMismatches.Count < 10) { firstMismatches.Add($"[{CodePointsOf(text)}] at {lineBreak.Position}: " + $"{lineBreak.ContentEnd}/{lineBreak.SpacesEnd}, expected {contentEnd}/{spacesEnd}"); } previousEnd = iterator.Current.Position; } } return string.Join("; ", firstMismatches); } /// /// Each break's position, then where its trailing spaces and hard break begin. /// public static string DescribeTrailing(StringView text) { var descriptions = new List(); var iterator = new LineBreakIterator(text); while (iterator.MoveNext()) { LineBreak lineBreak = iterator.Current; descriptions.Add($"{lineBreak.Position}:{lineBreak.ContentEnd}/{lineBreak.SpacesEnd}"); } return string.Join(" ", descriptions); } } }