Merge branch 'master' of github.com:bcvery1/pixel

This commit is contained in:
Ben Cragg 2019-03-26 15:32:42 +00:00
commit 09db6d8b38
11 changed files with 2693 additions and 0 deletions

154
batch_test.go Normal file
View File

@ -0,0 +1,154 @@
package pixel_test
import (
"image/color"
"reflect"
"testing"
"github.com/faiface/pixel"
)
func TestNewBatch(t *testing.T) {
type args struct {
container pixel.Triangles
pic pixel.Picture
}
tests := []struct {
name string
args args
want *pixel.Batch
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := pixel.NewBatch(tt.args.container, tt.args.pic); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewBatch() = %v, want %v", got, tt.want)
}
})
}
}
func TestBatch_Dirty(t *testing.T) {
tests := []struct {
name string
b *pixel.Batch
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.b.Dirty()
})
}
}
func TestBatch_Clear(t *testing.T) {
tests := []struct {
name string
b *pixel.Batch
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.b.Clear()
})
}
}
func TestBatch_Draw(t *testing.T) {
type args struct {
t pixel.Target
}
tests := []struct {
name string
b *pixel.Batch
args args
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.b.Draw(tt.args.t)
})
}
}
func TestBatch_SetMatrix(t *testing.T) {
type args struct {
m pixel.Matrix
}
tests := []struct {
name string
b *pixel.Batch
args args
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.b.SetMatrix(tt.args.m)
})
}
}
func TestBatch_SetColorMask(t *testing.T) {
type args struct {
c color.Color
}
tests := []struct {
name string
b *pixel.Batch
args args
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.b.SetColorMask(tt.args.c)
})
}
}
func TestBatch_MakeTriangles(t *testing.T) {
type args struct {
t pixel.Triangles
}
tests := []struct {
name string
b *pixel.Batch
args args
want pixel.TargetTriangles
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.b.MakeTriangles(tt.args.t); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Batch.MakeTriangles() = %v, want %v", got, tt.want)
}
})
}
}
func TestBatch_MakePicture(t *testing.T) {
type args struct {
p pixel.Picture
}
tests := []struct {
name string
b *pixel.Batch
args args
want pixel.TargetPicture
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.b.MakePicture(tt.args.p); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Batch.MakePicture() = %v, want %v", got, tt.want)
}
})
}
}

View File

@ -3,6 +3,7 @@ package pixel_test
import (
"fmt"
"image/color"
"reflect"
"testing"
"github.com/faiface/pixel"
@ -22,3 +23,363 @@ func BenchmarkColorToRGBA(b *testing.B) {
})
}
}
func TestRGB(t *testing.T) {
type args struct {
r float64
g float64
b float64
}
tests := []struct {
name string
args args
want pixel.RGBA
}{
{
name: "RBG: create black",
args: args{r: 0, g: 0, b: 0},
want: pixel.RGBA{R: 0, G: 0, B: 0, A: 1},
},
{
name: "RBG: create white",
args: args{r: 1, g: 1, b: 1},
want: pixel.RGBA{R: 1, G: 1, B: 1, A: 1},
},
{
name: "RBG: create nonsense",
args: args{r: 500, g: 500, b: 500},
want: pixel.RGBA{R: 500, G: 500, B: 500, A: 1},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := pixel.RGB(tt.args.r, tt.args.g, tt.args.b); !reflect.DeepEqual(got, tt.want) {
t.Errorf("RGB() = %v, want %v", got, tt.want)
}
})
}
}
func TestAlpha(t *testing.T) {
type args struct {
a float64
}
tests := []struct {
name string
args args
want pixel.RGBA
}{
{
name: "Alpha: transparent",
args: args{a: 0},
want: pixel.RGBA{R: 0, G: 0, B: 0, A: 0},
},
{
name: "Alpha: obaque",
args: args{a: 1},
want: pixel.RGBA{R: 1, G: 1, B: 1, A: 1},
},
{
name: "Alpha: nonsense",
args: args{a: 1024},
want: pixel.RGBA{R: 1024, G: 1024, B: 1024, A: 1024},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := pixel.Alpha(tt.args.a); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Alpha() = %v, want %v", got, tt.want)
}
})
}
}
func TestRGBA_Add(t *testing.T) {
type fields struct {
R float64
G float64
B float64
A float64
}
type args struct {
d pixel.RGBA
}
tests := []struct {
name string
fields fields
args args
want pixel.RGBA
}{
{
name: "RGBA.Add: add to black",
fields: fields{R: 0, G: 0, B: 0, A: 1},
args: args{d: pixel.RGBA{R: 50, G: 50, B: 50, A: 0}},
want: pixel.RGBA{R: 50, G: 50, B: 50, A: 1},
},
{
name: "RGBA.Add: add to white",
fields: fields{R: 1, G: 1, B: 1, A: 1},
args: args{d: pixel.RGBA{R: 1, G: 1, B: 1, A: 1}},
want: pixel.RGBA{R: 2, G: 2, B: 2, A: 2},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := pixel.RGBA{
R: tt.fields.R,
G: tt.fields.G,
B: tt.fields.B,
A: tt.fields.A,
}
if got := c.Add(tt.args.d); !reflect.DeepEqual(got, tt.want) {
t.Errorf("RGBA.Add() = %v, want %v", got, tt.want)
}
})
}
}
func TestRGBA_Sub(t *testing.T) {
type fields struct {
R float64
G float64
B float64
A float64
}
type args struct {
d pixel.RGBA
}
tests := []struct {
name string
fields fields
args args
want pixel.RGBA
}{
{
name: "RGBA.Sub: sub from white",
fields: fields{R: 1, G: 1, B: 1, A: 1},
args: args{d: pixel.RGBA{R: .5, G: .5, B: .5, A: 0}},
want: pixel.RGBA{R: .5, G: .5, B: .5, A: 1},
},
{
name: "RGBA.Sub: sub from black",
fields: fields{R: 0, G: 0, B: 0, A: 0},
args: args{d: pixel.RGBA{R: 1, G: 1, B: 1, A: 1}},
want: pixel.RGBA{R: -1, G: -1, B: -1, A: -1},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := pixel.RGBA{
R: tt.fields.R,
G: tt.fields.G,
B: tt.fields.B,
A: tt.fields.A,
}
if got := c.Sub(tt.args.d); !reflect.DeepEqual(got, tt.want) {
t.Errorf("RGBA.Sub() = %v, want %v", got, tt.want)
}
})
}
}
func TestRGBA_Mul(t *testing.T) {
type fields struct {
R float64
G float64
B float64
A float64
}
type args struct {
d pixel.RGBA
}
greaterThanOne := args{d: pixel.RGBA{R: 2, G: 3, B: 4, A: 5}}
lessThanOne := args{d: pixel.RGBA{R: .2, G: .3, B: .4, A: .5}}
tests := []struct {
name string
fields fields
args args
want pixel.RGBA
}{
{
name: "RGBA.Mul: multiply black by >1",
fields: fields{R: 0, G: 0, B: 0, A: 0},
args: greaterThanOne,
want: pixel.RGBA{R: 0, G: 0, B: 0, A: 0},
},
{
name: "RGBA.Mul: multiply white by >1",
fields: fields{R: 1, G: 1, B: 1, A: 1},
args: greaterThanOne,
want: pixel.RGBA{R: 2, G: 3, B: 4, A: 5},
},
{
name: "RGBA.Mul: multiply black by <1",
fields: fields{R: 0, G: 0, B: 0, A: 0},
args: lessThanOne,
want: pixel.RGBA{R: 0, G: 0, B: 0, A: 0},
},
{
name: "RGBA.Mul: multiply white by <1",
fields: fields{R: 1, G: 1, B: 1, A: 1},
args: lessThanOne,
want: pixel.RGBA{R: .2, G: .3, B: .4, A: .5},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := pixel.RGBA{
R: tt.fields.R,
G: tt.fields.G,
B: tt.fields.B,
A: tt.fields.A,
}
if got := c.Mul(tt.args.d); !reflect.DeepEqual(got, tt.want) {
t.Errorf("RGBA.Mul() = %v, want %v", got, tt.want)
}
})
}
}
func TestRGBA_Scaled(t *testing.T) {
type fields struct {
R float64
G float64
B float64
A float64
}
type args struct {
scale float64
}
tests := []struct {
name string
fields fields
args args
want pixel.RGBA
}{
{
name: "RBGA.Scaled: black <1",
fields: fields{R: 0, G: 0, B: 0, A: 0},
args: args{scale: 0.5},
want: pixel.RGBA{R: 0, G: 0, B: 0, A: 0},
},
{
name: "RBGA.Scaled: black <1",
fields: fields{R: 1, G: 1, B: 1, A: 1},
args: args{scale: 0.5},
want: pixel.RGBA{R: .5, G: .5, B: .5, A: .5},
},
{
name: "RBGA.Scaled: black >1",
fields: fields{R: 0, G: 0, B: 0, A: 0},
args: args{scale: 2},
want: pixel.RGBA{R: 0, G: 0, B: 0, A: 0},
},
{
name: "RBGA.Scaled: black >1",
fields: fields{R: 1, G: 1, B: 1, A: 1},
args: args{scale: 2},
want: pixel.RGBA{R: 2, G: 2, B: 2, A: 2},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := pixel.RGBA{
R: tt.fields.R,
G: tt.fields.G,
B: tt.fields.B,
A: tt.fields.A,
}
if got := c.Scaled(tt.args.scale); !reflect.DeepEqual(got, tt.want) {
t.Errorf("RGBA.Scaled() = %v, want %v", got, tt.want)
}
})
}
}
func TestRGBA_RGBA(t *testing.T) {
type fields struct {
R float64
G float64
B float64
A float64
}
tests := []struct {
name string
fields fields
wantR uint32
wantG uint32
wantB uint32
wantA uint32
}{
{
name: "RGBA.RGBA: black",
fields: fields{R: 0, G: 0, B: 0, A: 0},
wantR: 0,
wantG: 0,
wantB: 0,
wantA: 0,
},
{
name: "RGBA.RGBA: white",
fields: fields{R: 1, G: 1, B: 1, A: 1},
wantR: 65535,
wantG: 65535,
wantB: 65535,
wantA: 65535,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := pixel.RGBA{
R: tt.fields.R,
G: tt.fields.G,
B: tt.fields.B,
A: tt.fields.A,
}
gotR, gotG, gotB, gotA := c.RGBA()
if gotR != tt.wantR {
t.Errorf("RGBA.RGBA() gotR = %v, want %v", gotR, tt.wantR)
}
if gotG != tt.wantG {
t.Errorf("RGBA.RGBA() gotG = %v, want %v", gotG, tt.wantG)
}
if gotB != tt.wantB {
t.Errorf("RGBA.RGBA() gotB = %v, want %v", gotB, tt.wantB)
}
if gotA != tt.wantA {
t.Errorf("RGBA.RGBA() gotA = %v, want %v", gotA, tt.wantA)
}
})
}
}
func TestToRGBA(t *testing.T) {
type args struct {
c color.Color
}
tests := []struct {
name string
args args
want pixel.RGBA
}{
{
name: "ToRGBA: black",
args: args{c: color.Black},
want: pixel.RGBA{R: 0, G: 0, B: 0, A: 1},
},
{
name: "ToRGBA: white",
args: args{c: color.White},
want: pixel.RGBA{R: 1, G: 1, B: 1, A: 1},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := pixel.ToRGBA(tt.args.c); !reflect.DeepEqual(got, tt.want) {
t.Errorf("ToRGBA() = %v, want %v", got, tt.want)
}
})
}
}

