restorable: Add TestRestoreChain2

This commit is contained in:
Hajime Hoshi 2018-03-25 19:42:44 +09:00
parent c0b1919020
commit fc47d2843a
3 changed files with 51 additions and 3 deletions

View File

@ -110,6 +110,11 @@ func (i *Image) makeStale() {
i.basePixels = nil i.basePixels = nil
i.drawImageHistory = nil i.drawImageHistory = nil
i.stale = true i.stale = true
// Don't have to call makeStale recursively here.
// Restoring is done after topological sorting is done.
// If an image depends on another stale image, this means that
// the former image can be restored from the latest state of the latter image.
} }
// ReplacePixels replaces the image pixels with the given pixels slice. // ReplacePixels replaces the image pixels with the given pixels slice.

View File

@ -121,8 +121,6 @@ func (i *images) makeStaleIfDependingOnImpl(target *Image) {
} }
i.lastTarget = target i.lastTarget = target
for img := range i.images { for img := range i.images {
// TODO: This seems not enough: What if img becomes stale but what about
// other images depend on img? (#357)
img.makeStaleIfDependingOn(target) img.makeStaleIfDependingOn(target)
} }
} }

View File

@ -117,7 +117,7 @@ func TestRestoreChain(t *testing.T) {
clr := color.RGBA{0x00, 0x00, 0x00, 0xff} clr := color.RGBA{0x00, 0x00, 0x00, 0xff}
fill(imgs[0], clr.R, clr.G, clr.B, clr.A) fill(imgs[0], clr.R, clr.G, clr.B, clr.A)
for i := 0; i < num-1; i++ { for i := 0; i < num-1; i++ {
imgs[i+1].DrawImage(imgs[i], 0, 0, 1, 1, nil, nil, opengl.CompositeModeSourceOver, graphics.FilterNearest) imgs[i+1].DrawImage(imgs[i], 0, 0, 1, 1, nil, nil, opengl.CompositeModeCopy, graphics.FilterNearest)
} }
if err := ResolveStaleImages(); err != nil { if err := ResolveStaleImages(); err != nil {
t.Fatal(err) t.Fatal(err)
@ -134,6 +134,51 @@ func TestRestoreChain(t *testing.T) {
} }
} }
func TestRestoreChain2(t *testing.T) {
const num = 10
imgs := []*Image{}
for i := 0; i < num; i++ {
img := NewImage(1, 1, false)
fill(img, 0, 0, 0, 0)
imgs = append(imgs, img)
}
defer func() {
for _, img := range imgs {
img.Dispose()
}
}()
clr0 := color.RGBA{0xff, 0x00, 0x00, 0xff}
fill(imgs[0], clr0.R, clr0.G, clr0.B, clr0.A)
clr7 := color.RGBA{0x00, 0xff, 0x00, 0xff}
fill(imgs[7], clr7.R, clr7.G, clr7.B, clr7.A)
clr8 := color.RGBA{0x00, 0x00, 0xff, 0xff}
fill(imgs[8], clr8.R, clr8.G, clr8.B, clr8.A)
imgs[8].DrawImage(imgs[7], 0, 0, 1, 1, nil, nil, opengl.CompositeModeCopy, graphics.FilterNearest)
imgs[9].DrawImage(imgs[8], 0, 0, 1, 1, nil, nil, opengl.CompositeModeCopy, graphics.FilterNearest)
for i := 0; i < 7; i++ {
imgs[i+1].DrawImage(imgs[i], 0, 0, 1, 1, nil, nil, opengl.CompositeModeCopy, graphics.FilterNearest)
}
if err := ResolveStaleImages(); err != nil {
t.Fatal(err)
}
if err := Restore(); err != nil {
t.Fatal(err)
}
for i, img := range imgs {
want := clr0
if i == 8 || i == 9 {
want = clr7
}
got := byteSliceToColor(img.BasePixelsForTesting(), 0)
if !sameColors(got, want, 1) {
t.Errorf("%d: got %v, want %v", i, got, want)
}
}
}
func TestRestoreOverrideSource(t *testing.T) { func TestRestoreOverrideSource(t *testing.T) {
img0 := NewImage(1, 1, false) img0 := NewImage(1, 1, false)
fill(img0, 0, 0, 0, 0) fill(img0, 0, 0, 0, 0)