Tests/OrgCoreTests/TableFormulaTests.swift
110 lines · 7099 bytes
1import Foundation
2import Testing
3@testable import OrgCore
4
5struct TableFormulaTests {
6 static let tables = [
7 "| a | b |\n|---+---|\n| 1 | |\n| 2 | |\n| 3 | |\n#+TBLFM: $2=$1*2\n",
8 "| x | y | sum |\n|---+---+-----|\n| 1 | 2 | |\n| 3.5 | 4 | |\n|---+---+-----|\n| | | |\n#+TBLFM: $3=$1+$2::@>$1=vsum(@I..@II)::@>$2=vmean(@2$2..@3$2)\n",
9 "| qty | price | total | share |\n|-----+-------+-------+-------|\n| 3 | 19.99 | | |\n| 1 | 5 | | |\n| 12 | 0.5 | | |\n|-----+-------+-------+-------|\n| | | | |\n#+TBLFM: $3=$1*$2;%.2f::$4=$3/@>$3;f3::@>$3=vsum(@I..@II)\n",
10 "| n | sq | cube | root |\n|---+----+------+------|\n| 1 | | | |\n| 2 | | | |\n| 10 | | | |\n#+TBLFM: $2=$1^2::$3=$1^3::$4=sqrt($1)\n",
11 "| a | b |\n| 1 | |\n| 2 | |\n| 7 | |\n#+TBLFM: $2=$1/2\n",
12 "| a | prev | next |\n|---+------+------|\n| 1 | | |\n| 2 | | |\n| 3 | | |\n#+TBLFM: $2=@-1$1::$3=@+1$1\n",
13 "| a | b | c |\n|---+---+---|\n| 1 | 2 | |\n| 4 | 5 | |\n#+TBLFM: $3=$-1*$-2::@2$3=100\n",
14 "| r | v |\n|---+---|\n| 1 | |\n| 2 | |\n| 3 | |\n#+TBLFM: @2$2..@4$2=$1*10\n",
15 "| r | v |\n|---+---|\n| 1 | |\n| 2 | |\n#+TBLFM: $>=@#*$#\n",
16 "| a | b | c |\n|---+---+---|\n| 1 | | |\n| | 2 | |\n#+TBLFM: $3=$1+$2\n",
17 "| a | b |\n|---+---|\n| 5 | |\n| 6 | |\n#+TBLFM: $2=$1*3\n#+TBLFM: $2=$1+100\n",
18 "| a | b |\n|---+---|\n| 1 |\n| 2 | |\n#+TBLFM: $3=$1*$1\n",
19 "| a | b | c |\n|---+---+---|\n| 1 | 2 | |\n| 3 | 4 | |\n#+TBLFM: $3=vsum($1..$2)::@2=7\n",
20 "| a | b |\n|---+---|\n| 1.25 | |\n| 0.1 | |\n| 1e3 | |\n| -2 | |\n#+TBLFM: $2=$1/7\n",
21 "| deg | sin | cos |\n|-----+-----+-----|\n| 0 | | |\n| 30 | | |\n| 45 | | |\n| 90 | | |\n#+TBLFM: $2=sin($1)::$3=cos($1);%.3f\n",
22 " | a | b |\n |---+---|\n | 2 | |\n | 3 | |\n #+TBLFM: $2=$1^0.5;n4\n",
23 "| a | b |\n|---+---|\n| x | |\n| 2 | |\n#+TBLFM: $2=$1*2\n",
24 "| a | b |\n|---+---|\n| 1 | |\n|---+---|\n| 2 | |\n| 3 | |\n#+TBLFM: $2=@I$1+@II$1+@-I$1\n",
25 "| a | b | c |\n|---+---+---|\n| 1 | 2 | |\n#+TBLFM: $3=($1+$2)*2/3-1::$1=$1\n",
26 "| a |\n|---|\n| 1 |\n| 2 |\n|---|\n| |\n#+TBLFM: @>$1=vmax(@I..@II)-vmin(@I..@II)+vcount(@I..@II)+vmedian(@I..@II)\n",
27 ]
28
29 static func carets(_ text: String) -> [Int] {
30 let ns = text as NSString
31 var result: [Int] = []
32 var location = 0
33 while location < ns.length {
34 let line = ns.lineRange(for: NSRange(location: location, length: 0))
35 let content = ns.substring(with: line)
36 if let bar = content.firstIndex(of: "|"), !content.contains("|-") {
37 // Inside the first field of the row.
38 result.append(line.location + content.distance(from: content.startIndex, to: bar) + 2)
39 }
40 if content.uppercased().contains("#+TBLFM") { result.append(line.location + 3) }
41 location = NSMaxRange(line)
42 }
43 return result
44 }
45
46 @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil))
47 func matchesEmacs() throws {
48 var cases: [EmacsOracle.Case] = []
49 var ours: [(text: String, caret: Int, failure: String?)] = []
50 for text in Self.tables {
51 for caret in Self.carets(text) {
52 let onTblfm = (text as NSString).substring(with: (text as NSString).lineRange(for: NSRange(location: caret, length: 0))).uppercased().contains("#+TBLFM")
53 let commands: [(any OrgCommand, String)] = onTblfm
54 ? [(TableRecalculate(all: true), "(org-ctrl-c-ctrl-c)")]
55 : [(TableRecalculate(all: true), "(org-table-recalculate t)"), (TableRecalculate(all: false), "(org-table-recalculate nil)")]
56 for (command, form) in commands {
57 cases.append(EmacsOracle.Case(text: text, point: EmacsOracle.point(caret, in: text), form: form))
58 ours.append(runCommand(command, text, caret: caret))
59 }
60 }
61 }
62 let emacs = try EmacsOracle.run(cases)
63 var fallbacks = 0
64 for (i, (mine, theirs)) in zip(ours, emacs).enumerated() {
65 if let failure = mine.failure, failure.hasPrefix("external:") {
66 fallbacks += 1
67 // Emacs gives symbolic results for text and division by zero.
68 #expect(failure.contains("non-numeric field") || failure.contains("division by zero"), "\(failure)")
69 continue
70 }
71 if !theirs.error.isEmpty {
72 // Emacs keeps the fields written before the error; nothing changes here.
73 #expect(mine.failure == theirs.error && mine.text == cases[i].text)
74 continue
75 }
76 let expected = (theirs.text, EmacsOracle.offset(theirs.point, in: theirs.text), theirs.error.isEmpty ? nil : theirs.error)
77 #expect(mine.text == expected.0 && mine.caret == expected.1 && mine.failure == expected.2,
78 "\(cases[i].form) at \(cases[i].point) in\n\(cases[i].text)\nours:\n\(mine.text)@\(mine.caret) \(mine.failure ?? "")\nemacs:\n\(theirs.text)@\(expected.1) \(theirs.error)")
79 }
80 #expect(fallbacks < ours.count / 2, "\(fallbacks) fallbacks")
81 }
82
83 /// `ORGSTAR_ORACLE_CORPUS=<folder>`: C-c C-c on every `#+TBLFM` line below the folder.
84 @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_ORACLE_CORPUS"] != nil))
85 func corpusMatchesEmacs() throws {
86 let root = URL(fileURLWithPath: ProcessInfo.processInfo.environment["ORGSTAR_ORACLE_CORPUS"]!)
87 let files = FileManager.default.enumerator(at: root, includingPropertiesForKeys: nil)!
88 .compactMap { $0 as? URL }.filter { $0.pathExtension == "org" }.sorted { $0.path < $1.path }
89 var cases: [EmacsOracle.Case] = []
90 var ours: [(text: String, caret: Int, failure: String?)] = []
91 for file in files {
92 guard let text = try? String(contentsOf: file, encoding: .utf8), text.uppercased().contains("#+TBLFM") else { continue }
93 let ns = text as NSString
94 var location = 0
95 while location < ns.length {
96 let line = ns.lineRange(for: NSRange(location: location, length: 0))
97 if ns.substring(with: line).trimmingCharacters(in: .whitespaces).uppercased().hasPrefix("#+TBLFM") {
98 cases.append(EmacsOracle.Case(text: text, point: EmacsOracle.point(line.location, in: text), form: "(org-ctrl-c-ctrl-c)"))
99 ours.append(runCommand(TableRecalculate(all: true), text, caret: line.location))
100 }
101 location = NSMaxRange(line)
102 }
103 }
104 let emacs = try EmacsOracle.run(cases)
105 for (mine, theirs) in zip(ours, emacs) where !(mine.failure ?? "").hasPrefix("external:") {
106 #expect(mine.text == theirs.text, "\(mine.failure ?? "")")
107 }
108 print("TBLFM lines: \(cases.count), native: \(ours.filter { !($0.failure ?? "").hasPrefix("external:") }.count)")
109 }
110}