From: Ian Davis Date: Tue, 25 Sep 2018 14:33:49 +0000 (+0100) Subject: image: avoid sharing test images between tests and benchmarks X-Git-Tag: go1.12beta1~1016 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=36a3d4f3fea7bbb22be061fbe830420990cd3ecf;p=gostls13.git image: avoid sharing test images between tests and benchmarks CL 136796 introduced benchmarks and refactored tests to use a common list of test images. The tests now fail when run with count > 2 since they rely on a fresh image each run. Fix this by changing the list of test images to a list of test image generator functions. Change-Id: I5884c6bccba5e29bf84ee546fa501bc258379f42 Reviewed-on: https://go-review.googlesource.com/137295 Reviewed-by: Brad Fitzpatrick Run-TryBot: Brad Fitzpatrick TryBot-Result: Gobot Gobot --- diff --git a/src/image/image_test.go b/src/image/image_test.go index 6f49752a25..dfd8eb35a8 100644 --- a/src/image/image_test.go +++ b/src/image/image_test.go @@ -24,25 +24,27 @@ func cmp(cm color.Model, c0, c1 color.Color) bool { var testImages = []struct { name string - image image + image func() image }{ - {"rgba", NewRGBA(Rect(0, 0, 10, 10))}, - {"rgba64", NewRGBA64(Rect(0, 0, 10, 10))}, - {"nrgba", NewNRGBA(Rect(0, 0, 10, 10))}, - {"nrgba64", NewNRGBA64(Rect(0, 0, 10, 10))}, - {"alpha", NewAlpha(Rect(0, 0, 10, 10))}, - {"alpha16", NewAlpha16(Rect(0, 0, 10, 10))}, - {"gray", NewGray(Rect(0, 0, 10, 10))}, - {"gray16", NewGray16(Rect(0, 0, 10, 10))}, - {"paletted", NewPaletted(Rect(0, 0, 10, 10), color.Palette{ - Transparent, - Opaque, - })}, + {"rgba", func() image { return NewRGBA(Rect(0, 0, 10, 10)) }}, + {"rgba64", func() image { return NewRGBA64(Rect(0, 0, 10, 10)) }}, + {"nrgba", func() image { return NewNRGBA(Rect(0, 0, 10, 10)) }}, + {"nrgba64", func() image { return NewNRGBA64(Rect(0, 0, 10, 10)) }}, + {"alpha", func() image { return NewAlpha(Rect(0, 0, 10, 10)) }}, + {"alpha16", func() image { return NewAlpha16(Rect(0, 0, 10, 10)) }}, + {"gray", func() image { return NewGray(Rect(0, 0, 10, 10)) }}, + {"gray16", func() image { return NewGray16(Rect(0, 0, 10, 10)) }}, + {"paletted", func() image { + return NewPaletted(Rect(0, 0, 10, 10), color.Palette{ + Transparent, + Opaque, + }) + }}, } func TestImage(t *testing.T) { for _, tc := range testImages { - m := tc.image + m := tc.image() if !Rect(0, 0, 10, 10).Eq(m.Bounds()) { t.Errorf("%T: want bounds %v, got %v", m, Rect(0, 0, 10, 10), m.Bounds()) continue @@ -120,9 +122,11 @@ func Test16BitsPerColorChannel(t *testing.T) { func BenchmarkAt(b *testing.B) { for _, tc := range testImages { b.Run(tc.name, func(b *testing.B) { + m := tc.image() b.ReportAllocs() + b.ResetTimer() for i := 0; i < b.N; i++ { - tc.image.At(4, 5) + m.At(4, 5) } }) } @@ -132,9 +136,11 @@ func BenchmarkSet(b *testing.B) { c := color.Gray{0xff} for _, tc := range testImages { b.Run(tc.name, func(b *testing.B) { + m := tc.image() b.ReportAllocs() + b.ResetTimer() for i := 0; i < b.N; i++ { - tc.image.Set(4, 5, c) + m.Set(4, 5, c) } }) }