blob: 01a5ad56a21fc9b10ba16d64c5b0cce5d2213748 [file] [log] [blame]
// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package iobuf
import (
"fmt"
"testing"
)
func TestAllocatorSmall(t *testing.T) {
pool := NewPool(iobufSize)
salloc := NewAllocator(pool, 0)
const count = 100
var slices [count]*Slice
for i := 0; i != count; i++ {
slices[i] = salloc.Copy([]byte(fmt.Sprintf("slice[%d]", i)))
}
for i := 0; i != count; i++ {
expectEq(t, fmt.Sprintf("slice[%d]", i), string(slices[i].Contents))
slices[i].Release()
}
salloc.Release()
}
func TestAllocatorLarge(t *testing.T) {
pool := NewPool(iobufSize)
salloc := NewAllocator(pool, 0)
const count = 100
var slices [count]*Slice
for i := 0; i != count; i++ {
slices[i] = salloc.Alloc(10000)
copy(slices[i].Contents, []byte(fmt.Sprintf("slice[%d]", i)))
}
for i := 0; i != count; i++ {
expected := fmt.Sprintf("slice[%d]", i)
expectEq(t, expected, string(slices[i].Contents[0:len(expected)]))
slices[i].Release()
}
salloc.Release()
}
// Check that the Allocator is unusable after it is closed.
func TestAllocatorClose(t *testing.T) {
pool := NewPool(iobufSize)
alloc := NewAllocator(pool, 0)
slice := alloc.Alloc(10)
if slice == nil {
t.Fatalf("slice should not be nil")
}
slice.Release()
alloc.Release()
slice = alloc.Alloc(10)
if slice != nil {
t.Errorf("slice should be nil")
}
pool.Close()
}