github.com/tetratelabs/wazero@v1.7.3-0.20240513003603-48f702e154b5/internal/engine/wazevo/wazevoapi/pool_test.go (about) 1 package wazevoapi 2 3 import ( 4 "testing" 5 6 "github.com/tetratelabs/wazero/internal/testing/require" 7 ) 8 9 func TestNewNilVarLength(t *testing.T) { 10 v := NewNilVarLength[uint64]() 11 require.NotNil(t, v) 12 pool := NewVarLengthPool[uint64]() 13 v = v.Append(&pool, 1) 14 require.Equal(t, []uint64{1}, v.View()) 15 } 16 17 func TestAllocate(t *testing.T) { 18 pool := NewVarLengthPool[uint64]() 19 // Array: 20 v := pool.Allocate(5) 21 require.NotNil(t, v.arr) 22 require.Equal(t, 0, v.arr.next) 23 require.Equal(t, arraySize, cap(v.arr.arr)) 24 25 // Slice backed: 26 v = pool.Allocate(25) 27 require.NotNil(t, v.slc) 28 require.Equal(t, 0, len(*v.slc)) 29 v.Append(&pool, 1) 30 require.NotNil(t, v.slc) 31 require.Equal(t, 1, len(*v.slc)) 32 v.Append(&pool, 2) 33 require.NotNil(t, v.slc) 34 require.Equal(t, 2, len(*v.slc)) 35 capacity := cap(*v.slc) 36 37 // Reset the pool and ensure the backing slice is reused. 38 pool.Reset() 39 40 v = pool.Allocate(5) 41 require.NotNil(t, v.arr) 42 require.Equal(t, 0, v.arr.next) 43 require.Equal(t, arraySize, cap(v.arr.arr)) 44 v = pool.Allocate(25) 45 require.NotNil(t, v.slc) 46 require.Equal(t, 0, len(*v.slc)) 47 require.Equal(t, capacity, cap(*v.slc)) 48 } 49 50 func TestAppendAndView(t *testing.T) { 51 pool := NewVarLengthPool[uint64]() 52 t.Run("zero start", func(t *testing.T) { 53 v := pool.Allocate(0) 54 v.Append(&pool, 1) 55 require.Equal(t, []uint64{1}, v.View()) 56 }) 57 t.Run("non zero start", func(t *testing.T) { 58 v := pool.Allocate(10) 59 for i := uint64(0); i < 10; i++ { 60 v.Append(&pool, i) 61 } 62 require.Equal(t, []uint64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, v.View()) 63 for i := uint64(0); i < 20; i++ { 64 v.Append(&pool, i) 65 } 66 require.Equal(t, []uint64{ 67 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 68 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 69 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10, 0x11, 0x12, 0x13, 70 }, v.View()) 71 }) 72 } 73 74 func TestCut(t *testing.T) { 75 pool := NewVarLengthPool[uint64]() 76 v := pool.Allocate(10) 77 for i := uint64(0); i < 10; i++ { 78 v.Append(&pool, i) 79 } 80 v.Cut(5) 81 require.Equal(t, []uint64{0, 1, 2, 3, 4}, v.View()) 82 }