aboutsummaryrefslogtreecommitdiffstats
path: root/lay/strain/solve_test.go
blob: 61ab7e6e76bbe92e3a447fc5210a005c41dec5f4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package strain_test

import (
	"testing"
	"image"
	"slices"

	"github.com/lithdew/casso"

	"github.com/faiface/gui/style"
	"github.com/faiface/gui/lay/strain"
)

type solverTest struct {
	t *testing.T
	*style.Style
	*strain.Solver
}

func newSolverTest(t *testing.T, constraints []<-chan strain.Constraint) solverTest {
	styl, err := style.New()
	if err != nil {
		t.Fatal(err)
	}
	solver, err := strain.NewSolver(styl, constraints)
	if err != nil {
		t.Fatal(err)
	}
	return solverTest{t, styl, solver}
}

func (st solverTest) Close() {
	st.Solver.Close()
	if err := st.Style.Close(); err != nil {
		st.t.Error(err)
	}
}

func (st solverTest) addConstraint(op casso.Op, lhs, rhs casso.Symbol) {
	if err := st.Solver.AddConstraint(op, lhs, rhs); err != nil {
		st.t.Error(err)
	}
}

func (st solverTest) solve(container image.Rectangle, wantFields []image.Rectangle) {
	fields, err := st.Solver.Solve(container)
	if err != nil {
		st.t.Errorf("Solve(%v): %v; want %v", container, err, wantFields)
	}
	if !slices.Equal(fields, wantFields) {
		st.t.Errorf("Solve(%v) = %v; want %v", container, fields, wantFields)
	}
}

// No constraints and zero-sized container.
func TestTrivial(t *testing.T) {
	t.Parallel()
	st := newSolverTest(t, nil)
	defer st.Close()
	fields, err := st.Solver.Solve(image.ZR)
	if err != nil {
		t.Error(err)
	}
	if len(fields) != 0 {
		t.Errorf("expected 0 fields; got %d", len(fields))
	}
}

// One field that occupies the whole container.
func TestSingleField(t *testing.T) {
	// Setup
	t.Parallel()
	constraints := make(chan strain.Constraint)
	st := newSolverTest(t, []<-chan strain.Constraint{constraints})
	defer st.Close()
	defer close(constraints)

	// Add constraints
	container := st.Solver.Container()
	field := st.Solver.Field(0)
	st.addConstraint(casso.EQ, field.Origin.X, container.Origin.X)
	st.addConstraint(casso.EQ, field.Origin.Y, container.Origin.Y)
	st.addConstraint(casso.EQ, field.Size.X, container.Size.X)
	st.addConstraint(casso.EQ, field.Size.Y, container.Size.Y)

	// Solve
	for _, container := range []image.Rectangle{
		image.ZR,
		image.Rectangle{image.ZP, image.Pt(800, 600)},
		image.Rectangle{image.Pt(12, 34), image.Pt(123, 456)},
	} {
		// field == container
		st.solve(container, []image.Rectangle{container})
	}
}

// Solver with only layout constaints, no field constraints.
func TestLayConstrs(t *testing.T) {
	t.Parallel()

	st := newSolverTest(t, nil)
	defer st.Close()

	t.Fail() // TODO
}