added a function to run the tests on the software backend
This commit is contained in:
parent
7fa1f9096f
commit
d1d2186a2f
1 changed files with 70 additions and 0 deletions
|
@ -3,6 +3,7 @@ package canvas_test
|
|||
import (
|
||||
"fmt"
|
||||
"image"
|
||||
_ "image/jpeg"
|
||||
"image/png"
|
||||
"math"
|
||||
"os"
|
||||
|
@ -12,6 +13,7 @@ import (
|
|||
|
||||
"github.com/go-gl/gl/v3.2-core/gl"
|
||||
"github.com/tfriedel6/canvas"
|
||||
"github.com/tfriedel6/canvas/backend/software"
|
||||
"github.com/tfriedel6/canvas/sdlcanvas"
|
||||
)
|
||||
|
||||
|
@ -90,6 +92,74 @@ func run(t *testing.T, fn func(cv *canvas.Canvas)) {
|
|||
}
|
||||
}
|
||||
|
||||
func runsw(t *testing.T, fn func(cv *canvas.Canvas)) {
|
||||
backend := softwarebackend.New(100, 100)
|
||||
cv := canvas.New(backend)
|
||||
|
||||
cv.SetFillStyle("#000")
|
||||
cv.FillRect(0, 0, 100, 100)
|
||||
fn(cv)
|
||||
img := cv.GetImageData(0, 0, 100, 100)
|
||||
|
||||
caller, _, _, ok := runtime.Caller(1)
|
||||
if !ok {
|
||||
t.Fatal("Failed to get caller")
|
||||
}
|
||||
|
||||
callerFunc := runtime.FuncForPC(caller)
|
||||
if callerFunc == nil {
|
||||
t.Fatal("Failed to get caller function")
|
||||
}
|
||||
|
||||
const prefix = "canvas_test.Test"
|
||||
callerFuncName := callerFunc.Name()
|
||||
callerFuncName = callerFuncName[strings.Index(callerFuncName, prefix)+len(prefix):]
|
||||
|
||||
fileName := fmt.Sprintf("testdata/%s.png", callerFuncName)
|
||||
|
||||
_, err := os.Stat(fileName)
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
t.Fatalf("Failed to stat file \"%s\": %v", fileName, err)
|
||||
}
|
||||
|
||||
if os.IsNotExist(err) {
|
||||
err = writeImage(img, fileName)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
f, err := os.Open(fileName)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to open file \"%s\": %v", fileName, err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
refImg, err := png.Decode(f)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to decode file \"%s\": %v", fileName, err)
|
||||
}
|
||||
|
||||
if b := img.Bounds(); b.Min.X != 0 || b.Min.Y != 0 || b.Max.X != 100 || b.Max.Y != 100 {
|
||||
t.Fatalf("Image bounds must be 0,0,100,100")
|
||||
}
|
||||
if b := refImg.Bounds(); b.Min.X != 0 || b.Min.Y != 0 || b.Max.X != 100 || b.Max.Y != 100 {
|
||||
t.Fatalf("Image bounds must be 0,0,100,100")
|
||||
}
|
||||
|
||||
for y := 0; y < 100; y++ {
|
||||
for x := 0; x < 100; x++ {
|
||||
r1, g1, b1, a1 := img.At(x, y).RGBA()
|
||||
r2, g2, b2, a2 := refImg.At(x, y).RGBA()
|
||||
if r1 != r2 || g1 != g2 || b1 != b2 || a1 != a2 {
|
||||
writeImage(img, fmt.Sprintf("testdata/%s_fail.png", callerFuncName))
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func writeImage(img *image.RGBA, fileName string) error {
|
||||
f, err := os.OpenFile(fileName, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0777)
|
||||
if err != nil {
|
||||
|
|
Loading…
Reference in a new issue