47 lines
1 KiB
Go
47 lines
1 KiB
Go
package arena_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"git.brut.systems/judah/xx/arena"
|
|
"git.brut.systems/judah/xx/mem"
|
|
"git.brut.systems/judah/xx/testx"
|
|
)
|
|
|
|
func TestMakeSlice(t *testing.T) {
|
|
a := arena.Linear(1024 * mem.Kilobyte)
|
|
defer arena.Reset(a)
|
|
|
|
s := arena.MakeSlice[int](a, 99, 100)
|
|
testx.Expect(t, len(s) == 99, "len = %d, expected 99", len(s))
|
|
testx.Expect(t, cap(s) == 100, "cap = %d, expected 100", cap(s))
|
|
|
|
p := &s[0]
|
|
|
|
s[2] = 0xCAFE_DECAF
|
|
s = append(s, 2)
|
|
|
|
testx.Expect(t, p == &s[0], "p = %p, expected %p", p, &s[0])
|
|
|
|
p = &s[0]
|
|
s = append(s, 3) // cause a reallocation
|
|
|
|
testx.Expect(t, p != &s[0], "p = %p, expected %p", p, &s[0])
|
|
}
|
|
|
|
func TestExtendSlice(t *testing.T) {
|
|
a := arena.Linear(1024 * mem.Kilobyte)
|
|
defer arena.Reset(a)
|
|
|
|
s := arena.MakeSlice[int](a, 0, 5)
|
|
testx.Expect(t, cap(s) == 5)
|
|
|
|
s = arena.ExtendSlice(a, s, cap(s)+2)
|
|
testx.Expect(t, cap(s) == 7)
|
|
|
|
testx.ShouldPanic(t, func() {
|
|
// cause a non-contiguous slice extension (which will panic)
|
|
_ = arena.New[int](a)
|
|
s = arena.ExtendSlice(a, s, cap(s)*2)
|
|
})
|
|
}
|