101
compose_test.go Normal file
View File

@ -0,0 +1,101 @@
package pixel_test
import (
"reflect"
"testing"
"github.com/faiface/pixel"
)
func TestComposeMethod_Compose(t *testing.T) {
type args struct {
a pixel.RGBA
b pixel.RGBA
}
a := pixel.RGBA{R: 200, G: 200, B: 200, A: .8}
b := pixel.RGBA{R: 100, G: 100, B: 100, A: .5}
c := pixel.RGBA{R: 200, G: 200, B: 200, A: .5}
tests := []struct {
name string
cm pixel.ComposeMethod
args args
want pixel.RGBA
}{
{
name: "ComposeMethod.Compose: ComposeOver",
cm: pixel.ComposeOver,
args: args{a: a, b: b},
want: pixel.RGBA{R: 220, G: 220, B: 220, A: .9},
},
{
name: "ComposeMethod.Compose: ComposeIn",
cm: pixel.ComposeIn,
args: args{a: a, b: b},
want: pixel.RGBA{R: 100, G: 100, B: 100, A: .4},
},
{
name: "ComposeMethod.Compose: ComposeOut",
cm: pixel.ComposeOut,
args: args{a: a, b: b},
want: pixel.RGBA{R: 100, G: 100, B: 100, A: .4},
},
{
name: "ComposeMethod.Compose: ComposeAtop",
cm: pixel.ComposeAtop,
args: args{a: a, b: b},
want: pixel.RGBA{R: 120, G: 120, B: 120, A: .5},
},
{
name: "ComposeMethod.Compose: ComposeRover",
cm: pixel.ComposeRover,
args: args{a: a, b: b},
want: pixel.RGBA{R: 200, G: 200, B: 200, A: .9},
},
{
name: "ComposeMethod.Compose: ComposeRin",
cm: pixel.ComposeRin,
args: args{a: a, b: b},
want: pixel.RGBA{R: 80, G: 80, B: 80, A: .4},
},
{
name: "ComposeMethod.Compose: ComposeRout",
cm: pixel.ComposeRout,
// Using 'c' here to make the "want"ed RGBA rational
args: args{a: c, b: b},
want: pixel.RGBA{R: 50, G: 50, B: 50, A: .25},
},
{
name: "ComposeMethod.Compose: ComposeRatop",
cm: pixel.ComposeRatop,
args: args{a: a, b: b},
want: pixel.RGBA{R: 180, G: 180, B: 180, A: .8},
},
{
name: "ComposeMethod.Compose: ComposeXor",
cm: pixel.ComposeXor,
args: args{a: a, b: b},
want: pixel.RGBA{R: 120, G: 120, B: 120, A: .5},
},
{
name: "ComposeMethod.Compose: ComposePlus",
cm: pixel.ComposePlus,
args: args{a: a, b: b},
want: pixel.RGBA{R: 300, G: 300, B: 300, A: 1.3},
},
{
name: "ComposeMethod.Compose: ComposeCopy",
cm: pixel.ComposeCopy,
args: args{a: a, b: b},
want: pixel.RGBA{R: 200, G: 200, B: 200, A: .8},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.cm.Compose(tt.args.a, tt.args.b); !reflect.DeepEqual(got, tt.want) {
t.Errorf("ComposeMethod.Compose() = %v, want %v", got, tt.want)
}
})
}
}

325
data_test.go Normal file
View File

