mirror of
https://github.com/fumiama/orbyte.git
synced 2026-06-06 02:30:30 +08:00
115 lines
2.4 KiB
Go
115 lines
2.4 KiB
Go
package pbuf
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/rand"
|
|
"encoding/hex"
|
|
"runtime"
|
|
"testing"
|
|
)
|
|
|
|
// TestBytesSlice sometimes fails at first run because
|
|
// GC not collecting all unused items.
|
|
func TestBytesSlice(t *testing.T) {
|
|
for i := 10; i < 4096; i++ {
|
|
b := NewBytes(i)
|
|
if b.Len() != i {
|
|
t.Fatal("index", i, "excpet len", i, "but got", b.Len())
|
|
}
|
|
rand.Read(b.Bytes())
|
|
buf := make([]byte, b.Len())
|
|
copy(buf, b.Bytes())
|
|
// test normal slice
|
|
x := b.SliceFrom(5).SliceTo(i - 5 - 5)
|
|
if !bytes.Equal(buf[5:i-5], x.Bytes()) {
|
|
t.Log("exp:", hex.EncodeToString(buf[5:i-5]))
|
|
t.Log("got:", hex.EncodeToString(x.Bytes()))
|
|
t.Fatal("index", i, "unexpected")
|
|
}
|
|
x.Destroy()
|
|
// test trans slice
|
|
b = b.Trans().SliceFrom(5).SliceTo(i - 5 - 5)
|
|
if !bytes.Equal(buf[5:i-5], b.Bytes()) {
|
|
t.Log("exp:", hex.EncodeToString(buf[5:i-5]))
|
|
t.Log("got:", hex.EncodeToString(b.Bytes()))
|
|
t.Fatal("index", i, "unexpected")
|
|
}
|
|
b.Destroy()
|
|
}
|
|
runtime.GC()
|
|
runtime.Gosched()
|
|
runtime.GC()
|
|
out, in := bufferPool.p.CountItems()
|
|
t.Log(out, in)
|
|
if out != 0 {
|
|
t.Fail()
|
|
}
|
|
}
|
|
|
|
func TestBytesInvolve(t *testing.T) {
|
|
buf := make([]byte, 4096)
|
|
rand.Read(buf)
|
|
for i := 0; i < 4096; i++ {
|
|
b := InvolveBytes(buf[:i]...)
|
|
if b.Len() != i {
|
|
t.Fatal("index", i, "excpet len", i, "but got", b.Len())
|
|
}
|
|
rand.Read(b.Bytes())
|
|
if !bytes.Equal(b.Bytes(), buf[:i]) {
|
|
t.Fatal("index", i, "unexpected")
|
|
}
|
|
b.Destroy()
|
|
}
|
|
runtime.GC()
|
|
out, in := bufferPool.p.CountItems()
|
|
t.Log(out, in)
|
|
if out != 0 {
|
|
t.Fail()
|
|
}
|
|
}
|
|
|
|
func TestBytesParse(t *testing.T) {
|
|
buf := make([]byte, 4096)
|
|
rand.Read(buf)
|
|
for i := 0; i < 4096; i++ {
|
|
b := ParseBytes(buf[:i]...)
|
|
if b.Len() != i {
|
|
t.Fatal("index", i, "excpet len", i, "but got", b.Len())
|
|
}
|
|
if !bytes.Equal(b.Bytes(), buf[:i]) {
|
|
t.Fatal("index", i, "unexpected")
|
|
}
|
|
b.Destroy()
|
|
}
|
|
runtime.GC()
|
|
out, in := bufferPool.p.CountItems()
|
|
t.Log(out, in)
|
|
if out != 0 {
|
|
t.Fail()
|
|
}
|
|
}
|
|
|
|
func TestBytesCopy(t *testing.T) {
|
|
buf := make([]byte, 4096)
|
|
rand.Read(buf)
|
|
for i := 10; i < 4096; i++ {
|
|
b := ParseBytes(buf...).Slice(5, i-5).Copy()
|
|
if b.Len() != i-10 {
|
|
t.Fatal("index", i, "excpet len", i, "but got", b.Len())
|
|
}
|
|
rand.Read(b.Bytes())
|
|
t.Log("org:", hex.EncodeToString(buf[:i]))
|
|
t.Log("new:", hex.EncodeToString(b.Bytes()))
|
|
if bytes.Equal(b.Bytes(), buf[:i]) {
|
|
t.Fatal("index", i, "unexpected")
|
|
}
|
|
b.Destroy()
|
|
}
|
|
runtime.GC()
|
|
out, in := bufferPool.p.CountItems()
|
|
t.Log(out, in)
|
|
if out != 0 {
|
|
t.Fail()
|
|
}
|
|
}
|