package mmtable
|
|
|
|
import (
|
|
"errors"
|
|
"math/rand"
|
|
"testing"
|
|
)
|
|
|
|
func TestBufferedSlice_ErrDataTruncated(t *testing.T) {
|
|
v := [][3]float32{}
|
|
et, shape := elemTypeForSlice(&v)
|
|
capacity := 100
|
|
buf := make([]byte, backingDataSize(et, shape, capacity)-1)
|
|
sc := SchemaCol{
|
|
ElemType: et,
|
|
Shape: shape,
|
|
Name: "test",
|
|
}
|
|
|
|
_, _, err := newBufferedSlice(buf, sc, &v, capacity)
|
|
if !errors.Is(err, ErrDataTruncated) {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestBufferedSlice_ErrIncorrectColType(t *testing.T) {
|
|
v := [][3]float32{}
|
|
et, shape := elemTypeForSlice(&v)
|
|
capacity := 100
|
|
buf := make([]byte, backingDataSize(et, shape, capacity))
|
|
sc := SchemaCol{
|
|
ElemType: et,
|
|
Shape: shape,
|
|
Name: "test",
|
|
}
|
|
|
|
v2 := []float32{}
|
|
_, _, err := newBufferedSlice(buf, sc, &v2, capacity)
|
|
if !errors.Is(err, ErrIncorrectColType) {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestBufferedSlice_Simple_Byte(t *testing.T) {
|
|
v := []float32{}
|
|
et, shape := elemTypeForSlice(&v)
|
|
|
|
capacity := 100
|
|
buf := make([]byte, backingDataSize(et, shape, capacity))
|
|
sc := SchemaCol{
|
|
ElemType: et,
|
|
Shape: shape,
|
|
Name: "test",
|
|
}
|
|
|
|
_, n, err := newBufferedSlice(buf, sc, &v, capacity)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if n != len(buf) {
|
|
t.Fatal(n, len(buf))
|
|
}
|
|
|
|
// Modify v.
|
|
v = v[:cap(v)]
|
|
for i := 0; i < len(v); i++ {
|
|
v[i] = float32(i) * 1.8
|
|
}
|
|
|
|
// Create new buffered slice on buf.
|
|
v2 := []float32{}
|
|
_, n, err = newBufferedSlice(buf, sc, &v2, capacity)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if n != len(buf) {
|
|
t.Fatal(n, len(buf))
|
|
}
|
|
|
|
v2 = v2[:len(v)]
|
|
|
|
// Ensure it matches v.
|
|
for i := range v {
|
|
if v[i] != v2[i] || v[i] != float32(i)*1.8 {
|
|
t.Fatal(v[i], v2[i])
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestBufferedSlice_Length(t *testing.T) {
|
|
v := []float32{}
|
|
et, shape := elemTypeForSlice(&v)
|
|
|
|
capacity := 100
|
|
buf := make([]byte, backingDataSize(et, shape, capacity))
|
|
sc := SchemaCol{
|
|
ElemType: et,
|
|
Shape: shape,
|
|
Name: "test",
|
|
}
|
|
|
|
bs, n, err := newBufferedSlice(buf, sc, &v, capacity)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if n != len(buf) {
|
|
t.Fatal(n, len(buf))
|
|
}
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
length := 0
|
|
|
|
v = v[:length]
|
|
if len(v) != length || bs.Len() != length {
|
|
t.Fatal(length, len(v), bs.Len())
|
|
}
|
|
|
|
length = rand.Intn(capacity)
|
|
v = v[:length]
|
|
if len(v) != length || bs.Len() != length {
|
|
t.Fatal(length, len(v), bs.Len())
|
|
}
|
|
|
|
length = rand.Intn(capacity)
|
|
bs.SetLen(length)
|
|
if len(v) != length || bs.Len() != length {
|
|
t.Fatal(length, len(v), bs.Len())
|
|
}
|
|
}
|
|
}
|