@ -0,0 +1,325 @@
package pixel_test
import (
"image"
"reflect"
"testing"
"github.com/faiface/pixel"
)
func TestMakeTrianglesData(t *testing.T) {
type args struct {
len int
}
tests := []struct {
name string
args args
want *pixel.TrianglesData
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := pixel.MakeTrianglesData(tt.args.len); !reflect.DeepEqual(got, tt.want) {
t.Errorf("MakeTrianglesData() = %v, want %v", got, tt.want)
}
})
}
}
func TestTrianglesData_Len(t *testing.T) {
tests := []struct {
name string
td *pixel.TrianglesData
want int
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.td.Len(); got != tt.want {
t.Errorf("TrianglesData.Len() = %v, want %v", got, tt.want)
}
})
}
}
func TestTrianglesData_SetLen(t *testing.T) {
type args struct {
len int
}
tests := []struct {
name string
td *pixel.TrianglesData
args args
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.td.SetLen(tt.args.len)
})
}
}
func TestTrianglesData_Slice(t *testing.T) {
type args struct {
i int
j int
}
tests := []struct {
name string
td *pixel.TrianglesData
args args
want pixel.Triangles
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.td.Slice(tt.args.i, tt.args.j); !reflect.DeepEqual(got, tt.want) {
t.Errorf("TrianglesData.Slice() = %v, want %v", got, tt.want)
}
})
}
}
func TestTrianglesData_Update(t *testing.T) {
type args struct {
t pixel.Triangles
}
tests := []struct {
name string
td *pixel.TrianglesData
args args
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.td.Update(tt.args.t)
})
}
}
func TestTrianglesData_Copy(t *testing.T) {
tests := []struct {
name string
td *pixel.TrianglesData
want pixel.Triangles
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.td.Copy(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("TrianglesData.Copy() = %v, want %v", got, tt.want)
}
})
}
}
func TestTrianglesData_Position(t *testing.T) {
type args struct {
i int
}
tests := []struct {
name string
td *pixel.TrianglesData
args args
want pixel.Vec
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.td.Position(tt.args.i); !reflect.DeepEqual(got, tt.want) {
t.Errorf("TrianglesData.Position() = %v, want %v", got, tt.want)
}
})
}
}
func TestTrianglesData_Color(t *testing.T) {
type args struct {
i int
}
tests := []struct {
name string
td *pixel.TrianglesData
args args
want pixel.RGBA
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.td.Color(tt.args.i); !reflect.DeepEqual(got, tt.want) {
t.Errorf("TrianglesData.Color() = %v, want %v", got, tt.want)
}
})
}
}
func TestTrianglesData_Picture(t *testing.T) {
type args struct {
i int
}
tests := []struct {
name string
td *pixel.TrianglesData
args args
wantPic pixel.Vec
wantIntensity float64
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotPic, gotIntensity := tt.td.Picture(tt.args.i)
if !reflect.DeepEqual(gotPic, tt.wantPic) {
t.Errorf("TrianglesData.Picture() gotPic = %v, want %v", gotPic, tt.wantPic)
}
if gotIntensity != tt.wantIntensity {
t.Errorf("TrianglesData.Picture() gotIntensity = %v, want %v", gotIntensity, tt.wantIntensity)
}
})
}
}
func TestMakePictureData(t *testing.T) {
type args struct {
rect pixel.Rect
}
tests := []struct {
name string
args args
want *pixel.PictureData
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := pixel.MakePictureData(tt.args.rect); !reflect.DeepEqual(got, tt.want) {
t.Errorf("MakePictureData() = %v, want %v", got, tt.want)
}
})
}
}
func TestPictureDataFromImage(t *testing.T) {
type args struct {
img image.Image
}
tests := []struct {
name string
args args
want *pixel.PictureData
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := pixel.PictureDataFromImage(tt.args.img); !reflect.DeepEqual(got, tt.want) {
t.Errorf("PictureDataFromImage() = %v, want %v", got, tt.want)
}
})
}
}
func TestPictureDataFromPicture(t *testing.T) {
type args struct {
pic pixel.Picture
}
tests := []struct {
name string
args args
want *pixel.PictureData
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := pixel.PictureDataFromPicture(tt.args.pic); !reflect.DeepEqual(got, tt.want) {
t.Errorf("PictureDataFromPicture() = %v, want %v", got, tt.want)
}
})
}
}
func TestPictureData_Image(t *testing.T) {
tests := []struct {
name string
pd *pixel.PictureData
want *image.RGBA
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.pd.Image(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("PictureData.Image() = %v, want %v", got, tt.want)
}
})
}
}
func TestPictureData_Index(t *testing.T) {
type args struct {
at pixel.Vec
}
tests := []struct {
name string
pd *pixel.PictureData
args args
want int
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.pd.Index(tt.args.at); got != tt.want {
t.Errorf("PictureData.Index() = %v, want %v", got, tt.want)
}
})
}
}
func TestPictureData_Bounds(t *testing.T) {
tests := []struct {
name string
pd *pixel.PictureData
want pixel.Rect
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.pd.Bounds(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("PictureData.Bounds() = %v, want %v", got, tt.want)
}
})
}
}
func TestPictureData_Color(t *testing.T) {
type args struct {
at pixel.Vec
}
tests := []struct {
name string
pd *pixel.PictureData
args args
want pixel.RGBA
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.pd.Color(tt.args.at); !reflect.DeepEqual(got, tt.want) {
t.Errorf("PictureData.Color() = %v, want %v", got, tt.want)
}
})
}
}

View File

@ -16,3 +16,121 @@ func BenchmarkSpriteDrawBatch(b *testing.B) {
sprite.Draw(batch, pixel.IM)
}
}
func TestDrawer_Dirty(t *testing.T) {
tests := []struct {
name string
d *pixel.Drawer
}{
{
name: "Drawer.Dirty",
d: &pixel.Drawer{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.d.Dirty()
})
}
}
type targetMock struct {
makeTrianglesCount int
makePictureCount int
}
func (t *targetMock) MakePicture(_ pixel.Picture) pixel.TargetPicture {
t.makePictureCount++
return targetPictureMock{}
}
func (t *targetMock) MakeTriangles(_ pixel.Triangles) pixel.TargetTriangles {
t.makeTrianglesCount++
return targetTrianglesMock{}
}
type targetTrianglesMock struct{}
func (targetTrianglesMock) Len() int {
return 0
}
func (targetTrianglesMock) SetLen(_ int) {
}
func (targetTrianglesMock) Slice(_, _ int) pixel.Triangles {
return nil
}
func (targetTrianglesMock) Update(_ pixel.Triangles) {
}
func (targetTrianglesMock) Copy() pixel.Triangles {
return nil
}
func (targetTrianglesMock) Draw() {
}
type targetPictureMock struct{}
func (targetPictureMock) Bounds() pixel.Rect {
return pixel.R(0, 0, 0, 0)
}
func (targetPictureMock) Draw(_ pixel.TargetTriangles) {
}
func TestDrawer_Draw(t *testing.T) {
type args struct {
t pixel.Target
}
tests := []struct {
name string
d *pixel.Drawer
args args
wantPictureCount int
wantTriangleCount int
}{
{
name: "Drawer.Draw: nil triangles",
d: &pixel.Drawer{},
args: args{t: &targetMock{}},
wantPictureCount: 0,
wantTriangleCount: 0,
},
{
name: "Drawer.Draw: non-nil triangles",
d: &pixel.Drawer{Triangles: pixel.MakeTrianglesData(1)},
args: args{t: &targetMock{}},
wantPictureCount: 0,
wantTriangleCount: 1,
},
{
name: "Drawer.Draw: non-nil picture",
d: &pixel.Drawer{
Triangles: pixel.MakeTrianglesData(1),
Picture: pixel.MakePictureData(pixel.R(0, 0, 0, 0)),
},
args: args{t: &targetMock{}},
wantPictureCount: 1,
wantTriangleCount: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.d.Draw(tt.args.t)
target := tt.args.t.(*targetMock)
if tt.wantPictureCount != target.makePictureCount {
t.Fatalf("MakePicture not called. Expected %d, got: %d", tt.wantPictureCount, target.makePictureCount)
}
if tt.wantTriangleCount != target.makeTrianglesCount {
t.Fatalf("MakeTriangles not called. Expected %d, got: %d", tt.wantTriangleCount, target.makeTrianglesCount)
}
})
}
}

