import Foundation import Testing import simd import FoldCompiler import FoldFormat import Folding import FoldKitTestSupport import Models import Notation @Test func everyShippedStepEndsOnItsEndFrame() throws { // the step records and the frames must agree: interpolating a step to t = 1 reproduces frame n for file in try ModelLibrary.all() { for step in try readSteps(file) { let i = FoldInterpolator(file: file, step: step) #expect(zip(i.positions(at: 1), file.frames[step.index].verticesCoords) .allSatisfy { simd_distance($0, $1) < 1e-6 * sheetSize(file) }, "\(file.fileTitle ?? "?") step \(step.index)") } } } @Test func everyShippedModelValidates() throws { for file in try ModelLibrary.all() { #expect(validate(file).isEmpty, "\(file.fileTitle ?? "?")") } } @Test func everyShippedModelReadsItsSteps() throws { for file in try ModelLibrary.all() { #expect(try readSteps(file).count == file.stepCount) } } @Test func noShippedModelStretchesAtAnyStep() throws { // flagged non-rigid steps are held to Task 0b's bound instead for file in try ModelLibrary.all() { for step in try readSteps(file) where !step.nonRigid { let i = FoldInterpolator(file: file, step: step) for t in stride(from: 0.0, through: 1.0, by: 1.0 / 256) { #expect(maxEdgeLengthDrift(i.positions(at: t), file) < 1e-6, "\(file.fileTitle ?? "?") step \(step.index) t \(t)") } } } } @Test func noShippedModelInterpenetratesAtAnyStep() throws { // non-rigid steps bend their faces; everyFlaggedStepStaysWithinTheBound checks them on the mesh for file in try ModelLibrary.all() { for step in try readSteps(file) where !step.nonRigid { let i = FoldInterpolator(file: file, step: step) #expect(interpenetrations(i, faces: file.facesVertices, samples: 64).isEmpty, "\(file.fileTitle ?? "?") step \(step.index)") } } } @Test func noShippedModelMovesThroughAStillFace() throws { for file in try ModelLibrary.all() { #expect(layerMotionDefects(file).isEmpty, "\(file.fileTitle ?? "?")") } } @Test func everyModelIsAttributedAsTraditional() throws { for file in try ModelLibrary.all() { #expect(file.fileAuthor == "Traditional") } } @Test func everyShippedModelRegeneratesFromItsScript() throws { for name in try ModelLibrary.names() { let recompiled = try compile(FoldScript(contentsOf: script(name)), pattern: FoldFile(contentsOf: pattern(name))) #expect(structurallyEqual(recompiled, try ModelLibrary.model(named: name), tolerance: 1e-12), "\(name)") } } @Test func everyFlaggedStepStaysWithinTheBound() throws { // decisions 0002 and 0003: 10% largest edge strain on the render mesh, and nothing passes // through anything in any phase for file in try ModelLibrary.all() { for step in try readSteps(file) where step.nonRigid { let i = FoldInterpolator(file: file, step: step) let (lo, hi) = (step.pop!.phases.lowerBound, step.pop!.phases.upperBound) let worst = (0...128).map { strain(i.meshPositions(at: lo + Double($0) / 128 * (hi - lo)), mesh: i.mesh) }.max()! #expect(worst <= 0.10, "\(file.fileTitle ?? "?") step \(step.index)") #expect(meshInterpenetrations(i, samples: 64).isEmpty, "\(file.fileTitle ?? "?") step \(step.index)") } } } @Test func everyShippedStepBuildsWhatTheAppShowsOfIt() throws { // per step, what the app's session builds, with its camera at the step's authored viewpoint for file in try ModelLibrary.all() { for n in 1.. Camera { func bounds(_ p: [SIMD3]) -> (SIMD3, SIMD3) { (p.dropFirst().reduce(p[0], simd_min), p.dropFirst().reduce(p[0], simd_max)) } let (low, high) = bounds(file.frames[0].verticesCoords) let (startLow, startHigh) = bounds(file.frames[step.index - 1].verticesCoords) let target = (startLow + startHigh) / 2 let fieldOfView = Double.pi / 6 let distance = simd_length(high - low) / 2 / sin(min(fieldOfView / 2, atan(tan(fieldOfView / 2) * aspect))) let eye = simd_normalize(step.viewpoint.eye) let up = simd_normalize(step.viewpoint.up - simd_dot(step.viewpoint.up, eye) * eye) return Camera(eye: target + distance * eye, target: target, up: up, fieldOfView: fieldOfView) } @Test func everyModelHasAnAuthoringRecord() throws { for name in try ModelLibrary.names() { #expect(FileManager.default.fileExists(atPath: authoringDoc(name).path)) } } @Test func theBoxRaisesItsEndsOnSolvedCurvesAndEndsStanding() throws { // decision 0002: the end walls are rigid, coupled steps; the box is not flat at the end let box = try ModelLibrary.model(named: "box") let raising = try readSteps(box).filter { $0.axes.contains { $0.curve != nil } } #expect(raising.map(\.index) == [9, 11]) #expect(raising.allSatisfy { !$0.nonRigid }) let heights = box.frames[box.stepCount].verticesCoords.map(\.z) #expect(heights.max()! - heights.min()! > 0.4) } @Test func theSampanTurnsInsideOutRigidlyAndEndsWithItsSidesUp() throws { // decision 0002: the inside-out step closes, so it is rigid; at the end the floor's corners // (½, 1½), (3½, 1½), (3½, 2½), (½, 2½) lie level and the sides' top corners (1, 1), (3, 1), // (3, 3), (1, 3) stand at least 0.4 above them let sampan = try ModelLibrary.model(named: "sampan") #expect(try readSteps(sampan).allSatisfy { !$0.nonRigid }) let flat = sampan.frames[0].verticesCoords, last = sampan.frames[sampan.stepCount].verticesCoords func height(_ x: Double, _ y: Double) throws -> Double { try #require(flat.indices.first { abs(flat[$0].x - x) < 1e-9 && abs(flat[$0].y - y) < 1e-9 }.map { last[$0].z }) } let floor = try [(0.5, 1.5), (3.5, 1.5), (3.5, 2.5), (0.5, 2.5)].map { try height($0.0, $0.1) } #expect(floor.max()! - floor.min()! < 1e-9) for (x, y) in [(1.0, 1.0), (3.0, 1.0), (3.0, 3.0), (1.0, 3.0)] { #expect(try height(x, y) - floor[0] > 0.4, "rim corner (\(x), \(y))") } } @Test func theCupContainsATurnOverStep() throws { #expect(try readSteps(ModelLibrary.model(named: "cup")).contains { $0.kind == .turnOver }) } @Test func foldlintExitsNonZeroOnADefectiveFile() throws { #expect(try runFoldlint(on: fixture("bad-index.fold")).exitCode == 1) }