SPB Git

Repository Public

MathRobustnessTests.swift 4.8 KB 63 lines swift

1// ORIX Docs2// File: Packages/ORIXDocsKit/Tests/DocLayoutTests/MathRobustnessTests.swift3// Description: Equations come from untrusted documents: malformed or hostile LaTeX must produce a visible error, never a crash.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Testing8import Foundation9import CoreGraphics10import DocMath11import DocModel1213@Suite struct MathRobustnessTests {14    static let hostile: [String] = [15        "", " ", "\\", "{", "}", "{{{{", "}}}}", "\\frac", "\\frac{", "\\frac{1}", "\\frac{1}{", "\\sqrt[", "\\sqrt[3", "^", "_", "x^", "x_", "x^^2", "x__2", "^^^", "\\left(", "\\right)",16        "\\left( x \\right", "\\left\\{ x \\right.", "\\begin{matrix}", "\\end{matrix}", "\\begin{matrix} a & b \\\\ c \\end{pmatrix}", "\\begin{nope} x \\end{nope}", "a & b", "a \\\\ b",17        "\\\\", "\\\\\\\\", "&", "&&&&", "\\begin{matrix} \\\\ \\end{matrix}", "\\begin{matrix} & \\\\ & \\end{matrix}", "\\begin{cases} \\end{cases}", "\\begin{aligned} a \\\\ \\\\ b \\end{aligned}",18        "\\unknowncommand", "\\text{", "\\text{unterminated", "\\mathbb{", "\\color{red}{x}", "\\limits", "x\\limits^2", "\\sum\\limits\\limits", "\\over", "a \\over", "\\over b", "a \\over b \\over c",19        "\\choose", "\\atop", "{\\over}", "\\binom{}{}", "\\frac{}{}", "\\sqrt{}", "\\sqrt[]{}", "\\hat{}", "\\vec", "\\overline", "\\left", "\\right", "\\left.\\right.", "\\left\\right",20        "\\begin{}", "\\end{}", "\\begin", "\\end", "\\ ", "\\,\\,\\,", "~", "$$", "$x$", "%comment", "#", "\\#", "\\$", "\\&", "\\%", "\\{\\}", "\u{0}", "\u{FFFC}", "👨‍👩‍👧", "e\u{0301}", "日本語",21        "\\frac{\\frac{\\frac{\\frac{1}{2}}{3}}{4}}{5}", "x^{x^{x^{x^{x^{x^{x}}}}}}", "\\sqrt{\\sqrt{\\sqrt{\\sqrt{x}}}}",22        String(repeating: "{", count: 500), String(repeating: "}", count: 500), String(repeating: "\\frac{1}{", count: 200), String(repeating: "x^", count: 300) + "x",23        String(repeating: "\\left(", count: 100), String(repeating: "a", count: 5000), String(repeating: "\\begin{matrix}", count: 50), String(repeating: "\\\\", count: 300),24        String(repeating: "&", count: 300), "\\begin{matrix}" + String(repeating: "a&", count: 400) + "\\end{matrix}", "\\begin{matrix}" + String(repeating: "a\\\\", count: 400) + "\\end{matrix}",25    ]2627    @Test(arguments: hostile)28    func hostileLatexNeverCrashes(latex: String) {29        for display in [false, true] {30            let layout = MathRenderer.layout(latex: latex, fontSize: 12, display: display)31            #expect(layout.width.isFinite && layout.ascent.isFinite && layout.descent.isFinite)32            #expect(layout.width >= 0 && layout.width < 1_000_000)33            // Drawing must not crash either.34            let ctx = CGContext(data: nil, width: 64, height: 64, bitsPerComponent: 8, bytesPerRow: 0, space: CGColorSpace(name: CGColorSpace.sRGB)!, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue)!35            layout.draw(in: ctx, at: CGPoint(x: 4, y: 40))36        }37    }3839    @Test func randomTokenSoupNeverCrashes() {40        let tokens = ["\\frac", "\\sqrt", "{", "}", "^", "_", "x", "1", "+", "\\left(", "\\right)", "\\begin{matrix}", "\\end{matrix}", "&", "\\\\", "\\sum", "\\int", "\\limits", "\\over",41                      "\\alpha", "[", "]", "\\text{", "\\mathbb{R}", "\\hat", "\\begin{cases}", "\\end{cases}", " ", "\\,", "\\binom", "\\left\\{", "\\right.", "|", "\\vec", "\\overline"]42        var seed: UInt64 = 0x0D0C543        func next() -> Int { seed = seed &* 6364136223846793005 &+ 1442695040888963407; return Int(seed >> 33) }44        for _ in 0..<3000 {45            let latex = (0..<(1 + next() % 14)).map { _ in tokens[next() % tokens.count] }.joined()46            if let log = ProcessInfo.processInfo.environment["ORIX_MATH_LOG"] { try? latex.write(toFile: log, atomically: false, encoding: .utf8) }47            let l = MathRenderer.layout(latex: latex, fontSize: 11, display: next() % 2 == 0)48            #expect(l.width.isFinite)49        }50    }5152    @Test func validEquationsTypesetWithoutError() {53        for latex in ["E = mc^2", "\\frac{a+b}{c}", "\\sqrt[3]{x^2+1}", "\\sum_{i=1}^{n} i = \\frac{n(n+1)}{2}", "\\int_0^\\infty e^{-x^2}\\,dx = \\frac{\\sqrt{\\pi}}{2}",54                      "\\begin{pmatrix} a & b \\\\ c & d \\end{pmatrix}", "\\alpha\\beta\\gamma\\Delta\\Omega", "\\lim_{x \\to 0} \\frac{\\sin x}{x} = 1", "x_{i,j}^{2}", "\\left( \\frac{1}{2} \\right)^n",55                      "f(x) = \\begin{cases} 1 & x > 0 \\\\ 0 & x \\leq 0 \\end{cases}", "\\hat{y} = \\bar{x} + \\vec{v}", "a \\leq b \\neq c \\approx d", "\\mathbb{R}^n \\to \\mathbb{C}"] {56            let l = MathRenderer.layout(latex: latex, fontSize: 12, display: true)57            #expect(l.error == nil, "“\(latex)” should typeset: \(l.error ?? "")")58            #expect(l.width > 8 && l.ascent + l.descent > 6)59            #expect(MathRenderer.validate(latex) == nil)60        }61    }62}63
Files main
Menu

Theme