package entity import ( "image" "image/color" "image/draw" "testing" ) func TestCollageLayout(t *testing.T) { if got := collageCells(0); len(got) != 0 { t.Fatalf("empty %d", len(got)) } two := collageCells(2) if len(two) != 2 { t.Fatalf("two %d", len(two)) } if two[0].Min.Y != two[1].Min.Y || two[0].Max.X >= two[1].Min.X { t.Fatalf("two should sit on one row: %+v", two) } nine := collageCells(9) if len(nine) != 9 { t.Fatalf("nine %d", len(nine)) } if two[0].Dx() <= nine[0].Dx() { t.Fatalf("two-person tiles should be larger than 3x3: two=%d nine=%d", two[0].Dx(), nine[0].Dx()) } three := collageCells(3) if len(three) != 3 { t.Fatalf("three %d", len(three)) } if three[0].Min.X <= three[1].Min.X { t.Fatalf("first row should be centered: %+v", three) } } func TestCollageColors(t *testing.T) { red := solid(color.RGBA{R: 255, A: 255}) green := solid(color.RGBA{G: 255, A: 255}) blue := solid(color.RGBA{B: 255, A: 255}) img := Collage([]image.Image{red, green, blue}) if img.Bounds().Dx() != collageSize { t.Fatalf("size %d", img.Bounds().Dx()) } cells := collageCells(3) assertNear(t, img.At(mid(cells[0]).X, mid(cells[0]).Y), color.RGBA{R: 255, A: 255}) assertNear(t, img.At(mid(cells[1]).X, mid(cells[1]).Y), color.RGBA{G: 255, A: 255}) assertNear(t, img.At(mid(cells[2]).X, mid(cells[2]).Y), color.RGBA{B: 255, A: 255}) } func solid(c color.RGBA) *image.RGBA { img := image.NewRGBA(image.Rect(0, 0, 16, 16)) draw.Draw(img, img.Bounds(), &image.Uniform{C: c}, image.Point{}, draw.Src) return img } func mid(r image.Rectangle) image.Point { return image.Pt((r.Min.X+r.Max.X)/2, (r.Min.Y+r.Max.Y)/2) } func assertNear(t *testing.T, got color.Color, want color.RGBA) { t.Helper() r, g, b, a := got.RGBA() if r>>8 != uint32(want.R) || g>>8 != uint32(want.G) || b>>8 != uint32(want.B) || a>>8 != uint32(want.A) { t.Fatalf("got rgba %d %d %d %d want %+v", r>>8, g>>8, b>>8, a>>8, want) } }