SPB Git

Repository Public

AuditorModels.swift 10.9 KB 218 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : ORIX Sheet9// File             : Packages/SheetAnalytics/Tests/AuditTests/AuditorModels.swift10// Module           : AuditTests11// Repository       : https://git.spboucher.ai/orix-sheet12// Created          : 2026-11-2113// Last Modified    : 2026-11-2114//15// =============================================================================16// AUTHOR & PROFESSIONAL AFFILIATION17// =============================================================================18//19// Author           : Simon-Pierre Boucher20// Position         : Professor of Finance21// Institution      : Université du Québec en Outaouais (UQO)22// Department       : Département des sciences administratives23// Organization     : SP Boucher24//25// Website          : https://spboucher.ai26// Git              : https://git.spboucher.ai27//28// =============================================================================29// OFFICIAL CONTACT DIRECTORY30// =============================================================================31//32// General          : info@spboucher.ai33// Professional     : contact@spboucher.ai34// Direct           : simon@spboucher.ai35// Research         : research@spboucher.ai36// Development      : dev@spboucher.ai37// Support          : support@spboucher.ai38// Security/Admin   : admin@spboucher.ai39// Notifications    : notifications@spboucher.ai40//41// Preferred Contact: support@spboucher.ai42//43// =============================================================================44// COPYRIGHT & INTELLECTUAL PROPERTY45// =============================================================================46//47// Copyright (c) 202648// Simon-Pierre Boucher49//50// All rights reserved where applicable.51//52// This file is governed by the applicable project license,53// copyright notices, institutional agreements and relevant law.54//55// Unless expressly authorized by the applicable license or56// rights holder, copying, redistribution, sublicensing,57// publication and commercial exploitation are not permitted.58//59// Existing third-party rights and notices remain unaffected.60//61// =============================================================================62// CONFIDENTIALITY & SECURITY63// =============================================================================64//65// Classification   : Public66// Distribution     : Public repository; use is governed by the project license67// Security Contact : admin@spboucher.ai68//69// Access to this source does not independently grant permission70// to redistribute or disclose confidential materials.71//72// Do not embed credentials, private keys, access tokens,73// certificates or confidential personal information.74//75// =============================================================================76// LICENSE & MAINTENANCE77// =============================================================================78//79// License          : None specified (no license file in this repository)80// Maintainer       : Simon-Pierre Boucher81// Technical Contact: dev@spboucher.ai82//83// =============================================================================84// END OF SP BOUCHER SOURCE HEADER85// =============================================================================8687import Foundation88import SheetCore89@testable import Audit9091/// Workbooks used by the auditor tests.92enum AuditModels {93    /// Clean two-year loan schedule (monthly). Inputs B1:B3, derived B4:B6, schedule rows 9–32, totals row 34.94    static func loan(_ b: ACBook, sheet: Int = 0) {95        let labels = ["Principal", "Annual rate", "Years", "Monthly rate", "Periods", "Payment"]96        for (i, l) in labels.enumerated() { b.setText("A\(i + 1)", l, sheet: sheet) }97        b.set("B1", "250000", sheet: sheet); b.set("B2", "0.06", sheet: sheet); b.set("B3", "2", sheet: sheet)98        b.set("B4", "=B2/12", sheet: sheet); b.set("B5", "=B3*12", sheet: sheet)99        b.set("B6", "=B1*B4/(1-(1+B4)^-B5)", sheet: sheet)100        for (i, h) in ["Period", "Opening", "Interest", "Principal", "Closing"].enumerated() { b.setText(A1.columnName(i) + "8", h, sheet: sheet) }101        for r in 9...32 {102            b.set("A\(r)", r == 9 ? "1" : "=A\(r - 1)+1", sheet: sheet)103            b.set("B\(r)", r == 9 ? "=B1" : "=E\(r - 1)", sheet: sheet)104            b.set("C\(r)", "=B\(r)*$B$4", sheet: sheet)105            b.set("D\(r)", "=$B$6-C\(r)", sheet: sheet)106            b.set("E\(r)", "=B\(r)-D\(r)", sheet: sheet)107        }108        b.setText("A34", "Totals", sheet: sheet)109        b.set("C34", "=SUM(C9:C32)", sheet: sheet); b.set("D34", "=SUM(D9:D32)", sheet: sheet)110    }111112    /// Clean five-year DCF. Years in B1:F1, revenue row 3, FCF row 4, discount factors row 5, PV row 6, inputs B10:B14.113    static func dcf(_ b: ACBook, sheet: Int = 0) {114        for (i, l) in ["Year", "", "Revenue", "Free cash flow", "Discount factor", "Present value", "Sum of PV", "Terminal value", "Enterprise value",115                       "Base revenue", "Growth", "FCF margin", "WACC", "Terminal growth"].enumerated() where !l.isEmpty { b.setText("A\(i + 1)", l, sheet: sheet) }116        b.set("B10", "1000", sheet: sheet); b.set("B11", "0.05", sheet: sheet); b.set("B12", "0.2", sheet: sheet)117        b.set("B13", "0.1", sheet: sheet); b.set("B14", "0.02", sheet: sheet)118        for c in 1...5 {119            let col = A1.columnName(c), prev = A1.columnName(c - 1)120            b.set(col + "1", String(c), sheet: sheet)121            b.set(col + "3", c == 1 ? "=B10" : "=\(prev)3*(1+$B$11)", sheet: sheet)122            b.set(col + "4", "=\(col)3*$B$12", sheet: sheet)123            b.set(col + "5", "=1/(1+$B$13)^\(col)1", sheet: sheet)124            b.set(col + "6", "=\(col)4*\(col)5", sheet: sheet)125        }126        b.set("B7", "=SUM(B6:F6)", sheet: sheet)127        b.set("B8", "=F4*(1+$B$14)/($B$13-$B$14)*F5", sheet: sheet)128        b.set("B9", "=B7+B8", sheet: sheet)129    }130131    /// Sales table rows 2–11 with a totals row 12: Region, Units, Price, Revenue (=B*C), Tax (=D*$I$1), Total (=D+E). Tax rate in I1.132    static func sales(_ b: ACBook, sheet: Int = 0) {133        for (i, h) in ["Region", "Units", "Price", "Revenue", "Tax", "Total"].enumerated() { b.setText(A1.columnName(i) + "1", h, sheet: sheet) }134        b.setText("H1", "Tax rate", sheet: sheet); b.set("I1", "0.15", sheet: sheet)135        let regions = ["North", "South", "East", "West", "Centre"]136        for r in 2...11 {137            b.setText("A\(r)", regions[(r - 2) % 5], sheet: sheet)138            b.set("B\(r)", String(10 * r), sheet: sheet); b.set("C\(r)", String(Double(r) + 0.5), sheet: sheet)139            b.set("D\(r)", "=B\(r)*C\(r)", sheet: sheet); b.set("E\(r)", "=D\(r)*$I$1", sheet: sheet); b.set("F\(r)", "=D\(r)+E\(r)", sheet: sheet)140        }141        b.setText("A12", "Total", sheet: sheet)142        for c in ["B", "D", "E", "F"] { b.set("\(c)12", "=SUM(\(c)2:\(c)11)", sheet: sheet) }143    }144145    /// Compact, comparable description of a finding.146    static func brief(_ f: AuditFinding) -> String {147        let loc = f.ranges.isEmpty ? "-" : f.ranges.map(\.a1).joined(separator: " ")148        return "\(f.severity.rawValue) \(f.rule.rawValue) \(f.sheet ?? "*") \(loc)"149    }150}151152extension AuditModels {153    /// The sales sheet (index 0) with one seeded defect per rule, plus the sheets "Data" (table), "Dup" and the hidden "Calc".154    static func seeded() -> ACBook {155        let b = ACBook(sheets: ["Sales", "Data", "Dup", "Calc"])156        sales(b)157        // inconsistent formula + hardcoded constant158        b.set("D7", "=B7*C7+25")159        // hardcoded number over a formula160        b.set("F5", "999")161        // text that looks like a number, used by D4 (arithmetic) and B12 (SUM)162        b.setText("B4", "120")163        // blank input read by D9164        b.engine.set(sheet: 0, "C9", .blank)165        // error born in K2, propagated to L2166        b.set("K2", "=D2/J2"); b.set("L2", "=K2+1")167        // broken reference168        b.set("K4", "=#REF!*2")169        // circular reference170        b.set("K6", "=L6+1"); b.set("L6", "=K6+1")171        // volatile functions, one with a whole-column reference172        b.set("K8", "=RAND()"); b.set("K9", "=COUNT(B:B)+RAND()")173        // external workbook174        b.set("K11", "='[Budget.xlsx]Plan'!A1")175        // complex formula (nesting depth 7)176        b.set("K13", "=IF(B2>1,IF(B3>1,IF(B5>1,IF(B6>1,IF(B7>1,IF(B8>1,MAX(B2,B3),0),0),0),0),0),0)")177        // defined names: unused, broken, used178        b.wb.setDefinedName(DefinedName(name: "Unused", formula: "Sales!$I$1"))179        b.wb.setDefinedName(DefinedName(name: "Broken", formula: "Sales!#REF!"))180        b.wb.setDefinedName(DefinedName(name: "TaxRate", formula: "Sales!$I$1"))181        b.set("K15", "=D2*TaxRate")182        // hidden row with a value and a formula; hidden column with a value183        b.set("A20", "77"); b.set("B20", "=A20*2"); b.set("P3", "5")184        b.wb.sheets[0].meta.hiddenRows.insert(19)185        b.wb.sheets[0].meta.hiddenCols.insert(15)186        // merged cells inside a range that a formula reads187        b.set("N2", "1"); b.set("N3", "2"); b.set("N4", "3"); b.set("N6", "=SUM(N2:O4)")188        b.wb.sheets[0].meta.merges = [RangeRef(a1: "N2:O2")!, RangeRef(a1: "R30:S30")!]189190        // Data: table T (A1:D7): Item, Amount, Double (=B*2), Flag191        for (i, h) in ["Item", "Amount", "Double", "Flag"].enumerated() { b.setText(A1.columnName(i) + "1", h, sheet: 1) }192        for r in 2...7 {193            b.setText("A\(r)", "item\(r)", sheet: 1)194            b.set("B\(r)", String(r * 100), sheet: 1)195            b.set("C\(r)", "=B\(r)*2", sheet: 1)196            b.set("D\(r)", r % 2 == 0 ? "TRUE" : "FALSE", sheet: 1)197        }198        b.setText("B5", "n/a", sheet: 1)                           // mixed types in Amount199        b.set("C6", "=IF(B6>100,\"big\",B6*2)", sheet: 1)          // formula returning text in a numeric column200        b.setText("F2", "42", sheet: 1); b.setText("F3", "43", sheet: 1); b.setText("F4", "007", sheet: 1)   // numbers stored as text (007 is an identifier)201        var t = TableDef(id: 1, name: "T", range: RangeRef(a1: "A1:D7")!, columns: ["Item", "Amount", "Double", "Flag"].map { TableColumn(name: $0) })202        t.hasHeader = true203        b.wb.sheets[1].meta.tables = [t]204        b.wb.sheets[1].meta.merges = [RangeRef(a1: "A3:A4")!]      // merge inside the table205206        // Dup: the same SUM over a fixed range in six formulas207        for r in 1...10 { b.set("A\(r)", String(r), sheet: 2) }208        for r in 1...6 { b.set("B\(r)", "=A\(r)/SUM($A$1:$A$10)", sheet: 2) }209210        // Calc: hidden sheet with content211        b.set("A1", "3", sheet: 3); b.set("A2", "=A1*A1", sheet: 3)212        b.wb.sheets[3].meta.isHidden = true213        b.engine.rebuildGraph()214        b.engine.recalculate()215        return b216    }217}218
Files main
Menu

Theme