12
go.mod Normal file
View File

@ -0,0 +1,12 @@
module github.com/faiface/pixel
require (
github.com/faiface/glhf v0.0.0-20181018222622-82a6317ac380
github.com/faiface/mainthread v0.0.0-20171120011319-8b78f0a41ae3
github.com/go-gl/gl v0.0.0-20181026044259-55b76b7df9d2 // indirect
github.com/go-gl/glfw v0.0.0-20181213070059-819e8ce5125f
github.com/go-gl/mathgl v0.0.0-20180804195959-cdf14b6b8f8a
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
github.com/pkg/errors v0.8.1
golang.org/x/image v0.0.0-20181116024801-cd38e8056d9b
)

16
go.sum Normal file
View File

@ -0,0 +1,16 @@
github.com/faiface/glhf v0.0.0-20181018222622-82a6317ac380 h1:FvZ0mIGh6b3kOITxUnxS3tLZMh7yEoHo75v3/AgUqg0=
github.com/faiface/glhf v0.0.0-20181018222622-82a6317ac380/go.mod h1:zqnPFFIuYFFxl7uH2gYByJwIVKG7fRqlqQCbzAnHs9g=
github.com/faiface/mainthread v0.0.0-20171120011319-8b78f0a41ae3 h1:baVdMKlASEHrj19iqjARrPbaRisD7EuZEVJj6ZMLl1Q=
github.com/faiface/mainthread v0.0.0-20171120011319-8b78f0a41ae3/go.mod h1:VEPNJUlxl5KdWjDvz6Q1l+rJlxF2i6xqDeGuGAxa87M=
github.com/go-gl/gl v0.0.0-20181026044259-55b76b7df9d2 h1:78Hza2KHn2PX1jdydQnffaU2A/xM0g3Nx1xmMdep9Gk=
github.com/go-gl/gl v0.0.0-20181026044259-55b76b7df9d2/go.mod h1:482civXOzJJCPzJ4ZOX/pwvXBWSnzD4OKMdH4ClKGbk=
github.com/go-gl/glfw v0.0.0-20181213070059-819e8ce5125f h1:7MsFMbSn8Lcw0blK4+NEOf8DuHoOBDhJsHz04yh13pM=
github.com/go-gl/glfw v0.0.0-20181213070059-819e8ce5125f/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-gl/mathgl v0.0.0-20180804195959-cdf14b6b8f8a h1:2n5w2v3knlspzjJWyQPC0j88Mwvq0SZV0Jdws34GJwc=
github.com/go-gl/mathgl v0.0.0-20180804195959-cdf14b6b8f8a/go.mod h1:dvrdneKbyWbK2skTda0nM4B9zSlS2GZSnjX7itr/skQ=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
golang.org/x/image v0.0.0-20181116024801-cd38e8056d9b h1:VHyIDlv3XkfCa5/a81uzaoDkHH4rr81Z62g+xlnO8uM=
golang.org/x/image v0.0.0-20181116024801-cd38e8056d9b/go.mod h1:ux5Hcp/YLpHSI86hEcLt0YII63i6oz57MZXIpbrjZUs=

View File

@ -2,6 +2,7 @@ package pixel_test
import (
"math/rand"
"reflect"
"testing"
"github.com/faiface/pixel"
@ -61,3 +62,170 @@ func BenchmarkMatrix(b *testing.B) {
}
})
}
func TestMatrix_String(t *testing.T) {
tests := []struct {
name string
m pixel.Matrix
want string
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.m.String(); got != tt.want {
t.Errorf("Matrix.String() = %v, want %v", got, tt.want)
}
})
}
}
func TestMatrix_Moved(t *testing.T) {
type args struct {
delta pixel.Vec
}
tests := []struct {
name string
m pixel.Matrix
args args
want pixel.Matrix
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.m.Moved(tt.args.delta); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Matrix.Moved() = %v, want %v", got, tt.want)
}
})
}
}
func TestMatrix_ScaledXY(t *testing.T) {
type args struct {
around pixel.Vec
scale pixel.Vec
}
tests := []struct {
name string
m pixel.Matrix
args args
want pixel.Matrix
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.m.ScaledXY(tt.args.around, tt.args.scale); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Matrix.ScaledXY() = %v, want %v", got, tt.want)
}
})
}
}
func TestMatrix_Scaled(t *testing.T) {
type args struct {
around pixel.Vec
scale float64
}
tests := []struct {
name string
m pixel.Matrix
args args
want pixel.Matrix
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.m.Scaled(tt.args.around, tt.args.scale); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Matrix.Scaled() = %v, want %v", got, tt.want)
}
})
}
}
func TestMatrix_Rotated(t *testing.T) {
type args struct {
around pixel.Vec
angle float64
}
tests := []struct {
name string
m pixel.Matrix
args args
want pixel.Matrix
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.m.Rotated(tt.args.around, tt.args.angle); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Matrix.Rotated() = %v, want %v", got, tt.want)
}
})
}
}
func TestMatrix_Chained(t *testing.T) {
type args struct {
next pixel.Matrix
}
tests := []struct {
name string
m pixel.Matrix
args args
want pixel.Matrix
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.m.Chained(tt.args.next); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Matrix.Chained() = %v, want %v", got, tt.want)
}
})
}
}
func TestMatrix_Project(t *testing.T) {
type args struct {
u pixel.Vec
}
tests := []struct {
name string
m pixel.Matrix
args args
want pixel.Vec
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.m.Project(tt.args.u); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Matrix.Project() = %v, want %v", got, tt.want)
}
})
}
}
func TestMatrix_Unproject(t *testing.T) {
type args struct {
u pixel.Vec
}
tests := []struct {
name string
m pixel.Matrix
args args
want pixel.Vec
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.m.Unproject(tt.args.u); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Matrix.Unproject() = %v, want %v", got, tt.want)
}
})
}
}

609
rect_test.go Normal file
View File

