Packages/FoldKit/Tests/FoldFormatTests/ValidatorTests.swift

175 lines · 8501 bytes

26 symbols in this file
  1import Foundation
  2import Testing
  3import FoldFormat
  4import FoldKitTestSupport
  5
  6@Test(arguments: ["square.fold", "half.fold", "mountain.fold", "letter.fold", "letter-sequential.fold",
  7                  "letter-staggered.fold", "quarters.fold", "occlusion.fold"])
  8func acceptsAValidModel(_ name: String) throws {
  9    #expect(validate(try FoldFile(contentsOf: fixture(name))).isEmpty)
 10}
 11
 12@Test func oppositeNormalsMakeAMutualAboveValid() {
 13    // [f, g, +1] and [g, f, +1] agree when the faces face opposite ways
 14    let pairs = normalisedFaceOrders([[0, 1, 1], [1, 0, 1]],
 15                                     normals: [SIMD3(0, 0, 1), SIMD3(0, 0, -1)])
 16    #expect(Set(pairs.map { [$0.below, $0.above] }) == [[0, 1]])
 17}
 18
 19@Test func rejectsAnOrderTheCreaseMakesImpossible() throws {
 20    #expect(validate(try FoldFile(contentsOf: fixture("order-against-crease.fold")))
 21        .contains(.orderDisagreesWithCrease(frame: 1, edge: 6)))
 22}
 23
 24@Test func rejectsInterleavedFolds() throws {
 25    let defects = validate(try FoldFile(contentsOf: fixture("interleaved-folds.fold")))
 26    #expect(defects.contains { if case .interleavedFolds(frame: 3, _) = $0 { return true }; return false })
 27}
 28
 29@Test func rejectsAFaceInsideAFoldItCrosses() throws {
 30    let defects = validate(try FoldFile(contentsOf: fixture("face-inside-fold.fold")))
 31    #expect(defects.contains(.faceInsideFold(frame: 2, face: 2, edge: 8)))
 32}
 33
 34@Test func rejectsAFoldAndUnfoldWithoutItsPeakOrder() throws {
 35    var file = try FoldFile(contentsOf: fixture("quarters.fold"))
 36    file.frames[1].faceOrdersMidFold = nil
 37    #expect(validate(file).contains(.foldAndUnfoldWithoutPeakOrder(step: 1)))
 38}
 39
 40@Test func rejectsANonConvexFace() throws {
 41    #expect(validate(try FoldFile(contentsOf: fixture("nonconvex.fold"))) == [.nonConvexFace(face: 0)])
 42}
 43
 44@Test func rejectsAFrameThatChangesTheVertexCount() throws {
 45    let defects = validate(try FoldFile(contentsOf: fixture("retopologised.fold")))
 46    #expect(defects.contains(.vertexCountChanged(frame: 2)))
 47}
 48
 49@Test func rejectsOverlappingMovingSetsWithNoMidFoldOrder() throws {
 50    let defects = validate(try FoldFile(contentsOf: fixture("letter-no-midfold.fold")))
 51    #expect(defects == [.overlappingMovingSetsWithoutMidFoldOrder(step: 1)])
 52}
 53
 54@Test func rejectsStartAndEndOrderDisagreementOnStillFaces() throws {
 55    let defects = validate(try FoldFile(contentsOf: fixture("still-face-reorder.fold")))
 56    #expect(defects.contains {
 57        if case .startAndEndOrderDisagreeOnStillFaces(step: 2, _, _) = $0 { return true }; return false })
 58}
 59
 60@Test func rejectsCyclicFaceOrder() throws {
 61    let defects = validate(try FoldFile(contentsOf: fixture("cyclic-order.fold")))
 62    #expect(defects.contains { if case .cyclicFaceOrder(frame: 2, _) = $0 { return true }; return false })
 63}
 64
 65@Test func rejectsNonIsometricTransition() throws {
 66    let defects = validate(try FoldFile(contentsOf: fixture("stretched-step.fold")))
 67    #expect(defects.contains { if case .nonIsometricTransition(step: 1, _, _, _) = $0 { return true }; return false })
 68}
 69
 70@Test func rejectsFoldAngleOutsideRange() throws {
 71    #expect(validate(try FoldFile(contentsOf: fixture("angle-200.fold")))
 72        == [.foldAngleOutOfRange(frame: 1, edge: 6, degrees: 200)])
 73}
 74
 75@Test func rejectsAFoldAngleWhoseSignDisagreesWithItsAssignment() throws {
 76    #expect(validate(try FoldFile(contentsOf: fixture("angle-sign.fold")))
 77        == [.foldAngleSignDisagreesWithAssignment(frame: 1, edge: 6)])
 78}
 79
 80@Test func rejectsAFoldedBoundaryEdge() throws {
 81    let defects = validate(try FoldFile(contentsOf: fixture("folded-boundary.fold")))
 82    #expect(defects.contains(.boundaryEdgeFolded(frame: 1, edge: 0)))
 83}
 84
 85@Test func rejectsAStepWithoutAnInstruction() throws {
 86    #expect(validate(try FoldFile(contentsOf: fixture("no-instruction.fold")))
 87        == [.missingInstruction(step: 1)])
 88}
 89
 90@Test func rejectsANonTraditionalAuthor() throws {
 91    #expect(validate(try FoldFile(contentsOf: fixture("designed-model.fold")))
 92        == [.authorNotTraditional("A. Designer")])
 93}
 94
 95@Test func rejectsEdgeReferencingMissingVertex() throws {
 96    #expect(validate(try FoldFile(contentsOf: fixture("bad-index.fold")))
 97        == [.vertexIndexOutOfRange(edge: 0)])
 98}
 99
