package fit
import (
"testing"
)
type selectTest struct {
item testItem
bin int
pos int
}
type testItem int64
func (t *testItem) Size() int64 {
return int64(*t)
}
var firstTests = []selectTest{
{
item: testItem(8),
bin: 0,
pos: 0,
},
{
item: testItem(1),
bin: 0,
pos: 1,
},
{
item: testItem(3),
bin: 1,
pos: 0,
},
{
item: testItem(1),
bin: 0,
pos: 2,
},
{
item: testItem(4),
bin: 1,
pos: 1,
},
}
// one must swap the bins in head, as they are sorted in place. this isn't nice.
var bestTests = []selectTest{
{
item: testItem(6),
bin: 0,
pos: 0,
},
{
item: testItem(8),
bin: 1,
pos: 0,
},
{
item: testItem(1),
bin: 0,
pos: 1,
},
{
item: testItem(2),
bin: 1,
pos: 1,
},
{
item: testItem(1),
bin: 0,
pos: 2,
},
}
var worstTests = []selectTest{
{
item: testItem(6),
bin: 0,
pos: 0,
},
{
item: testItem(8),
bin: 1,
pos: 0,
},
{
item: testItem(1),
bin: 0,
pos: 1,
},
{
item: testItem(2),
bin: 0,
pos: 2,
},
{
item: testItem(1),
bin: 0,
pos: 1,
},
}
func testSelect(t *testing.T, binSelect BinSelectFunc, tests []selectTest) {
bins := []Bin{}
for _, test := range tests {
var err error
bins, err = binSelect(bins, 10, &test.item)
if err != nil {
t.FailNow()
}
t.Logf("%#v", bins)
binItem := bins[test.bin].Items()[test.pos]
if binItem.Size() != test.item.Size() {
t.Logf("items differ: %+v %+v", binItem, test.item)
t.Fail()
}
}
}
func TestFirst(t *testing.T) {
testSelect(t, First, firstTests)
}
func TestBest(t *testing.T) {
testSelect(t, Best, bestTests)
}
func TestWorst(t *testing.T) {
testSelect(t, Worst, worstTests)
}