Skip to content

Commit

Permalink
fix tests on arm64, ppc64le, s390x
Browse files Browse the repository at this point in the history
  • Loading branch information
disintegration committed Aug 24, 2019
1 parent 5c2e6f4 commit a999ff8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
5 changes: 2 additions & 3 deletions colors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,16 +346,15 @@ func TestBrightness(t *testing.T) {
image.Rect(0, 0, 5, 3),
[]uint8{
0x00, 0x40, 0x00, 0x40, 0x00,
0x60, 0xB0, 0xA0, 0xB0, 0x60,
0x60, 0xB0, 0xA1, 0xB0, 0x60,
0x00, 0x80, 0x00, 0x80, 0x00,
},
[]uint8{
0x00, 0x00, 0x00, 0x00, 0x00,
0x14, 0x64, 0x53, 0x64, 0x14,
0x14, 0x64, 0x55, 0x64, 0x14,
0x00, 0x34, 0x00, 0x34, 0x00,
},
},

{
"brightness (100)",
100,
Expand Down
33 changes: 31 additions & 2 deletions gift_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
_ "image/jpeg"
_ "image/png"
"os"
"reflect"
"runtime"
"testing"
)

Expand Down Expand Up @@ -589,12 +589,41 @@ func TestGolden(t *testing.T) {
dst := image.NewNRGBA(g.Bounds(src.Bounds()))
g.Draw(dst, src)
want := loadImageNRGBA(t, "testdata/dst_"+name+".png")
if !reflect.DeepEqual(dst, want) {
if !goldenEqual(dst, want) {
t.Errorf("resulting image differs from golden: %s", name)
}
}
}

// goldenEqual compares two NRGBA images. It is used in golden tests only.
// All the golden images are generated on amd64 architecture. Due to differences
// in floating-point rounding on different architectures, we need to add some
// level of tolerance when comparing images on architectures other than amd64.
// See https://golang.org/ref/spec#Floating_point_operators for information on
// fused multiply and add (FMA) instruction.
func goldenEqual(img1, img2 *image.NRGBA) bool {
maxDiff := 0
if runtime.GOARCH != "amd64" {
maxDiff = 1
}
if !img1.Rect.Eq(img2.Rect) {
return false
}
if len(img1.Pix) != len(img2.Pix) {
return false
}
for i := 0; i < len(img1.Pix); i++ {
diff := int(img1.Pix[i]) - int(img2.Pix[i])
if diff < 0 {
diff = -diff
}
if diff > maxDiff {
return false
}
}
return true
}

func BenchmarkFilter(b *testing.B) {
file, err := os.Open("testdata/src.jpg")
if err != nil {
Expand Down

0 comments on commit a999ff8

Please sign in to comment.