91 lines
1.7 KiB
Go
91 lines
1.7 KiB
Go
package pfile
|
|
|
|
import (
|
|
"math/rand"
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func (fl *freeList) Assert(t *testing.T, pageIDs ...uint64) {
|
|
t.Helper()
|
|
|
|
if len(fl.h) != len(pageIDs) {
|
|
t.Fatalf("FreeList: Expected %d pages but got %d.\n%v != %v",
|
|
len(pageIDs), len(fl.h), fl.h, pageIDs)
|
|
}
|
|
|
|
containsPageID := func(pageID uint64) bool {
|
|
for _, v := range fl.h {
|
|
if v == pageID {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
for _, pageID := range pageIDs {
|
|
if !containsPageID(pageID) {
|
|
t.Fatalf("Page not free: %d", pageID)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestFreeList(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
p0 := uint64(1 + rand.Int63())
|
|
|
|
type TestCase struct {
|
|
Name string
|
|
Put []uint64
|
|
Alloc int
|
|
Expected []uint64
|
|
}
|
|
|
|
testCases := []TestCase{
|
|
{
|
|
Name: "Alloc first page",
|
|
Put: []uint64{},
|
|
Alloc: 1,
|
|
Expected: []uint64{p0},
|
|
}, {
|
|
Name: "Alloc second page",
|
|
Put: []uint64{},
|
|
Alloc: 1,
|
|
Expected: []uint64{p0 + 1},
|
|
}, {
|
|
Name: "Put second page",
|
|
Put: []uint64{p0 + 1},
|
|
Alloc: 0,
|
|
Expected: []uint64{},
|
|
}, {
|
|
Name: "Alloc 2 pages",
|
|
Put: []uint64{},
|
|
Alloc: 2,
|
|
Expected: []uint64{p0 + 1, p0 + 2},
|
|
}, {
|
|
Name: "Put back and alloc pages",
|
|
Put: []uint64{p0},
|
|
Alloc: 3,
|
|
Expected: []uint64{p0, p0 + 3, p0 + 4},
|
|
}, {
|
|
Name: "Put back large and alloc",
|
|
Put: []uint64{p0, p0 + 2, p0 + 4, p0 + 442},
|
|
Alloc: 4,
|
|
Expected: []uint64{p0, p0 + 2, p0 + 4, p0 + 442},
|
|
},
|
|
}
|
|
|
|
fl := newFreeList(p0)
|
|
|
|
var pages []uint64
|
|
|
|
for _, tc := range testCases {
|
|
fl.Push(tc.Put...)
|
|
pages = fl.Pop(tc.Alloc, pages)
|
|
if !reflect.DeepEqual(pages, tc.Expected) {
|
|
t.Fatal(tc.Name, pages, tc.Expected)
|
|
}
|
|
}
|
|
}
|