@ -0,0 +1,609 @@
package pixel_test
import (
"fmt"
"reflect"
"testing"
"github.com/faiface/pixel"
)
var (
squareAroundOrigin = pixel.R(-10, -10, 10, 10)
squareAround2020 = pixel.R(10, 10, 30, 30)
rectangleAroundOrigin = pixel.R(-20, -10, 20, 10)
rectangleAround2020 = pixel.R(0, 10, 40, 30)
)
func TestR(t *testing.T) {
type args struct {
minX float64
minY float64
maxX float64
maxY float64
}
tests := []struct {
name string
args args
want pixel.Rect
}{
{
name: "R(): square around origin",
args: args{minX: -10, minY: -10, maxX: 10, maxY: 10},
want: squareAroundOrigin,
},
{
name: "R(): square around 20 20",
args: args{minX: 10, minY: 10, maxX: 30, maxY: 30},
want: squareAround2020,
},
{
name: "R(): rectangle around origin",
args: args{minX: -20, minY: -10, maxX: 20, maxY: 10},
want: rectangleAroundOrigin,
},
{
name: "R(): square around 20 20",
args: args{minX: 0, minY: 10, maxX: 40, maxY: 30},
want: rectangleAround2020,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := pixel.R(tt.args.minX, tt.args.minY, tt.args.maxX, tt.args.maxY); !reflect.DeepEqual(got, tt.want) {
t.Errorf("R() = %v, want %v", got, tt.want)
}
})
}
}
func TestRect_String(t *testing.T) {
tests := []struct {
name string
r pixel.Rect
want string
}{
{
name: "Rect.String(): square around origin",
r: squareAroundOrigin,
want: "Rect(-10, -10, 10, 10)",
},
{
name: "Rect.String(): square around 20 20",
r: squareAround2020,
want: "Rect(10, 10, 30, 30)",
},
{
name: "Rect.String(): rectangle around origin",
r: rectangleAroundOrigin,
want: "Rect(-20, -10, 20, 10)",
},
{
name: "Rect.String(): square around 20 20",
r: rectangleAround2020,
want: "Rect(0, 10, 40, 30)",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.r.String(); got != tt.want {
t.Errorf("Rect.String() = %v, want %v", got, tt.want)
}
})
}
}
func TestRect_Norm(t *testing.T) {
tests := []struct {
name string
r pixel.Rect
want pixel.Rect
}{
{
name: "Rect.Norm(): square around origin",
r: squareAroundOrigin,
want: squareAroundOrigin,
},
{
name: "Rect.Norm(): square around 20 20",
r: squareAround2020,
want: squareAround2020,
},
{
name: "Rect.Norm(): rectangle around origin",
r: rectangleAroundOrigin,
want: rectangleAroundOrigin,
},
{
name: "Rect.Norm(): square around 20 20",
r: rectangleAround2020,
want: rectangleAround2020,
},
{
name: "Rect.Norm(): square around origin unnormalized",
r: pixel.Rect{Min: squareAroundOrigin.Max, Max: squareAroundOrigin.Min},
want: squareAroundOrigin,
},
{
name: "Rect.Norm(): square around 20 20 unnormalized",
r: pixel.Rect{Min: squareAround2020.Max, Max: squareAround2020.Min},
want: squareAround2020,
},
{
name: "Rect.Norm(): rectangle around origin unnormalized",
r: pixel.Rect{Min: rectangleAroundOrigin.Max, Max: rectangleAroundOrigin.Min},
want: rectangleAroundOrigin,
},
{
name: "Rect.Norm(): square around 20 20 unnormalized",
r: pixel.Rect{Min: rectangleAround2020.Max, Max: rectangleAround2020.Min},
want: rectangleAround2020,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.r.Norm(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Rect.Norm() = %v, want %v", got, tt.want)
}
})
}
}
func TestRect_W(t *testing.T) {
tests := []struct {
name string
r pixel.Rect
want float64
}{
{
name: "Rect.W(): square around origin",
r: squareAroundOrigin,
want: 20,
},
{
name: "Rect.W(): square around 20 20",
r: squareAround2020,
want: 20,
},
{
name: "Rect.W(): rectangle around origin",
r: rectangleAroundOrigin,
want: 40,
},
{
name: "Rect.W(): square around 20 20",
r: rectangleAround2020,
want: 40,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.r.W(); got != tt.want {
t.Errorf("Rect.W() = %v, want %v", got, tt.want)
}
})
}
}
func TestRect_H(t *testing.T) {
tests := []struct {
name string
r pixel.Rect
want float64
}{
{
name: "Rect.H(): square around origin",
r: squareAroundOrigin,
want: 20,
},
{
name: "Rect.H(): square around 20 20",
r: squareAround2020,
want: 20,
},
{
name: "Rect.H(): rectangle around origin",
r: rectangleAroundOrigin,
want: 20,
},
{
name: "Rect.H(): square around 20 20",
r: rectangleAround2020,
want: 20,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.r.H(); got != tt.want {
t.Errorf("Rect.H() = %v, want %v", got, tt.want)
}
})
}
}
func TestRect_Size(t *testing.T) {
tests := []struct {
name string
r pixel.Rect
want pixel.Vec
}{
{
name: "Rect.Size(): square around origin",
r: squareAroundOrigin,
want: pixel.V(20, 20),
},
{
name: "Rect.Size(): square around 20 20",
r: squareAround2020,
want: pixel.V(20, 20),
},
{
name: "Rect.Size(): rectangle around origin",
r: rectangleAroundOrigin,
want: pixel.V(40, 20),
},
{
name: "Rect.Size(): square around 20 20",
r: rectangleAround2020,
want: pixel.V(40, 20),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.r.Size(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Rect.Size() = %v, want %v", got, tt.want)
}
})
}
}
func TestRect_Area(t *testing.T) {
tests := []struct {
name string
r pixel.Rect
want float64
}{
{
name: "Rect.Area(): square around origin",
r: squareAroundOrigin,
want: 400,
},
{
name: "Rect.Area(): square around 20 20",
r: squareAround2020,
want: 400,
},
{
name: "Rect.Area(): rectangle around origin",
r: rectangleAroundOrigin,
want: 800,
},
{
name: "Rect.Area(): square around 20 20",
r: rectangleAround2020,
want: 800,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.r.Area(); got != tt.want {
t.Errorf("Rect.Area() = %v, want %v", got, tt.want)
}
})
}
}
func TestRect_Center(t *testing.T) {
tests := []struct {
name string
r pixel.Rect
want pixel.Vec
}{
{
name: "Rect.Center(): square around origin",
r: squareAroundOrigin,
want: pixel.V(0, 0),
},
{
name: "Rect.Center(): square around 20 20",
r: squareAround2020,
want: pixel.V(20, 20),
},
{
name: "Rect.Center(): rectangle around origin",
r: rectangleAroundOrigin,
want: pixel.V(0, 0),
},
{
name: "Rect.Center(): square around 20 20",
r: rectangleAround2020,
want: pixel.V(20, 20),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.r.Center(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Rect.Center() = %v, want %v", got, tt.want)
}
})
}
}
func TestRect_Moved(t *testing.T) {
positiveShift := pixel.V(10, 10)
negativeShift := pixel.V(-10, -10)
type args struct {
delta pixel.Vec
}
tests := []struct {
name string
r pixel.Rect
args args
want pixel.Rect
}{
{
name: "Rect.Moved(): positive shift",
r: squareAroundOrigin,
args: args{delta: positiveShift},
want: pixel.R(0, 0, 20, 20),
},
{
name: "Rect.Moved(): negative shift",
r: squareAroundOrigin,
args: args{delta: negativeShift},
want: pixel.R(-20, -20, 0, 0),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.r.Moved(tt.args.delta); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Rect.Moved() = %v, want %v", got, tt.want)
}
})
}
}
func TestRect_Resized(t *testing.T) {
// resize transformations
resizeByHalfAroundCenter := rectTestTransform{"by half around center", func(rect pixel.Rect) pixel.Rect {
return rect.Resized(rect.Center(), rect.Size().Scaled(0.5))
}}
resizeByHalfAroundMin := rectTestTransform{"by half around Min", func(rect pixel.Rect) pixel.Rect {
return rect.Resized(rect.Min, rect.Size().Scaled(0.5))
}}
resizeByHalfAroundMax := rectTestTransform{"by half around Max", func(rect pixel.Rect) pixel.Rect {
return rect.Resized(rect.Max, rect.Size().Scaled(0.5))
}}
resizeByHalfAroundMiddleOfLeftSide := rectTestTransform{"by half around middle of left side", func(rect pixel.Rect) pixel.Rect {
return rect.Resized(pixel.V(rect.Min.X, rect.Center().Y), rect.Size().Scaled(0.5))
}}
resizeByHalfAroundOrigin := rectTestTransform{"by half around the origin", func(rect pixel.Rect) pixel.Rect {
return rect.Resized(pixel.ZV, rect.Size().Scaled(0.5))
}}
testCases := []struct {
input pixel.Rect
transform rectTestTransform
answer pixel.Rect
}{
{squareAroundOrigin, resizeByHalfAroundCenter, pixel.R(-5, -5, 5, 5)},
{squareAround2020, resizeByHalfAroundCenter, pixel.R(15, 15, 25, 25)},
{rectangleAroundOrigin, resizeByHalfAroundCenter, pixel.R(-10, -5, 10, 5)},
{rectangleAround2020, resizeByHalfAroundCenter, pixel.R(10, 15, 30, 25)},
{squareAroundOrigin, resizeByHalfAroundMin, pixel.R(-10, -10, 0, 0)},
{squareAround2020, resizeByHalfAroundMin, pixel.R(10, 10, 20, 20)},
{rectangleAroundOrigin, resizeByHalfAroundMin, pixel.R(-20, -10, 0, 0)},
{rectangleAround2020, resizeByHalfAroundMin, pixel.R(0, 10, 20, 20)},
{squareAroundOrigin, resizeByHalfAroundMax, pixel.R(0, 0, 10, 10)},
{squareAround2020, resizeByHalfAroundMax, pixel.R(20, 20, 30, 30)},
{rectangleAroundOrigin, resizeByHalfAroundMax, pixel.R(0, 0, 20, 10)},
{rectangleAround2020, resizeByHalfAroundMax, pixel.R(20, 20, 40, 30)},
{squareAroundOrigin, resizeByHalfAroundMiddleOfLeftSide, pixel.R(-10, -5, 0, 5)},
{squareAround2020, resizeByHalfAroundMiddleOfLeftSide, pixel.R(10, 15, 20, 25)},
{rectangleAroundOrigin, resizeByHalfAroundMiddleOfLeftSide, pixel.R(-20, -5, 0, 5)},
{rectangleAround2020, resizeByHalfAroundMiddleOfLeftSide, pixel.R(0, 15, 20, 25)},
{squareAroundOrigin, resizeByHalfAroundOrigin, pixel.R(-5, -5, 5, 5)},
{squareAround2020, resizeByHalfAroundOrigin, pixel.R(5, 5, 15, 15)},
{rectangleAroundOrigin, resizeByHalfAroundOrigin, pixel.R(-10, -5, 10, 5)},
{rectangleAround2020, resizeByHalfAroundOrigin, pixel.R(0, 5, 20, 15)},
}
for _, testCase := range testCases {
t.Run(fmt.Sprintf("Resize %v %s", testCase.input, testCase.transform.name), func(t *testing.T) {
testResult := testCase.transform.f(testCase.input)
if testResult != testCase.answer {
t.Errorf("Got: %v, wanted: %v\n", testResult, testCase.answer)
}
})
}
}
func TestRect_ResizedMin(t *testing.T) {
grow := pixel.V(5, 5)
shrink := pixel.V(-5, -5)
type args struct {
size pixel.Vec
}
tests := []struct {
name string
r pixel.Rect
args args
want pixel.Rect
}{
{
name: "Rect.ResizedMin(): square around origin - growing",
r: squareAroundOrigin,
args: args{size: grow},
want: pixel.R(-10, -10, -5, -5),
},
{
name: "Rect.ResizedMin(): square around 20 20 - growing",
r: squareAround2020,
args: args{size: grow},
want: pixel.R(10, 10, 15, 15),
},
{
name: "Rect.ResizedMin(): rectangle around origin - growing",
r: rectangleAroundOrigin,
args: args{size: grow},
want: pixel.R(-20, -10, -15, -5),
},
{
name: "Rect.ResizedMin(): square around 20 20 - growing",
r: rectangleAround2020,
args: args{size: grow},
want: pixel.R(0, 10, 5, 15),
},
{
name: "Rect.ResizedMin(): square around origin - growing",
r: squareAroundOrigin,
args: args{size: shrink},
want: pixel.R(-10, -10, -15, -15),
},
{
name: "Rect.ResizedMin(): square around 20 20 - growing",
r: squareAround2020,
args: args{size: shrink},
want: pixel.R(10, 10, 5, 5),
},
{
name: "Rect.ResizedMin(): rectangle around origin - growing",
r: rectangleAroundOrigin,
args: args{size: shrink},
want: pixel.R(-20, -10, -25, -15),
},
{
name: "Rect.ResizedMin(): square around 20 20 - growing",
r: rectangleAround2020,
args: args{size: shrink},
want: pixel.R(0, 10, -5, 5),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.r.ResizedMin(tt.args.size); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Rect.ResizedMin() = %v, want %v", got, tt.want)
}
})
}
}
func TestRect_Contains(t *testing.T) {
type args struct {
u pixel.Vec
}
tests := []struct {
name string
r pixel.Rect
args args
want bool
}{
{
name: "Rect.Contains(): square and contained vector",
r: squareAroundOrigin,
args: args{u: pixel.V(-5, 5)},
want: true,
},
{
name: "Rect.Contains(): square and seperate vector",
r: squareAroundOrigin,
args: args{u: pixel.V(50, 55)},
want: false,
},
{
name: "Rect.Contains(): square and overlapping vector",
r: squareAroundOrigin,
args: args{u: pixel.V(0, 50)},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.r.Contains(tt.args.u); got != tt.want {
t.Errorf("Rect.Contains() = %v, want %v", got, tt.want)
}
})
}
}
func TestRect_Union(t *testing.T) {
type args struct {
s pixel.Rect
}
tests := []struct {
name string
r pixel.Rect
args args
want pixel.Rect
}{
{
name: "Rect.Union(): seperate squares",
r: squareAroundOrigin,
args: args{s: squareAround2020},
want: pixel.R(-10, -10, 30, 30),
},
{
name: "Rect.Union(): overlapping squares",
r: squareAroundOrigin,
args: args{s: pixel.R(0, 0, 20, 20)},
want: pixel.R(-10, -10, 20, 20),
},
{
name: "Rect.Union(): square within a square",
r: squareAroundOrigin,
args: args{s: pixel.R(-5, -5, 5, 5)},
want: squareAroundOrigin,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.r.Union(tt.args.s); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Rect.Union() = %v, want %v", got, tt.want)
}
})
}
}
func TestRect_Intersect(t *testing.T) {
type args struct {
s pixel.Rect
}
tests := []struct {
name string
r pixel.Rect
args args
want pixel.Rect
}{
{
name: "Rect.Union(): seperate squares",
r: squareAroundOrigin,
args: args{s: squareAround2020},
want: pixel.R(0, 0, 0, 0),
},
{
name: "Rect.Union(): overlapping squares",
r: squareAroundOrigin,
args: args{s: pixel.R(0, 0, 20, 20)},
want: pixel.R(0, 0, 10, 10),
},
{
name: "Rect.Union(): square within a square",
r: squareAroundOrigin,
args: args{s: pixel.R(-5, -5, 5, 5)},
want: pixel.R(-5, -5, 5, 5),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.r.Intersect(tt.args.s); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Rect.Intersect() = %v, want %v", got, tt.want)
}
})
}
}

