1package core_test
2
3import (
4 "image"
5 "testing"
6
7 "github.com/stretchr/testify/assert"
8 "github.com/stretchr/testify/require"
9
10 "github.com/bnema/nefercap/internal/core"
11 "github.com/bnema/nefercap/internal/ports"
12)
13
14func TestMonitorGridPhysicalUsesTheGivenPixels(t *testing.T) {
15 // An odd 901x601 output mode: the lines are its own thirds, one pixel each.
16 g, err := core.MonitorGridPhysical(901, 601)
17 require.NoError(t, err)
18 assert.Equal(t, gridWant(901, 601, [3]int{300, 450, 600}, [3]int{200, 300, 400}), g)
19
20 same, err := core.MonitorGridPhysical(1920, 1080)
21 require.NoError(t, err)
22 fromScale, err := core.MonitorGrid(1600, 900, 1.2)
23 require.NoError(t, err)
24 assert.Equal(t, fromScale, same, "an exact-fit buffer and mode agree")
25}
26
27func TestMonitorGridPhysicalSpansItsOwnSize(t *testing.T) {
28 g, err := core.MonitorGridPhysical(901, 601)
29 require.NoError(t, err)
30 for i, r := range g {
31 assert.True(t, r.In(image.Rect(0, 0, 901, 601)), "line %d %v", i, r)
32 if i < 3 {
33 assert.Equal(t, image.Rect(r.Min.X, 0, r.Min.X+1, 601), r)
34 } else {
35 assert.Equal(t, image.Rect(0, r.Min.Y, 901, r.Min.Y+1), r)
36 }
37 }
38}
39
40func TestMonitorGridPhysicalValidation(t *testing.T) {
41 const limit = ports.MaxDimension * core.MaxGridScale
42 for name, c := range map[string]struct{ w, h int }{
43 "zero width": {0, 600},
44 "zero height": {900, 0},
45 "negative width": {-1, 600},
46 "negative height": {900, -5},
47 "width too large": {limit + 1, 600},
48 "height too high": {900, limit + 1},
49 } {
50 t.Run(name, func(t *testing.T) {
51 g, err := core.MonitorGridPhysical(c.w, c.h)
52 assert.ErrorIs(t, err, core.ErrInvalidGrid)
53 assert.Equal(t, core.GridLines{}, g)
54 })
55 }
56 // A physical mode may exceed MaxDimension when the scale is above 1.
57 g, err := core.MonitorGridPhysical(limit, limit)
58 require.NoError(t, err)
59 assert.Equal(t, limit, g[3].Dx())
60 _, err = core.MonitorGridPhysical(1, 1)
61 assert.NoError(t, err)
62}
63
64func TestMonitorGridPhysicalAllocations(t *testing.T) {
65 var g core.GridLines
66 n := testing.AllocsPerRun(200, func() {
67 g, _ = core.MonitorGridPhysical(901, 601)
68 _, _ = core.MonitorGridPhysical(0, 601)
69 })
70 assert.Zero(t, n)
71 _ = g
72}
73
74func BenchmarkMonitorGridPhysical(b *testing.B) {
75 b.ReportAllocs()
76 var g core.GridLines
77 for i := 0; b.Loop(); i++ {
78 g, _ = core.MonitorGridPhysical(901+i%3, 601)
79 }
80 _ = g
81}