2018-02-25 12:16:27 +01:00
|
|
|
// Copyright 2018 The Ebiten Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-03-05 16:38:56 +01:00
|
|
|
package packing_test
|
2018-02-25 12:16:27 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/packing"
|
2018-02-25 12:16:27 +01:00
|
|
|
)
|
|
|
|
|
2018-03-06 18:29:11 +01:00
|
|
|
func TestPage(t *testing.T) {
|
2018-02-25 12:16:27 +01:00
|
|
|
type Rect struct {
|
|
|
|
X int
|
|
|
|
Y int
|
|
|
|
Width int
|
|
|
|
Height int
|
|
|
|
}
|
|
|
|
|
|
|
|
type Op struct {
|
|
|
|
Width int
|
|
|
|
Height int
|
|
|
|
FreeNodeID int
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []struct {
|
2018-03-10 13:02:04 +01:00
|
|
|
Name string
|
|
|
|
In []Op
|
|
|
|
Out []*Rect
|
2018-02-25 12:16:27 +01:00
|
|
|
}{
|
|
|
|
{
|
2018-03-10 13:02:04 +01:00
|
|
|
Name: "alloc and random free",
|
2018-02-25 12:16:27 +01:00
|
|
|
In: []Op{
|
|
|
|
{100, 100, -1},
|
|
|
|
{100, 100, -1},
|
|
|
|
{100, 100, -1},
|
|
|
|
{100, 100, -1},
|
|
|
|
{100, 100, -1},
|
|
|
|
{100, 100, -1},
|
|
|
|
{0, 0, 1},
|
|
|
|
{0, 0, 3},
|
|
|
|
{0, 0, 5},
|
|
|
|
{0, 0, 0},
|
|
|
|
{0, 0, 2},
|
|
|
|
{0, 0, 4},
|
2018-03-10 12:53:20 +01:00
|
|
|
{1024, 1024, -1},
|
2018-02-25 12:16:27 +01:00
|
|
|
},
|
|
|
|
Out: []*Rect{
|
|
|
|
{0, 0, 100, 100},
|
|
|
|
{0, 100, 100, 100},
|
|
|
|
{0, 200, 100, 100},
|
|
|
|
{0, 300, 100, 100},
|
|
|
|
{0, 400, 100, 100},
|
|
|
|
{0, 500, 100, 100},
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
2018-03-10 12:53:20 +01:00
|
|
|
{0, 0, 1024, 1024},
|
2018-02-25 12:16:27 +01:00
|
|
|
},
|
|
|
|
},
|
2018-03-10 13:04:58 +01:00
|
|
|
{
|
|
|
|
Name: "alloc and free and empty",
|
|
|
|
In: []Op{
|
|
|
|
{31, 41, -1},
|
|
|
|
{59, 26, -1},
|
|
|
|
{53, 58, -1},
|
|
|
|
{97, 93, -1},
|
|
|
|
{28, 84, -1},
|
|
|
|
{62, 64, -1},
|
|
|
|
{0, 0, 0},
|
|
|
|
{0, 0, 1},
|
|
|
|
{0, 0, 2},
|
|
|
|
{0, 0, 3},
|
|
|
|
{0, 0, 4},
|
|
|
|
{0, 0, 5},
|
|
|
|
},
|
|
|
|
Out: []*Rect{
|
|
|
|
{0, 0, 31, 41},
|
|
|
|
{31, 0, 59, 26},
|
|
|
|
{31, 26, 53, 58},
|
|
|
|
{31, 84, 97, 93},
|
|
|
|
{0, 41, 28, 84},
|
|
|
|
{31, 177, 62, 64},
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
},
|
2018-02-25 12:16:27 +01:00
|
|
|
{
|
2018-03-10 13:02:04 +01:00
|
|
|
Name: "random Alloc",
|
2018-02-25 12:16:27 +01:00
|
|
|
In: []Op{
|
2018-03-10 12:53:20 +01:00
|
|
|
{100, 200, -1},
|
|
|
|
{1024, 1024, -1},
|
|
|
|
{100, 200, -1},
|
|
|
|
{50, 50, -1},
|
2018-03-09 08:02:57 +01:00
|
|
|
{200, 200, -1},
|
2018-03-10 12:53:20 +01:00
|
|
|
{1024, 1024, -1},
|
|
|
|
{500, 500, -1},
|
|
|
|
{600, 600, -1},
|
|
|
|
{100, 100, -1},
|
2018-02-25 12:16:27 +01:00
|
|
|
{0, 0, 2},
|
2018-03-10 12:53:20 +01:00
|
|
|
{100, 200, -1},
|
2018-02-25 12:16:27 +01:00
|
|
|
},
|
|
|
|
Out: []*Rect{
|
2018-03-10 12:53:20 +01:00
|
|
|
{0, 0, 100, 200},
|
2018-02-25 12:16:27 +01:00
|
|
|
nil,
|
2018-03-10 12:53:20 +01:00
|
|
|
{0, 200, 100, 200},
|
|
|
|
{0, 400, 50, 50},
|
|
|
|
{100, 0, 200, 200},
|
2018-02-25 12:16:27 +01:00
|
|
|
nil,
|
2018-03-10 12:53:20 +01:00
|
|
|
{100, 200, 500, 500},
|
2018-02-25 12:16:27 +01:00
|
|
|
nil,
|
2018-03-10 12:53:20 +01:00
|
|
|
{0, 450, 100, 100},
|
2018-02-25 12:16:27 +01:00
|
|
|
nil,
|
2018-03-10 12:53:20 +01:00
|
|
|
{0, 200, 100, 200},
|
2018-02-25 12:16:27 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2018-03-10 13:02:04 +01:00
|
|
|
Name: "fill squares",
|
2018-02-25 12:16:27 +01:00
|
|
|
In: []Op{
|
2018-03-10 12:53:20 +01:00
|
|
|
{256, 256, -1},
|
|
|
|
{256, 256, -1},
|
|
|
|
{256, 256, -1},
|
|
|
|
{256, 256, -1},
|
2018-03-09 08:02:57 +01:00
|
|
|
|
2018-03-10 12:53:20 +01:00
|
|
|
{256, 256, -1},
|
|
|
|
{256, 256, -1},
|
|
|
|
{256, 256, -1},
|
|
|
|
{256, 256, -1},
|
2018-03-09 08:02:57 +01:00
|
|
|
|
2018-03-10 12:53:20 +01:00
|
|
|
{256, 256, -1},
|
|
|
|
{256, 256, -1},
|
|
|
|
{256, 256, -1},
|
|
|
|
{256, 256, -1},
|
2018-03-09 08:02:57 +01:00
|
|
|
|
2018-03-10 12:53:20 +01:00
|
|
|
{256, 256, -1},
|
|
|
|
{256, 256, -1},
|
|
|
|
{256, 256, -1},
|
|
|
|
{256, 256, -1},
|
2018-03-09 08:02:57 +01:00
|
|
|
|
2018-03-10 12:53:20 +01:00
|
|
|
{256, 256, -1},
|
2018-02-25 12:16:27 +01:00
|
|
|
},
|
|
|
|
Out: []*Rect{
|
2018-03-10 12:53:20 +01:00
|
|
|
{0, 0, 256, 256},
|
|
|
|
{0, 256, 256, 256},
|
|
|
|
{0, 512, 256, 256},
|
|
|
|
{0, 768, 256, 256},
|
2018-03-09 08:02:57 +01:00
|
|
|
|
2018-03-10 12:53:20 +01:00
|
|
|
{256, 0, 256, 256},
|
|
|
|
{512, 0, 256, 256},
|
|
|
|
{768, 0, 256, 256},
|
|
|
|
{256, 256, 256, 256},
|
2018-03-09 08:02:57 +01:00
|
|
|
|
2018-03-10 12:53:20 +01:00
|
|
|
{256, 512, 256, 256},
|
|
|
|
{256, 768, 256, 256},
|
|
|
|
{512, 256, 256, 256},
|
|
|
|
{768, 256, 256, 256},
|
2018-03-09 08:02:57 +01:00
|
|
|
|
2018-03-10 12:53:20 +01:00
|
|
|
{512, 512, 256, 256},
|
|
|
|
{512, 768, 256, 256},
|
|
|
|
{768, 512, 256, 256},
|
|
|
|
{768, 768, 256, 256},
|
2018-02-25 12:16:27 +01:00
|
|
|
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2018-03-10 13:02:04 +01:00
|
|
|
Name: "fill not fitting squares",
|
2018-02-25 12:16:27 +01:00
|
|
|
In: []Op{
|
2018-03-10 12:53:20 +01:00
|
|
|
{300, 300, -1},
|
|
|
|
{300, 300, -1},
|
|
|
|
{300, 300, -1},
|
|
|
|
{300, 300, -1},
|
|
|
|
{300, 300, -1},
|
|
|
|
{300, 300, -1},
|
|
|
|
{300, 300, -1},
|
|
|
|
{300, 300, -1},
|
|
|
|
{300, 300, -1},
|
|
|
|
{300, 300, -1},
|
2018-02-25 12:16:27 +01:00
|
|
|
},
|
|
|
|
Out: []*Rect{
|
2018-03-10 12:53:20 +01:00
|
|
|
{0, 0, 300, 300},
|
|
|
|
{0, 300, 300, 300},
|
|
|
|
{0, 600, 300, 300},
|
|
|
|
{300, 0, 300, 300},
|
|
|
|
{600, 0, 300, 300},
|
|
|
|
{300, 300, 300, 300},
|
|
|
|
{300, 600, 300, 300},
|
|
|
|
{600, 300, 300, 300},
|
|
|
|
{600, 600, 300, 300},
|
2018-02-25 12:16:27 +01:00
|
|
|
nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-03-10 13:02:04 +01:00
|
|
|
for _, c := range cases {
|
2021-10-02 12:58:48 +02:00
|
|
|
p := packing.NewPage(1024, 1024)
|
|
|
|
nodes := []*packing.Node{}
|
2018-03-10 13:04:58 +01:00
|
|
|
nnodes := 0
|
|
|
|
for i, in := range c.In {
|
2018-02-25 12:16:27 +01:00
|
|
|
if in.FreeNodeID == -1 {
|
|
|
|
n := p.Alloc(in.Width, in.Height)
|
|
|
|
nodes = append(nodes, n)
|
2018-03-10 13:04:58 +01:00
|
|
|
nnodes++
|
2018-02-25 12:16:27 +01:00
|
|
|
} else {
|
|
|
|
p.Free(nodes[in.FreeNodeID])
|
|
|
|
nodes = append(nodes, nil)
|
2018-03-10 13:04:58 +01:00
|
|
|
nnodes--
|
|
|
|
}
|
|
|
|
if nnodes < 0 {
|
|
|
|
panic("not reached")
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.IsEmpty() != (nnodes == 0) {
|
|
|
|
t.Errorf("%s: nodes[%d]: page.IsEmpty(): got: %v, want: %v", c.Name, i, p.IsEmpty(), (nnodes == 0))
|
2018-02-25 12:16:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for i, out := range c.Out {
|
|
|
|
if nodes[i] == nil {
|
|
|
|
if out != nil {
|
2018-03-10 13:02:04 +01:00
|
|
|
t.Errorf("%s: nodes[%d]: should be nil but %v", c.Name, i, out)
|
2018-02-25 12:16:27 +01:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
x, y, width, height := nodes[i].Region()
|
|
|
|
got := Rect{x, y, width, height}
|
|
|
|
if out == nil {
|
2018-03-10 13:02:04 +01:00
|
|
|
t.Errorf("%s: nodes[%d]: got: %v, want: %v", c.Name, i, got, nil)
|
2018-02-25 12:16:27 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
want := *out
|
|
|
|
if got != want {
|
2018-03-10 13:02:04 +01:00
|
|
|
t.Errorf("%s: nodes[%d]: got: %v, want: %v", c.Name, i, got, want)
|
2018-02-25 12:16:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-10 17:13:53 +01:00
|
|
|
|
2022-11-11 13:15:28 +01:00
|
|
|
func TestAlloc(t *testing.T) {
|
|
|
|
p := packing.NewPage(1024, 2048)
|
|
|
|
w, h := p.Size()
|
|
|
|
p.Alloc(w/2, h/2)
|
|
|
|
|
|
|
|
n0 := p.Alloc(w*3/2, h*2)
|
2019-11-17 16:23:10 +01:00
|
|
|
if n0 == nil {
|
2022-11-11 13:15:28 +01:00
|
|
|
t.Errorf("p.Alloc failed: width: %d, height: %d", w*3/2, h*2)
|
2018-03-10 17:13:53 +01:00
|
|
|
}
|
2022-11-11 13:15:28 +01:00
|
|
|
n1 := p.Alloc(w/2, h*3/2)
|
2019-11-17 16:23:10 +01:00
|
|
|
if n1 == nil {
|
2022-11-11 13:15:28 +01:00
|
|
|
t.Errorf("p.Alloc failed: width: %d, height: %d", w/2, h*3/2)
|
2018-03-10 17:13:53 +01:00
|
|
|
}
|
2022-11-11 11:25:52 +01:00
|
|
|
if p.Alloc(1, 1) != nil {
|
2022-11-11 13:15:28 +01:00
|
|
|
t.Errorf("p.Alloc(1, 1) must fail but not")
|
2022-11-11 11:25:52 +01:00
|
|
|
}
|
2019-11-17 16:23:10 +01:00
|
|
|
p.Free(n1)
|
2022-11-11 13:15:28 +01:00
|
|
|
if p.Alloc(1, 1) == nil {
|
|
|
|
t.Errorf("p.Alloc(1, 1) failed")
|
2022-11-11 11:34:10 +01:00
|
|
|
}
|
2022-11-11 13:15:28 +01:00
|
|
|
p.Free(n0)
|
2018-03-10 17:13:53 +01:00
|
|
|
}
|
|
|
|
|
2022-11-11 13:15:28 +01:00
|
|
|
func TestAlloc2(t *testing.T) {
|
|
|
|
p := packing.NewPage(1024, 2048)
|
|
|
|
w, h := p.Size()
|
|
|
|
p.Alloc(w/2, h/2)
|
|
|
|
n1 := p.Alloc(w/2, h/2)
|
|
|
|
n2 := p.Alloc(w/2, h/2)
|
|
|
|
p.Alloc(w/2, h/2)
|
2018-03-10 17:13:53 +01:00
|
|
|
p.Free(n1)
|
|
|
|
p.Free(n2)
|
2019-11-17 16:23:10 +01:00
|
|
|
|
2022-11-11 13:15:28 +01:00
|
|
|
n3 := p.Alloc(w, h*2)
|
2019-11-17 16:23:10 +01:00
|
|
|
if n3 == nil {
|
2022-11-11 13:15:28 +01:00
|
|
|
t.Errorf("p.Alloc failed: width: %d, height: %d", w, h*2)
|
2018-03-10 17:13:53 +01:00
|
|
|
}
|
2022-11-11 13:15:28 +01:00
|
|
|
n4 := p.Alloc(w, h)
|
2019-11-17 16:23:10 +01:00
|
|
|
if n4 == nil {
|
2022-11-11 13:15:28 +01:00
|
|
|
t.Errorf("p.Alloc failed: width: %d, height: %d", w, h)
|
2018-03-10 17:13:53 +01:00
|
|
|
}
|
2019-11-17 16:23:10 +01:00
|
|
|
p.Free(n4)
|
|
|
|
p.Free(n3)
|
2020-12-31 09:33:24 +01:00
|
|
|
}
|
|
|
|
|
2022-11-11 13:15:28 +01:00
|
|
|
func TestAllocJustSize(t *testing.T) {
|
2021-10-02 12:58:48 +02:00
|
|
|
p := packing.NewPage(1024, 4096)
|
2022-11-11 13:15:28 +01:00
|
|
|
if p.Alloc(4096, 4096) == nil {
|
|
|
|
t.Errorf("got: nil, want: non-nil")
|
2022-11-11 11:34:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-11 13:15:28 +01:00
|
|
|
// Issue #1454
|
|
|
|
func TestAllocTooMuch(t *testing.T) {
|
2022-11-11 11:34:10 +01:00
|
|
|
p := packing.NewPage(1024, 4096)
|
2022-11-11 13:15:28 +01:00
|
|
|
p.Alloc(1, 1)
|
|
|
|
if p.Alloc(4096, 4096) != nil {
|
|
|
|
t.Errorf("got: non-nil, want: nil")
|
2020-12-31 09:33:24 +01:00
|
|
|
}
|
|
|
|
}
|
2022-11-11 14:08:29 +01:00
|
|
|
|
|
|
|
func TestNonSquareAlloc(t *testing.T) {
|
|
|
|
p := packing.NewPage(1024, 16384)
|
|
|
|
n0 := p.Alloc(16384, 1)
|
|
|
|
if _, h := p.Size(); h != 1024 {
|
|
|
|
t.Errorf("got: %d, want: 1024", h)
|
|
|
|
}
|
|
|
|
n1 := p.Alloc(16384, 1)
|
|
|
|
if _, h := p.Size(); h != 1024 {
|
|
|
|
t.Errorf("got: %d, want: 1024", h)
|
|
|
|
}
|
|
|
|
p.Free(n0)
|
|
|
|
p.Free(n1)
|
|
|
|
}
|