100@Test func rejectsAFoldAndUnfoldCreaseWithoutFirstCreasedAtStep() throws {
101    // without step 3, x = 1 is only folded and unfolded in step 1, so e8 is F and flat throughout
102    var file = try FoldFile(contentsOf: fixture("quarters.fold"))
103    file.frames.removeLast()
104    file.edgesFirstCreasedAtStep[8] = nil
105    file.edgesFirstCreasedAtStep[9] = nil
106    // step 1 moves f1 and f2 and holds f0 and f3, so it creases e9 (f2 | f3) as well as e8 (f0 | f1)
107    #expect(validate(file) == [.missingFirstCreasedAtStep(edge: 8), .missingFirstCreasedAtStep(edge: 9)])
108}
109
110@Test func rejectsAShearedFaceWhoseEdgesKeepTheirLengths() throws {
111    // f1 = [v1, v2, v3, v4] in half.fold's frame 1, sheared into a parallelogram on e6 = v1–v4
112    var file = try FoldFile(contentsOf: fixture("half.fold"))
113    let slant = SIMD3<Double>(-0.6, 0.8, 0)
114    file.frames[1].verticesCoords[2] = file.frames[1].verticesCoords[1] + slant
115    file.frames[1].verticesCoords[3] = file.frames[1].verticesCoords[4] + slant
116    let defects = validate(file)
117    #expect(defects.contains(.faceDistorted(frame: 1, face: 1)))
118    #expect(!defects.contains { if case .nonIsometricTransition = $0 { return true }; return false })
119}
120
121@Test func aReportedCycleListsEachFaceAboveTheNext() throws {
122    // frame 2 puts f0 above f1, f1 above f2 and f2 above f0
123    let defects = validate(try FoldFile(contentsOf: fixture("cyclic-order.fold")))
124    let cycle = try #require(defects.lazy.compactMap { d -> [Int]? in
125        if case .cyclicFaceOrder(frame: 2, let cycle) = d { return cycle }; return nil
126    }.first)
127    let rotations = [[0, 1, 2], [1, 2, 0], [2, 0, 1]]
128    #expect(rotations.contains(cycle))
129}
130
131@Test func normalsAtGivenPositionsAndTheFacesOfEachEdge() throws {
132    let file = try FoldFile(contentsOf: fixture("half.fold"))
133    #expect(faceNormals(positions: file.frames[1].verticesCoords, faces: file.facesVertices)
134        == [SIMD3(0, 0, 1), SIMD3(0, 0, -1)])
135    #expect(edgesFaces(file) == [[0], [1], [1], [1], [0], [0], [0, 1]])
136}
137
138@Test func rejectsAMalformedPop() throws {
139    #expect(validate(try FoldFile(contentsOf: fixture("nonrigid-no-pop.fold"))).contains(.popMalformed(step: 1)))
140
141    var file = try FoldFile(contentsOf: fixture("nonrigid-no-pop.fold"))
142    let record = file.frames[1].step!
143    func withPop(popped: Int, phases: [Double]) -> StepRecord {
144        StepRecord(kind: record.kind, technique: record.technique, instruction: record.instruction, axes: record.axes,
145                   nonRigid: true,
146                   pop: PopRecord(spine: [6], openAngle: 30, phases: phases, openAxes: [],
147                                  popped: Array(file.frames[0].verticesCoords.prefix(popped)), closeAxes: []),
148                   viewpoint: record.viewpoint)
149    }
150    file.frames[1].step = withPop(popped: 5, phases: [1.0 / 3, 2.0 / 3])
151    #expect(validate(file).contains(.popMalformed(step: 1)))
152    file.frames[1].step = withPop(popped: 6, phases: [2.0 / 3, 1.0 / 3])
153    #expect(validate(file).contains(.popMalformed(step: 1)))
154    file.frames[1].step = withPop(popped: 6, phases: [1.0 / 3, 2.0 / 3])
155    #expect(!validate(file).contains(.popMalformed(step: 1)))
156}
157
158@Test func rejectsAViewpointWhoseUpIsAlongItsEye() throws {
159    var file = try FoldFile(contentsOf: fixture("half.fold"))
160    let record = file.frames[1].step!
161    func viewing(eye: SIMD3<Double>, up: SIMD3<Double>) -> StepRecord {
162        StepRecord(kind: record.kind, technique: record.technique, instruction: record.instruction, axes: record.axes,
163                   viewpoint: Viewpoint(eye: eye, up: up))
164    }
165    file.frames[1].step = viewing(eye: SIMD3(0, 0, 1), up: SIMD3(0, 0, -2))
166    #expect(validate(file).contains(.viewpointDegenerate(step: 1)))
167    file.frames[1].step = viewing(eye: SIMD3(0, 0, 1), up: SIMD3(1e-6, 0, 1))
168    #expect(validate(file).contains(.viewpointDegenerate(step: 1)))
169    file.frames[1].step = viewing(eye: SIMD3(0, 0, 1), up: .zero)
170    #expect(validate(file).contains(.viewpointDegenerate(step: 1)))
171    file.frames[1].step = viewing(eye: .zero, up: SIMD3(0, 1, 0))
172    #expect(validate(file).contains(.viewpointDegenerate(step: 1)))
173    file.frames[1].step = viewing(eye: SIMD3(0, 0, 1), up: SIMD3(0, 1, 1))
174    #expect(!validate(file).contains(.viewpointDegenerate(step: 1)))
175}