SPB Git

Repository Public License

Support.swift 8.3 KB 167 lines swift

1// ORIX Convert — Packages/EngineScientific/Tests/EngineScientificTests/Support.swift2// Description: Test support: temporary folders, a conversion runner that collects warnings and runs every validation3//              check, and helpers to build HDF5 fixtures directly with the C library.4// Author: Simon-Pierre Boucher — contact@spboucher.ai5import Foundation6import Testing7import CHDF58import CHDF5Shim9import ConvertCore10@testable import EngineScientific1112final class TempDir: @unchecked Sendable {13    let url: URL14    init() {15        url = FileManager.default.temporaryDirectory.appendingPathComponent("orix-sci-tests-\(UUID().uuidString)", isDirectory: true)16        try? FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)17    }18    deinit { try? FileManager.default.removeItem(at: url) }19    func file(_ name: String) -> URL { url.appendingPathComponent(name) }20    func sub(_ name: String) -> URL {21        let u = url.appendingPathComponent(name, isDirectory: true)22        try? FileManager.default.createDirectory(at: u, withIntermediateDirectories: true)23        return u24    }25}2627final class WarningBox: @unchecked Sendable {28    private let lock = NSLock()29    private var items: [ConversionWarning] = []30    func add(_ w: ConversionWarning) { lock.lock(); items.append(w); lock.unlock() }31    var all: [ConversionWarning] { lock.lock(); defer { lock.unlock() }; return items }32    func first(_ code: String) -> ConversionWarning? { all.first { $0.code == code } }33    func all(_ code: String) -> [ConversionWarning] { all.filter { $0.code == code } }34    var codes: [String] { all.map(\.code) }35}3637final class ProgressBox: @unchecked Sendable {38    private let lock = NSLock()39    private var values: [Double?] = []40    func add(_ f: Double?) { lock.lock(); values.append(f); lock.unlock() }41    var fractions: [Double] { lock.lock(); defer { lock.unlock() }; return values.compactMap { $0 } }42}4344struct Converted {45    let output: ConversionOutput46    let warnings: WarningBox47    let checks: [ValidationCheck]48    let progress: ProgressBox49    var file: URL { output.files[0] }50    func text(_ name: String? = nil) throws -> String { try String(contentsOf: name.map { file.appendingPathComponent($0) } ?? file, encoding: .utf8) }51}5253/// Converts and validates; every check must pass.54func convert(_ source: URL, _ from: FormatID, to: FormatID, in temp: TempDir, operation: String? = nil, options: [String: OptionValue] = [:], name: String? = nil) async throws -> Converted {55    let engine = ScientificEngine()56    let work = temp.sub("work-\(UUID().uuidString.prefix(8))")57    let box = WarningBox(), progress = ProgressBox()58    let request = ConversionRequest(source: source, sourceFormat: from, destinationFormat: to, operation: operation, options: ConversionOptions(options), outputBaseName: name)59    #expect(engine.path(for: request) != nil, "path \(from) → \(to) \(operation ?? "") is not declared")60    let context = ConversionContext(workDirectory: work, progress: { progress.add($0.fraction) }, warn: { box.add($0) })61    let output = try await engine.convert(request, context: context)62    let generic = OutputValidator.validate(output, destinationFormat: to, workDirectory: work)63    let checks = generic + (try await engine.validate(output, request: request, source: nil))64    for c in checks { #expect(c.passed, "\(from)→\(to) check \(c.name): \(c.detail)") }65    return Converted(output: output, warnings: box, checks: checks, progress: progress)66}6768/// The message of the error a conversion throws.69func failure(_ source: URL, _ from: FormatID, to: FormatID, in temp: TempDir, operation: String? = nil, options: [String: OptionValue] = [:]) async -> String {70    do {71        _ = try await convert(source, from, to: to, in: temp, operation: operation, options: options)72        Issue.record("\(from) → \(to) was expected to be refused")73        return ""74    } catch let e as ConversionError {75        return e.errorDescription ?? "\(e)"76    } catch { return "\(error)" }77}7879// MARK: - HDF5 through the C library8081/// Small helpers over the C API. Every call site is inside `H5.sync`.82enum C {83    static let orderTracked = UInt32(H5P_CRT_ORDER_TRACKED | H5P_CRT_ORDER_INDEXED)8485    /// Fixtures use the library's default format bounds (the newest features allowed), like files from the wild.86    static func create(_ url: URL) -> hid_t {87        let fapl = orix_h5_fapl(0)88        defer { H5Pclose(fapl) }89        let f = H5Fcreate(url.path, UInt32(H5F_ACC_TRUNC), 0, fapl)90        precondition(f >= 0, "H5Fcreate failed: \(H5.lastError())")91        return f92    }9394    static func group(_ loc: hid_t, _ name: String) -> hid_t {95        let g = H5Gcreate2(loc, name, 0, 0, 0)96        precondition(g >= 0, "H5Gcreate2 \(name) failed: \(H5.lastError())")97        return g98    }99100    static func stringType(_ size: Int, pad: H5T_str_t = H5T_STR_NULLPAD, utf8: Bool = false) -> hid_t {101        let t = H5Tcopy(H5.type(ORIX_T_C_S1))102        H5Tset_size(t, size)103        H5Tset_strpad(t, pad)104        if utf8 { H5Tset_cset(t, H5T_CSET_UTF8) }105        return t106    }107108    static func variableString() -> hid_t {109        let t = H5Tcopy(H5.type(ORIX_T_C_S1))110        H5Tset_size(t, orix_h5_variable())111        H5Tset_cset(t, H5T_CSET_UTF8)112        return t113    }114115    /// Creates a dataset from raw memory laid out as `memory`. `dims == nil` gives a scalar. Returns the open id.116    @discardableResult117    static func dataset(_ loc: hid_t, _ name: String, file: hid_t, memory: hid_t, dims: [hsize_t]?, maxDims: [hsize_t]? = nil, dcpl: hid_t = 0, data: UnsafeRawPointer?, close: Bool = true) -> hid_t {118        let space = dims.map { d in d.withUnsafeBufferPointer { p in (maxDims ?? d).withUnsafeBufferPointer { m in H5Screate_simple(Int32(d.count), p.baseAddress, m.baseAddress) } } } ?? H5Screate(H5S_SCALAR)119        defer { H5Sclose(space) }120        let d = H5Dcreate2(loc, name, file, space, 0, dcpl, 0)121        precondition(d >= 0, "H5Dcreate2 \(name) failed: \(H5.lastError())")122        if let data { precondition(H5Dwrite(d, memory, Int64(H5S_ALL), Int64(H5S_ALL), 0, data) >= 0, "H5Dwrite \(name) failed: \(H5.lastError())") }123        if close { H5Dclose(d); return -1 }124        return d125    }126127    @discardableResult128    static func numbers<T>(_ loc: hid_t, _ name: String, _ type: orix_type_t, _ values: [T], dims: [hsize_t]? = nil, maxDims: [hsize_t]? = nil, dcpl: hid_t = 0, fileType: hid_t? = nil, close: Bool = true) -> hid_t {129        values.withUnsafeBytes { dataset(loc, name, file: fileType ?? H5.type(type), memory: H5.type(type), dims: dims ?? [hsize_t(values.count)], maxDims: maxDims, dcpl: dcpl, data: $0.baseAddress, close: close) }130    }131132    static func attribute(_ obj: hid_t, _ name: String, file: hid_t, memory: hid_t, dims: [hsize_t]?, data: UnsafeRawPointer) {133        let space = dims.map { d in d.withUnsafeBufferPointer { H5Screate_simple(Int32(d.count), $0.baseAddress, nil) } } ?? H5Screate(H5S_SCALAR)134        defer { H5Sclose(space) }135        let a = H5Acreate2(obj, name, file, space, 0, 0)136        precondition(a >= 0, "H5Acreate2 \(name) failed: \(H5.lastError())")137        precondition(H5Awrite(a, memory, data) >= 0, "H5Awrite \(name) failed: \(H5.lastError())")138        H5Aclose(a)139    }140141    static func attribute<T>(_ obj: hid_t, _ name: String, _ type: orix_type_t, _ values: [T], scalar: Bool = false) {142        values.withUnsafeBytes { attribute(obj, name, file: H5.type(type), memory: H5.type(type), dims: scalar ? nil : [hsize_t(values.count)], data: $0.baseAddress!) }143    }144145    static func attribute(_ obj: hid_t, _ name: String, text: String) {146        let t = stringType(max(1, text.utf8.count), pad: H5T_STR_NULLTERM)147        defer { H5Tclose(t) }148        var bytes = Array(text.utf8)149        if bytes.isEmpty { bytes = [0] }150        bytes.withUnsafeBytes { attribute(obj, name, file: t, memory: t, dims: nil, data: $0.baseAddress!) }151    }152153    /// Fixed-length strings packed into `size`-byte slots.154    static func packed(_ strings: [String], size: Int, pad: UInt8 = 0) -> [UInt8] {155        strings.flatMap { s -> [UInt8] in156            let b = Array(s.utf8.prefix(size))157            return b + [UInt8](repeating: pad, count: size - b.count)158        }159    }160161    static func open(_ loc: hid_t, _ path: String) -> hid_t {162        let d = H5Dopen2(loc, path, 0)163        precondition(d >= 0, "H5Dopen2 \(path) failed: \(H5.lastError())")164        return d165    }166}167
Files main
Menu

Theme