|
| 1 | +// |
| 2 | +// TypesetterWrapLengthTests.swift |
| 3 | +// TableProTests |
| 4 | +// |
| 5 | +// Regression tests for wrapped line typesetting. `suggestLineBreak` returns an offset into the run, |
| 6 | +// not a length, but the typesetter passed it straight through as the CTLine length. Every fragment |
| 7 | +// after the first then re-typeset all the text before it, so glyph work and retained memory grew |
| 8 | +// with the square of the line length and a long single line could exhaust memory. |
| 9 | +// |
| 10 | +// These live here rather than in CodeEditTextViewTests because the TablePro scheme does not run |
| 11 | +// that package's test target, so a test there would never gate a regression. |
| 12 | +// |
| 13 | + |
| 14 | +import AppKit |
| 15 | +@testable import CodeEditTextView |
| 16 | +import Foundation |
| 17 | +import Testing |
| 18 | + |
| 19 | +@MainActor |
| 20 | +@Suite("Typesetter wrapped fragment lengths") |
| 21 | +struct TypesetterWrapLengthTests { |
| 22 | + private static let attributes: [NSAttributedString.Key: Any] = [ |
| 23 | + .font: NSFont.monospacedSystemFont(ofSize: 10, weight: .regular) |
| 24 | + ] |
| 25 | + |
| 26 | + private func typesetWrapped(characterCount: Int, maxWidth: CGFloat) -> Typesetter { |
| 27 | + let typesetter = Typesetter() |
| 28 | + typesetter.typeset( |
| 29 | + NSAttributedString(string: String(repeating: "A", count: characterCount), attributes: Self.attributes), |
| 30 | + documentRange: NSRange(location: 0, length: characterCount), |
| 31 | + displayData: TextLine.DisplayData( |
| 32 | + maxWidth: maxWidth, |
| 33 | + lineHeightMultiplier: 1.0, |
| 34 | + estimatedLineHeight: 20.0, |
| 35 | + breakStrategy: .character |
| 36 | + ), |
| 37 | + markedRanges: nil, |
| 38 | + attachments: [] |
| 39 | + ) |
| 40 | + return typesetter |
| 41 | + } |
| 42 | + |
| 43 | + @Test("A wrapped line typesets each character exactly once") |
| 44 | + func wrappedLineTypesetsEachCharacterOnce() { |
| 45 | + let characterCount = 1_000 |
| 46 | + let typesetter = typesetWrapped(characterCount: characterCount, maxWidth: 150) |
| 47 | + |
| 48 | + var typesetCharacters = 0 |
| 49 | + for fragment in typesetter.lineFragments { |
| 50 | + typesetCharacters += fragment.data.contents.reduce(0) { $0 + $1.length } |
| 51 | + } |
| 52 | + |
| 53 | + #expect( |
| 54 | + typesetCharacters == characterCount, |
| 55 | + "Each character must be typeset once, not once per following fragment" |
| 56 | + ) |
| 57 | + } |
| 58 | + |
| 59 | + @Test("Each wrapped fragment typesets exactly the characters it covers") |
| 60 | + func eachFragmentTypesetsOnlyItsOwnCharacters() { |
| 61 | + let typesetter = typesetWrapped(characterCount: 1_000, maxWidth: 150) |
| 62 | + |
| 63 | + for fragment in typesetter.lineFragments { |
| 64 | + let typesetLength = fragment.data.contents.reduce(0) { $0 + $1.length } |
| 65 | + #expect( |
| 66 | + typesetLength == fragment.range.length, |
| 67 | + "A fragment covering \(fragment.range.length) characters typeset \(typesetLength)" |
| 68 | + ) |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + @Test("Wrapping stays linear as the line grows") |
| 73 | + func wrappingStaysLinearAsTheLineGrows() { |
| 74 | + let small = typesetWrapped(characterCount: 1_000, maxWidth: 150) |
| 75 | + let large = typesetWrapped(characterCount: 4_000, maxWidth: 150) |
| 76 | + |
| 77 | + func typesetCharacters(in typesetter: Typesetter) -> Int { |
| 78 | + var total = 0 |
| 79 | + for fragment in typesetter.lineFragments { |
| 80 | + total += fragment.data.contents.reduce(0) { $0 + $1.length } |
| 81 | + } |
| 82 | + return total |
| 83 | + } |
| 84 | + |
| 85 | + // Four times the text must cost four times the typesetting, not sixteen. |
| 86 | + #expect(typesetCharacters(in: large) == typesetCharacters(in: small) * 4) |
| 87 | + } |
| 88 | +} |
0 commit comments