using Xunit; namespace XFESystem.Desktop.Tests; public sealed class TextLineIndexTests { [Theory] [InlineData("")] [InlineData("\n")] [InlineData("\n\n")] [InlineData("one\ntwo\n")] [InlineData("one\r\ntwo\r\n")] [InlineData("a\rb\nc")] [InlineData("中文😀\r\n尾行")] public void LazyLinesPreserveRawCaretOffsetsAndExistingDisplayedText(string text) { var index = new TextLineIndex(text); string[] raw = text.Split('\n'), displayed = text.Replace("\r", "").Split('\n'); Assert.Equal(raw.Length, index.Count); int start = 0; for (int line = 0; line < index.Count; line++) { Assert.Equal(start, index.Start(line)); Assert.Equal(raw[line], index.RawLine(line)); Assert.Equal(displayed[line], index.DisplayLine(line)); Assert.Same(index.RawLine(line), index.RawLine(line)); Assert.Same(index.DisplayLine(line), index.DisplayLine(line)); start += raw[line].Length + 1; } } [Fact] public void RepeatedLongDocumentScrollReadsReuseVisibleLinesWithoutSplittingDocumentAgain() { long allocated = -1; int count = 0; // Measure a warmed render thread, separate from the runner's reused // worker thread and its first-use/JIT work. Keep the zero-byte limit. var worker = new Thread(() => { string text = string.Join('\n', Enumerable.Range(0, 3000).Select(i => $"Line {i} some text")); var index = new TextLineIndex(text); for (int iteration = 0; iteration < 256; iteration++) for (int line = 0; line < 40; line++) _ = index.DisplayLine(line); long before = GC.GetAllocatedBytesForCurrentThread(); for (int iteration = 0; iteration < 1000; iteration++) for (int line = 0; line < 40; line++) _ = index.DisplayLine(line); allocated = GC.GetAllocatedBytesForCurrentThread() - before; count = index.Count; }); worker.Start(); worker.Join(); Assert.Equal(0, allocated); Assert.Equal(3000, count); } }