122
sprite_test.go Normal file
View File

@ -0,0 +1,122 @@
package pixel_test
import (
"image/color"
"reflect"
"testing"
"github.com/faiface/pixel"
)
func TestNewSprite(t *testing.T) {
type args struct {
pic pixel.Picture
frame pixel.Rect
}
tests := []struct {
name string
args args
want *pixel.Sprite
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := pixel.NewSprite(tt.args.pic, tt.args.frame); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewSprite() = %v, want %v", got, tt.want)
}
})
}
}
func TestSprite_Set(t *testing.T) {
type args struct {
pic pixel.Picture
frame pixel.Rect
}
tests := []struct {
name string
s *pixel.Sprite
args args
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.s.Set(tt.args.pic, tt.args.frame)
})
}
}
func TestSprite_Picture(t *testing.T) {
tests := []struct {
name string
s *pixel.Sprite
want pixel.Picture
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.s.Picture(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Sprite.Picture() = %v, want %v", got, tt.want)
}
})
}
}
func TestSprite_Frame(t *testing.T) {
tests := []struct {
name string
s *pixel.Sprite
want pixel.Rect
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.s.Frame(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Sprite.Frame() = %v, want %v", got, tt.want)
}
})
}
}
func TestSprite_Draw(t *testing.T) {
type args struct {
t pixel.Target
matrix pixel.Matrix
}
tests := []struct {
name string
s *pixel.Sprite
args args
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.s.Draw(tt.args.t, tt.args.matrix)
})
}
}
func TestSprite_DrawColorMask(t *testing.T) {
type args struct {
t pixel.Target
matrix pixel.Matrix
mask color.Color
}
tests := []struct {
name string
s *pixel.Sprite
args args
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.s.DrawColorMask(tt.args.t, tt.args.matrix, tt.args.mask)
})
}
}

