|
| 1 | +package ifc |
| 2 | + |
| 3 | +import ( |
| 4 | + "math" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +// TestBuildImport_DeductionAndGrossReachNetArea pins the reachability of the |
| 9 | +// two halves NetArea is the difference of. NetArea alone cannot be aggregated: |
| 10 | +// a host with no IfcRelVoidsElement is absent from the reconciliation, so |
| 11 | +// summing nets over a facade silently drops every solid wall. Netting a total |
| 12 | +// means subtracting the DEDUCTION from the gross being totalled, which is only |
| 13 | +// possible if both cross the import contract. |
| 14 | +// |
| 15 | +// The identity is the assertion that matters. ProjectedGross - OpeningDeduction |
| 16 | +// must equal NetArea exactly, because all three are read from one |
| 17 | +// reconciliation on one plane; if they ever disagree, the pair cannot be used |
| 18 | +// to net anything and a consumer would be quietly inventing area. |
| 19 | +func TestBuildImport_DeductionAndGrossReachNetArea(t *testing.T) { |
| 20 | + f := parseFixture(t, "geometry/testdata/synthetic/netarea_rect_window.ifc") |
| 21 | + m, err := BuildImport(f) |
| 22 | + if err != nil { |
| 23 | + t.Fatalf("BuildImport: %v", err) |
| 24 | + } |
| 25 | + |
| 26 | + var withNet int |
| 27 | + for _, n := range m.Nodes { |
| 28 | + if (n.NetArea == nil) != (n.OpeningDeduction == nil) { |
| 29 | + t.Errorf("node %q: NetArea and OpeningDeduction disagree about presence "+ |
| 30 | + "(net=%v deduction=%v)", n.Name, n.NetArea, n.OpeningDeduction) |
| 31 | + } |
| 32 | + if (n.NetArea == nil) != (n.ProjectedGross == nil) { |
| 33 | + t.Errorf("node %q: NetArea and ProjectedGross disagree about presence "+ |
| 34 | + "(net=%v gross=%v)", n.Name, n.NetArea, n.ProjectedGross) |
| 35 | + } |
| 36 | + if n.NetArea == nil { |
| 37 | + continue |
| 38 | + } |
| 39 | + // Report the missing field and move on rather than dereferencing it: a |
| 40 | + // test that panics on the very regression it exists to catch reports a |
| 41 | + // stack trace instead of a diagnosis, and takes the remaining nodes' |
| 42 | + // assertions down with it. |
| 43 | + if n.OpeningDeduction == nil || n.ProjectedGross == nil { |
| 44 | + continue |
| 45 | + } |
| 46 | + withNet++ |
| 47 | + |
| 48 | + if got := math.Abs(*n.ProjectedGross - *n.OpeningDeduction - *n.NetArea); got > 1e-9 { |
| 49 | + t.Errorf("node %q: gross(%v) - deduction(%v) != net(%v), off by %v", |
| 50 | + n.Name, *n.ProjectedGross, *n.OpeningDeduction, *n.NetArea, got) |
| 51 | + } |
| 52 | + // The fixture is a 4x3 wall with a 1x1 void: 12 gross, 1 deducted. |
| 53 | + // Pins units and transport; the union semantics live in geometry's |
| 54 | + // own tests. |
| 55 | + if got := *n.ProjectedGross; got < 11.999 || got > 12.001 { |
| 56 | + t.Errorf("node %q: ProjectedGross = %v, want 12 (4x3 wall)", n.Name, got) |
| 57 | + } |
| 58 | + if got := *n.OpeningDeduction; got < 0.999 || got > 1.001 { |
| 59 | + t.Errorf("node %q: OpeningDeduction = %v, want 1 (1x1 void)", n.Name, got) |
| 60 | + } |
| 61 | + } |
| 62 | + if withNet == 0 { |
| 63 | + t.Fatal("fixture produced no node with a trusted NetArea; the assertions above never ran") |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +// TestBuildImport_UntrustedHostPublishesNoDeduction confirms the untrusted path |
| 68 | +// stays silent in the new fields too. NetArea.OpeningDeduction is documented as |
| 69 | +// 0 when untrusted, and a published 0 is the failure mode worth guarding: it |
| 70 | +// satisfies a presence check and reads as "this wall has no openings", turning |
| 71 | +// a refused measurement into a confident claim that the whole gross is net. |
| 72 | +func TestBuildImport_UntrustedHostPublishesNoDeduction(t *testing.T) { |
| 73 | + f := parseFixture(t, "geometry/testdata/synthetic/netarea_mostly_glass.ifc") |
| 74 | + m, err := BuildImport(f) |
| 75 | + if err != nil { |
| 76 | + t.Fatalf("BuildImport: %v", err) |
| 77 | + } |
| 78 | + var sawUntrusted int |
| 79 | + for _, n := range m.Nodes { |
| 80 | + if n.OpeningDeduction != nil || n.ProjectedGross != nil { |
| 81 | + t.Errorf("node %q: untrusted host published deduction=%v gross=%v, want both absent", |
| 82 | + n.Name, n.OpeningDeduction, n.ProjectedGross) |
| 83 | + } |
| 84 | + if n.HasOpenings { |
| 85 | + sawUntrusted++ |
| 86 | + // The discriminator that makes the absence above readable. Without |
| 87 | + // it a consumer sees the same nils a solid wall produces and nets |
| 88 | + // this fully-glazed host at its whole gross. |
| 89 | + if n.NetArea != nil { |
| 90 | + t.Errorf("node %q: HasOpenings with a trusted net; fixture is meant to be refused", n.Name) |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + if sawUntrusted == 0 { |
| 95 | + t.Fatal("fixture produced no host with openings; the untrusted path was never exercised") |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +// TestBuildImport_UnvoidedHostIsDistinguishableFromUntrusted is the whole point |
| 100 | +// of HasOpenings. Both a solid wall and a refused reconciliation publish nil in |
| 101 | +// all three area fields, and they are OPPOSITE answers: the solid wall's net |
| 102 | +// equals its gross, the refused one's net is unknown. A consumer totalling a |
| 103 | +// facade has to branch on something, and this is the only thing to branch on. |
| 104 | +func TestBuildImport_UnvoidedHostIsDistinguishableFromUntrusted(t *testing.T) { |
| 105 | + // A wall WITH a trusted window: HasOpenings, and every area field present. |
| 106 | + var voided int |
| 107 | + for _, n := range buildImport(t, "geometry/testdata/synthetic/netarea_rect_window.ifc").Nodes { |
| 108 | + if !n.HasOpenings { |
| 109 | + continue |
| 110 | + } |
| 111 | + voided++ |
| 112 | + if n.NetArea == nil { |
| 113 | + t.Errorf("node %q: HasOpenings but no net; this fixture's window is trusted", n.Name) |
| 114 | + } |
| 115 | + } |
| 116 | + if voided == 0 { |
| 117 | + t.Error("rect_window produced no host with openings; the true branch went untested") |
| 118 | + } |
| 119 | + |
| 120 | + // A wall with NO openings: not in the reconciliation at all, so every area |
| 121 | + // field is nil — the same nils the untrusted fixture produces above. Only |
| 122 | + // HasOpenings separates "its net IS its gross" from "its net is unknown". |
| 123 | + var unvoided int |
| 124 | + for _, n := range buildImport(t, "model/testdata/synthetic/wall_no_openings.ifc").Nodes { |
| 125 | + if n.HasOpenings { |
| 126 | + t.Errorf("node %q: HasOpenings on a fixture with no voids", n.Name) |
| 127 | + continue |
| 128 | + } |
| 129 | + unvoided++ |
| 130 | + if n.NetArea != nil || n.OpeningDeduction != nil || n.ProjectedGross != nil { |
| 131 | + t.Errorf("node %q: !HasOpenings yet published net=%v deduction=%v gross=%v", |
| 132 | + n.Name, n.NetArea, n.OpeningDeduction, n.ProjectedGross) |
| 133 | + } |
| 134 | + } |
| 135 | + if unvoided == 0 { |
| 136 | + t.Error("wall_no_openings produced no nodes; the false branch went untested") |
| 137 | + } |
| 138 | +} |
0 commit comments