blob: dd512e85239a1b954e40164c180a0dfdc89e9e3c [file] [log] [blame]
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package tensor_test
import (
"fmt"
"reflect"
"testing"
"github.com/apache/arrow/go/arrow"
"github.com/apache/arrow/go/arrow/array"
"github.com/apache/arrow/go/arrow/memory"
"github.com/apache/arrow/go/arrow/tensor"
)
{{range .In}}
func TestTensor{{.Name}}(t *testing.T) {
mem := memory.NewCheckedAllocator(memory.NewGoAllocator())
defer mem.AssertSize(t, 0)
bld := array.New{{.Name}}Builder(mem)
defer bld.Release()
raw := []{{.Type}}{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
bld.AppendValues(raw, nil)
arr := bld.New{{.Name}}Array()
defer arr.Release()
var (
shape = []int64{2, 5}
names = []string{"x", "y"}
bw = int64(arrow.PrimitiveTypes.{{.Name}}.(arrow.FixedWidthDataType).BitWidth()) / 8
)
tsr := tensor.New(arr.Data(), shape, nil, names).(*tensor.{{.Name}})
defer tsr.Release()
tsr.Retain()
tsr.Release()
if got, want := tsr.Len(), 10; got != want {
t.Fatalf("invalid length: got=%d, want=%d", got, want)
}
if got, want := tsr.Shape(), shape; !reflect.DeepEqual(got, want) {
t.Fatalf("invalid shape: got=%v, want=%v", got, want)
}
if got, want := tsr.Strides(), []int64{5*bw, 1*bw}; !reflect.DeepEqual(got, want) {
t.Fatalf("invalid strides: got=%v, want=%v", got, want)
}
if got, want := tsr.NumDims(), 2; got != want {
t.Fatalf("invalid dims: got=%d, want=%d", got, want)
}
for i, name := range names {
if got, want := tsr.DimName(i), name; got != want {
t.Fatalf("invalid dim-name[%d]: got=%q, want=%q", i, got, want)
}
}
if got, want := tsr.DataType(), arr.DataType(); got != want {
t.Fatalf("invalid data-type: got=%q, want=%q", got.Name(), want.Name())
}
if got, want := tsr.Data(), arr.Data(); got != want {
t.Fatalf("invalid data: got=%v, want=%v", got, want)
}
if tsr.IsMutable() {
t.Fatalf("should not be mutable")
}
if !tsr.IsContiguous() {
t.Fatalf("should be contiguous")
}
if !tsr.IsRowMajor() || tsr.IsColMajor() {
t.Fatalf("should be row-major")
}
if got, want := tsr.{{.Name}}Values(), raw; !reflect.DeepEqual(got, want) {
t.Fatalf("invalid backing array: got=%v, want=%v", got, want)
}
for _, tc := range []struct {
i []int64
v {{.Type}}
}{
{i: []int64{0, 0}, v: 1},
{i: []int64{0, 1}, v: 2},
{i: []int64{0, 2}, v: 3},
{i: []int64{0, 3}, v: 4},
{i: []int64{0, 4}, v: 5},
{i: []int64{1, 0}, v: 6},
{i: []int64{1, 1}, v: 7},
{i: []int64{1, 2}, v: 8},
{i: []int64{1, 3}, v: 9},
{i: []int64{1, 4}, v: 10},
} {
t.Run(fmt.Sprintf("%v", tc.i), func(t *testing.T) {
got := tsr.Value(tc.i)
if got != tc.v {
t.Fatalf("arr[%v]: got=%v, want=%v", tc.i, got, tc.v)
}
})
}
}
{{end}}