707
vec_test.go Normal file
View File

@ -0,0 +1,707 @@
package pixel_test
import (
"math"
"reflect"
"testing"
"github.com/faiface/pixel"
)
// closeEnough is a test helper function to establish if vectors are "close enough". This is to resolve floating point
// errors, specifically when dealing with `math.Pi`
func closeEnough(u, v pixel.Vec) bool {
uX, uY := math.Round(u.X), math.Round(u.Y)
vX, vY := math.Round(v.X), math.Round(v.Y)
return uX == vX && uY == vY
}
func TestV(t *testing.T) {
type args struct {
x float64
y float64
}
tests := []struct {
name string
args args
want pixel.Vec
}{
{
name: "V(): both 0",
args: args{x: 0, y: 0},
want: pixel.ZV,
},
{
name: "V(): x < y",
args: args{x: 0, y: 10},
want: pixel.Vec{X: 0, Y: 10},
},
{
name: "V(): x > y",
args: args{x: 10, y: 0},
want: pixel.Vec{X: 10, Y: 0},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := pixel.V(tt.args.x, tt.args.y); !reflect.DeepEqual(got, tt.want) {
t.Errorf("V() = %v, want %v", got, tt.want)
}
})
}
}
func TestUnit(t *testing.T) {
type args struct {
angle float64
}
tests := []struct {
name string
args args
want pixel.Vec
}{
{
name: "Unit(): 0 radians",
args: args{angle: 0},
want: pixel.V(1, 0),
},
{
name: "Unit(): pi radians",
args: args{angle: math.Pi},
want: pixel.V(-1, 0),
},
{
name: "Unit(): 10 * pi radians",
args: args{angle: 10 * math.Pi},
want: pixel.V(1, 0),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := pixel.Unit(tt.args.angle); !closeEnough(got, tt.want) {
t.Errorf("Unit() = %v, want %v", got, tt.want)
}
})
}
}
func TestVec_String(t *testing.T) {
tests := []struct {
name string
u pixel.Vec
want string
}{
{
name: "Vec.String(): both 0",
u: pixel.Vec{X: 0, Y: 0},
want: "Vec(0, 0)",
},
{
name: "Vec.String(): x < y",
u: pixel.Vec{X: 0, Y: 10},
want: "Vec(0, 10)",
},
{
name: "Vec.String(): x > y",
u: pixel.Vec{X: 10, Y: 0},
want: "Vec(10, 0)",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.u.String(); got != tt.want {
t.Errorf("Vec.String() = %v, want %v", got, tt.want)
}
})
}
}
func TestVec_XY(t *testing.T) {
tests := []struct {
name string
u pixel.Vec
wantX float64
wantY float64
}{
{
name: "Vec.XY(): both 0",
u: pixel.Vec{X: 0, Y: 0},
wantX: 0,
wantY: 0,
},
{
name: "Vec.XY(): x < y",
u: pixel.Vec{X: 0, Y: 10},
wantX: 0,
wantY: 10,
},
{
name: "Vec.XY(): x > y",
u: pixel.Vec{X: 10, Y: 0},
wantX: 10,
wantY: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotX, gotY := tt.u.XY()
if gotX != tt.wantX {
t.Errorf("Vec.XY() gotX = %v, want %v", gotX, tt.wantX)
}
if gotY != tt.wantY {
t.Errorf("Vec.XY() gotY = %v, want %v", gotY, tt.wantY)
}
})
}
}
func TestVec_Add(t *testing.T) {
type args struct {
v pixel.Vec
}
tests := []struct {
name string
u pixel.Vec
args args
want pixel.Vec
}{
{
name: "Vec.Add(): positive vector",
u: pixel.V(0, 10),
args: args{v: pixel.V(10, 10)},
want: pixel.V(10, 20),
},
{
name: "Vec.Add(): zero vector",
u: pixel.V(0, 10),
args: args{v: pixel.ZV},
want: pixel.V(0, 10),
},
{
name: "Vec.Add(): negative vector",
u: pixel.V(0, 10),
args: args{v: pixel.V(-20, -30)},
want: pixel.V(-20, -20),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.u.Add(tt.args.v); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Vec.Add() = %v, want %v", got, tt.want)
}
})
}
}
func TestVec_Sub(t *testing.T) {
type args struct {
v pixel.Vec
}
tests := []struct {
name string
u pixel.Vec
args args
want pixel.Vec
}{
{
name: "Vec.Sub(): positive vector",
u: pixel.V(0, 10),
args: args{v: pixel.V(10, 10)},
want: pixel.V(-10, 0),
},
{
name: "Vec.Sub(): zero vector",
u: pixel.V(0, 10),
args: args{v: pixel.ZV},
want: pixel.V(0, 10),
},
{
name: "Vec.Sub(): negative vector",
u: pixel.V(0, 10),
args: args{v: pixel.V(-20, -30)},
want: pixel.V(20, 40),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.u.Sub(tt.args.v); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Vec.Sub() = %v, want %v", got, tt.want)
}
})
}
}
func TestVec_To(t *testing.T) {
type args struct {
v pixel.Vec
}
tests := []struct {
name string
u pixel.Vec
args args
want pixel.Vec
}{
{
name: "Vec.To(): positive vector",
u: pixel.V(0, 10),
args: args{v: pixel.V(10, 10)},
want: pixel.V(10, 0),
},
{
name: "Vec.To(): zero vector",
u: pixel.V(0, 10),
args: args{v: pixel.ZV},
want: pixel.V(0, -10),
},
{
name: "Vec.To(): negative vector",
u: pixel.V(0, 10),
args: args{v: pixel.V(-20, -30)},
want: pixel.V(-20, -40),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.u.To(tt.args.v); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Vec.To() = %v, want %v", got, tt.want)
}
})
}
}
func TestVec_Scaled(t *testing.T) {
type args struct {
c float64
}
tests := []struct {
name string
u pixel.Vec
args args
want pixel.Vec
}{
{
name: "Vec.Scaled(): positive scale",
u: pixel.V(0, 10),
args: args{c: 10},
want: pixel.V(0, 100),
},
{
name: "Vec.Scaled(): zero scale",
u: pixel.V(0, 10),
args: args{c: 0},
want: pixel.ZV,
},
{
name: "Vec.Scaled(): identity scale",
u: pixel.V(0, 10),
args: args{c: 1},
want: pixel.V(0, 10),
},
{
name: "Vec.Scaled(): negative scale",
u: pixel.V(0, 10),
args: args{c: -10},
want: pixel.V(0, -100),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.u.Scaled(tt.args.c); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Vec.Scaled() = %v, want %v", got, tt.want)
}
})
}
}
func TestVec_ScaledXY(t *testing.T) {
type args struct {
v pixel.Vec
}
tests := []struct {
name string
u pixel.Vec
args args
want pixel.Vec
}{
{
name: "Vec.ScaledXY(): positive scale",
u: pixel.V(0, 10),
args: args{v: pixel.V(10, 20)},
want: pixel.V(0, 200),
},
{
name: "Vec.ScaledXY(): zero scale",
u: pixel.V(0, 10),
args: args{v: pixel.ZV},
want: pixel.ZV,
},
{
name: "Vec.ScaledXY(): identity scale",
u: pixel.V(0, 10),
args: args{v: pixel.V(1, 1)},
want: pixel.V(0, 10),
},
{
name: "Vec.ScaledXY(): negative scale",
u: pixel.V(0, 10),
args: args{v: pixel.V(-5, -10)},
want: pixel.V(0, -100),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.u.ScaledXY(tt.args.v); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Vec.ScaledXY() = %v, want %v", got, tt.want)
}
})
}
}
func TestVec_Len(t *testing.T) {
tests := []struct {
name string
u pixel.Vec
want float64
}{
{
name: "Vec.Len(): positive vector",
u: pixel.V(40, 30),
want: 50,
},
{
name: "Vec.Len(): zero vector",
u: pixel.ZV,
want: 0,
},
{
name: "Vec.Len(): negative vector",
u: pixel.V(-5, -12),
want: 13,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.u.Len(); got != tt.want {
t.Errorf("Vec.Len() = %v, want %v", got, tt.want)
}
})
}
}
func TestVec_Angle(t *testing.T) {
tests := []struct {
name string
u pixel.Vec
want float64
}{
{
name: "Vec.Angle(): positive vector",
u: pixel.V(0, 30),
want: math.Pi / 2,
},
{
name: "Vec.Angle(): zero vector",
u: pixel.ZV,
want: 0,
},
{
name: "Vec.Angle(): negative vector",
u: pixel.V(-5, -0),
want: math.Pi,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.u.Angle(); got != tt.want {
t.Errorf("Vec.Angle() = %v, want %v", got, tt.want)
}
})
}
}
func TestVec_Unit(t *testing.T) {
tests := []struct {
name string
u pixel.Vec
want pixel.Vec
}{
{
name: "Vec.Unit(): positive vector",
u: pixel.V(0, 30),
want: pixel.V(0, 1),
},
{
name: "Vec.Unit(): zero vector",
u: pixel.ZV,
want: pixel.V(1, 0),
},
{
name: "Vec.Unit(): negative vector",
u: pixel.V(-5, 0),
want: pixel.V(-1, 0),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.u.Unit(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Vec.Unit() = %v, want %v", got, tt.want)
}
})
}
}
func TestVec_Rotated(t *testing.T) {
type args struct {
angle float64
}
tests := []struct {
name string
u pixel.Vec
args args
want pixel.Vec
}{
{
name: "Vec.Rotated(): partial rotation",
u: pixel.V(0, 1),
args: args{angle: math.Pi / 2},
want: pixel.V(-1, 0),
},
{
name: "Vec.Rotated(): full rotation",
u: pixel.V(0, 1),
args: args{angle: 2 * math.Pi},
want: pixel.V(0, 1),
},
{
name: "Vec.Rotated(): zero rotation",
u: pixel.V(0, 1),
args: args{angle: 0},
want: pixel.V(0, 1),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.u.Rotated(tt.args.angle); !closeEnough(got, tt.want) {
t.Errorf("Vec.Rotated() = %v, want %v", got, tt.want)
}
})
}
}
func TestVec_Normal(t *testing.T) {
tests := []struct {
name string
u pixel.Vec
want pixel.Vec
}{
{
name: "Vec.Normal(): positive vector",
u: pixel.V(0, 30),
want: pixel.V(-30, 0),
},
{
name: "Vec.Normal(): zero vector",
u: pixel.ZV,
want: pixel.ZV,
},
{
name: "Vec.Normal(): negative vector",
u: pixel.V(-5, 0),
want: pixel.V(0, -5),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.u.Normal(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Vec.Normal() = %v, want %v", got, tt.want)
}
})
}
}
func TestVec_Dot(t *testing.T) {
type args struct {
v pixel.Vec
}
tests := []struct {
name string
u pixel.Vec
args args
want float64
}{
{
name: "Vec.Dot(): positive vector",
u: pixel.V(0, 30),
args: args{v: pixel.V(10, 10)},
want: 300,
},
{
name: "Vec.Dot(): zero vector",
u: pixel.ZV,
args: args{v: pixel.V(10, 10)},
want: 0,
},
{
name: "Vec.Dot(): negative vector",
u: pixel.V(-5, 1),
args: args{v: pixel.V(10, 10)},
want: -40,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.u.Dot(tt.args.v); got != tt.want {
t.Errorf("Vec.Dot() = %v, want %v", got, tt.want)
}
})
}
}
func TestVec_Cross(t *testing.T) {
type args struct {
v pixel.Vec
}
tests := []struct {
name string
u pixel.Vec
args args
want float64
}{
{
name: "Vec.Cross(): positive vector",
u: pixel.V(0, 30),
args: args{v: pixel.V(10, 10)},
want: -300,
},
{
name: "Vec.Cross(): zero vector",
u: pixel.ZV,
args: args{v: pixel.V(10, 10)},
want: 0,
},
{
name: "Vec.Cross(): negative vector",
u: pixel.V(-5, 1),
args: args{v: pixel.V(10, 10)},
want: -60,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.u.Cross(tt.args.v); got != tt.want {
t.Errorf("Vec.Cross() = %v, want %v", got, tt.want)
}
})
}
}
func TestVec_Project(t *testing.T) {
type args struct {
v pixel.Vec
}
tests := []struct {
name string
u pixel.Vec
args args
want pixel.Vec
}{
{
name: "Vec.Project(): positive vector",
u: pixel.V(0, 30),
args: args{v: pixel.V(10, 10)},
want: pixel.V(15, 15),
},
{
name: "Vec.Project(): zero vector",
u: pixel.ZV,
args: args{v: pixel.V(10, 10)},
want: pixel.ZV,
},
{
name: "Vec.Project(): negative vector",
u: pixel.V(-30, 0),
args: args{v: pixel.V(10, 10)},
want: pixel.V(-15, -15),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.u.Project(tt.args.v); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Vec.Project() = %v, want %v", got, tt.want)
}
})
}
}
func TestVec_Map(t *testing.T) {
type args struct {
f func(float64) float64
}
tests := []struct {
name string
u pixel.Vec
args args
want pixel.Vec
}{
{
name: "Vec.Map(): positive vector",
u: pixel.V(0, 25),
args: args{f: math.Sqrt},
want: pixel.V(0, 5),
},
{
name: "Vec.Map(): zero vector",
u: pixel.ZV,
args: args{f: math.Sqrt},
want: pixel.ZV,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.u.Map(tt.args.f); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Vec.Map() = %v, want %v", got, tt.want)
}
})
}
}
func TestLerp(t *testing.T) {
type args struct {
a pixel.Vec
b pixel.Vec
t float64
}
tests := []struct {
name string
args args
want pixel.Vec
}{
{
name: "Lerp(): t = 0",
args: args{a: pixel.V(10, 10), b: pixel.ZV, t: 0},
want: pixel.V(10, 10),
},
{
name: "Lerp(): t = 1/4",
args: args{a: pixel.V(10, 10), b: pixel.ZV, t: .25},
want: pixel.V(7.5, 7.5),
},
{
name: "Lerp(): t = 1/2",
args: args{a: pixel.V(10, 10), b: pixel.ZV, t: .5},
want: pixel.V(5, 5),
},
{
name: "Lerp(): t = 1",
args: args{a: pixel.V(10, 10), b: pixel.ZV, t: 1},
want: pixel.ZV,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := pixel.Lerp(tt.args.a, tt.args.b, tt.args.t); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Lerp() = %v, want %v", got, tt.want)
}
})
}
}