added test for all implementations
This commit is contained in:
73
internal/viz/viz_test.go
Normal file
73
internal/viz/viz_test.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package viz
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"bspviz/internal/bsp"
|
||||
"bspviz/internal/geom"
|
||||
"bspviz/internal/mapfmt"
|
||||
)
|
||||
|
||||
func TestEmitDOT(t *testing.T) {
|
||||
root := &bsp.Node{
|
||||
O: geom.V(0, 0),
|
||||
D: geom.V(1, 0),
|
||||
Left: &bsp.Node{
|
||||
Leaf: &bsp.Leaf{Segs: []geom.Seg{{A: geom.V(0, 0), B: geom.V(1, 1)}}},
|
||||
},
|
||||
Right: &bsp.Node{
|
||||
Leaf: &bsp.Leaf{},
|
||||
},
|
||||
}
|
||||
out := filepath.Join(t.TempDir(), "tree.dot")
|
||||
if err := EmitDOT(root, out); err != nil {
|
||||
t.Fatalf("EmitDOT error: %v", err)
|
||||
}
|
||||
got, err := os.ReadFile(out)
|
||||
if err != nil {
|
||||
t.Fatalf("ReadFile: %v", err)
|
||||
}
|
||||
expect := "digraph BSP {\n" +
|
||||
" node [fontname=\"Helvetica\"];\n" +
|
||||
" n0 [label=\"Split\\nO=(0,0) D=(1,0)\"];\n" +
|
||||
" n1 [label=\"Leaf\\nSegs=1\", shape=ellipse, style=filled, fillcolor=lightgray];\n" +
|
||||
" n2 [label=\"Leaf\\nSegs=0\", shape=ellipse, style=filled, fillcolor=lightgray];\n" +
|
||||
" n0 -> n1 [label=\"L\"];\n" +
|
||||
" n0 -> n2 [label=\"R\"];\n" +
|
||||
"}\n"
|
||||
if string(got) != expect {
|
||||
t.Fatalf("unexpected DOT output:\n%s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderPNGProducesFile(t *testing.T) {
|
||||
m := &mapfmt.MapData{
|
||||
Vertices: []mapfmt.Vertex{{X: 0, Y: 0}, {X: 64, Y: 0}, {X: 64, Y: 64}},
|
||||
Linedefs: []mapfmt.Linedef{
|
||||
{V1: 0, V2: 1},
|
||||
{V1: 1, V2: 2},
|
||||
{V1: 2, V2: 0},
|
||||
},
|
||||
}
|
||||
root := &bsp.Node{
|
||||
O: geom.V(0, 0),
|
||||
D: geom.V(1, 0),
|
||||
Left: &bsp.Node{Leaf: &bsp.Leaf{Segs: []geom.Seg{{A: geom.V(0, 0), B: geom.V(64, 64)}}}},
|
||||
Right: &bsp.Node{Leaf: &bsp.Leaf{}},
|
||||
}
|
||||
out := filepath.Join(t.TempDir(), "render.png")
|
||||
if err := RenderPNG(m, root, out); err != nil {
|
||||
t.Fatalf("RenderPNG error: %v", err)
|
||||
}
|
||||
data, err := os.ReadFile(out)
|
||||
if err != nil {
|
||||
t.Fatalf("ReadFile: %v", err)
|
||||
}
|
||||
pngMagic := []byte{0x89, 'P', 'N', 'G', '\r', '\n', 0x1a, '\n'}
|
||||
if len(data) < len(pngMagic) || !bytes.Equal(data[:len(pngMagic)], pngMagic) {
|
||||
t.Fatalf("output is not a PNG")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user