1package hints
2
3import (
4 "testing"
5
6 "github.com/stretchr/testify/require"
7)
8
9func TestTileRegions(t *testing.T) {
10 whole := [][2]int{{3840, 2160}, {1920, 1080}, {2560, 1080}, {2560, 1440}, {1280, 720}}
11 for _, s := range whole {
12 require.Nil(t, tileRegions(s[0], s[1]), "%dx%d", s[0], s[1])
13 }
14 require.Equal(t, []region{{0, 0, 2560, 1440}, {1280, 0, 2560, 1440}, {2560, 0, 2560, 1440},
15 {0, 720, 2560, 1440}, {1280, 720, 2560, 1440}, {2560, 720, 2560, 1440}}, tileRegions(5120, 2160))
16 require.Equal(t, []region{{0, 0, 2560, 1440}, {880, 0, 2560, 1440}}, tileRegions(3440, 1440))
17
18 for _, s := range [][2]int{{5120, 2160}, {3440, 1440}, {5120, 1440}, {7680, 2160}, {3000, 2999}, {1000, 3000}, {5121, 2161}, {6000, 1001}} {
19 w, h := s[0], s[1]
20 rs := tileRegions(w, h)
21 require.NotEmpty(t, rs, "%dx%d", w, h)
22 require.LessOrEqual(t, len(rs), maxRegions)
23 covered := make([]bool, w*h)
24 for _, r := range rs {
25 require.Equal(t, r.h*16, r.w*9, "%dx%d tile %+v is not 16:9", w, h, r)
26 require.GreaterOrEqual(t, r.x, 0)
27 require.GreaterOrEqual(t, r.y, 0)
28 require.LessOrEqual(t, r.x+r.w, w)
29 require.LessOrEqual(t, r.y+r.h, h)
30 for y := r.y; y < r.y+r.h; y++ {
31 for x := r.x; x < r.x+r.w; x++ {
32 covered[y*w+x] = true
33 }
34 }
35 }
36 for i, c := range covered {
37 if !c {
38 t.Fatalf("%dx%d: pixel %d,%d not covered", w, h, i%w, i/w)
39 }
40 }
41 }
42}
43
44func TestTileStartsOverlap(t *testing.T) {
45 for _, l := range []int{2561, 3440, 5120, 7680, 10000} {
46 xs := tileStarts(l, 2560)
47 require.Equal(t, 0, xs[0])
48 require.Equal(t, l-2560, xs[len(xs)-1])
49 for i := 1; i < len(xs); i++ {
50 require.GreaterOrEqual(t, xs[i-1]+2560-xs[i], 256, "length %d", l) // >= 10 %
51 }
52 }
53}
54
55func TestTileRegionsTooMany(t *testing.T) {
56 require.Nil(t, tileRegions(60000, 20000))
57}
58
59func TestDropCutBoxes(t *testing.T) {
60 rs := tileRegions(5120, 2160) // tile 0: x 0..2560, tile 1: x 1280..3840
61 // A button at x 2500..2700 is cut at 2560 by tile 0 and whole in tile 1.
62 cutBox := detection{x1: 2500, y1: 100, x2: 2560, y2: 140, score: 0.6, region: 0}
63 whole := detection{x1: 2500, y1: 100, x2: 2700, y2: 140, score: 0.8, region: 1}
64 require.Equal(t, []detection{whole}, dropCutBoxes([]detection{cutBox, whole}, rs))
65 // Nobody saw it whole: the cut box stays.
66 require.Equal(t, []detection{cutBox}, dropCutBoxes([]detection{cutBox}, rs))
67 // An edge of the frame is not an inner edge.
68 edge := detection{x1: 0, y1: 0, x2: 60, y2: 30, score: 0.6, region: 0}
69 // Region 3 starts at y 720: its box at the frame's left edge is not cut
70 // either, so both stay though they sit in different regions.
71 other := detection{x1: 0, y1: 800, x2: 60, y2: 830, score: 0.5, region: 3}
72 require.Equal(t, []detection{edge, other}, dropCutBoxes([]detection{edge, other}, rs))
73 // No regions: unchanged.
74 require.Equal(t, []detection{cutBox}, dropCutBoxes([]detection{cutBox}, nil))
75}