package mmtable
|
|
|
|
import (
|
|
"errors"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestShape(t *testing.T) {
|
|
type TestCase struct {
|
|
in []uint8
|
|
out [3]uint8
|
|
}
|
|
|
|
cases := []TestCase{
|
|
{in: []uint8{}, out: [3]uint8{0, 0, 0}},
|
|
{in: []uint8{0}, out: [3]uint8{0, 0, 0}},
|
|
{in: []uint8{0, 0}, out: [3]uint8{0, 0, 0}},
|
|
{in: []uint8{0, 0, 0}, out: [3]uint8{0, 0, 0}},
|
|
{in: []uint8{0, 0, 0, 0}, out: [3]uint8{0, 0, 0}},
|
|
{in: []uint8{1}, out: [3]uint8{1, 0, 0}},
|
|
{in: []uint8{0, 1}, out: [3]uint8{0, 0, 0}},
|
|
{in: []uint8{1, 2, 3}, out: [3]uint8{1, 2, 3}},
|
|
{in: []uint8{1, 0, 1}, out: [3]uint8{1, 0, 0}},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
out := Shape(tc.in...)
|
|
if !reflect.DeepEqual(out, tc.out) {
|
|
t.Fatal(out, tc.out)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestSchemaCol_validate(t *testing.T) {
|
|
type TestCase struct {
|
|
sc SchemaCol
|
|
err error
|
|
}
|
|
|
|
cases := []TestCase{
|
|
{
|
|
sc: SchemaCol{ElemType: elemTypeMax, Shape: Shape(), Name: "x"},
|
|
err: ErrUnknownElementType,
|
|
}, {
|
|
sc: SchemaCol{
|
|
ElemType: ElemTypeBool,
|
|
Shape: Shape(),
|
|
Name: strings.Repeat("x", ColumnNameMaxLength) + "z",
|
|
},
|
|
err: ErrNameTooLong,
|
|
}, {
|
|
sc: SchemaCol{ElemType: ElemTypeInt8, Shape: Shape(), Name: ""},
|
|
err: ErrNameEmpty,
|
|
}, {
|
|
sc: SchemaCol{ElemType: ElemTypeInt8, Shape: Shape(), Name: "x"},
|
|
err: nil,
|
|
},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
err := tc.sc.validate()
|
|
if !errors.Is(err, tc.err) {
|
|
t.Fatal(tc.sc, tc.err, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestSchema_Validate(t *testing.T) {
|
|
schema := Schema([]SchemaCol{
|
|
{
|
|
ElemType: ElemTypeInt64,
|
|
Shape: Shape(2, 2, 0),
|
|
Name: "col1",
|
|
},
|
|
})
|
|
|
|
if err := schema.Validate(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
schema = append(schema, SchemaCol{
|
|
ElemType: elemTypeMax,
|
|
Shape: Shape(),
|
|
Name: "col2",
|
|
})
|
|
|
|
if err := schema.Validate(); !errors.Is(err, ErrUnknownElementType) {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
schema[1].ElemType = ElemTypeUint32
|
|
schema[1].Name = "col1"
|
|
|
|
if err := schema.Validate(); !errors.Is(err, ErrNameDuplicate) {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestSchema_schemaSize(t *testing.T) {
|
|
schema := Schema([]SchemaCol{
|
|
{
|
|
ElemType: ElemTypeByte,
|
|
Shape: Shape(2, 0, 0),
|
|
Name: "col1",
|
|
}, {
|
|
ElemType: ElemTypeInt32,
|
|
Shape: Shape(2, 2, 2),
|
|
Name: "col2",
|
|
}, {
|
|
ElemType: ElemTypeFloat32,
|
|
Shape: Shape(),
|
|
Name: "col3",
|
|
},
|
|
})
|
|
|
|
expected := 2 + 128*3
|
|
if schema.schemaSize() != expected {
|
|
t.Fatal(schema.schemaSize(), expected)
|
|
}
|
|
}
|
|
|
|
func TestSchema_dataSize(t *testing.T) {
|
|
schema := Schema([]SchemaCol{
|
|
{
|
|
ElemType: ElemTypeByte,
|
|
Shape: Shape(2, 0, 0),
|
|
Name: "col1",
|
|
}, {
|
|
ElemType: ElemTypeInt32,
|
|
Shape: Shape(2, 2, 2),
|
|
Name: "col2",
|
|
}, {
|
|
ElemType: ElemTypeFloat64,
|
|
Shape: Shape(),
|
|
Name: "col3",
|
|
},
|
|
})
|
|
|
|
capacity := 100
|
|
expected := capacity * (2 + 8*4 + 8)
|
|
if schema.dataSize(capacity) != expected {
|
|
t.Fatal(schema.dataSize(capacity), expected)
|
|
}
|
|
}
|
|
|
|
func TestSchema_write_readSchema(t *testing.T) {
|
|
schema := Schema{}
|
|
|
|
out := make([]byte, schema.schemaSize())
|
|
schema.write(out)
|
|
s2, err := readSchema(out)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !reflect.DeepEqual(schema, s2) {
|
|
t.Fatal(schema, s2)
|
|
}
|
|
|
|
schema = append(schema, SchemaCol{
|
|
ElemType: ElemTypeByte,
|
|
Shape: Shape(2, 0, 0),
|
|
Name: "col1",
|
|
})
|
|
|
|
out = make([]byte, schema.schemaSize())
|
|
schema.write(out)
|
|
s2, err = readSchema(out)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !reflect.DeepEqual(schema, s2) {
|
|
t.Fatal(schema, s2)
|
|
}
|
|
|
|
schema = append(schema, SchemaCol{
|
|
ElemType: ElemTypeFloat64,
|
|
Shape: Shape(2, 2, 3),
|
|
Name: "col2",
|
|
})
|
|
|
|
out = make([]byte, schema.schemaSize())
|
|
schema.write(out)
|
|
s2, err = readSchema(out)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !reflect.DeepEqual(schema, s2) {
|
|
t.Fatal(schema, s2)
|
|
}
|
|
|
|
schema = append(schema, SchemaCol{
|
|
ElemType: ElemTypeUint16,
|
|
Shape: Shape(2, 0, 0),
|
|
Name: "col3",
|
|
})
|
|
|
|
out = make([]byte, schema.schemaSize())
|
|
schema.write(out)
|
|
s2, err = readSchema(out)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !reflect.DeepEqual(schema, s2) {
|
|
t.Fatal(schema, s2)
|
|
}
|
|
}
|
|
|
|
func TestReadSchema_ErrCorruptSchema(t *testing.T) {
|
|
schema := Schema([]SchemaCol{
|
|
{
|
|
ElemType: ElemTypeByte,
|
|
Shape: Shape(2, 0, 0),
|
|
Name: "col1",
|
|
},
|
|
})
|
|
|
|
out := make([]byte, schema.schemaSize())
|
|
schema.write(out)
|
|
out = out[:len(out)-1]
|
|
if _, err := readSchema(out); !errors.Is(err, ErrCorruptSchema) {
|
|
t.Fatal(err)
|
|
}
|
|
}
|